links.rb revision a686bfa88234792b2dd2515da6a60aa110380916
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenmodule Ontology::Links
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen extend ActiveSupport::Concern
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen included do
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen has_many :links,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen autosave: false,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen extend: Methods
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen end
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen module Methods
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen def iri_for_child(*args)
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen proxy_association.owner.iri_for_child(*args)
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen end
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen def determine_link_type(typename)
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen raise "link type missing" if typename.blank?
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen kind = Link::KINDS_MAPPING[typename]
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen if kind.nil?
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen key = Link::KINDS_MAPPING.keys.find {|k| typename.include?(k) }
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen kind = Link::KINDS_MAPPING[key]
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen end
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen if kind.nil?
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen kind = Link::KINDS.find {|k| typename.downcase.include?(k) }
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen end
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen kind || Link::DEFAULT_LINK_KIND
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen end
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen def update_or_create_from_hash(hash, user, timestamp = Time.now)
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen raise ArgumentError, 'No hash given.' unless hash.is_a? Hash
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen # hash['name'] # maybe nil, in this case, we need to generate a name
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen link_iri = iri_for_child(hash['name'] || hash['linkid'])
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen link_name = hash['name']
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen source_iri = hash['source_iri'] || iri_for_child(hash['source'])
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen target_iri = hash['target_iri'] || iri_for_child(hash['target'])
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen source = Ontology.find_with_iri(source_iri) || (raise ArgumentError,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen "source #{Settings.OMS} not found: #{source_iri}")
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen target = Ontology.find_with_iri(target_iri) || (raise ArgumentError,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen "target #{Settings.OMS} not found: #{target_iri}")
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen # linktype
9aa52288a4b53186d81b0ec9afa7d9e0a8ee8753Timo Sirainen linktype = hash['type']
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen kind = determine_link_type(linktype)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
e06c0b65c16ccce69bbee009ead14d7d3d17a256Timo Sirainen # morphism
7a6b45405fb1544ac476e6eb1402a70cc1ddcdcfTimo Sirainen gmorphism = hash['morphism']
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen raise "gmorphism missing" if gmorphism.blank?
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen gmorphism = 'http://purl.net/dol/translations/' + gmorphism unless gmorphism.include?('://')
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen # finally, create or update the link
02b79f9c2636da1829eee5b92753602bba8b67edTimo Sirainen link = find_or_initialize_by_iri(link_iri)
02b79f9c2636da1829eee5b92753602bba8b67edTimo Sirainen link.attributes = {
02b79f9c2636da1829eee5b92753602bba8b67edTimo Sirainen name: link_name,
02b79f9c2636da1829eee5b92753602bba8b67edTimo Sirainen source_id: source.id,
02b79f9c2636da1829eee5b92753602bba8b67edTimo Sirainen target_id: target.id,
02b79f9c2636da1829eee5b92753602bba8b67edTimo Sirainen kind: kind,
02b79f9c2636da1829eee5b92753602bba8b67edTimo Sirainen theorem: linktype.include?("Thm"),
02b79f9c2636da1829eee5b92753602bba8b67edTimo Sirainen proven: linktype.include?("Proven"),
02b79f9c2636da1829eee5b92753602bba8b67edTimo Sirainen local: linktype.include?("Local"),
02b79f9c2636da1829eee5b92753602bba8b67edTimo Sirainen inclusion: linktype.include?("Inc"),
02b79f9c2636da1829eee5b92753602bba8b67edTimo Sirainen }
02b79f9c2636da1829eee5b92753602bba8b67edTimo Sirainen link.updated_at = timestamp
659fe5d24825b160cae512538088020d97a60239Timo Sirainen link.save!
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen link_version = LinkVersion.create(link: link,
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen source: source.current_version,
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen target: target.current_version)
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen link.versions << link_version
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen link.update_version!(to: link_version)
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen # entity mapping
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen if hash["map"]
445f9e31c6c3aa6c0a72be8565da8f6e594d24fbTimo Sirainen source = Entity.where(text: hash["map"].first["text"],ontology_id: link.source.id).first
445f9e31c6c3aa6c0a72be8565da8f6e594d24fbTimo Sirainen target = Entity.where(text: hash["map"].second["text"], ontology_id: link.target.id).first
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen entity_mapping = EntityMapping.
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen where(source_id: source, target_id: target, link_id: link).
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen first_or_create!
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen end
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen end
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen end
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainenend
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen