@freelog/freelog-cg-collection 1.2.11 → 1.2.13

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.
@@ -0,0 +1,329 @@
1
+ import {AudienceSection, ContractDeclarationSection, PolicyInfo, StateInfo} from "../model/policy/PolicyInfo";
2
+ import {VarDeclarationInfo} from "../model/policy/VarDeclarationInfo";
3
+ import {ContractDeclarationInfo} from "../model/policy/ContractDeclarationInfo";
4
+ import {EventInfo} from "../model/policy/EventInfo";
5
+ import {AssignmentClauseInfo} from "../model/AssignmentClauseInfo";
6
+ import {FunctionCallInfo} from "../model/exp/FunctionCallInfo";
7
+ import {ExpArgsGroup} from "../model/exp-args-group/ExpArgsGroup";
8
+ import {VariableChainInfo} from "../model/exp/VariableChainInfo";
9
+ import {ExpCondition} from "../model/exp-condition/ExpCondition";
10
+ import {StringInfo} from "../model/exp/StringInfo";
11
+ import {EntityInfo} from "../model/exp/EntityInfo";
12
+ import {NumberInfo} from "../model/exp/NumberInfo";
13
+ import {ExpBooleanOpLogic} from "../model/exp-boolean/ExpBooleanOpLogic";
14
+ import {ExpBooleanParen} from "../model/exp-boolean/ExpBooleanParen";
15
+ import {ExpBooleanOpCollection} from "../model/exp-boolean/ExpBooleanOpCollection";
16
+ import {ActionFunctionCall, ActionInfo, ActionVarAssignment} from "../model/policy/ActionInfo";
17
+ import {BooleanInfo} from "../model/exp-boolean/BooleanInfo";
18
+ import {ExpOp} from "../model/exp/ExpOp";
19
+ import {ExpBooleanOpCompare} from "../model/exp-boolean/ExpBooleanOpCompare";
20
+ import {ExpParen} from "../model/exp/ExpParen";
21
+ import {ExpSigned} from "../model/exp/ExpSigned";
22
+
23
+ export class PolicyDecompiler {
24
+
25
+ static decompile(policyInfo: PolicyInfo): string {
26
+ let content = "";
27
+ content += `\n${PolicyDecompiler.decompileAudienceSection(policyInfo.audienceSection)}`;
28
+ if (policyInfo.contractDeclarationSection != null && policyInfo.contractDeclarationSection.contractDeclarations != null && policyInfo.contractDeclarationSection.contractDeclarations.length != 0) {
29
+ content += `\n${PolicyDecompiler.decompileContractDeclarationSection(policyInfo.contractDeclarationSection)}`;
30
+ }
31
+ if (policyInfo.varDeclarationList != null && policyInfo.varDeclarationList.length != 0) {
32
+ content += `\n${PolicyDecompiler.decompileVarDeclarationList(policyInfo.varDeclarationList)}`;
33
+ }
34
+ content += `\n${PolicyDecompiler.decompileStateMachine(policyInfo.stateMachine)}`;
35
+
36
+ return content;
37
+ }
38
+
39
+ static decompileAudienceSection(audienceSection: AudienceSection): string {
40
+ let content = "";
41
+ content += `FOR ${audienceSection.audiences.join(", ")}`;
42
+ if (audienceSection.conditions != null && audienceSection.conditions.length != 0) {
43
+ content += `(${PolicyDecompiler.decompileParamList(audienceSection.conditions)})`;
44
+ }
45
+ return content;
46
+ }
47
+
48
+ static decompileContractDeclarationSection(contractDeclarationSection: ContractDeclarationSection): string {
49
+ let content = "\n";
50
+ if (contractDeclarationSection.contractDeclarations != null && contractDeclarationSection.contractDeclarations.length != 0) {
51
+ content += contractDeclarationSection.contractDeclarations.map(PolicyDecompiler.decompileContractDeclaration).join("\n");
52
+ }
53
+ return content;
54
+ }
55
+
56
+ static decompileContractDeclaration(contractDeclarationInfo: ContractDeclarationInfo): string {
57
+ let content = "";
58
+ let varValueObj = contractDeclarationInfo.varValueObj;
59
+ switch (contractDeclarationInfo.type) {
60
+ case "contractDeclaration": {
61
+ content += `${varValueObj.keyword} &${varValueObj.type}(${PolicyDecompiler.decompileParamList(varValueObj.conditions)}) AS ${varValueObj.varName}`;
62
+ break;
63
+ }
64
+ }
65
+ return content;
66
+ }
67
+
68
+ static decompileVarDeclarationList(varDeclarationList: VarDeclarationInfo[]): string {
69
+ let content = "";
70
+ if (varDeclarationList != null && varDeclarationList.length != 0) {
71
+ content += "\n";
72
+ content += varDeclarationList.map(PolicyDecompiler.decompileVarDeclaration).join("\n");
73
+ }
74
+ return content;
75
+ }
76
+
77
+ static decompileVarDeclaration(varDeclaration: VarDeclarationInfo): string {
78
+ let content = "";
79
+ switch (varDeclaration.type) {
80
+ case "varGlobal":
81
+ content += `${varDeclaration.varName} = ${PolicyDecompiler.decompileAssignmentClauseInfo(varDeclaration.varValueObj)}`;
82
+ break;
83
+ case "varMacro":
84
+ content += `${varDeclaration.varName} <- ${PolicyDecompiler.decompileAssignmentClauseInfo(varDeclaration.varValueObj)}`;
85
+ break;
86
+ case "expCustom":
87
+ content += `defExpr ${varDeclaration.varName}`;
88
+ content += `(`;
89
+ if (varDeclaration.paramList != null && varDeclaration.paramList.length != 0) {
90
+ content += varDeclaration.paramList.join(", ");
91
+ }
92
+ content += `)`;
93
+ content += ` = ${PolicyDecompiler.decompileAssignmentClauseInfo(varDeclaration.varValueObj)}`;
94
+ break;
95
+ case "varInitial":
96
+ content += `let ${varDeclaration.varName} = ${PolicyDecompiler.decompileAssignmentClauseInfo(varDeclaration.varValueObj)}`;
97
+ break;
98
+ case "varAssigment":
99
+ content = `${varDeclaration.varName} = ${PolicyDecompiler.decompileAssignmentClauseInfo(varDeclaration.varValueObj)}`;
100
+ break;
101
+ }
102
+ return content;
103
+ }
104
+
105
+ static decompileStateMachine(stateMachine: StateInfo[]): string {
106
+ let content = "\n";
107
+ content += `${stateMachine.map(this.decompileStateInfo).join("\n\n")}`;
108
+ return content;
109
+ }
110
+
111
+ static decompileStateInfo(stateInfo: StateInfo): string {
112
+ let content = "";
113
+
114
+ /*
115
+ * serviceStateSection
116
+ */
117
+ content += stateInfo.stateName;
118
+ if (stateInfo.serviceStateSection.serviceState != null) {
119
+ content += ` ${stateInfo.serviceStateSection.serviceState}`
120
+ }
121
+ if (stateInfo.serviceStateSection.serviceStateCollection != null && stateInfo.serviceStateSection.serviceStateCollection.length != 0) {
122
+ content += `{`;
123
+ content += `${stateInfo.serviceStateSection.serviceStateCollection.join(", ")}`;
124
+ content += `}`;
125
+ }
126
+ if (stateInfo.stateContextArgs != null && stateInfo.stateContextArgs.length != 0) {
127
+ content += `[`;
128
+ content += `${stateInfo.stateContextArgs.join(", ")}`;
129
+ content += `]`;
130
+ }
131
+ content += `:\n`;
132
+
133
+ /*
134
+ * assignments
135
+ */
136
+ if (stateInfo.assignments != null && stateInfo.assignments.length != 0) {
137
+ content += stateInfo.assignments.map(assignment => {
138
+ return `\t${PolicyDecompiler.decompileVarDeclaration(assignment)}`;
139
+ }).join("\n");
140
+ content += `\n`;
141
+ }
142
+
143
+ /*
144
+ * events
145
+ */
146
+ content += stateInfo.events.map(eventInfo => {
147
+ return `\t${PolicyDecompiler.decompileEventInfo(eventInfo)}`;
148
+ }).join("\n");
149
+
150
+ return content;
151
+ }
152
+
153
+ static decompileEventInfo(eventInfo: EventInfo): string {
154
+ let content = "";
155
+ if (eventInfo.type == "normal") {
156
+ content += `Event.`;
157
+ if (eventInfo.eventPath != null) {
158
+ content += `${eventInfo.eventPath}.`;
159
+ }
160
+ content += `${eventInfo.eventName}(${PolicyDecompiler.decompileParamList(eventInfo.eventParamList)}) -> ${eventInfo.toState}`;
161
+ if (eventInfo.actions != null && eventInfo.actions.length != 0) {
162
+ content += `{`;
163
+ content += eventInfo.actions.map(PolicyDecompiler.decompileActionInfo).join("; ");
164
+ content += `}`;
165
+ }
166
+ } else if (eventInfo.type == "terminate") {
167
+ content += `terminate`;
168
+ }
169
+
170
+ return content;
171
+ }
172
+
173
+ static decompileActionInfo(actionInfo: ActionInfo): string {
174
+ let content = "";
175
+ if (actionInfo.type == "functionCall") {
176
+ let action: ActionFunctionCall = actionInfo as any;
177
+ content += PolicyDecompiler.decompileAssignmentClauseInfo(action.actionObj);
178
+ } else if (actionInfo.type == "varAssignment") {
179
+ let action: ActionVarAssignment = actionInfo as any;
180
+ content += `${action.actionObj.varName} = ${PolicyDecompiler.decompileAssignmentClauseInfo(action.actionObj.varValueObj)}`;
181
+ }
182
+ return content;
183
+ }
184
+
185
+ static decompileAssignmentClauseInfo(assignmentClauseInfo: AssignmentClauseInfo): string {
186
+ let content = "";
187
+
188
+ if (["expOp", "number", "string", "entity", "variableChain", "functionCall", "expParen", "expSigned"].includes(assignmentClauseInfo.expType)) {
189
+ // 表达式
190
+ content += PolicyDecompiler.decompileAssignmentClauseInfo4Exp(assignmentClauseInfo);
191
+ } else if (["expBooleanOpLogic", "expBooleanOpCollection", "expBooleanOpCompare", "expBooleanParen", "boolean"].includes(assignmentClauseInfo.expType)) {
192
+ // 布尔值
193
+ content += PolicyDecompiler.decompileAssignmentClauseInfo4ExpBoolean(assignmentClauseInfo);
194
+ } else if (assignmentClauseInfo.expType == "expCondition") {
195
+ // 数组
196
+ let info: ExpCondition = assignmentClauseInfo as any;
197
+ content += `[`;
198
+ if (info.paramList != null && info.paramList.length != 0) {
199
+ content += info.paramList.map(paramInfo => {
200
+ if (paramInfo.paramConditionObj != null) {
201
+ return `${PolicyDecompiler.decompileAssignmentClauseInfo(paramInfo.paramConditionObj)} : ${PolicyDecompiler.decompileAssignmentClauseInfo(paramInfo.paramValueObj)}`;
202
+ } else {
203
+ return `${PolicyDecompiler.decompileAssignmentClauseInfo(paramInfo.paramValueObj)}`;
204
+ }
205
+ }).join(", ");
206
+ }
207
+ content += `]`;
208
+ } else if (assignmentClauseInfo.expType == "expArgsGroup") {
209
+ // 对象
210
+ let info: ExpArgsGroup = assignmentClauseInfo as any;
211
+ content += `{`;
212
+ if (info.paramList != null && info.paramList.length != 0) {
213
+ content += info.paramList.map(paramInfo => {
214
+ return `"${paramInfo.paramName}":${PolicyDecompiler.decompileAssignmentClauseInfo(paramInfo.paramValueObj)}`;
215
+ }).join(", ");
216
+ }
217
+ content += `}`;
218
+ }
219
+
220
+ return content;
221
+ }
222
+
223
+ private static decompileAssignmentClauseInfo4Exp(assignmentClauseInfo: AssignmentClauseInfo): string {
224
+ let content = "";
225
+ switch (assignmentClauseInfo.expType) {
226
+ // 运算
227
+ case "expOp": {
228
+ let info: ExpOp = assignmentClauseInfo as any;
229
+ content += `${PolicyDecompiler.decompileAssignmentClauseInfo(info.var1)} ${info.op} ${PolicyDecompiler.decompileAssignmentClauseInfo(info.var2)}`;
230
+ break;
231
+ }
232
+ // 数字
233
+ case "number": {
234
+ let info: NumberInfo = assignmentClauseInfo as any;
235
+ content += `${info.value}`;
236
+ break;
237
+ }
238
+ // 字符串
239
+ case "string": {
240
+ let info: StringInfo = assignmentClauseInfo as any;
241
+ content += `"${info.value}"`;
242
+ break;
243
+ }
244
+ // 实体
245
+ case "entity": {
246
+ let info: EntityInfo = assignmentClauseInfo as any;
247
+ content += `${info.entityType}.<${info.entityName}>`;
248
+ break;
249
+ }
250
+ // 变量
251
+ case "variableChain": {
252
+ let info: VariableChainInfo = assignmentClauseInfo as any;
253
+ content += info.content;
254
+ break;
255
+ }
256
+ // 函数
257
+ case "functionCall": {
258
+ let info: FunctionCallInfo = assignmentClauseInfo as any;
259
+ if (info.varChain != null) {
260
+ content += `${info.varChain}.`;
261
+ }
262
+ content += `${info.funcName}(${PolicyDecompiler.decompileParamList(info.paramList)})`;
263
+ break;
264
+ }// 瓜括号
265
+ case "expParen": {
266
+ let info: ExpParen = assignmentClauseInfo as any;
267
+ content += `(${PolicyDecompiler.decompileAssignmentClauseInfo(info.var1)})`;
268
+ break;
269
+ }
270
+ // 符号
271
+ case "expSigned": {
272
+ let info: ExpSigned = assignmentClauseInfo as any;
273
+ content += `${info.symbol}${PolicyDecompiler.decompileAssignmentClauseInfo(info.var1)}`;
274
+ break;
275
+ }
276
+ }
277
+ return content;
278
+ }
279
+
280
+ private static decompileAssignmentClauseInfo4ExpBoolean(assignmentClauseInfo: AssignmentClauseInfo): string {
281
+ let content = "";
282
+ switch (assignmentClauseInfo.expType) {
283
+ case "expBooleanOpLogic": {
284
+ let info: ExpBooleanOpLogic = assignmentClauseInfo as any;
285
+ if (info.var2 != null) {
286
+ content += `${PolicyDecompiler.decompileAssignmentClauseInfo4ExpBoolean(info.var1)} ${info.op} ${PolicyDecompiler.decompileAssignmentClauseInfo4ExpBoolean(info.var2)}`;
287
+ } else {
288
+ content += `${info.op} ${PolicyDecompiler.decompileAssignmentClauseInfo4ExpBoolean(info.var1)}`;
289
+ }
290
+ break;
291
+ }
292
+ case "expBooleanOpCollection": {
293
+ let info: ExpBooleanOpCollection = assignmentClauseInfo as any;
294
+ content += `${PolicyDecompiler.decompileAssignmentClauseInfo(info.var1)} ${info.op} ${PolicyDecompiler.decompileAssignmentClauseInfo(info.var2)}`;
295
+ break;
296
+ }
297
+ case "expBooleanOpCompare": {
298
+ let info: ExpBooleanOpCompare = assignmentClauseInfo as any;
299
+ content += `${PolicyDecompiler.decompileAssignmentClauseInfo(info.var1)} ${info.op} ${PolicyDecompiler.decompileAssignmentClauseInfo(info.var2)}`;
300
+ break;
301
+ }
302
+ case "expBooleanParen": {
303
+ let info: ExpBooleanParen = assignmentClauseInfo as any;
304
+ content += `(${this.decompileAssignmentClauseInfo4ExpBoolean(info.var1)})`;
305
+ break;
306
+ }
307
+ case "boolean": {
308
+ let info: BooleanInfo = assignmentClauseInfo as any;
309
+ content += `${info.value}`;
310
+ break;
311
+ }
312
+ }
313
+ return content;
314
+ }
315
+
316
+ static decompileParamList(paramList: { paramName?: string, paramValue: string, paramValueObj: any }[]): string {
317
+ let content = "";
318
+ if (paramList != null && paramList.length != 0) {
319
+ content += paramList.map(paramInfo => {
320
+ if (paramInfo.paramName != null) {
321
+ return `${paramInfo.paramName} = ${PolicyDecompiler.decompileAssignmentClauseInfo(paramInfo.paramValueObj)}`;
322
+ } else {
323
+ return `${PolicyDecompiler.decompileAssignmentClauseInfo(paramInfo.paramValueObj)}`;
324
+ }
325
+ }).join(", ");
326
+ }
327
+ return content;
328
+ }
329
+ }
@@ -0,0 +1,81 @@
1
+ import * as fs from "fs";
2
+
3
+ export class PolicyMapper {
4
+
5
+ static compileResult4Old2New(compileResult): any {
6
+ let eventConfigs = JSON.parse(fs.readFileSync("./resources/context_config/event_configs.json", "utf-8"));
7
+ let eventNameMap = new Map([["TransactionEvent", "Pay"], ["RelativeTimeEvent", "Relative"], ["TimeEvent", "Schedule"]]);
8
+
9
+ let compileResultNew = {
10
+ audienceSection: {
11
+ conditions: [],
12
+ audiences: compileResult["audiences"].map(a => {
13
+ return a["name"].toUpperCase();
14
+ })
15
+ },
16
+ contractDeclarationSection: {contractDeclarations: []},
17
+ varDeclarationList: [],
18
+ stateMachine: []
19
+ };
20
+ for (let stateData of Object.entries(compileResult["states"])) {
21
+ let stateInfo = {stateName: stateData[0], serviceStateSection: {}, events: []};
22
+ if (stateInfo.stateName == "initial") {
23
+ stateInfo.stateName = "Initial";
24
+ }
25
+ if (stateData[1]["serviceStates"].includes("active")) {
26
+ stateInfo.serviceStateSection["serviceState"] = "Permit";
27
+ }
28
+ if (stateData[1]["transitions"].length != 0) {
29
+ for (let transitionInfo of stateData[1]["transitions"]) {
30
+ let eventConfig = eventConfigs.find(ec => ec["name"] == eventNameMap.get(transitionInfo["name"]));
31
+ if (eventConfig == null) {
32
+ continue;
33
+ }
34
+ let eventInfo = {
35
+ type: "normal",
36
+ eventName: eventConfig["name"],
37
+ eventPath: eventConfig["path"],
38
+ eventParamList: [],
39
+ toState: transitionInfo["toState"]
40
+ };
41
+
42
+ // 参数处理
43
+ for (let paramConfig of eventConfig["paramList"]) {
44
+ let paramValue = transitionInfo["args"][paramConfig["name"]];
45
+ if (paramValue != null) {
46
+ switch (paramConfig["type"]) {
47
+ case "number":
48
+ eventInfo.eventParamList.push({
49
+ // paramName: paramConfig["name"],
50
+ paramValueObj: {expType: "number", value: Number(paramValue)}
51
+ });
52
+ break;
53
+ case "string":
54
+ eventInfo.eventParamList.push({
55
+ paramValueObj: {expType: "string", value: paramValue}
56
+ });
57
+ break;
58
+ case "boolean":
59
+ eventInfo.eventParamList.push({
60
+ paramValueObj: {expType: "boolean", value: paramValue == "true"}
61
+ });
62
+ break;
63
+ }
64
+ }
65
+ }
66
+ // 特殊事件参数处理
67
+ if (eventInfo.eventName == "Pay") {
68
+ eventInfo.eventParamList.push({paramValueObj: {expType: "number", value: 1}});
69
+ }
70
+
71
+ stateInfo.events.push(eventInfo);
72
+ }
73
+ } else {
74
+ stateInfo.events.push({type: "terminate", eventName: "terminate"});
75
+ }
76
+ compileResultNew.stateMachine.push(stateInfo);
77
+ }
78
+
79
+ return compileResultNew;
80
+ }
81
+ }
@@ -1,3 +1,4 @@
1
+ import * as fs from "fs";
1
2
  const lodash = require('lodash');
