我使用Jess和Java,我想在Jess规则中调用Java函数,但是当我启动代码时,我有这样的消息:
以下是代码:
// Create a Jess rule engine
ENGINE.reset();
// Load the rules and the templates
//TEMPLATES
ENGINE.eval("(deftemplate Light (declare (from-class ro.domoticbot.data.Light)))");
ENGINE.eval("(deftemplate Door (declare (from-class ro.domoticbot.data.Door)))");
//RULES
ENGINE.eval("(defrule switch-light \"rule to switch light state\" (Light {lightOn == FALSE}) => "
+ "(call flip)(printout t \"the lamp is on\" crlf))");
ENGINE.eval("(defrule open-door \"rule to open a door\" (Door {open == FALSE}) => "
+ "(call flip)(printout t \"the door is open\" crlf))");
以下是两类门和光:
public class Door implements Programmable, Serializable{
private static final long serialVersionUID = 1L;
private boolean open = false;
private final Map<Integer, List<Timeslot>> timeslots = new HashMap<>();
private final String name;
public Door(String name) {
this.name = Objects.requireNonNull(name);
}
/**
* Change the state of the door (open / close)
* Return the new state
*/
@Override
public void flip() {
this.open = !this.open;
}
public boolean getOpen() {
return open;
}
public class Light implements Programmable, Serializable {
private static final long serialVersionUID = 1L;
private boolean lightOn = false;
private final Map<Integer, List<Timeslot>> timeslots = new HashMap<>();
private final String name;
public Light(String name) {
this.name = Objects.requireNonNull(name);
}
/**
* Change the state of the light (light on/off).
*/
@Override
public void flip() {
this.lightOn = !this.lightOn;
}
public boolean getLightOn() {
return lightOn;
}
我想在规则中调用函数flip(),但是这个方法在Light和Door类中定义。 谢谢。
答案 0 :(得分:0)
“call”的第一个参数是调用方法的对象,或静态方法的类名。将 Door 对象绑定到变量(它位于 OBJECT 插槽中),然后将该变量作为第一个参数传递。