Fel軽量高効率式計算エンジン

23466 ワード

Felは軽量レベルの効率的な式計算エンジンです
Felはエンタープライズ・プロジェクトに由来し、変化する機能要件とパフォーマンス要件を満たすことを目標としています.
Felはオープンで、エンジン実行中の複数のモジュールを拡張または置換できます.Felの実行は主に関数によって実現され,演算子(+,−などはすべてFel関数)であり,これらの関数はすべて置換可能であり,拡張関数も非常に簡単である.
Felには2つのエンジンがあり、解釈実行とコンパイル実行をサポートしています.パフォーマンス要件に応じて実行方法を選択できます.コンパイル実行とは、式をバイトコードにコンパイルすることです(javaコードの生成とコンパイルモジュールは拡張と置換が可能です).
Felはどれくらい速いですか.
通常、Fel-0.7は毎秒1000万回の式(コンパイル時間を含まない)を実行できます.速度はJexl-2.0の20倍以上です.
オープンソースの式エンジンがFelより速いことはまだ発見されていない.
テストデータの詳細については、「http://code.google.com/p/fast-el/wiki/Performance.
なぜFelを使うのですか?
Fel構文とAPIは非常に簡単で、構文はJavaとほぼ同じで、学習コストはほとんどありません.
Felはとても速く、簡単に説明しました.
Felはパッケージ全体で200 KB以上しかありません.
Felは配列,集合,Mapの要素,オブジェクトの属性に非常に容易にアクセスできる.
Felは、オブジェクトのメソッドとクラスメソッドを簡単に呼び出すことができます(これらが足りない場合は、カスタム関数を追加できます).
Felは大きい数値の高い精度の計算を支持します
Felは良好な安全管理機能を持っている
Felがあなたの要求を満たすことができなければ、Felを拡張して修正するのは簡単です.
Felは何ができないの?
Felは式のみをサポートし、スクリプトはサポートしません.
Fel適用シーン:
Felは大量のデータを処理するのに適しており、Felの良好な拡張性はユーザーのデータ処理をよりよく助けることができる.
Felは、ワークフロー、計算式、データ有効性チェックなど、式エンジンを使用する必要がある他の場所にも適用されます.
インストール
1:Felの取得
プロジェクトのホームページ:http://code.google.com/p/fast-el/ダウンロード先:http://code.google.com/p/fast-el/downloads/list
Felの使用例:
1:算術式:
FelEngine fel = new FelEngineImpl();    
Object result = fel.eval("5000*12+7500");    
System.out.println(result);   

出力結果:67500
2:変数
次のように、変数を使用します.
FelContext ctx = fel.getContext();    
ctx.set("  ", 5000);    
ctx.set("  ", 12);    
ctx.set("  ", 7500);    
Object result = fel.eval("  *  +  ");    
System.out.println(result);  

出力結果:67500
3:オブジェクト属性へのアクセス
Felでは、オブジェクトのプロパティへのアクセスが非常に便利である可能性があります.サンプルコードは次のとおりです.
FelEngine fel = new FelEngineImpl();
FelContext ctx = fel.getContext();
Foo foo = new Foo();
ctx.set("foo", foo);
Map m = new HashMap();
m.put("ElName", "fel");
ctx.set("m",m);
        
//  foo.getSize()  。
Object result = fel.eval("foo.size");

//  foo.isSample()  。
result = fel.eval("foo.sample");
                
//foo  name、getName、isName  
//foo.name   foo.get("name")  。
result = fel.eval("foo.name");
                
//m.ElName   m.get("ElName");
result = fel.eval("m.ElName");

4:アクセス配列、集合、Map
FelEngine fel = new FelEngineImpl();
FelContext ctx = fel.getContext();

//  
int[] intArray = {1,2,3};
ctx.set("intArray",intArray);
//  intArray[0]
String exp = "intArray[0]";
System.out.println(exp+"->"+fel.eval(exp));

//List
List list = Arrays.asList(1,2,3);
ctx.set("list",list);
//  list.get(0)
exp = "list[0]";
System.out.println(exp+"->"+fel.eval(exp));

//  
Collection coll = Arrays.asList("a","b","c");
ctx.set("coll",coll);
//          。     "a"
exp = "coll[0]";
System.out.println(exp+"->"+fel.eval(exp));

//   
Iterator iterator = coll.iterator();
ctx.set("iterator", iterator);
//           。     "a"
exp = "iterator[0]";
System.out.println(exp+"->"+fel.eval(exp));

