-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathAlgorithmEngineHelper.js
More file actions
299 lines (277 loc) · 10.8 KB
/
AlgorithmEngineHelper.js
File metadata and controls
299 lines (277 loc) · 10.8 KB
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
import { CharUtil } from './Internals/Visitors/CharUtil.js';
import { AntlrCharStream } from './Internals/Visitors/AntlrCharStream.js';
import { AntlrErrorTextWriter } from './Internals/Visitors/AntlrErrorTextWriter.js';
import { DiyNameVisitor } from './Internals/Visitors/DiyNameVisitor.js';
import { MathFunctionVisitor } from './Internals/Visitors/MathFunctionVisitor.js';
import { MathSplitVisitor } from './Internals/Visitors/MathSplitVisitor.js';
import { MathSplitVisitor2 } from './Internals/Visitors/MathSplitVisitor2.js';
import { Function_AND } from './Internals/Functions/Operator/Function_AND.js';
import { Function_OR } from './Internals/Functions/Operator/Function_OR.js';
import { Function_Add } from './Internals/Functions/Operator/Function_Add.js';
import { Function_Sub } from './Internals/Functions/Operator/Function_Sub.js';
import { Function_Mul } from './Internals/Functions/Operator/Function_Mul.js';
import { Function_Div } from './Internals/Functions/Operator/Function_Div.js';
import { Function_Mod } from './Internals/Functions/Operator/Function_Mod.js';
import { Function_Connect } from './Internals/Functions/Operator/Function_Connect.js';
import { DistanceConverter } from './UnitConversion/DistanceConverter.js';
import { MassConverter } from './UnitConversion/MassConverter.js';
import { AreaConverter } from './UnitConversion/AreaConverter.js';
import { VolumeConverter } from './UnitConversion/VolumeConverter.js';
import { ConditionTreeType } from './Enums/ConditionTreeType.js';
import { CalculateTreeType } from './Enums/CalculateTreeType.js';
// 导入ANTLR生成的文件
import mathLexer from './math/mathLexer.js';
import CommonTokenStream from './antlr4/CommonTokenStream.js';
import mathParser from './math/mathParser.js';
/**
* 算法引擎助手
*/
export class AlgorithmEngineHelper {
static unitRegex = null;
/**
* 是不是参数
*/
static IsParameter(parameter) {
if (!parameter || parameter.trim() === '') { return false; }
try {
let diy = this.GetDiyNames(parameter);
if (diy.Functions && diy.Functions.length > 0) { return false; }
if (diy.Parameters && diy.Parameters.length === 1) {
let p = diy.Parameters[0];
return p.Name === parameter;
}
} catch (ex) { }
return false;
}
/**
* 获取 DIY 名称
*/
static GetDiyNames(exp) {
if (!exp || exp.trim() === '') {
throw new Error("Parameter exp invalid !");
}
let antlrErrorTextWriter = new AntlrErrorTextWriter();
let stream =new AntlrCharStream(exp);
let lexer = new mathLexer(stream, null, antlrErrorTextWriter);
lexer.removeErrorListeners();
lexer.addErrorListener(antlrErrorTextWriter);
let tokens = new CommonTokenStream(lexer);
let parser = new mathParser(tokens, null, antlrErrorTextWriter);
parser.removeErrorListeners();
parser.addErrorListener(antlrErrorTextWriter);
let context = parser.prog();
if (antlrErrorTextWriter.IsError) {
throw new Error(antlrErrorTextWriter.ErrorMsg);
}
let visitor = new DiyNameVisitor();
visitor.visit(context);
return visitor.diy;
}
/**
* 单位转换
*/
static UnitConversion(src, oldSrcUnit, oldTarUnit, name = null) {
if (!oldSrcUnit || !oldTarUnit) { return src; }
if (!this.unitRegex) {
this.unitRegex = /[\s \(\)()\[\]<>]/g;
}
oldSrcUnit = oldSrcUnit.replace(this.unitRegex, "");
if (oldSrcUnit === oldTarUnit) { return src; }
if (DistanceConverter.exists(oldSrcUnit, oldTarUnit)) {
let c = new DistanceConverter(oldSrcUnit, oldTarUnit);
return c.leftToRight(src);
}
if (MassConverter.exists(oldSrcUnit, oldTarUnit)) {
let c = new MassConverter(oldSrcUnit, oldTarUnit);
return c.leftToRight(src);
}
if (AreaConverter.exists(oldSrcUnit, oldTarUnit)) {
let c = new AreaConverter(oldSrcUnit, oldTarUnit);
return c.leftToRight(src);
}
if (VolumeConverter.exists(oldSrcUnit, oldTarUnit)) {
let c = new VolumeConverter(oldSrcUnit, oldTarUnit);
return c.leftToRight(src);
}
if (!name) {
throw new Error(`The input item has different units and cannot be converted from [${oldSrcUnit}] to [${oldTarUnit}]`);
}
throw new Error(`The input item [${name}] has different units and cannot be converted from [${oldSrcUnit}] to [${oldTarUnit}]`);
}
/**
* 编译公式
*/
static ParseFormula(exp,errorListener) {
if (!exp || exp.trim() === '') {
throw new Error("Parameter exp invalid !");
}
let antlrErrorTextWriter = new AntlrErrorTextWriter();
let stream =new AntlrCharStream(exp);
let lexer = new mathLexer(stream, null, antlrErrorTextWriter);
lexer.removeErrorListeners();
lexer.addErrorListener(antlrErrorTextWriter);
let tokens = new CommonTokenStream(lexer);
let parser = new mathParser(tokens, null, antlrErrorTextWriter);
parser.removeErrorListeners();
parser.addErrorListener(antlrErrorTextWriter);
if (errorListener) {
parser.addErrorListener(errorListener);
let context = parser.prog();
let visitor = new MathFunctionVisitor();
return visitor.visit(context);
}
let context = parser.prog();
if (antlrErrorTextWriter.IsError) {
throw new Error(antlrErrorTextWriter.ErrorMsg);
}
let visitor = new MathFunctionVisitor();
return visitor.visit(context);
}
/**
* 检查公式是否正确
*/
static CheckFormula(exp) {
if (!exp || exp.trim() === '') { return false; }
let antlrErrorTextWriter = new AntlrErrorTextWriter();
let stream =new AntlrCharStream(exp);
let lexer = new mathLexer(stream, null, antlrErrorTextWriter);
lexer.removeErrorListeners();
lexer.addErrorListener(antlrErrorTextWriter);
let tokens = new CommonTokenStream(lexer);
let parser = new mathParser(tokens, null, antlrErrorTextWriter);
parser.removeErrorListeners();
parser.addErrorListener(antlrErrorTextWriter);
let context = parser.prog();
if (antlrErrorTextWriter.IsError) {
return false;
}
return true;
}
/**
* 解析条件
*/
static ParseCondition(condition) {
let tree = {
Type: null,
ErrorMessage: null
};
if (!condition || condition.trim() === '') {
tree.Type = ConditionTreeType.Error;
tree.ErrorMessage = "condition is null";
return tree;
}
try {
let antlrErrorTextWriter = new AntlrErrorTextWriter();
let stream =new AntlrCharStream(condition);
let lexer = new mathLexer(stream, null, antlrErrorTextWriter);
lexer.removeErrorListeners();
lexer.addErrorListener(antlrErrorTextWriter);
let tokens = new CommonTokenStream(lexer);
let parser = new mathParser(tokens, null, antlrErrorTextWriter);
parser.removeErrorListeners();
parser.addErrorListener(antlrErrorTextWriter);
let context = parser.prog();
if (antlrErrorTextWriter.IsError) {
tree.Type = ConditionTreeType.Error;
tree.ErrorMessage = antlrErrorTextWriter.ErrorMsg;
return tree;
}
let visitor = new MathSplitVisitor();
return visitor.visit(context);
} catch (ex) {
tree.Type = ConditionTreeType.Error;
tree.ErrorMessage = ex.message;
}
return tree;
}
/**
* Creates a logical AND function that combines two specified functions.
*/
static Condition_And(left, right) {
return new Function_AND(left, right);
}
/**
* Creates a logical OR function that combines two specified functions.
*/
static Condition_Or(left, right) {
return new Function_OR(left, right);
}
/**
* 解析计算表达式
*/
static ParseCalculate(exp) {
let tree = {
Type: null,
ErrorMessage: null
};
if (!exp || exp.trim() === '') {
tree.Type = CalculateTreeType.Error;
tree.ErrorMessage = "exp is null";
return tree;
}
try {
let antlrErrorTextWriter = new AntlrErrorTextWriter();
let stream =new AntlrCharStream(exp);
let lexer = new mathLexer(stream, null, antlrErrorTextWriter);
lexer.removeErrorListeners();
lexer.addErrorListener(antlrErrorTextWriter);
let tokens = new CommonTokenStream(lexer);
let parser = new mathParser(tokens, null, antlrErrorTextWriter);
parser.removeErrorListeners();
parser.addErrorListener(antlrErrorTextWriter);
let context = parser.prog();
if (antlrErrorTextWriter.IsError) {
tree.Type = CalculateTreeType.Error;
tree.ErrorMessage = antlrErrorTextWriter.ErrorMsg;
return tree;
}
let visitor = new MathSplitVisitor2();
return visitor.visit(context);
} catch (ex) {
tree.Type = CalculateTreeType.Error;
tree.ErrorMessage = ex.message;
}
return tree;
}
/**
* Creates a function that represents the sum of two specified functions.
*/
static Calculate_Add(left, right) {
return new Function_Add(left, right);
}
/**
* Creates a function that represents the subtraction of two functions.
*/
static Calculate_Subtract(left, right) {
return new Function_Sub(left, right);
}
/**
* Creates a function that represents the multiplication of two functions.
*/
static Calculate_Multiply(left, right) {
return new Function_Mul(left, right);
}
/**
* Creates a function that represents the division of two functions.
*/
static Calculate_Divide(left, right) {
return new Function_Div(left, right);
}
/**
* Creates a function that computes the remainder after dividing the result of the left function by the result of the
* right function.
*/
static Calculate_Mod(left, right) {
return new Function_Mod(left, right);
}
/**
* Creates a new function that represents the connection of two functions.
*/
static Calculate_Connect(left, right) {
return new Function_Connect(left, right);
}
}
// 浏览器支持
if (typeof window !== 'undefined') {
window.AlgorithmEngineHelper = AlgorithmEngineHelper;
}