|
Vocabulary of Natural Terms
RuleLab.Net Vocabulary allows you to build a list of the Natural Terms and use them to create Rules in syntax close to a natural language. The Vocabulary contains Vocabulary Terms each linking a list of Natural Term aliases with the Term Implementation – a logical expression involving Atoms and Facts that you’d normally use in a Rule Condition or Action. Natural Terms are entered in plain English and may include data parameters that are resolved when Terms are utilized in a Rule. Term Implementations are entered just like Rule Conditions and Actions only they are generally shorter than Rules. Vocabulary Term is basically a Rule building block associated with a Natural Language string. Every Rule that uses the Vocabulary Terms can be alternatively expressed using Atoms, Facts and other usual items that make a Rule. Vocabulary hides the lower level implementation. It allows specifying Rules in a more natural syntax than the RuleLab.Net default Rule format.
Every RuleLab.Net demo application uses a Vocabulary to define some of the Rules. To see how the Rules w/Natural Terms compare to the ones without them please open the “Purchase Transactions” demo application. This page utilizes one of the Rules from that demo to illustrate the usage of the Term Editor.
For example, let’s assume there is data showing multiple purchase transactions made by a customer. You may want to enter a Rule that utilizes the multi-transaction data to offer frequent customers (with over 5 annual purchases) a discount:
If frequent customer has purchased more than 2 of our Outdoors products over the past 6 months and has never been offered a discount before Then give customer 10% discount on the today’s purchase
With the Vocabulary of Natural Terms, you can compose a Rule that looks pretty much like this phrase. To better understand how this is accomplished, let’s examine the logic of this Rule first. At the Atom/Fact level, you can graphically build this Rule to could look like this: |
|
If Average Number of Annual Transactions is GreaterThan 5 and
number of Products is GreaterThan 2 (filter criteria: Category is Outdoors and number of months since Purchase Date is Less Than or Equal To 6) and
number of PastDiscount is Equal To 0
Then set DiscountPercentage to 10%
To make it appear more natural, some of the logical expressions should be associated with Natural Terms. Multiple aliases can be entered for each Term. A Term can be one of the following 3 types: Condition, Filter or Action:
|
Vocabulary Term Type |
Natural Term |
RuleLab.Net Term Implementation |
|
Condition |
1) frequent Customer |
Average Number of Annual Transactions is GreaterThan 5 |
|
Condition |
1) purchased more than (value) |
number of Products is GreaterThan value |
|
Filter |
1) Outdoors products |
Category is Outdoors |
|
Filter |
1) over the past (number) months |
number of months since Purchase Date is Less Than or Equal To value |
|
Condition |
1) has never been offered a discount 2) never had a discount |
number of PastDiscount is Equal To 0 |
|
Action |
1) give Customer (value)% discount 2) offer (value)% discount |
set DiscountPercentage to value% |
Constants do not have to be hardcoded in the Vocabulary. They can be entered as parameters in parentheses that you resolve to specific data when entering a Rule. By replacing the parameters with numbers 2, 6, and 10 correspondingly you’d enter the Rule as
If frequent Customer has purchased more than 2 of our Outdoors products over the past 6 months and has never been offered a discount before
Then give Customer 10% discount on the today’s purchase
This syntax will have specific meaning to the RuleLab.Net allowing mapping it to your application data context. To interpret this Rule, RuleLab.Net concatenates the Conditional terms via “AND” predicates, combines same loop filter terms into a single a Filter, and applies this Filter to every Conditional term of the same loop type.
The Rule Browser lets you click the highlighted terms to see how they are interpreted in the Vocabulary.
Using the Natural Terms Vocabulary, you can drive your application logic via natural language type syntax.
Vocabulary Terms Editor
Entering the Natural Terms is similar to entering Rules. First, you need to create a Vocabulary file via the Project screen and load it into the Project. Once the file has been added to the Project, open the Vocabulary tab to create the Natural Terms. Your XML References, Atoms and Facts should be fairly stable at this point because the Vocabulary Terms are composed of Atoms and Facts.
There are 3 types of Vocabulary Terms that you can enter:
Adding Vocabulary Terms
To add a new Term, select its type and click the Add or Insert button. The Vocabulary Terms Editor will open.
If you are creating a Filter type Term, you will need to select the loop that it is applied to. Loops are identified by Atoms that belong to theses loops. Simply select one of the Atoms that you know is in the loop. Once the selection has been made, the Editor will be automatically pre-selecting only the applicable Atoms to help you build an expression that targets the desired loop. For example, by selecting the “Purchase Date” as the loop identity we will limit the new Filter’s Atoms to the ones matching same loop:

The Term Editor screen is quite similar to the Rule Editor. It consists of 2 parts:
Term’s logic can be as complex as the RuleLab.Net syntax allows. Feel free to nest AND’s and OR’s just like you’d do it when editing a Rule. You can even add Filters to your expression – just like in a Rule – only keep in mind that such Filter will only apply to its respective outer Atom. This is different from the Filter type Term which applies to every sibling Condition type Term in a Rule.
Entering Parameters
You can create Vocabulary Terms with parameters that will be filled in when this Term is utilized in a Rule. For example, you may want to add a Natural Term involving the price without specifying the actual price in the Vocabulary:

