import.rb revision b01557b07aa1bc83ca1ce43f91e65c9112330306
transaction do
ontologies_count = 0
versions = []
root = h
},
ontologies_count += 1
if distributed?
# generate IRI for sub-ontology
# find or create sub-ontology by IRI
if ontology.nil?
without_protection: true)
self.children << ontology
end
version = ontology.versions.build
version.user = user
version.code_reference = code_position_for(ontology.name, code_doc)
versions << version
else
raise "more than one ontology found" if ontologies_count > 1
ontology = self
end
if h['language']
ontology.language = Language.where(:iri => "http://purl.net/dol/language/#{h['language']}")
.first_or_create(user: user, name: h['language'])
end
if h['logic']
ontology.logic = Logic.where(:iri => "http://purl.net/dol/logics/#{h['logic']}")
.first_or_create(user: user, name: h['logic'])
end
ontology.entities_count = 0
ontology.sentences_count = 0
},
ontology_end: Proc.new {
# remove outdated sentences and entities
conditions = ['updated_at < ?', now]
ontology.entities.where(conditions).destroy_all
ontology.sentences.where(conditions).delete_all
ontology.save!
},
symbol: Proc.new { |h|
ontology.entities.update_or_create_from_hash(h, now)
ontology.entities_count += 1
},
axiom: Proc.new { |h|
ontology.sentences.update_or_create_from_hash(h, now)
ontology.sentences_count += 1
},
link: Proc.new { |h|
self.links.update_or_create_from_hash(h, user, now)
}
save!
versions.each { |version| version.save! }
end
end
def import_xml_from_file(path, code_path, user)
code_io = code_path ? File.open(code_path) : nil
import_xml File.open(path), code_io, user
end
def import_latest_version(user)
latest_version = versions.last
return if latest_version.nil?
import_xml_from_file latest_version.xml_path,
latest_version.code_reference_path, user
end
def code_position_for(ontology_name, code_doc)
return nil unless code_doc
elements = code_doc.xpath("//*[contains(@name, '##{ontology_name}')]")
code_range = elements.first.try(:attr, "range")
code_position = code_reference_from_range(code_range)
end
def code_reference_from_range(range)
return if range.nil?
match = range.match( %r{
(?<begin_line>\d+)\.
(?<begin_column>\d+)
-
(?<end_line>\d+)\.
(?<end_column>\d+)}x)
if match
reference = CodeReference.new(begin_line: match[:begin_line].to_i,
begin_column: match[:begin_column].to_i,
end_line: match[:end_line].to_i,
end_column: match[:end_column].to_i)
end
end
end