@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()); }
public void testQueryTerminalNode() { final ClassObjectType queryObjectType = new ClassObjectType(DroolsQuery.class); final ObjectTypeNode queryObjectTypeNode = new ObjectTypeNode(this.buildContext.getNextId(), queryObjectType, buildContext); queryObjectTypeNode.attach(); ClassFieldExtractor extractor = cache.getExtractor(DroolsQuery.class, "name", DroolsQuery.class.getClassLoader()); FieldValue field = FieldFactory.getFieldValue("query-1"); final Evaluator evaluator = ValueType.STRING_TYPE.getEvaluator(Operator.EQUAL); LiteralConstraint constraint = new LiteralConstraint(extractor, evaluator, field); AlphaNode alphaNode = new AlphaNode(this.buildContext.getNextId(), constraint, queryObjectTypeNode, buildContext); alphaNode.attach(); final LeftInputAdapterNode liaNode = new LeftInputAdapterNode(this.buildContext.getNextId(), alphaNode, this.buildContext); liaNode.attach(); final ClassObjectType cheeseObjectType = new ClassObjectType(Cheese.class); final ObjectTypeNode cheeseObjectTypeNode = new ObjectTypeNode(this.buildContext.getNextId(), cheeseObjectType, buildContext); cheeseObjectTypeNode.attach(); extractor = cache.getExtractor(Cheese.class, "type", getClass().getClassLoader()); field = FieldFactory.getFieldValue("stilton"); constraint = new LiteralConstraint(extractor, evaluator, field); alphaNode = new AlphaNode( this.buildContext.getNextId(), constraint, cheeseObjectTypeNode, buildContext); alphaNode.attach(); BuildContext buildContext = new BuildContext(ruleBase, ruleBase.getReteooBuilder().getIdGenerator()); buildContext.setTupleMemoryEnabled(false); final JoinNode joinNode = new JoinNode( this.buildContext.getNextId(), liaNode, alphaNode, EmptyBetaConstraints.getInstance(), buildContext); joinNode.attach(); final Query query = new Query("query-1"); final QueryTerminalNode queryNode = new QueryTerminalNode(this.buildContext.getNextId(), joinNode, query, query.getLhs()); queryNode.attach(); final org.drools.rule.Package pkg = new org.drools.rule.Package("com.drools.test"); pkg.addRule(query); try { final Field pkgField = ruleBase.getClass().getSuperclass().getDeclaredField("pkgs"); pkgField.setAccessible(true); final Map pkgs = (Map) pkgField.get(ruleBase); pkgs.put(pkg.getName(), pkg); } catch (final Exception e) { Assert.fail("Should not throw any exception: " + e.getMessage()); } final WorkingMemory workingMemory = ruleBase.newStatefulSession(); QueryResults results = workingMemory.getQueryResults("query-1"); assertEquals(0, results.size()); final Cheese stilton1 = new Cheese("stilton", 100); final FactHandle handle1 = workingMemory.insert(stilton1); results = workingMemory.getQueryResults("query-1"); assertEquals(1, results.size()); final Cheese cheddar = new Cheese("cheddar", 55); workingMemory.insert(cheddar); results = workingMemory.getQueryResults("query-1"); assertEquals(1, results.size()); final Cheese stilton2 = new Cheese("stilton", 5); final FactHandle handle2 = workingMemory.insert(stilton2); results = workingMemory.getQueryResults("query-1"); assertEquals(2, results.size()); QueryResult result = results.get(0); assertEquals(1, result.size()); assertEquals(stilton2, result.get(0)); result = results.get(1); assertEquals(1, result.size()); assertEquals(stilton1, result.get(0)); int i = 0; for (final Iterator it = results.iterator(); it.hasNext(); ) { result = (QueryResult) it.next(); assertEquals(1, result.size()); if (i == 1) { assertSame(stilton1, result.get(0)); } else { assertSame(stilton2, result.get(0)); } i++; } workingMemory.retract(handle1); results = workingMemory.getQueryResults("query-1"); assertEquals(1, results.size()); workingMemory.retract(handle2); results = workingMemory.getQueryResults("query-1"); assertEquals(0, results.size()); }
@Test public void testQueryTerminalNode() { final ClassObjectType queryObjectType = new ClassObjectType(DroolsQuery.class); final ObjectTypeNode queryObjectTypeNode = new ObjectTypeNode( this.buildContext.getNextId(), this.entryPoint, queryObjectType, buildContext); queryObjectTypeNode.attach(buildContext); ClassFieldReader extractor = store.getReader(DroolsQuery.class, "name"); MvelConstraint constraint = new MvelConstraintTestUtil( "name == \"query-1\"", FieldFactory.getInstance().getFieldValue("query-1"), extractor); final QueryImpl query = new QueryImpl("query-1"); buildContext.setRule(query); AlphaNode alphaNode = new AlphaNode(this.buildContext.getNextId(), constraint, queryObjectTypeNode, buildContext); alphaNode.attach(buildContext); final LeftInputAdapterNode liaNode = new LeftInputAdapterNode(this.buildContext.getNextId(), alphaNode, this.buildContext); liaNode.attach(buildContext); final ClassObjectType cheeseObjectType = new ClassObjectType(Cheese.class); final ObjectTypeNode cheeseObjectTypeNode = new ObjectTypeNode( this.buildContext.getNextId(), this.entryPoint, cheeseObjectType, buildContext); cheeseObjectTypeNode.attach(buildContext); extractor = store.getReader(Cheese.class, "type"); constraint = new MvelConstraintTestUtil( "type == \"stilton\"", FieldFactory.getInstance().getFieldValue("stilton"), extractor); alphaNode = new AlphaNode( this.buildContext.getNextId(), constraint, cheeseObjectTypeNode, buildContext); alphaNode.attach(buildContext); BuildContext buildContext = new BuildContext(kBase, kBase.getReteooBuilder().getIdGenerator()); buildContext.setTupleMemoryEnabled(false); final JoinNode joinNode = new JoinNode( this.buildContext.getNextId(), liaNode, alphaNode, EmptyBetaConstraints.getInstance(), buildContext); joinNode.attach(buildContext); final QueryTerminalNode queryNode = new QueryTerminalNode( this.buildContext.getNextId(), joinNode, query, ((QueryImpl) query).getLhs(), 0, buildContext); queryNode.attach(buildContext); final KnowledgePackageImpl pkg = new KnowledgePackageImpl("com.drools.test"); pkg.addRule(query); ((KnowledgeBaseImpl) kBase).addPackages(Arrays.asList(new InternalKnowledgePackage[] {pkg})); KieSession kSession = kBase.newKieSession(); QueryResults results = kSession.getQueryResults("query-1"); assertEquals(0, results.size()); final Cheese stilton1 = new Cheese("stilton", 100); final FactHandle handle1 = kSession.insert(stilton1); results = kSession.getQueryResults("query-1"); assertEquals(1, results.size()); final Cheese cheddar = new Cheese("cheddar", 55); kSession.insert(cheddar); results = kSession.getQueryResults("query-1"); assertEquals(1, results.size()); final Cheese stilton2 = new Cheese("stilton", 5); final FactHandle handle2 = kSession.insert(stilton2); results = kSession.getQueryResults("query-1"); assertEquals(2, results.size()); /** * QueryResultsRow result = results.get( 0 ); assertEquals( 1, result.size() ); assertEquals( * stilton1, result.get( 0 ) ); * * <p>result = results.get( 1 ); assertEquals( 1, result.size() ); assertEquals( stilton2, * result.get( 0 ) ); */ int i = 0; for (final Iterator<QueryResultsRow> it = results.iterator(); it.hasNext(); ) { QueryResultsRow resultRow = it.next(); if (i == 1) { // assertSame( stilton2, result.g( 0 ) ); } else { // assertSame( stilton1, result.get( 0 ) ); } i++; } kSession.delete(handle1); results = kSession.getQueryResults("query-1"); assertEquals(1, results.size()); kSession.delete(handle2); results = kSession.getQueryResults("query-1"); assertEquals(0, results.size()); }