@freelog/freelog-cg-collection 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (73) hide show
  1. package/CollectionPolicyCustomVisitor.js +544 -0
  2. package/CollectionPolicyDecompiler.js +131 -0
  3. package/CollectionPolicyErrorLexerListener.js +23 -0
  4. package/CollectionPolicyErrorListener.js +22 -0
  5. package/README.md +398 -0
  6. package/TransitionEventArgsMatchUtil.js +79 -0
  7. package/gen/CollectionPolicyLexer.js +705 -0
  8. package/gen/CollectionPolicyListener.js +672 -0
  9. package/gen/CollectionPolicyParser.js +7301 -0
  10. package/gen/CollectionPolicyVisitor.js +454 -0
  11. package/gen/LexToken.js +680 -0
  12. package/index.js +53 -0
  13. package/indexTest.js +23 -0
  14. package/package.json +32 -0
  15. package/resources/event_definition.json +169 -0
  16. package/resources/function_definite.json +79 -0
  17. package/resources/test/CalculatorTest.json +26 -0
  18. package/resources/test/FunctionHelperTest.json +21 -0
  19. package/resources/variable_definite.json +10 -0
  20. package/resources/zhaojn.json +68 -0
  21. package/resources/zhaojn.sc +8 -0
  22. package/src/calculator/ExpArgsGroupCalculator.ts +34 -0
  23. package/src/calculator/ExpBooleanCalculator.ts +77 -0
  24. package/src/calculator/ExpCalculator.ts +68 -0
  25. package/src/calculator/ExpCollectionCalculator.ts +0 -0
  26. package/src/calculator/ExpConditionCalculator.ts +43 -0
  27. package/src/function-executor/FunctionHandle.ts +45 -0
  28. package/src/function-executor/api/FunctionApiExecutor.ts +11 -0
  29. package/src/function-executor/fun/FunctionFunExecutor.ts +31 -0
  30. package/src/function-executor/kit/FunctionKitExecutor.ts +11 -0
  31. package/src/function-executor/query/FunctionQueryExecutor.ts +11 -0
  32. package/src/function-executor/time/FunctionTimeExecutor.ts +11 -0
  33. package/src/helper/ExpHelper.ts +66 -0
  34. package/src/helper/FunctionHelper.ts +97 -0
  35. package/src/helper/PolicyHelper.ts +99 -0
  36. package/src/interface/Calculator.ts +6 -0
  37. package/src/interface/DefaultFunctionInvoke.ts +12 -0
  38. package/src/interface/FunctionInvoke.ts +6 -0
  39. package/src/interface/VarContextCapable.ts +3 -0
  40. package/src/model/AssignmentClauseInfo.ts +3 -0
  41. package/src/model/exp/EntityInfo.ts +7 -0
  42. package/src/model/exp/Exp.ts +5 -0
  43. package/src/model/exp/ExpAtom.ts +5 -0
  44. package/src/model/exp/ExpOp.ts +8 -0
  45. package/src/model/exp/ExpParen.ts +6 -0
  46. package/src/model/exp/ExpSigned.ts +7 -0
  47. package/src/model/exp/FunctionCallInfo.ts +13 -0
  48. package/src/model/exp/NumberInfo.ts +6 -0
  49. package/src/model/exp/StringInfo.ts +6 -0
  50. package/src/model/exp/VariableChainInfo.ts +7 -0
  51. package/src/model/exp-args-group/ExpArgsGroup.ts +10 -0
  52. package/src/model/exp-boolean/BooleanInfo.ts +6 -0
  53. package/src/model/exp-boolean/ExpBoolean.ts +5 -0
  54. package/src/model/exp-boolean/ExpBooleanAtom.ts +5 -0
  55. package/src/model/exp-boolean/ExpBooleanOpCollection.ts +8 -0
  56. package/src/model/exp-boolean/ExpBooleanOpCompare.ts +9 -0
  57. package/src/model/exp-boolean/ExpBooleanOpLogic.ts +8 -0
  58. package/src/model/exp-boolean/ExpBooleanParen.ts +6 -0
  59. package/src/model/exp-collection/ExpCollection.ts +10 -0
  60. package/src/model/exp-condition/ExpCondition.ts +12 -0
  61. package/src/model/policy/ActionInfo.ts +23 -0
  62. package/src/model/policy/EventInfo.ts +13 -0
  63. package/src/model/policy/PolicyInfo.ts +20 -0
  64. package/src/model/policy/VarDeclarationInfo.ts +8 -0
  65. package/test/calculator/CalculatorTest.test.ts +13 -0
  66. package/test/function/FunAddElements.ts +9 -0
  67. package/test/function/FunctionFunExecutorTest.test.ts +11 -0
  68. package/test/function/FunctionHandleTest.test.ts +9 -0
  69. package/test/helper/ExpHelperTest.test.ts +7 -0
  70. package/test/helper/FunctionHelperTest.test.ts +16 -0
  71. package/test/helper/PolicyHelperTest.test.ts +14 -0
  72. package/test/tsconfig.test.json +6 -0
  73. package/tsconfig.json +24 -0
