-
Notifications
You must be signed in to change notification settings - Fork 42
Interface Description
Ruleby provides a pure Ruby Domain Specific Language (DSL) for defining rules, and asserting facts to the system. This interface takes advantage of the language features in Ruby that make it a good base for creating DSLs.
The following figure is an example of a Ruleby rule.
rule [Message, :m, m.status == :HELLO] do |v|
puts v[:m].message
end
In this example, the left hand side of the production (the ‘when’) is represented by the Array parameter. The right hand side (the ‘then’) is represented by the block.
This rule asserts that an object with a status
of :HELLO
must exist in the collection of facts. If such an object has been added to the working memory, then the ‘action’ specified in the right-hand of this production rule will be fired.
Now let us demonstrate how this rule is used:
engine :hello_engine do |e|
HelloWorldRulebook.new(e).rules
assert e, Message.new(:HELLO, ‘Hello World’)
e.match
end
The ‘HelloWorld’ rule is contained in the
HelloWorldRulebook
class (this is shown in the source code). To use it we create a new inference engine, a new rule set, and a new fact. The ‘assert’ method adds the fact to working memory.
When the match
method is invoked, the action in the ‘HelloWorld’ rule will be fired. And the system will output the following to the console:
Hello World
Now let us add some more actions to our rule and introduce a second rule to our rulebook:
rule [Message, :m, m.status == :HELLO] do |v|
puts v[:m].message
v[:m].message = “Goodbye world”
v[:m].status = :GOODBYE
modify v[:m]
endrule [Message, :m, m.status == :GOODBYE] do |v|
puts v[:m].message
end
When the
match
method is called now, the output will be:
Hello World
Goodbye world
Because the fact in working memory was modified by the action in the first rule, the ‘Goodbye’ rule became satisfied.
There is a lot more that can be done in the Ruleby interface. Read The Ruleby DSL for more information.