ontology.rb revision c361c823f26bbaf7682313664825fa8e67059f94
6bdda696b3ea703c47e87fea61017ec655f91d92ndclass Ontology < ActiveRecord::Base
6bdda696b3ea703c47e87fea61017ec655f91d92nd
6bdda696b3ea703c47e87fea61017ec655f91d92nd # Ontohub Library Includes
6bdda696b3ea703c47e87fea61017ec655f91d92nd include Commentable
6bdda696b3ea703c47e87fea61017ec655f91d92nd include Metadatable
6bdda696b3ea703c47e87fea61017ec655f91d92nd
6bdda696b3ea703c47e87fea61017ec655f91d92nd # Ontology Model Includes
6bdda696b3ea703c47e87fea61017ec655f91d92nd include GraphStructures::SpecificFetchers::Mappings
6bdda696b3ea703c47e87fea61017ec655f91d92nd include IRIUrlBuilder::Includeable
6bdda696b3ea703c47e87fea61017ec655f91d92nd include Ontology::AssociationsAndAttributes
6bdda696b3ea703c47e87fea61017ec655f91d92nd include Ontology::Categories
6bdda696b3ea703c47e87fea61017ec655f91d92nd include Ontology::ClassMethodsAndScopes
6bdda696b3ea703c47e87fea61017ec655f91d92nd include Ontology::Distributed
6bdda696b3ea703c47e87fea61017ec655f91d92nd include Ontology::FileExtensions
6bdda696b3ea703c47e87fea61017ec655f91d92nd include Ontology::Import
6bdda696b3ea703c47e87fea61017ec655f91d92nd include Ontology::Mappings
6bdda696b3ea703c47e87fea61017ec655f91d92nd include Ontology::Oops
6bdda696b3ea703c47e87fea61017ec655f91d92nd include Ontology::OwlClasses
6bdda696b3ea703c47e87fea61017ec655f91d92nd include Ontology::Searching
6bdda696b3ea703c47e87fea61017ec655f91d92nd include Ontology::Sentences
6bdda696b3ea703c47e87fea61017ec655f91d92nd include Ontology::States
6bdda696b3ea703c47e87fea61017ec655f91d92nd include Ontology::Symbols
6bdda696b3ea703c47e87fea61017ec655f91d92nd include Ontology::Validations
6bdda696b3ea703c47e87fea61017ec655f91d92nd include Ontology::Versions
6bdda696b3ea703c47e87fea61017ec655f91d92nd
6bdda696b3ea703c47e87fea61017ec655f91d92nd # Multiple Class Features
6bdda696b3ea703c47e87fea61017ec655f91d92nd include Aggregatable
6bdda696b3ea703c47e87fea61017ec655f91d92nd
6bdda696b3ea703c47e87fea61017ec655f91d92nd class Ontology::DeleteError < StandardError; end
6bdda696b3ea703c47e87fea61017ec655f91d92nd
6bdda696b3ea703c47e87fea61017ec655f91d92nd delegate :permission?, to: :repository
6bdda696b3ea703c47e87fea61017ec655f91d92nd delegate :unproven_theorems, to: :current_version
6bdda696b3ea703c47e87fea61017ec655f91d92nd
6bdda696b3ea703c47e87fea61017ec655f91d92nd strip_attributes :only => [:name, :iri]
6bdda696b3ea703c47e87fea61017ec655f91d92nd
6bdda696b3ea703c47e87fea61017ec655f91d92nd def repository
6bdda696b3ea703c47e87fea61017ec655f91d92nd @repository ||= Repository.find(repository_id)
6bdda696b3ea703c47e87fea61017ec655f91d92nd end
6bdda696b3ea703c47e87fea61017ec655f91d92nd
6bdda696b3ea703c47e87fea61017ec655f91d92nd def generate_name(name)
6bdda696b3ea703c47e87fea61017ec655f91d92nd match = name.match(%r{
6bdda696b3ea703c47e87fea61017ec655f91d92nd \A
6bdda696b3ea703c47e87fea61017ec655f91d92nd .+?
6bdda696b3ea703c47e87fea61017ec655f91d92nd :// # A uri has a separation between schema and hierarchy
6bdda696b3ea703c47e87fea61017ec655f91d92nd .+
6bdda696b3ea703c47e87fea61017ec655f91d92nd (?:/|\#)
6bdda696b3ea703c47e87fea61017ec655f91d92nd (?<filename>[^/]+) # Match filename after a slash/hash
6bdda696b3ea703c47e87fea61017ec655f91d92nd \z
6bdda696b3ea703c47e87fea61017ec655f91d92nd }x)
6bdda696b3ea703c47e87fea61017ec655f91d92nd if match
6bdda696b3ea703c47e87fea61017ec655f91d92nd filename = match[:filename].sub(/\.[\w\d]+\z/, '')
6bdda696b3ea703c47e87fea61017ec655f91d92nd capitalized_name = filename.split(/[_ ]/).map(&:capitalize).join(' ')
6bdda696b3ea703c47e87fea61017ec655f91d92nd else
6bdda696b3ea703c47e87fea61017ec655f91d92nd name
6bdda696b3ea703c47e87fea61017ec655f91d92nd end
6bdda696b3ea703c47e87fea61017ec655f91d92nd end
6bdda696b3ea703c47e87fea61017ec655f91d92nd
6bdda696b3ea703c47e87fea61017ec655f91d92nd def iri_for_child(child_name)
6bdda696b3ea703c47e87fea61017ec655f91d92nd child_name = child_name[1..-2] if child_name[0] == '<'
6bdda696b3ea703c47e87fea61017ec655f91d92nd child_name.include?("://") ? child_name : "#{iri}?#{child_name}"
ac7985784d08a3655291f24f711812b4d8b1cbcffuankg end
6bdda696b3ea703c47e87fea61017ec655f91d92nd
6bdda696b3ea703c47e87fea61017ec655f91d92nd def locid_for_child(child_name)
6bdda696b3ea703c47e87fea61017ec655f91d92nd child_name = child_name[1..-2] if child_name[0] == '<'
6bdda696b3ea703c47e87fea61017ec655f91d92nd child_name.include?('://') ? child_name : "#{locid}//#{child_name}"
6bdda696b3ea703c47e87fea61017ec655f91d92nd end
6bdda696b3ea703c47e87fea61017ec655f91d92nd
6bdda696b3ea703c47e87fea61017ec655f91d92nd def is?(logic_name)
6bdda696b3ea703c47e87fea61017ec655f91d92nd self.logic ? (self.logic.name == logic_name) : false
6bdda696b3ea703c47e87fea61017ec655f91d92nd end
6bdda696b3ea703c47e87fea61017ec655f91d92nd
6bdda696b3ea703c47e87fea61017ec655f91d92nd def owl?
6bdda696b3ea703c47e87fea61017ec655f91d92nd self.is?('OWL') || self.is?('OWL2')
6bdda696b3ea703c47e87fea61017ec655f91d92nd end
ac7985784d08a3655291f24f711812b4d8b1cbcffuankg
6bdda696b3ea703c47e87fea61017ec655f91d92nd def to_s
6bdda696b3ea703c47e87fea61017ec655f91d92nd name? ? name : iri
6bdda696b3ea703c47e87fea61017ec655f91d92nd end
6bdda696b3ea703c47e87fea61017ec655f91d92nd
6bdda696b3ea703c47e87fea61017ec655f91d92nd # Title for mappings
6bdda696b3ea703c47e87fea61017ec655f91d92nd def title
6bdda696b3ea703c47e87fea61017ec655f91d92nd name? ? iri : nil
6bdda696b3ea703c47e87fea61017ec655f91d92nd end
6bdda696b3ea703c47e87fea61017ec655f91d92nd
6bdda696b3ea703c47e87fea61017ec655f91d92nd def is_imported?
6bdda696b3ea703c47e87fea61017ec655f91d92nd import_mappings.present?
6bdda696b3ea703c47e87fea61017ec655f91d92nd end
6bdda696b3ea703c47e87fea61017ec655f91d92nd
6bdda696b3ea703c47e87fea61017ec655f91d92nd def is_imported_from_other_repository?
6bdda696b3ea703c47e87fea61017ec655f91d92nd import_mappings_from_other_repositories.present?
6bdda696b3ea703c47e87fea61017ec655f91d92nd end
6bdda696b3ea703c47e87fea61017ec655f91d92nd
6bdda696b3ea703c47e87fea61017ec655f91d92nd def imported_by
6bdda696b3ea703c47e87fea61017ec655f91d92nd import_mappings.map(&:source)
6bdda696b3ea703c47e87fea61017ec655f91d92nd end
6bdda696b3ea703c47e87fea61017ec655f91d92nd
ac7985784d08a3655291f24f711812b4d8b1cbcffuankg def destroy_with_parent(user)
6bdda696b3ea703c47e87fea61017ec655f91d92nd if parent
6bdda696b3ea703c47e87fea61017ec655f91d92nd repository.delete_file(parent.path, user,
ac7985784d08a3655291f24f711812b4d8b1cbcffuankg "Delete #{Settings.OMS} #{parent}") do
6bdda696b3ea703c47e87fea61017ec655f91d92nd parent.destroy
6bdda696b3ea703c47e87fea61017ec655f91d92nd end
6bdda696b3ea703c47e87fea61017ec655f91d92nd else
6bdda696b3ea703c47e87fea61017ec655f91d92nd repository.delete_file(path, user,
6bdda696b3ea703c47e87fea61017ec655f91d92nd "Delete #{Settings.OMS} #{self}") do
6bdda696b3ea703c47e87fea61017ec655f91d92nd destroy
6bdda696b3ea703c47e87fea61017ec655f91d92nd end
6bdda696b3ea703c47e87fea61017ec655f91d92nd end
6bdda696b3ea703c47e87fea61017ec655f91d92nd end
6bdda696b3ea703c47e87fea61017ec655f91d92nd
6bdda696b3ea703c47e87fea61017ec655f91d92nd def destroy
ac7985784d08a3655291f24f711812b4d8b1cbcffuankg raise Ontology::DeleteError unless can_be_deleted?
6bdda696b3ea703c47e87fea61017ec655f91d92nd super
6bdda696b3ea703c47e87fea61017ec655f91d92nd end
ac7985784d08a3655291f24f711812b4d8b1cbcffuankg
6bdda696b3ea703c47e87fea61017ec655f91d92nd def can_be_deleted?
6bdda696b3ea703c47e87fea61017ec655f91d92nd if repository.is_destroying
6bdda696b3ea703c47e87fea61017ec655f91d92nd can_be_deleted_with_whole_repository?
6bdda696b3ea703c47e87fea61017ec655f91d92nd else
6bdda696b3ea703c47e87fea61017ec655f91d92nd can_be_deleted_alone?
6bdda696b3ea703c47e87fea61017ec655f91d92nd end
6bdda696b3ea703c47e87fea61017ec655f91d92nd end
6bdda696b3ea703c47e87fea61017ec655f91d92nd
6bdda696b3ea703c47e87fea61017ec655f91d92nd def can_be_deleted_alone?
6bdda696b3ea703c47e87fea61017ec655f91d92nd !is_imported?
6bdda696b3ea703c47e87fea61017ec655f91d92nd end
6bdda696b3ea703c47e87fea61017ec655f91d92nd
6bdda696b3ea703c47e87fea61017ec655f91d92nd def can_be_deleted_with_whole_repository?
6bdda696b3ea703c47e87fea61017ec655f91d92nd !is_imported_from_other_repository?
6bdda696b3ea703c47e87fea61017ec655f91d92nd end
6bdda696b3ea703c47e87fea61017ec655f91d92nd
6bdda696b3ea703c47e87fea61017ec655f91d92nd def imported_ontologies
6bdda696b3ea703c47e87fea61017ec655f91d92nd fetch_mappings_by_kind(self, 'import')
6bdda696b3ea703c47e87fea61017ec655f91d92nd end
6bdda696b3ea703c47e87fea61017ec655f91d92nd
6bdda696b3ea703c47e87fea61017ec655f91d92nd def contains_logic_translations?
6bdda696b3ea703c47e87fea61017ec655f91d92nd query, args = contains_logic_translations_query(self)
6bdda696b3ea703c47e87fea61017ec655f91d92nd pluck_select([query, *args], :logically_translated).size > 1
6bdda696b3ea703c47e87fea61017ec655f91d92nd end
6bdda696b3ea703c47e87fea61017ec655f91d92nd
ac7985784d08a3655291f24f711812b4d8b1cbcffuankg def direct_imported_ontologies
6bdda696b3ea703c47e87fea61017ec655f91d92nd ontology_ids = Mapping.where(target_id: self, kind: 'import').
6bdda696b3ea703c47e87fea61017ec655f91d92nd pluck(:source_id)
ac7985784d08a3655291f24f711812b4d8b1cbcffuankg Ontology.where(id: ontology_ids)
6bdda696b3ea703c47e87fea61017ec655f91d92nd end
6bdda696b3ea703c47e87fea61017ec655f91d92nd
6bdda696b3ea703c47e87fea61017ec655f91d92nd def combined_sentences
ac7985784d08a3655291f24f711812b4d8b1cbcffuankg affected_ontology_ids = [self.id] + imported_ontologies.pluck(:id)
6bdda696b3ea703c47e87fea61017ec655f91d92nd Sentence.where(ontology_id: affected_ontology_ids)
6bdda696b3ea703c47e87fea61017ec655f91d92nd end
6bdda696b3ea703c47e87fea61017ec655f91d92nd
6bdda696b3ea703c47e87fea61017ec655f91d92nd # list all sentences defined on this ontology,
6bdda696b3ea703c47e87fea61017ec655f91d92nd # those who are self defined and those which
6bdda696b3ea703c47e87fea61017ec655f91d92nd # are imported (ImpAxioms)
6bdda696b3ea703c47e87fea61017ec655f91d92nd def all_sentences
6bdda696b3ea703c47e87fea61017ec655f91d92nd Sentence.unscoped.
6bdda696b3ea703c47e87fea61017ec655f91d92nd where(ontology_id: self).
6bdda696b3ea703c47e87fea61017ec655f91d92nd where('imported = ? OR imported = ?', true, false)
6bdda696b3ea703c47e87fea61017ec655f91d92nd end
6bdda696b3ea703c47e87fea61017ec655f91d92nd
6bdda696b3ea703c47e87fea61017ec655f91d92nd def imported_sentences
6bdda696b3ea703c47e87fea61017ec655f91d92nd Sentence.unscoped.
6bdda696b3ea703c47e87fea61017ec655f91d92nd where(ontology_id: self).
6bdda696b3ea703c47e87fea61017ec655f91d92nd where('imported = ?', true)
6bdda696b3ea703c47e87fea61017ec655f91d92nd end
6bdda696b3ea703c47e87fea61017ec655f91d92nd
6bdda696b3ea703c47e87fea61017ec655f91d92nd def basepath
6bdda696b3ea703c47e87fea61017ec655f91d92nd has_versions? ? current_version.basepath : read_attribute(:basepath)
6bdda696b3ea703c47e87fea61017ec655f91d92nd end
6bdda696b3ea703c47e87fea61017ec655f91d92nd
6bdda696b3ea703c47e87fea61017ec655f91d92nd def file_extension
6bdda696b3ea703c47e87fea61017ec655f91d92nd has_versions? ? current_version.file_extension : read_attribute(:file_extension)
6bdda696b3ea703c47e87fea61017ec655f91d92nd end
6bdda696b3ea703c47e87fea61017ec655f91d92nd
6bdda696b3ea703c47e87fea61017ec655f91d92nd def path
ac7985784d08a3655291f24f711812b4d8b1cbcffuankg "#{basepath}#{file_extension}"
6bdda696b3ea703c47e87fea61017ec655f91d92nd end
6bdda696b3ea703c47e87fea61017ec655f91d92nd
6bdda696b3ea703c47e87fea61017ec655f91d92nd def has_versions?
6bdda696b3ea703c47e87fea61017ec655f91d92nd current_version.present?
6bdda696b3ea703c47e87fea61017ec655f91d92nd end
6bdda696b3ea703c47e87fea61017ec655f91d92nd
6bdda696b3ea703c47e87fea61017ec655f91d92nd def file_in_repository
6bdda696b3ea703c47e87fea61017ec655f91d92nd repository.get_file(path)
6bdda696b3ea703c47e87fea61017ec655f91d92nd end
6bdda696b3ea703c47e87fea61017ec655f91d92nd
6bdda696b3ea703c47e87fea61017ec655f91d92nd # Uses where in order to force a Relation as a result
6bdda696b3ea703c47e87fea61017ec655f91d92nd def formality_levels
6bdda696b3ea703c47e87fea61017ec655f91d92nd FormalityLevel.joins(:ontologies).
6bdda696b3ea703c47e87fea61017ec655f91d92nd where(ontologies: {id: self})
6bdda696b3ea703c47e87fea61017ec655f91d92nd end
ac7985784d08a3655291f24f711812b4d8b1cbcffuankg
6bdda696b3ea703c47e87fea61017ec655f91d92nd def versioned_locid
6bdda696b3ea703c47e87fea61017ec655f91d92nd current_version.locid
6bdda696b3ea703c47e87fea61017ec655f91d92nd end
6bdda696b3ea703c47e87fea61017ec655f91d92nd
6bdda696b3ea703c47e87fea61017ec655f91d92nd protected
6bdda696b3ea703c47e87fea61017ec655f91d92nd
6bdda696b3ea703c47e87fea61017ec655f91d92nd def import_mappings
6bdda696b3ea703c47e87fea61017ec655f91d92nd Mapping.where(source_id: id, kind: 'import')
6bdda696b3ea703c47e87fea61017ec655f91d92nd end
6bdda696b3ea703c47e87fea61017ec655f91d92nd
6bdda696b3ea703c47e87fea61017ec655f91d92nd def import_mappings_from_other_repositories
6bdda696b3ea703c47e87fea61017ec655f91d92nd import_mappings.select { |l| l.target.repository != repository }
6bdda696b3ea703c47e87fea61017ec655f91d92nd end
6bdda696b3ea703c47e87fea61017ec655f91d92ndend
6bdda696b3ea703c47e87fea61017ec655f91d92nd