Warning: HTML/RTF email - sorry for the broken mailer. I *promise* to get this 
fixed ASAP.
 
There are several problems with your logic here:
 
1) Your rule should have triggered an endless loop of firing default-rule 
because the RHS modifies a fact matched on the LHS. See the Jess docs for the 
(declare (no-loop TRUE)) construct for how to prevent this.
 
Why didn't this happen in your test? Well, this leads us to another problem 
which is:
 
2) Your (modify) statement doesn't modify the value bound to the ?name variable 
so (printout) shows the original pattern matched value, not the modified 
object's name property.

3) Your use of ?factId <- (TestModify) and (modify ?factId) should be replaced 
with the logic as shown below:
 
(defrule default-rule 
    "Testing rule for modify java object." 
    (TestModify (name ?name) (OBJECT ?obj)) 
    => 
    (?obj setName "Modified")
    (printout t ?name crlf) 
)

You will need to rethink your test case to avoid the endless loop.
 
An example (not actually tested w/ Jess) you might try is this:

(reset)
(watch all)

(defclass TestModify test.TestModify dynamic)
(defrule default-rule
   (declare (no-loop TRUE))
   (TestModify (name ?name) (OBJECT ?obj))
=>
   (printout t "TestModify object updated.")
   (printout t "?name variable = " ?name))
   (printout t "getName = " (?obj getName))
   (?obj setName "Modified") ;; note: no-loop prevents this from triggering 
another default-rule activation
   (printout t "?name variable = " ?name))
   (printout t "getName = " (?obj getName))
)

(bind ?obj (new test.TestModify))
(?obj setName "Hello")
(definstance TestModify ?obj)
(run)
(?obj setName "World")
(run)

I think what you really need to do is to create a more meaningful test case. I 
hope this helps you.

Good luck!
 
alan
__________________
Bleib immer locker

________________________________

From: [EMAIL PROTECTED] on behalf of [EMAIL PROTECTED]
Sent: Mon 6/6/2005 10:49 AM
To: jess-users@sandia.gov
Subject: JESS: Problem when modify facts when the facts is from a java object.



Dear All 

I have created a java object 

/* 
 * Created on Jun 7, 2005 
 * 
 * To change the template for this generated file go to 
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments 
 */ 
package test; 

import java.beans.PropertyChangeListener; 
import java.beans.PropertyChangeSupport; 

/** 
 * @author Samuel Tsang 
 * 
 * To change the template for this generated type comment go to 
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments 
 */ 
public class TestModify { 

    private String name; 
    
    protected PropertyChangeSupport m_pcs = new PropertyChangeSupport(this); 

    public void addPropertyChangeListener(PropertyChangeListener p) { 
        m_pcs.addPropertyChangeListener(p); 
    } 

    public void removePropertyChangeListener(PropertyChangeListener p) { 
        m_pcs.removePropertyChangeListener(p); 
    } 
    
    /** 
     * @return Returns the name. 
     */ 
    public String getName() { 
        return name; 
    } 
    /** 
     * @param name The name to set. 
     */ 
    public void setName(String name) { 
        this.name = name; 
    } 
} 

And a jess code 

(reset) 

(watch all) 
(defclass TestModify test.TestModify dynamic) 

(bind ?obj (new test.TestModify)) 
(call ?obj setName "Hello") 
(definstance TestModify ?obj) 

(facts) 

(defrule default-rule 
    "Testing rule for modify java object." 
    ?factId <- (TestModify (name ?name)) 
    => 
    (modify ?factId (name "Modified")) 
    (printout t ?name crlf) 
) 

(run) 

(facts) 

and get the following result. 

Jess, the Rule Engine for the Java Platform 
Copyright (C) 2004 Sandia Corporation 
Jess Version 7.0a6 3/23/2005 

This copy of Jess will expire in 26 day(s). 
 ==> f-1 (MAIN::TestModify (class <External-Address:java.lang.Class>) (name 
"Hello") (OBJECT <External-Address:test.TestModify>)) 
f-0   (MAIN::initial-fact) 
f-1   (MAIN::TestModify (class <External-Address:java.lang.Class>) (name 
"Hello") (OBJECT <External-Address:test.TestModify>)) 
For a total of 2 facts in module MAIN. 
==> Activation: MAIN::default-rule :  f-1 
MAIN::default-rule: +1+1+t 
FIRE 1 MAIN::default-rule f-1 
Hello 
 <== Focus MAIN 
f-0   (MAIN::initial-fact) 
f-1   (MAIN::TestModify (class <External-Address:java.lang.Class>) (name 
"Hello") (OBJECT <External-Address:test.TestModify>)) 
For a total of 2 facts in module MAIN. 

I expected that the rule will modify the Java Object but it turn out to be no 
modification at all. 

Thanks for any response. 

Samuel

<<winmail.dat>>

Reply via email to