欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

传文件,感谢UC

程序员文章站 2022-07-14 08:57:09
...
/**
 * 
 */
package com.ly.rule;

import java.util.HashMap;
import java.util.Map;

import org.apache.ibatis.ognl.Ognl;
import org.apache.ibatis.ognl.OgnlContext;
import org.apache.ibatis.ognl.OgnlException;

/**
 * @author ly
 *
 */
public class OgnlDemo {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// test1();
		// test2();
		test0();
	}

	private static void test0() {
		OgnlContext context = new OgnlContext();
		Map<String, Object> obj = new HashMap<>();
		obj.put("name", "test");
		obj.put("user_age", new Integer(10));
		obj.put("order_amount", new Double(3000));
		context.setRoot(obj);
		String expression = "name == 'test' && user_age < 10 && order_amount > 3000";
		try {
			Boolean result = (Boolean) Ognl.getValue(expression, context, context.getRoot());
			System.out.println(result);
		} catch (OgnlException e) {
			e.printStackTrace();
		}
	}

	private static void test1() {
		Foo foo = new Foo();
		foo.setName("test");
		User user = new User();
		user.setAge(9);
		Order order = new Order();
		order.setAmount(3003.56);
		OgnlContext context = new OgnlContext();
		context.setRoot(foo);
		context.put("user", user);
		context.put("order", order);
		System.out.println(context.getValues());
		String expression = "name == 'test' && #user.age < 10 && #order.amount > 3000";
		try {
			Boolean result = (Boolean) Ognl.getValue(expression, context, context.getRoot());
			System.out.println(result);
		} catch (OgnlException e) {
			e.printStackTrace();
		}
	}

	private static void test2() {
		Foo foo = new Foo();
		foo.setName("test");
		User user = new User();
		user.setAge(19);
		Order order = new Order();
		order.setAmount(3003.56);
		OgnlContext context = new OgnlContext();
		context.setRoot(foo);
		context.put("user", user);
		context.put("order", order);
		System.out.println(context.getValues());
		String expression = "name == 'test' && #user.age < 10 && #order.amount > 3000";
		try {
			Boolean result = (Boolean) Ognl.getValue(expression, context, context.getRoot());
			System.out.println(result);
		} catch (OgnlException e) {
			e.printStackTrace();
		}
	}

}

class Order {
	private double amount;

	public double getAmount() {
		return amount;
	}

	public void setAmount(double amount) {
		this.amount = amount;
	}

	@Override
	public String toString() {
		return "Order [amount=" + amount + "]";
	}

}

class User {
	private int age;

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	@Override
	public String toString() {
		return "User [age=" + age + "]";
	}
}

class Foo {
	private String name;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public String toString() {
		return "Foo [name=" + name + "]";
	}

}