1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
|
.. This work is licensed under a Creative Commons Attribution 4.0 International License.
.. http://creativecommons.org/licenses/by/4.0
.. _apex-myFirstExample:
HowTo: My First Policy
**********************
.. contents::
:depth: 3
Introduction
^^^^^^^^^^^^
.. container:: sectionbody
.. container:: paragraph
Consider a scenario where a supermarket chain called
*HyperM* controls how it sells items in a policy-based
manner. Each time an item is processed by *HyperM*'s
point-of-sale (PoS) system an event is generated and
published about that item of stock being sold. This
event can then be used to update stock levels, etc..
.. container:: paragraph
*HyperM* want to extend this approach to allow some
checks to be performed before the sale can be
completed. This can be achieved by requesting a
policy-controlled decision as each item is processed
by for sale by each PoS system. The decision process
is integrated with *HyperM*'s other IT systems that
manage stock control, sourcing and purchasing,
personnel systems, etc.
.. container:: paragraph
In this document we will show how APEX and APEX
Policies can be used to achieve this, starting with a
simple policy, building up to more complicated policy
that demonstrates the features of APEX.
Data Models
^^^^^^^^^^^
.. container:: sectionbody
.. container:: sect2
.. rubric:: Sales Decision Event
:name: sales_decision_event
.. container:: paragraph
Each time a PoS system processes a sales item an
event with the following format is emitted:
.. table:: Table 1. Sale Input Event
+----------------------+----------------------+-----------------------+
| Event | Fields | Description |
+======================+======================+=======================+
| SALE_INPUT | time, sale_ID, | Event indicating a |
| | amount, item_ID, | sale of an item is |
| | quantity, | occurring |
| | assistant_ID, | |
| | branch_ID, notes, … | |
+----------------------+----------------------+-----------------------+
.. container:: paragraph
In each ``SALE_INPUT`` event the ``sale_ID`` field
is a unique ID generated by the PoS system. A
timestamp for the event is stored in the ``time``
field. The ``amount`` field refers to the value of
the item(s) to be sold (in cents). The ``item_ID``
field is a unique identifier for each item type,
and can be used to retrieve more information about
the item from *HyperM*'s stock control system. The
``quantity`` field refers to the quantity of the
item to be sold. The ``assistant_ID`` field is a
unique identifier for the PoS operator, and can be
used to retrieve more information about the
operator from the *HyperM*'s personnel system.
Since *HyperM* has many branches the ``branch_ID``
identifies the shop. The ``notes`` field contains
arbitrary notes about the sale.
.. container:: sect2
.. rubric:: Sales Decision Event
:name: sales_decision_event
.. container:: paragraph
After a ``SALE_INPUT`` event is emitted by the PoS
system *HyperM*'s policy-based controlled sales
checking system emits a Sale Authorization Event
indicating whether the sale is authorized or
denied. The PoS system can then listen for this
event before continuing with the sale.
.. table:: Table 2. Sale Authorisation Event
+----------------------+----------------------+-----------------------+
| Event | Fields | Description |
+======================+======================+=======================+
| SALE_AUTH | sale_ID, time, | Event indicating a |
| | authorized, amount, | sale of an item is |
| | item_ID, quantity, | authorized or denied |
| | assistant_ID, | |
| | branch_ID, notes, | |
| | message… | |
+----------------------+----------------------+-----------------------+
.. container:: paragraph
In each ``SALE_AUTH`` event the ``sale_ID`` field
is copied from the ``SALE_INPUT`` event that
trigger the decision request. The ``SALE_AUTH``
event is also timestamped using the ``time`` field,
and a field called ``authorised`` is set to
``true`` or ``false`` depending on whether the sale
is authorized or denied. The ``message`` field
carries an optional message about why a sale was
not authorized. The other fields from the
``SALE_INPUT`` event are also included for
completeness.
.. container:: sect2
.. rubric:: Stock Control: Items
:name: stock_control_items
.. container:: paragraph
*HyperM* maintains information about each item for
sale in a database table called ``ITEMS``.
.. table:: Table 3. Items Database
+----------------------+----------------------+-----------------------+
| Table | Fields | Description |
+======================+======================+=======================+
| ITEMS | item_ID, | Database table |
| | description, | describing each item |
| | cost_price, barcode, | for sale |
| | supplier_ID, | |
| | category, … | |
+----------------------+----------------------+-----------------------+
.. container:: paragraph
The database table ``ITEMS`` has a row for each
items that *HyperM* sells. Each item is identified
by an ``item_ID`` value. The ``description`` field
stores a description of the item. The cost price of
the item is given in ``cost_price``. The barcode of
the item is encoded in ``barcode``, while the item
supplier is identified by ``supplier_ID``. Items
may also be classified into categories using the
``category`` field. Useful categories might
include: ``soft drinks``, ``alcoholic drinks``,
``cigarettes``, ``knives``, ``confectionery``,
``bakery``, ``fruit&vegetables``, ``meat``, etc..
.. container:: sect2
.. rubric:: Personnel System: Assistants
:name: personnel_system_assistants
.. table:: Table 4. Assistants Database
+----------------------+----------------------+-----------------------+
| Table | Fields | Description |
+======================+======================+=======================+
| ASSISTANTS | assistant_ID, | Database table |
| | surname, firstname, | describing each |
| | middlename, age, | *HyperM* sales |
| | grade, phone_number, | assistant |
| | … | |
+----------------------+----------------------+-----------------------+
.. container:: paragraph
The database table ``ASSISTANTS`` has a row for
each sales assistant employed by *HyperM*. Each
assistant is identified by an ``assistant_ID``
value, with their name given in the ``firstname``,
``middlename`` and ``surname`` fields. The
assistant’s age in years is given in ``age``, while
their phone number is contained in the
``phone_number`` field. The assistant’s grade is
encoded in ``grade``. Useful values for ``grade``
might include: ``trainee``, ``operator``,
``supervisor``, etc..
.. container:: sect2
.. rubric:: Locations: Branches
:name: locations_branches
.. table:: Table 5. Branches Database
+----------------------+----------------------+-----------------------+
| Table | Fields | Description |
+======================+======================+=======================+
| BRANCHES | branch_ID, | Database table |
| | branch_Name, | describing each |
| | category, street, | *HyperM* branch |
| | city, country, | |
| | postcode, … | |
+----------------------+----------------------+-----------------------+
.. container:: paragraph
*HyperM* operates a number of branches. Each branch
is described in the ``BRANCHES`` database table.
Each branch is identified by a ``branch_ID``, with
a branch name given in ``branch_Name``. The address
for the branch is encoded in ``street``, ``city``,
``country`` and ``postcode``. The branch category
is given in the ``category`` field. Useful values
for ``category`` might include: ``Small``,
``Large``, ``Super``, ``Hyper``, etc..
Policy Step 1
^^^^^^^^^^^^^
.. container:: sectionbody
.. container:: sect1
.. rubric:: Scenario
:name: scenario
.. container:: paragraph
For the first version of our policy, let’s start
with something simple. Let us assume that there
exists some restriction that alcohol products
cannot be sold before 11:30am. In this section we
will go through the necessary steps to define a
policy that can enforce this for *HyperM*.
.. container:: ulist
- Alcohol cannot be sold before 11:30am... container::
New Policy Model
----------------
.. container:: sectionbody
.. container:: sect1
.. rubric:: Create the an new empty Policy Model ``MyFirstPolicyModel``
:name: create_the_an_new_empty_policy_model_code_myfirstpolicymodel_code
.. container:: paragraph
Since an organisation like *HyperM* may have many
policies covering many different domains, policies should
be grouped into policy sets. In order to edit or deploy a
policy, or policy set, the definition of the policy(ies)
and all required events, tasks, states, etc., are grouped
together into a 'Policy Model'. An organization might
define many Policy Models, each containing a different
set of policies.
.. container:: paragraph
So the first step is to create a new empty Policy Model
called ``MyFirstPolicyModel``. Using the APEX Policy
Editor, click on the 'File' menus and select 'New'. Then
define our new policy model called
``MyFirstPolicyModel``. Use the 'Generate UUID' button to
create a new unique ID for the policy model, and fill in
a description for the policy model. Press the ``Submit``
button to save your changes.
.. container:: imageblock
.. container:: content
|File > New to create a new Policy Model|
.. container:: imageblock
.. container:: content
|Create a new Policy Model|
Events
------
.. container:: sectionbody
.. container:: sect1
.. rubric:: Create the input event ``SALE_INPUT`` and the
output event ``SALE_AUTH``
:name: create_the_input_event_code_sale_input_code_and_the_output_event_code_sale_auth_code
.. container:: paragraph
Using the APEX Policy Editor, click on the 'Events' tab.
In the 'Events' pane, right click and select 'New':
.. container:: imageblock
.. container:: content
|Right click to create a new event|
.. container:: paragraph
Create a new event type called ``SALE_INPUT``. Use the
'Generate UUID' button to create a new unique ID for the
event type, and fill in a description for the event. Add
a namespace, e.g. ``com.hyperm``. We can add hard-coded
strings for the ``Source`` and ``Target``, e.g. ``POS``
and ``APEX``. At this stage we will not add any parameter
fields, we will leave this until later. Use the
``Submit`` button to create the event.
.. container:: imageblock
.. container:: content
|Fill in the necessary information for the
'SALE_INPUT' event and click 'Submit'|
.. container:: paragraph
Repeat the same steps for a new event type called
``SALE_AUTH``. Just use ``APEX`` as source and ``POS`` as
target, since this is the output event coming from APEX
going to the sales point.
.. container:: paragraph
Before we can add parameter fields to an event we must
first define APEX Context Item Schemas that can be used
by those fields.
.. container:: paragraph
To create new item schemas, click on the 'Context Item
Schemas' tab. In that 'Context Item Schemas' pane, right
click and select 'Create new ContextSchema'.
.. container:: imageblock
.. container:: content
|Right click to create a new Item Schema|
.. container:: paragraph
Create item schemas with the following characteristics,
each with its own unique UUID:
.. table:: Table 1. Item Schemas
+-----------------+-----------------+-----------------+-----------------+
| Name | Schema Flavour | Schema | Description |
| | | Definition | |
+=================+=================+=================+=================+
| timestamp_type | Java | java.lang.Long | A type for |
| | | | ``time`` values |
+-----------------+-----------------+-----------------+-----------------+
| sale_ID_type | Java | java.lang.Long | A type for |
| | | | ``sale_ID`` |
| | | | values |
+-----------------+-----------------+-----------------+-----------------+
| price_type | Java | java.lang.Long | A type for |
| | | | ``amo |
| | | | unt``/``price`` |
| | | | values |
+-----------------+-----------------+-----------------+-----------------+
| item_ID_type | Java | java.lang.Long | A type for |
| | | | ``item_ID`` |
| | | | values |
+-----------------+-----------------+-----------------+-----------------+
| as | Java | java.lang.Long | A type for |
| sistant_ID_type | | | ` |
| | | | `assistant_ID`` |
| | | | values |
+-----------------+-----------------+-----------------+-----------------+
| quantity_type | Java | ja | A type for |
| | | va.lang.Integer | ``quantity`` |
| | | | values |
+-----------------+-----------------+-----------------+-----------------+
| branch_ID_type | Java | java.lang.Long | A type for |
| | | | ``branch_ID`` |
| | | | values |
+-----------------+-----------------+-----------------+-----------------+
| notes_type | Java | j | A type for |
| | | ava.lang.String | ``notes`` |
| | | | values |
+-----------------+-----------------+-----------------+-----------------+
| authorised_type | Java | ja | A type for |
| | | va.lang.Boolean | ``authorised`` |
| | | | values |
+-----------------+-----------------+-----------------+-----------------+
| message_type | Java | j | A type for |
| | | ava.lang.String | ``message`` |
| | | | values |
+-----------------+-----------------+-----------------+-----------------+
.. container:: imageblock
.. container:: content
|Create a new Item Schema|
.. container:: paragraph
The item schemas can now be seen on the 'Context Item
Schemas' tab, and can be updated at any time by
right-clicking on the item schemas on the 'Context Item
Schemas' tab. Now we can go back to the event definitions
for ``SALE_INPUT`` and ``SALE_AUTH`` and add some
parameter fields.
.. container:: admonitionblock tip
.. TIP::
.. container:: paragraph
APEX natively supports schema definitions in ``Java`` and ``Avro``.
``Java`` schema definitions are simply the name of a Java Class. There are some restrictions:
.. container:: ulist
- the class must be instantiatable, i.e. not an Java interface or abstract class
- primitive types are not supported, i.e. use ``java.lang.Integer`` instead of ``int``, etc.
- it must be possible to find the class, i.e. the class must be contained in the Java classpath.
.. container:: paragraph
``Avro`` schema definitions can be any valid `Avro <https://avro.apac
he.org/docs/current/spec.html>`__ schema. For events using fields defined with
Avro schemas, any incoming event containing that field must contain a value that conforms to the Avro schema.
.. container:: paragraph
Click on the 'Events' tab, then right click the
``SALE_INPUT`` row and select 'Edit Event
:literal:`SALE_INPUT’. To add a new event parameter use the 'Add Event Parameter' button at the bottom of the screen. For the `SALE_INPUT`
event add the following event parameters:
.. table:: Table 2. Event Parameter Fields for the ``SALE_INPUT`` Event
+----------------------+----------------------+-----------------------+
| Parameter Name | Parameter Type | Optional |
+======================+======================+=======================+
| time | timestamp_type | no |
+----------------------+----------------------+-----------------------+
| sale_ID | sale_ID_type | no |
+----------------------+----------------------+-----------------------+
| amount | price_type | no |
+----------------------+----------------------+-----------------------+
| item_ID | item_ID_type | no |
+----------------------+----------------------+-----------------------+
| quantity | quantity_type | no |
+----------------------+----------------------+-----------------------+
| assistant_ID | assistant_ID_type | no |
+----------------------+----------------------+-----------------------+
| branch_ID | branch_ID_type | no |
+----------------------+----------------------+-----------------------+
| notes | notes_type | *yes* |
+----------------------+----------------------+-----------------------+
.. container:: paragraph
Remember to click the 'Submit' button at the bottom of
the event definition pane.
.. container:: admonitionblock tip
.. TIP::
.. container:: paragraph
Parameter fields can be *optional* in events. If a parameter is not marked as
*optional* then by default it is *mandatory*, so it must appear in any input event
passed to APEX. If an *optional* field is not set for an output event then value
will be set to ``null``.
.. container:: imageblock
.. container:: content
|Add new event parameters to an event|
.. container:: paragraph
Select the ``SALE_AUTH`` event and add the following
event parameters:
.. table:: Table 3. Event Parameter Fields for the ``SALE_AUTH`` Event
+----------------------+----------------------+-----------------------+
| Parameter Name | Parameter Type | no |
+======================+======================+=======================+
| sale_ID | sale_ID_type | no |
+----------------------+----------------------+-----------------------+
| time | timestamp_type | no |
+----------------------+----------------------+-----------------------+
| authorised | authorised_type | no |
+----------------------+----------------------+-----------------------+
| message | message_type | *yes* |
+----------------------+----------------------+-----------------------+
| amount | price_type | no |
+----------------------+----------------------+-----------------------+
| item_ID | item_ID_type | no |
+----------------------+----------------------+-----------------------+
| assistant_ID | assistant_ID_type | no |
+----------------------+----------------------+-----------------------+
| quantity | quantity_type | no |
+----------------------+----------------------+-----------------------+
| branch_ID | branch_ID_type | no |
+----------------------+----------------------+-----------------------+
| notes | notes_type | *yes* |
+----------------------+----------------------+-----------------------+
.. container:: paragraph
Remember to click the 'Submit' button at the bottom of
the event definition pane.
.. container:: paragraph
The events for our policy are now defined.
New Policy
----------
.. container:: sectionbody
.. container:: sect1
.. rubric:: Create a new Policy and add the *"No Booze
before 11:30"* check
:name: create_a_new_policy_and_add_the_em_no_booze_before_11_30_em_check
.. container:: paragraph
APEX policies are defined using a state-machine model.
Each policy comprises one or more *states* that can be
individually executed. Where there is more than one
*state* the states are chained together to form a
`Directed Acyclic Graph
(DAG) <https://en.wikipedia.org/wiki/Directed_acyclic_graph>`__
of states. A *state* is triggered by passing it a single
input (or 'trigger') event and once executed each state
then emits an output event. For each *state* the logic
for the *state* is embedded in one or more *tasks*. Each
*task* contains specific *task logic* that is executed by
the APEX execution environment each time the *task* is
invoked. Where there is more than one *task* in a *state*
then the *state* also defines some *task selection logic*
to select an appropriate task each time the *state* is
executed.
.. container:: paragraph
Therefore, to create a new policy we must first define
one or more tasks.
.. container:: paragraph
To create a new Task click on the 'Tasks' tab. In the
'Tasks' pane, right click and select 'Create new Task'.
Create a new Task called ``MorningBoozeCheck``. Use the
'Generate UUID' button to create a new unique ID for the
task, and fill in a description for the task.
.. container:: imageblock
.. container:: content
|Right click to create a new task|
.. container:: paragraph
Tasks are configured with a set of *input fields* and a
set of *output fields*. To add new input/output fields
for a task use the 'Add Task Input Field' and 'Add Task
Output Field' button. The list of input and out fields to
add for the ``MorningBoozeCheck`` task are given below.
The input fields are drawn from the parameters in the
state’s input event, and the task’s output fields are
used to populate the state’s output event. The task’s
input and output fields must be a subset of the event
parameters defined for the input and output events for
any state that uses that task. (You may have noticed that
the input and output fields for the ``MorningBoozeCheck``
task have the exact same names and reuse the item schemas
that we used for the parameters in the ``SALE_INPUT`` and
``SALE_AUTH`` events respectively).
.. table:: Table 1. Input fields for ``MorningBoozeCheck`` task
+-----------------------------------+-----------------------------------+
| Parameter Name | Parameter Type |
+===================================+===================================+
| time | timestamp_type |
+-----------------------------------+-----------------------------------+
| sale_ID | sale_ID_type |
+-----------------------------------+-----------------------------------+
| amount | price_type |
+-----------------------------------+-----------------------------------+
| item_ID | item_ID_type |
+-----------------------------------+-----------------------------------+
| quantity | quantity_type |
+-----------------------------------+-----------------------------------+
| assistant_ID | assistant_ID_type |
+-----------------------------------+-----------------------------------+
| branch_ID | branch_ID_type |
+-----------------------------------+-----------------------------------+
| notes | notes_type |
+-----------------------------------+-----------------------------------+
.. table:: Table 2. Output fields for ``MorningBoozeCheck`` task
+-----------------------------------+-----------------------------------+
| Parameter Name | Parameter Type |
+===================================+===================================+
| sale_ID | sale_ID_type |
+-----------------------------------+-----------------------------------+
| time | timestamp_type |
+-----------------------------------+-----------------------------------+
| authorised | authorised_type |
+-----------------------------------+-----------------------------------+
| message | message_type |
+-----------------------------------+-----------------------------------+
| amount | price_type |
+-----------------------------------+-----------------------------------+
| item_ID | item_ID_type |
+-----------------------------------+-----------------------------------+
| assistant_ID | assistant_ID_type |
+-----------------------------------+-----------------------------------+
| quantity | quantity_type |
+-----------------------------------+-----------------------------------+
| branch_ID | branch_ID_type |
+-----------------------------------+-----------------------------------+
| notes | notes_type |
+-----------------------------------+-----------------------------------+
.. container:: imageblock
.. container:: content
|Add input and out fields for the task|
.. container:: paragraph
Each task must include some 'Task Logic' that implements
the behaviour for the task. Task logic can be defined in
a number of different ways using a choice of languages.
For this task we will author the logic using the
Java-like scripting language called
```MVEL`` <https://en.wikipedia.org/wiki/MVEL>`__.
.. container:: paragraph
For simplicity use the following code for the task logic.
Paste the script text into the 'Task Logic' box, and use
"MVEL" as the 'Task Logic Type / Flavour'.
.. container:: paragraph
This logic assumes that all items with ``item_ID``
between 1000 and 2000 contain alcohol, which is not very
realistic, but we will see a better approach for this
later. It also uses the standard ``Java`` time utilities
to check if the current time is between ``00:00:00 GMT``
and ``11:30:00 GMT``. For a detailed guide to how to
write your own logic in
```JavaScript`` <https://en.wikipedia.org/wiki/JavaScript>`__,
```MVEL`` <https://en.wikipedia.org/wiki/MVEL>`__ or one
of the other supported languages please refer to APEX
Programmers Guide.
.. container:: listingblock
.. container:: content
.. code:: CodeRay
/*
* ============LICENSE_START=======================================================
* Copyright (C) 2016-2018 Ericsson. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
* ============LICENSE_END=========================================================
*/
import java.util.Date;
import java.util.Calendar;
import java.util.TimeZone;
import java.text.SimpleDateFormat;
logger.info("Task Execution: '"+subject.id+"'. Input Fields: '"+inFields+"'");
outFields.put("amount" , inFields.get("amount"));
outFields.put("assistant_ID", inFields.get("assistant_ID"));
outFields.put("notes" , inFields.get("notes"));
outFields.put("quantity" , inFields.get("quantity"));
outFields.put("branch_ID" , inFields.get("branch_ID"));
outFields.put("item_ID" , inFields.get("item_ID"));
outFields.put("time" , inFields.get("time"));
outFields.put("sale_ID" , inFields.get("sale_ID"));
item_id = inFields.get("item_ID");
//The events used later to test this task use GMT timezone!
gmt = TimeZone.getTimeZone("GMT");
timenow = Calendar.getInstance(gmt);
df = new SimpleDateFormat("HH:mm:ss z");
df.setTimeZone(gmt);
timenow.setTimeInMillis(inFields.get("time"));
midnight = timenow.clone();
midnight.set(
timenow.get(Calendar.YEAR),timenow.get(Calendar.MONTH),
timenow.get(Calendar.DATE),0,0,0);
eleven30 = timenow.clone();
eleven30.set(
timenow.get(Calendar.YEAR),timenow.get(Calendar.MONTH),
timenow.get(Calendar.DATE),11,30,0);
itemisalcohol = false;
if(item_id != null && item_id >=1000 && item_id < 2000)
itemisalcohol = true;
if( itemisalcohol
&& timenow.after(midnight) && timenow.before(eleven30)){
outFields.put("authorised", false);
outFields.put("message", "Sale not authorised by policy task "+subject.taskName+
" for time "+df.format(timenow.getTime())+
". Alcohol can not be sold between "+df.format(midnight.getTime())+
" and "+df.format(eleven30.getTime()));
return true;
}
else{
outFields.put("authorised", true);
outFields.put("message", "Sale authorised by policy task "+subject.taskName+
" for time "+df.format(timenow.getTime()));
return true;
}
/*
This task checks if a sale request is for an item that is an alcoholic drink.
If the local time is between 00:00:00 GMT and 11:30:00 GMT then the sale is not
authorised. Otherwise the sale is authorised.
In this implementation we assume that items with item_ID value between 1000 and
2000 are all alcoholic drinks :-)
*/
.. container:: imageblock
.. container:: content
|Add task logic the task|
.. container:: paragraph
An alternative version of the same logic is available in
JavaScript. Just use "JAVASCRIPT" as the 'Task Logic Type
/ Flavour' instead.
.. container:: listingblock
.. container:: content
.. code:: CodeRay
/*
* ============LICENSE_START=======================================================
* Copyright (C) 2016-2018 Ericsson. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
* ============LICENSE_END=========================================================
*/
var returnValueType = Java.type("java.lang.Boolean");
var returnValue = new returnValueType(true);
// Load compatibility script for imports etc
load("nashorn:mozilla_compat.js");
importPackage(java.text);
importClass(java.text.SimpleDateFormat);
executor.logger.info("Task Execution: '"+executor.subject.id+"'. Input Fields: '"+executor.inFields+"'");
executor.outFields.put("amount" , executor.inFields.get("amount"));
executor.outFields.put("assistant_ID", executor.inFields.get("assistant_ID"));
executor.outFields.put("notes" , executor.inFields.get("notes"));
executor.outFields.put("quantity" , executor.inFields.get("quantity"));
executor.outFields.put("branch_ID" , executor.inFields.get("branch_ID"));
executor.outFields.put("item_ID" , executor.inFields.get("item_ID"));
executor.outFields.put("time" , executor.inFields.get("time"));
executor.outFields.put("sale_ID" , executor.inFields.get("sale_ID"));
item_id = executor.inFields.get("item_ID");
//All times in this script are in GMT/UTC since the policy and events assume time is in GMT.
var timenow_gmt = new Date(Number(executor.inFields.get("time")));
var midnight_gmt = new Date(Number(executor.inFields.get("time")));
midnight_gmt.setUTCHours(0,0,0,0);
var eleven30_gmt = new Date(Number(executor.inFields.get("time")));
eleven30_gmt.setUTCHours(11,30,0,0);
var timeformatter = new java.text.SimpleDateFormat("HH:mm:ss z");
var itemisalcohol = false;
if(item_id != null && item_id >=1000 && item_id < 2000)
itemisalcohol = true;
if( itemisalcohol
&& timenow_gmt.getTime() >= midnight_gmt.getTime()
&& timenow_gmt.getTime() < eleven30_gmt.getTime()) {
executor.outFields.put("authorised", false);
executor.outFields.put("message", "Sale not authorised by policy task " +
executor.subject.taskName+ " for time " + timeformatter.format(timenow_gmt.getTime()) +
". Alcohol can not be sold between " + timeformatter.format(midnight_gmt.getTime()) +
" and " + timeformatter.format(eleven30_gmt.getTime()));
}
else{
executor.outFields.put("authorised", true);
executor.outFields.put("message", "Sale authorised by policy task " +
executor.subject.taskName + " for time "+timeformatter.format(timenow_gmt.getTime()));
}
/*
This task checks if a sale request is for an item that is an alcoholic drink.
If the local time is between 00:00:00 GMT and 11:30:00 GMT then the sale is not
authorised. Otherwise the sale is authorised.
In this implementation we assume that items with item_ID value between 1000 and
2000 are all alcoholic drinks :-)
*/
.. container:: paragraph
The task definition is now complete so click the 'Submit'
button to save the task. The task can now be seen on the
'Tasks' tab, and can be updated at any time by
right-clicking on the task on the 'Task' tab. Now that we
have created our task, we can can create a policy that
uses that task.
.. container:: paragraph
To create a new Policy click on the 'Policies' tab. In
the 'Policies' pane, right click and select 'Create new
Policy':
.. container:: paragraph
Create a new Policy called ``MyFirstPolicy``. Use the
'Generate UUID' button to create a new unique ID for the
policy, and fill in a description for the policy. Use
'FREEFORM' as the 'Policy Flavour'.
.. container:: paragraph
Each policy must have at least one state. Since this is
'freeform' policy we can add as many states as we wish.
Let’s start with one state. Add a new state called
``BoozeAuthDecide`` to this ``MyFirstPolicy`` policy
using the 'Add new State' button after filling in the
name of our new state.
.. container:: imageblock
.. container:: content
|Create a new policy|
.. container:: paragraph
Each state must uses one input event type. For this new
state select the ``SALE_INPUT`` event as the input event.
.. container:: paragraph
Each policy must define a 'First State' and a 'Policy
Trigger Event'. The 'Policy Trigger Event' is the input
event for the policy as a whole. This event is then
passed to the first state in the chain of states in the
policy, therefore the 'Policy Trigger Event' will be the
input event for the first state. Each policy can only
have one 'First State'. For our ``MyFirstPolicy`` policy,
select ``BoozeAuthDecide`` as the 'First State'. This
will automatically select ``SALE_INPUT`` as the 'Policy
Trigger Event' for our policy.
.. container:: imageblock
.. container:: content
|Create a state|
.. container:: paragraph
In this case we will create a reference the pre-existing
``MorningBoozeCheck`` task that we defined above using
the 'Add New Task' button. Select the
``MorningBoozeCheck`` task, and use the name of the task
as the 'Local Name' for the task.
.. container:: paragraph
in the case where a state references more than one task,
a 'Default Task' must be selected for the state and some
logic ('Task Selection Logic') must be specified to
select the appropriate task at execution time. Since our
new state ``BoozeAuthDecide`` only has one task the
default task is automatically selected and no 'Task
Selection Logic' is required.
.. container:: admonitionblock note
.. NOTE::
.. container:: paragraph
In a 'Policy' 'State' a 'State Output Mapping' has 3 roles: 1) Select which 'State'
should be executed next, 2) Select the type of the state’s 'Outgoing Event', and
3) Populate the state’s 'Outgoing Event'. This is how states are chained together
to form a (`Directed Acyclic Graph (DAG) <https://en.wikipedia.org/wiki/Directed_acyclic_graph>`__)
of states. The final state(s) of a policy are those that do not select any 'next'
state. Since a 'State' can only accept a single type of event, the type of the
event emitted by a previous 'State' must match the incoming event type of the next
'State'. This is also how the last state(s) in a policy can emit events of
different types. The 'State Output Mapping' is also responsible for taking the
fields that are output by the task executed in the state and populating the
state’s output populating the state’s output event before it is emitted.
Each 'Task' referenced in 'State' must have a defined 'Output Mapping' to take the output of the task, select an 'Outgoing Event' type for the state, populate the state's outgoing event, and then select the next state to be executed (if any).
There are 2 basic types of output mappings:
.. container:: olist arabic
#. **Direct Output Mappings** have a single value for 'Next State' and a single value for 'State Output Event'. The outgoing event for the state is automatically created, any outgoing event parameters that were present in the incoming event are copied into the outgoing event, then any task output fields that have the same name and type as parameters in the outgoing event are automatically copied into the outgoing event.
#. **Logic-Based State Output Mappings / Finalizers** have some logic defined that dynamically selects and creates the 'State Outgoing Event', manages the population of the outgoing event parameters (perhaps changing or adding to the outputs from the task), and then dynamically selects the next state to be executed (if any).
.. container:: paragraph
Each task reference must also have an associated 'Output
State Mapping' so we need an 'Output State Mapping' for
the ``BoozeAuthDecide`` state to use when the
``MorningBoozeCheck`` task is executed. The simplest type
of output mapping is a 'Direct Output Mapping'.
.. container:: paragraph
Create a new 'Direct Output Mapping' for the state called
``MorningBoozeCheck_Output_Direct`` using the 'Add New
Direct State Output Mapping' button. Select ``SALE_AUTH``
as the output event and select ``None`` for the next
state value. We can then select this output mapping for
use when the the ``MorningBoozeCheck`` task is executed.
Since there is only state, and only one task for that
state, this output mapping ensures that the
``BoozeAuthDecide`` state is the only state executed and
the state (and the policy) can only emit events of type
``SALE_AUTH``. (You may remember that the output fields
for the ``MorningBoozeCheck`` task have the exact same
names and reuse the item schemas that we used for the
parameters in ``SALE_AUTH`` event. The
``MorningBoozeCheck_Output_Direct`` direct output mapping
can now automatically copy the values from the
``MorningBoozeCheck`` task directly into outgoing
``SALE_AUTH`` events.)
.. container:: imageblock
.. container:: content
|Add a Task and Output Mapping|
.. container:: paragraph
Click the 'Submit' button to complete the definition of
our ``MyFirstPolicy`` policy. The policy
``MyFirstPolicy`` can now be seen in the list of policies
on the 'Policies' tab, and can be updated at any time by
right-clicking on the policy on the 'Policies' tab.
.. container:: paragraph
The ``MyFirstPolicyModel``, including our
``MyFirstPolicy`` policy can now be checked for errors.
Click on the 'Model' menu and select 'Validate'. The
model should validate without any 'Warning' or 'Error'
messages. If you see any 'Error' or 'Warning' messages,
carefully read the message as a hint to find where you
might have made a mistake when defining some aspect of
your policy model.
.. container:: imageblock
.. container:: content
|Validate the policy model for error using the 'Model'
> 'Validate' menu item|
.. container:: paragraph
Congratulations, you have now completed your first APEX
policy. The policy model containing our new policy can
now be exported from the editor and saved. Click on the
'File' menu and select 'Download' to save the policy
model in JSON format. The exported policy model is then
available in the directory you selected, for instance
``$APEX_HOME/examples/models/MyFirstPolicy/1/MyFirstPolicyModel_0.0.1.json``.
The exported policy can now be loaded into the APEX
Policy Engine, or can be re-loaded and edited by the APEX
Policy Editor.
.. container:: imageblock
.. container:: content
|Download the completed policy model using the 'File'
> 'Download' menu item|
Test The Policy
---------------
.. container:: sectionbody
.. container:: sect1
.. rubric:: Test Policy Step 1
:name: test_policy_step_1
.. container:: paragraph
To start a new APEX Engine you can use the following
configuration. In a full APEX installation you can find
this configuration in
``$APEX_HOME/examples/config/MyFirstPolicy/1/MyFirstPolicyConfigStdin2StdoutJsonEvent.json``.
This configuration expects incoming events to be in
``JSON`` format and to be passed into the APEX Engine
from ``stdin``, and result events will be printed in
``JSON`` format to ``stdout``. This configuration loads
the policy model stored in the file
'MyFirstPolicyModel_0.0.1.json' as exported from the APEX
Editor. Note, you may need to edit this file to provide
the full path to wherever you stored the exported policy
model file.
.. container:: listingblock
.. container:: content
.. code:: CodeRay
{
"engineServiceParameters" : {
"name" : "MyFirstPolicyApexEngine",
"version" : "0.0.1",
"id" : 101,
"instanceCount" : 4,
"deploymentPort" : 12345,
"policyModelFileName" : "examples/models/MyFirstPolicy/1/MyFirstPolicyModel_0.0.1.json",
"engineParameters" : {
"executorParameters" : {
"MVEL" : {
"parameterClassName" : "org.onap.policy.apex.plugins.executor.mvel.MVELExecutorParameters"
},
"JAVASCRIPT" : {
"parameterClassName" : "org.onap.policy.apex.plugins.executor.javascript.JavascriptExecutorParameters"
}
}
}
},
"eventOutputParameters": {
"FirstProducer": {
"carrierTechnologyParameters" : {
"carrierTechnology" : "FILE",
"parameters" : {
"standardIO" : true
}
},
"eventProtocolParameters" : {
"eventProtocol" : "JSON"
}
}
},
"eventInputParameters": {
"FirstConsumer": {
"carrierTechnologyParameters" : {
"carrierTechnology" : "FILE",
"parameters" : {
"standardIO" : true
}
},
"eventProtocolParameters" : {
"eventProtocol" : "JSON"
}
}
}
}
.. container:: paragraph
To test the policy try paste the following events into
the console as the APEX engine executes:
.. list-table:: Title
:widths: 25 40 35
:header-rows: 1
* - Input Event (JSON)
- Output Event (JSON)
- comment
* - .. literalinclude:: events/1/EventIn_NonBoozeItem_101309GMT.json
:language: JSON
- .. literalinclude:: events/1/EventOut_NonBoozeItem_101309GMT.json
:language: JSON
- .. container:: paragraph
Request to buy a non-alcoholic item (item_ID=5123) at 10:13:09 on Tuesday, 10 January 2017.
Sale is authorized.
* - .. literalinclude:: events/1/EventIn_BoozeItem_084106GMT.json
:language: JSON
- .. literalinclude:: events/1/EventOut_BoozeItem_084106GMT.json
:language: JSON
- .. container:: paragraph
Request to buy alcohol item (`item_ID=1249`) at _08:41:06_ on _Monday, 02 January 2017_.
Sale is not authorized.
* - .. literalinclude:: events/1/EventIn_BoozeItem_201713GMT.json
:language: JSON
- .. literalinclude:: events/1/EventOut_BoozeItem_201713GMT.json
:language: JSON
- .. container:: paragraph
Request to buy alcohol (`item_ID=1943`) at _20:17:13_ on _Tuesday, 20 December 2016_.
Sale is authorized.
CLI Editor File
---------------
.. container:: sectionbody
.. container:: sect1
.. rubric:: Policy 1 in CLI Editor
:name: policy_1_in_cli_editor
.. container:: paragraph
An equivalent version of the ``MyFirstPolicyModel``
policy model can again be generated using the APEX CLI
editor. A sample APEX CLI script is shown below:
.. container:: listingblock
.. container:: content
.. code:: CodeRay
#-------------------------------------------------------------------------------
# ============LICENSE_START=======================================================
# Copyright (C) 2016-2018 Ericsson. All rights reserved.
# ================================================================================
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
# ============LICENSE_END=========================================================
#-------------------------------------------------------------------------------
model create name=MyFirstPolicyModel version=0.0.1 uuid=540226fb-55ee-4f0e-a444-983a0494818e description="This is my first Apex Policy Model."
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
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
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
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
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
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
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
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
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
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
task create name=MorningBoozeCheck version=0.0.1 uuid=3351b0f4-cf06-4fa2-8823-edf67bd30223 description=LS
This task checks if the sales request is for an item that contains alcohol.
If the local time is between 00:00:00 and 11:30:00 then the sale is not authorised. Otherwise the sale is authorised.
In this implementation we assume that all items with item_ID values between 1000 and 2000 contain alcohol :-)
LE
task inputfield create name=MorningBoozeCheck version=0.0.1 fieldName=sale_ID schemaName=sale_ID_type schemaVersion=0.0.1
task inputfield create name=MorningBoozeCheck version=0.0.1 fieldName=amount schemaName=price_type schemaVersion=0.0.1
task inputfield create name=MorningBoozeCheck version=0.0.1 fieldName=assistant_ID schemaName=assistant_ID_type schemaVersion=0.0.1
task inputfield create name=MorningBoozeCheck version=0.0.1 fieldName=notes schemaName=notes_type schemaVersion=0.0.1 optional=true
task inputfield create name=MorningBoozeCheck version=0.0.1 fieldName=quantity schemaName=quantity_type schemaVersion=0.0.1
task inputfield create name=MorningBoozeCheck version=0.0.1 fieldName=branch_ID schemaName=branch_ID_type schemaVersion=0.0.1
task inputfield create name=MorningBoozeCheck version=0.0.1 fieldName=item_ID schemaName=item_ID_type schemaVersion=0.0.1
task inputfield create name=MorningBoozeCheck version=0.0.1 fieldName=time schemaName=timestamp_type schemaVersion=0.0.1
task outputfield create name=MorningBoozeCheck version=0.0.1 fieldName=sale_ID schemaName=sale_ID_type schemaVersion=0.0.1
task outputfield create name=MorningBoozeCheck version=0.0.1 fieldName=amount schemaName=price_type schemaVersion=0.0.1
task outputfield create name=MorningBoozeCheck version=0.0.1 fieldName=assistant_ID schemaName=assistant_ID_type schemaVersion=0.0.1
task outputfield create name=MorningBoozeCheck version=0.0.1 fieldName=notes schemaName=notes_type schemaVersion=0.0.1 optional=true
task outputfield create name=MorningBoozeCheck version=0.0.1 fieldName=quantity schemaName=quantity_type schemaVersion=0.0.1
task outputfield create name=MorningBoozeCheck version=0.0.1 fieldName=branch_ID schemaName=branch_ID_type schemaVersion=0.0.1
task outputfield create name=MorningBoozeCheck version=0.0.1 fieldName=item_ID schemaName=item_ID_type schemaVersion=0.0.1
task outputfield create name=MorningBoozeCheck version=0.0.1 fieldName=authorised schemaName=authorised_type schemaVersion=0.0.1
task outputfield create name=MorningBoozeCheck version=0.0.1 fieldName=time schemaName=timestamp_type schemaVersion=0.0.1
task outputfield create name=MorningBoozeCheck version=0.0.1 fieldName=message schemaName=message_type schemaVersion=0.0.1 optional=true
task logic create name=MorningBoozeCheck version=0.0.1 logicFlavour=MVEL logic=LS
/*
* ============LICENSE_START=======================================================
* Copyright (C) 2016-2018 Ericsson. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
* ============LICENSE_END=========================================================
*/
import java.util.Date;
import java.util.Calendar;
import java.util.TimeZone;
import java.text.SimpleDateFormat;
logger.info("Task Execution: '"+subject.id+"'. Input Fields: '"+inFields+"'");
outFields.put("amount" , inFields.get("amount"));
outFields.put("assistant_ID", inFields.get("assistant_ID"));
outFields.put("notes" , inFields.get("notes"));
outFields.put("quantity" , inFields.get("quantity"));
outFields.put("branch_ID" , inFields.get("branch_ID"));
outFields.put("item_ID" , inFields.get("item_ID"));
outFields.put("time" , inFields.get("time"));
outFields.put("sale_ID" , inFields.get("sale_ID"));
item_id = inFields.get("item_ID");
//The events used later to test this task use GMT timezone!
gmt = TimeZone.getTimeZone("GMT");
timenow = Calendar.getInstance(gmt);
df = new SimpleDateFormat("HH:mm:ss z");
df.setTimeZone(gmt);
timenow.setTimeInMillis(inFields.get("time"));
midnight = timenow.clone();
midnight.set(
timenow.get(Calendar.YEAR),timenow.get(Calendar.MONTH),
timenow.get(Calendar.DATE),0,0,0);
eleven30 = timenow.clone();
eleven30.set(
timenow.get(Calendar.YEAR),timenow.get(Calendar.MONTH),
timenow.get(Calendar.DATE),11,30,0);
itemisalcohol = false;
if(item_id != null && item_id >=1000 && item_id < 2000)
itemisalcohol = true;
if( itemisalcohol
&& timenow.after(midnight) && timenow.before(eleven30)){
outFields.put("authorised", false);
outFields.put("message", "Sale not authorised by policy task "+subject.taskName+
" for time "+df.format(timenow.getTime())+
". Alcohol can not be sold between "+df.format(midnight.getTime())+
" and "+df.format(eleven30.getTime()));
return true;
}
else{
outFields.put("authorised", true);
outFields.put("message", "Sale authorised by policy task "+subject.taskName+
" for time "+df.format(timenow.getTime()));
return true;
}
/*
This task checks if a sale request is for an item that is an alcoholic drink.
If the local time is between 00:00:00 GMT and 11:30:00 GMT then the sale is not
authorised. Otherwise the sale is authorised.
In this implementation we assume that items with item_ID value between 1000 and
2000 are all alcoholic drinks :-)
*/
LE
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"
event parameter create name=SALE_AUTH version=0.0.1 parName=amount schemaName=price_type schemaVersion=0.0.1
event parameter create name=SALE_AUTH version=0.0.1 parName=assistant_ID schemaName=assistant_ID_type schemaVersion=0.0.1
event parameter create name=SALE_AUTH version=0.0.1 parName=authorised schemaName=authorised_type schemaVersion=0.0.1
event parameter create name=SALE_AUTH version=0.0.1 parName=branch_ID schemaName=branch_ID_type schemaVersion=0.0.1
event parameter create name=SALE_AUTH version=0.0.1 parName=item_ID schemaName=item_ID_type schemaVersion=0.0.1
event parameter create name=SALE_AUTH version=0.0.1 parName=message schemaName=message_type schemaVersion=0.0.1 optional=true
event parameter create name=SALE_AUTH version=0.0.1 parName=notes schemaName=notes_type schemaVersion=0.0.1 optional=true
event parameter create name=SALE_AUTH version=0.0.1 parName=quantity schemaName=quantity_type schemaVersion=0.0.1
event parameter create name=SALE_AUTH version=0.0.1 parName=sale_ID schemaName=sale_ID_type schemaVersion=0.0.1
event parameter create name=SALE_AUTH version=0.0.1 parName=time schemaName=timestamp_type schemaVersion=0.0.1
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"
event parameter create name=SALE_INPUT version=0.0.1 parName=amount schemaName=price_type schemaVersion=0.0.1
event parameter create name=SALE_INPUT version=0.0.1 parName=assistant_ID schemaName=assistant_ID_type schemaVersion=0.0.1
event parameter create name=SALE_INPUT version=0.0.1 parName=branch_ID schemaName=branch_ID_type schemaVersion=0.0.1
event parameter create name=SALE_INPUT version=0.0.1 parName=item_ID schemaName=item_ID_type schemaVersion=0.0.1
event parameter create name=SALE_INPUT version=0.0.1 parName=notes schemaName=notes_type schemaVersion=0.0.1 optional=true
event parameter create name=SALE_INPUT version=0.0.1 parName=quantity schemaName=quantity_type schemaVersion=0.0.1
event parameter create name=SALE_INPUT version=0.0.1 parName=sale_ID schemaName=sale_ID_type schemaVersion=0.0.1
event parameter create name=SALE_INPUT version=0.0.1 parName=time schemaName=timestamp_type schemaVersion=0.0.1
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
policy state create name=MyFirstPolicy version=0.0.1 stateName=BoozeAuthDecide triggerName=SALE_INPUT triggerVersion=0.0.1 defaultTaskName=MorningBoozeCheck defaultTaskVersion=0.0.1
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
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
Policy Step 2
^^^^^^^^^^^^^
.. container:: sectionbody
.. container:: sect1
.. rubric:: Scenario
:name: scenario
.. container:: paragraph
*HyperM* have just opened a new branch in a
different country, but that country has different
rules about when alcohol can be sold! In this
section we will go through the necessary steps to
extend our policy to enforce this for *HyperM*.
.. container:: ulist
- In some branches alcohol cannot be sold before
1pm, and not at all on Sundays.
.. container:: paragraph
Although there are a number of ways to accomplish
this the easiest approach for us is to define
another task and then select which task is
appropriate at runtime depending on the branch
identifier in the incoming event.
Extend Policy Model
-------------------
.. container:: sectionbody
.. container:: sect2
.. rubric:: Extend the Policy with the new Scenario
:name: extend_the_policy_with_the_new_scenario
.. container:: paragraph
To create a new Task click on the 'Tasks' tab. In the
'Tasks' pane, right click and select 'Create new Task':
.. container:: paragraph
Create a new Task called ``MorningBoozeCheckAlt1``. Use
the 'Generate UUID' button to create a new unique ID for
the task, and fill in a description for the task. Select
the same input and output fields that we used earlier
when we defined the ``MorningBoozeCheck`` task earlier.
.. table:: Table 1. Input fields for
``MorningBoozeCheckAlt1`` task
+-----------------------------------+-----------------------------------+
| Parameter Name | Parameter Type |
+===================================+===================================+
| time | timestamp_type |
+-----------------------------------+-----------------------------------+
| sale_ID | sale_ID_type |
+-----------------------------------+-----------------------------------+
| amount | price_type |
+-----------------------------------+-----------------------------------+
| item_ID | item_ID_type |
+-----------------------------------+-----------------------------------+
| quantity | quantity_type |
+-----------------------------------+-----------------------------------+
| assistant_ID | assistant_ID_type |
+-----------------------------------+-----------------------------------+
| branch_ID | branch_ID_type |
+-----------------------------------+-----------------------------------+
| notes | notes_type |
+-----------------------------------+-----------------------------------+
.. table:: Table 2. Output fields for
``MorningBoozeCheckAlt1`` task
+-----------------------------------+-----------------------------------+
| Parameter Name | Parameter Type |
+===================================+===================================+
| sale_ID | sale_ID_type |
+-----------------------------------+-----------------------------------+
| time | timestamp_type |
+-----------------------------------+-----------------------------------+
| authorised | authorised_type |
+-----------------------------------+-----------------------------------+
| message | message_type |
+-----------------------------------+-----------------------------------+
| amount | price_type |
+-----------------------------------+-----------------------------------+
| item_ID | item_ID_type |
+-----------------------------------+-----------------------------------+
| assistant_ID | assistant_ID_type |
+-----------------------------------+-----------------------------------+
| quantity | quantity_type |
+-----------------------------------+-----------------------------------+
| branch_ID | branch_ID_type |
+-----------------------------------+-----------------------------------+
| notes | notes_type |
+-----------------------------------+-----------------------------------+
.. container:: paragraph
This task also requires some 'Task Logic' to implement
the new behaviour for this task.
.. container:: paragraph
For simplicity use the following code for the task logic.
It again assumes that all items with ``item_ID`` between
1000 and 2000 contain alcohol. We again use the standard
``Java`` time utilities to check if the current time is
between ``00:00:00 CET`` and ``13:00:00 CET`` or if it is
``Sunday``.
.. container:: paragraph
For this task we will again author the logic using the
```MVEL`` <https://en.wikipedia.org/wiki/MVEL>`__
scripting language. Sample task logic code (specified in
```MVEL`` <https://en.wikipedia.org/wiki/MVEL>`__) is
given below. For a detailed guide to how to write your
own logic in
```JavaScript`` <https://en.wikipedia.org/wiki/JavaScript>`__,
```MVEL`` <https://en.wikipedia.org/wiki/MVEL>`__ or one
of the other supported languages please refer to APEX
Programmers Guide.
.. container:: listingblock
.. container:: content
.. code:: CodeRay
/*
* ============LICENSE_START=======================================================
* Copyright (C) 2016-2018 Ericsson. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
* ============LICENSE_END=========================================================
*/
import java.util.Date;
import java.util.Calendar;
import java.util.TimeZone;
import java.text.SimpleDateFormat;
logger.info("Task Execution: '"+subject.id+"'. Input Event: '"+inFields+"'");
outFields.put("amount" , inFields.get("amount"));
outFields.put("assistant_ID", inFields.get("assistant_ID"));
outFields.put("notes" , inFields.get("notes"));
outFields.put("quantity" , inFields.get("quantity"));
outFields.put("branch_ID" , inFields.get("branch_ID"));
outFields.put("item_ID" , inFields.get("item_ID"));
outFields.put("time" , inFields.get("time"));
outFields.put("sale_ID" , inFields.get("sale_ID"));
item_id = inFields.get("item_ID");
//The events used later to test this task use CET timezone!
cet = TimeZone.getTimeZone("CET");
timenow = Calendar.getInstance(cet);
df = new SimpleDateFormat("HH:mm:ss z");
df.setTimeZone(cet);
timenow.setTimeInMillis(inFields.get("time"));
midnight = timenow.clone();
midnight.set(
timenow.get(Calendar.YEAR),timenow.get(Calendar.MONTH),
timenow.get(Calendar.DATE),0,0,0);
onepm = timenow.clone();
onepm.set(
timenow.get(Calendar.YEAR),timenow.get(Calendar.MONTH),
timenow.get(Calendar.DATE),13,0,0);
itemisalcohol = false;
if(item_id != null && item_id >=1000 && item_id < 2000)
itemisalcohol = true;
if( itemisalcohol &&
( (timenow.after(midnight) && timenow.before(onepm))
||
(timenow.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)
)){
outFields.put("authorised", false);
outFields.put("message", "Sale not authorised by policy task "+subject.taskName+
" for time "+df.format(timenow.getTime())+
". Alcohol can not be sold between "+df.format(midnight.getTime())+
" and "+df.format(onepm.getTime()) +" or on Sunday");
return true;
}
else{
outFields.put("authorised", true);
outFields.put("message", "Sale authorised by policy task "+subject.taskName+
" for time "+df.format(timenow.getTime()));
return true;
}
/*
This task checks if a sale request is for an item that is an alcoholic drink.
If the local time is between 00:00:00 CET and 13:00:00 CET then the sale is not authorised.
Also alcohol sales are not allowed on Sundays. Otherwise the sale is authorised.
In this implementation we assume that items with item_ID between 1000 and 2000 are all alcoholic drinks :-)
*/
.. container:: imageblock
.. container:: content
|Create a new alternative task
\`MorningBoozeCheckAlt1\`|
.. container:: paragraph
The task definition is now complete so click the 'Submit'
button to save the task. Now that we have created our
task, we can can add this task to the single pre-existing
state (``BoozeAuthDecide``) in our policy.
.. container:: paragraph
To edit the ``BoozeAuthDecide`` state in our policy click
on the 'Policies' tab. In the 'Policies' pane, right
click on our ``MyFirstPolicy`` policy and select 'Edit'.
Navigate to the ``BoozeAuthDecide`` state in the 'states'
section at the bottom of the policy definition pane.
.. container:: imageblock
.. container:: content
|Right click to edit a policy|
.. container:: paragraph
To add our new task ``MorningBoozeCheckAlt1``, scroll
down to the ``BoozeAuthDecide`` state in the 'States'
section. In the 'State Tasks' section for
``BoozeAuthDecide`` use the 'Add new task' button. Select
our new ``MorningBoozeCheckAlt1`` task, and use the name
of the task as the 'Local Name' for the task. The
``MorningBoozeCheckAlt1`` task can reuse the same
``MorningBoozeCheck_Output_Direct`` 'Direct State Output
Mapping' that we used for the ``MorningBoozeCheck`` task.
(Recall that the role of the 'State Output Mapping' is to
select the output event for the state, and select the
next state to be executed. These both remain the same as
before.)
.. container:: paragraph
Since our state has more than one task we must define
some logic to determine which task should be used each
time the state is executed. This *task selection logic*
is defined in the state definition. For our
``BoozeAuthDecide`` state we want the choice of which
task to use to be based on the ``branch_ID`` from which
the ``SALE_INPUT`` event originated. For simplicity sake
let us assume that branches with ``branch_ID`` between
``0`` and ``999`` should use the ``MorningBoozeCheck``
task, and the branches with with ``branch_ID`` between
``1000`` and ``1999`` should use the
``MorningBoozeCheckAlt1`` task.
.. container:: paragraph
This time, for variety, we will author the task selection
logic using the
```JavaScript`` <https://en.wikipedia.org/wiki/JavaScript>`__
scripting language. Sample task selection logic code
(specified in
```JavaScript`` <https://en.wikipedia.org/wiki/JavaScript>`__)
is given below. Paste the script text into the 'Task
Selection Logic' box, and use "JAVASCRIPT" as the 'Task
Selection Logic Type / Flavour'. It is necessary to mark
one of the tasks as the 'Default Task' so that the task
selection logic always has a fallback default option in
cases where a particular task cannot be selected. In this
case the ``MorningBoozeCheck`` task can be the default
task.
.. container:: listingblock
.. container:: content
.. code:: CodeRay
/*
* ============LICENSE_START=======================================================
* Copyright (C) 2016-2018 Ericsson. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
* ============LICENSE_END=========================================================
*/
var returnValueType = Java.type("java.lang.Boolean");
var returnValue = new returnValueType(true);
executor.logger.info("Task Selection Execution: '"+executor.subject.id+
"'. Input Event: '"+executor.inFields+"'");
branchid = executor.inFields.get("branch_ID");
taskorig = executor.subject.getTaskKey("MorningBoozeCheck");
taskalt = executor.subject.getTaskKey("MorningBoozeCheckAlt1");
taskdef = executor.subject.getDefaultTaskKey();
if(branchid >=0 && branchid <1000){
taskorig.copyTo(executor.selectedTask);
}
else if (branchid >=1000 && branchid <2000){
taskalt.copyTo(executor.selectedTask);
}
else{
taskdef.copyTo(executor.selectedTask);
}
/*
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"
*/
.. container:: imageblock
.. container:: content
|State definition with 2 Tasks and Task Selection
Logic|
.. container:: paragraph
When complete don’t forget to click the 'Submit' button
at the bottom of 'Policies' pane for our
``MyFirstPolicy`` policy after updating the
``BoozeAuthDecide`` state.
.. container:: paragraph
Congratulations, you have now completed the second step
towards your first APEX policy. The policy model
containing our new policy can again be validated and
exported from the editor and saved as shown in Step 1.
.. container:: paragraph
The exported policy model is then available in the
directory you selected, as
`MyFirstPolicyModel_0.0.1.json <files/mfp-files/2/MyFirstPolicyModel_0.0.1.json>`__.
The exported policy can now be loaded into the APEX
Policy Engine, or can be re-loaded and edited by the APEX
Policy Editor.
Test The Policy
---------------
.. container:: sectionbody
.. container:: sect2
.. rubric:: Test Policy Step 2
:name: test_policy_step_2
.. container:: paragraph
To start a new APEX Engine you can use the following
configuration. In a full APEX installation you can find
this configuration in
``$APEX_HOME/examples/config/MyFirstPolicy/2/MyFirstPolicyConfigStdin2StdoutJsonEvent.json``.
Note, this has changed from the configuration file in
Step 1 to enable the ``JAVASCRIPT`` executor for our new
'Task Selection Logic'.
.. container:: listingblock
.. container:: content
.. code:: CodeRay
{
"engineServiceParameters" : {
"name" : "MyFirstPolicyApexEngine",
"version" : "0.0.1",
"id" : 102,
"instanceCount" : 4,
"deploymentPort" : 12345,
"policyModelFileName" : "examples/models/MyFirstPolicy/2/MyFirstPolicyModel_0.0.1.json",
"engineParameters" : {
"executorParameters" : {
"MVEL" : {
"parameterClassName" : "org.onap.policy.apex.plugins.executor.mvel.MVELExecutorParameters"
},
"JAVASCRIPT" : {
"parameterClassName" : "org.onap.policy.apex.plugins.executor.javascript.JavascriptExecutorParameters"
}
}
}
},
"eventOutputParameters": {
"FirstProducer": {
"carrierTechnologyParameters" : {
"carrierTechnology" : "FILE",
"parameters" : {
"standardIO" : true
}
},
"eventProtocolParameters" : {
"eventProtocol" : "JSON"
}
}
},
"eventInputParameters": {
"FirstConsumer": {
"carrierTechnologyParameters" : {
"carrierTechnology" : "FILE",
"parameters" : {
"standardIO" : true
}
},
"eventProtocolParameters" : {
"eventProtocol" : "JSON"
}
}
}
}
.. container:: paragraph
To test the policy try paste the following events into
the console as the APEX engine executes. Note, all tests
from Step 1 will still work perfectly since none of those
events originate from a branch with ``branch_ID`` between
``1000`` and ``2000``. The 'Task Selection Logic' will
therefore pick the ``MorningBoozeCheck`` task as
expected, and will therefore give the same results.
.. list-table:: Table 1. Inputs and Outputs when testing *My First Policy*
:widths: 25 40 35
:header-rows: 1
* - Input Event (JSON)
- Output Event (JSON)
- comment
* - .. literalinclude:: events/1/EventIn_BoozeItem_084106GMT.json
:language: JSON
- .. literalinclude:: events/1/EventOut_BoozeItem_084106GMT.json
:language: JSON
- .. container:: paragraph
Request to buy alcohol item (`item_ID=1249`) at _08:41:06 GMT_ on _Monday, 02 January 2017_.
Sale is not authorized. Uses the `MorningBoozeCheck` task.
* - .. literalinclude:: events/2/EventIn_BoozeItem_101433CET_thurs.json
:language: JSON
- .. literalinclude:: events/2/EventOut_BoozeItem_101433CET_thurs.json
:language: JSON
- .. container:: paragraph
Request to buy alcohol (`item_ID=1047`) at _10:14:33_ on _Thursday, 22 December 2016_.
Sale is not authorized. Uses the `MorningBoozeCheckAlt1` task.
* - .. literalinclude:: events/2/EventIn_BoozeItem_171937CET_sun.json
:language: JSON
- .. literalinclude:: events/2/EventOut_BoozeItem_171937CET_sun.json
:language: JSON
- .. container:: paragraph
Request to buy alcohol (`item_ID=1443`) at _17:19:37_ on _Sunday, 18 December 2016_.
Sale is not authorized. Uses the `MorningBoozeCheckAlt1` task.
* - .. literalinclude:: events/2/EventIn_NonBoozeItem_111309CET_mon.json
:language: JSON
- .. literalinclude:: events/2/EventOut_NonBoozeItem_111309CET_mon.json
:language: JSON
- .. container:: paragraph
Request to buy non-alcoholic item (`item_ID=5321`) at _11:13:09_ on _Monday, 2 January 2017_.
Sale is authorized. Uses the `MorningBoozeCheckAlt1` task.
CLI Editor File
---------------
.. container:: sectionbody
.. container:: sect2
.. rubric:: Policy 2 in CLI Editor
:name: policy_2_in_cli_editor
.. container:: paragraph
An equivalent version of the ``MyFirstPolicyModel``
policy model can again be generated using the APEX CLI
editor. A sample APEX CLI script is shown below:
.. container:: listingblock
.. container:: content
.. code:: CodeRay
#-------------------------------------------------------------------------------
# ============LICENSE_START=======================================================
# Copyright (C) 2016-2018 Ericsson. All rights reserved.
# ================================================================================
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
# ============LICENSE_END=========================================================
#-------------------------------------------------------------------------------
model create name=MyFirstPolicyModel version=0.0.1 uuid=540226fb-55ee-4f0e-a444-983a0494818e description="This is my first Apex Policy Model."
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
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
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
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
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
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
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
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
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
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
task create name=MorningBoozeCheck version=0.0.1 uuid=3351b0f4-cf06-4fa2-8823-edf67bd30223 description=LS
This task checks if the sales request is for an item that contains alcohol.
If the local time is between 00:00:00 and 11:30:00 then the sale is not authorised. Otherwise the sale is authorised.
In this implementation we assume that all items with item_ID values between 1000 and 2000 contain alcohol :-)
LE
task inputfield create name=MorningBoozeCheck version=0.0.1 fieldName=sale_ID schemaName=sale_ID_type schemaVersion=0.0.1
task inputfield create name=MorningBoozeCheck version=0.0.1 fieldName=amount schemaName=price_type schemaVersion=0.0.1
task inputfield create name=MorningBoozeCheck version=0.0.1 fieldName=assistant_ID schemaName=assistant_ID_type schemaVersion=0.0.1
task inputfield create name=MorningBoozeCheck version=0.0.1 fieldName=notes schemaName=notes_type schemaVersion=0.0.1 optional=true
task inputfield create name=MorningBoozeCheck version=0.0.1 fieldName=quantity schemaName=quantity_type schemaVersion=0.0.1
task inputfield create name=MorningBoozeCheck version=0.0.1 fieldName=branch_ID schemaName=branch_ID_type schemaVersion=0.0.1
task inputfield create name=MorningBoozeCheck version=0.0.1 fieldName=item_ID schemaName=item_ID_type schemaVersion=0.0.1
task inputfield create name=MorningBoozeCheck version=0.0.1 fieldName=time schemaName=timestamp_type schemaVersion=0.0.1
task outputfield create name=MorningBoozeCheck version=0.0.1 fieldName=sale_ID schemaName=sale_ID_type schemaVersion=0.0.1
task outputfield create name=MorningBoozeCheck version=0.0.1 fieldName=amount schemaName=price_type schemaVersion=0.0.1
task outputfield create name=MorningBoozeCheck version=0.0.1 fieldName=assistant_ID schemaName=assistant_ID_type schemaVersion=0.0.1
task outputfield create name=MorningBoozeCheck version=0.0.1 fieldName=notes schemaName=notes_type schemaVersion=0.0.1 optional=true
task outputfield create name=MorningBoozeCheck version=0.0.1 fieldName=quantity schemaName=quantity_type schemaVersion=0.0.1
task outputfield create name=MorningBoozeCheck version=0.0.1 fieldName=branch_ID schemaName=branch_ID_type schemaVersion=0.0.1
task outputfield create name=MorningBoozeCheck version=0.0.1 fieldName=item_ID schemaName=item_ID_type schemaVersion=0.0.1
task outputfield create name=MorningBoozeCheck version=0.0.1 fieldName=authorised schemaName=authorised_type schemaVersion=0.0.1
task outputfield create name=MorningBoozeCheck version=0.0.1 fieldName=time schemaName=timestamp_type schemaVersion=0.0.1
task outputfield create name=MorningBoozeCheck version=0.0.1 fieldName=message schemaName=message_type schemaVersion=0.0.1 optional=true
task logic create name=MorningBoozeCheck version=0.0.1 logicFlavour=MVEL logic=LS
/*
* ============LICENSE_START=======================================================
* Copyright (C) 2016-2018 Ericsson. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
* ============LICENSE_END=========================================================
*/
import java.util.Date;
import java.util.Calendar;
import java.util.TimeZone;
import java.text.SimpleDateFormat;
logger.info("Task Execution: '"+subject.id+"'. Input Fields: '"+inFields+"'");
outFields.put("amount" , inFields.get("amount"));
outFields.put("assistant_ID", inFields.get("assistant_ID"));
outFields.put("notes" , inFields.get("notes"));
outFields.put("quantity" , inFields.get("quantity"));
outFields.put("branch_ID" , inFields.get("branch_ID"));
outFields.put("item_ID" , inFields.get("item_ID"));
outFields.put("time" , inFields.get("time"));
outFields.put("sale_ID" , inFields.get("sale_ID"));
item_id = inFields.get("item_ID");
//The events used later to test this task use GMT timezone!
gmt = TimeZone.getTimeZone("GMT");
timenow = Calendar.getInstance(gmt);
df = new SimpleDateFormat("HH:mm:ss z");
df.setTimeZone(gmt);
timenow.setTimeInMillis(inFields.get("time"));
midnight = timenow.clone();
midnight.set(
timenow.get(Calendar.YEAR),timenow.get(Calendar.MONTH),
timenow.get(Calendar.DATE),0,0,0);
eleven30 = timenow.clone();
eleven30.set(
timenow.get(Calendar.YEAR),timenow.get(Calendar.MONTH),
timenow.get(Calendar.DATE),11,30,0);
itemisalcohol = false;
if(item_id != null && item_id >=1000 && item_id < 2000)
itemisalcohol = true;
if( itemisalcohol
&& timenow.after(midnight) && timenow.before(eleven30)){
outFields.put("authorised", false);
outFields.put("message", "Sale not authorised by policy task "+subject.taskName+
" for time "+df.format(timenow.getTime())+
". Alcohol can not be sold between "+df.format(midnight.getTime())+
" and "+df.format(eleven30.getTime()));
return true;
}
else{
outFields.put("authorised", true);
outFields.put("message", "Sale authorised by policy task "+subject.taskName+
" for time "+df.format(timenow.getTime()));
return true;
}
/*
This task checks if a sale request is for an item that is an alcoholic drink.
If the local time is between 00:00:00 GMT and 11:30:00 GMT then the sale is not
authorised. Otherwise the sale is authorised.
In this implementation we assume that items with item_ID value between 1000 and
2000 are all alcoholic drinks :-)
*/
LE
task create name=MorningBoozeCheckAlt1 version=0.0.1 uuid=bc6d90c9-c902-4686-afd3-925b30e39990 description=LS
This task checks if a sale request is for an item that is an alcoholic drink.
If the local time is between 00:00:00 CET and 13:00:00 CET then the sale is not authorised.
Also alcohol sales are not allowed on Sundays. Otherwise the sale is authorised.
In this implementation we assume that items with item_ID between 1000 and 2000 are all alcoholic drinks
LE
task inputfield create name=MorningBoozeCheckAlt1 version=0.0.1 fieldName=sale_ID schemaName=sale_ID_type schemaVersion=0.0.1
task inputfield create name=MorningBoozeCheckAlt1 version=0.0.1 fieldName=amount schemaName=price_type schemaVersion=0.0.1
task inputfield create name=MorningBoozeCheckAlt1 version=0.0.1 fieldName=assistant_ID schemaName=assistant_ID_type schemaVersion=0.0.1
task inputfield create name=MorningBoozeCheckAlt1 version=0.0.1 fieldName=notes schemaName=notes_type schemaVersion=0.0.1 optional=true
task inputfield create name=MorningBoozeCheckAlt1 version=0.0.1 fieldName=quantity schemaName=quantity_type schemaVersion=0.0.1
task inputfield create name=MorningBoozeCheckAlt1 version=0.0.1 fieldName=branch_ID schemaName=branch_ID_type schemaVersion=0.0.1
task inputfield create name=MorningBoozeCheckAlt1 version=0.0.1 fieldName=item_ID schemaName=item_ID_type schemaVersion=0.0.1
task inputfield create name=MorningBoozeCheckAlt1 version=0.0.1 fieldName=time schemaName=timestamp_type schemaVersion=0.0.1
task outputfield create name=MorningBoozeCheckAlt1 version=0.0.1 fieldName=sale_ID schemaName=sale_ID_type schemaVersion=0.0.1
task outputfield create name=MorningBoozeCheckAlt1 version=0.0.1 fieldName=amount schemaName=price_type schemaVersion=0.0.1
task outputfield create name=MorningBoozeCheckAlt1 version=0.0.1 fieldName=assistant_ID schemaName=assistant_ID_type schemaVersion=0.0.1
task outputfield create name=MorningBoozeCheckAlt1 version=0.0.1 fieldName=notes schemaName=notes_type schemaVersion=0.0.1 optional=true
task outputfield create name=MorningBoozeCheckAlt1 version=0.0.1 fieldName=quantity schemaName=quantity_type schemaVersion=0.0.1
task outputfield create name=MorningBoozeCheckAlt1 version=0.0.1 fieldName=branch_ID schemaName=branch_ID_type schemaVersion=0.0.1
task outputfield create name=MorningBoozeCheckAlt1 version=0.0.1 fieldName=item_ID schemaName=item_ID_type schemaVersion=0.0.1
task outputfield create name=MorningBoozeCheckAlt1 version=0.0.1 fieldName=authorised schemaName=authorised_type schemaVersion=0.0.1
task outputfield create name=MorningBoozeCheckAlt1 version=0.0.1 fieldName=time schemaName=timestamp_type schemaVersion=0.0.1
task outputfield create name=MorningBoozeCheckAlt1 version=0.0.1 fieldName=message schemaName=message_type schemaVersion=0.0.1 optional=true
task logic create name=MorningBoozeCheckAlt1 version=0.0.1 logicFlavour=MVEL logic=LS
/*
* ============LICENSE_START=======================================================
* Copyright (C) 2016-2018 Ericsson. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
* ============LICENSE_END=========================================================
*/
import java.util.Date;
import java.util.Calendar;
import java.util.TimeZone;
import java.text.SimpleDateFormat;
logger.info("Task Execution: '"+subject.id+"'. Input Event: '"+inFields+"'");
outFields.put("amount" , inFields.get("amount"));
outFields.put("assistant_ID", inFields.get("assistant_ID"));
outFields.put("notes" , inFields.get("notes"));
outFields.put("quantity" , inFields.get("quantity"));
outFields.put("branch_ID" , inFields.get("branch_ID"));
outFields.put("item_ID" , inFields.get("item_ID"));
outFields.put("time" , inFields.get("time"));
outFields.put("sale_ID" , inFields.get("sale_ID"));
item_id = inFields.get("item_ID");
//The events used later to test this task use CET timezone!
cet = TimeZone.getTimeZone("CET");
timenow = Calendar.getInstance(cet);
df = new SimpleDateFormat("HH:mm:ss z");
df.setTimeZone(cet);
timenow.setTimeInMillis(inFields.get("time"));
midnight = timenow.clone();
midnight.set(
timenow.get(Calendar.YEAR),timenow.get(Calendar.MONTH),
timenow.get(Calendar.DATE),0,0,0);
onepm = timenow.clone();
onepm.set(
timenow.get(Calendar.YEAR),timenow.get(Calendar.MONTH),
timenow.get(Calendar.DATE),13,0,0);
itemisalcohol = false;
if(item_id != null && item_id >=1000 && item_id < 2000)
itemisalcohol = true;
if( itemisalcohol &&
( (timenow.after(midnight) && timenow.before(onepm))
||
(timenow.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)
)){
outFields.put("authorised", false);
outFields.put("message", "Sale not authorised by policy task "+subject.taskName+
" for time "+df.format(timenow.getTime())+
". Alcohol can not be sold between "+df.format(midnight.getTime())+
" and "+df.format(onepm.getTime()) +" or on Sunday");
return true;
}
else{
outFields.put("authorised", true);
outFields.put("message", "Sale authorised by policy task "+subject.taskName+
" for time "+df.format(timenow.getTime()));
return true;
}
/*
This task checks if a sale request is for an item that is an alcoholic drink.
If the local time is between 00:00:00 CET and 13:00:00 CET then the sale is not authorised.
Also alcohol sales are not allowed on Sundays. Otherwise the sale is authorised.
In this implementation we assume that items with item_ID between 1000 and 2000 are all alcoholic drinks :-)
*/
LE
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"
event parameter create name=SALE_AUTH version=0.0.1 parName=amount schemaName=price_type schemaVersion=0.0.1
event parameter create name=SALE_AUTH version=0.0.1 parName=assistant_ID schemaName=assistant_ID_type schemaVersion=0.0.1
event parameter create name=SALE_AUTH version=0.0.1 parName=authorised schemaName=authorised_type schemaVersion=0.0.1
event parameter create name=SALE_AUTH version=0.0.1 parName=branch_ID schemaName=branch_ID_type schemaVersion=0.0.1
event parameter create name=SALE_AUTH version=0.0.1 parName=item_ID schemaName=item_ID_type schemaVersion=0.0.1
event parameter create name=SALE_AUTH version=0.0.1 parName=message schemaName=message_type schemaVersion=0.0.1 optional=true
event parameter create name=SALE_AUTH version=0.0.1 parName=notes schemaName=notes_type schemaVersion=0.0.1 optional=true
event parameter create name=SALE_AUTH version=0.0.1 parName=quantity schemaName=quantity_type schemaVersion=0.0.1
event parameter create name=SALE_AUTH version=0.0.1 parName=sale_ID schemaName=sale_ID_type schemaVersion=0.0.1
event parameter create name=SALE_AUTH version=0.0.1 parName=time schemaName=timestamp_type schemaVersion=0.0.1
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"
event parameter create name=SALE_INPUT version=0.0.1 parName=amount schemaName=price_type schemaVersion=0.0.1
event parameter create name=SALE_INPUT version=0.0.1 parName=assistant_ID schemaName=assistant_ID_type schemaVersion=0.0.1
event parameter create name=SALE_INPUT version=0.0.1 parName=branch_ID schemaName=branch_ID_type schemaVersion=0.0.1
event parameter create name=SALE_INPUT version=0.0.1 parName=item_ID schemaName=item_ID_type schemaVersion=0.0.1
event parameter create name=SALE_INPUT version=0.0.1 parName=notes schemaName=notes_type schemaVersion=0.0.1 optional=true
event parameter create name=SALE_INPUT version=0.0.1 parName=quantity schemaName=quantity_type schemaVersion=0.0.1
event parameter create name=SALE_INPUT version=0.0.1 parName=sale_ID schemaName=sale_ID_type schemaVersion=0.0.1
event parameter create name=SALE_INPUT version=0.0.1 parName=time schemaName=timestamp_type schemaVersion=0.0.1
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
policy state create name=MyFirstPolicy version=0.0.1 stateName=BoozeAuthDecide triggerName=SALE_INPUT triggerVersion=0.0.1 defaultTaskName=MorningBoozeCheck defaultTaskVersion=0.0.1
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
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
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
policy state selecttasklogic create name=MyFirstPolicy version=0.0.1 stateName=BoozeAuthDecide logicFlavour=JAVASCRIPT logic=LS
/*
* ============LICENSE_START=======================================================
* Copyright (C) 2016-2018 Ericsson. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
* ============LICENSE_END=========================================================
*/
var returnValueType = Java.type("java.lang.Boolean");
var returnValue = new returnValueType(true);
executor.logger.info("Task Selection Execution: '"+executor.subject.id+"'. Input Event: '"+executor.inFields+"'");
branchid = executor.inFields.get("branch_ID");
taskorig = executor.subject.getTaskKey("MorningBoozeCheck");
taskalt = executor.subject.getTaskKey("MorningBoozeCheckAlt1");
taskdef = executor.subject.getDefaultTaskKey();
if(branchid >=0 && branchid <1000){
taskorig.copyTo(executor.selectedTask);
}
else if (branchid >=1000 && branchid <2000){
taskalt.copyTo(executor.selectedTask);
}
else{
taskdef.copyTo(executor.selectedTask);
}
/*
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"
*/
LE
.. container::
:name: footer-text
2.3.0-SNAPSHOT
Last updated 2020-04-03 16:04:24 IST
.. |File > New to create a new Policy Model| image:: images/mfp/MyFirstPolicy_P1_newPolicyModel1.png
.. |Create a new Policy Model| image:: images/mfp/MyFirstPolicy_P1_newPolicyModel2.png
.. |ONAP| image:: ../../../images/logos.png
:class: builtBy
:target: http://www.onap.org/
.. |Right click to create a new event| image:: images/mfp/MyFirstPolicy_P1_newEvent1.png
.. |Fill in the necessary information for the 'SALE_INPUT' event and click 'Submit'| image:: images/mfp/MyFirstPolicy_P1_newEvent2.png
.. |Right click to create a new Item Schema| image:: images/mfp/MyFirstPolicy_P1_newItemSchema1.png
.. |Create a new Item Schema| image:: images/mfp/MyFirstPolicy_P1_newItemSchema2.png
.. |Add new event parameters to an event| image:: images/mfp/MyFirstPolicy_P1_newEvent3.png
.. |Right click to create a new task| image:: images/mfp/MyFirstPolicy_P1_newTask1.png
.. |Add input and out fields for the task| image:: images/mfp/MyFirstPolicy_P1_newTask2.png
.. |Add task logic the task| image:: images/mfp/MyFirstPolicy_P1_newTask3.png
.. |Create a new policy| image:: images/mfp/MyFirstPolicy_P1_newPolicy1.png
.. |Create a state| image:: images/mfp/MyFirstPolicy_P1_newState1.png
.. |Add a Task and Output Mapping| image:: images/mfp/MyFirstPolicy_P1_newState2.png
.. |Validate the policy model for error using the 'Model' > 'Validate' menu item| image:: images/mfp/MyFirstPolicy_P1_validatePolicyModel.png
.. |Download the completed policy model using the 'File' > 'Download' menu item| image:: images/mfp/MyFirstPolicy_P1_exportPolicyModel1.png
.. |Create a new alternative task \`MorningBoozeCheckAlt1\`| image:: images/mfp/MyFirstPolicy_P2_newTask1.png
.. |Right click to edit a policy| image:: images/mfp/MyFirstPolicy_P2_editPolicy1.png
.. |State definition with 2 Tasks and Task Selection Logic| image:: images/mfp/MyFirstPolicy_P2_editState1.png
|