this.entryPoint,
new ClassObjectType( String.class ),
buildContext );
objectTypeNode.attach( buildContext );
MockRightTupleSink sink = new MockRightTupleSink();
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";
}
};
// Create first justifier
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());
// get the activation onto the agenda
node.assertLeftTuple( tuple1,
context1,
workingMemory );
// Create the second justifier
final Rule rule2 = new Rule( "test-rule2" );
final RuleTerminalNode node2 = new RuleTerminalNode( idGenerator.getNextId(),
new MockTupleSource( idGenerator.getNextId() ),
rule2,
rule2.getLhs(),
0,
buildContext );
rule2.setConsequence( consequence );
final DefaultFactHandle handle2 = new DefaultFactHandle( 2,
"cheese" );
final RuleTerminalNodeLeftTuple tuple2 = new RuleTerminalNodeLeftTuple( handle2,
node2,
true );
final PropagationContext context2 = pctxFactory.createPropagationContext(0, PropagationContext.INSERTION, null, null, new DefaultFactHandle());
// get the activations onto the agenda
node2.assertLeftTuple( tuple2,
context2,
workingMemory );
// Create the first justifieable relationship
final String logicalString1 = new String( "logical" );
final InternalFactHandle logicalHandle1 = (InternalFactHandle) workingMemory.insert( logicalString1,
"value1",
false,
true,
rule1,
(Activation) tuple1.getObject() );
new RightTuple( logicalHandle1,
sink );
// Create the second justifieable relationship
final String logicalString2 = new String( "logical" );
final InternalFactHandle logicalHandle2 = (InternalFactHandle) workingMemory.insert( logicalString2,
"value2",
false,
true,
rule2,
(Activation) tuple2.getObject() );
assertSame( logicalHandle1, logicalHandle2 );
TruthMaintenanceSystem tms = ((NamedEntryPoint)workingMemory.getWorkingMemoryEntryPoint( EntryPointId.DEFAULT.getEntryPointId() ) ).getTruthMaintenanceSystem();
// "logical" should only appear once
assertEquals( 1,
getLogicalCount( tms ) );
BeliefSet bs = ( BeliefSet ) logicalHandle2.getEqualityKey().getBeliefSet();
assertEquals( "value1", ((LogicalDependency) ((LinkedListEntry)bs.getFirst()).getObject()).getValue() );
assertEquals( "value2", ((LogicalDependency) ((LinkedListEntry)bs.getFirst().getNext()).getObject()).getValue() );
// Now lets cancel the first activation
node2.retractLeftTuple( tuple2,
context2,
workingMemory );
workingMemory.executeQueuedActions();
// because this logical fact has two relationships it shouldn't retract yet
assertLength( 0,
sink.getRetracted() );
// check "logical" is still in the system
assertEquals( 1,
getLogicalCount( ( tms ) ) );
// now remove that final justification
node.retractLeftTuple( tuple1,
context1,
workingMemory );
workingMemory.executeQueuedActions();
// Should cause the logical fact to be retracted
assertLength( 1,
sink.getRetracted() );
// "logical" fact should no longer be in the system
assertEquals( 0,
getLogicalCount( tms ) );
}