//Map
Map m = new HashMap();
m.put("name", "HashMap");
ctx.set("map",m);
exp = "map.name";
System.out.println(exp+"->"+fel.eval(exp));

//    
int[][] intArrays= {{11,12},{21,22}};
ctx.set("intArrays",intArrays);
exp = "intArrays[0][0]";
System.out.println(exp+"->"+fel.eval(exp));

//     ,    、       。
List listArray = new ArrayList();
listArray.add(new int[]{1,2,3});
listArray.add(new int[]{4,5,6});
ctx.set("listArray",listArray);
exp = "listArray[0][0]";
System.out.println(exp+"->"+fel.eval(exp));

5:JAVAメソッドの呼び出し
FelEngine fel = new FelEngineImpl();   
FelContext ctx = fel.getContext();   
ctx.set("out", System.out);   
fel.eval("out.println('Hello Everybody'.substring(6))");  

出力結果:Everybody
6:カスタムコンテキスト環境
//                 
FelContext ctx = new AbstractConetxt() {   
    public Object get(Object name) {   
        if("  ".equals(name)){   
            return " ";   
        }   
        if("  ".equals(name)){   
            return 25;   
        }   
        return null;   
    }   
};   
FelEngine fel = new FelEngineImpl(ctx);   
Object eval = fel.eval("'  :'+  +';  :'+  ");   
System.out.println(eval);  

出力結果:天気:晴れ;温度:25
7:多層コンテキスト環境(ネーミングスペース)
FelEngine fel = new FelEngineImpl();   
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);  

出力結果:
期待利益:50
実績利益:30
8:コンパイル実行
FelEngine fel = new FelEngineImpl();   
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);  

実行結果:67500
備考:大量のデータを処理するのに適しており、コンパイル実行の速度は基本的にJavaバイトコードの実行速度と同じくらい速い.
9:カスタム関数
    //  hello     
    Function fun = new CommonFunction() {   
  
        public String getName() {   
            return "hello";   
        }   
           
        /*   
         *   hello("xxx")        
         */   
        @Override   
        public Object call(Object[] arguments) {   
            Object msg = null;   
            if(arguments!= null && arguments.length>0){   
                msg = arguments[0];   
            }   
            return ObjectUtils.toString(msg);   
        }   
  
    };   
    FelEngine e = new FelEngineImpl();   
    //        。   
    e.addFun(fun);   
    String exp = "hello('fel')";   
    //       
    Object eval = e.eval(exp);   
    System.out.println("hello "+eval);   
    //       
    Expression compile = e.compile(exp, null);   
    eval = compile.eval(null);   
    System.out.println("hello "+eval);  

