Wongi-engine syntax

Lately I have been using the wongi-engine ruby gem, which is fast and works well. In my opinion the documentation could use more examples of proper syntax, so I will provide some here. Before this project I had not used a business rules engine  or heard of the Rete algorithm before, so some of this might be obvious to more experienced coders, but I had to figure it out.

harry = Student.new
engine = Wongi::Engine.create
engine << [harry, "wizard", true]
engine << [harry, "muggle", false]
engine << [harry, "potions_grade", 1]
engine << [harry, "spells_grade", 2]

advance_grade = engine.rule "advanced to second grade?" do
forall {
has :Student, "wizard", true
neg :Student, "muggle", true
maybe :Student, "potions_grade", :Potions_grade
maybe :Student, "spells_grade", :Spells_grade
assert do |token|
token[:Potions_grade] < 3 && token[:Spells_grade] < 3
end
}
end

if advance_grade.tokens.size > 0
harry.grade = 2
end

As you can see from the example code snippet above, you can easily perform simple operations like == or != using the has and neg keywords in a Wongi::Engine rule block. However, to do more complex operations you can use maybe to put a value into a token variable, then perform operations like < on the values stored in the token in an assert block. The assert block will return true or false based on the final line in the block, so don’t do anything important in earlier lines and then lose track of it. The documentation refers to less and greater keywords, but describes their use as “obvious”, and I wasn’t able to figure out how to use them to compare anything stored in a variable.

Advertisements

4 thoughts on “Wongi-engine syntax

  1. úlfurinn says:

    You shouldn’t use #maybe here. It will pass even if you don’t have any matching facts, leave the grade variables unbound, and then Ruby will yell at you for comparing nils and integers. Just use #has.

  2. Joe says:

    Turns out a bug in Wongi was preventing the use of :Variables with less and greater. I’ve fixed it in the following pull request: https://github.com/ulfurinn/wongi-engine/pull/29

  3. Joe says:

    Turns out there was a bug in Wongi that broke using :Variables in less and greater statements. I’ve fixed it in the following pull request, if you want to check it out. https://github.com/ulfurinn/wongi-engine/pull/29

  4. herringtown says:

    @Joe, thanks for the post here…

    Interested to see an example here of a deeper directed graph….i.e., something like Harry’s progression/pathway through Hogwarts with different outcomes, where rules interact with each other to modify/add facts.

    Like, now Harry is in grade 2…OK, then there some more rules that kick in; or, maybe he failed, then XYZ.

    I’m struggling with how to conceptualize this as more of a decision-table/”course of action” flow as most of the examples I’ve seen are “flat”, where you input a couple facts, run a rule, and boom, there’s your result. What I’d be interested to see is an example where the result leads to some sort of decision point rule (some rule that relates to if harry passed or Failed second grade), which leads to some actions that produce more results based on existing facts, modified facts, or facts added to the engine later..

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: