Search in sources :

Example 1 with MockObjectSink

use of org.drools.core.reteoo.MockObjectSink in project drools by kiegroup.

the class AlphaNodeTest method testLiteralConstraintAssertObjectWithoutMemory.

@Test
public void testLiteralConstraintAssertObjectWithoutMemory() throws Exception {
    InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
    BuildContext buildContext = new BuildContext(kBase, Collections.emptyList());
    buildContext.setRule(new RuleImpl("test"));
    StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
    final RuleImpl rule = new RuleImpl("test-rule");
    PropagationContextFactory pctxFactory = RuntimeComponentFactory.get().getPropagationContextFactory();
    final PropagationContext context = pctxFactory.createPropagationContext(0, PropagationContext.Type.INSERTION, null, null, null);
    final MockObjectSource source = new MockObjectSource(buildContext.getNextNodeId());
    AlphaNodeFieldConstraint constraint = ConstraintTestUtil.createCheeseTypeEqualsConstraint(store, "cheddar", useLambdaConstraint);
    // With Memory
    final AlphaNode alphaNode = new AlphaNode(buildContext.getNextNodeId(), constraint, source, // no memory
    buildContext);
    final MockObjectSink sink = new MockObjectSink();
    alphaNode.addObjectSink(sink);
    final Cheese cheddar = new Cheese("cheddar", 5);
    final DefaultFactHandle f0 = (DefaultFactHandle) ksession.insert(cheddar);
    // check sink is empty
    assertLength(0, sink.getAsserted());
    // object should assert as it passes text
    alphaNode.assertObject(f0, context, ksession);
    Assert.assertEquals(1, sink.getAsserted().size());
    Object[] list = (Object[]) sink.getAsserted().get(0);
    assertSame(cheddar, ksession.getObject((DefaultFactHandle) list[0]));
    final Cheese stilton = new Cheese("stilton", 6);
    final DefaultFactHandle f1 = new DefaultFactHandle(1, stilton);
    // object should NOT assert as it does not pass test
    alphaNode.assertObject(f1, context, ksession);
    assertLength(1, sink.getAsserted());
    list = (Object[]) sink.getAsserted().get(0);
    assertSame(cheddar, ksession.getObject((DefaultFactHandle) list[0]));
}
Also used : PropagationContextFactory(org.drools.core.common.PropagationContextFactory) PropagationContext(org.drools.core.spi.PropagationContext) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) Cheese(org.drools.mvel.model.Cheese) MockObjectSource(org.drools.mvel.model.MockObjectSource) MockObjectSink(org.drools.core.reteoo.MockObjectSink) AlphaNode(org.drools.core.reteoo.AlphaNode) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) BuildContext(org.drools.core.reteoo.builder.BuildContext) StatefulKnowledgeSessionImpl(org.drools.kiesession.session.StatefulKnowledgeSessionImpl) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) Test(org.junit.Test)

Example 2 with MockObjectSink

use of org.drools.core.reteoo.MockObjectSink in project drools by kiegroup.

the class AlphaNodeTest method testUpdateSinkWithoutMemory.