実行結果:
hello fel hello fel
10:静的メソッドの呼び出し
上記のカスタム関数も面倒だと思う場合は、Felが提供する$関数がツールクラスを簡単に呼び出す方法jQueryに詳しい友人は、「$」関数の威力を知っているに違いありません.Felは東にひそみを施し、classを取得し、オブジェクトを作成する「$」関数も実現した.ポイントオペレータを組み合わせると、ツールクラスやオブジェクトのメソッドを簡単に呼び出すことができます.
  //  Math.min(1,2)
  FelEngine.instance.eval("$('Math').min(1,2)");
  //  new Foo().toString();
  FelEngine.instance.eval("$('com.greenpineyu.test.Foo.new').toString());

「$('class').method」形式の構文では、任意の等方クラスパッケージ(commons langなど)やカスタムツールクラスのメソッドを呼び出すか、オブジェクトを作成し、オブジェクトを呼び出すメソッドを呼び出すことができます.必要に応じて、Java Methodを関数マネージャに直接登録することもできます.
11大数値計算(バージョン0.9より開始)
Felが発表された後、一部のネットユーザーは大きな数値計算機能を提供することを望んで、そこで、大きな数値計算機能がありました.例は次のとおりです.
  FelEngine fel = FelBuilder.bigNumberEngine();
  String input = "111111111111111111111111111111+22222222222222222222222222222222";
  Object value = fel.eval(input);
  Object compileValue = fel.compile(input, fel.getContext()).eval(fel.getContext());
  System.out.println("     (    ):" + value);
  System.out.println("     (    ):" + compileValue);

上記の例から分かるように、大数値計算エンジンと従来の計算エンジンは使用方法が同じである.式の数値が大きい場合、精度が高いことが要求される場合は、大きな数値計算エンジンを使用します.不十分な点は、通常のコンピューティングエンジンほど効率が高くないことです.
セキュリティ(バージョン0.8から)
「${'System'}.exit(1)」という表現が発生してシステムがクラッシュするのを防ぐためです.Felにはセキュリティマネージャが組み込まれており,主にメソッドアクセスを制御している.セキュリティマネージャでは、アクセスが許可されているメソッドリスト(ホワイトリスト)とアクセスが禁止されているメソッドリスト(ブラックリスト)でメソッドアクセスを制御します.「java.lang.System.*」をブラックリストに追加し、Systemクラスのすべてのメソッドがアクセスできないことを示します.「java.lang.Math.*」ホワイトリストに追加すると、Mathクラスにのみアクセスできるメソッドを示します.このセキュリティマネージャが好きでない場合は、自分で開発することができます.非常に簡単で、1つの方法を実現するだけでいいです.
基本Javaエンジニアリングソースコードを添付します.
Exampleクラス:
public class Example {

	public static void main(String[] args) {

		System.out.println("-----------1.  ---------");
		helloworld();

		System.out.println("-----------2.    ---------");
		useVariable();

		System.out.println("-----------3.      ---------");
		getAttr();

		System.out.println("---------4.       -----------");
		callMethod();

		System.out.println("--------5.    、  ------------");
		visitColl();

		System.out.println("--------6.        ------------");
		context();

		System.out.println("--------7.        (      )------------");
		contexts();

		System.out.println("---------8.     -----------");
		testBigNumber();

		System.out.println("----------9.  ----------");
		userFunction();

		System.out.println("---------10.       -----------");
		userInterpreter();

		System.out.println("----------11.     ----------");
		operatorOverload();

		System.out.println("----------12.    ----------");
		testSpeed();

		System.out.println("----------13.    ----------");
		staticMethod();
	}

	/**
	 *   
	 */
	public static void helloworld() {
		// FelEngine fel = new FelEngineImpl();
		Object result = FelEngine.instance.eval("5000*12+7500");
		System.out.println(result);
	}

	/**
	 *     
	 */
	public static void useVariable() {
		FelEngine fel = getEngine();
		FelContext ctx = fel.getContext();
		ctx.set("  ", 5000);
		ctx.set("  ", 12);
		ctx.set("  ", 7500);
		Object result = fel.eval("  *  +  ");
		System.out.println(result);
	}

	/**
	 *       
	 */
	public static void getAttr() {
		FelEngine fel = getEngine();
		FelContext ctx = fel.getContext();
		Foo foo = new Foo();
		ctx.set("foo", foo);
		Map m = new HashMap();
		m.put("ElName", "fel");
		ctx.set("m", m);

		//   foo.getSize()  。
		Object result = fel.eval("foo.size");
		System.out.println(result);
		//   foo.isSample()  。
		result = fel.eval("foo.sample");
		System.out.println(result);
		// foo  name、getName、isName  
		// foo.name   foo.get("name")  。
		result = fel.eval("foo.name");
		System.out.println(result);
		// m.ElName   m.get("ElName");
		result = fel.eval("m.ElName");
		System.out.println(result);
	}

	/**
	 *        
	 */
	public static void callMethod() {
		FelEngine fel = getEngine();
		FelContext ctx = fel.getContext();
		ctx.set("out", System.out);
		fel.eval("out.println('Hello Everybody'.substring(6))");
	}

	/**
	 *     、  
	 */
	public static void visitColl() {
		FelEngine fel = getEngine();
		FelContext ctx = fel.getContext();

		//   
		int[] intArray = { 1, 2, 3 };
		ctx.set("intArray", intArray);
		//   intArray[0]
		String exp = "intArray[0]";
		System.out.println(exp + "->" + fel.eval(exp));

		// List
		List list = Arrays.asList(1, 2, 3);
		ctx.set("list", list);
		//   list.get(0)
		exp = "list[0]";
		System.out.println(exp + "->" + fel.eval(exp));

		//   
		Collection coll = Arrays.asList("a", "b", "c");
		ctx.set("coll", coll);
		//           。     "a"
		exp = "coll[0]";
		System.out.println(exp + "->" + fel.eval(exp));

		//    
		Iterator iterator = coll.iterator();
		ctx.set("iterator", iterator);
		//            。     "a"
		exp = "iterator[0]";
		System.out.println(exp + "->" + fel.eval(exp));

		// Map
		Map m = new HashMap();
		m.put("name", "Wangxiaoming");
		ctx.set("map", m);
		exp = "map.name";
		System.out.println(exp + "->" + fel.eval(exp));

		//     
		int[][] intArrays = { { 11, 12 }, { 21, 22 } };
		ctx.set("intArrays", intArrays);
		exp = "intArrays[0][0]";
		System.out.println(exp + "->" + fel.eval(exp));

		//      ,    、       。
		List listArray = new ArrayList();
		listArray.add(new int[] { 1, 2, 3 });
		listArray.add(new int[] { 4, 5, 6 });
		ctx.set("listArray", listArray);
		exp = "listArray[0][0]";
		System.out.println(exp + "->" + fel.eval(exp));
	}

	/**
	 *         
	 */
	public static void context() {
		//               
		FelContext ctx = new AbstractContext() {
			@Override
			public Object get(String name) {
				if ("  ".equals(name)) {
					return " ";
				}
				if ("  ".equals(name)) {
					return 25;
				}
				return null;
			}

		};
		FelEngine fel = new FelEngineImpl(ctx);
		String exp = "'  -----:'+  +';  ------:'+  ";
		Object eval = fel.compile(exp, ctx).eval(ctx);
		System.out.println(eval);
	}

	/**
	 *         (      )
	 */
	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);
	}

	/**
	 *      
	 */
	public static void testBigNumber() {
		//          
		FelEngine fel = FelBuilder.bigNumberEngine();
		String input = "111111111111111111111111111111+22222222222222222222222222222222";
		Object value = fel.eval(input);//     
		Object compileValue = fel.compile(input, fel.getContext()).eval(
				fel.getContext());//     
		System.out.println("     (    ):" + value);
		System.out.println("     (    ):" + compileValue);
	}

	/**
	 *   
	 */
	public static void userFunction() {
		//   hello  
		Function fun = new CommonFunction() {

			@Override
			public String getName() {
				return "hello";
			}

			/*
			 *   hello("xxx")      
			 */
			@Override
			public Object call(Object[] arguments) {
				Object msg = null;
				if (arguments != null && arguments.length > 0) {
					msg = arguments[0];
				}
				return ObjectUtils.toString(msg);
			}

		};
		FelEngine e = getEngine();
		//         。
		e.addFun(fun);
		String exp = "hello('fel')";
		//     
		Object eval = e.eval(exp);
		System.out.println("hello " + eval);
		//     
		Expression compile = e.compile(exp, null);
		eval = compile.eval(null);
		System.out.println("hello " + eval);
	}

	/**
	 * 
	 */
	public static void testCompileX() {
		FelEngine fel = getEngine();
		String exp = "  *  ";
		final MutableInt index = new MutableInt(0);

		//           
		final int[] price = new int[] { 2, 3, 4 };
		//           
		final double[] number = new double[] { 10.99, 20.99, 9.9 };
		FelContext context = new AbstractContext() {

			@Override
			public Object get(String name) {
				if ("  ".equals(name)) {
					return price[index.intValue()];
				}
				if ("  ".equals(name)) {
					return number[index.intValue()];
				}
				return null;
			}
		};
		Expression compExp = fel.compile(exp, context);
		for (int i = 0; i < number.length; i++) {
			index.setValue(i);
			Object eval = compExp.eval(context);
			System.out.println("  [" + price[i] + "*" + number[i] + "=" + eval
					+ "]");
		}
	}

	/**
	 *        
	 */
	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));
	}

	/**
	 *      ,               
	 */
	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 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);
	}

	/**
	 *     
	 */
	public static void testSpeed() {
		FelEngine fel = getEngine();
		String exp = "40.52334+60*(21.8144+17*32.663)";
		FelNode node = fel.parse(exp);
		int times = 1000;
		long s1 = System.currentTimeMillis();
		for (int i = 0; i < times; i++) {
			// double j = 40.52334 + 60 * (21.8144 + 17 * 32.663);
			node.eval(null);
		}
		long s2 = System.currentTimeMillis();
		System.out.println("     :" + (s2 - s1));
	}

	/**
	 *       (  1   )
	 */
	public static void massData() {
		FelEngine fel = getEngine();
		final Interpreters opti = new Interpreters();
		final MutableInt index = new MutableInt(0);
		int count = 10 * 1000 * 1000;
		final double[] counts = new double[count];
		final double[] prices = new double[count];
		Arrays.fill(counts, 10d);
		Arrays.fill(prices, 2.5d);
		opti.add("  ", new Interpreter() {
			@Override
			public Object interpret(FelContext context, FelNode node) {
				return prices[index.intValue()];
			}
		});
		opti.add("  ", new Interpreter() {
			@Override
			public Object interpret(FelContext context, FelNode node) {
				return counts[index.intValue()];
			}
		});
		Expression expObj = fel.compile("  *  ", null, opti);
		long start = System.currentTimeMillis();
		Object result = null;
		for (int i = 0; i < count; i++) {
			result = expObj.eval(null);
			index.increment();
		}
		long end = System.currentTimeMillis();

		System.out.println("      :" + result + ";  :" + (end - start));
	}

	/**
	 *     
	 * 
	 * 
	 *                 ,Fel   $                  jQuery       "$"     。
	 * Fel    ,      "$"  ,      class     。      ,                。
	 *   "$('class').method"     ,            (commons lang )          ,
	 *        ,       。     ,       Java Method       。
	 */
	public static void staticMethod() {
		//   Math.min(1,2)
		System.out.println(FelEngine.instance.eval("$('Math').max(1,3)"));
		//   new Foo().toString();
		System.out.println(FelEngine.instance
				.eval("$('com.ebiz.fel.Foo.new').toString()"));
	}

	private static FelEngine getEngine() {
		return FelBuilder.engine();
	}

}

