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

Fenderbosch, Eric Eric.Fenderbosch at fedex.com
Fri May 30 12:17:51 EDT 2008


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 at 17653ae
AssertBehaviour : equality
ConflictResolver : org.drools.conflict.DepthConflictResolver at 16fe0f4
ConsequenceExceptionHandler :
org.drools.base.DefaultConsequenceExceptionHandler at 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());
	}




More information about the rules-users mailing list