Module: Opencdd::Database::Queries
- Included in:
- Opencdd::Database
- Defined in:
- lib/opencdd/database/queries.rb
Overview
Graph queries: find, coerce, powertype API, tree walkers, property/class/value-list lookups, relation queries.
Instance Method Summary collapse
-
#categorical_classes ⇒ Object
── Powertype accessors (4-layer ontology) ──────────────.
-
#class_tree(fields: Opencdd::ClassTree::DEFAULT_FIELDS) ⇒ Object
── Tree walkers ─────────────────────────────────────────.
- #classes_with_property(property) ⇒ Object
- #coerce_entity(value) ⇒ Object
- #coerce_irdi(value) ⇒ Object
- #composition_tree(klass, max_depth: 10) ⇒ Object
- #effective_properties ⇒ Object
- #find(irdi) ⇒ Object
- #find_all_by_code(code) ⇒ Object
- #find_by_code(code) ⇒ Object
- #find_by_name(name, type: nil, lang: :en) ⇒ Object
- #functions_involving(property) ⇒ Object
- #instances_of(categorical_klass) ⇒ Object
-
#properties_of(klass) ⇒ Object
── Property / class / value-list lookups ────────────────.
- #relation_tree(root = nil, max_depth: 10) ⇒ Object
- #relations_for(domain: nil, codomain: nil) ⇒ Object
- #resolve_reference(ref) ⇒ Object
- #root_classes ⇒ Object
- #terms_of(value_list) ⇒ Object
- #valid_class_reference?(categorical_klass, value) ⇒ Boolean
- #value_list_identifier_of(property) ⇒ Object
- #value_list_of(property) ⇒ Object
Instance Method Details
#categorical_classes ⇒ Object
── Powertype accessors (4-layer ontology) ──────────────
75 76 77 |
# File 'lib/opencdd/database/queries.rb', line 75 def categorical_classes classes.select(&:powertype?) end |
#class_tree(fields: Opencdd::ClassTree::DEFAULT_FIELDS) ⇒ Object
── Tree walkers ─────────────────────────────────────────
93 94 95 |
# File 'lib/opencdd/database/queries.rb', line 93 def class_tree(fields: Opencdd::ClassTree::DEFAULT_FIELDS) Opencdd::ClassTree.new(self, fields: fields) end |
#classes_with_property(property) ⇒ Object
117 118 119 120 121 |
# File 'lib/opencdd/database/queries.rb', line 117 def classes_with_property(property) irdi = coerce_irdi(property) return [] unless irdi @class_by_property_irdi&.fetch(irdi, []) || [] end |
#coerce_entity(value) ⇒ Object
56 57 58 59 60 |
# File 'lib/opencdd/database/queries.rb', line 56 def coerce_entity(value) return nil if value.nil? return value if value.is_a?(Opencdd::Entity) find(value) end |
#coerce_irdi(value) ⇒ Object
62 63 64 65 66 67 |
# File 'lib/opencdd/database/queries.rb', line 62 def coerce_irdi(value) return nil if value.nil? return value.irdi if value.is_a?(Opencdd::Entity) return value if value.is_a?(Opencdd::IRDI) Opencdd::IRDI.parse(value.to_s) end |
#composition_tree(klass, max_depth: 10) ⇒ Object
101 102 103 |
# File 'lib/opencdd/database/queries.rb', line 101 def composition_tree(klass, max_depth: 10) Opencdd::CompositionTree.new(self).for(klass, max_depth: max_depth) end |
#effective_properties ⇒ Object
97 98 99 |
# File 'lib/opencdd/database/queries.rb', line 97 def effective_properties @effective_properties ||= Opencdd::EffectiveProperties.new(self) end |
#find(irdi) ⇒ Object
30 31 32 33 34 35 |
# File 'lib/opencdd/database/queries.rb', line 30 def find(irdi) return nil if irdi.nil? key = Opencdd::IRDI === irdi ? irdi : Opencdd::IRDI.parse(irdi) return nil unless key @entities_by_irdi[key] end |
#find_all_by_code(code) ⇒ Object
43 44 45 |
# File 'lib/opencdd/database/queries.rb', line 43 def find_all_by_code(code) @entities_by_code[code.to_s].dup end |
#find_by_code(code) ⇒ Object
37 38 39 40 41 |
# File 'lib/opencdd/database/queries.rb', line 37 def find_by_code(code) matches = @entities_by_code[code.to_s] return nil if matches.empty? matches.first end |
#find_by_name(name, type: nil, lang: :en) ⇒ Object
47 48 49 50 51 52 53 54 |
# File 'lib/opencdd/database/queries.rb', line 47 def find_by_name(name, type: nil, lang: :en) pools = type ? [@entities_by_type[type]].compact : @entities_by_type.values pools.each do |pool| hit = pool.find { |e| e.preferred_name(lang).to_s.casecmp(name.to_s).zero? } return hit if hit end nil end |
#functions_involving(property) ⇒ Object
162 163 164 165 166 167 168 |
# File 'lib/opencdd/database/queries.rb', line 162 def functions_involving(property) irdi = coerce_irdi(property) return [] unless irdi relations.select do |r| r.function? && (r.domain_irdis.include?(irdi) || r.codomain_irdi == irdi) end end |
#instances_of(categorical_klass) ⇒ Object
79 80 81 82 83 |
# File 'lib/opencdd/database/queries.rb', line 79 def instances_of(categorical_klass) k = coerce_entity(categorical_klass) return [] unless k.is_a?(Opencdd::Klass) k.categorical_instances(self) end |
#properties_of(klass) ⇒ Object
── Property / class / value-list lookups ────────────────
111 112 113 114 115 |
# File 'lib/opencdd/database/queries.rb', line 111 def properties_of(klass) k = coerce_entity(klass) return [] unless k.is_a?(Opencdd::Klass) k.properties_on_class(self) end |
#relation_tree(root = nil, max_depth: 10) ⇒ Object
105 106 107 |
# File 'lib/opencdd/database/queries.rb', line 105 def relation_tree(root = nil, max_depth: 10) Opencdd::RelationTree.new(self).for(root, max_depth: max_depth) end |
#relations_for(domain: nil, codomain: nil) ⇒ Object
153 154 155 156 157 158 159 160 |
# File 'lib/opencdd/database/queries.rb', line 153 def relations_for(domain: nil, codomain: nil) dom = coerce_irdi(domain) cod = coerce_irdi(codomain) relations.select do |r| (dom.nil? || r.domain_irdis.include?(dom)) && (cod.nil? || r.codomain_irdi == cod) end end |
#resolve_reference(ref) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/opencdd/database/queries.rb', line 8 def resolve_reference(ref) return nil if ref.nil? return ref if ref.is_a?(Opencdd::Entity) key = ref.to_s.strip return nil if key.empty? if key.include?("/") || key.include?("#") irdi = Opencdd::IRDI.parse(key) return find(irdi) end entity = @symbol_table[key] return entity if entity by_code = find_by_code(key) return by_code if by_code irdi = Opencdd::IRDI.parse(key) irdi ? find(irdi) : nil end |
#root_classes ⇒ Object
69 70 71 |
# File 'lib/opencdd/database/queries.rb', line 69 def root_classes classes.select { |c| c.parent_irdi.nil? } end |
#terms_of(value_list) ⇒ Object
148 149 150 151 |
# File 'lib/opencdd/database/queries.rb', line 148 def terms_of(value_list) return [] if value_list.nil? value_list.term_irdis.map { |i| find(i) }.compact end |
#valid_class_reference?(categorical_klass, value) ⇒ Boolean
85 86 87 88 89 |
# File 'lib/opencdd/database/queries.rb', line 85 def valid_class_reference?(categorical_klass, value) target = coerce_entity(value) return false unless target instances_of(categorical_klass).any? { |c| c.irdi == target.irdi } end |
#value_list_identifier_of(property) ⇒ Object
141 142 143 144 145 146 |
# File 'lib/opencdd/database/queries.rb', line 141 def value_list_identifier_of(property) case property.parsed_data_type when Opencdd::DataType::EnumStringType, Opencdd::DataType::EnumReferenceType property.parsed_data_type.value_list_identifier end end |
#value_list_of(property) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/opencdd/database/queries.rb', line 123 def value_list_of(property) prop = coerce_entity(property) return nil unless prop.is_a?(Opencdd::Property) relations.each do |r| next unless r.predication? next unless r.domain_irdis.include?(prop.irdi) vl = r.codomain_irdi && find(r.codomain_irdi) return vl if vl.is_a?(Opencdd::ValueList) end return nil unless prop.enum? identifier = value_list_identifier_of(prop) return nil unless identifier vl = resolve_reference(identifier) vl if vl.is_a?(Opencdd::ValueList) end |