2
3
 
3
4
  describe("test/library/CommonTest.test.ts", () => {
@@ -29,8 +30,15 @@ describe("test/library/CommonTest.test.ts", () => {
29
30
  console.log(arr.filter(a => a != 1));
30
31
  });
31
32
 
33
+ it("encodeURIComponent", async () => {
34
+ // 普通 FOR%20PUBLIC%0D%0A%0D%0AInitial%20Permit%3A%0D%0A%20%20%20%20terminate
35
+ // 集合 FOR%20PUBLIC%0D%0A%0D%0AInitial%20Full%3A%0D%0A%20%20%20%20terminate
36
+ let s = fs.readFileSync("./resources/zhaojn.sc", "utf-8");
37
+ console.log(encodeURIComponent(s));
38
+ });
39
+
32
40
  it("decodeURIComponent", async () => {
33
- let s = "mongodb%3A%2F%2Fcontract_service%3AQzA4Qzg3QTA3NDRCQTA0NDU1RUQxMjI3MTA4ODQ1MTk%3D%40dds-wz9ff825617df8e41606-pub.mongodb.rds.aliyuncs.com%3A3717%2Cdds-wz9ff825617df8e42773-pub.mongodb.rds.aliyuncs.com%3A3717%2Fcontracts_dev%3FreplicaSet%3Dmgset-82892902";
41
+ let s = "for%20public%0A%0Ainitial%5Bactive%5D%3A%0A%20%20terminate";
34
42
  console.log(decodeURIComponent(s));
35
43
  });
36
44
 
@@ -0,0 +1,9 @@
1
+ import * as fs from "fs";
2
+ import {PolicyDecompiler} from "../../src/tools/PolicyDecompiler";
3
+
4
+ describe("test/tools/PolicyDecompilerTest.test.ts", () => {
5
+ it("decompile", async () => {
6
+ let source = JSON.parse(fs.readFileSync("./resources/zhaojn.json", "utf-8"));
7
+ console.log(PolicyDecompiler.decompile(source));
8
+ });
9
+ });
@@ -0,0 +1,15 @@
1
+ import * as fs from "fs";
2
+ import {PolicyMapper} from "../../src/tools/PolicyMapper";
3
+ import {PolicyDecompiler} from "../../src/tools/PolicyDecompiler";
4
+
5
+ describe("test/tools/PolicyMapperTest.test.ts", () => {
6
+ it("compileResult4Old2New", async () => {
7
+ let source = JSON.parse(fs.readFileSync("./resources/zhaojn2.json", "utf-8"));
8
+
9
+ let compileResultNew = PolicyMapper.compileResult4Old2New(source["state_machine"]);
10
+
11
+ fs.writeFileSync("./resources/zhaojn3.json", JSON.stringify(compileResultNew, null, 4));
12
+
13
+ console.log(PolicyDecompiler.decompile(compileResultNew));
14
+ });
15
+ });