final ObjectTypeNode objectTypeNode = new ObjectTypeNode( idGenerator.getNextId(),
this.entryPoint,
new ClassObjectType( String.class ),
buildContext );
objectTypeNode.attach( buildContext );
final MockObjectSink sink = new MockObjectSink();
objectTypeNode.addObjectSink( sink );
final RuleTerminalNode node = new RuleTerminalNode( idGenerator.getNextId(),
new MockTupleSource( idGenerator.getNextId() ),
rule1,
rule1.getLhs(),
0,
buildContext );
final AbstractWorkingMemory workingMemory = (AbstractWorkingMemory) ruleBase.newStatefulSession();
final Agenda agenda = workingMemory.getAgenda();
final Consequence consequence = new Consequence() {
private static final long serialVersionUID = 510l;
public void evaluate(KnowledgeHelper knowledgeHelper,
WorkingMemory workingMemory) {
LinkedList< LogicalDependency > list = ((DefaultKnowledgeHelper)knowledgeHelper).getpreviousJustified();
if ( list != null ) {
for ( SimpleLogicalDependency dep = ( SimpleLogicalDependency ) list.getFirst(); dep != null; dep = ( SimpleLogicalDependency ) dep.getNext() ){
knowledgeHelper.insertLogical( dep.getObject(), dep.getValue() );
}
}
}
public void readExternal(ObjectInput in) throws IOException,
ClassNotFoundException {
}
public void writeExternal(ObjectOutput out) throws IOException {
}
public String getName() {
return "default";
}
};
rule1.setConsequence( consequence );
final DefaultFactHandle handle1 = new DefaultFactHandle( 1,
"cheese" );
final RuleTerminalNodeLeftTuple tuple1 = new RuleTerminalNodeLeftTuple( handle1,
node,
true );
final PropagationContext context1 = pctxFactory.createPropagationContext(0, PropagationContext.INSERTION, null, null, new DefaultFactHandle());
// Test that a STATED assertion overrides a logical assertion
node.assertLeftTuple( tuple1,
context1,
workingMemory );
String logicalString1 = new String( "logical" );
FactHandle logicalHandle1 = workingMemory.insert( logicalString1,
null,
false,
true,
rule1,
(Activation) tuple1.getObject() );
// This assertion is stated and should override any previous justified
// "equals" objects.
String logicalString2 = new String( "logical" );
FactHandle logicalHandle2 = workingMemory.insert( logicalString2 );
node.retractLeftTuple( tuple1,
context1,
workingMemory );
assertLength( 0,
sink.getRetracted() );
// we override and discard the original logical object
assertSame( logicalHandle2,
logicalHandle1 );
// so while new STATED assertion is equal
assertEquals( logicalString1,
workingMemory.getObject( logicalHandle2 ) );
// they are not identity same
assertNotSame( logicalString1,
workingMemory.getObject( logicalHandle2 ) );
// Test that a logical assertion cannot override a STATED assertion
node.assertLeftTuple( tuple1,
context1,
workingMemory );
logicalString2 = new String( "logical" );
logicalHandle2 = workingMemory.insert( logicalString2 );
// This logical assertion will be ignored as there is already
// an equals STATED assertion.
logicalString1 = new String( "logical" );
logicalHandle1 = workingMemory.insert( logicalString1,
null,
false,
true,
rule1,
(Activation) tuple1.getObject() );
assertNull( logicalHandle1 );
// Already identify same so return previously assigned handle
logicalHandle1 = workingMemory.insert( logicalString2,
null,
false,
false,
rule1,
(Activation) tuple1.getObject() );
// return the matched handle
assertSame( logicalHandle2,
logicalHandle1 );
node.retractLeftTuple( tuple1,
context1,
workingMemory );
assertLength( 0,
sink.getRetracted() );
// Should keep the same handle when overriding
assertSame( logicalHandle1,
logicalHandle2 );