Wongi Engine is a rule engine formulated in Ruby. This library contains the rule sets to be defined in a readable way with the help of DSL (Defined Under Namespace).
After installing Gem ‘Wongi-Engine’ and integrating the wongi-engine library inside the ruby file, the Wongi Rule Engine is created as mentioned below.
gem install wongi-engine
After installation of the gem, it’s time to create an instance of the wongi-engine to start adding the essential facts or for additional procedures.
engine = Wongi::Engine.create
The Wongi Rule Engine mainly contains rules with matchers/conditions along with the required actions and the facts are as follows:
The rules are interpreted by the statements and it then executes the action if the condition for the rule matches the fact. Every rule is defined in the form mentioned below:
Rule = Condition + Action Facts:
All the input data in the Wongi Rule Engine is implemented as an array of triplets ie. Subject, Predicate. and Object. A Subject is nothing but an entity and an Object signifies the value of that specific entity.
engine << [“OrganizerA”, “offers”, “TicketA”] engine << [“OrganizerB”, “offers”, “TicketB”]
There are special functions that are also provided to add (<<) and remove(retract) the facts to the Wongi Rule Engine.
Rule Engine adds the following advantages to your applications:
1) Higher adaptability - Adapts easily when changing the decision. Rules are saved in the database.
2) Easier to grasp - The rules are easier to follow. And build the bridge between business analysts and developers.
3) Reduces Complexity - The rules in the rule engine help to reduce the complexity of the program.
4) Reusability - The same rule sets can be implemented multiple times leading to an increase in the reusability of the code.
Let’s consider a situation where ‘Organizers award tickets’ and ‘Person requires tickets’ and also let’s assume that Organizers and Person are real-time models.
Organizers has_many: tickets and Person has_and_belongs_to_many: tickets
For a given person (Person A) we need to find all the organizers who offer tickets.
Here is the output result generated using the Wongi Rules Engine. Also, in the above code, the developer makes the rule and according to that, if the rule’s condition is satisfied, the action(output) is performed.
An important aspect of the Rule Engine is if the condition is satisfied only then the work will proceed further or the action will be performed. If the condition is not satisfied, the action will not go further, it will simply be terminated.