Module: RDF::Reasoner::OWL

Defined in:
lib/rdf/reasoner/owl.rb

Overview

Rules for generating OWL entailment triples

Extends `RDF::URI` and `RDF::Statement` with specific entailment capabilities

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(mod) ⇒ Object


101
102
103
104
# File 'lib/rdf/reasoner/owl.rb', line 101

def self.included(mod)
  mod.add_entailment :equivalentClass, :_entail_equivalentClass
  mod.add_entailment :equivalentProperty, :_entail_equivalentProperty
end

Instance Method Details

#_entail_equivalentClassObject

For a Term: yield or return inferred equivalentClass relationships For a Statement: if predicate is `rdf:types`, yield or return inferred statements having a equivalentClass relationship to the type of this statement


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rdf/reasoner/owl.rb', line 27

def _entail_equivalentClass
  case self
  when RDF::URI, RDF::Node
    unless class?
      yield self if block_given?
      return Array(self)
    end

    # Initialize @equivalentClass_cache by iterating over all defined property terms having an `owl:equivalentClass` attribute and adding the source class as an equivalent of the destination class
    if equivalentClass_cache.empty?
      RDF::Vocabulary.each do |v|
        v.each do |term|
          term.equivalentClass.each do |equiv|
            (equivalentClass_cache[equiv] ||= []) << term
          end if term.class?
        end
      end
    end
    terms = (self.equivalentClass + Array(equivalentClass_cache[self])).uniq
    terms.each {|t| yield t} if block_given?
    terms
  when RDF::Statement
    statements = []
    if self.predicate == RDF.type
      if term = (RDF::Vocabulary.find_term(self.object) rescue nil)
        term._entail_equivalentClass do |t|
          statements << RDF::Statement(self.to_h.merge(object: t, inferred: true))
        end
      end
    end
    statements.each {|s| yield s} if block_given?
    statements
  else []
  end
end

#_entail_equivalentPropertyObject

For a Term: yield or return return inferred equivalentProperty relationships For a Statement: yield or return inferred statements having a equivalentProperty relationship to predicate of this statement


67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/rdf/reasoner/owl.rb', line 67

def _entail_equivalentProperty
  case self
  when RDF::URI, RDF::Node
    unless property?
      yield self if block_given?
      return Array(self)
    end

    # Initialize equivalentProperty_cache by iterating over all defined property terms having an `owl:equivalentProperty` attribute and adding the source class as an equivalent of the destination class
    if equivalentProperty_cache.empty?
      RDF::Vocabulary.each do |v|
        v.each do |term|
          term.equivalentProperty.each do |equiv|
            (equivalentProperty_cache[equiv] ||= []) << term
          end if term.property?
        end
      end
    end
    terms = (self.equivalentProperty + Array(equivalentProperty_cache[self])).uniq
    terms.each {|t| yield t} if block_given?
    terms
  when RDF::Statement
    statements = []
    if term = (RDF::Vocabulary.find_term(self.predicate) rescue nil)
      term._entail_equivalentProperty do |t|
        statements << RDF::Statement(self.to_h.merge(predicate: t, inferred: true))
      end
    end
    statements.each {|s| yield s} if block_given?
    statements
  else []
  end
end

#equivalentClass_cacheRDF::Util::Cache

Returns:

  • (RDF::Util::Cache)

12
13
14
# File 'lib/rdf/reasoner/owl.rb', line 12

def equivalentClass_cache
  @@subPropertyOf_cache ||= {}
end

#equivalentProperty_cacheRDF::Util::Cache

Returns:

  • (RDF::Util::Cache)

18
19
20
# File 'lib/rdf/reasoner/owl.rb', line 18

def equivalentProperty_cache
  @@equivalentProperty_cache ||= {}
end