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

Inherits:
Object
  • Object
show all
Includes:
Util::Logger
Defined in:
vendor/bundler/ruby/2.5.0/bundler/gems/rdf-microdata-4faeb525a56b/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

Methods included from Util::Logger

#log_debug, #log_depth, #log_error, #log_fatal, #log_info, #log_recover, #log_recovering?, #log_statistics, #log_warn, #logger

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:



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-microdata-4faeb525a56b/lib/rdf/microdata/expansion.rb', line 75

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:



54
55
56
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-microdata-4faeb525a56b/lib/rdf/microdata/expansion.rb', line 54

def antecedents
  @antecedents
end

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

Returns result of this rule

Returns:



58
59
60
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-microdata-4faeb525a56b/lib/rdf/microdata/expansion.rb', line 58

def consequents
  @consequents
end

#nameString (readonly)

Returns Name of this rule

Returns:

  • (String)

    Name of this rule



62
63
64
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-microdata-4faeb525a56b/lib/rdf/microdata/expansion.rb', line 62

def name
  @name
end

Instance Method Details

#antecedent(subject, prediate, object) ⇒ Object



89
90
91
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-microdata-4faeb525a56b/lib/rdf/microdata/expansion.rb', line 89

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

#consequent(subject, prediate, object) ⇒ Object



93
94
95
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-microdata-4faeb525a56b/lib/rdf/microdata/expansion.rb', line 93

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:



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-microdata-4faeb525a56b/lib/rdf/microdata/expansion.rb', line 103

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