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 <Eric.Fenderbosch@fedex.com>:
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: rules-users-bounces@lists.jboss.org
[mailto:rules-users-bounces@lists.jboss.org] 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 <Eric.Fenderbosch@fedex.com>:


       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 :
       org.drools.common.PriorityQueueAgendaGroupFactory@17653ae
       AssertBehaviour : equality
       ConflictResolver :
org.drools.conflict.DepthConflictResolver@16fe0f4
       ConsequenceExceptionHandler :
       org.drools.base.DefaultConsequenceExceptionHandler@19d0a1
       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



--
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