You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: drools-docs/src/modules/ROOT/pages/language-reference-traditional/_drl-functions-con.adoc
+24Lines changed: 24 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -73,3 +73,27 @@ end
73
73
====
74
74
A function declared in a DRL file cannot be imported to a rule in a different package while a Java static method in a different package can be imported.
75
75
====
76
+
77
+
// see https://stackoverflow.com/a/75788542/893991
78
+
[NOTE]
79
+
====
80
+
A function body cannot access globals.
81
+
82
+
From the RHS of a rule, you can always pass a global as a function parameter when invoking the function, for exmaple:
83
+
84
+
[source]
85
+
----
86
+
global List names;
87
+
88
+
rule "Using a function with parameters"
89
+
when
90
+
// Empty
91
+
then
92
+
addName( names, "James" );
93
+
end
94
+
95
+
function void addName(List names, String applicantName) {
Copy file name to clipboardExpand all lines: drools-docs/src/modules/ROOT/pages/language-reference/_drl-rules.adoc
+44-38Lines changed: 44 additions & 38 deletions
Original file line number
Diff line number
Diff line change
@@ -3067,10 +3067,41 @@ In this example, the condition is intended to be empty but the word `None` is us
3067
3067
--
3068
3068
3069
3069
[id="con-drl-legacy_{context}"]
3070
-
== Legacy DRL conventions
3070
+
== Legacy DRL conventions for rule unit
3071
3071
3072
3072
[role="_abstract"]
3073
-
The following Drools Rule Language (DRL) conventions are no longer applicable or optimal in {PRODUCT} but might be available for backward compatibility.
3073
+
The following Drools Rule Language (DRL) conventions are no longer applicable or optimal with rule unit but might be available for backward compatibility. Note that they are still fully supported in traditional DRL.
3074
+
3075
+
=== Global variables
3076
+
3077
+
Global variables (`global`) are replaced by properties of a RuleUnitData. Properties of a RuleUnitData can be referred in DRL, so used just like global variables.
3078
+
3079
+
.Example properties of a RuleUnitData as global
3080
+
[source,java]
3081
+
----
3082
+
public class HelloWorldUnit implements RuleUnitData {
3083
+
3084
+
private final List<String> results = new ArrayList<>();
3085
+
3086
+
public List<String> getResults() {
3087
+
return results;
3088
+
}
3089
+
3090
+
// DataSources...
3091
+
}
3092
+
----
3093
+
3094
+
[source]
3095
+
----
3096
+
unit HelloWorldUnit;
3097
+
3098
+
rule HelloWorld
3099
+
when
3100
+
/strings [ this == "Hello World" ]
3101
+
then
3102
+
results.add("it worked!");
3103
+
end
3104
+
----
3074
3105
3075
3106
=== Legacy functions in DRL
3076
3107
@@ -3126,34 +3157,9 @@ end
3126
3157
A function declared in a DRL file cannot be imported to a rule in a different package while a Java static method in a different package can be imported.
3127
3158
====
3128
3159
3129
-
// see https://stackoverflow.com/a/75788542/893991
3130
-
[NOTE]
3131
-
====
3132
-
A function body cannot access globals.
3133
-
3134
-
From the RHS of a rule, you can always pass a global as a function parameter when invoking the function, for exmaple:
3135
-
3136
-
[source]
3137
-
----
3138
-
global List names;
3139
-
3140
-
rule "Using a function with parameters"
3141
-
when
3142
-
// Empty
3143
-
then
3144
-
addName( names, "James" );
3145
-
end
3146
-
3147
-
function void addName(List names, String applicantName) {
3148
-
names.add(applicantName);
3149
-
}
3150
-
----
3151
-
3152
-
====
3153
-
3154
3160
=== Legacy rule attributes
3155
3161
3156
-
The following attributes were used in earlier versions of the {RULE_ENGINE} to provide grouping of rules across a rule base. These attributes are superseded by DRL rule units and are only available for backward compatibility reasons. If you need to group your rules, use DRL rule units as a clearer and simpler grouping method.
3162
+
The following attributes are used in traditional DRL to provide grouping of rules across a rule base. If you need to group your rules with rule unit, rule units themselves are a clearer and simpler grouping method.
In {PRODUCT}, the preferred syntax for DRL rule conditions is through OOPath expressions. For legacy use cases, you can write rules using traditional pattern matching. In this case, you must explicitly indicate the data source using the `from` clause, as shown in the following comparative examples:
3183
+
With rule unit, the preferred syntax for DRL rule conditions is through OOPath expressions. For legacy use cases, you can write rules using traditional pattern matching. In this case, you must explicitly indicate the data source using the `from` clause, as shown in the following comparative examples:
3178
3184
3179
3185
.Example `PersonRules` DRL file using OOPath notation
3180
3186
[source]
@@ -3185,11 +3191,11 @@ unit PersonRules;
3185
3191
import org.acme.Person;
3186
3192
3187
3193
rule isAdult
3188
-
when
3189
-
$person: /persons[ age > 18 ]
3190
-
then
3194
+
when
3195
+
$person: /persons[ age > 18 ]
3196
+
then
3191
3197
modify($person) {
3192
-
setAdult(true)
3198
+
setAdult(true)
3193
3199
};
3194
3200
end
3195
3201
----
@@ -3203,11 +3209,11 @@ unit PersonRules;
3203
3209
import org.acme.Person;
3204
3210
3205
3211
rule isAdult
3206
-
when
3207
-
$person: Person(age > 18) from person
3208
-
then
3212
+
when
3213
+
$person: Person(age > 18) from person
3214
+
then
3209
3215
modify($person) {
3210
-
setAdult(true)
3216
+
setAdult(true)
3211
3217
};
3212
3218
end
3213
3219
----
@@ -3219,7 +3225,7 @@ Using OOPath, you can write nested paths. For example, `/persons[name == "Mark"]
3219
3225
3220
3226
=== Legacy DRL rule condition elements
3221
3227
3222
-
The following rule condition elements (keywords) are obsolete in {PRODUCT}:
3228
+
The following rule condition elements (keywords) are obsolete with rule unit:
0 commit comments