这是我的主要方法。
public static void main(String[] args) throws JessException {
Rete engine = new Rete();
engine.batch("template.clp");
PizzaBase pizzaBase = new PizzaBase();
pizzaBase.setSize(9);
PizzaTopping pizzaTopping = new PizzaTopping();
pizzaTopping.setName("T1");
PizzaTopping pizzaTopping2 = new PizzaTopping();
pizzaTopping2.setName("T2");
PizzaTopping pizzaTopping3 = new PizzaTopping();
pizzaTopping3.setName("T3");
List<PizzaTopping> pizzaToppingList = new ArrayList<PizzaTopping>();
pizzaToppingList.add(pizzaTopping);
pizzaToppingList.add(pizzaTopping2);
pizzaToppingList.add(pizzaTopping3);
Pizza pizza = new Pizza();
pizza.setBase(pizzaBase);
pizza.setPizzaToppingList(pizzaToppingList);
engine.add(pizzaBase);
engine.addAll(pizzaToppingList);
engine.add(pizza);
engine.eval("(facts)");
engine.batch("rules.clp");
engine.reset();
engine.run();
engine.eval("(facts)");
}
我想在pizzaToppingList中检查包含顶部名称的披萨是否为“T2”。规则如下所示
(defrule rule1
(Pizza (pizzaToppingList ?t2) (OBJECT ?a))
(PizzaTopping (name "T2") (OBJECT ?t2))
=>
(printout t "fired")
)
但它不起作用,因为在JESS中,pizzaToppingList的槽值是ArrayList(java对象)。这意味着pizzaToppingList槽不是JESS中的Lists值。
我如何解决这个问题?谢谢!
答案 0 :(得分:1)
好吧,你必须调用Java自己的ArrayList类的方法。一种简单的方法是使用test
CE:
(defrule rule1
(Pizza (pizzaToppingList ?ptList) (OBJECT ?a))
(PizzaTopping (name "T2") (OBJECT ?t2))
(test (?ptList contains ?t2) )
=>
(printout t "fired")
)
您尝试的内容无法使用,因为您对列表使用相同的绑定变量?t2
以及可能是其元素之一。