public void testRetractObjectWithMemory() throws Exception { RuleBaseConfiguration config = new RuleBaseConfiguration(); config.setAlphaMemory(true); ReteooRuleBase ruleBase = (ReteooRuleBase) RuleBaseFactory.newRuleBase(config); BuildContext buildContext = new BuildContext(ruleBase, ((ReteooRuleBase) ruleBase).getReteooBuilder().getIdGenerator()); ReteooWorkingMemory workingMemory = (ReteooWorkingMemory) ruleBase.newStatefulSession(); final Rule rule = new Rule("test-rule"); final PropagationContext context = new PropagationContextImpl(0, PropagationContext.ASSERTION, null, null); final MockObjectSource source = new MockObjectSource(buildContext.getNextId()); final FieldExtractor extractor = cache.getExtractor(Cheese.class, "type", getClass().getClassLoader()); final FieldValue field = FieldFactory.getFieldValue("cheddar"); final Evaluator evaluator = ValueType.OBJECT_TYPE.getEvaluator(Operator.EQUAL); final LiteralConstraint constraint = new LiteralConstraint(extractor, evaluator, field); buildContext.setAlphaNodeMemoryAllowed(true); final AlphaNode alphaNode = new AlphaNode(buildContext.getNextId(), constraint, source, buildContext); // has memory final MockObjectSink sink = new MockObjectSink(); alphaNode.addObjectSink(sink); final Cheese cheddar = new Cheese("cheddar", 5); final DefaultFactHandle f0 = new DefaultFactHandle(0, cheddar); // check alpha memory is empty final AlphaMemory memory = (AlphaMemory) workingMemory.getNodeMemory(alphaNode); assertEquals(0, memory.facts.size()); // object should assert as it passes text alphaNode.assertObject(f0, context, workingMemory); assertEquals(1, memory.facts.size()); final DefaultFactHandle f1 = new DefaultFactHandle(1, "cheese"); // object should NOT retract as it doesn't exist alphaNode.retractObject(f1, context, workingMemory); assertLength(0, sink.getRetracted()); assertEquals(1, memory.facts.size()); assertTrue("Should contain 'cheddar handle'", memory.facts.contains(f0)); // object should retract as it does exist alphaNode.retractObject(f0, context, workingMemory); assertLength(1, sink.getRetracted()); assertEquals(0, memory.facts.size()); final Object[] list = (Object[]) sink.getRetracted().get(0); assertSame(f0, list[0]); }