RE: [rules-users] Updating an existing fact w/o using fact handle

2008-06-02 Thread Fenderbosch, Eric
Thanks.
 
What I've done is add an insertOrUpdate method on a util class we use.
How efficient is getFactHandle?  Is it a simple HashMap lookup?  Or
would we be better off keeping our own map of handles-facts?

public static FactHandle insertOrUpdate(Object fact, boolean dynamic) {
FactHandle factHandle = workingMemory.getFactHandle(fact);
if (factHandle == null) {
return workingMemory.insert(fact, dynamic);
}
workingMemory.update(factHandle, fact);
return factHandle;
}



From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Edson Tirelli
Sent: Saturday, May 31, 2008 9:12 AM
To: Rules Users List
Subject: Re: [rules-users] Updating an existing fact w/o using fact
handle



   Nope. You must use the update method. You can get the previous fact
handle using the get method in working memory if you still have the
original non-modified object, or if behavior is equals based, using an
equals object.

   []
   Edson


2008/5/30 Fenderbosch, Eric [EMAIL PROTECTED]:


Is it required to use WorkingMemory.update to update an existing
fact?
I thought if assert behavior was set to equality and you
implemented the
equals method properly, then you could simply use
WorkingMemory.insert
to overwrite a fact in working memory with a new version.  If
this isn't
the case, then are there other settings that will give this
behavior?

I'm using 4.0.7.

Thanks for any help.

Eric

Here's my RuleBaseConfiguration:
AlphaNodeHashingThreshold : 3
CompositeKeyDepth : 3
ExecutorServiceorg.drools.concurrent.DefaultExecutorService
RuleBaseUpdateHandler :
org.drools.base.FireAllRulesRuleBaseUpdateListener
AgendaGroupFactory :
[EMAIL PROTECTED]
AssertBehaviour : equality
ConflictResolver :
[EMAIL PROTECTED]
ConsequenceExceptionHandler :
[EMAIL PROTECTED]
LogicalOverride : discard
SequentialAgenda : sequential
AlphaMemory : false
IndexLeftBetaMemory : true
IndexRightBetaMemory : true
MaintainTms : true
RemoveIdenities : true
Sequential : false
ShadowProxy : true
ShareAlphaNodes : true
ShareBetaNodes : true
UseStaticObjensis : false


My TestFact class:

public class TestFact {

   private String id;
   private String value;

   public String getId() {
   return id;
   }

   public void setId(String id) {
   this.id = id;
   }

   public String getValue() {
   return value;
   }

   public void setValue(String value) {
   this.value = value;
   }

   @Override
   public int hashCode() {
   return id.hashCode();
   }

   @Override
   public boolean equals(Object obj) {
   if (this == obj) return true;
   if (!(obj instanceof TestFact)) return false;
   TestFact other = (TestFact) obj;
   // not null safe, i know
   return this.id.equals(other.id);
   }
}

And the JUnit Test Case that fails:
   public void testFactUpdate() throws Exception {
   TestFact testFact = new TestFact();
   testFact.setId(1234);
   testFact.setValue(old);

   FactHandle testFactHandle =
workingMemory.insert(testFact);

   TestFact updatedFact = new TestFact();
   updatedFact.setId(1234);
   updatedFact.setValue(new);

   FactHandle updatedFactHandle =
workingMemory.insert(updatedFact);
   // using workingMemory.update here works

   // passes
   assertTrue(testFactHandle == updatedFactHandle);

   TestFact retrievedTestFact = (TestFact)
workingMemory.getObject(testFactHandle);
   // fails
   assertEquals(new,
retrievedTestFact.getValue());
   }

___
rules-users mailing list
rules-users@lists.jboss.org
https://lists.jboss.org/mailman/listinfo/rules-users





-- 
Edson Tirelli
JBoss Drools Core Development
Office: +55 11 3529-6000
Mobile: +55 11 9287-5646
JBoss, a division of Red Hat @ www.jboss.com

Re: [rules-users] Updating an existing fact w/o using fact handle

2008-06-02 Thread Edson Tirelli
   Yes, your implementation looks fine. It is a hashmap lookup with a few
additional checks, but I don't think it has noticeable perf difference,
since keeping an external map would also require object creation (Map.Entry,
etc) and would duplicate something that already exists internally.

   []s
   Edson