@@ -0,0 +1,22 @@
1
+ const antlr4 = require("antlr4");
2
+
3
+ class CollectionPolicyErrorListener extends antlr4.error.ErrorListener {
4
+
5
+ constructor() {
6
+ super();
7
+ this.errors = [];
8
+ this.errorObjects = [];
9
+ }
10
+
11
+ syntaxError(recognizer, offendingSymbol, line, charPositionInLine, msg, e) {
12
+ this.errors.push(`line ${line}:${charPositionInLine} ${msg}`);
13
+ this.errorObjects.push({
14
+ line: line,
15
+ charPositionInLine: charPositionInLine,
16
+ offendingSymbol: offendingSymbol.text,
17
+ msg: msg
18
+ });
19
+ }
20
+ }
21
+
22
+ exports.CollectionPolicyErrorListener = CollectionPolicyErrorListener;
package/README.md ADDED
@@ -0,0 +1,398 @@
1
+ #### 组合结构文档
2
+
3
+ 输入文件:
4
+
5
+ ```
6
+ for public
7
+
8
+ c1 // 广告已授权
9
+ c2 = this.all.range(0,9) // 免费
10
+ c3 = this.all.range(10,19) // 广告
11
+ c4 = this.all.exclude(c2).exclude(c3) // 付费
12
+ c5 // 付费已授权
13
+ c6 = c1.unionSet(c5) // 已授权
14
+
15
+ s1[c2,c6]:
16
+ case in c3 ~freelog.e1() => s2 do c1.set(context.id);c1.unset(context.id);c1.set(context.id); // 看完广告
17
+ s2[c2,c6]:
18
+ case in c4 ~freelog.e2() => s2 do c5.set(context.id); // 付费
19
+ /*
20
+ * 定期清理付费的单品,假设付费的单品是有时间期限的
21
+ * 定期清理广告的单品,假设广告的单品是有时间期限的
22
+ */
23
+ ~freelog.CycleEndEvent("4","cycle") => s2 do c1.clean();c5.clean();
24
+ ~freelog.e4() => s3 // 合约终止
25
+ s3:
26
+ terminate
27
+
28
+ ```
29
+
30
+ 输出文件:
31
+
32
+ ```
33
+ {
34
+ "audiences": [
35
+ {
36
+ "name": "public",
37
+ "type": "public"
38
+ }
39
+ ],
40
+ "serviceStates": [
41
+ {
42
+ "name": "c1"
43
+ },
44
+ {
45
+ "name": "c2",
46
+ "caller": {
47
+ "name": "this.all",
48
+ "type": "variable"
49
+ },
50
+ "functions": [
51
+ {
52
+ "functionName": "range",
53
+ "functionArgs": {
54
+ "index": "0",
55
+ "limit": "9"
56
+ }
57
+ }
58
+ ]
59
+ },
60
+ {
61
+ "name": "c3",
62
+ "caller": {
63
+ "name": "this.all",
64
+ "type": "variable"
65
+ },
66
+ "functions": [
67
+ {
68
+ "functionName": "range",
69
+ "functionArgs": {
70
+ "index": "10",
71
+ "limit": "19"
72
+ }
73
+ }
74
+ ]
75
+ },
76
+ {
77
+ "name": "c4",
78
+ "caller": {
79
+ "name": "this.all",
80
+ "type": "variable"
81
+ },
82
+ "functions": [
83
+ {
84
+ "functionName": "exclude",
85
+ "functionArgs": {
86
+ "a": "c2"
87
+ }
88
+ },
89
+ {
90
+ "functionName": "exclude",
91
+ "functionArgs": {
92
+ "a": "c3"
93
+ }
94
+ }
95
+ ]
96
+ },
97
+ {
98
+ "name": "c5"
99
+ },
100
+ {
101
+ "name": "c6",
102
+ "caller": {
103
+ "name": "c1",
104
+ "type": "serviceState"
105
+ },
106
+ "functions": [
107
+ {
108
+ "functionName": "unionSet",
109
+ "functionArgs": {
110
+ "a": "c5"
111
+ }
112
+ }
113
+ ]
114
+ }
115
+ ],
116
+ "stateMachine": {
117
+ "s1": {
118
+ "serviceStates": [
119
+ "c2",
120
+ "c6"
121
+ ],
122
+ "transitions": [
123
+ {
124
+ "toState": "s2",
125
+ "service": "freelog",
126
+ "name": "e1",
127
+ "condition": {
128
+ "type": "case",
129
+ "serviceState": "c3"
130
+ },
131
+ "actions": [
132
+ {
133
+ "caller": {
134
+ "name": "c1",
135
+ "type": "serviceState"
136
+ },
137
+ "functions": [
138
+ {
139
+ "functionName": "set",
140
+ "functionArgs": {
141
+ "a": "context.id"
142
+ }
143
+ }
144
+ ]
145
+ },
146
+ {
147
+ "caller": {
148
+ "name": "c1",
149
+ "type": "serviceState"
150
+ },
151
+ "functions": [
152
+ {
153
+ "functionName": "unset",
154
+ "functionArgs": {
155
+ "a": "context.id"
156
+ }
157
+ }
158
+ ]
159
+ },
160
+ {
161
+ "caller": {
162
+ "name": "c1",
163
+ "type": "serviceState"
164
+ },
165
+ "functions": [
166
+ {
167
+ "functionName": "set",
168
+ "functionArgs": {
169
+ "a": "context.id"
170
+ }
171
+ }
172
+ ]
173
+ }
174
+ ]
175
+ }
176
+ ]
177
+ },
178
+ "s2": {
179
+ "serviceStates": [
180
+ "c2",
181
+ "c6"
182
+ ],
183
+ "transitions": [
184
+ {
185
+ "toState": "s2",
186
+ "service": "freelog",
187
+ "name": "e2",
188
+ "condition": {
189
+ "type": "case",
190
+ "serviceState": "c4"
191
+ },
192
+ "actions": [
193
+ {
194
+ "caller": {
195
+ "name": "c5",
196
+ "type": "serviceState"
197
+ },
198
+ "functions": [
199
+ {
200
+ "functionName": "set",
201
+ "functionArgs": {
202
+ "a": "context.id"
203
+ }
204
+ }
205
+ ]
206
+ }
207
+ ]
208
+ },
209
+ {
210
+ "toState": "s2",
211
+ "service": "freelog",
212
+ "name": "CycleEndEvent",
213
+ "args": {
214
+ "cycleCount": 4,
215
+ "timeUnit": "cycle"
216
+ },
217
+ "actions": [
218
+ {
219
+ "caller": {
220
+ "name": "c1",
221
+ "type": "serviceState"
222
+ },
223
+ "functions": [
224
+ {
225
+ "functionName": "clean",
226
+ "functionArgs": []
227
+ }
228
+ ]
229
+ },
230
+ {
231
+ "caller": {
232
+ "name": "c5",
233
+ "type": "serviceState"
234
+ },
235
+ "functions": [
236
+ {
237
+ "functionName": "clean",
238
+ "functionArgs": []
239
+ }
240
+ ]
241
+ }
242
+ ],
243
+ "code": "A101",
244
+ "description": "raise when n cycle ends",
245
+ "isSingleton": false
246
+ },
247
+ {
248
+ "toState": "s3",
249
+ "service": "freelog",
250
+ "name": "e4"
251
+ }
252
+ ]
253
+ },
254
+ "s3": {
255
+ "serviceStates": [],
256
+ "transitions": [
257
+ {
258
+ "name": "terminate"
259
+ }
260
+ ]
261
+ }
262
+ },
263
+ "info": {
264
+ "environmentVariables": [
265
+ "this.all",
266
+ "context.id"
267
+ ]
268
+ },
269
+ "warnings": [
270
+ "该事件未定义:{\"toState\":\"s2\",\"service\":\"freelog\",\"name\":\"e1\",\"condition\":{\"type\":\"case\",\"serviceState\":\"c3\"},\"actions\":[{\"caller\":{\"name\":\"c1\",\"type\":\"serviceState\"},\"functions\":[{\"functionName\":\"set\",\"functionArgs\":[\"context.id\"]}]},{\"caller\":{\"name\":\"c1\",\"type\":\"serviceState\"},\"functions\":[{\"functionName\":\"unset\",\"functionArgs\":[\"context.id\"]}]},{\"caller\":{\"name\":\"c1\",\"type\":\"serviceState\"},\"functions\":[{\"functionName\":\"set\",\"functionArgs\":[\"context.id\"]}]}]}",
271
+ "该事件未定义:{\"toState\":\"s2\",\"service\":\"freelog\",\"name\":\"e2\",\"condition\":{\"type\":\"case\",\"serviceState\":\"c4\"},\"actions\":[{\"caller\":{\"name\":\"c5\",\"type\":\"serviceState\"},\"functions\":[{\"functionName\":\"set\",\"functionArgs\":[\"context.id\"]}]}]}",
272
+ "该事件未定义:{\"toState\":\"s3\",\"service\":\"freelog\",\"name\":\"e4\"}"
273
+ ],
274
+ "warningObjects": [
275
+ {
276
+ "msg": "该事件未定义:{\"toState\":\"s2\",\"service\":\"freelog\",\"name\":\"e1\",\"condition\":{\"type\":\"case\",\"serviceState\":\"c3\"},\"actions\":[{\"caller\":{\"name\":\"c1\",\"type\":\"serviceState\"},\"functions\":[{\"functionName\":\"set\",\"functionArgs\":[\"context.id\"]}]},{\"caller\":{\"name\":\"c1\",\"type\":\"serviceState\"},\"functions\":[{\"functionName\":\"unset\",\"functionArgs\":[\"context.id\"]}]},{\"caller\":{\"name\":\"c1\",\"type\":\"serviceState\"},\"functions\":[{\"functionName\":\"set\",\"functionArgs\":[\"context.id\"]}]}]}"
277
+ },
278
+ {
279
+ "msg": "该事件未定义:{\"toState\":\"s2\",\"service\":\"freelog\",\"name\":\"e2\",\"condition\":{\"type\":\"case\",\"serviceState\":\"c4\"},\"actions\":[{\"caller\":{\"name\":\"c5\",\"type\":\"serviceState\"},\"functions\":[{\"functionName\":\"set\",\"functionArgs\":[\"context.id\"]}]}]}"
280
+ },
281
+ {
282
+ "msg": "该事件未定义:{\"toState\":\"s3\",\"service\":\"freelog\",\"name\":\"e4\"}"
283
+ }
284
+ ],
285
+ "errors": [],
286
+ "errorObjects": []
287
+ }
288
+ ```
289
+
290
+ 结构说明:
291
+
292
+ ```typescript
293
+ export interface Result {
294
+ // 目标用户
295
+ audiences: Audience[];
296
+ // 色块声明
297
+ serviceStates: ServiceState[];
298
+ // 状态机
299
+ stateMachine: StateMachine;
300
+ // 信息
301
+ info: Info;
302
+ }
303
+
304
+ // 目标用户
305
+ export interface Audience {
306
+ // 名称
307
+ name: string;
308
+ // 类型
309
+ type: string;
310
+ }
311
+
312
+ // 色块声明
313
+ export interface ServiceState {
314
+ // 色块名
315
+ name: string;
316
+ // 色块赋值调用者
317
+ caller?: Caller;
318
+ // 函数集合,链式调用
319
+ functions?: Function[];
320
+ }
321
+
322
+ // 调用者
323
+ export interface Caller {
324
+ // 名称
325
+ name: string;
326
+ // 类型 variable:变量 | serviceState:色块
327
+ type: string;
328
+ }
329
+
330
+ // 函数
331
+ export interface Function {
332
+ // 函数名
333
+ functionName: string;
334
+ // 函数参数
335
+ functionArgs: { name: "value" };
336
+ }
337
+
338
+ // 状态机
339
+ export interface StateMachine {
340
+ // 状态名
341
+ state: {
342
+ // 色块集合
343
+ serviceStates: string[];
344
+ // 事件转换集合
345
+ transitions: Transition[];
346
+ }
347
+ }
348
+
349
+ // 事件转换
350
+ export interface Transition {
351
+ // 条件
352
+ condition?: Condition;
353
+ // 事件名
354
+ name: string;
355
+ // 事件服务
356
+ service: string;
357
+ // 事件路径
358
+ path?: string;
359
+ // 转换目标状态
360
+ toState: string;
361
+ // 事件参数
362
+ args: {
363
+ // 参数名,参数值
364
+ argName: string;
365
+ };
366
+ // 事件代码
367
+ code: string;
368
+ // 事件描述
369
+ description: string;
370
+ // 是否是单例
371
+ isSingleton: boolean;
372
+ // 动作
373
+ actions?: Action[];
374
+ }
375
+
376
+ // 条件
377
+ export interface Condition {
378
+ // 类型 case
379
+ type: string;
380
+ // 色块 当类型是case时,有该属性
381
+ serviceState?: string;
382
+ }
383
+
384
+ // 动作
385
+ export interface Action {
386
+ // 调用者
387
+ caller?: Caller;
388
+ // 函数集合
389
+ functions: Function[];
390
+ }
391
+
392
+ // 信息
393
+ export interface Info {
394
+ // 组合策略中使用到的环境变量
395
+ environmentVariables: string[];
396
+ }
397
+ ```
398
+
@@ -0,0 +1,79 @@
1
+ const REGEX_ARG_ACCOUNT = "^(\\d+)|((this)\\.[a-zA-Z0-9_]+)$";
2
+
3
+ class TransitionEventArgsMatchUtil {
4
+
5
+ constructor() {
6
+ this.specificRegexMap = new Map();
7
+ this.specificRegexMap.set("resourceName", "^[a-zA-Z\u4e00-\u9fef0-9\\-_.]+/[a-zA-Z\u4e00-\u9fef0-9\\-_.]+$")
8
+ this.specificRegexMap.set("account", REGEX_ARG_ACCOUNT);
9
+ }
10
+
11
+ match(arg, argValue) {
12
+ let argName = arg["name"];
13
+ let argType = arg["type"];
14
+ let argEnum = arg["enum"];
15
+
16
+ if (argType == null || argValue == null) {
17
+ return false;
18
+ }
19
+
20
+ let result = false;
21
+ switch (argType) {
22
+ case "none":
23
+ result = true;
24
+ break;
25
+ case "decimal":
26
+ result = argValue.match("^\\d+(\\.\\d{1,2})?$");
27
+ break;
28
+ case "string":
29
+ result = argValue.match("^[a-zA-Z\u4e00-\u9fef0-9\\-_./]*$");
30
+ break;
31
+ case "timeUnit":
32
+ result = argValue.match("^second|minute|hour|cycle|day|week|month|year$");
33
+ break;
34
+ case "dateTime":
35
+ result = argValue.match("^\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}$");
36
+ break;
37
+ }
38
+
39
+ result = result && this.matchSpecific(argName, argValue);
40
+
41
+ if (result && argEnum != null) {
42
+ return argEnum.indexOf(argValue) > -1;
43
+ } else {
44
+ return result;
45
+ }
46
+ }
47
+
48
+ matchSpecific(argName, argValue) {
49
+ return !this.specificRegexMap.has(argName) || argValue.match(this.specificRegexMap.get(argName));
50
+ }
51
+
52
+ extract(arg, argValue) {
53
+ let argName = arg["name"];
54
+ let argType = arg["type"];
55
+ let argEnum = arg["enum"];
56
+
57
+ // 1:普通 2:环境变量
58
+ let symbolType = 0;
59
+ switch (argName) {
60
+ case "account":
61
+ let regExp = new RegExp(REGEX_ARG_ACCOUNT);
62
+ if (!argValue.match(regExp)) {
63
+ throw new Error("参数错误,提取参数信息失败");
64
+ }
65
+ symbolType = RegExp.$1 ? 1 : 0;
66
+ if (symbolType === 0) {
67
+ // 环境变量
68
+ symbolType = RegExp.$2 ? 2 : 0;
69
+ }
70
+ break;
71
+ }
72
+
73
+ return {
74
+ symbolType
75
+ };
76
+ }
77
+ }
78
+
79
+ exports.TransitionEventArgsMatchUtil = TransitionEventArgsMatchUtil;