Hi all,
I attached the source of a simple Clock bean that implements Runnable.
In the source are two implementations of the getDateObject() method.
METHOD 1 is working, METHOD 2 isn't and produces following watch output
(batch clocktest.jess) :
==> f-0 (Clock (class <External-Address:java.lang.Class>) (date 18)
(dateObject <External-Address:java.util.Date>) (day 2) (hours 21) (minutes 59) (month
9) (propertyChangeSupport <External-Address:java.beans.PropertyChangeSupport>)
(seconds 0) (year 2001) (OBJECT <External-Address:jesstest.Clock>))
==> f-0 (Clock (class <External-Address:java.lang.Class>) (date 18)
(dateObject <External-Address:java.util.Date>) (day 2) (hours 22) (minutes 0) (month 9)
(propertyChangeSupport <External-Address:java.beans.PropertyChangeSupport>)
(seconds 0) (year 2001) (OBJECT <External-Address:jesstest.Clock>))
As you can see the clock fact isn't retracted before the changed clock is
reasserted. There are also multiple facts of this clock, all with the same
fact-id.
Q: Why isn't METHOD 2 working? Why have I to clone the date object?
Jess Version: 6.0a5
Thank You...
Regards,
Matthias
package jesstest;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
/**
* A runnable Clock
*/
public class Clock implements Runnable
{
///////////////////////////////////////////
/*
// working method (METHOD 1)
public java.util.Date getDateObject()
{
return(java.util.Date) date.clone();
}
*/
//-----------------------------------------
// NOT working method (METHOD 2)
public java.util.Date getDateObject()
{
return date;
}
///////////////////////////////////////////
private boolean halt = false;
/**
* The Java Date
*/
private java.util.Date date = new java.util.Date();
/**
* The property change support
*/
private PropertyChangeSupport pcs = new PropertyChangeSupport(this);
public void run()
{
while (!halt)
{
date.setTime(System.currentTimeMillis());
pcs.firePropertyChange(null,null,null);
try
{
Thread.sleep(60010 - (System.currentTimeMillis() % 60000));
}
catch(InterruptedException e)
{
}
}
}
public void halt()
{
halt = true;
}
public int getYear()
{
return date.getYear() + 1900;
}
public int getMonth()
{
return date.getMonth() + 1;
}
public int getDate()
{
return date.getDate();
}
public int getDay()
{
return date.getDay();
}
public int getHours()
{
return date.getHours();
}
public int getMinutes()
{
return date.getMinutes();
}
public int getSeconds()
{
return date.getSeconds();
}
public boolean before(java.util.Date date)
{
return this.date.before(date);
}
public boolean after(java.util.Date date)
{
return this.date.after(date);
}
// property change support
public PropertyChangeSupport getPropertyChangeSupport()
{
return pcs;
}
public void addPropertyChangeListener(PropertyChangeListener pcl)
{
pcs.addPropertyChangeListener(pcl);
}
public void removePropertyChangeListener(PropertyChangeListener pcl)
{
pcs.removePropertyChangeListener(pcl);
}
}
;; Clock test
(watch all)
;; create deftemplate from class
(defclass Clock jesstest.Clock)
;; new clock object
(bind ?clock (new jesstest.Clock) )
;; new clock-thread
(bind ?clockThread (new Thread ?clock) )
;; create fact (shadow)
(definstance Clock ?clock dynamic)
;; start clock-thread
(call ?clockThread start)