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