/* Create an initial rete with just two object type nodes */
this.rete = new Rete();
this.workingMemory = new WorkingMemoryImpl( new RuleBaseImpl( this.rete ) );
this.objectTypeNode = this.rete.getOrCreateObjectTypeNode( new ClassObjectType( Object.class ) );
this.objectTypeNode.addObjectSink( new MockObjectSink() );
this.stringTypeNode = this.rete.getOrCreateObjectTypeNode( new ClassObjectType( String.class ) );
this.stringTypeNode.addObjectSink( new MockObjectSink() );
this.rete.ruleAdded();
/* Create three objects and assert them */
Object object1 = new Object();
String string1 = "cheese";
String string2 = "bread";
FactHandleImpl handle1 = new FactHandleImpl( 1 );
FactHandleImpl handle2 = new FactHandleImpl( 2 );
FactHandleImpl handle3 = new FactHandleImpl( 3 );
this.workingMemory.putObject( handle1,
object1 );
this.workingMemory.putObject( handle2,
string1 );
this.workingMemory.putObject( handle3,
string2 );
this.rete.assertObject( object1,
handle1,
context,
this.workingMemory );
this.rete.assertObject( string1,
handle2,
context,
this.workingMemory );
this.rete.assertObject( string2,
handle3,
context,
this.workingMemory );
/* check object was asserted correctly */
MockObjectSink sink1 = (MockObjectSink) this.objectTypeNode.getObjectSinks().get( 0 );
assertLength( 3,
sink1.getAsserted() );
/* check string was asserted correctly */
MockObjectSink sink2 = (MockObjectSink) this.stringTypeNode.getObjectSinks().get( 0 );
assertLength( 2,
sink2.getAsserted() );
/* Now lets try adding a new "rule". We simulate this by getting/creating the string ObjectTypeNode
* and then adding a tuplesink - at this point its obvlivious that the ObjectTypeNode already exists
*/
this.stringTypeNode = this.rete.getOrCreateObjectTypeNode( new ClassObjectType( String.class ) );
this.stringTypeNode.addObjectSink( new MockObjectSink() );
assertLength( 2,
this.stringTypeNode.getObjectSinks() );
MockObjectSink sink3 = (MockObjectSink) this.stringTypeNode.getObjectSinks().get( 1 );
/* TupleSink is added but has no assertions */
assertLength( 0,
sink3.getAsserted() );
/* Lets add a Cheese ObjectTypeNode just to see show what happens when a totally new ObjectTypeNode is asserted */
this.cheeseTypeNode = this.rete.getOrCreateObjectTypeNode( new ClassObjectType( Cheese.class ) );
this.cheeseTypeNode.addObjectSink( new MockObjectSink() );
/* And ofcourse its got zero assertions */
MockObjectSink sink4 = (MockObjectSink) this.cheeseTypeNode.getObjectSinks().get( 0 );
assertLength( 0,