Search in sources :

Example 6 with ReteEvaluator

use of org.drools.core.common.ReteEvaluator in project drools by kiegroup.

the class ReteooRuleBuilderTest method testAddRuleWithPatterns.

@Test
public void testAddRuleWithPatterns() {
    final RuleImpl rule = new RuleImpl("only patterns");
    final Pattern c1 = new Pattern(0, new ClassObjectType(String.class));
    final Pattern c2 = new Pattern(1, new ClassObjectType(String.class));
    final Pattern c3 = new Pattern(2, new ClassObjectType(String.class));
    final GroupElement lhsroot = GroupElementFactory.newAndInstance();
    lhsroot.addChild(c1);
    lhsroot.addChild(c2);
    lhsroot.addChild(c3);
    rule.setLhs(lhsroot);
    final Consequence consequence = new Consequence() {

        public void evaluate(KnowledgeHelper knowledgeHelper, ReteEvaluator reteEvaluator) throws Exception {
            System.out.println("Consequence!");
        }

        public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
        }

        public void writeExternal(ObjectOutput out) throws IOException {
        }

        public String getName() {
            return "default";
        }
    };
    rule.setConsequence(consequence);
    final List terminals = this.builder.addRule(rule, this.rulebase, Collections.emptyList());
    assertEquals("Rule must have a single terminal node", 1, terminals.size());
    final RuleTerminalNode terminal = (RuleTerminalNode) terminals.get(0);
}
Also used : ReteEvaluator(org.drools.core.common.ReteEvaluator) Pattern(org.drools.core.rule.Pattern) ClassObjectType(org.drools.core.base.ClassObjectType) ObjectOutput(java.io.ObjectOutput) GroupElement(org.drools.core.rule.GroupElement) Consequence(org.drools.core.spi.Consequence) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) KnowledgeHelper(org.drools.core.spi.KnowledgeHelper) List(java.util.List) ObjectInput(java.io.ObjectInput) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) Test(org.junit.Test)

Example 7 with ReteEvaluator

use of org.drools.core.common.ReteEvaluator 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 8 with ReteEvaluator

use of org.drools.core.common.ReteEvaluator in project drools by kiegroup.

the class RuleExecutor method evaluateNetworkAndFire.

public int evaluateNetworkAndFire(ActivationsManager activationsManager, AgendaFilter filter, int fireCount, int fireLimit) {
    ReteEvaluator reteEvaluator = activationsManager.getReteEvaluator();
    reEvaluateNetwork(activationsManager);
    if (reteEvaluator.getSessionConfiguration().isDirectFiring()) {
        RuleTerminalNode rtn = (RuleTerminalNode) pmem.getPathEndNode();
        RuleImpl rule = rtn.getRule();
        int directFirings = tupleList.size();
        for (Tuple tuple = tupleList.getFirst(); tuple != null; tuple = tupleList.getFirst()) {
            if (cancelAndContinue(reteEvaluator, rtn, rule, tuple, filter)) {
                directFirings--;
            } else {
                innerFireActivation(reteEvaluator, activationsManager, (Activation) tuple, ((Activation) tuple).getConsequence());
            }
            removeLeftTuple(tuple);
        }
        ruleAgendaItem.remove();
        return directFirings;
    }
    return fire(reteEvaluator, activationsManager, filter, fireCount, fireLimit);
}
Also used : ReteEvaluator(org.drools.core.common.ReteEvaluator) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) Tuple(org.drools.core.spi.Tuple) RuleTerminalNodeLeftTuple(org.drools.core.reteoo.RuleTerminalNodeLeftTuple) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode)

Example 9 with ReteEvaluator

use of org.drools.core.common.ReteEvaluator in project drools by kiegroup.

the class InterpretedRuleUnit method internalCreateInstance.

@Override
public RuleUnitInstance<T> internalCreateInstance(T data) {
    KnowledgeBuilder kBuilder = new KnowledgeBuilderImpl();
    Class<? extends RuleUnitData> wmClass = data.getClass();
    String canonicalName = wmClass.getCanonicalName();
    // transform foo.bar.Baz to /foo/bar/Baz.drl
    // this currently only works for single files
    InputStream resourceAsStream = wmClass.getResourceAsStream(String.format("/%s.drl", canonicalName.replace('.', '/')));
    kBuilder.add(new InputStreamResource(resourceAsStream), ResourceType.DRL);
    InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
    kBase.addPackages(kBuilder.getKnowledgePackages());
    ReteEvaluator reteEvaluator = new RuleUnitExecutorImpl(kBase);
    return new InterpretedRuleUnitInstance<>(this, data, reteEvaluator);
}
Also used : ReteEvaluator(org.drools.core.common.ReteEvaluator) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) RuleUnitExecutorImpl(org.drools.ruleunits.impl.sessions.RuleUnitExecutorImpl) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) InputStream(java.io.InputStream) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) InputStreamResource(org.drools.core.io.impl.InputStreamResource)

Example 10 with ReteEvaluator

use of org.drools.core.common.ReteEvaluator in project drools by kiegroup.

the class ReteooRuleBaseMultiThreadedTest method setUp.

@Before
public void setUp() {
    this.kBase = KnowledgeBaseFactory.newKnowledgeBase();
    pkg = CoreComponentFactory.get().createKnowledgePackage("org.droos.test");
    JavaDialectRuntimeData data = new JavaDialectRuntimeData();
    data.onAdd(pkg.getDialectRuntimeRegistry(), kBase.getRootClassLoader());
    pkg.getDialectRuntimeRegistry().setDialectData("java", data);
    // we need to add one rule to the package because the previous deadlock was encountered
    // while removing rules from a package when said package is removed from the rulebase
    rule = new RuleImpl("Test");
    rule.setDialect("java");
    rule.setConsequence(new Consequence() {

        public void evaluate(KnowledgeHelper knowledgeHelper, ReteEvaluator reteEvaluator) throws Exception {
        }

        public String getName() {
            return "default";
        }
    });
    pkg.addRule(rule);
    kBase.addPackage(pkg);
}
Also used : ReteEvaluator(org.drools.core.common.ReteEvaluator) Consequence(org.drools.core.spi.Consequence) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) KnowledgeHelper(org.drools.core.spi.KnowledgeHelper) JavaDialectRuntimeData(org.drools.core.rule.JavaDialectRuntimeData) Before(org.junit.Before)

Aggregations

ReteEvaluator (org.drools.core.common.ReteEvaluator)22 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)9 KnowledgeHelper (org.drools.core.spi.KnowledgeHelper)8 InternalFactHandle (org.drools.core.common.InternalFactHandle)7 Consequence (org.drools.core.spi.Consequence)7 Tuple (org.drools.core.spi.Tuple)7 Test (org.junit.Test)7 ObjectInput (java.io.ObjectInput)5 ObjectOutput (java.io.ObjectOutput)5 ClassObjectType (org.drools.core.base.ClassObjectType)5 LeftTuple (org.drools.core.reteoo.LeftTuple)5 Pattern (org.drools.core.rule.Pattern)5 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)4 DeclarationMatcher (org.drools.mvel.asm.GeneratorHelper.DeclarationMatcher)4 GeneratorHelper.createInvokerClassGenerator (org.drools.mvel.asm.GeneratorHelper.createInvokerClassGenerator)4 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)3 RuleTerminalNodeLeftTuple (org.drools.core.reteoo.RuleTerminalNodeLeftTuple)3 Declaration (org.drools.core.rule.Declaration)3