[jboss-svn-commits] JBL Code SVN: r6562 - in labs/jbossrules/trunk/drools-core/src: main/java/org/drools/reteoo test/java/org/drools/reteoo
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Tue Oct 3 18:00:50 EDT 2006
Author: tirelli
Date: 2006-10-03 18:00:42 -0400 (Tue, 03 Oct 2006)
New Revision: 6562
Removed:
labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/AlphaNodeSwitchTest.java
Modified:
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/AlphaNode.java
labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/AlphaNodeTest.java
labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/MockObjectSource.java
Log:
JBRULES-498: fixing unit tests for AlphaNode.
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/AlphaNode.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/AlphaNode.java 2006-10-03 21:58:25 UTC (rev 6561)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/AlphaNode.java 2006-10-03 22:00:42 UTC (rev 6562)
@@ -24,7 +24,6 @@
import org.drools.common.PropagationContextImpl;
import org.drools.spi.FieldConstraint;
import org.drools.spi.PropagationContext;
-import org.drools.util.AbstractHashTable;
import org.drools.util.FactHashTable;
import org.drools.util.Iterator;
import org.drools.util.AbstractHashTable.FactEntry;
@@ -61,22 +60,39 @@
/**
* Construct an <code>AlphaNode</code> with a unique id using the provided
- * <code>FieldConstraint</code>. <code>NodeMemory</code> is optional in
- * <code>AlphaNode</code>s and is only of benefit when adding additional
- * <code>Rule</code>s at runtime.
+ * <code>FieldConstraint</code> and the given <code>ObjectSource</code>.
+ * Nodes created in this way will not have a local memory by default.
*
* @param id
* @param constraint
- * @param hasMemory
* @param objectSource
*/
AlphaNode(final int id,
final FieldConstraint constraint,
final ObjectSource objectSource) {
+ this(id, constraint, objectSource, false);
+ }
+
+ /**
+ * Construct an <code>AlphaNode</code> with a unique id using the provided
+ * <code>FieldConstraint</code> and the given <code>ObjectSource</code>.
+ * Set the boolean flag to true if the node is supposed to have local
+ * memory, or false otherwise. Memory is optional for <code>AlphaNode</code>s
+ * and is only of benefic when adding additional <code>Rule</code>s at runtime.
+ *
+ * @param id Node's ID
+ * @param constraint Node's constraints
+ * @param objectSource Node's object source
+ * @param hasMemory true if node shall be configured with local memory. False otherwise.
+ */
+ AlphaNode(final int id,
+ final FieldConstraint constraint,
+ final ObjectSource objectSource,
+ final boolean hasMemory) {
super( id );
this.constraint = constraint;
this.objectSource = objectSource;
- setHasMemory( true );
+ setHasMemory( hasMemory );
}
/**
@@ -102,7 +118,7 @@
// we are attaching this node with existing working memories
// so this node must also have memory
- this.hasMemory = true;
+ setHasMemory( true );
for ( int i = 0, length = workingMemories.length; i < length; i++ ) {
final InternalWorkingMemory workingMemory = workingMemories[i];
final PropagationContext propagationContext = new PropagationContextImpl( workingMemory.getNextPropagationIdCounter(),
Deleted: labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/AlphaNodeSwitchTest.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/AlphaNodeSwitchTest.java 2006-10-03 21:58:25 UTC (rev 6561)
+++ labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/AlphaNodeSwitchTest.java 2006-10-03 22:00:42 UTC (rev 6562)
@@ -1,171 +0,0 @@
-package org.drools.reteoo;
-
-/*
- * Copyright 2005 JBoss Inc
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-import junit.framework.Assert;
-import junit.framework.TestCase;
-
-import org.drools.Cheese;
-import org.drools.RuleBaseFactory;
-import org.drools.base.ClassFieldExtractor;
-import org.drools.base.ValueType;
-import org.drools.base.evaluators.Operator;
-import org.drools.common.DefaultFactHandle;
-import org.drools.rule.LiteralConstraint;
-import org.drools.spi.Evaluator;
-import org.drools.spi.FieldExtractor;
-import org.drools.spi.FieldValue;
-import org.drools.spi.MockField;
-
-public class AlphaNodeSwitchTest extends TestCase {
- AlphaNode alphaNode1;
- AlphaNode alphaNode2;
- LiteralConstraint constraint1;
- LiteralConstraint constraint2;
- LiteralConstraint constraint3;
- DefaultFactHandle f0;
- ReteooWorkingMemory workingMemory;
-
- protected void setUp() throws Exception {
- super.setUp();
-
- this.workingMemory = new ReteooWorkingMemory( 1,
- (ReteooRuleBase) RuleBaseFactory.newRuleBase() );
- final MockObjectSource source = new MockObjectSource( 15 );
- final MockObjectSink sink = new MockObjectSink();
-
- final FieldExtractor extractor = new ClassFieldExtractor( Cheese.class,
- "type" );
- final FieldExtractor extractor2 = new ClassFieldExtractor( Cheese.class,
- "price" );
- final FieldValue field1 = new MockField( "cheddar" );
- final FieldValue field2 = new MockField( "mussarela" );
-
- final Evaluator evaluator = ValueType.OBJECT_TYPE.getEvaluator( Operator.EQUAL );
- this.constraint1 = new LiteralConstraint( extractor,
- evaluator,
- field1 );
- this.constraint2 = new LiteralConstraint( extractor,
- evaluator,
- field2 );
- this.constraint3 = new LiteralConstraint( extractor2,
- evaluator,
- field2 );
-
- this.alphaNode1 = new AlphaNode( 2,
- this.constraint1,
- source );
- this.alphaNode1.addObjectSink( sink );
-
- this.alphaNode2 = new AlphaNode( 2,
- this.constraint2,
- source );
- this.alphaNode2.addObjectSink( sink );
-
- final Cheese cheddar = new Cheese( "mussarela",
- 5 );
-
- this.f0 = new DefaultFactHandle( 0,
- cheddar );
- }
-
- protected void tearDown() throws Exception {
- super.tearDown();
- }
-
- /*
- * Test method for 'org.drools.reteoo.AlphaNodeSwitch.hashCode()'
- */
- public void testHashCode() {
- final AlphaNodeSwitch alphaSwitch1 = new AlphaNodeSwitch( this.constraint1 );
- Assert.assertTrue( "hashCode() should be different of 0",
- alphaSwitch1.hashCode() != 0 );
-
- final AlphaNodeSwitch alphaSwitch2 = new AlphaNodeSwitch( this.constraint2 );
- Assert.assertEquals( "hashCode() should be the same",
- alphaSwitch1.hashCode(),
- alphaSwitch2.hashCode() );
-
- final AlphaNodeSwitch alphaSwitch3 = new AlphaNodeSwitch( this.constraint3 );
- Assert.assertTrue( "hashCode() should not be the same",
- alphaSwitch1.hashCode() != alphaSwitch3.hashCode() );
- }
-
- /*
- * Test method for 'org.drools.reteoo.AlphaNodeSwitch.equals(Object)'
- */
- public void testEqualsObject() {
- final AlphaNodeSwitch alphaSwitch1 = new AlphaNodeSwitch( this.constraint1 );
- final AlphaNodeSwitch alphaSwitch2 = new AlphaNodeSwitch( this.constraint2 );
- Assert.assertTrue( "equals() should return true",
- alphaSwitch1.equals( alphaSwitch2 ) );
-
- final AlphaNodeSwitch alphaSwitch3 = new AlphaNodeSwitch( this.constraint3 );
- Assert.assertFalse( "equals() should return false",
- alphaSwitch1.equals( alphaSwitch3 ) );
- }
-
- /*
- * Test method for 'org.drools.reteoo.AlphaNodeSwitch.addAlphaNode(AlphaNode)'
- */
- public void testAddAlphaNode() {
- final AlphaNodeSwitch alphaSwitch1 = new AlphaNodeSwitch( this.constraint1 );
-
- Assert.assertEquals( "AlphaSwitch should be empty",
- 0,
- alphaSwitch1.getSwitchCount() );
-
- alphaSwitch1.addAlphaNode( this.alphaNode1 );
-
- Assert.assertEquals( "AlphaSwitch should not be empty",
- 1,
- alphaSwitch1.getSwitchCount() );
- }
-
- /*
- * Test method for 'org.drools.reteoo.AlphaNodeSwitch.removeAlphaNode(AlphaNode)'
- */
- public void testRemoveAlphaNode() {
- final AlphaNodeSwitch alphaSwitch1 = new AlphaNodeSwitch( this.constraint1 );
- alphaSwitch1.addAlphaNode( this.alphaNode1 );
- Assert.assertEquals( "AlphaSwitch should not be empty",
- 1,
- alphaSwitch1.getSwitchCount() );
- alphaSwitch1.removeAlphaNode( this.alphaNode1 );
- Assert.assertEquals( "AlphaSwitch should be empty",
- 0,
- alphaSwitch1.getSwitchCount() );
- }
-
- /*
- * Test method for 'org.drools.reteoo.AlphaNodeSwitch.getNode(WorkingMemory, FactHandleImpl)'
- */
- public void testGetNode() {
- final AlphaNodeSwitch alphaSwitch1 = new AlphaNodeSwitch( this.constraint1 );
- alphaSwitch1.addAlphaNode( this.alphaNode1 );
- alphaSwitch1.addAlphaNode( this.alphaNode2 );
-
- final AlphaNode node = alphaSwitch1.getNode( this.workingMemory,
- this.f0 );
-
- Assert.assertSame( "Switch should have returned alphaNode2",
- this.alphaNode2,
- node );
-
- }
-
-}
\ No newline at end of file
Modified: labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/AlphaNodeTest.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/AlphaNodeTest.java 2006-10-03 21:58:25 UTC (rev 6561)
+++ labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/AlphaNodeTest.java 2006-10-03 22:00:42 UTC (rev 6562)
@@ -17,8 +17,6 @@
*/
import java.beans.IntrospectionException;
-import java.util.HashSet;
-import java.util.Set;
import org.drools.Cheese;
import org.drools.DroolsTestCase;
@@ -40,23 +38,6 @@
public class AlphaNodeTest extends DroolsTestCase {
-// public void testAttach() throws Exception {
-// final MockObjectSource source = new MockObjectSource( 15 );
-//
-// final AlphaNode alphaNode = new AlphaNode( 2,
-// null,
-// source );
-// assertEquals( 2,
-// alphaNode.getId() );
-// assertLength( 0,
-// source.getObjectSinksAsList() );
-// alphaNode.attach();
-// assertLength( 1,
-// source.getObjectSinksAsList() );
-// assertSame( alphaNode,
-// source.getObjectSinks().getLastObjectSink() );
-// }
-
public void testMemory() {
final ReteooWorkingMemory workingMemory = new ReteooWorkingMemory( 1,
(ReteooRuleBase) RuleBaseFactory.newRuleBase() );
@@ -94,7 +75,8 @@
// With Memory
final AlphaNode alphaNode = new AlphaNode( 2,
constraint,
- source );
+ source,
+ true ); // has memory
final MockObjectSink sink = new MockObjectSink();
alphaNode.addObjectSink( sink );
@@ -109,7 +91,7 @@
// check alpha memory is empty
final FactHashTable memory = (FactHashTable) workingMemory.getNodeMemory( alphaNode );
-
+
assertEquals( 0,
memory.size() );
@@ -148,7 +130,87 @@
assertTrue( "Should contain 'cheddar handle'",
memory.contains( f0 ) );
}
-
+
+ public void testLiteralConstraintAssertObjectWithoutMemory() throws Exception {
+ final ReteooWorkingMemory workingMemory = new ReteooWorkingMemory( 1,
+ (ReteooRuleBase) RuleBaseFactory.newRuleBase() );
+ final Rule rule = new Rule( "test-rule" );
+ final PropagationContext context = new PropagationContextImpl( 0,
+ PropagationContext.ASSERTION,
+ null,
+ null );
+
+ final MockObjectSource source = new MockObjectSource( 15 );
+
+ final ClassFieldExtractor extractor = new ClassFieldExtractor( Cheese.class,
+ "type" );
+
+ final FieldValue field = new MockField( "cheddar" );
+
+ final Evaluator evaluator = ValueType.OBJECT_TYPE.getEvaluator( Operator.EQUAL );
+ final LiteralConstraint constraint = new LiteralConstraint( extractor,
+ evaluator,
+ field );
+
+ // With Memory
+ final AlphaNode alphaNode = new AlphaNode( 2,
+ constraint,
+ source,
+ false ); // no memory
+
+ final MockObjectSink sink = new MockObjectSink();
+ alphaNode.addObjectSink( sink );
+
+ final Cheese cheddar = new Cheese( "cheddar",
+ 5 );
+ final DefaultFactHandle f0 = (DefaultFactHandle) workingMemory.assertObject( cheddar );
+
+ // check sink is empty
+ assertLength( 0,
+ sink.getAsserted() );
+
+ // check alpha memory is empty
+ final FactHashTable memory = (FactHashTable) workingMemory.getNodeMemory( alphaNode );
+
+ assertEquals( 0,
+ memory.size() );
+
+ // object should assert as it passes text
+ alphaNode.assertObject( f0,
+ context,
+ workingMemory );
+
+ assertEquals( 1,
+ sink.getAsserted().size() );
+ assertEquals( 0,
+ memory.size() );
+ Object[] list = (Object[]) sink.getAsserted().get( 0 );
+ assertSame( cheddar,
+ workingMemory.getObject( (DefaultFactHandle) list[0] ) );
+ assertFalse( "Should not contain 'cheddar handle'",
+ memory.contains( f0 ) );
+
+ final Cheese stilton = new Cheese( "stilton",
+ 6 );
+ final DefaultFactHandle f1 = new DefaultFactHandle( 1,
+ stilton );
+
+ // object should NOT assert as it does not pass test
+ alphaNode.assertObject( f1,
+ context,
+ workingMemory );
+
+ assertLength( 1,
+ sink.getAsserted() );
+ assertEquals( 0,
+ memory.size() );
+ list = (Object[]) sink.getAsserted().get( 0 );
+ assertSame( cheddar,
+ workingMemory.getObject( (DefaultFactHandle) list[0] ) );
+ assertFalse( "Should not contain 'cheddar handle'",
+ memory.contains( f0 ) );
+ }
+
/*
* dont need to test with and without memory on this, as it was already done
* on the previous two tests. This just test AlphaNode With a different
@@ -215,7 +277,7 @@
sink.getAsserted() );
}
- public void testRetractObject() throws Exception {
+ public void testRetractObjectWithMemory() throws Exception {
final ReteooWorkingMemory workingMemory = new ReteooWorkingMemory( 1,
(ReteooRuleBase) RuleBaseFactory.newRuleBase() );
final Rule rule = new Rule( "test-rule" );
@@ -238,7 +300,8 @@
final AlphaNode alphaNode = new AlphaNode( 2,
constraint,
- source );
+ source,
+ true ); // has memory
final MockObjectSink sink = new MockObjectSink();
alphaNode.addObjectSink( sink );
@@ -291,8 +354,86 @@
}
- public void testUpdateSink() throws FactException,
- IntrospectionException {
+ public void testRetractObjectWithoutMemory() throws Exception {
+ final ReteooWorkingMemory workingMemory = new ReteooWorkingMemory( 1,
+ (ReteooRuleBase) RuleBaseFactory.newRuleBase() );
+ final Rule rule = new Rule( "test-rule" );
+ final PropagationContext context = new PropagationContextImpl( 0,
+ PropagationContext.ASSERTION,
+ null,
+ null );
+
+ final MockObjectSource source = new MockObjectSource( 15 );
+
+ final FieldExtractor extractor = new ClassFieldExtractor( Cheese.class,
+ "type" );
+
+ final FieldValue field = new MockField( "cheddar" );
+
+ final Evaluator evaluator = ValueType.OBJECT_TYPE.getEvaluator( Operator.EQUAL );
+ final LiteralConstraint constraint = new LiteralConstraint( extractor,
+ evaluator,
+ field );
+
+ final AlphaNode alphaNode = new AlphaNode( 2,
+ constraint,
+ source,
+ false ); // no memory
+ final MockObjectSink sink = new MockObjectSink();
+ alphaNode.addObjectSink( sink );
+
+ final Cheese cheddar = new Cheese( "cheddar",
+ 5 );
+
+ final DefaultFactHandle f0 = new DefaultFactHandle( 0,
+ cheddar );
+
+ // check alpha memory is empty
+ final FactHashTable memory = (FactHashTable) workingMemory.getNodeMemory( alphaNode );
+ assertEquals( 0,
+ memory.size() );
+
+ // object should assert as it passes text
+ alphaNode.assertObject( f0,
+ context,
+ workingMemory );
+
+ assertEquals( 0,
+ memory.size() );
+
+ final DefaultFactHandle f1 = new DefaultFactHandle( 1,
+ "cheese" );
+
+ // object should NOT retract as it doesn't exist
+ alphaNode.retractObject( f1,
+ context,
+ workingMemory );
+
+ // without memory, it will always propagate a retract
+ assertLength( 1,
+ sink.getRetracted() );
+ assertEquals( 0,
+ memory.size() );
+ assertFalse( "Should not contain 'cheddar handle'",
+ memory.contains( f0 ) );
+
+ // object should retract as it does exist
+ alphaNode.retractObject( f0,
+ context,
+ workingMemory );
+
+ assertLength( 2,
+ sink.getRetracted() );
+ assertEquals( 0,
+ memory.size() );
+ final Object[] list = (Object[]) sink.getRetracted().get( 1 );
+ assertSame( f0,
+ list[0] );
+
+ }
+
+ public void testUpdateSinkWithMemory() throws FactException,
+ IntrospectionException {
// An AlphaNode with memory should not try and repropagate from its source
// Also it should only update the latest tuple sinky
@@ -318,7 +459,8 @@
final AlphaNode alphaNode = new AlphaNode( 2,
constraint,
- source );
+ source,
+ true ); // has memory
alphaNode.attach();
@@ -333,7 +475,9 @@
final DefaultFactHandle handle1 = new DefaultFactHandle( 1,
cheese );
- alphaNode.assertObject( handle1, context, workingMemory );
+ alphaNode.assertObject( handle1,
+ context,
+ workingMemory );
assertLength( 1,
sink1.getAsserted() );
@@ -346,7 +490,7 @@
// likewise the source should not do anything
alphaNode.updateSink( sink2,
context,
- workingMemory);
+ workingMemory );
assertLength( 1,
sink1.getAsserted() );
@@ -355,4 +499,73 @@
assertEquals( 0,
source.getUdated() );
}
+
+ public void testUpdateSinkWithoutMemory() throws FactException,
+ IntrospectionException {
+ // An AlphaNode without memory should try and repropagate from its source
+ final ReteooWorkingMemory workingMemory = new ReteooWorkingMemory( 1,
+ (ReteooRuleBase) RuleBaseFactory.newRuleBase() );
+ final Rule rule = new Rule( "test-rule" );
+ final PropagationContext context = new PropagationContextImpl( 0,
+ PropagationContext.ASSERTION,
+ null,
+ null );
+
+ final MockObjectSource source = new MockObjectSource( 1 );
+
+ final FieldExtractor extractor = new ClassFieldExtractor( Cheese.class,
+ "type" );
+
+ final FieldValue field = new MockField( "cheddar" );
+
+ final Evaluator evaluator = ValueType.OBJECT_TYPE.getEvaluator( Operator.EQUAL );
+ final LiteralConstraint constraint = new LiteralConstraint( extractor,
+ evaluator,
+ field );
+
+ final AlphaNode alphaNode = new AlphaNode( 2,
+ constraint,
+ source,
+ false ); // no memory
+
+ alphaNode.attach();
+
+ final MockObjectSink sink1 = new MockObjectSink();
+ alphaNode.addObjectSink( sink1 );
+
+ // Assert a single fact which should be in the AlphaNode memory and also
+ // propagated to the
+ // the tuple sink
+ final Cheese cheese = new Cheese( "cheddar",
+ 0 );
+ final DefaultFactHandle handle1 = new DefaultFactHandle( 1,
+ cheese );
+ // adding handle to the mock source
+ source.addFact( handle1 );
+
+ alphaNode.assertObject( handle1,
+ context,
+ workingMemory );
+
+ assertLength( 1,
+ sink1.getAsserted() );
+
+ // Attach a new tuple sink
+ final MockObjectSink sink2 = new MockObjectSink();
+
+ // Tell the alphanode to update the new node. Make sure the first sink1
+ // is not updated
+ // likewise the source should not do anything
+ alphaNode.updateSink( sink2,
+ context,
+ workingMemory );
+
+ assertLength( 1,
+ sink1.getAsserted() );
+ assertLength( 1,
+ sink2.getAsserted() );
+ assertEquals( 1,
+ source.getUdated() );
+ }
+
}
\ No newline at end of file
Modified: labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/MockObjectSource.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/MockObjectSource.java 2006-10-03 21:58:25 UTC (rev 6561)
+++ labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/MockObjectSource.java 2006-10-03 22:00:42 UTC (rev 6562)
@@ -16,7 +16,12 @@
* limitations under the License.
*/
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
import org.drools.common.BaseNode;
+import org.drools.common.InternalFactHandle;
import org.drools.common.InternalWorkingMemory;
import org.drools.spi.PropagationContext;
@@ -29,9 +34,12 @@
private int attached;
private int updated;
+
+ private List facts;
public MockObjectSource(final int id) {
super( id );
+ this.facts = new ArrayList();
}
public void attach() {
@@ -46,11 +54,19 @@
public int getUdated() {
return this.updated;
}
+
+ public void addFact(InternalFactHandle handle) {
+ this.facts.add( handle );
+ }
public void updateSink(final ObjectSink sink,
final PropagationContext context,
final InternalWorkingMemory workingMemory) {
this.updated++;
+ for(Iterator it = this.facts.iterator(); it.hasNext(); ) {
+ InternalFactHandle handle = (InternalFactHandle) it.next();
+ sink.assertObject( handle, context, workingMemory );
+ }
}
public void remove(final BaseNode node,
More information about the jboss-svn-commits
mailing list