Search in sources :

Example 1 with MockTupleSource

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

the class MarshallingTest method testScheduledActivation.

@Test
@Ignore("This test is suspicious to say the least...")
public void testScheduledActivation() {
    KnowledgeBaseImpl knowledgeBase = (KnowledgeBaseImpl) KnowledgeBaseFactory.newKnowledgeBase();
    KnowledgePackageImpl impl = new KnowledgePackageImpl("test");
    BuildContext buildContext = new BuildContext(knowledgeBase);
    // simple rule that fires after 10 seconds
    final RuleImpl rule = new RuleImpl("test-rule");
    new RuleTerminalNode(1, new MockTupleSource(2), rule, rule.getLhs(), 0, buildContext);
    final List<String> fired = new ArrayList<String>();
    rule.setConsequence(new Consequence() {

        public void evaluate(KnowledgeHelper knowledgeHelper, WorkingMemory workingMemory) throws Exception {
            fired.add("a");
        }

        public String getName() {
            return "default";
        }
    });
    rule.setTimer(new DurationTimer(10000));
    rule.setPackage("test");
    impl.addRule(rule);
    knowledgeBase.addPackages(Collections.singleton(impl));
    SessionConfiguration config = SessionConfiguration.newInstance();
    config.setClockType(ClockType.PSEUDO_CLOCK);
    KieSession ksession = knowledgeBase.newKieSession(config, KieServices.get().newEnvironment());
    PseudoClockScheduler scheduler = (PseudoClockScheduler) ksession.<SessionClock>getSessionClock();
    Marshaller marshaller = MarshallerFactory.newMarshaller(knowledgeBase);
    ksession.insert("cheese");
    assertTrue(fired.isEmpty());
    // marshall, then unmarshall session
    readWrite(knowledgeBase, ksession, config);
    // the activations should fire after 10 seconds
    assertTrue(fired.isEmpty());
    scheduler.advanceTime(12, TimeUnit.SECONDS);
    assertFalse(fired.isEmpty());
}
Also used : Marshaller(org.kie.api.marshalling.Marshaller) WorkingMemory(org.drools.core.WorkingMemory) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) ArrayList(java.util.ArrayList) Consequence(org.drools.core.spi.Consequence) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) IOException(java.io.IOException) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) DurationTimer(org.drools.core.time.impl.DurationTimer) MockTupleSource(org.drools.core.reteoo.MockTupleSource) BuildContext(org.drools.core.reteoo.builder.BuildContext) KnowledgeHelper(org.drools.core.spi.KnowledgeHelper) KieSession(org.kie.api.runtime.KieSession) KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) SessionConfiguration(org.drools.core.SessionConfiguration) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 2 with MockTupleSource

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

the class MVELAccumulateBuilderTest method testSimpleExpression.

