Project

General

Profile

Cruxflowdesign » History » Version 19

Kaushal Alate, 30/10/2023 02:52 PM

1 1 Shuvam Misra
# Algorithms and data structures for the flow engine
2
3
{{>toc}}
4
5 12 Shuvam Misra
This page is best understood only after studyihg [[cruximplement|the algorithms and data structures of the BRE]].
6 1 Shuvam Misra
7
Here too, we need a rules schema, rulesets and a matching engine.
8
9
## Rules schema
10
11 2 Shuvam Misra
In the BRE, each rules schema has two parts: the pattern schema and the action schema. Each `rulesschema` block is tagged with a `class` attribute, specifying which class the schema applies to. There is one rules schema for each class of entities.
12 1 Shuvam Misra
13
In the flow engine, there is no `class` -- there is a `process` instead. A workflow schema applies to a process.
14
15
The pattern schema specification is identical here to that used in the BRE. 
16
17 2 Shuvam Misra
In the flow engine, there is no `actionschema`, but a `flowschema`. The flow schema is much simpler than an action schema. The output of a flow engine matching exercise is just the ID of one step. Therefore the flow schema here is a list of possible steps.
18 1 Shuvam Misra
19
``` json
20
"ruleschema": {
21
    "process": "customerkyc",
22
    "patternschema": {
23
        "attr": [{
24 3 Shuvam Misra
            "name": "accttype",
25 1 Shuvam Misra
            "type": "enum",
26 3 Shuvam Misra
            "vals": [ "savings", "current", "recurring", "fixeddeposit", "ppf" ]
27 1 Shuvam Misra
        },{
28 3 Shuvam Misra
            "name": "acctholdertype",
29
            "type": "enum",
30
            "vals": [ "individual", "joint", "corporate", "hinduundivided", "partnership" ]
31
        },{
32 4 Shuvam Misra
            "name": "branchtype",
33
            "type": "enum",
34
            "vals": [ "urban", "semirural", "rural" ]
35 1 Shuvam Misra
        },{
36 4 Shuvam Misra
            "name": "branchcode",
37
            "type": "str"
38 1 Shuvam Misra
        },{
39 4 Shuvam Misra
            "name": "refererquality",
40
            "type": "int",
41
            "valmin": 0,
42
            "valmax": 5
43 1 Shuvam Misra
        },{
44 4 Shuvam Misra
            "name": "districtcode",
45 1 Shuvam Misra
            "type": "int"
46
        }]
47
    }
48
    "flowschema": {
49 4 Shuvam Misra
        "steps": [ "initialdoc", "aadhaarcheck", "creditbureauchk", "pancheck", "bankdetails", "referenchk", "complete" ]
50 1 Shuvam Misra
    }
51
}
52
```
53
In the example above:
54 13 Shuvam Misra
* `class` of the business rules schema is replaced with `process`
55 1 Shuvam Misra
* `patternschema` remains unchanged
56 15 Shuvam Misra
* `actionschema` is replaced with `flowschema` and the structure of `flowschema` is just one array of words. Each word here is an ID of a step. So, the `flowschema.steps` lists all the valid steps which the flow engine may return after matching an entity with the rules. The array of words in the`steps` array is totally irrelevant; treat it as a set, not an array.
57 4 Shuvam Misra
58
The example above attempts to illustrate what a workflow may incorporate when a customer is applying to open a bank account and the workflow engine is guiding the processing centre about the sequence of steps to follow to complete the applicant's KYC (Know Your Customer) process. In this example, the workflow decisions will be taken based on the following parameters of the application:
59
* `accttype`: whether the account being opened is a current account, a savings account, *etc*
60
* `acctholdertype`: whether the applicant is an individual, a Hindu Undivided Family, an incorporated company, a partnership firm, *etc*
61
* `branchtype`: whether the branch is in an urban area, a semi-rural area or a rural area -- customer assessment norms may be relaxed or done quite differently for rural and urban areas
62
* `branchcode`: an integer code which has been assigned for each branch. With this attribute, it is possible to define patterns which match specific branches
63
* `refererquality`: an integer which, it is assumed, will have a higher value if the quality of the referer is higher in the bank's eyes
64
* `districtcode: an integer uniquely identifying the district where the applicant is located; customer KYC norms may differ from district to district
65
66
This set of six attributes of each account opening application can form a solid foundation to define a rich set of rules of how the KYC process will proceed.
67 5 Shuvam Misra
68
In addition there will always be one additional attribute in the pattern section of each rule -- the `step`. This will specify the current `step` the process is on. The ruleset(s) will look at all the other attributes, the current step the process is at, and will attempt to come up with a specification of the next step to execute. That's the entire *raison d'etre* for a workflow engine.
69
70
## Specifying a ruleset
71
72
The following is a hyopthetical ruleset for the process `customerkyc`.
73
``` json
74 6 Shuvam Misra
"ruleset": {
75
    "ver": 4,
76
    "process": "customerkyc",
77
    "setname": "prioritylending",
78
    "rules": [{
79
        "rulepattern": {
80
            "step": "initialdoc",
81 7 Shuvam Misra
            "attrs": [{
82
                "attr": "branchtype",
83
                "op": "eq",
84
                "val": "rural",
85
            },{
86
                "attr": "accttype",
87
                "op": "eq",
88
                "val": "savings"
89 8 Shuvam Misra
            }]
90 6 Shuvam Misra
        },
91 7 Shuvam Misra
        "rulenextstep": "aadharchk",
92
        "call": ""
93 1 Shuvam Misra
    },{
94
        "rulepattern": {
95
            "step": "initialdoc",
96 7 Shuvam Misra
            "attrs": [{
97
                "attr": "branchtype",
98
                "op": "eq",
99
                "val": "semirural"
100
            },{
101
                "attr": "accttype",
102
                "op": "ne",
103
                "val": "ppf"
104 8 Shuvam Misra
            }]
105 6 Shuvam Misra
        },
106 7 Shuvam Misra
        "call": "overseaskyc",
107
        "rulenextstep": ""
108 1 Shuvam Misra
    },{
109
        :
110
    }],
111
}
112
```
113 7 Shuvam Misra
Each `ruleset` has a perpetually-incrementing integer attribute called `ver`. This tracks changes to the ruleset. The ruleset also has
114
* `process`, which names a process, and
115
* `setname`, which specifies the ruleset name
116
* `rules`, which is an array of rules.
117
118
In the `rules` array, each element is a rule, and a rule has two parts:
119
* `rulepattern` which lists various attributes to match
120 9 Shuvam Misra
* a what-to-do part, which means either a `rulenextstep` which specifies the next step or a `call` which specifies another ruleset to call
121 7 Shuvam Misra
122 17 Shuvam Misra
Inside the `rulepattern`, there is an array of attributes and values, which are used for matching. There is one special attribute, called `step`, which specifies the state of the caller in the process. The attribute `step` is not listed in the `patternschema`. The term `"step": "initialdoc"` actually is a shorthand for
123 7 Shuvam Misra
``` json
124
{
125
    "attr": "step",
126
    "op": "eq",
127
    "val": "initialdoc"
128
}
129
```
130 1 Shuvam Misra
131 17 Shuvam Misra
Just like the special attribute `step`, there is another special attribute `stepfailed` which is not listed in `patternschema`. This is a special attribute of type `bool`. It may be specified by the caller when calling the flow engine, with `"stepfailed": "true"`. The meaning of this attribute is that the previous step failed. This is an extra facility for the workflow author to decide whether to take the flow engine along a different path and come up with a different value for `rulenextstep`. The attribute may also be specified with `"stepfailed": "false"`, as needed. The term `"step": "aadhaarchk", "stepfailed": "true"` means that the last step in the workflow which was attempted was the Aadhaar identity check, and that step failed. It is shorthand for
132
``` json
133
[{
134
    "attr": "step",
135
    "op": "eq",
136
    "val": "aadhaarchk"
137
},{
138
    "attr": "stepfailed",
139
    "op": "eq",
140
    "val": "true"
141
}]
142 18 Shuvam Misra
```
143 17 Shuvam Misra
144 1 Shuvam Misra
Each rule can end with **either** the ID of a next step, **or** a ruleset to call. Since the matching engine for workflow follows the FIRST-MATCH algorithm, it unwinds from all subroutine calls and nestings as soon as it matches a rule with a `rulenextstep`.
145 15 Shuvam Misra
146
The step ID in `rulenextstep` can have a special value not listed in `flowschema.steps`. This value is `END`. If `rulenextstep` has this value, it tells the caller that the workflow for this process is complete. The caller then is expected to stop calling the flow engine asking for what-next.
147 19 Kaushal Alate
148
## The `queryWFE()` function (can be renamed...)