Search code examples
jess

How to compare java object in jess


I have a problem when I compare java object as attribute inside the java class

This is my clp file

(import Model.*)

(deftemplate PizzaBase    
    (declare (from-class PizzaBase)  
             (include-variables TRUE)))  

(deftemplate PizzaTopping   
    (declare (from-class PizzaTopping)  
             (include-variables TRUE)))  

(deftemplate Pizza   
    (declare (from-class Pizza)  
             (include-variables TRUE)))  

(defrule make-pizza
        ?pizzaBase1 <-(PizzaBase{size == 9})
        (Pizza(pizzaBase ?pizzaBase1))  
        =>  
         (add (new PizzaBase "New DeepPan" 10))  
         )  

According from my rule, I want to create a new pizzaBase.When the pizzaBase object in Pizza equal pizzaBase1(size = 9), but JESS is not create a new fact for me. From my thinking, I think JESS cannot compare the Java object that create from the class.Therefore, There isn't add any fact to JESS.

So,"How to solve this problem?",because I look on the manual on the JESS website but there aren't any title that according my problem.

Thank!


Solution

  • You may have overlooked section 5.3.2., Adding Java objects to working memory.

    A Java object isn't the same as a fact, even when you derive a shadow (!) fact from a POJO, using from-class and include-variables. A fact contains a reference to the Java object you insert by calling (add ?aNewObject) in the reserved slot name OBJECT.

    Change your rule like this:

     (defrule make-pizza
        (PizzaBase{size == 9}(OBJECT ?pizzaBase1))
        (Pizza(pizzaBase ?pizzaBase1))  
        =>  
         (add (new PizzaBase "New DeepPan" 10))  
         )