From 76cea265de042f19b56d93298b5ccbc49e1c376d Mon Sep 17 00:00:00 2001 From: tianqiao Date: Mon, 26 Feb 2018 17:20:18 +0800 Subject: [PATCH] update version 3.2.1 --- VERSIONS.md | 16 +- pom.xml | 2 +- .../com/ql/util/express/ExpressRunner.java | 33 ++++ .../op/OperatorSelfDefineClassFunction.java | 21 +++ .../match/NodeTypeManagerTestImpl.java | 28 --- .../ql/util/express/parse/ExpressParse.java | 19 +- .../util/express/parse/KeyWordDefine4SQL.java | 73 -------- .../util/express/parse/NodeTypeManager.java | 7 - .../express/bugfix/IgnoreConstCharTest.java | 22 +++ .../util/express/bugfix/LoopFunctionTest.java | 173 ++++++++++++++++++ .../ql/util/express/example/WorkflowTest.java | 8 +- .../ql/util/express/test/ErrorListTest.java | 52 ------ .../express/test/newmatch/PatternTest.java | 51 ------ .../test/newmatch/QLPatternDefineTest.java | 31 ---- .../util/express/test/newmatch/SqlTest.java | 27 --- .../util/express/test/newmatch/TestMatch.java | 118 ------------ 16 files changed, 285 insertions(+), 396 deletions(-) delete mode 100644 src/main/java/com/ql/util/express/match/NodeTypeManagerTestImpl.java delete mode 100644 src/main/java/com/ql/util/express/parse/KeyWordDefine4SQL.java create mode 100644 src/test/java/com/ql/util/express/bugfix/IgnoreConstCharTest.java create mode 100644 src/test/java/com/ql/util/express/bugfix/LoopFunctionTest.java delete mode 100644 src/test/java/com/ql/util/express/test/ErrorListTest.java delete mode 100644 src/test/java/com/ql/util/express/test/newmatch/PatternTest.java delete mode 100644 src/test/java/com/ql/util/express/test/newmatch/QLPatternDefineTest.java delete mode 100644 src/test/java/com/ql/util/express/test/newmatch/SqlTest.java delete mode 100644 src/test/java/com/ql/util/express/test/newmatch/TestMatch.java diff --git a/VERSIONS.md b/VERSIONS.md index 9070842..3a75b8a 100644 --- a/VERSIONS.md +++ b/VERSIONS.md @@ -103,5 +103,17 @@ public Object execute(InstructionSet[] instructionSets,IExpressContext + com.alibaba + QLExpress + jar + 3.2.0 + +``` + +## 3.2.1版本[2018-2-23] +(1)增加扩展功能:ExpressRunner#setIgnoreConstChar(Boolean),设置可以忽略单字符操作,即 'a'自动变成"a"。 +(2)增加接口来支持绑定自定义classloader的class的method:ExpressRunner#addFunctionOfClassMethod(String name, Class aClass,...)。 \ No newline at end of file diff --git a/pom.xml b/pom.xml index ceae560..3e21834 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.alibaba QLExpress jar - 3.2.0 + 3.2.1 QLExpress QLExpress is a powerful, lightweight, dynamic language for the Java platform aimed at improving developers’ productivity in different business scenes. https://github.com/alibaba/QLExpress diff --git a/src/main/java/com/ql/util/express/ExpressRunner.java b/src/main/java/com/ql/util/express/ExpressRunner.java index 6348f79..5341850 100644 --- a/src/main/java/com/ql/util/express/ExpressRunner.java +++ b/src/main/java/com/ql/util/express/ExpressRunner.java @@ -283,6 +283,24 @@ public class ExpressRunner { aClassName, aFunctionName, aParameterClassTypes,null,null, errorInfo)); } + + /** + * 添加一个类的函数定义,例如:Math.abs(double) 映射为表达式中的 "取绝对值(-5.0)" + * @param name 函数名称 + * @param aClass 类 + * @param aFunctionName 类中的方法名称 + * @param aParameterClassTypes 方法的参数类型名称 + * @param errorInfo 如果函数执行的结果是false,需要输出的错误信息 + * @throws Exception + */ + public void addFunctionOfClassMethod(String name, Class aClass, + String aFunctionName, Class[] aParameterClassTypes, + String errorInfo) throws Exception { + this.addFunction(name, new OperatorSelfDefineClassFunction(name, + aClass, aFunctionName, aParameterClassTypes,null,null, errorInfo)); + + } + /** * 添加一个类的函数定义,例如:Math.abs(double) 映射为表达式中的 "取绝对值(-5.0)" * @param name 函数名称 @@ -735,4 +753,19 @@ public class ExpressRunner { public void setShortCircuit(boolean isShortCircuit) { this.isShortCircuit = isShortCircuit; } + + /** + * 是否忽略charset类型的数据,而识别为string,比如'a' -> "a" + * 默认为不忽略,正常识别为String + */ + public boolean isIgnoreConstChar() { + return this.parse.isIgnoreConstChar(); + } + public void setIgnoreConstChar(boolean ignoreConstChar) { + this.parse.setIgnoreConstChar(ignoreConstChar); + } + public void checkySyntax(String text) throws Exception { + Map selfDefineClass = new HashMap (); + this.parse.parse(this.rootExpressPackage,text, true,selfDefineClass); + } } diff --git a/src/main/java/com/ql/util/express/instruction/op/OperatorSelfDefineClassFunction.java b/src/main/java/com/ql/util/express/instruction/op/OperatorSelfDefineClassFunction.java index 25abc40..6acc4f5 100644 --- a/src/main/java/com/ql/util/express/instruction/op/OperatorSelfDefineClassFunction.java +++ b/src/main/java/com/ql/util/express/instruction/op/OperatorSelfDefineClassFunction.java @@ -20,6 +20,27 @@ public class OperatorSelfDefineClassFunction extends OperatorBase implements Can Method method; boolean isReturnVoid; boolean maybeDynamicParams; + + public OperatorSelfDefineClassFunction(String aOperName,Class aOperClass, String aFunctionName, + Class[] aParameterClassTypes,String[] aParameterDesc,String[] aParameterAnnotation,String aErrorInfo) throws Exception { + if (errorInfo != null && errorInfo.trim().length() == 0) { + errorInfo = null; + } + this.name = aOperName; + this.errorInfo = aErrorInfo; + this.functionName = aFunctionName; + this.parameterClasses = aParameterClassTypes; + this.parameterTypes = new String[aParameterClassTypes.length]; + this.operDataDesc = aParameterDesc; + this.operDataAnnotation = aParameterAnnotation; + for(int i=0;i[] aParameterClassTypes,String[] aParameterDesc,String[] aParameterAnnotation,String aErrorInfo) throws Exception { diff --git a/src/main/java/com/ql/util/express/match/NodeTypeManagerTestImpl.java b/src/main/java/com/ql/util/express/match/NodeTypeManagerTestImpl.java deleted file mode 100644 index 416f86e..0000000 --- a/src/main/java/com/ql/util/express/match/NodeTypeManagerTestImpl.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.ql.util.express.match; - -public class NodeTypeManagerTestImpl implements INodeTypeManager { - - public INodeType findNodeType(String name) { - return new TestNodeTypeImpl(name); - } - -} - -class TestNodeTypeImpl implements INodeType{ - String name; - public TestNodeTypeImpl(String aName){ - this.name = aName; - } - public String getName() { - return this.name; - } - - public INodeTypeManager getManager() { - throw new RuntimeException("没有实现的方法"); - } - - @Override - public QLPatternNode getPatternNode() { - throw new RuntimeException("没有实现的方法"); - } -} diff --git a/src/main/java/com/ql/util/express/parse/ExpressParse.java b/src/main/java/com/ql/util/express/parse/ExpressParse.java index 546934a..8eecfb4 100644 --- a/src/main/java/com/ql/util/express/parse/ExpressParse.java +++ b/src/main/java/com/ql/util/express/parse/ExpressParse.java @@ -19,11 +19,26 @@ public class ExpressParse { private static final Log log = LogFactory.getLog(ExpressParse.class); NodeTypeManager nodeTypeManager; IExpressResourceLoader expressResourceLoader; + + /** + * 是否忽略charset类型的数据,而识别为string,比如'a' -> "a" + * 在计算比如 '1'+'2'=='12' + */ + private boolean ignoreConstChar = false; /** * 是否需要高精度计算 */ private boolean isPrecise = false; - public ExpressParse(NodeTypeManager aNodeTypeManager,IExpressResourceLoader aLoader,boolean aIsPrecise){ + + public boolean isIgnoreConstChar() { + return ignoreConstChar; + } + + public void setIgnoreConstChar(boolean ignoreConstChar) { + this.ignoreConstChar = ignoreConstChar; + } + + public ExpressParse(NodeTypeManager aNodeTypeManager, IExpressResourceLoader aLoader, boolean aIsPrecise){ this.nodeTypeManager = aNodeTypeManager; this.expressResourceLoader = aLoader; this.isPrecise = aIsPrecise; @@ -174,7 +189,7 @@ public class ExpressParse { tempWord = tempWord.substring(1,tempWord.length() -1); treeNodeType = nodeTypeManager.findNodeType("CONST"); - if(tempWord.length() == 1){ //转换为字符串 + if(tempWord.length() == 1 && !ignoreConstChar){ //转换为字符串 tempType =nodeTypeManager.findNodeType("CONST_CHAR"); objectValue = tempWord.charAt(0); }else{ diff --git a/src/main/java/com/ql/util/express/parse/KeyWordDefine4SQL.java b/src/main/java/com/ql/util/express/parse/KeyWordDefine4SQL.java deleted file mode 100644 index 1c5937f..0000000 --- a/src/main/java/com/ql/util/express/parse/KeyWordDefine4SQL.java +++ /dev/null @@ -1,73 +0,0 @@ -package com.ql.util.express.parse; - - -public class KeyWordDefine4SQL { - public String[] splitWord={ - "+", "-","*", "/",//四则运算: - ".",",",":",";","(", ")","[", "]","{","}","?",//分隔符号 - "!","<", ">", "<=", ">=", "<>","=" - }; - public String[] keyWords = new String[] { - "select","from","where","and","or","like","if","then","else","as" - }; - public String[] nodeTypeDefines = new String[] { - "in:TYPE=KEYWORD", - "EOF:TYPE=WORDDEF", - "FUNCTION_NAME:TYPE=WORDDEF", - "FUNCTION_DEFINE:TYPE=WORDDEF", - "ID:TYPE=WORDDEF", - "LEFT_BRACKET:TYPE=WORDDEF,DEFINE=(", - "RIGHT_BRACKET:TYPE=WORDDEF,DEFINE=)", - - "CONST_BYTE:TYPE=WORDDEF", - "CONST_SHORT:TYPE=WORDDEF", - "CONST_INTEGER:TYPE=WORDDEF", - "CONST_LONG:TYPE=WORDDEF", - "CONST_FLOAT:TYPE=WORDDEF", - "CONST_DOUBLE:TYPE=WORDDEF", - "CONST_NUMBER:TYPE=WORDDEF,DEFINE=CONST_BYTE|CONST_SHORT|CONST_INTEGER|CONST_LONG|CONST_FLOAT|CONST_DOUBLE", - "CONST_CHAR:TYPE=WORDDEF", - "CONST_STRING:TYPE=WORDDEF", - "CONST_BOOLEAN:TYPE=WORDDEF", - "CONST_CLASS:TYPE=WORDDEF", - "CONST:TYPE=GROUP,DEFINE=CONST_NUMBER|CONST_CHAR|CONST_STRING|CONST_BOOLEAN|CONST_CLASS", - -// "():TYPE=BLOCK,STARTTAG=(,ENDTAG=)", -// "[]:TYPE=BLOCK,STARTTAG=[,ENDTAG=]", -// "{}:TYPE=BLOCK,STARTTAG={,ENDTAG=}", -// "EXPRESS_CHILD:TYPE=GROUP,DEFINE=()|[]", - - "OP_LEVEL1:TYPE=OPERATOR,DEFINE=!", - "OP_LEVEL2:TYPE=OPERATOR,DEFINE=*|/", - "OP_LEVEL3:TYPE=OPERATOR,DEFINE=+|-", - "OP_LEVEL4:TYPE=OPERATOR,DEFINE=in|like", - "OP_LEVEL5:TYPE=OPERATOR,DEFINE=>|>=|<|<=|=|<>", - "OP_LEVEL6:TYPE=OPERATOR,DEFINE=and", - "OP_LEVEL7:TYPE=OPERATOR,DEFINE=or", - - "TAB_COL_NAME:TYPE=STATEMENT,DEFINE=ID$(.$ID)*#TAB_COL_NAME", - "CHILD_EXPRESS:TYPE=EXPRESS,DEFINE=LEFT_BRACKET~$EXPRESS$RIGHT_BRACKET~", - "OPDATA:TYPE=EXPRESS,DEFINE=CHILD_EXPRESS|CONST|TAB_COL_NAME", - "TAB_COL_NAME_ALIAS:TYPE=STATEMENT,DEFINE=TAB_COL_NAME$(as^$TAB_COL_NAME){0:1}", - "TAB_COL_LIST:TYPE=STATEMENT,DEFINE=TAB_COL_NAME_ALIAS$(,~$TAB_COL_NAME_ALIAS)*", - - "EXPRESS_OP_L1:TYPE=EXPRESS,DEFINE=OP_LEVEL1^*$OPDATA", - "EXPRESS_OP_L2:TYPE=EXPRESS,DEFINE=EXPRESS_OP_L1$(OP_LEVEL2^$EXPRESS_OP_L1)^*", - "EXPRESS_OP_L3:TYPE=EXPRESS,DEFINE=EXPRESS_OP_L2$(OP_LEVEL3^$EXPRESS_OP_L2)^*", - "EXPRESS_OP_L4:TYPE=EXPRESS,DEFINE=EXPRESS_OP_L3$(OP_LEVEL4^$EXPRESS_OP_L3)^*", - "EXPRESS_OP_L5:TYPE=EXPRESS,DEFINE=EXPRESS_OP_L4$(OP_LEVEL5^$EXPRESS_OP_L4)^*", - "EXPRESS_OP_L6:TYPE=EXPRESS,DEFINE=EXPRESS_OP_L5$(OP_LEVEL6^$EXPRESS_OP_L5)^*", - "EXPRESS_OP_L7:TYPE=EXPRESS,DEFINE=EXPRESS_OP_L6$(OP_LEVEL7^$EXPRESS_OP_L6)^*", - - "EXPRESS:TYPE=STATEMENT,DEFINE=EXPRESS_OP_L7", - - "OP_LIST:TYPE=GROUP,DEFINE=OP_LEVEL1|OP_LEVEL2|OP_LEVEL3|OP_LEVEL4|OP_LEVEL5|OP_LEVEL6|OP_LEVEL7|LEFT_BRACKET|RIGHT_BRACKET|[|]", - "PARAMETER_LIST:TYPE=STATEMENT,DEFINE=EXPRESS$(,~$EXPRESS)*", - "SELECT:TYPE=STATEMENT,DEFINE=(select^$TAB_COL_LIST)$(from^$PARAMETER_LIST)$(where^$EXPRESS){0:1}#SELECT", - - "STATEMENT:TYPE=STATEMENT", - "PROGRAM:TYPE=STATEMENT,DEFINE=SELECT", - - - }; -} diff --git a/src/main/java/com/ql/util/express/parse/NodeTypeManager.java b/src/main/java/com/ql/util/express/parse/NodeTypeManager.java index cb257ed..c7a5ace 100644 --- a/src/main/java/com/ql/util/express/parse/NodeTypeManager.java +++ b/src/main/java/com/ql/util/express/parse/NodeTypeManager.java @@ -25,13 +25,6 @@ public class NodeTypeManager implements INodeTypeManager { public NodeTypeManager() { this(new KeyWordDefine4Java()); } - public NodeTypeManager(KeyWordDefine4SQL keyWorkdDefine){ - this.splitWord = keyWorkdDefine.splitWord; - this.keyWords = keyWorkdDefine.keyWords; - this.nodeTypeDefines = keyWorkdDefine.nodeTypeDefines; - this.initial(); - - } public NodeTypeManager(KeyWordDefine4Java keyWorkdDefine){ this.splitWord = keyWorkdDefine.splitWord; com.ql.util.express.parse.WordSplit.sortSplitWord(this.splitWord); diff --git a/src/test/java/com/ql/util/express/bugfix/IgnoreConstCharTest.java b/src/test/java/com/ql/util/express/bugfix/IgnoreConstCharTest.java new file mode 100644 index 0000000..c8e718b --- /dev/null +++ b/src/test/java/com/ql/util/express/bugfix/IgnoreConstCharTest.java @@ -0,0 +1,22 @@ +package com.ql.util.express.bugfix; + +import com.ql.util.express.DefaultContext; +import com.ql.util.express.ExpressRunner; +import com.ql.util.express.IExpressContext; +import org.junit.Test; + +/** + * Created by tianqiao on 18/1/29. + */ +public class IgnoreConstCharTest { + + @Test + public void test() throws Exception{ + ExpressRunner runner = new ExpressRunner(); + runner.setIgnoreConstChar(true); + String exp = "'1'+'2'==\"12\""; + IExpressContext context = new DefaultContext(); + Object result = runner.execute(exp,context,null,false,true); + assert ((Boolean) result); + } +} diff --git a/src/test/java/com/ql/util/express/bugfix/LoopFunctionTest.java b/src/test/java/com/ql/util/express/bugfix/LoopFunctionTest.java new file mode 100644 index 0000000..504ac76 --- /dev/null +++ b/src/test/java/com/ql/util/express/bugfix/LoopFunctionTest.java @@ -0,0 +1,173 @@ +package com.ql.util.express.bugfix; + +import com.ql.util.express.DefaultContext; +import com.ql.util.express.ExpressRunner; +import com.ql.util.express.IExpressContext; +import com.ql.util.express.Operator; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Collection; + +/** + * 自定义的LoopAnd,LoopOr,LoopSet function功能 + * Created by tianqiao on 18/2/8. + */ +public class LoopFunctionTest { + + @Test + public void test() throws Exception{ + ExpressRunner runner = new ExpressRunner(false,true); + runner.addFunction("loopAnd", new Operator() { + private ExpressRunner loppRunner = new ExpressRunner(); + @Override + public Object executeInner(Object[] list) throws Exception { + if(list[0]==null){ + return false; + } + if(!(list[0] instanceof Collection)||((Collection)list[0]).size()==0){ + return false; + } + + Collection objs = (Collection) list[0]; + String exp = (String) list[1]; + Integer index = 0; + for(Object obj : objs) { + IExpressContext map = new DefaultContext(); + map.put("x",obj); + map.put("index",index++); + try { + Object r = loppRunner.execute(exp, map, null, true, false); + if(r!=null && r instanceof Boolean && (Boolean) r){ + continue; + }else{ + return false; + } + }catch (Exception e){ + return false; + } + } + return true; + } + }); + runner.addFunction("loopOr", new Operator() { + private ExpressRunner loppRunner = new ExpressRunner(); + @Override + public Object executeInner(Object[] list) throws Exception { + if(list[0]==null){ + return false; + } + if(!(list[0] instanceof Collection)||((Collection)list[0]).size()==0){ + return false; + } + + Collection objs = (Collection) list[0]; + String exp = (String) list[1]; + Integer index=0; + for(Object obj : objs) { + IExpressContext map = new DefaultContext(); + map.put("x",obj); + map.put("index",index++); + try { + Object r = loppRunner.execute(exp, map, null, true, false); + if(r!=null && r instanceof Boolean && (Boolean) r){ + return true; + }else{ + continue; + } + }catch (Exception e){ + return false; + } + } + return false; + } + }); + runner.addFunction("loopSet", new Operator() { + private ExpressRunner loppRunner = new ExpressRunner(); + @Override + public Object executeInner(Object[] list) throws Exception { + if(list[0]==null){ + return false; + } + if(!(list[0] instanceof Collection)||((Collection)list[0]).size()==0){ + return false; + } + + Collection objs = (Collection) list[0]; + String exp = (String) list[1]; + Integer index=0; + for(Object obj : objs) { + IExpressContext map = new DefaultContext(); + map.put("x",obj); + map.put("index",index++); + try { + loppRunner.execute(exp, map, null, true, false); + }catch (Exception e){ + return null; + } + } + return null; + } + }); + + + ArrayList skuList = createSkuList(); + IExpressContext context = new DefaultContext(); + String exp = "loopAnd(skuList,'x.price>10')"; + context.put("skuList",skuList); + Object result = runner.execute(exp,context,null,false,true); + assert ((Boolean)result); + + exp = "loopSet(skuList,'if(index>=2){x.price=9.9}')"; + runner.execute(exp,context,null,false,true); + assert (skuList.get(0).getPrice()==10.1); + assert (skuList.get(1).getPrice()==10.1); + assert (skuList.get(2).getPrice()==9.9); + + exp = "loopOr(skuList,'x.price<10')"; + result = runner.execute(exp,context,null,false,true); + assert ((Boolean)result); + + + } + + private ArrayList createSkuList() { + ArrayList skuList = new ArrayList(); + for(int i=0;i<5;i++) { + SkuDO sku = new SkuDO(); + sku.setPrice(10.1); + skuList.add(sku); + } + return skuList; + } + + public class SkuDO{ + private Long id; + private Double price; + private String title; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public Double getPrice() { + return price; + } + + public void setPrice(Double price) { + this.price = price; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + } +} diff --git a/src/test/java/com/ql/util/express/example/WorkflowTest.java b/src/test/java/com/ql/util/express/example/WorkflowTest.java index 3c45211..ffdc7f1 100644 --- a/src/test/java/com/ql/util/express/example/WorkflowTest.java +++ b/src/test/java/com/ql/util/express/example/WorkflowTest.java @@ -62,7 +62,7 @@ public class WorkflowTest { expressContext.put("申请人", "小强"); expressContext.put("金额", new Integer(4000)); //执行表达式 -// runner.execute(exp, expressContext, null,false, false); + runner.execute(exp, expressContext, null,false, false); } /** @@ -94,7 +94,7 @@ public class WorkflowTest { expressContext.put("申请人", "小强"); expressContext.put("金额", new Integer(5000)); -// runner.executeByExpressName("example/approve1", expressContext, null, false,false,null); + runner.executeByExpressName("example/approve1", expressContext, null, false,false,null); } /** @@ -120,7 +120,7 @@ public class WorkflowTest { expressContext.put("申请人", "小强"); expressContext.put("金额", new Integer(6000)); -// runner.executeByExpressName("example/approve", expressContext, null, false,false,null); + runner.executeByExpressName("example/approve", expressContext, null, false,false,null); } /** @@ -148,7 +148,7 @@ public class WorkflowTest { expressContext.put("申请人", "小强"); expressContext.put("金额", new Integer(7000)); -// runner.executeByExpressName("example/approve1", expressContext, null, false,false,null); + runner.executeByExpressName("example/approve1", expressContext, null, false,false,null); } } diff --git a/src/test/java/com/ql/util/express/test/ErrorListTest.java b/src/test/java/com/ql/util/express/test/ErrorListTest.java deleted file mode 100644 index e48b64b..0000000 --- a/src/test/java/com/ql/util/express/test/ErrorListTest.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.ql.util.express.test; - -import com.ql.util.express.DefaultContext; -import com.ql.util.express.ExpressRunner; -import com.ql.util.express.IExpressContext; -import com.ql.util.express.InstructionSet; -import org.junit.Test; - -import java.util.ArrayList; -import java.util.List; - -public class ErrorListTest { - - public boolean isVIP(String nick) - { - return nick.contains("vip_"); - } - public Integer getUserLevel(String nick) - { - if(nick.contains("vip_")){ - return 3; - } - return 2; - } - - @Test - public void testErrorList() throws Exception { - ExpressRunner runner = new ExpressRunner(false, false); - runner.addFunctionOfServiceMethod("isVIP", new ErrorListTest(), - "isVIP", new Class[]{String.class},"$1不是vip"); - runner.addFunctionOfServiceMethod("getUserLevel", new ErrorListTest(), - "getUserLevel", new Class[]{String.class},""); - runner.addOperatorWithAlias("大于",">","用户等级不够"); - - runner.addOperatorWithAlias("是否VIP","isVIP","亲爱的$1,你还不是VIP用户"); - testExample(runner,"isVIP('vip_11111')"); - testExample(runner,"isVIP('common_11111')"); - - testExample(runner,"getUserLevel('vip_11111') 大于 2"); - testExample(runner,"getUserLevel('common_11111') 大于 2"); - } - - public void testExample(ExpressRunner runner,String express) throws Exception { - IExpressContext context = new DefaultContext(); - List errorList = new ArrayList(); - Object r = runner.execute(express,context,errorList,false,false,null); - System.out.println(r); - for(int i=0;i tempList = parse.transferWord2ExpressNode(null,words,null,true); - System.out.println("单词分析结果:" + ExpressParse.printInfo(tempList,",")); - QLPatternNode pattern = manager.findNodeType(row[0]).getPatternNode(); - QLMatchResult result =QLPattern.findMatchStatement(manager, pattern, tempList, 0); - if(result == null){ - throw new Exception("没有正确的匹配:" + row[0] + ":" + row[1]); - } - System.out.println(result); - } - - } -} diff --git a/src/test/java/com/ql/util/express/test/newmatch/QLPatternDefineTest.java b/src/test/java/com/ql/util/express/test/newmatch/QLPatternDefineTest.java deleted file mode 100644 index b5f4f23..0000000 --- a/src/test/java/com/ql/util/express/test/newmatch/QLPatternDefineTest.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.ql.util.express.test.newmatch; - -import org.junit.Test; - -import com.ql.util.express.match.INodeTypeManager; -import com.ql.util.express.match.NodeTypeManagerTestImpl; -import com.ql.util.express.match.QLPattern; -import com.ql.util.express.match.QLPatternNode; - -public class QLPatternDefineTest { - INodeTypeManager manager = new NodeTypeManagerTestImpl(); - - @Test - public void testDefine() throws Exception { - String[] defines = new String[] { - //"ABC", - //"ABC^*", -// "\\(~$ID$\\)~#()", -// "CONST$(+^$(CONST|ID))^{1:222}#SQL", -// "(CONST|ID)$((*|/)^$CONST)^*", -// "(CONST|ID)$(,~$(CONST|ID))*#PARAMETER_LIST", -// "OPDATA$(.->FIELD_CALL^$ID->CONST_STRING)^*", -// "\\(->CHILD_EXPRESS", - "OP_LEVEL1|OP_LEVEL2|OP_LEVEL3|OP_LEVEL4|OP_LEVEL5|OP_LEVEL6|OP_LEVEL7|OP_LEVEL8|OP_LEVEL9|=|LEFT_BRACKET|RIGHT_BRACKET" - }; - for (String s : defines) { - QLPatternNode t = QLPattern.createPattern(manager,"ANONY_PATTERN", s); - System.out.println(t); - } - } -} diff --git a/src/test/java/com/ql/util/express/test/newmatch/SqlTest.java b/src/test/java/com/ql/util/express/test/newmatch/SqlTest.java deleted file mode 100644 index ce80e32..0000000 --- a/src/test/java/com/ql/util/express/test/newmatch/SqlTest.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.ql.util.express.test.newmatch; - -import org.junit.Test; - -import com.ql.util.express.parse.ExpressNode; -import com.ql.util.express.parse.ExpressParse; -import com.ql.util.express.parse.KeyWordDefine4SQL; -import com.ql.util.express.parse.NodeTypeManager; - -public class SqlTest { - - public String[] testString ={ - "select id as id2 from upp_biz_order", - "select id as id2,name as name2 from upp_biz_order where a=1", - "select id as id2,name from upp_biz_order where 1=1", - }; - @Test - public void testDefine() throws Exception { - NodeTypeManager manager = new NodeTypeManager(new KeyWordDefine4SQL()); - ExpressParse parse = new ExpressParse(manager,null,false); - for(String text : testString){ - ExpressNode result = parse.parse(null, text, true, null); - System.out.print(result); - } - - } -} diff --git a/src/test/java/com/ql/util/express/test/newmatch/TestMatch.java b/src/test/java/com/ql/util/express/test/newmatch/TestMatch.java deleted file mode 100644 index d350fd1..0000000 --- a/src/test/java/com/ql/util/express/test/newmatch/TestMatch.java +++ /dev/null @@ -1,118 +0,0 @@ -package com.ql.util.express.test.newmatch; - -import java.util.ArrayList; -import java.util.List; - -import com.ql.util.express.ExpressRunner; -import com.ql.util.express.IExpressContext; -import com.ql.util.express.test.ExpressContextExample; - -public class TestMatch { - @org.junit.Test - public void testExpress() throws Exception{ - ExpressRunner runner = new ExpressRunner(false,true); - String[][] expressTest = new String[][] { - {"1+2","3"}, - {"2-1","1"}, - {"2+(-1)","1"}, - {"(3+3)*(4+4)/3 + 8 - 2*2","20"}, - {"7==8","false"}, - {"!((8>=8) && !false) || 9==9","true"}, - {"a=(b=9)","9"}, - {"int a = 1+3","4"}, - {"(int[][] a = new int[2][2]).length","2"}, - {" f = (exportDef int b = (int a = 2+3))","5"}, - {"name=new String() + 100","100"}, - {"name=new String((3+3)*4+\"$\") + 100","24$100"}, - {"name=new String(\"xuannan-\") + 100","xuannan-100"}, - {"new String[2][10].length","2"}, - {"[1,2,3,4,5].length","5"}, - {"[1,2,3,4,5][2]","3"}, - {"[[1,2],[3,4]].length","2"}, - {"1000 + new String(\"abc\").substring(1).substring(1).length() + 100","1101"}, - {"new Object().getClass().getAnnotations().length","0"}, - {"new Object().getClass().getMethods()[1].getName()","wait"}, -// {"Object.class.getName().getClass().getMethods().length","72"}, - {"1/2","0"}, - {"1/(double)2","0.5"}, - {"(double)1/2","0.5"}, - {"((Object)\"ABC\").getClass().getName()","java.lang.String"}, - {"[[1,2],[3,4]][0][1]","2"}, - {"(new String[2])[1]","null"}, - {"max(2,100)","100"}, - {"max(max(1,2,3),min(2,100))","3"}, - {"true?1:2","1"}, - {"9==9?100+200:10+20","300"}, - {"9==8?100+200:20+20","40"}, - {"return a=100+90","190"}, - {"alias xuannan qianghui","null"}, - {"max 2-1,3,4+2","6"}, - {"int a = 2 + 1;b=100;return a + b","103"}, - {"{int a = 10;{int a = 20;} return a;}","10"}, - {"{int a = 10;{a = 20;} return a;}","20"}, - {"if true then return 100 else return 200","100"}, - {"if true then {return 100;} else {return 200;}","100"}, - {"if (false) then return 100 else return 200","200"}, - {"if (1!=1) then {return 100;} else {return 200;}","200"}, - {"if true then return 100","100"}, - {"if true then {return 100;} ","100"}, - {"if (false) then return 100","null"}, - {"if (1!=1) then {return 100;}","null"}, - {"if (true) return 100; else return 200","100"}, - {"if (true) {return 100;} else {return 200;}","100"}, - {"if (false) return 100; else return 200","200"}, - {"if (1!=1) {return 100;} else {return 200;}","200"}, - {"if (false) if(false) return 100;else return 200; else return 300;","300"}, - {"int a = 0;{a = a + 100;}{ a= a+ 200;}","300"}, - {"int a =0;for(int i=1;i<=10;i++){a = a + i;} return a;","55"}, - {"int a =0;for(int i=1;i<10;i++){if(i >5) break; a = a + 100;} return a;","500"}, - {"function abc(){return 100;} return abc()","100"}, - {"function abc(int a,int b){ return a + b;} return abc(1+100,2*100)","301"}, - {"macro abc { return a + 100;} int a = 100; return abc + 100","300"}, - {"class Person(String aName,int aYear){" + - "String name = aName;" + - "int year = aYear;" + - "function getName(){return name;}" + - "function getYear(){return year;}" + - "} " + - "Person person =new Person(\"xuannan\",100);" + - "return person.getName() + '-' + person.getYear();" - ,"xuannan-100"}, - {"map = NewMap('ABC':100,'BCD':200,'DEF':1000 + 1000);return map.get('BCD')","200"}, - {"Object[] abc = [];return abc.length","0"}, - {"2 in 2","true"}, - {"2 in (4,5,6)","false"}, - {"(-1)","-1"}, - {"/**1,2,3,4**/1+2","3"}, - {"1+/**1,2,3,4**/2+3","6"}, - {"(new String[3][5])[1].length","5"}, - {"class ABC(com.ql.util.express.test.BeanExample bean,String name){" - +"InnerClass a = new InnerClass();" - + "哈希值:{bean.hashCode();};" - + "class InnerClass(){" + - "int 计数 =200;" + - "};" - + "}" + - "return new ABC(new com.ql.util.express.test.BeanExample(),'xuannan').a.计数" , - "200" - }, - {";i=100;;","100"} - }; - for (int point = 0; point < expressTest.length; point++) { - String expressStr = expressTest[point][0]; - List errorList = new ArrayList(); - IExpressContext expressContext = new ExpressContextExample(null); - Object result = runner.execute(expressStr,expressContext, null, false,false); - if (expressTest[point][1].equalsIgnoreCase("null") - && result != null - || expressTest[point][1].equalsIgnoreCase(result==null?"null":result.toString()) == false) { - throw new Exception("处理错误,计算结果与预期的不匹配:" + expressStr + " = " + result + "但是期望值是:" + expressTest[point][1]); - } - System.out.println("Example " + point + " : " + expressStr + " = " + result); - System.out.println(expressContext); - if(errorList.size() > 0){ - System.out.println("\t\t系统输出的错误提示信息:" + errorList); - } - } - } -} -- GitLab