@Test
public void testUpdateSinkWithoutMemory() {
    // An AlphaNode should try and repropagate from its source
    InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
    BuildContext buildContext = new BuildContext(kBase, Collections.emptyList());
    buildContext.setRule(new RuleImpl("test"));
    StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
    final RuleImpl rule = new RuleImpl("test-rule");
    PropagationContextFactory pctxFactory = RuntimeComponentFactory.get().getPropagationContextFactory();
    final PropagationContext context = pctxFactory.createPropagationContext(0, PropagationContext.Type.INSERTION, null, null, null);
    final MockObjectSource source = new MockObjectSource(buildContext.getNextNodeId());
    AlphaNodeFieldConstraint constraint = ConstraintTestUtil.createCheeseTypeEqualsConstraint(store, "cheddar", useLambdaConstraint);
    final AlphaNode alphaNode = new AlphaNode(buildContext.getNextNodeId(), constraint, source, // no memory
    buildContext);
    alphaNode.attach(buildContext);
    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, ksession);
    // 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, ksession);
    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, ksession);
    assertLength(1, sink1.getAsserted());
    assertLength(1, sink2.getAsserted());
    Assert.assertEquals(1, source.getUdated());
}
Also used : PropagationContextFactory(org.drools.core.common.PropagationContextFactory) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) BuildContext(org.drools.core.reteoo.builder.BuildContext) PropagationContext(org.drools.core.spi.PropagationContext) StatefulKnowledgeSessionImpl(org.drools.kiesession.session.StatefulKnowledgeSessionImpl) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) Cheese(org.drools.mvel.model.Cheese) MockObjectSource(org.drools.mvel.model.MockObjectSource) MockObjectSink(org.drools.core.reteoo.MockObjectSink) AlphaNode(org.drools.core.reteoo.AlphaNode) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) Test(org.junit.Test)

Example 3 with MockObjectSink

use of org.drools.core.reteoo.MockObjectSink in project drools by kiegroup.

the class AlphaNodeTest method testReturnValueConstraintAssertObject.

/*
     *  This just test AlphaNode With a different Constraint type.
     */
@Test
public void testReturnValueConstraintAssertObject() throws Exception {
    InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
    BuildContext buildContext = new BuildContext(kBase, Collections.emptyList());
    buildContext.setRule(new RuleImpl("test"));
    StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
    final RuleImpl rule = new RuleImpl("test-rule");
    PropagationContextFactory pctxFactory = RuntimeComponentFactory.get().getPropagationContextFactory();
    final PropagationContext context = pctxFactory.createPropagationContext(0, PropagationContext.Type.INSERTION, null, null, null);
    final MockObjectSource source = new MockObjectSource(buildContext.getNextNodeId());
    AlphaNodeFieldConstraint constraint = ConstraintTestUtil.createCheeseTypeEqualsConstraint(store, "cheddar", useLambdaConstraint);
    final AlphaNode alphaNode = new AlphaNode(buildContext.getNextNodeId(), constraint, source, buildContext);
    final MockObjectSink sink = new MockObjectSink();
    alphaNode.addObjectSink(sink);
    final Cheese cheddar = new Cheese("cheddar", 5);
    final DefaultFactHandle f0 = (DefaultFactHandle) ksession.insert(cheddar);
    assertLength(0, sink.getAsserted());
    // object should assert as it passes text
    alphaNode.assertObject(f0, context, ksession);
    assertLength(1, sink.getAsserted());
    final Object[] list = (Object[]) sink.getAsserted().get(0);
    assertSame(cheddar, ksession.getObject((DefaultFactHandle) list[0]));
    final Cheese stilton = new Cheese("stilton", 6);
    f0.setObject(stilton);
    sink.getAsserted().clear();
    // object should not assert as it does not pass text
    alphaNode.assertObject(f0, context, ksession);
    assertLength(0, sink.getAsserted());
}
Also used : PropagationContextFactory(org.drools.core.common.PropagationContextFactory) PropagationContext(org.drools.core.spi.PropagationContext) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) Cheese(org.drools.mvel.model.Cheese) MockObjectSource(org.drools.mvel.model.MockObjectSource) MockObjectSink(org.drools.core.reteoo.MockObjectSink) AlphaNode(org.drools.core.reteoo.AlphaNode) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) BuildContext(org.drools.core.reteoo.builder.BuildContext) StatefulKnowledgeSessionImpl(org.drools.kiesession.session.StatefulKnowledgeSessionImpl) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) Test(org.junit.Test)

Example 4 with MockObjectSink

use of org.drools.core.reteoo.MockObjectSink in project drools by kiegroup.

the class PropertyChangeListenerTest method test1.

