@Test
public void testRetract() throws Exception {
// create a RuleBase with a single ObjectTypeNode we attach a
// MockObjectSink so we can detect assertions and retractions
final RuleImpl rule1 = new RuleImpl( "test-rule1" );
IdGenerator idGenerator = kBase.getReteooBuilder().getIdGenerator();
final Rete rete = kBase.getRete();
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 );
StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl)kBase.newStatefulKnowledgeSession();
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 the first activation which will justify the fact "logical"
rule1.setConsequence( consequence );
final DefaultFactHandle handle1 = new DefaultFactHandle( 1,
"cheese" );
final RuleTerminalNodeLeftTuple tuple1 = new RuleTerminalNodeLeftTuple( handle1,
node,
true );
final PropagationContext context = pctxFactory.createPropagationContext(0, PropagationContext.INSERTION, null, null, new DefaultFactHandle());
node.assertLeftTuple( tuple1,
context,
ksession );
// Assert the logical "logical" fact
final String logicalString1 = new String( "logical" );
final FactHandle logicalHandle1 = ksession.insert( logicalString1,
null,
false,
true,
rule1,
(Activation) tuple1.getObject() );
// create the second activation to justify the "logical" fact
final RuleImpl rule2 = new RuleImpl( "test-rule2" );
final RuleTerminalNode node2 = new RuleTerminalNode( idGenerator.getNextId(),
new MockTupleSource( 3 ),
rule2,
rule2.getLhs(),
0,
buildContext );