Class: RDFS::Rule
Overview
An RDFS entailment rule.
Direct Known Subclasses
Semantics::RDF1, Semantics::RDFS10, Semantics::RDFS11, Semantics::RDFS12, Semantics::RDFS13, Semantics::RDFS2, Semantics::RDFS3, Semantics::RDFS4a, Semantics::RDFS4b, Semantics::RDFS5, Semantics::RDFS6, Semantics::RDFS7, Semantics::RDFS8, Semantics::RDFS9
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
- #antecedents ⇒ Array<Statement> readonly
- #consequents ⇒ Array<Statement> readonly
- #constraints ⇒ Hash{Symbol => Class} readonly
Class Method Summary collapse
- .substitute(consequents, assignment_hash) ⇒ Object
-
.unitary_match(antecedent, statement) ⇒ Object
returns either false or the assignment hash of the match.
Instance Method Summary collapse
-
#antecedent(s, p, o) ⇒ void
Defines an antecedent for this rule.
- #antecedents_ordered_by_decreasing_specificity ⇒ Object
-
#consequent(s, p, o) ⇒ void
Defines the consequent of this rule.
-
#constraint(types = {}) ⇒ void
Defines a type constraint for this rule.
-
#initialize(options = {}) {|rule| ... } ⇒ Rule
constructor
A new instance of Rule.
- #match(statement1, statement2 = nil, noisy = false) ⇒ Object (also: #[])
Constructor Details
#initialize(options = {}) {|rule| ... } ⇒ Rule
Returns a new instance of Rule.
54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/rdfs/rule.rb', line 54 def initialize( = {}, &block) @antecedents = (@@antecedents[self.class] || []).concat([:antecedents] || []) @constraints = (@@constraints[self.class] || {}).merge( [:constraints] || {}) @consequents = (@@consequents[self.class] || []).concat([:consequents] || []) if block_given? case block.arity when 1 then block.call(self) else instance_eval(&block) end end end |
Instance Attribute Details
#antecedents ⇒ Array<Statement> (readonly)
40 41 42 |
# File 'lib/rdfs/rule.rb', line 40 def antecedents @antecedents end |
#consequents ⇒ Array<Statement> (readonly)
46 47 48 |
# File 'lib/rdfs/rule.rb', line 46 def consequents @consequents end |
#constraints ⇒ Hash{Symbol => Class} (readonly)
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.
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_specificity ⇒ Object
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.
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.
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 |