2008/6/2 Fenderbosch, Eric [EMAIL PROTECTED]:

 Thanks.

 What I've done is add an insertOrUpdate method on a util class we use.
 How efficient is getFactHandle?  Is it a simple HashMap lookup?  Or
 would we be better off keeping our own map of handles-facts?

 public static FactHandle insertOrUpdate(Object fact, boolean dynamic) {
FactHandle factHandle = workingMemory.getFactHandle(fact);
if (factHandle == null) {
return workingMemory.insert(fact, dynamic);
}
workingMemory.update(factHandle, fact);
return factHandle;
 }

 

 From: [EMAIL PROTECTED]
 [mailto:[EMAIL PROTECTED] On Behalf Of Edson Tirelli
 Sent: Saturday, May 31, 2008 9:12 AM
 To: Rules Users List
 Subject: Re: [rules-users] Updating an existing fact w/o using fact
 handle



   Nope. You must use the update method. You can get the previous fact
 handle using the get method in working memory if you still have the
 original non-modified object, or if behavior is equals based, using an
 equals object.

   []
   Edson


 2008/5/30 Fenderbosch, Eric [EMAIL PROTECTED]:


Is it required to use WorkingMemory.update to update an existing
 fact?
I thought if assert behavior was set to equality and you
 implemented the
equals method properly, then you could simply use
 WorkingMemory.insert
to overwrite a fact in working memory with a new version.  If
 this isn't
the case, then are there other settings that will give this
 behavior?

I'm using 4.0.7.

Thanks for any help.

Eric

Here's my RuleBaseConfiguration:
AlphaNodeHashingThreshold : 3
CompositeKeyDepth : 3
ExecutorServiceorg.drools.concurrent.DefaultExecutorService
RuleBaseUpdateHandler :
org.drools.base.FireAllRulesRuleBaseUpdateListener
AgendaGroupFactory :
[EMAIL PROTECTED]
AssertBehaviour : equality
ConflictResolver :
 [EMAIL PROTECTED]
ConsequenceExceptionHandler :
[EMAIL PROTECTED]
LogicalOverride : discard
SequentialAgenda : sequential
AlphaMemory : false
IndexLeftBetaMemory : true
IndexRightBetaMemory : true
MaintainTms : true
RemoveIdenities : true
Sequential : false
ShadowProxy : true
ShareAlphaNodes : true
ShareBetaNodes : true
UseStaticObjensis : false


My TestFact class:

public class TestFact {

   private String id;
   private String value;

   public String getId() {
   return id;
   }

   public void setId(String id) {
   this.id = id;
   }

   public String getValue() {
   return value;
   }

   public void setValue(String value) {
   this.value = value;
   }

   @Override
   public int hashCode() {
   return id.hashCode();
   }

   @Override
   public boolean equals(Object obj) {
   if (this == obj) return true;
   if (!(obj instanceof TestFact)) return false;
   TestFact other = (TestFact) obj;
   // not null safe, i know
   return this.id.equals(other.id);
   }
}

And the JUnit Test Case that fails:
   public void testFactUpdate() throws Exception {
   TestFact testFact = new TestFact();
   testFact.setId(1234);
   testFact.setValue(old);

   FactHandle testFactHandle =
workingMemory.insert(testFact);

   TestFact updatedFact = new TestFact();
   updatedFact.setId(1234);
   updatedFact.setValue(new);

   FactHandle updatedFactHandle =
workingMemory.insert(updatedFact);
   // using workingMemory.update here works

   // passes
   assertTrue(testFactHandle == updatedFactHandle);

   TestFact retrievedTestFact = (TestFact)
workingMemory.getObject(testFactHandle);
   // fails
   assertEquals(new,
 retrievedTestFact.getValue());
   }

___
rules-users mailing list
rules-users@lists.jboss.org
https://lists.jboss.org/mailman

Re: [rules-users] Updating an existing fact w/o using fact handle

2008-05-31 Thread Edson Tirelli
   Nope. You must use the update method. You can get the previous fact
handle using the get method in working memory if you still have the original
non-modified object, or if behavior is equals based, using an equals object.

   []
   Edson

2008/5/30 Fenderbosch, Eric [EMAIL PROTECTED]:

 Is it required to use WorkingMemory.update to update an existing fact?
 I thought if assert behavior was set to equality and you implemented the
 equals method properly, then you could simply use WorkingMemory.insert
 to overwrite a fact in working memory with a new version.  If this isn't
 the case, then are there other settings that will give this behavior?

 I'm using 4.0.7.

 Thanks for any help.

 Eric

 Here's my RuleBaseConfiguration:
 AlphaNodeHashingThreshold : 3
 CompositeKeyDepth : 3
 ExecutorServiceorg.drools.concurrent.DefaultExecutorService
 RuleBaseUpdateHandler :
 org.drools.base.FireAllRulesRuleBaseUpdateListener
 AgendaGroupFactory :
 [EMAIL PROTECTED]
 AssertBehaviour : equality
 ConflictResolver : [EMAIL PROTECTED]
 ConsequenceExceptionHandler :
 [EMAIL PROTECTED]
 LogicalOverride : discard
 SequentialAgenda : sequential
 AlphaMemory : false
 IndexLeftBetaMemory : true
 IndexRightBetaMemory : true
 MaintainTms : true
 RemoveIdenities : true
 Sequential : false
 ShadowProxy : true
 ShareAlphaNodes : true
 ShareBetaNodes : true
 UseStaticObjensis : false


 My TestFact class:

 public class TestFact {

private String id;
private String value;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}

@Override
public int hashCode() {
return id.hashCode();
}

@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (!(obj instanceof TestFact)) return false;
TestFact other = (TestFact) obj;
// not null safe, i know
return this.id.equals(other.id);
}
 }

 And the JUnit Test Case that fails:
public void testFactUpdate() throws Exception {
TestFact testFact = new TestFact();
testFact.setId(1234);
testFact.setValue(old);

FactHandle testFactHandle =
 workingMemory.insert(testFact);

TestFact updatedFact = new TestFact();
updatedFact.setId(1234);
updatedFact.setValue(new);

FactHandle updatedFactHandle =
 workingMemory.insert(updatedFact);
// using workingMemory.update here works

// passes
assertTrue(testFactHandle == updatedFactHandle);

TestFact retrievedTestFact = (TestFact)
 workingMemory.getObject(testFactHandle);
// fails
assertEquals(new, retrievedTestFact.getValue());
}

 ___
 rules-users mailing list
 rules-users@lists.jboss.org
 https://lists.jboss.org/mailman/listinfo/rules-users




-- 
Edson Tirelli
JBoss Drools Core Development
Office: +55 11 3529-6000
Mobile: +55 11 9287-5646
JBoss, a division of Red Hat @ www.jboss.com
___
rules-users mailing list
rules-users@lists.jboss.org
https://lists.jboss.org/mailman/listinfo/rules-users