Class: RuleBase

Inherits:
Object
  • Object
show all
Defined in:
lib/activerdf_rules/rule_base.rb

Overview

An instance of the RuleBase class contains a set of rules for processing with the RuleEngine. It has a built in DSL that can be used to create a rule base.

Example:

rb = RuleBase.new 'DSL RuleBase' do
  rule 'Rule1' do
    condition :x, :y, :z

    conclusion :z, :y, :x
  end

  rule 'Rule2' do
    condition :x, :y, :z

    conclusion :y, :x, :z
  end
end

Additionally, you can create an instance of RuleBase and just add rules to the rules array manually

Example:

rb = RuleBase.new 'Manual RuleBase'
r = RuleBase::Rule.new 'Rule 1'
r.condition :x, :y, :z
r.conclusion :z, :y, :x
rb.rules << r

As you can see from these examples you can create variables in your rules by using symbols.

There is no point in adding more than one rule that has the same conditions and conclusion (i.e. they do the same thing), because it will just mean that the RuleEngine will process an extra rule with no extra benefit.

Currently, RuleBase does not verify that you are adding unique rules. However, it does verify that the conclusion and all of the conditions are triples. It also verifies that the subject and predicate of the conclusion and every condition are either variables or resources. Additionally, it only allows you to set one conclusion. If any of these conditions is not met, then it will throw an exception.

When using the class as a DSL it will also check each rule as it is added to ensure that all of the variables that appear in the conclusion also appear in at least one condition. If this condition is not met it will throw an exception. When adding rules manually to the rules array it will not perform this check. However, later on when the RuleEngine converts the rule to a query ActiveRDF will throw an exception if this condition is not met.

Defined Under Namespace

Classes: Rule

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = nil, &b) ⇒ RuleBase

Any block that is passed in will be instance_eval’ed, which will allow you to use the built in DSL.



55
56
57
58
59
# File 'lib/activerdf_rules/rule_base.rb', line 55

def initialize(name = nil, &b)
  @name = name || 'NoName'
  @rules = []
  instance_eval(&b) if b
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



51
52
53
# File 'lib/activerdf_rules/rule_base.rb', line 51

def name
  @name
end

#rulesObject

Returns the value of attribute rules.



51
52
53
# File 'lib/activerdf_rules/rule_base.rb', line 51

def rules
  @rules
end

Instance Method Details

#dupObject

Makes a deep copy of the rule base, including the rules array.



71
72
73
74
75
# File 'lib/activerdf_rules/rule_base.rb', line 71

def dup
  rb = super
  rb.rules = self.rules.dup
  rb
end

#rule(name = nil, &b) ⇒ Object

Used in the DSL method to add a rule to this rule base.



62
63
64
65
66
67
68
# File 'lib/activerdf_rules/rule_base.rb', line 62

def rule(name = nil, &b)
  r = Rule.new name || "Rule #{@rules.size + 1}"
  r.instance_eval(&b)
  r.validate
  @rules << r
  $activerdflog.debug("Adding '#{r}' to '#{self}'")
end

#to_sObject

Returns the name of the rule base



78
79
80
# File 'lib/activerdf_rules/rule_base.rb', line 78

def to_s
  @name
end