Kaynağa Gözat

改进内置算术表达式计算器

jacky6024 8 yıl önce
ebeveyn
işleme
afde3a609f

+ 11 - 21
urule-core/src/main/java/com/bstek/urule/runtime/ElUtils.java → urule-core/src/main/java/com/bstek/urule/runtime/ElCalculator.java

@@ -10,19 +10,9 @@ import com.bstek.urule.Utils;
  * @author Jacky.gao
  * @since 2017年10月27日
  */
-public class ElUtils {
-    private static int[] operatPriority = new int[] { 0, 3, 2, 1, -1, 1, 0, 2 };// 运用运算符ASCII码-40做索引的运算符优先级
-    
-    public static void main(String[] args) {
-    	try{
-    		String expr="'gaojie:'+1.3243248E7+1024*1024";
-    		System.out.println(eval(expr));    		
-    	}catch(Exception ex){
-    		System.out.println("="+ex.getMessage()+"=");
-    	}
-	}
-
-    public static Object eval(String expression) {
+public class ElCalculator {
+    private static int[] PRIORITY = new int[] { 0, 3, 2, 1, -1, 1, 0, 2 };// 运用运算符ASCII码-40做索引的运算符优先级
+    public Object eval(String expression) {
     	expression = transform(expression);
     	Object result = calculate(expression);
         return result;
@@ -33,7 +23,7 @@ public class ElUtils {
      * @param expression 例如-2+-1*(-3E-2)-(-1) 被转为 ~2+~1*(~3E~2)-(~1)
      * @return 返回转换结果
      */
-    private static String transform(String expression) {
+    private String transform(String expression) {
         char[] arr = expression.toCharArray();
         for (int i = 0; i < arr.length; i++) {
         	char cc=arr[i];
@@ -61,7 +51,7 @@ public class ElUtils {
      * @param expression 要计算的表达式例如:5+12*(3+5)/7
      * @return 返回计算结果
      */
-    private static Object calculate(String expression) {
+    private Object calculate(String expression) {
     	Stack<String> postfixStack = new Stack<String>();
         Stack<Object> resultStack = new Stack<Object>();
         prepare(expression,postfixStack);
@@ -89,7 +79,7 @@ public class ElUtils {
         return resultStack.pop();
     }
 
-    private static void prepare(String expression,Stack<String> postfixStack) {
+    private void prepare(String expression,Stack<String> postfixStack) {
     	Stack<Character> opStack = new Stack<Character>();
         opStack.push(',');// 运算符放入栈底元素逗号,此符号优先级最低
         char[] arr = expression.toCharArray();
@@ -155,24 +145,24 @@ public class ElUtils {
         }
     }
     
-    private static boolean isInvertedComma(char c){
+    private boolean isInvertedComma(char c){
     	return c=='"';
     }
     
-    private static boolean isOperator(char c) {
+    private boolean isOperator(char c) {
         return c == '+' || c == '-' || c == '*' || c == '/' || c == '%' || c == '(' || c == ')';
     }
-    public static boolean compare(char cur, char peek) {// 如果是peek优先级高于cur,返回true,默认都是peek优先级要低
+    public boolean compare(char cur, char peek) {// 如果是peek优先级高于cur,返回true,默认都是peek优先级要低
     	if(cur=='%')cur='*';
     	if(peek=='%')peek='*';
         boolean result = false;
-        if (operatPriority[(peek) - 40] >= operatPriority[(cur) - 40]) {
+        if (PRIORITY[(peek) - 40] >= PRIORITY[(cur) - 40]) {
             result = true;
         }
         return result;
     }
 
-    private static Object calculate(String firstValue, String secondValue, char currentOp) {
+    private Object calculate(String firstValue, String secondValue, char currentOp) {
         Object result = null;
         BigDecimal first,second;
         try{

+ 4 - 2
urule-core/src/main/java/com/bstek/urule/runtime/rete/ContextImpl.java

@@ -22,7 +22,7 @@ import java.util.Map;
 import org.apache.commons.lang.StringUtils;
 import org.springframework.context.ApplicationContext;
 
-import com.bstek.urule.runtime.ElUtils;
+import com.bstek.urule.runtime.ElCalculator;
 import com.bstek.urule.runtime.WorkingMemory;
 import com.bstek.urule.runtime.assertor.AssertorEvaluator;
 
@@ -36,8 +36,10 @@ public class ContextImpl implements Context {
 	private Map<String,String> variableCategoryMap;
 	private ValueCompute valueCompute;
 	private WorkingMemory workingMemory;
+	private ElCalculator elCalculator;
 	public ContextImpl(WorkingMemory workingMemory,ApplicationContext applicationContext,Map<String,String> variableCategoryMap) {
 		this.workingMemory=workingMemory;
+		this.elCalculator=new ElCalculator();
 		this.applicationContext = applicationContext;
 		this.assertorEvaluator=(AssertorEvaluator)applicationContext.getBean(AssertorEvaluator.BEAN_ID);
 		this.variableCategoryMap=variableCategoryMap;
@@ -57,7 +59,7 @@ public class ContextImpl implements Context {
 	
 	@Override
 	public Object parseExpression(String expression) {
-		return ElUtils.eval(expression);
+		return elCalculator.eval(expression);
 	}
 	
 	public String getVariableCategoryClass(String variableCategory) {