Yeah, this being my first major Java project the String.equals mistake is
one I've probably made half a dozen times. Below is the code where I add
the object to working memory.
private void setEngine(Rete engine)
{
this.engine = engine;
// Set round after retrieving it from game
Game game = (Game)AppContext.getDataManager().getBinding(gameName);
round = game.getRound();
// Add round to engine and run
try
{
engine.removeFacts("Round"); // Remove any existing Round
objects from working memory (needed to eliminate objects added by aborted
transactions)
factId = this.engine.definstance("Round", round, true);
this.engine.run();
}
catch (JessException e)
{
logger.log(Level.WARNING, "Error setting round");
}
}
On Fri, Aug 21, 2009 at 11:48 AM, Ernest Friedman-Hill
<[email protected]>wrote:
> The only thing here that's clearly wrong is the equals() method -- you
> can't (generally) compare Strings with equals -- you want to use the
> equals() method instead. But otherwise from Jess's perspective I think
> things should be fine.
>
> How are you adding the objects to working memory? Using "add" or
> "definstance"? Can I see that line of code?
>
>
>
> On Aug 21, 2009, at 11:20 AM, Pierre Bezuhov wrote:
>
> When I call firePropertyChange from my Java object after setting an
>> attribute the working memory of my engine is not updated. I know this
>> because I loop through the facts before and after the setting of the
>> attribute, and "round" is set to "0" both times.
>>
>> My Java and Jess code is below (if you're wondering why I have a class
>> just to wrap an int and string, it's to tie into some 3rd party code I don't
>> have control over). Any idea what I'm doing wrong?
>>
>> ######## JAVA CLASS ########
>> package game.server;
>>
>> import java.beans.*;
>> import java.io.*;
>> import java.nio.*;
>> import java.util.*;
>> import jess.*;
>>
>> public class Round implements Serializable
>> {
>> private static final long serialVersionUID = 1L;
>> private int round;
>> private String gameName;
>> private PropertyChangeSupport propertyChange;
>>
>> protected Round(int round, String game)
>> {
>> this.round = round;
>> gameName = game;
>> propertyChange = new PropertyChangeSupport(this);
>> }
>>
>> public int getRound()
>> {
>> return round;
>> }
>>
>> public void setRound(int round)
>> {
>> if (this.round != round)
>> {
>> int oldValue = this.round;
>> this.round = round;
>>
>> // Fire property change event so Jess working memory is updated
>> propertyChange.firePropertyChange("round", new
>> Integer(oldValue), new Integer(this.round));
>> }
>> }
>>
>> public String getGameName()
>> {
>> return gameName;
>> }
>>
>> public void setGameName(String game)
>> {
>> gameName = game;
>> }
>>
>>
>> public void addPropertyChangeListener(PropertyChangeListener listener)
>> {
>> propertyChange.addPropertyChangeListener(listener);
>> }
>>
>> public void removePropertyChangeListener(PropertyChangeListener
>> listener)
>> {
>> propertyChange.removePropertyChangeListener(listener);
>> }
>>
>> public boolean equals(Object obj)
>> {
>> boolean equal = false;
>>
>> if (obj instanceof Round)
>> {
>> Round objRound = (Round)obj;
>> equal = ((objRound.getRound() == round) &&
>> (objRound.getGameName() == gameName));
>> }
>>
>> return equal;
>> }
>>
>> public int hashCode()
>> {
>> // Game name never changes so hashCode is mutable
>> return gameName.hashCode();
>> }
>> }
>>
>>
>> ######## JESS RULES ########
>> (import game.server.*)
>>
>> (deftemplate Round (declare (from-class Round)))
>>
>> (defrule process-round
>> (Round {round >= 0} (round ?round))
>> =>
>> (printout t "Round " ?round crlf)
>> )
>>
>
> ---------------------------------------------------------
> Ernest Friedman-Hill
> Informatics & Decision Sciences, Sandia National Laboratories
> PO Box 969, MS 9012, Livermore, CA 94550
> http://www.jessrules.com
>
>
>
>
>
>
>
> --------------------------------------------------------------------
> To unsubscribe, send the words 'unsubscribe jess-users [email protected]'
> in the BODY of a message to [email protected], NOT to the list
> (use your own address!) List problems? Notify [email protected].
> --------------------------------------------------------------------
>
>