buildContext );
objectTypeNode.attach( buildContext );
MockRightTupleSink sink = new MockRightTupleSink();
final RuleTerminalNode node = new RuleTerminalNode( idGenerator.getNextId(),
new MockTupleSource( idGenerator.getNextId() ),
rule1,
rule1.getLhs(),
0,
buildContext );
StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl)kBase.newStatefulKnowledgeSession();
final Agenda agenda = ksession.getAgenda();
final Consequence consequence = new Consequence() {
private static final long serialVersionUID = 510l;
public void evaluate(KnowledgeHelper knowledgeHelper,
WorkingMemory workingMemory) {
LinkedList< LogicalDependency<M>> list = ((DefaultKnowledgeHelper)knowledgeHelper).getpreviousJustified();
if ( list != null ) {
for ( SimpleLogicalDependency dep = ( SimpleLogicalDependency ) list.getFirst(); dep != null; dep = ( SimpleLogicalDependency ) dep.getNext() ){
knowledgeHelper.insertLogical( dep.getObject(), dep.getMode() );
}
}
}
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,
ksession );
// Create the second justifier
final RuleImpl rule2 = new RuleImpl( "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,
ksession );
// Create the first justifieable relationship
final String logicalString1 = new String( "logical" );
final InternalFactHandle logicalHandle1 = (InternalFactHandle) ksession.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) ksession.insert( logicalString2,
"value2",
false,
true,
rule2,
(Activation) tuple2.getObject() );
assertSame( logicalHandle1, logicalHandle2 );
TruthMaintenanceSystem tms = ((NamedEntryPoint)ksession.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()).getMode() );
assertEquals( "value2", ((LogicalDependency) ((LinkedListEntry)bs.getFirst().getNext()).getObject()).getMode() );
// Now lets cancel the first activation
node2.retractLeftTuple( tuple2,
context2,
ksession );
ksession.executeQueuedActions();