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

Inherits:
Object
  • Object
show all
Defined in:
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

- (Rule) initialize(name, &block)

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:



112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/rdf/rdfa/expansion.rb', line 112

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

- (Object) antecedents (readonly)



93
94
95
# File 'lib/rdf/rdfa/expansion.rb', line 93

def antecedents
  @antecedents
end

- (Object) consequents (readonly)



96
97
98
# File 'lib/rdf/rdfa/expansion.rb', line 96

def consequents
  @consequents
end

- (Object) name (readonly)



99
100
101
# File 'lib/rdf/rdfa/expansion.rb', line 99

def name
  @name
end

Instance Method Details

- (Object) antecedent(subject, prediate, object, context = nil)



125
126
127
# File 'lib/rdf/rdfa/expansion.rb', line 125

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

- (Object) consequent(subject, prediate, object, context = nil)



129
130
131
# File 'lib/rdf/rdfa/expansion.rb', line 129

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

- (Object) execute(queryable) {|statement| ... }

Execute the rule against queryable, yielding each consequent with bindings

Parameters:

  • queryable (RDF::Queryable)

Yields:

  • (statement)

Yield Parameters:

  • statement (RDF::Statement)


139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/rdf/rdfa/expansion.rb', line 139

def execute(queryable)
  RDF::Query.new(antecedents).execute(queryable).each do |solution|
    nodes = {}
    consequents.each do |consequent|
      terms = {}
      [:subject, :predicate, :object, :context].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