class ColumnInterpreter implements Interpreter {
	MutableInt index;

	double[] records;

	ColumnInterpreter(MutableInt index, double[] records) {
		this.index = index;
		this.records = records;
	}

	@Override
	public Object interpret(FelContext context, FelNode node) {
		return records[index.intValue()];
	}
}

class MutableInt {
	private int value;

	public MutableInt(int i) {
		this.value = i;
	}

	public int intValue() {
		return value;
	}

	public void setValue(int i) {
		this.value = i;
	}

	public void increment() {
		value++;
	}
}

Fooクラス:
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Foo {

	private final String name;

	private Foo foo = null;

	static private Foo[] f = new Foo[] { new Foo("array0"), new Foo("array1") };

	static private Foo[] fooes = f;

	public Foo[] getFooes() {
		return fooes;
	}

	public void setFooes(Foo[] fooes) {
		this.fooes = fooes;
	}

	private boolean beenModified = false;
	private String property1 = "some value";

	public Foo(String name) {
		this.name = name;
	}

	public Foo() {
		this("anonymity");
	}

	public static String sayHello(String str) {
		return "hello" + str;
	}

	public class Cheezy {
		public Iterator iterator() {
			return getCheeseList().iterator();
		}
	}

	public String get(String arg) {
		if ("name".equals(arg)) {
			return name;
		}
		return "can't find " + arg;
	}

	public String convertBoolean(boolean b) {
		return "Boolean : " + b;
	}

	public int getCount() {
		return 5;
	}

	public String contact(String a, String b, String c, String d) {
		return a + b + c + c;
	}

	public List getCheeseList() {
		ArrayList answer = new ArrayList();
		answer.add("cheddar");
		answer.add("edam");
		answer.add("brie");
		return answer;
	}

	public Cheezy getCheezy() {
		return new Cheezy();
	}

	public boolean isSimple() {
		return true;
	}

	public int square(int value) {
		return value * value;
	}

	public boolean getTrueAndModify() {
		beenModified = true;
		return true;
	}

	public boolean getModified() {
		return beenModified;
	}

	public int getSize() {
		return 22;
	}

	public String getProperty1() {
		return property1;
	}

	public void setProperty1(String newValue) {
		property1 = newValue;
	}

	public Foo getFoo() {
		return this.foo;
	}

	public void setFoo(Foo foo) {
		this.foo = foo;
	}

	@Override
	public String toString() {
		return this.name;
	}
}

実行結果:
-----------1.  ---------
67500
-----------2.    ---------
67500
-----------3.      ---------
22
can't find sample
anonymity
fel
---------4.       -----------
Everybody
--------5.    、  ------------
intArray[0]->1
list[0]->1
coll[0]->a
iterator[0]->a
map.name->Wangxiaoming
intArrays[0][0]->11
listArray[0][0]->1
--------6.        ------------
  -----: ;  ------:25
--------7.        (      )------------
    :50
    :30
---------8.     -----------
     (    ):22333333333333333333333333333333
     (    ):22333333333333333333333333333333
----------9.  ----------
hello fel
hello fel
---------10.       -----------
60000
----------11.     ----------
    :10.0
----------12.    ----------
     :91
----------13.    ----------
3
anonymity

付felJArダウンロードアドレス:http://download.csdn.net/detail/chichengjunma/9758635