[jboss-svn-commits] JBL Code SVN: r13170 - in labs/jbossrules/trunk: drools-compiler/src/test/java/org/drools/integrationtests and 3 other directories.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Fri Jul 6 13:24:55 EDT 2007
Author: tirelli
Date: 2007-07-06 13:24:55 -0400 (Fri, 06 Jul 2007)
New Revision: 13170
Added:
labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/PersonWithEquals.java
labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_equalitySupport.drl
Modified:
labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/MiscTest.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/DefaultKnowledgeHelper.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AbstractWorkingMemory.java
Log:
JBRULES-926: fixing update for equality assert behavior
Added: labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/PersonWithEquals.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/PersonWithEquals.java (rev 0)
+++ labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/PersonWithEquals.java 2007-07-06 17:24:55 UTC (rev 13170)
@@ -0,0 +1,54 @@
+package org.drools;
+
+public class PersonWithEquals {
+
+ private String name;
+ private int age;
+
+ public PersonWithEquals() {
+ }
+
+ public PersonWithEquals(String name,
+ int age) {
+ super();
+ this.name = name;
+ this.age = age;
+ }
+
+ public int getAge() {
+ return age;
+ }
+
+ public void setAge(int age) {
+ this.age = age;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public int hashCode() {
+ final int PRIME = 31;
+ int result = 1;
+ result = PRIME * result + age;
+ result = PRIME * result + ((name == null) ? 0 : name.hashCode());
+ return result;
+ }
+
+ public boolean equals(Object obj) {
+ if ( this == obj ) return true;
+ if ( obj == null ) return false;
+ if ( getClass() != obj.getClass() ) return false;
+ final PersonWithEquals other = (PersonWithEquals) obj;
+ if ( age != other.age ) return false;
+ if ( name == null ) {
+ if ( other.name != null ) return false;
+ } else if ( !name.equals( other.name ) ) return false;
+ return true;
+ }
+
+}
Modified: labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/MiscTest.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/MiscTest.java 2007-07-06 17:05:52 UTC (rev 13169)
+++ labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/MiscTest.java 2007-07-06 17:24:55 UTC (rev 13170)
@@ -50,6 +50,7 @@
import org.drools.OrderItem;
import org.drools.Person;
import org.drools.PersonInterface;
+import org.drools.PersonWithEquals;
import org.drools.Primitives;
import org.drools.QueryResults;
import org.drools.RandomNumber;
@@ -3080,5 +3081,33 @@
assertEquals( 2, results.size() );
assertEquals( "not memberOf", results.get( 1 ));
}
+
+ public void testEqualitySupport() throws Exception {
+ final PackageBuilder builder = new PackageBuilder();
+ builder.addPackageFromDrl( new InputStreamReader( getClass().getResourceAsStream( "test_equalitySupport.drl" ) ) );
+ final Package pkg = builder.getPackage();
+
+ RuleBaseConfiguration conf = new RuleBaseConfiguration();
+ conf.setAssertBehaviour( RuleBaseConfiguration.AssertBehaviour.EQUALITY );
+ final RuleBase ruleBase = getRuleBase(conf);
+ ruleBase.addPackage( pkg );
+ final WorkingMemory workingMemory = ruleBase.newStatefulSession();
+
+ final List results = new ArrayList();
+ workingMemory.setGlobal( "results",
+ results );
+
+ PersonWithEquals person = new PersonWithEquals("bob", 30);
+
+ workingMemory.insert( person );
+
+ workingMemory.fireAllRules();
+
+ assertEquals( 1, results.size() );
+ assertEquals( "mark", results.get( 0 ));
+
+ }
+
+
}
\ No newline at end of file
Added: labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_equalitySupport.drl
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_equalitySupport.drl (rev 0)
+++ labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_equalitySupport.drl 2007-07-06 17:24:55 UTC (rev 13170)
@@ -0,0 +1,21 @@
+package org.drools;
+
+global java.util.List results;
+
+rule "test equality support"
+ salience 10
+when
+ $p : PersonWithEquals( name == "bob" )
+then
+ $p.setName( "mark" );
+ results.add( $p.getName() );
+ update( $p );
+end
+
+rule "test 2"
+when
+ $p : PersonWithEquals( name == "bob" )
+then
+ results.add( "This rule should NEVER fire" );
+end
+
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/DefaultKnowledgeHelper.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/DefaultKnowledgeHelper.java 2007-07-06 17:05:52 UTC (rev 13169)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/DefaultKnowledgeHelper.java 2007-07-06 17:24:55 UTC (rev 13170)
@@ -16,18 +16,14 @@
* limitations under the License.
*/
-import java.util.List;
-
import org.drools.FactException;
import org.drools.FactHandle;
-import org.drools.QueryResults;
import org.drools.WorkingMemory;
import org.drools.common.InternalWorkingMemoryActions;
import org.drools.rule.Declaration;
import org.drools.rule.GroupElement;
import org.drools.rule.Rule;
import org.drools.spi.Activation;
-import org.drools.spi.AgendaGroup;
import org.drools.spi.KnowledgeHelper;
import org.drools.spi.Tuple;
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AbstractWorkingMemory.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AbstractWorkingMemory.java 2007-07-06 17:05:52 UTC (rev 13169)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AbstractWorkingMemory.java 2007-07-06 17:24:55 UTC (rev 13170)
@@ -84,69 +84,70 @@
// ------------------------------------------------------------
// Constants
// ------------------------------------------------------------
- protected static final Class[] ADD_REMOVE_PROPERTY_CHANGE_LISTENER_ARG_TYPES = new Class[]{PropertyChangeListener.class};
+ protected static final Class[] ADD_REMOVE_PROPERTY_CHANGE_LISTENER_ARG_TYPES = new Class[]{PropertyChangeListener.class};
// ------------------------------------------------------------
// Instance members
// ------------------------------------------------------------
- protected final long id;
+ protected final long id;
/** The arguments used when adding/removing a property change listener. */
- protected final Object[] addRemovePropertyChangeListenerArgs = new Object[]{this};
+ protected final Object[] addRemovePropertyChangeListenerArgs = new Object[]{this};
/** The actual memory for the <code>JoinNode</code>s. */
- protected final PrimitiveLongMap nodeMemories = new PrimitiveLongMap( 32,
- 8 );
+ protected final PrimitiveLongMap nodeMemories = new PrimitiveLongMap( 32,
+ 8 );
/** Global values which are associated with this memory. */
- protected Map globals = new HashMap();
+ protected Map globals = new HashMap();
/** Object-to-handle mapping. */
- private final ObjectHashMap assertMap;
+ private final ObjectHashMap assertMap;
+ private final ObjectHashMap identityMap;
- protected Map queryResults = Collections.EMPTY_MAP;
+ protected Map queryResults = Collections.EMPTY_MAP;
- protected GlobalResolver globalResolver;
+ protected GlobalResolver globalResolver;
- protected static final Object NULL = new Serializable() {
- private static final long serialVersionUID = 400L;
- };
+ protected static final Object NULL = new Serializable() {
+ private static final long serialVersionUID = 400L;
+ };
/** The eventSupport */
- protected WorkingMemoryEventSupport workingMemoryEventSupport = new WorkingMemoryEventSupport( );
+ protected WorkingMemoryEventSupport workingMemoryEventSupport = new WorkingMemoryEventSupport();
- protected AgendaEventSupport agendaEventSupport = new AgendaEventSupport( );
+ protected AgendaEventSupport agendaEventSupport = new AgendaEventSupport();
- protected RuleFlowEventSupport ruleFlowEventSupport = new RuleFlowEventSupport( );
+ protected RuleFlowEventSupport ruleFlowEventSupport = new RuleFlowEventSupport();
/** The <code>RuleBase</code> with which this memory is associated. */
- protected transient InternalRuleBase ruleBase;
+ protected transient InternalRuleBase ruleBase;
- protected final FactHandleFactory handleFactory;
+ protected final FactHandleFactory handleFactory;
- protected final TruthMaintenanceSystem tms;
+ protected final TruthMaintenanceSystem tms;
/** Rule-firing agenda. */
- protected DefaultAgenda agenda;
+ protected DefaultAgenda agenda;
- protected final List actionQueue = new ArrayList();
+ protected final List actionQueue = new ArrayList();
- protected final ReentrantLock lock = new ReentrantLock();
+ protected final ReentrantLock lock = new ReentrantLock();
- protected final boolean discardOnLogicalOverride;
+ protected final boolean discardOnLogicalOverride;
- protected long propagationIdCounter;
+ protected long propagationIdCounter;
- private final boolean maintainTms;
+ private final boolean maintainTms;
- private final boolean sequential;
+ private final boolean sequential;
- private List liaPropagations = Collections.EMPTY_LIST;
+ private List liaPropagations = Collections.EMPTY_LIST;
/** Flag to determine if a rule is currently being fired. */
- protected boolean firing;
+ protected boolean firing;
- protected boolean halt;
+ protected boolean halt;
// ------------------------------------------------------------
// Constructors
@@ -178,8 +179,11 @@
if ( conf.getAssertBehaviour() == AssertBehaviour.IDENTITY ) {
this.assertMap.setComparator( new IdentityAssertMapComparator() );
+ this.identityMap = assertMap;
} else {
this.assertMap.setComparator( new EqualityAssertMapComparator() );
+ this.identityMap = new ObjectHashMap();
+ this.identityMap.setComparator( new IdentityAssertMapComparator() );
}
// Only takes effect if are using idententity behaviour for assert
@@ -198,7 +202,7 @@
void setRuleBase(final InternalRuleBase ruleBase) {
this.ruleBase = ruleBase;
}
-
+
public void setWorkingMemoryEventSupport(WorkingMemoryEventSupport workingMemoryEventSupport) {
this.workingMemoryEventSupport = workingMemoryEventSupport;
}
@@ -571,7 +575,7 @@
public FactHandle getFactHandle(final Object object) {
try {
this.lock.lock();
- final FactHandle factHandle = (FactHandle) this.assertMap.get( object );
+ final FactHandle factHandle = (FactHandle) this.identityMap.get( object );
return factHandle;
} finally {
@@ -703,6 +707,11 @@
this.assertMap.put( handle,
handle,
false );
+ if ( this.ruleBase.getConfiguration().getAssertBehaviour() == AssertBehaviour.EQUALITY ) {
+ this.identityMap.put( handle,
+ handle,
+ false );
+ }
insert( handle,
object,
rule,
@@ -757,6 +766,12 @@
this.assertMap.put( handle,
handle,
false );
+ if ( this.ruleBase.getConfiguration().getAssertBehaviour() == AssertBehaviour.EQUALITY ) {
+ this.identityMap.put( handle,
+ handle,
+ false );
+ }
+
key = new EqualityKey( handle );
handle.setEqualityKey( key );
this.tms.put( key );
@@ -804,6 +819,13 @@
this.assertMap.put( handle,
handle,
false );
+
+ if ( this.ruleBase.getConfiguration().getAssertBehaviour() == AssertBehaviour.EQUALITY ) {
+ this.identityMap.put( handle,
+ handle,
+ false );
+ }
+
}
} else {
@@ -813,6 +835,12 @@
false );
key.addFactHandle( handle );
handle.setEqualityKey( key );
+ if ( this.ruleBase.getConfiguration().getAssertBehaviour() == AssertBehaviour.EQUALITY ) {
+ this.identityMap.put( handle,
+ handle,
+ false );
+ }
+
}
} else {
@@ -839,6 +867,12 @@
this.assertMap.put( handle,
handle,
false );
+ if ( this.ruleBase.getConfiguration().getAssertBehaviour() == AssertBehaviour.EQUALITY ) {
+ this.identityMap.put( handle,
+ handle,
+ false );
+ }
+
}
if ( dynamic ) {
@@ -1005,7 +1039,11 @@
this );
this.assertMap.remove( handle );
+ if ( this.ruleBase.getConfiguration().getAssertBehaviour() == AssertBehaviour.EQUALITY ) {
+ this.identityMap.remove( handle );
+ }
+
this.handleFactory.destroyFactHandle( handle );
if ( !this.actionQueue.isEmpty() ) {
@@ -1167,17 +1205,26 @@
this.agenda.getDormantActivations() );
doRetract( handle,
propagationContext );
-
+
if ( (originalObject != object) || (this.ruleBase.getConfiguration().getAssertBehaviour() != AssertBehaviour.IDENTITY) ) {
// as assertMap may be using an "identity" equality comparator,
// we need to remove the handle from the map, before replacing the object
// and then re-add the handle. Otherwise we may end up with a leak.
this.assertMap.remove( handle );
+ if ( this.ruleBase.getConfiguration().getAssertBehaviour() == AssertBehaviour.EQUALITY ) {
+ this.identityMap.remove( handle );
+ }
+
// set anyway, so that it updates the hashCodes
handle.setObject( object );
this.assertMap.put( handle,
handle,
false );
+ if ( this.ruleBase.getConfiguration().getAssertBehaviour() == AssertBehaviour.EQUALITY ) {
+ this.identityMap.put( handle,
+ handle,
+ false );
+ }
}
if ( this.maintainTms ) {
@@ -1346,7 +1393,8 @@
processInstance.setProcess( process );
processInstance.start();
- getRuleFlowEventSupport().fireRuleFlowProcessStarted( processInstance, this );
+ getRuleFlowEventSupport().fireRuleFlowProcessStarted( processInstance,
+ this );
return processInstance;
} else {
More information about the jboss-svn-commits
mailing list