Class: RDF::Microdata::Expansion::Rule

Inherits:
Object
  • Object
show all
Includes:
Util::Logger
Defined in:
lib/rdf/microdata/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, **options, &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:

  • name (String)


74
75
76
77
78
79
80
81
82
83
84
85
86
# 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

Instance Attribute Details

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

Returns patterns necessary to invoke this rule.

Returns:

  • (Array<RDF::Query::Pattern>)

    patterns necessary to invoke this rule



53
54
55
# File 'lib/rdf/microdata/expansion.rb', line 53

def antecedents
  @antecedents
end

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

Returns result of this rule.

Returns:

  • (Array<RDF::Query::Pattern>)

    result of this rule



57
58
59
# File 'lib/rdf/microdata/expansion.rb', line 57

def consequents
  @consequents
end

#nameString (readonly)

Returns Name of this rule.

Returns:

  • (String)

    Name of this rule



61
62
63
# File 'lib/rdf/microdata/expansion.rb', line 61

def name
  @name
end

Instance Method Details

#antecedent(subject, prediate, object) ⇒ Object



88
89
90
# 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) ⇒ Object



92
93
94
# File 'lib/rdf/microdata/expansion.rb', line 92

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:

  • queryable (RDF::Queryable)

Yields:

  • (statement)

Yield Parameters:

  • statement (RDF::Statement)


102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# 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