symbols.rb revision 2218ff30ec39253b75bd6cede5fb7683b5f4c503
642N/Aclass Ontology
642N/A module Symbols
642N/A extend ActiveSupport::Concern
642N/A
642N/A included do
642N/A has_many :symbols,
642N/A autosave: false,
642N/A class_name: 'OntologyMember::Symbol',
642N/A extend: Methods
642N/A end
642N/A
642N/A module Methods
642N/A def update_or_create_from_hash(hash, timestamp = Time.now)
642N/A raise ArgumentError, 'No hash given.' unless hash.is_a? Hash
642N/A
642N/A e = where(text: hash['text']).first_or_initialize
642N/A
642N/A e.ontology = @association.owner
642N/A e.range = hash['range']
642N/A e.updated_at = timestamp
642N/A
642N/A unless hash['name'] || hash['kind']
3143N/A Rails.logger.warn(
3158N/A "Using work-around to determine symbol name and kind: #{e.inspect}")
3143N/A
642N/A if e2 = Symbol.where(text: hash['text']).first
3143N/A e.name = e2.name
642N/A e.kind = e2.kind
642N/A else
642N/A e.name = e.text
642N/A e.kind = 'Undefined'
642N/A end
642N/A else
642N/A e.name = hash['name']
642N/A e.kind = hash['kind']
642N/A end
642N/A
3234N/A e.iri = hash['iri']
642N/A e.label = hash['label']
642N/A
642N/A sep = '//'
642N/A locid_portion =
642N/A if e.name.include?('://')
642N/A if e.label
642N/A e.label
642N/A else
642N/A portion = e.name.end_with?('>') ? e.name[0..-2] : e.name
642N/A portion = portion.split('#', 2).last
642N/A end
642N/A else
642N/A e.name
642N/A end
642N/A e.locid = "#{e.ontology.locid}#{sep}#{locid_portion}"
642N/A
642N/A if e.range.to_s.include?(':')
3143N/A # remove path from range
642N/A # Examples/Reichel:28.9 -> 28.9
3143N/A e.range = e.range.split(':', 2).last
3143N/A end
642N/A
642N/A e.ontology.symbols << e if e.id.nil?
3143N/A e.save!
3194N/A e
3143N/A end
3143N/A end
642N/A
642N/A def delete_edges
3143N/A %i[parent_id child_id].each do |key|
3194N/A EEdge.where(key => symbols.where(kind: 'Class')).delete_all
3143N/A end
3143N/A end
642N/A
642N/A def create_symbol_tree
642N/A raise StandardError.new('Ontology is not OWL') unless owl?
642N/A
642N/A # Delete previous set of categories
642N/A delete_edges
642N/A subclasses =
642N/A sentences.where("text LIKE '%SubClassOf%'").select do |sentence|
642N/A sentence.text.split(' ').size == 4
642N/A end
642N/A transaction requires_new: true do
642N/A subclasses.each do |s|
642N/A c1, c2 = s.hierarchical_class_names
642N/A
642N/A unless c1 == 'Thing' || c2 == 'Thing'
642N/A child_id = symbols.where('name = ? OR iri = ?', c1, c1).first.id
3234N/A parent_id = symbols.where('name = ? OR iri = ?', c2, c2).first.id
642N/A
3234N/A EEdge.create! child_id: child_id, parent_id: parent_id
642N/A if EEdge.where(child_id: child_id, parent_id: parent_id).first.nil?
642N/A raise StandardError.new('Circle detected')
3234N/A end
642N/A end
3234N/A end
642N/A end
642N/A end
642N/A end
642N/Aend
642N/A