Class: RDF::RDFa::Expansion::Rule

Inherits:
Object
  • Object
show all
Defined in:
vendor/bundler/ruby/3.3.0/bundler/gems/rdf-rdfa-ea6265716853/lib/rdf/rdfa/expansion.rb

Overview

An entailment rule

Takes a list of antecedent patterns used to find solutions against a queryable object. Yields each consequent with bindings from the solution

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, &block) ⇒ Rule

Returns a new instance of Rule.

Examples:

r = Rule.new("scm-spo") do
  antecedent :p1, RDF::RDFS.subPropertyOf, :p2
  antecedent :p2, RDF::RDFS.subPropertyOf, :p3
  consequent :p1, RDF::RDFS.subPropertyOf, :p3, "t-box"
end

r.execute(queryable) {|statement| puts statement.inspect}

Parameters:



91
92
93
94
95
96
97
98
99
100
101
102
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-rdfa-ea6265716853/lib/rdf/rdfa/expansion.rb', line 91

def initialize(name, &block)
  @antecedents = []
  @consequents = []
  @name = name

  if block_given?
    case block.arity
      when 1 then block.call(self)
      else instance_eval(&block)
    end
  end
end

Instance Attribute Details

#antecedentsArray<RDF::Query::Pattern> (readonly)



66
67
68
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-rdfa-ea6265716853/lib/rdf/rdfa/expansion.rb', line 66

def antecedents
  @antecedents
end

#consequentsArray<RDF::Query::Pattern> (readonly)



70
71
72
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-rdfa-ea6265716853/lib/rdf/rdfa/expansion.rb', line 70

def consequents
  @consequents
end

#deletionsArray<RDF::Query::Pattern> (readonly)



74
75
76
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-rdfa-ea6265716853/lib/rdf/rdfa/expansion.rb', line 74

def deletions
  @deletions
end

#nameString (readonly)

Returns:



78
79
80
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-rdfa-ea6265716853/lib/rdf/rdfa/expansion.rb', line 78

def name
  @name
end

Instance Method Details

#antecedent(subject, prediate, object) ⇒ Object



104
105
106
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-rdfa-ea6265716853/lib/rdf/rdfa/expansion.rb', line 104

def antecedent(subject, prediate, object)
  antecedents << RDF::Query::Pattern.new(subject, prediate, object)
end

#consequent(subject, prediate, object) ⇒ Object



108
109
110
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-rdfa-ea6265716853/lib/rdf/rdfa/expansion.rb', line 108

def consequent(subject, prediate, object)
  consequents << RDF::Query::Pattern.new(subject, prediate, object)
end

#execute(queryable) {|statement| ... } ⇒ Object

Execute the rule against queryable, yielding each consequent with bindings

Parameters:

Yields:

  • (statement)

Yield Parameters:



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-rdfa-ea6265716853/lib/rdf/rdfa/expansion.rb', line 118

def execute(queryable)
  RDF::Query.new(antecedents).execute(queryable).each do |solution|
    nodes = {}
    consequents.each do |consequent|
      terms = {}
      [:subject, :predicate, :object].each do |r|
        terms[r] = case o = consequent.send(r)
        when RDF::Node            then nodes[o] ||= RDF::Node.new
        when RDF::Query::Variable then solution[o]
        else                           o
        end
      end

      yield RDF::Statement.from(terms)
    end
  end
end