Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9f48b80

Browse files
committed
sync inner version 3.1.4 of alibaba
1 add rule execute method to runner 2 fix some bugs 3 enhance some basic operator:instanceof ,bit operator
1 parent 2336835 commit 9f48b80

File tree

74 files changed

+2091
-516
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+2091
-516
lines changed

src/main/java/com/ql/util/express/ExpressRunner.java

Lines changed: 127 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,22 @@
44
import java.util.List;
55
import java.util.Map;
66
import java.util.Stack;
7+
import java.util.regex.Matcher;
8+
import java.util.regex.Pattern;
79

10+
import com.ql.util.express.instruction.op.*;
811
import com.ql.util.express.parse.*;
12+
import com.ql.util.express.rule.Condition;
13+
import com.ql.util.express.rule.Rule;
14+
import com.ql.util.express.rule.RuleManager;
15+
import com.ql.util.express.rule.RuleResult;
916
import org.apache.commons.logging.Log;
1017
import org.apache.commons.logging.LogFactory;
1118

1219
import com.ql.util.express.instruction.ForRelBreakContinue;
1320
import com.ql.util.express.instruction.IOperateDataCache;
1421
import com.ql.util.express.instruction.InstructionFactory;
1522
import com.ql.util.express.instruction.OperateDataCacheImpl;
16-
import com.ql.util.express.instruction.op.OperatorBase;
17-
import com.ql.util.express.instruction.op.OperatorFactory;
18-
import com.ql.util.express.instruction.op.OperatorMinMax;
19-
import com.ql.util.express.instruction.op.OperatorPrint;
20-
import com.ql.util.express.instruction.op.OperatorPrintln;
21-
import com.ql.util.express.instruction.op.OperatorRound;
22-
import com.ql.util.express.instruction.op.OperatorSelfDefineClassFunction;
23-
import com.ql.util.express.instruction.op.OperatorSelfDefineServiceFunction;
2423

2524
/**
2625
* 语法分析和计算的入口类
@@ -35,22 +34,27 @@ public class ExpressRunner {
3534
* 是否输出所有的跟踪信息,同时还需要log级别是DEBUG级别
3635
*/
3736
private boolean isTrace = false;
38-
37+
3938
/**
4039
* 是否使用逻辑短路特性增强质量的效率
4140
*/
4241
private boolean isShortCircuit = true;
43-
42+
4443
/**
4544
* 是否需要高精度计算
4645
*/
4746
private boolean isPrecise = false;
48-
47+
4948
/**
5049
* 一段文本对应的指令集的缓存
5150
*/
5251
private Map<String,InstructionSet> expressInstructionSetCache = new HashMap<String, InstructionSet>();
53-
52+
53+
/**
54+
* 一段文本对应的规则的缓存
55+
*/
56+
private Map<String,Rule> ruleCache = new HashMap<String, Rule>();
57+
5458
private ExpressLoader loader;
5559
private IExpressResourceLoader expressResourceLoader;
5660
/**
@@ -64,8 +68,8 @@ public class ExpressRunner {
6468
/**
6569
* 语法分析器
6670
*/
67-
private ExpressParse parse ;
68-
71+
private ExpressParse parse ;
72+
6973
/**
7074
* 缺省的Class查找的包管理器
7175
*/
@@ -82,7 +86,7 @@ public AppendingClassFieldManager getAppendingClassFieldManager() {
8286
}
8387

