@freelog/freelog-cg-collection 1.1.0 → 1.1.1

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@freelog/freelog-cg-collection",
3
- "version": "1.1.0",
3
+ "version": "1.1.1",
4
4
  "main": "index.js",
5
5
  "dependencies": {
6
6
  "antlr4": "4.8.0",
@@ -9,7 +9,7 @@ export class ExpArgsGroupCalculator implements Calculator,ExpHelperAware {
9
9
 
10
10
  private expHelper: ExpHelper;
11
11
 
12
- getExpValue(varObj: AssignmentClauseInfo, varContextCapable: VarContextCapable<any>): any {
12
+ async getExpValue(varObj: AssignmentClauseInfo, varContextCapable: VarContextCapable<any>): Promise<any> {
13
13
  if (varObj.expType == "expArgsGroup") {
14
14
  return this.generateExpArgsGroupValue(varObj as any, varContextCapable);
15
15
  }
@@ -11,10 +11,10 @@ export class ExpBooleanCalculator implements Calculator, ExpHelperAware {
11
11
 
12
12
  private expHelper: ExpHelper;
13
13
 
14
- getExpValue(varObj: AssignmentClauseInfo, varContextCapable: VarContextCapable<any>): boolean {
14
+ async getExpValue(varObj: AssignmentClauseInfo, varContextCapable: VarContextCapable<any>): Promise<boolean> {
15
15
  if (varObj.expType == "expBooleanOpLogic") {
16
16
  return this.calculateExpBooleanOpLogic(varObj as any, varContextCapable);
17
- }
17
+ }2
18
18
  if (varObj.expType == "expBooleanOpCollection") {
19
19
  return this.calculateExpBooleanOpCollection(varObj as any, varContextCapable);
20
20
  }
@@ -31,7 +31,7 @@ export class ExpBooleanCalculator implements Calculator, ExpHelperAware {
31
31
  throw new Error("getExpValue fail [ExpBooleanCalculator]...");
32
32
  }
33
33
 
34
- private calculateExpBooleanOpLogic(expBooleanOpLogic: ExpBooleanOpLogic, varContextCapable: VarContextCapable<any>): boolean {
34
+ private async calculateExpBooleanOpLogic(expBooleanOpLogic: ExpBooleanOpLogic, varContextCapable: VarContextCapable<any>): Promise<boolean> {
35
35
  let var1 = this.expHelper.getExpValue(expBooleanOpLogic.var1, varContextCapable);
36
36
  if (expBooleanOpLogic.op == "not") {
37
37
  return !var1;
@@ -48,9 +48,9 @@ export class ExpBooleanCalculator implements Calculator, ExpHelperAware {
48
48
  return false;
49
49
  }
50
50
 
51
- private calculateExpBooleanOpCollection(expBooleanOpCollection: ExpBooleanOpCollection, varContextCapable: VarContextCapable<any>): boolean {
52
- let var1 = this.expHelper.getExpValue(expBooleanOpCollection.var1, varContextCapable);
53
- let var2 = this.expHelper.getExpValue(expBooleanOpCollection.var2, varContextCapable);
51
+ private async calculateExpBooleanOpCollection(expBooleanOpCollection: ExpBooleanOpCollection, varContextCapable: VarContextCapable<any>): Promise<boolean> {
52
+ let var1 = await this.expHelper.getExpValue(expBooleanOpCollection.var1, varContextCapable);
53
+ let var2 = await this.expHelper.getExpValue(expBooleanOpCollection.var2, varContextCapable);
54
54
  if (expBooleanOpCollection.op == "in") {
55
55
  return var2.indexOf(var1) != -1;
56
56
  }
@@ -14,7 +14,7 @@ export class ExpCalculator implements Calculator, FunctionHelperAware, ExpHelper
14
14
 
15
15
  private expHelper: ExpHelper;
16
16
 
17
- getExpValue(varObj: AssignmentClauseInfo, varContextCapable: VarContextCapable<any>): any {
17
+ async getExpValue(varObj: AssignmentClauseInfo, varContextCapable: VarContextCapable<any>): Promise<any> {
18
18
  if (varObj.expType == "expOp") {
19
19
  return this.calculateExpOp(varObj as any, varContextCapable);
20
20
  }
@@ -51,7 +51,7 @@ export class ExpCalculator implements Calculator, FunctionHelperAware, ExpHelper
51
51
  return eval(expStr);
52
52
  }
53
53
 
54
- private calculateExpSigned(expSigned: ExpSigned, varContextCapable: VarContextCapable<any>): number {
54
+ private async calculateExpSigned(expSigned: ExpSigned, varContextCapable: VarContextCapable<any>): Promise<number> {
55
55
  let var1 = this.expHelper.getExpValue(expSigned.var1, varContextCapable);
56
56
  if (expSigned.symbol == "-") {
57
57
  return -var1;
@@ -9,7 +9,7 @@ export class ExpConditionCalculator implements Calculator, ExpHelperAware {
9
9
 
10
10
  private expHelper: ExpHelper;
11
11
 
12
- getExpValue(varObj: AssignmentClauseInfo, varContextCapable: VarContextCapable<any>): any {
12
+ async getExpValue(varObj: AssignmentClauseInfo, varContextCapable: VarContextCapable<any>): Promise<any> {
13
13
  if (varObj.expType == "expCondition") {
14
14
  return this.generateExpConditionValue(varObj as any, varContextCapable);
15
15
  }
@@ -9,7 +9,7 @@ export abstract class DefaultFunctionExecutor implements FunctionInvoke {
9
9
  this.outsideApiService = outsideApiService;
10
10
  }
11
11
 
12
- invoke(functionName: string, args: any[]): any {
12
+ async invoke(functionName: string, args: any[]): Promise<any> {
13
13
  throw new Error("这是个空实现,不能被调用!");
14
14
  }
15
15
 
@@ -9,23 +9,25 @@ export class DynamicFunctionExecutor extends DefaultFunctionExecutor {
9
9
  return this.executorsMap.has(functionName) || this.getFunctionCodeMap().has(functionName);
10
10
  }
11
11
 
12
- invoke(functionName: string, args: any[]): any {
12
+ async invoke(functionName: string, args: any[]): Promise<any> {
13
13
  if (this.executorsMap.has(functionName)) {
14
14
  return this.executorsMap.get(functionName).invoke(functionName, args);
15
15
  }
16
16
  if (this.getFunctionCodeMap().has(functionName)) {
17
17
  let functionCode = this.getFunctionCodeMap().get(functionName);
18
- let argsStr = "";
19
- for (let i = 0; i < args.length; i++) {
20
- if (i == 0) {
21
- argsStr = JSON.stringify(args[i]);
22
- } else {
23
- argsStr = argsStr + "," + JSON.stringify(args[i]);
24
- }
25
- }
26
- let expStr = `this.${functionCode}(${argsStr})`;
27
-
28
- return eval(expStr);
18
+ // let argsStr = "";
19
+ // for (let i = 0; i < args.length; i++) {
20
+ // if (i == 0) {
21
+ // argsStr = JSON.stringify(args[i]);
22
+ // } else {
23
+ // argsStr = argsStr + "," + JSON.stringify(args[i]);
24
+ // }
25
+ // }
26
+ // let expStr = `this.${functionCode}(${argsStr})`;
27
+
28
+ return Reflect.apply(this[functionCode], this, args);
29
+
30
+ // return eval(expStr);
29
31
  }
30
32
 
31
33
  throw new Error("该函数未实现");
@@ -13,7 +13,7 @@ export class QueryFunctionExecutor extends DynamicFunctionExecutor {
13
13
  ]);
14
14
  }
15
15
 
16
- F_001(args: { subject: any, interval?: "PAST_MONTH", startTime?: string, limitTime?: string }): number {
16
+ async F_001(args: { subject: any, interval?: "PAST_MONTH", startTime?: string, limitTime?: string }): Promise<number> {
17
17
  return this.outsideApiService.resourceCountOfUpdates(args);
18
18
  // throw new Error("该函数未实现【Query.ResourceService.CountOfUpdates】");
19
19
  }
@@ -21,7 +21,7 @@ export class ExpHelper implements Calculator, FunctionHelperAware {
21
21
  this.calculatorFactory = factoryBuilder.build();
22
22
  }
23
23
 
24
- getExpValue(varObj: AssignmentClauseInfo, varContextCapable: VarContextCapable<any>): any {
24
+ async getExpValue(varObj: AssignmentClauseInfo, varContextCapable: VarContextCapable<any>): Promise<any> {
25
25
  console.debug(`getExpValue 【ExpHelper】${JSON.stringify(varObj)}`);
26
26
 
27
27
  let calculator = this.calculatorFactory.getCalculatorByExpType(varObj.expType);
@@ -43,7 +43,7 @@ export class FunctionHelper implements FunctionInvoke, ExpHelperAware {
43
43
  });
44
44
  }
45
45
 
46
- getFunctionValue(funcInfo: FunctionCallInfo, varContextCapable: VarContextCapable<any>): any {
46
+ async getFunctionValue(funcInfo: FunctionCallInfo, varContextCapable: VarContextCapable<any>): Promise<any> {
47
47
  let args = funcInfo.paramList.map(paramElem => {
48
48
  return this.expHelper.getExpValue(paramElem.paramValueObj, varContextCapable);
49
49
  });
@@ -59,7 +59,7 @@ export class FunctionHelper implements FunctionInvoke, ExpHelperAware {
59
59
  throw new Error("这是个空实现,不能被调用!");
60
60
  }
61
61
 
62
- invoke(functionName: string, args: any[]): any {
62
+ async invoke(functionName: string, args: any[]): Promise<any> {
63
63
  console.debug(`invoke 【FunctionHelper】functionName = ${functionName} args = ${args}`);
64
64
 
65
65
  let expCustom = this.getExpCustom(functionName);
@@ -46,7 +46,7 @@ export class PolicyHelper implements VarContextCapable<any>, Calculator, Functio
46
46
  }
47
47
 
48
48
  // 在合约签约的时候,可签约条件和全局申明的变量是已经计算完毕的,在这里做这件事
49
- init(varContextCapable: VarContextCapable<any>, checkConditions: boolean) {
49
+ async init(varContextCapable: VarContextCapable<any>, checkConditions: boolean) {
50
50
  this.audienceInfo["audiences"] = this.policyInfo.audienceSection.audiences;
51
51
 
52
52
  /*
@@ -112,7 +112,7 @@ export class PolicyHelper implements VarContextCapable<any>, Calculator, Functio
112
112
  if (this.audienceInfo["condition"]["echoSubject"] != null && this.audienceInfo["condition"]["labels"] != null) {
113
113
  let echoSubject = this.audienceInfo["condition"]["echoSubject"];
114
114
  let contractLicensee = this.getVarContext("contract.licensee");
115
- let labels = this.invoke("Query.EchoService.Labels", [echoSubject, contractLicensee]);
115
+ let labels = await this.invoke("Query.EchoService.Labels", [echoSubject, contractLicensee]);
116
116
  if (labels.filter(label => this.audienceInfo["condition"]["labels"].indexOf(label) != -1).length != this.audienceInfo["condition"]["labels"].length) {
117
117
  throw new Error(`【echoSubject】【labels】验证失败!`);
118
118
  }
@@ -134,7 +134,7 @@ export class PolicyHelper implements VarContextCapable<any>, Calculator, Functio
134
134
  for (let varDeclaration of varDeclarationList) {
135
135
  if (varDeclaration.type == "varGlobal") {
136
136
  if (this.getVarGlobal(varDeclaration.varName) == null) {
137
- let value = this.getExpValue(varDeclaration.varValueObj, varContextCapable);
137
+ let value = await this.getExpValue(varDeclaration.varValueObj, varContextCapable);
138
138
  this.setVarGlobal(varDeclaration.varName, value);
139
139
  }
140
140
  }
@@ -244,7 +244,7 @@ export class PolicyHelper implements VarContextCapable<any>, Calculator, Functio
244
244
  return this.contractDeclarationHelper.execute(varObj["keyword"], varObj);
245
245
  }
246
246
 
247
- getExpValue(varObj: AssignmentClauseInfo, varContextCapable: VarContextCapable<any>): any {
247
+ async getExpValue(varObj: AssignmentClauseInfo, varContextCapable: VarContextCapable<any>): Promise<any> {
248
248
  return this.expHelper.getExpValue(varObj, varContextCapable);
249
249
  }
250
250
 
@@ -252,7 +252,7 @@ export class PolicyHelper implements VarContextCapable<any>, Calculator, Functio
252
252
  throw new Error("这是个空实现,不能被调用!");
253
253
  }
254
254
 
255
- invoke(functionName: string, args: any[]): any {
255
+ async invoke(functionName: string, args: any[]): Promise<any> {
256
256
  return this.functionHelper.invoke(functionName, args);
257
257
  }
258
258
 
@@ -2,5 +2,5 @@ import {AssignmentClauseInfo} from "../model/AssignmentClauseInfo";
2
2
  import {VarContextCapable} from "./VarContextCapable";
3
3
 
4
4
  export interface Calculator {
5
- getExpValue(varObj: AssignmentClauseInfo, varContextCapable: VarContextCapable<any>): any;
5
+ getExpValue(varObj: AssignmentClauseInfo, varContextCapable: VarContextCapable<any>): Promise<any>;
6
6
  }
@@ -2,5 +2,5 @@ export interface FunctionInvoke {
2
2
 
3
3
  invokeEnable(functionName: string): boolean;
4
4
 
5
- invoke(functionName: string, args: any[]): any;
5
+ invoke(functionName: string, args: any[]): Promise<any>;
6
6
  }
@@ -1,6 +1,6 @@
1
1
  export interface GetPostInvoke {
2
2
 
3
- doGet(url: string, payloadData: any): any;
3
+ doGet(url: string, payloadData: any): Promise<any>;
4
4
 
5
- doPost(url: string, payloadData: any): any;
5
+ doPost(url: string, payloadData: any): Promise<any>;
6
6
  }
@@ -6,12 +6,12 @@ export class OutsideApiService {
6
6
 
7
7
  private apiAddressMap: Map<string, string> = new Map<string, string>();
8
8
 
9
- resourceCountOfUpdates(args: {
9
+ async resourceCountOfUpdates(args: {
10
10
  subject: any,
11
11
  interval?: "PAST_MONTH",
12
12
  startTime?: string,
13
13
  limitTime?: string
14
- }): number {
14
+ }): Promise<number> {
15
15
  return this.getPostInvoke.doPost(this.apiAddressMap.get("resourceCountOfUpdates"), args);
16
16
  }
17
17
 
@@ -2,7 +2,7 @@ import {DefaultFunctionExecutor} from "../../../src/function-executor/DefaultFun
2
2
 
3
3
  export class ApiContractQuerySignCount extends DefaultFunctionExecutor {
4
4
 
5
- invoke(functionName: string, args: any[]): number {
5
+ async invoke(functionName: string, args: any[]): Promise<number> {
6
6
  console.debug(`invoke 【ApiContractQuerySignCount】functionName = ${functionName} args = ${args}`);
7
7
 
8
8
  return -1;
@@ -2,7 +2,7 @@ import {DefaultFunctionExecutor} from "../../../src/function-executor/DefaultFun
2
2
 
3
3
  export class FunAddElements extends DefaultFunctionExecutor {
4
4
 
5
- invoke(functionName: string, args: any[]): any {
5
+ async invoke(functionName: string, args: any[]): Promise<any> {
6
6
  console.debug(`invoke 【FunAddElements】functionName = ${functionName} args = ${args}`);
7
7
 
8
8
  args[0].push(...args[1]);
@@ -2,7 +2,7 @@ import {DefaultFunctionExecutor} from "../../../src/function-executor/DefaultFun
2
2
 
3
3
  export class QueryEchoServiceLabels extends DefaultFunctionExecutor {
4
4
 
5
- invoke(functionName: string, args: any[]): string[] {
5
+ async invoke(functionName: string, args: any[]): Promise<string[]> {
6
6
  console.debug(`invoke 【QueryEchoServiceLabels】functionName = ${functionName} args = ${args}`);
7
7
 
8
8
  return ["vip1"];
@@ -12,9 +12,9 @@ describe("test/helper/PolicyHelperTest.test.ts", () => {
12
12
 
13
13
  let source = JSON.parse(fs.readFileSync("./resources/zhaojn.json", "utf-8"))
14
14
 
15
- let functionHelper = new FunctionHelper();
16
15
  let outsideApiService = new OutsideApiService();
17
16
  outsideApiService.setGetPostInvoke(new CustomGetPostHelper());
17
+ let functionHelper = new FunctionHelper();
18
18
  functionHelper.setOutsideApiService(outsideApiService);
19
19
  let policyHelper = new PolicyHelper(source, functionHelper);
20
20
  policyHelper.addContractDeclarationExecutor("DEMAND", new USEContractDeclaration());