Wiki source code of Drools Integration
Hide last authors
1.1 | 1 | = Introduction = | |
2 | |||
3 | In 3.0, there is the possibility to activate rules in Operations. | ||
4 | |||
5 | Rules is a new way to express some business coding that sometimes is more appropriate than procedural javascript. | ||
6 | Note that rules do not replace JavaScript, but in some cases is more efficient and more maintainable. | ||
7 | |||
8 | |||
9 | By default, the rules management system uses JBoss Drools: [[http:~~/~~/www.jboss.org/drools>>http://www.jboss.org/drools]] | ||
10 | |||
11 | |||
12 | |||
13 | = Using Rules in Operations = | ||
14 | |||
15 | A Rule based step can be added in operations: | ||
16 | |||
17 | [[image:img53.png||alt="Rule operation Step"]] | ||
18 | |||
19 | |||
20 | An example of rules: | ||
21 | |||
22 | |||
23 | [[image:img52.png||alt="Rules based example"]]: | ||
24 | |||
25 | |||
26 | == Rules definitions == | ||
27 | |||
28 | Rules can be | ||
29 | |||
30 | * inline: the rule (or rules) are defined directly in the step. | ||
31 | * knowledge base: the rules are defined in external files | ||
32 | |||
33 | |||
34 | == Objects added to the rule set == | ||
35 | |||
36 | In an operation step, records (data) are added to the rule session. | ||
37 | |||
38 | * if the current record (data) is an entity, the entity is added | ||
39 | * if the data is a list of entities, all the entities are added in the rule session | ||
40 | |||
41 | == Setting values in Rules == | ||
42 | |||
43 | In the "then" clause of rules, you can set properties: | ||
44 | |||
45 | {{code language="javascript"}} | ||
46 | $alarm.almPriority = "Low"; | ||
47 | {{/code}} | ||
48 | |||
49 | == Calling Operations in Rules == | ||
50 | |||
51 | You can call operations on objects that are defined in the Rules: | ||
52 | |||
53 | * if the operation has the "data" parameter defined as of type "Entity" of the same type, the operation is considered as an "instance" operation, and can be called directly on the object. Example: | ||
54 | |||
55 | {{code language="javascript"}} | ||
56 | $alarm.Save(); | ||
57 | {{/code}} | ||
58 | |||
59 | * if the operation has different data defined as the case above, the operation can be called using the "Registry" object: | ||
60 | |||
61 | {{code language="javascript"}} | ||
62 | $alarm = Registry.rqServerMonitorAlarm.Trigger($monitor, "MEM_HIGH_0001"); | ||
63 | {{/code}} | ||
64 | |||
65 | |||
66 | == Rules Examples == | ||
67 | |||
68 | Here are some examples of rules. In those examples, you can see: | ||
69 | |||
70 | * accessing properties (with the mvel dialect) | ||
71 | * joining entities by reference | ||
72 | * inserting related entities in the rule session for further processing | ||
73 | * setting properties | ||
74 | * calling operation on entities | ||
75 | * using salience | ||
76 | |||
77 | |||
78 | {{code language="javascript"}} | ||
79 | package com.requea.rules; | ||
80 | |||
81 | import com.requea.entity.Registry; | ||
82 | import com.requea.entity.*; | ||
83 | |||
84 | |||
85 | rule"New monitor" | ||
86 | dialect"mvel" | ||
87 | when | ||
88 | $monitor : rqServerMonitor() | ||
89 | then | ||
90 | // eval server info | ||
91 | insert($monitor.rqServer); | ||
92 | // eval memory info | ||
93 | insert($monitor.rqHeapMemoryInfo); | ||
94 | end | ||
95 | |||
96 | |||
97 | |||
98 | rule"MEM_HIGH_0001 - When memory is too high" | ||
99 | dialect"mvel" | ||
100 | when | ||
101 | $mem : rqMemoryInfo(rqUsed > 0.9 * rqMax) | ||
102 | $monitor : rqServerMonitor(rqHeapMemoryInfo == $mem) | ||
103 | then | ||
104 | // trigger an alarm | ||
105 | $alarm = Registry.rqServerMonitorAlarm.Trigger($monitor, "MEM_HIGH_0001"); | ||
106 | // this is a low priority alarm | ||
107 | $alarm.almPriority = "Low"; | ||
108 | insert($alarm); | ||
109 | end | ||
110 | |||
111 | |||
112 | |||
113 | rule"MEM_HIGH_0001 - Memory Alarm on a production server" | ||
114 | dialect"mvel" | ||
115 | when | ||
116 | $server : rqRepoServerConfiguration(rqProduction == true) | ||
117 | $monitor : rqServerMonitor(rqServer == $server) | ||
118 | $alarm : rqServerMonitorAlarm(almPriority != "High", rqServerMonitor == $monitor) | ||
119 | then | ||
120 | // trigger an alarm | ||
121 | modify($alarm) { | ||
122 | // priority should be high | ||
123 | almPriority = "High"; | ||
124 | } | ||
125 | end | ||
126 | |||
127 | |||
128 | rule"Save Alarms" | ||
129 | dialect"mvel" | ||
130 | salience -50 | ||
131 | when | ||
132 | $alarm : rqServerMonitorAlarm() | ||
133 | then | ||
134 | // save the alarm | ||
135 | $alarm.Save(); | ||
136 | end | ||
137 | |||
138 | {{/code}} | ||
139 | |||
140 | |||
141 |