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

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

Constant Summary

Constants included from Util::Logger

Util::Logger::IOWrapper

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:



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-microdata-73e8bfd8e661/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:



53
54
55
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-microdata-73e8bfd8e661/lib/rdf/microdata/expansion.rb', line 53

def antecedents
  @antecedents
end

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

Returns result of this rule.

Returns:



57
58
59
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-microdata-73e8bfd8e661/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 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-microdata-73e8bfd8e661/lib/rdf/microdata/expansion.rb', line 61

def name
  @name
end

Instance Method Details

#antecedent(subject, prediate, object) ⇒ Object



88
89
90
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-microdata-73e8bfd8e661/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 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-microdata-73e8bfd8e661/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:

Yields:

  • (statement)

Yield Parameters:



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-microdata-73e8bfd8e661/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