javajess

JESS Exception: expected method name


I use Jess and Java and I want to call a Java function inside a Jess rule but when I launch my code I have this message :

Stacktrace

Here is the code :

// 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))");

Here are the two classes Door and Light :

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;
}

I want to call the function flip() inside the rules but this method is defined in the classes Light and Door. Thank you.


Solution

  • The first argument to “call” is either the object to call the method on, or the name of a class for a static method. Bind the Door object to a variable (it’s in the OBJECT slot) and then pass that variable as the first argument.