Project

General

Profile

Cruximplement » History » Version 27

Shuvam Misra, 21/09/2023 01:17 AM

1 4 Shuvam Misra
*(For a conceptual overview and design of Crux, see [[cruxdesign|this page]] if you haven't already.)*
2
3 1 Shuvam Misra
# Implementation of Remiges Crux
4
5
{{>toc}}
6
7
A rules engine implementation must include the following:
8
* **RULE SCHEMA**. A notation to specify the list of valid terms in a rule. This list will be separate for each class of entities. For instance, for items in inventory, the list of attributes may be:
9
  * Price
10
  * Full name
11
  * Age in stock
12
  * Quantity in inventory
13
14
    For vendors, the list could include:
15
  * Amount outstanding
16
  * Total value of business done  in the last financial year
17
18
* **RULE NOTATION**. A notation to specify the pattern and actions of a rule.
19
* **THE MATCHING ENGINE**. Something which will take an entity with all its attributes, apply each rule to it, and follow the trail of rules to come up with a list of actions which will emerge.
20
21
So, if these three can be designed and then implemented, the core of a rules engine or a flow engine can be built.
22
23 2 Shuvam Misra
## Representing the schema of patterns
24 1 Shuvam Misra
25
If using JSON, the schema of all valid patterns may be represented in structures of this form:
26
27
``` json
28
"patternschema": {
29
    "class": "inventoryitems",
30
    "attr": [{
31
        "name": "cat",
32
        "type": "enum",
33
        "vals": [ "textbook", "notebook", "stationery", "refbooks" ]
34
    },{
35
        "name": "mrp",
36
        "type": "float"
37
    },{
38
        "name": "fullname",
39
        "type": "str",
40
    },{
41
        "name": "ageinstock",
42
        "type": "int"
43
    },{
44
        "name": "inventoryqty",
45
        "type": "int"
46
    }]
47
}
48
```
49
50 20 Shuvam Misra
In this example, the object `patternschema` is the schema for one category of entities. This schema says that for rules which work on entities of type `inventoryitems`, there are five attributes available which may be used to make patterns. Each attribute has a type. Boolean, enum types, integers, floating point numbers, timestamps (`ts`) and strings are supported. The example above does not have any attribute of type `ts` or  `bool`.
51 1 Shuvam Misra
52
So, the full schema of the rules engine will be an array of `patternschema` blocks. Initial examples have discussed inventory items and vendors. The `patternschema` block above is for inventory items. If the schema of patterns for vendors needed to be specified, there would be a second `patternschema` with `“class”: “vendors”`
53
54 5 Shuvam Misra
While the fields in the example above are adequate from a purely functional point of view, it may be necessary to have some additional metadata to allow the building of a good UI which will allow users to manage these schema objects. So, an augmented data structure may look like this:
55
56
``` json
57
"patternschema": {
58
    "class": "inventoryitems",
59
    "attr": [{
60
        "name": "cat",
61
        "shortdesc": "Category of item",
62
        "longdesc": "Each item can belong to one of the following categories: textbooks, notebooks, stationery, or reference books.",
63
        "type": "enum",
64
        "vals": [ "textbook", "notebook", "stationery", "refbooks" ],
65
        "enumdesc": [ "Text books", "Notebooks", "Stationery and miscellaneous items", "Reference books, library books" ]
66
    },{
67
        "name": "mrp",
68
        "shortdesc": "Maximum retail price",
69
        "longdesc": "The maximum retail price of the item as declared by the manufacturer."
70
        "type": "float",
71
        "valmax": 20000,
72
        "valmin": 0
73
    },{
74
        "name": "fullname",
75
        "shortdesc": "Full name of item",
76
        "longdesc": "The full human-readable name of the item. Not unique, therefore sometimes confusing.",         
77
        "type": "str",
78
        "lenmax": 40,
79
        "lenmin": 5
80
    },{
81
        "name": "ageinstock",
82
        "shortdesc": "Age in stock, in days",
83
        "longdescr": "The age in days that the oldest sample of this item has been lying in stock",
84
        "type": "int",
85
        "valmax": 1000,
86
        "valmin": 1
87
    },{
88
        "name": "inventoryqty",
89
        "shortdesc": "Number of items in inventory",
90
        "longdescr": "How many of these items are currently present in the inventory",
91
        "type": "int",
92
        "valmax": 10000,
93
        "valmin": 0
94
    }]
95
}
96
```
97
98
Here, the `shortdesc` and `longdesc` are useful attributes of each attribute, for displaying labels and help text in any UI which is displayed to the human user who manages the rules for entities of this class. The `valmax`, `valmin`, `lenmax`, `lenmin`, allow the system to enforce some sanity checks on the patterns defined in any rules for this entity.
99
100 2 Shuvam Misra
## Representing the schema of actions
101 1 Shuvam Misra
102
The schema of the action section of rules is simpler than patterns. Each rule's action section will contain a set of zero or more words, each denoting an action, and zero or more attribute assignments. There is no need for any type specification, etc.
103
* An example of an action word: `invitefordiwali`
104
* An example of an attribute assignment: `discount=7`
105
106
So, the schema of the actions will just specify the valid action names and the attribute names for assignments.
107
108
``` json
109
"actionschema": {
110
    "class": "inventoryitems",
111
    "actions": [ "invitefordiwali", "allowretailsale", "assigntotrash" ],
112
    "attribs": [ "discount", "shipby" ],
113
    "tags": [ "specialvendor", "tryoverseas" ]
114
}
115
```
116
The schema of actions above indicates that there are three actions, any or all of which may be present in any rule for this class of entities. There are two attributes which may be assigned values by any rule. And there are two tags for this class of entities – if a rule wishes to tag an entity with one or both of these tags, it may do so.
117
118
Putting the `patternschema` and `actionschema` blocks together, a better representation for the full schema for a class of entities will be:
119
120
``` json
121
"ruleschema": {
122
    "class": "inventoryitems",
123
    "patternschema": {
124
        "attr": [{
125
            "name": "cat",
126
            "type": "enum",
127
            "vals": [ "textbook", "notebook", "stationery", "refbooks" ]
128
        },{
129
            "name": "mrp",
130
            "type": "float"
131
        },{
132
            "name": "fullname",
133
            "type": "str",
134
        },{
135
            "name": "ageinstock",
136
            "type": "int"
137
        },{
138
            "name": "inventoryqty",
139
            "type": "int"
140
        }]
141
    }
142
    "actionschema": {
143
        "actions": [ "invitefordiwali", "allowretailsale", "assigntotrash" ],
144
        "attribs": [ "discount", "shipby" ],
145
    }
146
}
147
```
148
149
There will need to be one such `ruleschema` block for each class.
150
151 2 Shuvam Misra
## Representing a pattern
152 1 Shuvam Misra
153 6 Shuvam Misra
Below is an example of a pattern of a rule, which conforms to the schema example given above.
154
155 1 Shuvam Misra
``` json
156
"rulepattern": {
157
    "pattern": [{
158
        "attr": "cat",
159
        "op": "eq",
160
        "val": "textbook"
161
    },{
162
        "attr": "mrp",
163
        "op": "ge",
164
        "val": 2000
165
    },{
166
        "attr": "ageinstock",
167
        "op": "ge",
168
        "val": 90
169 22 Shuvam Misra
    },{
170
        "attr": "invitefordiwali",
171
        "op": "eq",
172
        "val": true
173 1 Shuvam Misra
    }]
174
}
175
```
176
177
If a rule has this pattern, it will match any entity which falls in the class `inventoryitems` which
178
* is of type textbook
179
* has MRP (max retail price) greater than INR 2000
180 25 Shuvam Misra
* has been in stock longer than 89 days 
181
* one of the earlier rules matched against this entity has added the action `invitefordiwali` to the action set of this entity
182 22 Shuvam Misra
183
It is important to note that a pattern does not need to have just the attributes listed in `patternschema`. It may also include actions listed in the `ruleschema`. Each such action becomes an implicit attribute of type `bool` for this class.
184 1 Shuvam Misra
185
For attributes which are of type `int`, `float`, `str` and `ts`, the following comparison operators are available:
186
* Greater than or equal to: `ge`
187
* Greater than: `gt`
188
* Less than or equal to: `le`
189
* Less than: `lt`
190
* Equal to: `eq`
191
* Not equal to: `ne`
192
193
Collation sequences for strings are system dependent, and will need to be standardised so that they work reliably across programming languages and Unicode strings in any language. That's an implementation issue.
194
195 21 Shuvam Misra
For `enum` and `bool` types, only `eq` and `ne` are available.
196 1 Shuvam Misra
197 2 Shuvam Misra
## Representing an action
198 1 Shuvam Misra
199
A rule has a set of one or more actions. The following are all examples of the action section of rules:
200
* `invitefordiwali`
201
* `discount=7`
202
* `shipwithoutpo`
203
* `CALL=intlbiz`
204
205
The terms which identify actions, *e.g.* `invitefordiwali`, will automatically be converted to lower-case and stored in the system. Reserved attribute names like `CALL`, `RETURN`, `EXIT`, will always be in uppercase. For an attribute assignment, the value of the attribute will be everything after the first `=` character till the end of the string, thus supporting multi-word values, *e.g.*
206
* `reprimand=This cannot go on any longer`
207
208
The action portion of a rule can have zero or one occurrence of a `CALL` term, a `RETURN` term, and an `EXIT` term. If it contains both a `RETURN` and an `EXIT`, then the `RETURN` will be ignored.
209
210
The action portion of a rule will have the following structure, shown here as an example:
211
``` json
212
"ruleactions": {
213
    "actions": [ "christmassale", "vipsupport" ],
214
    "attribs": [ "shipby=fedex" ],
215
    "call": "internationalrules",
216
    "return": true,
217
    "exit": false
218
}
219
```
220
This example shows all five attributes of `ruleactions`, but in reality, some of the attributes will typically be missing from most of the rules.
221
222 2 Shuvam Misra
## An entire rule
223 1 Shuvam Misra
224
This is what an entire rule looks like:
225
226
``` json
227
"rule": {
228
    "class": "inventoryitems",
229
    "ver": 4,
230
    "rulepattern": [{
231
        "attr": "cat",
232
        "op": "eq",
233
        "val": "textbook"
234
    },{
235
        "attr": "mrp",
236
        "op": "ge",
237
        "val": 5000
238
    }],
239
    "ruleactions": {
240
        "actions": [ "christmassale" ],
241
        "attribs": [ "shipby=fedex" ]
242
    }
243
}
244
```
245
246
This structure represents one rule. The rule applies to entities of class `inventoryitems`. It has a pattern section which tries to match two attributes and an action section which throws up one action and one assignment.
247
248
A rule has a version number, which is incremented whenever the rule is updated. This number is for internal logging and rule engine debugging.
249
250
An array of such structures is a set of rules, and will be traversed in the order in which the rules appear in the array. Named rulesets will be represented thus:
251
``` json
252
"ruleset": {
253
    "class": "inventoryitems",
254
    "setname": "overseaspo",
255
    "rules": [{
256
        "ver": 4,
257
        "rulepattern": {
258
            :
259
            :
260
        },
261
        "ruleactions": {
262
            :
263
            :
264
        }
265
    }, {
266
        "ver": 3,
267
        "rulepattern": {
268
            :
269
            :
270
        },
271
        "ruleactions": {
272
            :
273
            :
274
        }
275
    }]
276
}
277
```
278
The example above shows a ruleset named `overseaspo` for class `inventoryitems` which has two rules. This ruleset may be invoked from any other rule with the action `CALL=overseaspo`.
279
280 2 Shuvam Misra
## The schema manager
281 1 Shuvam Misra
282 7 Shuvam Misra
The schema for each class of entities may be written by hand using a text editor. JSON or YAML files are easy to write. If the schema of one class has less than a dozen attributes, it may be short enough to edit or audit by hand. However, a tool to manage and maintain the schema eliminates typos and enforces various types of consistency, and a second-level implementation of a schema manager may also enforce authorisation policies.
283 1 Shuvam Misra
284
A schema manager will have the following features:
285
* It will allow the user to create new instances of `ruleschema`
286
* It will sharply restrict editing of, and prevent deletion of any `patternschema` block or `actionschema` block if there are rules defined in the rules engine for this class of entities. In other words, schema are editable only as long as there are no rules for the class. The only kind of editing it will permit for “live” schema are
287
  * the addition of additional attributes in a `patternschema` or
288
  * additional attributes, action names or tags in an `actionschema`.
289
* It will ensure that there is no scope for typos when defining the schema.
290
291 3 Shuvam Misra
## The rule manager
292 1 Shuvam Misra
293
The rule manager will allow a user to manage rules. Core functionality:
294
* It will provide a user interface to let the user edit rules.
295
* It will check each rule against the schema for the class, and will not give the user the opportunity to define any rule inconsistent with the schema.
296
* It will allow the user to move a rule up or down in the sequence, since ordering is important.
297
* If a rule is being defined with a `CALL` action, then the rule manager will ensure that a ruleset with that target name exists.
298
* Most important: it will provide a testing facility by which sample entities may be submitted to the rule engine for testing, and the rule manager will display a full trace showing which rules were attempted to match, which rules actually matched, and how the result set of actions, attributes, *etc* grew with each step. This feature will be provided without having to save the rule changes.
299
* Finally, when the editing session is complete and all rulesets need to be saved, it will perform a detailed cross-validation of all rules across each other to ensure consistency. If there is any inconsistency, it will give readable explanations of the problems and not permit saving of the updates.
300
301 2 Shuvam Misra
## The matching engine
302 1 Shuvam Misra
303
The matching engine has a one-line job. It will take a full set of attributes of one entity, apply all the rules which apply to its class, and return with the list of actions, attributes, *etc* from all the matching rules.
304
305 11 Shuvam Misra
The operation of the engine, in a highly simplified notation, is:
306
```
307
for each rule in the ruleset do
308
    match the pattern of the rule with the entity
309
    if the pattern matches, then
310
        collect the actions from the rule into the actionset
311
    endif
312
endfor
313
```
314 1 Shuvam Misra
315 2 Shuvam Misra
### Matching one rule's pattern
316 1 Shuvam Misra
317 18 Shuvam Misra
The algorithm for the matching of one rule's pattern is shown below. Here, it is assumed that the object being matched is in `entity` and pattern of the rule being matched is in `rulepattern`. It is assumed that the matching engine  may have proceeded some distance in its matching process, and may have collected zero or more actions in its `actionset`.
318 1 Shuvam Misra
```
319
func matchOnePattern()
320 18 Shuvam Misra
    input parameters: entity, rulepattern, actionset
321 1 Shuvam Misra
    returns patternmatch: boolean
322
323 16 Shuvam Misra
#
324
# In this loop we iterate through terms from our pattern array
325
# one by one
326 1 Shuvam Misra
#
327
for patternterm in rulepattern do
328 18 Shuvam Misra
    #
329
    # We get into a loop, stepping through the attributes of this
330
    # entity to pull out the value of the attribute
331
    # we will now be matching against the term we have selected
332
    # for this iteration of the outer loop, i.e. patternterm
333
    #
334 19 Shuvam Misra
    entitytermval = null
335 18 Shuvam Misra
    for entityoneterm in entity.attrs do
336
        if entityoneterm.attr == patternterm.attr then
337
            entitytermval = entityoneterm.val
338
        endif
339 1 Shuvam Misra
    endfor
340 19 Shuvam Misra
341
    if entitytermval == null then
342
        #
343
        # We reach here if none of the attributes in the entity
344
        # has a name matching the term in the pattern array. This
345
        # can only mean one thing: this is a pattern clause which
346 26 Shuvam Misra
        # will match a tag with which this entity has been tagged.
347
        # A tag is an action clause which has already been collected
348
        # against this entity from matching a previous rule.
349 19 Shuvam Misra
        #
350
        # So we will now cycle through the action clauses in the
351
        # actionset to see if any of those matches this patternterm.
352
        #
353
        for oneactionclause in actionset.actions do
354
            if oneactionclause == patternterm.attr then
355
                #
356
                # Bingo! We have found an action clause which matches
357
                # the name of an entry in the pattern array.
358
                #
359
                entitytermval = "true"
360 18 Shuvam Misra
            endif
361 19 Shuvam Misra
        endfor
362 18 Shuvam Misra
    endif
363
364 19 Shuvam Misra
    if entitytermval == null then
365
        #
366
        # If we reach here, it means that we have a term of the entity which
367
        # is not listed in the pattern at all. Every entity has all the
368
        # attributes listed against its class in the schema, but rule patterns
369
        # may have just one or two terms, so it's likely that many of the
370
        # attributes of an entity may not match any term in the pattern array.
371
        #
372
        # In that case we just loop to the next term in the pattern array.
373
        #
374
        continue
375
    endif
376
377 1 Shuvam Misra
    case patternterm.op in
378
    "eq":
379
        if entitytermval != patternterm.val then
380
            return false
381
        endif
382
    "ne":
383
        if entitytermval == patternterm.val then
384
            return false
385
        endif
386
    endcase
387
    if patternterm.type in [ "int", "float", "ts", "str" ] then
388
        case patternterm.op in
389
        "le":
390
            if entitytermval > patternterm.val then
391
                return false
392
            endif
393
        "lt":
394
            if entitytermval >= patternterm.val then
395
                return false
396
            endif
397
        "ge":
398
            if entitytermval < patternterm.val then
399
                return false
400
            endif
401
        "gt":
402
            if entitytermval <= patternterm.val then
403
                return false
404
            endif
405
        default:
406
            log error with priority = CRITICAL: "system inconsistency with BRE rule terms"
407
        endcase
408
    endif
409
endfor
410
411
return true
412 2 Shuvam Misra
```
413 1 Shuvam Misra
414
### Collecting the actions from one rule
415
416
If the pattern for one rule matches the entity being processed, then the actions of that rule will need to be added to the result set for that entity. Here we assume that the result of the action-collection function will return an object of the following structure. This object will be passed as input to the action-collecting function, and a (possibly extended) object will be returned, after merging the input object with the action terms from the rule just matched. The object structure will be:
417
``` json
418
"actionset": {
419
    "actions": [ "dodiscount", "yearendsale" ],
420
    "attribs": [ "shipby=fedex" ],
421
    "call": "overseaspo",
422
    "return": true,
423
    "exit": false
424 8 Shuvam Misra
}
425 1 Shuvam Misra
```
426
These five attributes will always be present in the object. The `actions` and `attribs` attributes will carry an array of strings, which will be a union set of all the action terms and attribute assignments collected from rules matched so far. The `call` attribute will either be a zero-length string (signifying that no ruleset needs to be called after this rule returns) or will carry the name of one ruleset to call after the current rule. The `return` and `exit` attributes will carry boolean values.
427
428
Performing a set union of action names is straightforward. Performing a set union of attribute assignments requires choosing one value of an attribute, if there was already the same attribute in the `actionset` and the current rule's actions also assigns a value to that attribute. In that case, the old value of the attribute will be overwritten by the new value.
429
430
```
431
function collectActions()
432
input parameters: actionset, ruleactions
433
    returns actionset
434
435
actionset.actions = actionset.actions UNION ruleactions.actions
436
actionset.attribs = actionset.attribs UNION ruleactions.attribs
437
438
actionset.call = ""
439
actionset.return = false
440
actionset.exit = false
441
if ruleactions.call is defined, then
442
    actionset.call = ruleactions.call
443
endif
444
if ruleactions.return is defined, then
445
    actionset.return = true
446
endif
447
if ruleactions.exit is defined,  then
448 14 Shuvam Misra
    actionset.exit = true
449
endif
450 1 Shuvam Misra
451
return actionset
452
```
453
454 13 Shuvam Misra
The matching engine needs to look at what has emerged from `collectActions()` and then take action. The flow of the matching engine will change based on the values of the `call`, `return` and `exit` attributes.
455
456 1 Shuvam Misra
### Representing one entity
457 14 Shuvam Misra
458 1 Shuvam Misra
The matching engine matches all the rules of a ruleset against one instance of a class, like one instance of `vendor` or `inventoryitem`. How do we represent this object instance, when the type and the fields are all dynamically determined at runtime and varies from invocation to invocation? Here is one example:
459
``` json
460
"inputentity": {
461 14 Shuvam Misra
    "class": "inventoryitems",
462
    "attribs": [{
463
        "name": "cat",
464
        "val": "refbook"
465
    },{
466
        "name": "mrp",
467
        "val": "1350"
468
    },{
469
        "name": "fullname",
470
        "val": "Advanced Level Physics, 2/ed"
471
    },{
472
        "name": "ageinstock",
473
        "val": "20"
474
    },{
475
        "name": "inventoryqty",
476 1 Shuvam Misra
        "val": "540"
477
    }]
478 14 Shuvam Misra
}
479 1 Shuvam Misra
```
480 14 Shuvam Misra
As this example highlights, all the values are supplied of type string, so that they may be converted from strings to their respective types later. This allows the data structure for specifying an object instance to be strongly typed and still allow attributes of all types to be captured. One more point illustrated is that **all attributes in the `patternschema` of the class must be present** in each object instance of that class.
481
482 1 Shuvam Misra
This is the way the entity will be submitted to the matching engine for processing.
483 13 Shuvam Misra
484 27 Shuvam Misra
### `doMatch()`: the matching function
485 13 Shuvam Misra
486 14 Shuvam Misra
This engine will go through rules one after another, and for each rule, it will call `matchOnePattern()`. If `matchOnePattern()` returns `true`, it will call `collectActions()`. And then it will inspect the result obtained from `collectActions()` and decide what to do next.
487
488
This engine will be implemented by the `getRules()` function, which will occasionally call itself recursively. It will be called with three parameters:
489
* an `inputentity`, which will be matched against the ruleset
490 1 Shuvam Misra
* a `ruleset` which will be traversed by the engine
491 14 Shuvam Misra
* an `actionset`, which collects the result of the action matching
492 1 Shuvam Misra
493
The pseudocode has been written with the assumption that parameters are all pass-by-value.
494
495
So, the `doMatch()` engine will work in the following way:
496 14 Shuvam Misra
```
497 1 Shuvam Misra
function doMatch()
498
input parameters: inputentity, ruleset, actionset
499 14 Shuvam Misra
    returns actionset
500
501
for each onerule in ruleset do
502 23 Shuvam Misra
    if matchOnePattern(inputentity, onerule.pattern, actionset) == true then
503 14 Shuvam Misra
        actionset = collectActions(actionset, onerule.actions)
504
        #
505
        # now check if the actions just collected includes an EXIT clause
506
        #
507
        if actionset.exit == true then
508
            return actionset
509
        endif
510
        #
511
        # If there was no EXIT clause, check if there was a RETURN clause
512
        #
513
        if actionset.return == true then
514
            actionset.return = false
515
            return actionset
516
        endif
517
        #
518
        # If there was no EXIT or RETURN, check if there was a CALL clause
519
        #
520 15 Shuvam Misra
        if actionset.call is not null then
521
            settocall = actionset.call
522
            if settocall.class != inputentity.class then
523
                log error with priority = CRITICAL:
524 14 Shuvam Misra
                       "system inconsistency with BRE rule terms, attempting to call ", settocall, " from ", ruleset, "!"
525
            endif
526
            actionset.call = null
527
            doMatch(inputentity, settocall, actionset)
528
            #
529
            # If the called ruleset has set EXIT to true, then we too need to
530
            # exit, and our caller too needs to exit, ad infinitum
531
            #
532
            if actionset.exit == true then
533
                return actionset
534
            endif
535
        endif
536
    endif
537
    #
538
    # We come here because we've done one rule and we've neither been thrown
539
    # out by an EXIT nor a RETURN clause. So we now loop to the next rule in
540
    # our ruleset.
541 13 Shuvam Misra
    #
542 14 Shuvam Misra
endfor
543 10 Shuvam Misra
544 14 Shuvam Misra
return actionset
545
```
546 1 Shuvam Misra
547 24 Shuvam Misra
This matching engine will be able to traverse all rulesets, make "subroutine calls" from one ruleset to another, and finally come up with a consolidated `actionset`.
548
549
The outermost calling code which calls the outermost layer of `doMatch()` for a given entity will initialise an empty `actionset` and pass it in. After all the ruleset traversals, `doMatch()` will return with a loaded `actionset`, which will then be returned to the client of the BRE.
550 1 Shuvam Misra
551
### API for the matching engine
552
553
The matching engine must support the following set of operations:
554
* `doMatch()`: take an entity, pass it through all relevant rules and rulesets, and respond with the set of final results.
555
* `getAttrSet()`: take a class name, pull out from the `patternschema` all the attributes listed against that class, with full details. This is useful to let the caller know what attributes are to be specified when calling `doMatch()`.