class RDFS::Rule

An RDFS entailment rule.

Constants

PLACEHOLDERS

Attributes

antecedents[R]

@return [Array<Statement>]

consequents[R]

@return [Array<Statement>]

constraints[R]

@return [Hash{Symbol => Class}]

Public Class Methods

new(options = {}, &block) click to toggle source

@option options [Array<Statement>] :antecedents ([]) @option options [Hash{Symbol => Class}] :constraints ({}) @option options [Array<Statement>] :consequents ([]) @yield [rule] @yieldparam [Rule]

# File lib/rdfs/rule.rb, line 54
def initialize(options = {}, &block)
  @antecedents = (@@antecedents[self.class] || []).concat(options[:antecedents] || [])
  @constraints = (@@constraints[self.class] || {}).merge( options[:constraints] || {})
  @consequents = (@@consequents[self.class] || []).concat(options[:consequents] || [])

  if block_given?
    case block.arity
      when 1 then block.call(self)
      else instance_eval(&block)
    end
  end
end
substitute(consequents, assignment_hash) click to toggle source
# File lib/rdfs/rule.rb, line 114
def self.substitute(consequents, assignment_hash)
  return nil if assignment_hash.nil?
  c = consequents.collect{|c| c.with_substitutions(assignment_hash)}
  return c.detect(&:has_placeholder?) ? false : c
  
  #perhaps add an integrity check to Rule to make sure that the consequents are fully substituted by the antecedents
end
unitary_match(antecedent, statement) click to toggle source

returns either false or the assignment hash of the match

# File lib/rdfs/rule.rb, line 94
def self.unitary_match(antecedent, statement)
  a, s = antecedent.to_hash, statement.to_hash
  #may need to exclude context
  bound = {}
  a.values.zip(s.values) {|antecedent_value, statement_value| 
    if PLACEHOLDERS.include?(antecedent_value) and !bound[antecedent_value]
      bound[antecedent_value] = statement_value
    elsif PLACEHOLDERS.include?(antecedent_value) and bound[antecedent_value]
      return false unless bound[antecedent_value] == statement_value
    else
      return false unless antecedent_value == statement_value
    end
    }
  return bound
end

Protected Class Methods

antecedent(s, p, o) click to toggle source

Defines an antecedent for this rule class.

@param [Symbol, URI] s @param [Symbol, URI] p @param [Symbol, URI] o @return [void]

# File lib/rdfs/rule.rb, line 173
def self.antecedent(s, p, o)
  @@antecedents[self] << RDF::Statement.new(s, p, o)
end
consequent(s, p, o) click to toggle source

Defines the consequent of this rule class.

@param [Symbol, URI] s @param [Symbol, URI] p @param [Symbol, URI] o @return [void]

# File lib/rdfs/rule.rb, line 193
def self.consequent(s, p, o)
  @@consequents[self] << RDF::Statement.new(s, p, o)
end
constraint(types = {}) click to toggle source

Defines a type constraint for this rule class.

@param [Hash{Symbol => Class}] types @return [void]

# File lib/rdfs/rule.rb, line 182
def self.constraint(types = {})
  @@constraints[self].merge!(types)
end
inherited(subclass) click to toggle source

@private

# File lib/rdfs/rule.rb, line 160
def self.inherited(subclass)
  @@antecedents[subclass] = []
  @@constraints[subclass] = {}
  @@consequents[subclass] = []
end

Public Instance Methods

[](statement1, statement2=nil, noisy = false)
Alias for: match
antecedent(s, p, o) click to toggle source

Defines an antecedent for this rule.

@param [Symbol, URI] s @param [Symbol, URI] p @param [Symbol, URI] o @return [void]

# File lib/rdfs/rule.rb, line 129
def antecedent(s, p, o)
  @antecedents << RDF::Statement.new(s, p, o)
end
antecedents_ordered_by_decreasing_specificity() click to toggle source
# File lib/rdfs/rule.rb, line 110
def antecedents_ordered_by_decreasing_specificity
  a ||= antecedents.sort_by(&:generality)
end
consequent(s, p, o) click to toggle source

Defines the consequent of this rule.

@param [Symbol, URI] s @param [Symbol, URI] p @param [Symbol, URI] o @return [void]

# File lib/rdfs/rule.rb, line 149
def consequent(s, p, o)
  @consequents << RDF::Statement.new(s, p, o)
end
constraint(types = {}) click to toggle source

Defines a type constraint for this rule.

@param [Hash{Symbol => Class}] types @return [void]

# File lib/rdfs/rule.rb, line 138
def constraint(types = {})
  @constraints.merge!(types)
end
match(statement1, statement2=nil, noisy = false) click to toggle source
# File lib/rdfs/rule.rb, line 68
def match(statement1, statement2=nil, noisy = false)
  statements = [statement1, statement2].compact      
  
  return false unless antecedents.size == statements.size
  if antecedents.size == 1
    return false unless (@subs = self.class.unitary_match(antecedents.first, statements.first))
    return Rule.substitute(consequents, @subs)
    
  elsif (implied_assignments = Rule.unitary_match(antecedents_ordered_by_decreasing_specificity.first, statements.first))
    q = Rule.unitary_match(antecedents_ordered_by_decreasing_specificity.last.with_substitutions(implied_assignments), 
                           statements.last.with_substitutions(implied_assignments))
    assignments = q ? q.merge(implied_assignments) : q
    return Rule.substitute(consequents, assignments)
  elsif implied_assignments = Rule.unitary_match(antecedents_ordered_by_decreasing_specificity.first, statements.last)
    q = Rule.unitary_match(antecedents_ordered_by_decreasing_specificity.last.with_substitutions(implied_assignments), 
                           statements.first.with_substitutions(implied_assignments))
    assignments = q ? q.merge(implied_assignments) : q
    return Rule.substitute(consequents, assignments)
  else
    return false
  end
end
Also aliased as: []