private BetaNode createBetaNode(int id, int type, LeftTupleSource leftTupleSource) { MockObjectSource mockObjectSource = new MockObjectSource(8); BetaNode betaNode = null; switch (type) { case JOIN_NODE: { betaNode = new JoinNode( id, leftTupleSource, mockObjectSource, new EmptyBetaConstraints(), buildContext); break; } case EXISTS_NODE: { betaNode = new ExistsNode( id, leftTupleSource, mockObjectSource, new EmptyBetaConstraints(), buildContext); break; } case NOT_NODE: { betaNode = new NotNode( id, leftTupleSource, mockObjectSource, new EmptyBetaConstraints(), buildContext); break; } } mockObjectSource.attach(); betaNode.attach(); return betaNode; }
@Test public void testObjectSourceConstructor() { final MockObjectSource source = new MockObjectSource(15); assertEquals(15, source.getId()); assertEquals(0, source.getAttached()); source.attach(); assertEquals(1, source.getAttached()); }
@Test public void testLeftInputAdapterNode() { final MockObjectSource source = new MockObjectSource(15); final LeftInputAdapterNode liaNode = new LeftInputAdapterNode(23, source, buildContext); assertEquals(23, liaNode.getId()); assertEquals(0, source.getAttached()); source.attach(); assertEquals(1, source.getAttached()); }
@Test public void testAddObjectSink() throws Exception { final MockObjectSource source = new MockObjectSource(15); // We need to re-assign this var each time the sink changes references final Field field = ObjectSource.class.getDeclaredField("sink"); field.setAccessible(true); ObjectSinkPropagator sink = (ObjectSinkPropagator) field.get(source); assertSame(EmptyObjectSinkAdapter.getInstance(), sink); final MockObjectSink sink1 = new MockObjectSink(); source.addObjectSink(sink1); sink = (ObjectSinkPropagator) field.get(source); assertSame(SingleObjectSinkAdapter.class, sink.getClass()); assertEquals(1, sink.getSinks().length); final MockObjectSink sink2 = new MockObjectSink(); source.addObjectSink(sink2); sink = (ObjectSinkPropagator) field.get(source); assertSame(CompositeObjectSinkAdapter.class, sink.getClass()); assertEquals(2, sink.getSinks().length); final MockObjectSink sink3 = new MockObjectSink(); source.addObjectSink(sink3); assertSame(CompositeObjectSinkAdapter.class, sink.getClass()); assertEquals(3, sink.getSinks().length); source.removeObjectSink(sink2); assertSame(CompositeObjectSinkAdapter.class, sink.getClass()); assertEquals(2, sink.getSinks().length); source.removeObjectSink(sink1); sink = (ObjectSinkPropagator) field.get(source); assertSame(SingleObjectSinkAdapter.class, sink.getClass()); assertEquals(1, sink.getSinks().length); source.removeObjectSink(sink3); sink = (ObjectSinkPropagator) field.get(source); assertSame(EmptyObjectSinkAdapter.getInstance(), sink); assertEquals(0, sink.getSinks().length); }
@Test public void testUpdateSinkWithoutMemory() throws FactException, IntrospectionException { // An AlphaNode should try and repropagate from its source ReteooRuleBase ruleBase = (ReteooRuleBase) RuleBaseFactory.newRuleBase(); 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.INSERTION, null, null, null); final MockObjectSource source = new MockObjectSource(buildContext.getNextId()); final InternalReadAccessor extractor = store.getReader(Cheese.class, "type", getClass().getClassLoader()); final FieldValue field = FieldFactory.getInstance().getFieldValue("cheddar"); final MvelConstraint constraint = new MvelConstraintTestUtil("type == \"cheddar\"", field, extractor); final AlphaNode alphaNode = new AlphaNode(buildContext.getNextId(), constraint, source, buildContext); // no memory alphaNode.attach(); final MockObjectSink sink1 = new MockObjectSink(); alphaNode.addObjectSink(sink1); // Assert a single fact which should be in the AlphaNode memory and also // propagated to the // the tuple sink final Cheese cheese = new Cheese("cheddar", 0); final DefaultFactHandle handle1 = new DefaultFactHandle(1, cheese); // adding handle to the mock source source.addFact(handle1); alphaNode.assertObject(handle1, context, workingMemory); // Create a fact that should not be propagated, since the alpha node restriction will filter it // out final Cheese stilton = new Cheese("stilton", 10); final DefaultFactHandle handle2 = new DefaultFactHandle(2, stilton); // adding handle to the mock source source.addFact(handle2); alphaNode.assertObject(handle2, context, workingMemory); assertLength(1, sink1.getAsserted()); // Attach a new tuple sink final MockObjectSink sink2 = new MockObjectSink(); // Tell the alphanode to update the new node. Make sure the first sink1 // is not updated // likewise the source should not do anything alphaNode.updateSink(sink2, context, workingMemory); assertLength(1, sink1.getAsserted()); assertLength(1, sink2.getAsserted()); assertEquals(1, source.getUdated()); }
public void testUpdateSinkWithMemory() throws FactException, IntrospectionException { // An AlphaNode with memory should not try and repropagate from its source // Also it should only update the latest tuple sinky 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 alphaNode.attach(); final MockObjectSink sink1 = new MockObjectSink(); alphaNode.addObjectSink(sink1); // Assert a single fact which should be in the AlphaNode memory and also // propagated to the // the tuple sink final Cheese cheese = new Cheese("cheddar", 0); final DefaultFactHandle handle1 = new DefaultFactHandle(1, cheese); alphaNode.assertObject(handle1, context, workingMemory); // Create a fact that should not be propagated, since the alpha node restriction will filter it // out final Cheese stilton = new Cheese("stilton", 10); final DefaultFactHandle handle2 = new DefaultFactHandle(2, stilton); // adding handle to the mock source source.addFact(handle2); alphaNode.assertObject(handle2, context, workingMemory); assertLength(1, sink1.getAsserted()); // Attach a new tuple sink final MockObjectSink sink2 = new MockObjectSink(); // Tell the alphanode to update the new node. Make sure the first sink1 // is not updated // likewise the source should not do anything alphaNode.updateSink(sink2, context, workingMemory); assertLength(1, sink1.getAsserted()); assertLength(1, sink2.getAsserted()); assertEquals(0, source.getUdated()); }
@Test public void testSingleNodeinSegment() { rule1 = new Rule("rule1"); rule2 = new Rule("rule2"); rule3 = new Rule("rule3"); KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration(); kconf.setOption(RuleEngineOption.PHREAK); ruleBase = (ReteooRuleBase) RuleBaseFactory.newRuleBase((RuleBaseConfiguration) kconf); BuildContext buildContext = new BuildContext(ruleBase, ruleBase.getReteooBuilder().getIdGenerator()); MockObjectSource mockObjectSource = new MockObjectSource(8); MockTupleSource mockTupleSource = new MockTupleSource(9); // n2 is only node in it's segment ObjectTypeNode otn = new ObjectTypeNode(2, null, new ClassObjectType(String.class), buildContext); BetaNode n1 = new JoinNode( 10, new LeftInputAdapterNode(3, otn, buildContext), mockObjectSource, new EmptyBetaConstraints(), buildContext); BetaNode n2 = new JoinNode(11, n1, mockObjectSource, new EmptyBetaConstraints(), buildContext); BetaNode n3 = new JoinNode(12, n1, mockObjectSource, new EmptyBetaConstraints(), buildContext); BetaNode n4 = new JoinNode(13, n2, mockObjectSource, new EmptyBetaConstraints(), buildContext); BetaNode n5 = new JoinNode(14, n2, mockObjectSource, new EmptyBetaConstraints(), buildContext); n1.addAssociation(rule1, null); n1.addAssociation(rule2, null); n1.addAssociation(rule3, null); n2.addAssociation(rule2, null); n2.addAssociation(rule3, null); n3.addAssociation(rule1, null); n4.addAssociation(rule2, null); n5.addAssociation(rule3, null); mockObjectSource.attach(); mockTupleSource.attach(); n1.attach(); n2.attach(); n3.attach(); n4.attach(); n5.attach(); AbstractWorkingMemory wm = new AbstractWorkingMemory( 1, (ReteooRuleBase) RuleBaseFactory.newRuleBase((RuleBaseConfiguration) kconf)); createSegmentMemory(n2, wm); BetaMemory bm = (BetaMemory) wm.getNodeMemory(n1); assertNull(bm.getSegmentMemory()); bm = (BetaMemory) wm.getNodeMemory(n3); assertNull(bm.getSegmentMemory()); bm = (BetaMemory) wm.getNodeMemory(n4); assertNull(bm.getSegmentMemory()); bm = (BetaMemory) wm.getNodeMemory(n2); assertEquals(1, bm.getNodePosMaskBit()); assertEquals(1, bm.getSegmentMemory().getAllLinkedMaskTest()); }