The parameter is identified by the following matching items:
The actual string doesn’t matter but there must be one such entry in every Natural Term alias, and it must be in (). In the sample above the (value) corresponds to the atom/fact/formula/value placeholder. You can enter only one parameter per Vocabulary Term.
Using the Natural Terms in a Rule
The Vocabulary of Natural Terms XML file must be present in the current Project. The Terms Vocabulary Lookup box opens automatically when you create or edit a Rule as long as the Vocabulary is part of the Project. To check if the Vocabulary has been populated, click the Vocabulary tab. The entries in the Vocabulary list should be the same that populate the Lookup box.
If the Vocabulary is up on your Rule Editor screen, then each new expression line that you create will include a phrase link. When you click it, the line is replaced with a text box for entering the Condition or Action phrase. For example:

The very first line has been replaced with the text box where a Condition phrase was typed in. There are also two new lines w/phrase links that can turn into a phrase or the usual Atom/Fact expression.
The Vocabulary Lookup box lists every Natural Term alias under the corresponding Implementation expression. To enter a Natural Term into the phrase text box, click into the text box and doubleclick a Natural Term under the desired Term Implementation. If there are multiple Natural Terms to choose from, choose the one that best matches the natural flow of your phrase. Alternatively, you can simply type one of the Natural Terms. Note that you don’t need to understand the Implementation part in order to use the Natural Terms but it helps if you do.
When you doubleclick a Natural Term, it is copied into the text box inside the square brackets. When typing a Natural Term, you must enter it in square brackets also. Otherwise, the Rules Engine will ignore your Term just like it ignores the “natural” connector wording such as “Customer has” and “for a” in the example above. Everything outside [] is ignored by the Engine. It is there for the readability only.
If a Natural Term has a parameter, this parameter is copied into the phrase box in parentheses. Replace the parameter placeholder with an actual value. In the example above, the values of 2, 12, and 300 where typed in by the user to replace placeholders from the Vocabulary Terms.
Phrases can be organized into logical AND/OR tree just like the familiar Atom/Fact expressions.
Internally, each phrase is translated into the expressions connected via an AND logical operator. If you click the “Translate Phrases” checkbox in the example above (see the Purchase Transaction sample in the RuleLab.Net online Demo) then the phrase is rendered as follows:

As you can see, the two Filter type Terms that were used in the phrase were concatenated and applied to each Condition Term matching the target loop of the Filter Term. If you are following along with the online Demo, then you can check in the References screen that Product Vendor, Product Category, Purchase Date, and Price Paid atoms all reside under the same loop. Note that that the interpretation expressions are not clickable.
When you are done with the Rule, apply your changes. Don’t forget to save your Rules once in the Rule Browser screen! The Rules with the Natural Terms are rendered by the Browser without brackets or parentheses, for example:
You can click the Natural Terms in the Rule to look them up in the Vocabulary.
The Rules XML file that you create does not require the Vocabulary XML file to accompany it into the Implementation process. The Rules file contains all the interpretation required for the BRE to parse the Rules that include Natural Terms.
Facts can be utilized in place of data values. For example, if you specified an amount of $300.00 as a Fact named “Max Spent” then you could enter it into a phrase:
“… for a [total amount exceeding $(FACT (Max Spent)]”.
Updating the Vocabulary Terms
Once a Vocabulary Term is utilized in a Rule, it becomes part of that Rule. Other than the Term alias, the Rule has no linking back to the Vocabulary Term. Therefore, changing a Vocabulary Term is not applied to the Rules.
If you are changing Term implementation, your changes will not be automatically applied to the Rules utilizing this Term. If you do want to apply them, click the “Used Where?” button to display the affected Rule list. You will have to open/save each Rule from the displayed list.
If you are changing an already utilized Natural Term, you may eventually receive an error from the Rules Validator when it fails to match each Natural Term to the Vocabulary. To resolve, simply add the missing (old) Natural Term or update the Rule with the new one.
Unlike changing Atoms and Facts, modifying the Vocabulary does not violate the relationships between RuleLab.Net objects. The Rules will continue to work regardless of the Vocabulary changes. However, for readability and usability, it is best to keep the Rules in sync with the Vocabulary. Therefore, before changing an existing Term, always check its usage via the “Used Where?” button.
Web Site Biz Logic Manager: Displaying HTML controls via the rule phrases
If you are dynamically creating INPUT controls in your HTML, you can add atoms to match them. The atoms will allow you to apply rules to the dynamic INPUT controls. For example, a rule phrase may contain an addition of “quantity” input control:
[Add table data field (<input type=text maxlenth=2 value=1 id=quantity>)]
When this rule executes against your web page, a new INPUT control will be rendered. After your web site user fills and submits this control to RuleLab.Net, you can utilize it in other rules as an Atom.
To use this feature, create a matching Atom in the References screen by setting the following:
Alias: quantity
XPath: /page/inps/*[@idc='quantity']
Atom: check
Save the References and switch to the Rules screen. Now you can create a rule using the “quantity” Atom.
A good example of using the dynamic input controls is the “Order Confirmation” rule in the Tucson-Hiking online demo. Atoms in the condition of this rule are matching the input controls created by the “product” rules.
Locking the
Vocabulary
You can lock/unlock the Vocabulary using the Key/Lock icons same way as locking the Rules.
Related topics
Introduction to RuleLab.Net
RuleLab.Net System architecture