@Test
public void testSimpleExpression() {
    KnowledgeBuilderImpl pkgBuilder = new KnowledgeBuilderImpl();
    pkgBuilder.addPackage(new PackageDescr("pkg1"));
    InternalKnowledgePackage pkg = pkgBuilder.getPackage("pkg1");
    final RuleDescr ruleDescr = new RuleDescr("rule 1");
    final KnowledgeBuilderConfigurationImpl conf = pkgBuilder.getBuilderConfiguration();
    DialectCompiletimeRegistry dialectRegistry = pkgBuilder.getPackageRegistry(pkg.getName()).getDialectCompiletimeRegistry();
    MVELDialect mvelDialect = (MVELDialect) dialectRegistry.getDialect("mvel");
    final RuleBuildContext context = new RuleBuildContext(pkgBuilder, ruleDescr, dialectRegistry, pkg, mvelDialect);
    final AccumulateDescr accDescr = new AccumulateDescr();
    final PatternDescr inputPattern = new PatternDescr("org.drools.mvel.compiler.Cheese", "$cheese");
    accDescr.setInputPattern(inputPattern);
    accDescr.setInitCode("total = 0;");
    accDescr.setActionCode("total += $cheese.price;");
    accDescr.setReverseCode("total -= $cheese.price;");
    accDescr.setResultCode("new Integer(total)");
    final MVELAccumulateBuilder builder = new MVELAccumulateBuilder();
    final Accumulate acc = (Accumulate) builder.build(context, accDescr);
    ((MVELCompileable) acc.getAccumulators()[0]).compile((MVELDialectRuntimeData) pkgBuilder.getPackageRegistry(pkg.getName()).getDialectRuntimeRegistry().getDialectData("mvel"));
    InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
    StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
    BuildContext buildContext = new BuildContext(kBase, Collections.emptyList());
    MockLeftTupleSink sink = new MockLeftTupleSink(buildContext);
    MockTupleSource source = new MockTupleSource(1, buildContext);
    source.setObjectCount(1);
    sink.setLeftTupleSource(source);
    final Cheese cheddar1 = new Cheese("cheddar", 10);
    final Cheese cheddar2 = new Cheese("cheddar", 8);
    final InternalFactHandle f0 = (InternalFactHandle) ksession.insert(new InitialFactImpl());
    final InternalFactHandle f1 = (InternalFactHandle) ksession.insert(cheddar1);
    final InternalFactHandle f2 = (InternalFactHandle) ksession.insert(cheddar2);
    final LeftTupleImpl tuple = new LeftTupleImpl(f0, sink, true);
    Object wmContext = acc.createWorkingMemoryContext();
    AccumulateNode.AccumulateContext accContext = new AccumulateNode.AccumulateContext();
    Object funcContext = acc.createFunctionContext();
    funcContext = acc.init(wmContext, accContext, funcContext, tuple, ksession);
    accContext.setFunctionContext(funcContext);
    Object value1 = acc.accumulate(wmContext, accContext, tuple, f1, ksession);
    acc.accumulate(wmContext, accContext, tuple, f2, ksession);
    assertEquals(new Integer(18), acc.getResult(wmContext, accContext, tuple, ksession));
    LeftTuple match = new FromNodeLeftTuple();
    match.setContextObject(value1);
    acc.tryReverse(wmContext, accContext, tuple, f1, null, match, ksession);
    assertEquals(new Integer(8), acc.getResult(wmContext, accContext, tuple, ksession));
}
Also used : MVELCompileable(org.drools.mvel.expr.MVELCompileable) PatternDescr(org.drools.drl.ast.descr.PatternDescr) KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) AccumulateNode(org.drools.core.reteoo.AccumulateNode) MockLeftTupleSink(org.drools.core.reteoo.MockLeftTupleSink) Cheese(org.drools.mvel.compiler.Cheese) AccumulateDescr(org.drools.drl.ast.descr.AccumulateDescr) LeftTuple(org.drools.core.reteoo.LeftTuple) FromNodeLeftTuple(org.drools.core.reteoo.FromNodeLeftTuple) Accumulate(org.drools.core.rule.Accumulate) MVELAccumulateBuilder(org.drools.mvel.builder.MVELAccumulateBuilder) FromNodeLeftTuple(org.drools.core.reteoo.FromNodeLeftTuple) MockTupleSource(org.drools.core.reteoo.MockTupleSource) LeftTupleImpl(org.drools.core.reteoo.LeftTupleImpl) PackageDescr(org.drools.drl.ast.descr.PackageDescr) InternalFactHandle(org.drools.core.common.InternalFactHandle) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) RuleBuildContext(org.drools.compiler.rule.builder.RuleBuildContext) DialectCompiletimeRegistry(org.drools.compiler.compiler.DialectCompiletimeRegistry) MVELDialect(org.drools.mvel.builder.MVELDialect) InitialFactImpl(org.drools.core.reteoo.InitialFactImpl) BuildContext(org.drools.core.reteoo.builder.BuildContext) RuleBuildContext(org.drools.compiler.rule.builder.RuleBuildContext) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) StatefulKnowledgeSessionImpl(org.drools.kiesession.session.StatefulKnowledgeSessionImpl) RuleDescr(org.drools.drl.ast.descr.RuleDescr) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.Test)

Example 3 with MockTupleSource

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

the class MarshallingTest method testScheduledActivation.

@Test
@Ignore("This test is suspicious to say the least...")
public void testScheduledActivation() {
    InternalKnowledgeBase knowledgeBase = KnowledgeBaseFactory.newKnowledgeBase();
    InternalKnowledgePackage impl = CoreComponentFactory.get().createKnowledgePackage("test");
    BuildContext buildContext = new BuildContext(knowledgeBase, Collections.emptyList());
    // simple rule that fires after 10 seconds
    final RuleImpl rule = new RuleImpl("test-rule");
    new RuleTerminalNode(1, new MockTupleSource(2, buildContext), rule, rule.getLhs(), 0, buildContext);
    final List<String> fired = new ArrayList<String>();
    rule.setConsequence(new Consequence() {

        public void evaluate(KnowledgeHelper knowledgeHelper, ReteEvaluator reteEvaluator) throws Exception {
            fired.add("a");
        }

        public String getName() {
            return "default";
        }
    });
    rule.setTimer(new DurationTimer(10000));
    rule.setPackage("test");
    impl.addRule(rule);
    knowledgeBase.addPackages(Collections.singleton(impl));
    SessionConfiguration config = SessionConfiguration.newInstance();
    config.setClockType(ClockType.PSEUDO_CLOCK);
    KieSession ksession = knowledgeBase.newKieSession(config, KieServices.get().newEnvironment());
    PseudoClockScheduler scheduler = (PseudoClockScheduler) ksession.<SessionClock>getSessionClock();
    Marshaller marshaller = MarshallerFactory.newMarshaller(knowledgeBase);
    ksession.insert("cheese");
    assertTrue(fired.isEmpty());
    // marshall, then unmarshall session
    readWrite(knowledgeBase, ksession, config);
    // the activations should fire after 10 seconds
    assertTrue(fired.isEmpty());
    scheduler.advanceTime(12, TimeUnit.SECONDS);
    assertFalse(fired.isEmpty());
}
Also used : ReteEvaluator(org.drools.core.common.ReteEvaluator) Marshaller(org.kie.api.marshalling.Marshaller) ArrayList(java.util.ArrayList) Consequence(org.drools.core.spi.Consequence) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) IOException(java.io.IOException) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) DurationTimer(org.drools.core.time.impl.DurationTimer) MockTupleSource(org.drools.core.reteoo.MockTupleSource) BuildContext(org.drools.core.reteoo.builder.BuildContext) KnowledgeHelper(org.drools.core.spi.KnowledgeHelper) KieSession(org.kie.api.runtime.KieSession) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) SessionConfiguration(org.drools.core.SessionConfiguration) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 4 with MockTupleSource

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

