Class: RDFS::Rule

Inherits:
Object
  • Object
show all
Includes:
RDF
Defined in:
lib/rdfs/rule.rb

Overview

An RDFS entailment rule.

Constant Summary collapse

PLACEHOLDERS =
(p = [:aaa, :bbb, :ccc, :ddd, :uuu, :vvv, :xxx, :yyy, :zzz]) + p.collect {|pl| RDF::Literal.new(pl)}  + p.collect {|pl| RDF::Node.new(pl)}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) {|rule| ... } ⇒ Rule

Returns a new instance of Rule.

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :antecedents (Array<Statement>) — default: []
  • :constraints (Hash{Symbol => Class}) — default: {}
  • :consequents (Array<Statement>) — default: []

Yields:

  • (rule)

Yield Parameters:



54
55
56
57
58
59
60
61
62
63
64
65
# 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

Instance Attribute Details

#antecedentsArray<Statement> (readonly)

Returns:



40
41
42
# File 'lib/rdfs/rule.rb', line 40

def antecedents
  @antecedents
end

#consequentsArray<Statement> (readonly)

Returns:



46
47
48
# File 'lib/rdfs/rule.rb', line 46

def consequents
  @consequents
end

#constraintsHash{Symbol => Class} (readonly)

Returns:

  • (Hash{Symbol => Class})


43
44
45
# File 'lib/rdfs/rule.rb', line 43

def constraints
  @constraints
end

Class Method Details

.substitute(consequents, assignment_hash) ⇒ Object



114
115
116
117
118
119
120
# 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) ⇒ Object

returns either false or the assignment hash of the match



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# 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

Instance Method Details

#antecedent(s, p, o) ⇒ void

This method returns an undefined value.

Defines an antecedent for this rule.

Parameters:

  • s (Symbol, URI)
  • p (Symbol, URI)
  • o (Symbol, URI)


129
130
131
# File 'lib/rdfs/rule.rb', line 129

def antecedent(s, p, o)
  @antecedents << RDF::Statement.new(s, p, o)
end

#antecedents_ordered_by_decreasing_specificityObject



110
111
112
# File 'lib/rdfs/rule.rb', line 110

def antecedents_ordered_by_decreasing_specificity
  a ||= antecedents.sort_by(&:generality)
end

#consequent(s, p, o) ⇒ void

This method returns an undefined value.

Defines the consequent of this rule.

Parameters:

  • s (Symbol, URI)
  • p (Symbol, URI)
  • o (Symbol, URI)


149
150
151
# File 'lib/rdfs/rule.rb', line 149

def consequent(s, p, o)
  @consequents << RDF::Statement.new(s, p, o)
end

#constraint(types = {}) ⇒ void

This method returns an undefined value.

Defines a type constraint for this rule.

Parameters:

  • types (Hash{Symbol => Class}) (defaults to: {})


138
139
140
# File 'lib/rdfs/rule.rb', line 138

def constraint(types = {})
  @constraints.merge!(types)
end

#match(statement1, statement2 = nil, noisy = false) ⇒ Object Also known as: []



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# 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