class RDF::Microdata::Expansion::Rule

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

Attributes

antecedents[R]

@!attribute [r] @return [Array<RDF::Query::Pattern>] patterns necessary to invoke this rule

consequents[R]

@!attribute [r] consequents @return [Array<RDF::Query::Pattern>] result of this rule

name[R]

@!attribute [r] name @return [String] Name of this rule

Public Class Methods

new(name, **options, &block) click to toggle source

@example

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}

@param [String] name

# File lib/rdf/microdata/expansion.rb, line 74
def initialize(name, **options, &block)
  @antecedents = []
  @consequents = []
  @options = options.dup
  @name = name

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

Public Instance Methods

antecedent(subject, prediate, object) click to toggle source
# File lib/rdf/microdata/expansion.rb, line 88
def antecedent(subject, prediate, object)
  antecedents << RDF::Query::Pattern.new(subject, prediate, object)
end
consequent(subject, prediate, object) click to toggle source
# File lib/rdf/microdata/expansion.rb, line 92
def consequent(subject, prediate, object)
  consequents << RDF::Query::Pattern.new(subject, prediate, object)
end
execute(queryable) { |from| ... } click to toggle source

Execute the rule against queryable, yielding each consequent with bindings

@param [RDF::Queryable] queryable @yield [statement] @yieldparam [RDF::Statement] statement

# File lib/rdf/microdata/expansion.rb, line 102
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