package com.muziyuchen.grule;

import com.muziyuchen.grule.context.Context;
import com.muziyuchen.grule.exception.UnitRunException;

/**
 * 规则引擎
 * Created by LI_ZHEN on 2016/5/5.
 */
public class RuleEngine {
    private static RuleEngine sharedInstance = new RuleEngine();

    /**
     * 获取规则引擎实例
     * @return 规则引擎共享实例
     * */
    public static RuleEngine getInstance() {
        return sharedInstance;
    }

    private Unit _entry;

    private RuleEngine() {
    }

    /**
     * 执行循环遍历单元
     * @param unit 单元
     * @param context 执行上下文
     * */
    private void runLoop(Unit unit, Context context) {
        if (unit != null) {
            try {
                unit.run(context);
                runLoop(unit.next(), context);
            } catch (UnitRunException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 运行规则引擎
     * @param context 上下文
     * */
    public void run(Context context) {
        runLoop(this._entry, context);
    }

    /**
     * 设置入口单元
     * @param unit 单元
     * @return 规则引擎
     * */
    public RuleEngine setEntry(Unit unit) {
        this._entry = unit;
        return this;
    }

}

@@ -427,17 +427,16 @@
 te Unit 
-_
 entry;%0A%0A
@@ -936,32 +936,128 @@
 text context) %7B%0A
+        if (context == null) throw new IllegalArgumentException(%22Context should not be null.%22);%0A
         runLoop(
@@ -1057,25 +1057,24 @@
 unLoop(this.
-_
 entry, conte
@@ -1216,17 +1216,16 @@
    this.
-_
 entry = 
@@ -1258,10 +1258,126 @@
 %0A    %7D%0A%0A
+    /**%0A     * %E8%8E%B7%E5%8F%96%E5%85%A5%E5%8F%A3%E5%8D%95%E5%85%83%0A     * @return %E5%85%A5%E5%8F%A3%E5%8D%95%E5%85%83%0A     * */%0A    public  Unit getEntry() %7B%0A        return this.entry;%0A    %7D%0A%0A
 %7D%0A

更新：为入口单元添加 getter 方法