Wiki Index | All wiki pages | Raw text

一个最典型的应用场景:

在业务系统中存在一些逻辑判断,例如"商品A"只能是三星卖家,而且已经开通淘宝店铺的用户才能订购。
那么我们期望业务人员看到的配置信息就是:"三星卖家 而且 已经开店"
则通过下列步骤可能实现这个功能:

1、定义一个用户信息对象:

class UserInfo {
	long id;
	long tag;
        String name;

	public UserInfo(long aId,String aName, long aUserTag) {
		this.id = aId;
		this.tag = aUserTag;
		this.name = aName;
	}
        public String getName(){
    	   return name;
        }
	public long getUserId() {
		return id;
	}

	public long getUserTag() {
		return tag;
	}
}

2、定义两个基础的功能函数:

   /**
     * 判断一个用户TAG的第X位是否为1。这个的demo,其实现合理性不考虑
     * @param user
     * @param tagBitIndex
     * @return
     */
    public boolean userTagJudge(UserInfo user,int tagBitIndex){
    	boolean r =  (user.getUserTag() & ((long)Math.pow(2, tagBitIndex))) > 0;
    	return r;
    }
	
	/**
	 * 判断一个用户是否订购过某个商品
	 * @param user
	 * @param goodsId
	 * @return
	 */
	public boolean hasOrderGoods(UserInfo user,long goodsId){
		//随机模拟一个
		if(user.getUserId() % 2 == 1){
			return true;
		}else{
			return false;
		}
	}

3、初始化语句执行器

	public void initial() throws Exception{
		runner.addOperatorWithAlias("而且","and",null);
		runner.addFunctionOfClassMethod("userTagJudge", Demo.class.getName(), "userTagJudge",
                            new String[] {UserInfo.class.getName(),"int"}, "你不是三星卖家");
		runner.addFunctionOfClassMethod("hasOrderGoods", Demo.class.getName(), "hasOrderGoods",
                            new String[] {UserInfo.class.getName(),"long"}, "你没有开通淘宝店铺");
		runner.addMacro("三星卖家", "userTagJudge(userInfo,3)");//3表示三星卖家的标志位
		runner.addMacro("已经开店", "hasOrderGoods(userInfo,100)");//100表示旺铺商品的ID
	}

4、定义一个逻辑判断函数:

	/**
	 * 判断逻辑执行函数
	 * @param userInfo
	 * @param expression
	 * @return
	 * @throws Exception
	 */
	public String hasPermission(UserInfo userInfo,String expression) throws Exception {			
        	IExpressContext<String,Object> expressContext = new DefaultContext<String,Object>();
    		expressContext.put("userInfo",userInfo);
    		List<String> errorInfo = new ArrayList<String>();
            Boolean result = (Boolean)runner.execute(expression, expressContext, errorInfo, true, false);
            String resultStr ="";
            if(result.booleanValue() == true){
            	resultStr = "可以订购此商品";
            }else{
              for(int i=0;i<errorInfo.size();i++){
            	  if(i > 0){
            		  resultStr  = resultStr + "," ;
            	  }
            	  resultStr  = resultStr + errorInfo.get(i);
              }
              resultStr = resultStr  + ",所以不能订购此商品";
            }
            return "亲爱的" + userInfo.getName() + " : " + resultStr;
    }	

5、调用执行器执行判断逻辑:

	public static void main(String args[]) throws Exception{
		Demo demo = new Demo();
		demo.initial();
		System.out.println(demo.hasPermission(new UserInfo(100,"xuannan",7),  "三星卖家   而且   已经开店"));
		System.out.println(demo.hasPermission(new UserInfo(101,"qianghui",8), "三星卖家   而且   已经开店"));
		System.out.println(demo.hasPermission(new UserInfo(100,"张三",8), "三星卖家 and 已经开店"));
		System.out.println(demo.hasPermission(new UserInfo(100,"李四",7), "三星卖家 and 已经开店"));
	}

All wiki pages | Raw text

Attachments

qhlhl | 2011-09-29 23:55:51 | QLExpress-2.2.4.zip | 793.6 KB
qhlhl | 2011-09-29 23:55:51 | QLExpress-2.2.5.zip | 788.8 KB
qhlhl | 2011-09-29 23:55:51 | QLExpress-2.2.7.zip | 790.6 KB
qhlhl | 2011-09-29 23:55:51 | Demo.java | 3.2 KB