8488
private AppendingClassFieldManager appendingClassFieldManager;
85-
89+
8690
private ThreadLocal<IOperateDataCache> m_OperateDataObjectCache = new ThreadLocal<IOperateDataCache>(){
8791
protected IOperateDataCache initialValue() {
8892
return new OperateDataCacheImpl(30);
@@ -91,12 +95,12 @@ protected IOperateDataCache initialValue() {
9195
public IOperateDataCache getOperateDataCache(){
9296
return this.m_OperateDataObjectCache.get();
9397
}
94-
98+
9599
public ExpressRunner(){
96100
this(false,false);
97101
}
98102
/**
99-
*
103+
*
100104
* @param aIsPrecise 是否需要高精度计算支持
101105
* @param aIstrace 是否跟踪执行指令的过程
102106
*/
@@ -107,7 +111,7 @@ public ExpressRunner(boolean aIsPrecise,boolean aIstrace,NodeTypeManager aManage
107111
this(aIsPrecise,aIstrace,new DefaultExpressResourceLoader(),aManager);
108112
}
109113
/**
110-
*
114+
*
111115
* @param aIsPrecise 是否需要高精度计算支持
112116
* @param aIstrace 是否跟踪执行指令的过程
113117
* @param aExpressResourceLoader 表达式的资源装载器
@@ -127,10 +131,20 @@ public ExpressRunner(boolean aIsPrecise,boolean aIstrace,IExpressResourceLoader
127131
rootExpressPackage.addPackage("java.lang");
128132
rootExpressPackage.addPackage("java.util");
129133
this.addSystemFunctions();
130-
}
131-
public void addSystemFunctions(){
132-
this.addFunction("max", new OperatorMinMax("max"));
133-
this.addFunction("min", new OperatorMinMax("min"));
134+
this.addSystemOperators();
135+
}
136+
137+
private void addSystemOperators() {
138+
try {
139+
this.addOperator("instanceof", new OperatorInstanceOf("instanceof"));
140+
}catch (Exception e){
141+
throw new RuntimeException(e);
142+
}
143+
}
144+
145+
public void addSystemFunctions(){
146+
this.addFunction("max", new OperatorMinMax("max"));
147+
this.addFunction("min", new OperatorMinMax("min"));
134148
this.addFunction("round", new OperatorRound("round"));
135149
this.addFunction("print", new OperatorPrint("print"));
136150
this.addFunction("println", new OperatorPrintln("println"));
@@ -157,23 +171,23 @@ public IExpressResourceLoader getExpressResourceLoader(){
157171
* 添加宏定义 例如: macro 玄难 { abc(userinfo.userId);}
158172
* @param macroName:玄难
159173
* @param express :abc(userinfo.userId);
160-
* @throws Exception
174+
* @throws Exception
161175
*/
162-
public void addMacro(String macroName,String express) throws Exception{
176+
public void addMacro(String macroName,String express) throws Exception{
163177
String macroExpress = "macro " + macroName +" {" + express + "}";
164178
this.loader.parseInstructionSet(GLOBAL_DEFINE_NAME,macroExpress);
165179
}
166-
180+
167181
/**
168182
* 装载表达式,但不执行,例如一些宏定义,或者自定义函数
169183
* @param groupName
170184
* @param express
171185
* @throws Exception
172186
*/
173-
public void loadMutilExpress(String groupName,String express) throws Exception{
187+
public void loadMutilExpress(String groupName,String express) throws Exception{
174188
if(groupName == null || groupName.trim().length() ==0){
175189
groupName = GLOBAL_DEFINE_NAME;
176-
}
190+
}
177191
this.loader.parseInstructionSet(groupName,express);
178192
}
179193
/**
@@ -267,15 +281,15 @@ public void addFunctionOfClassMethod(String name, String aClassName,
267281
String errorInfo) throws Exception {
268282
this.addFunction(name, new OperatorSelfDefineClassFunction(name,
269283
aClassName, aFunctionName, aParameterClassTypes,null,null, errorInfo));
270-
284+
271285
}
272286
/**
273287
* 添加一个类的函数定义,例如:Math.abs(double) 映射为表达式中的 "取绝对值(-5.0)"
274288
* @param name 函数名称
275289
* @param aClassName 类名称
276290
* @param aFunctionName 类中的方法名称
277291
* @param aParameterClassTypes 方法的参数类型名称
278-
* @param aParameterDesc 方法的参数说明
292+
* @param aParameterDesc 方法的参数说明
279293
* @param aParameterAnnotation 方法的参数注解
280294
* @param errorInfo 如果函数执行的结果是false,需要输出的错误信息
281295
* @throws Exception
@@ -301,15 +315,15 @@ public void addFunctionOfClassMethod(String name, String aClassName,
301315
String aFunctionName, String[] aParameterTypes, String errorInfo)
302316
throws Exception {
303317
this.addFunction(name, new OperatorSelfDefineClassFunction(name,
304-
aClassName, aFunctionName, aParameterTypes, null,null,errorInfo));
318+
aClassName, aFunctionName, aParameterTypes, null,null,errorInfo));
305319
}
306320
/**
307321
* 添加一个类的函数定义,例如:Math.abs(double) 映射为表达式中的 "取绝对值(-5.0)"
308322
* @param name 函数名称
309323
* @param aClassName 类名称
310324
* @param aFunctionName 类中的方法名称
311325
* @param aParameterTypes 方法的参数类型名称
312-
* @param aParameterDesc 方法的参数说明
326+
* @param aParameterDesc 方法的参数说明
313327
* @param aParameterAnnotation 方法的参数注解
314328
* @param errorInfo 如果函数执行的结果是false,需要输出的错误信息
315329
* @throws Exception
@@ -320,8 +334,8 @@ public void addFunctionOfClassMethod(String name, String aClassName,
320334
String errorInfo)
321335
throws Exception {
322336
this.addFunction(name, new OperatorSelfDefineClassFunction(name,
323-
aClassName, aFunctionName, aParameterTypes, aParameterDesc,aParameterAnnotation,errorInfo));
324-
337+
aClassName, aFunctionName, aParameterTypes, aParameterDesc,aParameterAnnotation,errorInfo));
338+
325339
}
326340
/**
327341
* 用于将一个用户自己定义的对象(例如Spring对象)方法转换为一个表达式计算的函数
@@ -337,19 +351,19 @@ public void addFunctionOfServiceMethod(String name, Object aServiceObject,
337351
String errorInfo) throws Exception {
338352
this.addFunction(name, new OperatorSelfDefineServiceFunction(name,
339353
aServiceObject, aFunctionName, aParameterClassTypes,null,null, errorInfo));
340-
354+
341355
}
342356
/**
343357
* 用于将一个用户自己定义的对象(例如Spring对象)方法转换为一个表达式计算的函数
344358
* @param name
345359
* @param aServiceObject
346360
* @param aFunctionName
347361
* @param aParameterClassTypes
348-
* @param aParameterDesc 方法的参数说明
362+
* @param aParameterDesc 方法的参数说明
349363
* @param aParameterAnnotation 方法的参数注解
350364
* @param errorInfo
351365
* @throws Exception
352-
*/
366+
*/
353367
public void addFunctionOfServiceMethod(String name, Object aServiceObject,
354368
String aFunctionName, Class<?>[] aParameterClassTypes,
355369
String[] aParameterDesc,String[] aParameterAnnotation,
@@ -369,7 +383,7 @@ public void addFunctionOfServiceMethod(String name, Object aServiceObject,
369383
*/
370384
public void addFunctionOfServiceMethod(String name, Object aServiceObject,
371385
String aFunctionName, String[] aParameterTypes, String errorInfo)
372-
throws Exception {
386+
throws Exception {
373387
this.addFunction(name, new OperatorSelfDefineServiceFunction(name,
374388
aServiceObject, aFunctionName, aParameterTypes,null,null, errorInfo));
375389

@@ -387,7 +401,7 @@ public void addFunctionOfServiceMethod(String name, Object aServiceObject,
387401
* 添加操作符号,此操作符号的优先级与 "*"相同,语法形式也是 data name data
388402
* @param name
389403
* @param op
390-
* @throws Exception
404+
* @throws Exception
391405
*/
392406
public void addOperator(String name,Operator op) throws Exception {
393407
this.addOperator(name, "*", op);
@@ -397,7 +411,7 @@ public void addOperator(String name,Operator op) throws Exception {
397411
* @param name 操作符号名称
398412
* @param aRefOpername 参照的操作符号,例如 "+","--"等
399413
* @param op
400-
* @throws Exception
414+
* @throws Exception
401415
*/
402416
public void addOperator(String name,String aRefOpername,Operator op) throws Exception {
403417
this.manager.addOperatorWithLevelOfReference(name, aRefOpername);
@@ -425,7 +439,7 @@ public void addOperatorWithAlias(String keyWordName, String realKeyWordName,
425439
}
426440
NodeType realNodeType = this.manager.findNodeType(realKeyWordName);
427441
if(realNodeType == null){
428-
throw new Exception("关键字:" + realKeyWordName +"不存在");
442+
throw new Exception("关键字:" + realKeyWordName +"不存在");
429443
}
430444
boolean isExist = this.operatorManager.isExistOperator(realNodeType.getName());
431445
if(isExist == false && errorInfo != null){
@@ -435,7 +449,7 @@ public void addOperatorWithAlias(String keyWordName, String realKeyWordName,
435449
//不需要新增操作符号,只需要建立一个关键子即可
436450
this.manager.addOperatorWithRealNodeType(keyWordName, realNodeType.getName());
437451
}else{
438-
this.manager.addOperatorWithLevelOfReference(keyWordName, realNodeType.getName());
452+
this.manager.addOperatorWithLevelOfReference(keyWordName, realNodeType.getName());
439453
this.operatorManager.addOperatorWithAlias(keyWordName, realNodeType.getName(), errorInfo);
440454
}
441455
}
@@ -446,7 +460,7 @@ public void addOperatorWithAlias(String keyWordName, String realKeyWordName,
446460
public OperatorBase replaceOperator(String name,OperatorBase op){
447461
return this.operatorManager.replaceOperator(name, op);
448462
}
449-
463+
450464
public ExpressPackage getRootExpressPackage(){
451465
return this.rootExpressPackage;
452466
}
@@ -492,7 +506,7 @@ public Object execute(InstructionSet[] instructionSets,IExpressContext<String,Ob
492506
return InstructionSetRunner.executeOuter(this,instructionSets[0],this.loader,context, errorList,
493507
isTrace,isCatchException,aLog,false);
494508
}
495-
509+
496510
/**
497511
* 执行指令集
498512
* @param instructionSets
@@ -557,6 +571,73 @@ public Object execute(String expressString, IExpressContext<String,Object> conte
557571
isTrace,false,aLog,false);
558572
}
559573

574+
public RuleResult executeRule(String expressString, IExpressContext<String,Object> context, boolean isCache, boolean isTrace)
575+
throws Exception {
576+
Rule rule = null;
577+
if (isCache == true) {
578+
rule = ruleCache.get(expressString);
579+
if (rule == null) {
580+
synchronized (ruleCache) {
581+
rule = ruleCache.get(expressString);
582+
if (rule == null) {
583+
rule = this.parseRule(expressString);
584+
ruleCache.put(expressString,
585+
rule);
586+
}
587+
}
588+
}
589+
} else {
590+
rule = this.parseRule(expressString);
591+
}
592+
return RuleManager.executeRule(this,rule,context,isCache,isTrace);
593+
}
594+
595+
static Pattern patternRule = Pattern.compile("rule[\\s]+'([^']+)'[\\s]+name[\\s]+'([^']+)'[\\s]+");
596+
597+
public Rule parseRule(String text)
598+
throws Exception {
599+
String ruleName = null;
600+
String ruleCode = null;
601+
Matcher matcher = patternRule.matcher(text);
602+
if(matcher.find()) {
603+
ruleCode = matcher.group(1);
604+
ruleName = matcher.group(2);
605+
text = text.substring(matcher.end());
606+
}
607+
608+
Map<String,String> selfDefineClass = new HashMap<String,String> ();
609+
for(ExportItem item : this.loader.getExportInfo()){
610+
if(item.getType().equals(InstructionSet.TYPE_CLASS)){
611+
selfDefineClass.put(item.getName(), item.getName());
612+
}
613+
}
614+
615+
// 分成两句话执行,用来保存中间的words结果
616+
// ExpressNode root = this.parse.parse(this.rootExpressPackage,text, isTrace,selfDefineClass);
617+
618+
Word[] words = this.parse.splitWords(rootExpressPackage,text,isTrace,selfDefineClass);
619+
ExpressNode root = this.parse.parse(rootExpressPackage,words,text,isTrace,selfDefineClass);
620+
Rule rule = RuleManager.createRule(root,words);
621+
rule.setCode(ruleCode);
622+
rule.setName(ruleName);
623+
return rule;
624+
}
625+
626+
public Condition parseContition(String text)
627+
throws Exception {
628+
629+
Map<String,String> selfDefineClass = new HashMap<String,String> ();
630+
for(ExportItem item : this.loader.getExportInfo()){
631+
if(item.getType().equals(InstructionSet.TYPE_CLASS)){
632+
selfDefineClass.put(item.getName(), item.getName());
633+
}
634+
}
635+
636+
Word[] words = this.parse.splitWords(rootExpressPackage,text,isTrace,selfDefineClass);
637+
ExpressNode root = this.parse.parse(rootExpressPackage,words,text,isTrace,selfDefineClass);
638+
return RuleManager.createCondition(root,words);
639+
}
640+
560641
/**
561642
* 解析一段文本,生成指令集合
562643
* @param text
@@ -571,7 +652,7 @@ public InstructionSet parseInstructionSet(String text)
571652
selfDefineClass.put(item.getName(), item.getName());
572653
}
573654
}
574-
655+
575656
ExpressNode root = this.parse.parse(this.rootExpressPackage,text, isTrace,selfDefineClass);
576657
InstructionSet result = createInstructionSet(root, "main");
577658
if (this.isTrace && log.isDebugEnabled()) {
@@ -586,7 +667,7 @@ public InstructionSet parseInstructionSet(String text)
586667
public ExportItem[] getExportInfo(){
587668
return this.loader.getExportInfo();
588669
}
589-
670+
590671
/**
591672
* 优先从本地指令集缓存获取指令集,没有的话生成并且缓存在本地
592673
* @param expressString
@@ -637,7 +718,7 @@ public boolean createInstructionSetPrivate(InstructionSet result,
637718
* 获取一个表达式需要的外部变量名称列表
638719
* @param express
639720
* @return
640-
* @throws Exception
721+
* @throws Exception
641722
*/
642723
public String[] getOutVarNames(String express) throws Exception{
643724
return this.parseInstructionSet(express).getOutAttrNames();

0 commit comments

Comments
 (0)