Esempio n. 1
0
 public static void testCompile() {
   FelEngine fel = getEngine();
   FelContext ctx = fel.getContext();
   ctx.set("单价", 5000);
   ctx.set("数量", 12);
   ctx.set("运费", 7500);
   Expression exp = fel.compile("单价*数量+运费", ctx);
   Object result = exp.eval(ctx);
   System.out.println(result);
 }
Esempio n. 2
0
 /** 自定义 解释器 */
 public static void userInterpreter() {
   FelEngine fel = getEngine();
   String costStr = "成本";
   FelContext rootContext = fel.getContext();
   rootContext.set(costStr, "60000");
   FelNode node = fel.parse(costStr);
   // 将变量解析成常量
   node.setInterpreter(new ConstInterpreter(rootContext, node));
   System.out.println(node.eval(rootContext));
 }
Esempio n. 3
0
  /** 多层次上下文环境(变量命名空间) */
  public static void contexts() {
    FelEngine fel = getEngine();
    String costStr = "成本";
    String priceStr = "价格";
    FelContext baseCtx = fel.getContext();
    // 父级上下文中设置成本和价格
    baseCtx.set(costStr, 50);
    baseCtx.set(priceStr, 100);

    String exp = priceStr + "-" + costStr;
    Object baseCost = fel.eval(exp);
    System.out.println("期望利润:" + baseCost);

    FelContext ctx = new ContextChain(baseCtx, new MapContext());
    // 通货膨胀导致成本增加(子级上下文 中设置成本,会覆盖父级上下文中的成本)
    ctx.set(costStr, 50 + 20);
    Object allCost = fel.eval(exp, ctx);
    System.out.println("实际利润:" + allCost);
  }
Esempio n. 4
0
  /** 操作符重载,使用自定义解释器实现操作符重载 */
  public static void operatorOverload() {
    /*
     * 扩展Fel的+运算符,使其支持数组+数组
     */

    FelEngine fel = getEngine();
    // 单价
    double[] price = new double[] {2, 3, 4};
    // 费用
    double[] cost = new double[] {0.3, 0.3, 0.4};
    FelContext ctx = fel.getContext();
    ctx.set("单价", price);
    ctx.set("费用", cost);
    String exp = "单价+费用";
    Interpreters interpreters = new Interpreters();
    // 定义"+"操作符的解释方法。
    interpreters.add(
        "+",
        new Interpreter() {
          @Override
          public Object interpret(FelContext context, FelNode node) {
            List<FelNode> args = node.getChildren();
            double[] leftArg = (double[]) args.get(0).eval(context);
            double[] rightArg = (double[]) args.get(1).eval(context);
            return sum(leftArg) + sum(rightArg);
          }

          // 对数组进行求和
          public double sum(double[] array) {
            double d = 0;
            for (int i = 0; i < array.length; i++) {
              d += array[i];
            }
            return d;
          }
        });

    // 使用自定义解释器作为编译选项进行进行编译
    Expression expObj = fel.compile(exp, null, interpreters);
    Object eval = expObj.eval(ctx);
    System.out.println("数组相加:" + eval);
  }
Esempio n. 5
0
 public static void testBigNumber() {
   // 构建大数值计算引擎
   FelEngine fel = FelBuilder.bigNumberEngine();
   FelContext ctx = fel.getContext();
   ctx.set("num", new BigInteger("22222222222222222222222222222222"));
   String input = "111111111111111111111111111111+num";
   Object value = fel.eval(input);
   Object compileValue = fel.compile(input, ctx).eval(ctx);
   System.out.println("大数值计算(解释执行):" + value);
   System.out.println("大数值计算(编译执行):" + compileValue);
 }