the class NodeSegmentUnlinkingTest method testSingleNodeinSegment.

@Test
public void testSingleNodeinSegment() {
    rule1 = new RuleImpl("rule1");
    rule2 = new RuleImpl("rule2");
    rule3 = new RuleImpl("rule3");
    kBase = KnowledgeBaseFactory.newKnowledgeBase();
    BuildContext buildContext = new BuildContext(kBase, Collections.emptyList());
    MockObjectSource mockObjectSource = new MockObjectSource(8);
    MockTupleSource mockTupleSource = new MockTupleSource(9, buildContext);
    // 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);
    n1.addAssociation(rule2);
    n1.addAssociation(rule3);
    n2.addAssociation(rule2);
    n2.addAssociation(rule3);
    n3.addAssociation(rule1);
    n4.addAssociation(rule2);
    n5.addAssociation(rule3);
    mockObjectSource.attach(buildContext);
    mockTupleSource.attach(buildContext);
    n1.attach(buildContext);
    n2.attach(buildContext);
    n3.attach(buildContext);
    n4.attach(buildContext);
    n5.attach(buildContext);
    StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
    createSegmentMemory(n2, ksession);
    BetaMemory bm = (BetaMemory) ksession.getNodeMemory(n1);
    assertNull(bm.getSegmentMemory());
    bm = (BetaMemory) ksession.getNodeMemory(n3);
    assertNull(bm.getSegmentMemory());
    bm = (BetaMemory) ksession.getNodeMemory(n4);
    assertNull(bm.getSegmentMemory());
    bm = (BetaMemory) ksession.getNodeMemory(n2);
    assertEquals(1, bm.getNodePosMaskBit());
    assertEquals(1, bm.getSegmentMemory().getAllLinkedMaskTest());
}
Also used : BetaNode(org.drools.core.reteoo.BetaNode) ClassObjectType(org.drools.core.base.ClassObjectType) JoinNode(org.drools.core.reteoo.JoinNode) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) BetaMemory(org.drools.core.reteoo.BetaMemory) MockObjectSource(org.drools.core.reteoo.MockObjectSource) MockTupleSource(org.drools.core.reteoo.MockTupleSource) BuildContext(org.drools.core.reteoo.builder.BuildContext) EmptyBetaConstraints(org.drools.core.common.EmptyBetaConstraints) StatefulKnowledgeSessionImpl(org.drools.kiesession.session.StatefulKnowledgeSessionImpl) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) Test(org.junit.Test)

Example 5 with MockTupleSource

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

the class QueryElementNodeTest method testAttach.

@Test
public void testAttach() throws Exception {
    QueryElement queryElement = new QueryElement(null, null, new QueryArgument[0], null, null, false, false);
    final MockTupleSource source = new MockTupleSource(12, buildContext);
    final QueryElementNode node = new QueryElementNode(18, source, queryElement, true, false, buildContext);
    assertEquals(18, node.getId());
    assertEquals(0, source.getAttached());
    node.attach(buildContext);
    assertEquals(1, source.getAttached());
}
Also used : MockTupleSource(org.drools.core.reteoo.MockTupleSource) QueryElementNode(org.drools.core.reteoo.QueryElementNode) QueryElement(org.drools.core.rule.QueryElement) Test(org.junit.Test)

Aggregations

MockTupleSource (org.drools.core.reteoo.MockTupleSource)9 Test (org.junit.Test)8 BuildContext (org.drools.core.reteoo.builder.BuildContext)7 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)5 StatefulKnowledgeSessionImpl (org.drools.kiesession.session.StatefulKnowledgeSessionImpl)5 ClassObjectType (org.drools.core.base.ClassObjectType)4 InternalFactHandle (org.drools.core.common.InternalFactHandle)4 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)4 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)4 LeftTupleImpl (org.drools.core.reteoo.LeftTupleImpl)4 InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)4 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)3 DialectCompiletimeRegistry (org.drools.compiler.compiler.DialectCompiletimeRegistry)3 RuleBuildContext (org.drools.compiler.rule.builder.RuleBuildContext)3 Declaration (org.drools.core.rule.Declaration)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 KnowledgeBuilderConfigurationImpl (org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl)2 SessionConfiguration (org.drools.core.SessionConfiguration)2