1 #-------------------------------------------------------------------------------
2 # ============LICENSE_START=======================================================
3 # Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 # Modifications Copyright (C) 2020 Nordix Foundation.
5 # ================================================================================
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
10 # http://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
18 # SPDX-License-Identifier: Apache-2.0
19 # ============LICENSE_END=========================================================
20 #-------------------------------------------------------------------------------
22 model create name=MyFirstPolicyModel version=0.0.1 uuid=540226fb-55ee-4f0e-a444-983a0494818e description="This is my first Apex Policy Model."
24 schema create name=assistant_ID_type version=0.0.1 uuid=36df4c71-9616-4206-8b53-976a5cd4bd87 description="A type for 'assistant_ID' values" flavour=Java schema=java.lang.Long
26 schema create name=authorised_type version=0.0.1 uuid=d48b619e-d00d-4008-b884-02d76ea4350b description="A type for 'authorised' values" flavour=Java schema=java.lang.Boolean
28 schema create name=branch_ID_type version=0.0.1 uuid=6468845f-4122-4128-8e49-0f52c26078b5 description="A type for 'branch_ID' values" flavour=Java schema=java.lang.Long
30 schema create name=item_ID_type version=0.0.1 uuid=4f227ff1-aee0-453a-b6b6-9a4b2e0da932 description="A type for 'item_ID' values" flavour=Java schema=java.lang.Long
32 schema create name=message_type version=0.0.1 uuid=ad1431bb-3155-4e73-b5a3-b89bee498749 description="A type for 'message' values" flavour=Java schema=java.lang.String
34 schema create name=notes_type version=0.0.1 uuid=eecfde90-896c-4343-8f9c-2603ced94e2d description="A type for 'notes' values" flavour=Java schema=java.lang.String
36 schema create name=price_type version=0.0.1 uuid=52c2fc45-fd8c-463c-bd6f-d91b0554aea7 description="A type for 'amount'/'price' values" flavour=Java schema=java.lang.Long
38 schema create name=quantity_type version=0.0.1 uuid=ac3d9842-80af-4a98-951c-bd79a431c613 description="A type for 'quantity' values" flavour=Java schema=java.lang.Integer
40 schema create name=sale_ID_type version=0.0.1 uuid=cca47d74-7754-4a61-b163-ca31f66b157b description="A type for 'sale_ID' values" flavour=Java schema=java.lang.Long
42 schema create name=timestamp_type version=0.0.1 uuid=fd594e88-411d-4a94-b2be-697b3a0d7adf description="A type for 'time' values" flavour=Java schema=java.lang.Long
44 task create name=MorningBoozeCheck version=0.0.1 uuid=3351b0f4-cf06-4fa2-8823-edf67bd30223 description=LS
45 This task checks if the sales request is for an item that contains alcohol.
46 If the local time is between 00:00:00 and 11:30:00 then the sale is not authorised. Otherwise the sale is authorised.
47 In this implementation we assume that all items with item_ID values between 1000 and 2000 contain alcohol :-)
49 task inputfield create name=MorningBoozeCheck version=0.0.1 fieldName=sale_ID schemaName=sale_ID_type schemaVersion=0.0.1
50 task inputfield create name=MorningBoozeCheck version=0.0.1 fieldName=amount schemaName=price_type schemaVersion=0.0.1
51 task inputfield create name=MorningBoozeCheck version=0.0.1 fieldName=assistant_ID schemaName=assistant_ID_type schemaVersion=0.0.1
52 task inputfield create name=MorningBoozeCheck version=0.0.1 fieldName=notes schemaName=notes_type schemaVersion=0.0.1 optional=true
53 task inputfield create name=MorningBoozeCheck version=0.0.1 fieldName=quantity schemaName=quantity_type schemaVersion=0.0.1
54 task inputfield create name=MorningBoozeCheck version=0.0.1 fieldName=branch_ID schemaName=branch_ID_type schemaVersion=0.0.1
55 task inputfield create name=MorningBoozeCheck version=0.0.1 fieldName=item_ID schemaName=item_ID_type schemaVersion=0.0.1
56 task inputfield create name=MorningBoozeCheck version=0.0.1 fieldName=time schemaName=timestamp_type schemaVersion=0.0.1
57 task outputfield create name=MorningBoozeCheck version=0.0.1 fieldName=sale_ID schemaName=sale_ID_type schemaVersion=0.0.1
58 task outputfield create name=MorningBoozeCheck version=0.0.1 fieldName=amount schemaName=price_type schemaVersion=0.0.1
59 task outputfield create name=MorningBoozeCheck version=0.0.1 fieldName=assistant_ID schemaName=assistant_ID_type schemaVersion=0.0.1
60 task outputfield create name=MorningBoozeCheck version=0.0.1 fieldName=notes schemaName=notes_type schemaVersion=0.0.1 optional=true
61 task outputfield create name=MorningBoozeCheck version=0.0.1 fieldName=quantity schemaName=quantity_type schemaVersion=0.0.1
62 task outputfield create name=MorningBoozeCheck version=0.0.1 fieldName=branch_ID schemaName=branch_ID_type schemaVersion=0.0.1
63 task outputfield create name=MorningBoozeCheck version=0.0.1 fieldName=item_ID schemaName=item_ID_type schemaVersion=0.0.1
64 task outputfield create name=MorningBoozeCheck version=0.0.1 fieldName=authorised schemaName=authorised_type schemaVersion=0.0.1
65 task outputfield create name=MorningBoozeCheck version=0.0.1 fieldName=time schemaName=timestamp_type schemaVersion=0.0.1
66 task outputfield create name=MorningBoozeCheck version=0.0.1 fieldName=message schemaName=message_type schemaVersion=0.0.1 optional=true
67 task logic create name=MorningBoozeCheck version=0.0.1 logicFlavour=MVEL logic=LS
69 * ============LICENSE_START=======================================================
70 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
71 * Modifications Copyright (C) 2020 Nordix Foundation.
72 * ================================================================================
73 * Licensed under the Apache License, Version 2.0 (the "License");
74 * you may not use this file except in compliance with the License.
75 * You may obtain a copy of the License at
77 * http://www.apache.org/licenses/LICENSE-2.0
79 * Unless required by applicable law or agreed to in writing, software
80 * distributed under the License is distributed on an "AS IS" BASIS,
81 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
82 * See the License for the specific language governing permissions and
83 * limitations under the License.
85 * SPDX-License-Identifier: Apache-2.0
86 * ============LICENSE_END=========================================================
88 import java.util.Date;
89 import java.util.Calendar;
90 import java.util.TimeZone;
91 import java.text.SimpleDateFormat;
93 logger.info("Task Execution: '"+subject.id+"'. Input Fields: '"+inFields+"'");
95 outFields.put("amount" , inFields.get("amount"));
96 outFields.put("assistant_ID", inFields.get("assistant_ID"));
97 outFields.put("notes" , inFields.get("notes"));
98 outFields.put("quantity" , inFields.get("quantity"));
99 outFields.put("branch_ID" , inFields.get("branch_ID"));
100 outFields.put("item_ID" , inFields.get("item_ID"));
101 outFields.put("time" , inFields.get("time"));
102 outFields.put("sale_ID" , inFields.get("sale_ID"));
104 item_id = inFields.get("item_ID");
106 //The events used later to test this task use GMT timezone!
107 gmt = TimeZone.getTimeZone("GMT");
108 timenow = Calendar.getInstance(gmt);
109 df = new SimpleDateFormat("HH:mm:ss z");
111 timenow.setTimeInMillis(inFields.get("time"));
113 midnight = timenow.clone();
115 timenow.get(Calendar.YEAR),timenow.get(Calendar.MONTH),
116 timenow.get(Calendar.DATE),0,0,0);
117 eleven30 = timenow.clone();
119 timenow.get(Calendar.YEAR),timenow.get(Calendar.MONTH),
120 timenow.get(Calendar.DATE),11,30,0);
122 itemisalcohol = false;
123 if(item_id != null && item_id >=1000 && item_id < 2000)
124 itemisalcohol = true;
127 && timenow.after(midnight) && timenow.before(eleven30)){
128 outFields.put("authorised", false);
129 outFields.put("message", "Sale not authorised by policy task "+subject.taskName+
130 " for time "+df.format(timenow.getTime())+
131 ". Alcohol can not be sold between "+df.format(midnight.getTime())+
132 " and "+df.format(eleven30.getTime()));
136 outFields.put("authorised", true);
137 outFields.put("message", "Sale authorised by policy task "+subject.taskName+
138 " for time "+df.format(timenow.getTime()));
143 This task checks if a sale request is for an item that is an alcoholic drink.
144 If the local time is between 00:00:00 GMT and 11:30:00 GMT then the sale is not
145 authorised. Otherwise the sale is authorised.
146 In this implementation we assume that items with item_ID value between 1000 and
147 2000 are all alcoholic drinks :-)
151 task create name=MorningBoozeCheckAlt1 version=0.0.1 uuid=bc6d90c9-c902-4686-afd3-925b30e39990 description=LS
152 This task checks if a sale request is for an item that is an alcoholic drink.
153 If the local time is between 00:00:00 CET and 13:00:00 CET then the sale is not authorised.
154 Also alcohol sales are not allowed on Sundays. Otherwise the sale is authorised.
155 In this implementation we assume that items with item_ID between 1000 and 2000 are all alcoholic drinks
157 task inputfield create name=MorningBoozeCheckAlt1 version=0.0.1 fieldName=sale_ID schemaName=sale_ID_type schemaVersion=0.0.1
158 task inputfield create name=MorningBoozeCheckAlt1 version=0.0.1 fieldName=amount schemaName=price_type schemaVersion=0.0.1
159 task inputfield create name=MorningBoozeCheckAlt1 version=0.0.1 fieldName=assistant_ID schemaName=assistant_ID_type schemaVersion=0.0.1
160 task inputfield create name=MorningBoozeCheckAlt1 version=0.0.1 fieldName=notes schemaName=notes_type schemaVersion=0.0.1 optional=true
161 task inputfield create name=MorningBoozeCheckAlt1 version=0.0.1 fieldName=quantity schemaName=quantity_type schemaVersion=0.0.1
162 task inputfield create name=MorningBoozeCheckAlt1 version=0.0.1 fieldName=branch_ID schemaName=branch_ID_type schemaVersion=0.0.1
163 task inputfield create name=MorningBoozeCheckAlt1 version=0.0.1 fieldName=item_ID schemaName=item_ID_type schemaVersion=0.0.1
164 task inputfield create name=MorningBoozeCheckAlt1 version=0.0.1 fieldName=time schemaName=timestamp_type schemaVersion=0.0.1
165 task outputfield create name=MorningBoozeCheckAlt1 version=0.0.1 fieldName=sale_ID schemaName=sale_ID_type schemaVersion=0.0.1
166 task outputfield create name=MorningBoozeCheckAlt1 version=0.0.1 fieldName=amount schemaName=price_type schemaVersion=0.0.1
167 task outputfield create name=MorningBoozeCheckAlt1 version=0.0.1 fieldName=assistant_ID schemaName=assistant_ID_type schemaVersion=0.0.1
168 task outputfield create name=MorningBoozeCheckAlt1 version=0.0.1 fieldName=notes schemaName=notes_type schemaVersion=0.0.1 optional=true
169 task outputfield create name=MorningBoozeCheckAlt1 version=0.0.1 fieldName=quantity schemaName=quantity_type schemaVersion=0.0.1
170 task outputfield create name=MorningBoozeCheckAlt1 version=0.0.1 fieldName=branch_ID schemaName=branch_ID_type schemaVersion=0.0.1
171 task outputfield create name=MorningBoozeCheckAlt1 version=0.0.1 fieldName=item_ID schemaName=item_ID_type schemaVersion=0.0.1
172 task outputfield create name=MorningBoozeCheckAlt1 version=0.0.1 fieldName=authorised schemaName=authorised_type schemaVersion=0.0.1
173 task outputfield create name=MorningBoozeCheckAlt1 version=0.0.1 fieldName=time schemaName=timestamp_type schemaVersion=0.0.1
174 task outputfield create name=MorningBoozeCheckAlt1 version=0.0.1 fieldName=message schemaName=message_type schemaVersion=0.0.1 optional=true
175 task logic create name=MorningBoozeCheckAlt1 version=0.0.1 logicFlavour=MVEL logic=LS
177 * ============LICENSE_START=======================================================
178 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
179 * Modifications Copyright (C) 2020 Nordix Foundation.
180 * ================================================================================
181 * Licensed under the Apache License, Version 2.0 (the "License");
182 * you may not use this file except in compliance with the License.
183 * You may obtain a copy of the License at
185 * http://www.apache.org/licenses/LICENSE-2.0
187 * Unless required by applicable law or agreed to in writing, software
188 * distributed under the License is distributed on an "AS IS" BASIS,
189 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
190 * See the License for the specific language governing permissions and
191 * limitations under the License.
193 * SPDX-License-Identifier: Apache-2.0
194 * ============LICENSE_END=========================================================
196 import java.util.Date;
197 import java.util.Calendar;
198 import java.util.TimeZone;
199 import java.text.SimpleDateFormat;
201 logger.info("Task Execution: '"+subject.id+"'. Input Event: '"+inFields+"'");
203 outFields.put("amount" , inFields.get("amount"));
204 outFields.put("assistant_ID", inFields.get("assistant_ID"));
205 outFields.put("notes" , inFields.get("notes"));
206 outFields.put("quantity" , inFields.get("quantity"));
207 outFields.put("branch_ID" , inFields.get("branch_ID"));
208 outFields.put("item_ID" , inFields.get("item_ID"));
209 outFields.put("time" , inFields.get("time"));
210 outFields.put("sale_ID" , inFields.get("sale_ID"));
212 item_id = inFields.get("item_ID");
214 //The events used later to test this task use CET timezone!
215 cet = TimeZone.getTimeZone("CET");
216 timenow = Calendar.getInstance(cet);
217 df = new SimpleDateFormat("HH:mm:ss z");
219 timenow.setTimeInMillis(inFields.get("time"));
221 midnight = timenow.clone();
223 timenow.get(Calendar.YEAR),timenow.get(Calendar.MONTH),
224 timenow.get(Calendar.DATE),0,0,0);
225 onepm = timenow.clone();
227 timenow.get(Calendar.YEAR),timenow.get(Calendar.MONTH),
228 timenow.get(Calendar.DATE),13,0,0);
230 itemisalcohol = false;
231 if(item_id != null && item_id >=1000 && item_id < 2000)
232 itemisalcohol = true;
235 ( (timenow.after(midnight) && timenow.before(onepm))
237 (timenow.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)
239 outFields.put("authorised", false);
240 outFields.put("message", "Sale not authorised by policy task "+subject.taskName+
241 " for time "+df.format(timenow.getTime())+
242 ". Alcohol can not be sold between "+df.format(midnight.getTime())+
243 " and "+df.format(onepm.getTime()) +" or on Sunday");
247 outFields.put("authorised", true);
248 outFields.put("message", "Sale authorised by policy task "+subject.taskName+
249 " for time "+df.format(timenow.getTime()));
254 This task checks if a sale request is for an item that is an alcoholic drink.
255 If the local time is between 00:00:00 CET and 13:00:00 CET then the sale is not authorised.
256 Also alcohol sales are not allowed on Sundays. Otherwise the sale is authorised.
257 In this implementation we assume that items with item_ID between 1000 and 2000 are all alcoholic drinks :-)
261 event create name=SALE_AUTH version=0.0.1 uuid=c4500941-3f98-4080-a9cc-5b9753ed050b description="An event emitted by the Policy to indicate whether the sale of an item has been authorised" nameSpace=com.hyperm source="APEX" target="POS"
262 event parameter create name=SALE_AUTH version=0.0.1 parName=amount schemaName=price_type schemaVersion=0.0.1
263 event parameter create name=SALE_AUTH version=0.0.1 parName=assistant_ID schemaName=assistant_ID_type schemaVersion=0.0.1
264 event parameter create name=SALE_AUTH version=0.0.1 parName=authorised schemaName=authorised_type schemaVersion=0.0.1
265 event parameter create name=SALE_AUTH version=0.0.1 parName=branch_ID schemaName=branch_ID_type schemaVersion=0.0.1
266 event parameter create name=SALE_AUTH version=0.0.1 parName=item_ID schemaName=item_ID_type schemaVersion=0.0.1
267 event parameter create name=SALE_AUTH version=0.0.1 parName=message schemaName=message_type schemaVersion=0.0.1 optional=true
268 event parameter create name=SALE_AUTH version=0.0.1 parName=notes schemaName=notes_type schemaVersion=0.0.1 optional=true
269 event parameter create name=SALE_AUTH version=0.0.1 parName=quantity schemaName=quantity_type schemaVersion=0.0.1
270 event parameter create name=SALE_AUTH version=0.0.1 parName=sale_ID schemaName=sale_ID_type schemaVersion=0.0.1
271 event parameter create name=SALE_AUTH version=0.0.1 parName=time schemaName=timestamp_type schemaVersion=0.0.1
273 event create name=SALE_INPUT version=0.0.1 uuid=4f04aa98-e917-4f4a-882a-c75ba5a99374 description="An event raised by the PoS system each time an item is scanned for purchase" nameSpace=com.hyperm source="POS" target="APEX"
274 event parameter create name=SALE_INPUT version=0.0.1 parName=amount schemaName=price_type schemaVersion=0.0.1
275 event parameter create name=SALE_INPUT version=0.0.1 parName=assistant_ID schemaName=assistant_ID_type schemaVersion=0.0.1
276 event parameter create name=SALE_INPUT version=0.0.1 parName=branch_ID schemaName=branch_ID_type schemaVersion=0.0.1
277 event parameter create name=SALE_INPUT version=0.0.1 parName=item_ID schemaName=item_ID_type schemaVersion=0.0.1
278 event parameter create name=SALE_INPUT version=0.0.1 parName=notes schemaName=notes_type schemaVersion=0.0.1 optional=true
279 event parameter create name=SALE_INPUT version=0.0.1 parName=quantity schemaName=quantity_type schemaVersion=0.0.1
280 event parameter create name=SALE_INPUT version=0.0.1 parName=sale_ID schemaName=sale_ID_type schemaVersion=0.0.1
281 event parameter create name=SALE_INPUT version=0.0.1 parName=time schemaName=timestamp_type schemaVersion=0.0.1
284 policy create name=MyFirstPolicy version=0.0.1 uuid=6c5e410f-489a-46ff-964e-982ce6e8b6d0 description="This is my first Apex policy. It checks if a sale should be authorised or not." template=FREEFORM firstState=BoozeAuthDecide
285 policy state create name=MyFirstPolicy version=0.0.1 stateName=BoozeAuthDecide triggerName=SALE_INPUT triggerVersion=0.0.1 defaultTaskName=MorningBoozeCheck defaultTaskVersion=0.0.1
286 policy state output create name=MyFirstPolicy version=0.0.1 stateName=BoozeAuthDecide outputName=MorningBoozeCheck_Output_Direct eventName=SALE_AUTH eventVersion=0.0.1 nextState=NULL
287 policy state taskref create name=MyFirstPolicy version=0.0.1 stateName=BoozeAuthDecide taskLocalName=MorningBoozeCheckAlt1 taskName=MorningBoozeCheckAlt1 taskVersion=0.0.1 outputType=DIRECT outputName=MorningBoozeCheck_Output_Direct
288 policy state taskref create name=MyFirstPolicy version=0.0.1 stateName=BoozeAuthDecide taskLocalName=MorningBoozeCheck taskName=MorningBoozeCheck taskVersion=0.0.1 outputType=DIRECT outputName=MorningBoozeCheck_Output_Direct
289 policy state selecttasklogic create name=MyFirstPolicy version=0.0.1 stateName=BoozeAuthDecide logicFlavour=JAVASCRIPT logic=LS
291 * ============LICENSE_START=======================================================
292 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
293 * Modifications Copyright (C) 2020 Nordix Foundation.
294 * ================================================================================
295 * Licensed under the Apache License, Version 2.0 (the "License");
296 * you may not use this file except in compliance with the License.
297 * You may obtain a copy of the License at
299 * http://www.apache.org/licenses/LICENSE-2.0
301 * Unless required by applicable law or agreed to in writing, software
302 * distributed under the License is distributed on an "AS IS" BASIS,
303 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
304 * See the License for the specific language governing permissions and
305 * limitations under the License.
307 * SPDX-License-Identifier: Apache-2.0
308 * ============LICENSE_END=========================================================
311 var returnValue = true;
313 executor.logger.info("Task Selection Execution: '"+executor.subject.id+"'. Input Event: '"+executor.inFields+"'");
315 branchid = executor.inFields.get("branch_ID");
316 taskorig = executor.subject.getTaskKey("MorningBoozeCheck");
317 taskalt = executor.subject.getTaskKey("MorningBoozeCheckAlt1");
318 taskdef = executor.subject.getDefaultTaskKey();
320 if(branchid >=0 && branchid <1000){
321 taskorig.copyTo(executor.selectedTask);
323 else if (branchid >=1000 && branchid <2000){
324 taskalt.copyTo(executor.selectedTask);
327 taskdef.copyTo(executor.selectedTask);
331 This task selection logic selects task "MorningBoozeCheck" for branches with 0<=branch_ID<1000 and selects task "MorningBoozeCheckAlt1" for branches with 1000<=branch_ID<2000. Otherwise the default task is selected. In this case the default task is also "MorningBoozeCheck"