表達式解析器--jeval試用 - OSCHINA - 中文開源技術交流社區

2021-01-10 開源中國

jeval是為為你的Java應用程式提供可加入的、高性能、數學、  布爾和函數表達式的解析和運算的高級資源包。

 最新版本: 0.9.3測試版

   public static void main(String args[]) {

  /*
   * This sample shows the basic usage of the JEval Evaluator class.
   * Calling the default contructor will set he quoteCharater to single
   * quote. This constructor will also load all math variables, math
   * functions and string variables.
   */
  Evaluator evaluator = new Evaluator();

  try {

   /**
    * 添加變量到 Evaluator 類實例.
    */
   evaluator.putVariable("a", "'Hello'");
   evaluator.putVariable("b", "'World'");

   /**
    * 簡單輸出變量.
    */
   System.out.println(evaluator.evaluate("#{a}"));
   System.out.println(evaluator.evaluate("#{b}"));

   /**
    * 簡單輸出數學常量.
    */
   System.out.println(evaluator.evaluate("#{PI}"));

   /**
    * 字符串拼裝.
    */
   System.out.println(evaluator.evaluate("#{a} + ' ' + #{b} + '!'"));

   /**
    * This sample clears the variables. This call will not clear
    * preloaded variables.
    */
   evaluator.clearVariables();
   /**
    * 自定義變量.
    */
   evaluator.setVariableResolver(new MockVariableResolver());
   System.out.println(evaluator
     .evaluate("#{MockVariable1} + #{MockVariable2}"));
   
   /**
    * This sample shows an invalid expression. The variables were just
    * cleared, therefor the variable "a" no longer exists.
    */
   System.out.println("An exception is expected in the "
     + "next evaluation.");
   System.out.println(evaluator.evaluate("#{a}"));
  } catch (EvaluationException ee) {
   System.out.println(ee);
  }
 }

 

類MockVariableResolver的內容:

 /**
 * This clas is an example of a custom variable resolver that can set onto an
 * instance of an evaluator.
 */
public class MockVariableResolver implements VariableResolver {

 /**
     * Returns a variable value for the specified variable name.
     *
     * @param variableName
     *            The name of the variable to return the variable value for.
     *
     * @return A variable value for the specified variable name. If the variable
     *         name can not be resolved, then null should be returned.
     *        
     * @throws Can throw a FunctionException if needed.
     */
 public String resolveVariable(String variableName) throws FunctionException {
  
  String returnValue = null;
  
  if (variableName.equals("MockVariable1")) {
   returnValue = "1";
  }
  else if (variableName.equals("MockVariable2")) {
   returnValue = "2";
  }
  else if (variableName.equals("MockVariable3")) {
   throw new FunctionException("Invalid mock variable name.");
  }
  
  return returnValue;
 }
}

帶數學操作符舉例:

  public static void main(String args[]) {

  /*
   * This sample shows the basic usage of the JEval Evaluator class.
   * Calling the default contructor will set he quoteCharater to single
   * quote. This constructor will also load all math variables, math
   * functions and string variables.
   */
  Evaluator evaluator = new Evaluator();

  try {
   /*
    * This sample shows basic addition.
    *
    * Note: The output will contain at least one decimal place, since
    * the results of math operations are treated like doubles.
    */
   System.out.println(evaluator.evaluate("4 + 1"));

   /**
    * This sample shows basic division
    */
   System.out.println(evaluator.evaluate("1 / 3"));

   /**
    * This sample shows a more complex expression involving
    * parentheses.
    */
   System.out.println(evaluator.evaluate("4 + (3 + 1) + (3 + 1) + 1"));

   /**
    * This sample shows an invalid expression. There is no operand to
    * the right of the plus addition operator.
    */
   System.out.println("An exception is expected in the "
     + "next evaluation.");
   System.out.println(evaluator.evaluate("4 + "));
  } catch (EvaluationException ee) {
   System.out.println(ee);
  }
 }

遺憾的是,jeval目前只支持數值,布爾,字符串,自定義函數等的操作,但並不支持日期類型的操作。

對於變量名包含『{』,『}』等保留符號時候的處理也不好。

期待下一版本能有所改善。

相關焦點