Search in sources :

Example 1 with DurationTimer

use of org.drools.core.time.impl.DurationTimer 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 DurationTimer

use of org.drools.core.time.impl.DurationTimer 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 3 with DurationTimer

use of org.drools.core.time.impl.DurationTimer in project drools by kiegroup.

the class PatternBuilder method checkDelaying.

private void checkDelaying(final BuildContext context, final Constraint constraint) {
    if (constraint instanceof IntervalProviderConstraint) {
        // variable constraints always require a single declaration
        Declaration target = constraint.getRequiredDeclarations()[0];
        if (target.isPatternDeclaration() && target.getPattern().getObjectType().isEvent()) {
            long uplimit = ((IntervalProviderConstraint) constraint).getInterval().getUpperBound();
            // only makes sense to add the new timer if the uplimit is not infinity (Long.MAX_VALUE)
            if (uplimit >= 0 && uplimit < Long.MAX_VALUE) {
                Timer timer = context.getRule().getTimer();
                DurationTimer durationTimer = new DurationTimer(uplimit);
                durationTimer.setEventFactHandle(target);
                if (timer instanceof CompositeMaxDurationTimer) {
                    // already a composite so just add
                    ((CompositeMaxDurationTimer) timer).addDurationTimer(durationTimer);
                } else {
                    if (timer == null) {
                        // no timer exists, so ok on it's own
                        timer = durationTimer;
                    } else {
                        // timer exists so we need to make a composite
                        CompositeMaxDurationTimer temp = new CompositeMaxDurationTimer();
                        if (timer instanceof DurationTimer) {
                            // previous timer was a duration, so add another DurationTimer
                            temp.addDurationTimer((DurationTimer) timer);
                        } else {
                            // previous timer was not a duration, so set it as the delegate Timer.
                            temp.setTimer(context.getRule().getTimer());
                        }
                        // now add the new durationTimer
                        temp.addDurationTimer(durationTimer);
                        timer = temp;
                    }
                    // with the composite made, reset it on the Rule
                    context.getRule().setTimer(timer);
                }
            }
        }
    }
}
Also used : CompositeMaxDurationTimer(org.drools.core.time.impl.CompositeMaxDurationTimer) DurationTimer(org.drools.core.time.impl.DurationTimer) CompositeMaxDurationTimer(org.drools.core.time.impl.CompositeMaxDurationTimer) Timer(org.drools.core.time.impl.Timer) DurationTimer(org.drools.core.time.impl.DurationTimer) CompositeMaxDurationTimer(org.drools.core.time.impl.CompositeMaxDurationTimer) TypeDeclaration(org.drools.core.rule.TypeDeclaration) Declaration(org.drools.core.rule.Declaration) IntervalProviderConstraint(org.drools.core.rule.IntervalProviderConstraint)

Aggregations

DurationTimer (org.drools.core.time.impl.DurationTimer)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 SessionConfiguration (org.drools.core.SessionConfiguration)2 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)2 MockTupleSource (org.drools.core.reteoo.MockTupleSource)2 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)2 BuildContext (org.drools.core.reteoo.builder.BuildContext)2 Consequence (org.drools.core.spi.Consequence)2 KnowledgeHelper (org.drools.core.spi.KnowledgeHelper)2 PseudoClockScheduler (org.drools.core.time.impl.PseudoClockScheduler)2 Ignore (org.junit.Ignore)2 Test (org.junit.Test)2 Marshaller (org.kie.api.marshalling.Marshaller)2 KieSession (org.kie.api.runtime.KieSession)2 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)2 WorkingMemory (org.drools.core.WorkingMemory)1 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)1 ReteEvaluator (org.drools.core.common.ReteEvaluator)1 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)1