public void testRetract() throws Exception {
// create a RuleBase with a single ObjectTypeNode we attach a
// MockObjectSink so we can detect assertions and retractions
final Rule rule1 = new Rule( "test-rule1" );
ReteooRuleBase ruleBase = (ReteooRuleBase) RuleBaseFactory.newRuleBase();
IdGenerator idGenerator = ruleBase.getReteooBuilder().getIdGenerator();
final Rete rete = ruleBase.getRete();
final ObjectTypeNode objectTypeNode = new ObjectTypeNode( idGenerator.getNextId(),
new ClassObjectType( String.class ),
rete,
3 );
objectTypeNode.attach();
final MockObjectSink sink = new MockObjectSink();
objectTypeNode.addObjectSink( sink );
final RuleTerminalNode node = new RuleTerminalNode( idGenerator.getNextId(),
new MockTupleSource( idGenerator.getNextId() ),
rule1,
rule1.getLhs() );
final ReteooWorkingMemory workingMemory = (ReteooWorkingMemory) ruleBase.newStatefulSession();
final Consequence consequence = new Consequence() {
/**
*
*/
private static final long serialVersionUID = 400L;
public void evaluate(KnowledgeHelper knowledgeHelper,
WorkingMemory workingMemory) {
// do nothing
}
};
// create the first activation which will justify the fact "logical"
rule1.setConsequence( consequence );
final DefaultFactHandle handle1 = new DefaultFactHandle( 1,
"cheese" );
final ReteTuple tuple1 = new ReteTuple( handle1 );
final PropagationContext context = new PropagationContextImpl( 0,
PropagationContext.ASSERTION,
null,
null );
node.assertTuple( tuple1,
context,
workingMemory );
// Assert the logical "logical" fact
final String logicalString1 = new String( "logical" );
final FactHandle logicalHandle1 = workingMemory.insert( logicalString1,
false,
true,
rule1,
tuple1.getActivation() );
// create the second activation to justify the "logical" fact
final Rule rule2 = new Rule( "test-rule2" );
final RuleTerminalNode node2 = new RuleTerminalNode( idGenerator.getNextId(),
new MockTupleSource( 3 ),
rule2,
rule2.getLhs() );
rule2.setConsequence( consequence );