@Test
public void test1() {
    StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
    final ObjectTypeNode objectTypeNode = new ObjectTypeNode(1, this.entryPoint, new ClassObjectType(State.class), buildContext);
    objectTypeNode.attach(buildContext);
    final MockObjectSink sink = new MockObjectSink();
    objectTypeNode.addObjectSink(sink);
    final State a = new State("go");
    ksession.insert(a, true);
    ksession.fireAllRules();
    assertEquals(1, sink.getAsserted().size());
    a.setState("stop");
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) StatefulKnowledgeSessionImpl(org.drools.kiesession.session.StatefulKnowledgeSessionImpl) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) MockObjectSink(org.drools.core.reteoo.MockObjectSink) Test(org.junit.Test)

Example 5 with MockObjectSink

use of org.drools.core.reteoo.MockObjectSink in project drools by kiegroup.

the class ReteTest method testNotShadowed.

@Test
@Ignore
public void testNotShadowed() {
    Properties properties = new Properties();
    properties.setProperty("drools.shadowProxyExcludes", "org.drools.core.test.model.Cheese");
    RuleBaseConfiguration conf = new RuleBaseConfiguration(properties);
    InternalKnowledgeBase kBase = (InternalKnowledgeBase) RuleBaseFactory.newRuleBase(conf);
    buildContext = new BuildContext(kBase, Collections.emptyList());
    final StatefulKnowledgeSessionImpl ksession = new StatefulKnowledgeSessionImpl(1L, kBase);
    // Create a Rete network with ObjectTypeNodes for List, Collection and ArrayList
    final Rete rete = kBase.getRete();
    final EntryPointNode entryPoint = new EntryPointNode(0, rete, buildContext);
    entryPoint.attach(buildContext);
    final ObjectTypeNode objectTypeNode = new ObjectTypeNode(1, entryPoint, new ClassObjectType(Cheese.class), buildContext);
    objectTypeNode.attach(buildContext);
    final MockObjectSink sink1 = new MockObjectSink();
    objectTypeNode.addObjectSink(sink1);
    // There are no String ObjectTypeNodes, make sure its not propagated
    final Cheese cheese = new Cheese("brie", 15);
    final DefaultFactHandle h1 = new DefaultFactHandle(1, cheese);
    rete.assertObject(h1, pctxFactory.createPropagationContext(0, PropagationContext.Type.INSERTION, null, null, null), ksession);
    ksession.fireAllRules();
    final Object[] results = (Object[]) sink1.getAsserted().get(0);
}
Also used : Rete(org.drools.core.reteoo.Rete) ClassObjectType(org.drools.core.base.ClassObjectType) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) Cheese(org.drools.core.test.model.Cheese) MockObjectSink(org.drools.core.reteoo.MockObjectSink) Properties(java.util.Properties) RuleBaseConfiguration(org.drools.core.RuleBaseConfiguration) EntryPointNode(org.drools.core.reteoo.EntryPointNode) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) BuildContext(org.drools.core.reteoo.builder.BuildContext) StatefulKnowledgeSessionImpl(org.drools.kiesession.session.StatefulKnowledgeSessionImpl) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

MockObjectSink (org.drools.core.reteoo.MockObjectSink)11 Test (org.junit.Test)11 StatefulKnowledgeSessionImpl (org.drools.kiesession.session.StatefulKnowledgeSessionImpl)10 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)9 ClassObjectType (org.drools.core.base.ClassObjectType)8 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)8 Rete (org.drools.core.reteoo.Rete)6 BuildContext (org.drools.core.reteoo.builder.BuildContext)5 InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 PropagationContextFactory (org.drools.core.common.PropagationContextFactory)3 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)3 AlphaNode (org.drools.core.reteoo.AlphaNode)3 AlphaNodeFieldConstraint (org.drools.core.spi.AlphaNodeFieldConstraint)3 PropagationContext (org.drools.core.spi.PropagationContext)3 Cheese (org.drools.mvel.model.Cheese)3 MockObjectSource (org.drools.mvel.model.MockObjectSource)3 ClassObjectTypeConf (org.drools.core.reteoo.ClassObjectTypeConf)2 EntryPointNode (org.drools.core.reteoo.EntryPointNode)2