Search in sources :

Example 6 with KieHelper

use of org.kie.internal.utils.KieHelper in project drools by kiegroup.

the class AbstractConcurrentTest method getKieBase.

protected synchronized KieBase getKieBase(final String... drls) {
    final KieHelper kieHelper = new KieHelper();
    for (final String drl : drls) {
        kieHelper.addContent(drl, ResourceType.DRL);
    }
    KieBase kieBase;
    if (enforcedJitting) {
        kieBase = kieHelper.build(ConstraintJittingThresholdOption.get(0));
    } else {
        kieBase = kieHelper.build();
    }
    if (serializeKieBase) {
        kieBase = serializeAndDeserializeKieBase(kieBase);
    }
    return kieBase;
}
Also used : KieBase(org.kie.api.KieBase) KieHelper(org.kie.internal.utils.KieHelper)

Example 7 with KieHelper

use of org.kie.internal.utils.KieHelper in project drools by kiegroup.

the class TypeDeclarationTest method testCircularDeclaration.

@Test
public void testCircularDeclaration() throws Exception {
    String rule = "package org.drools.compiler.test\n" + "declare FactA\n" + "    fieldB: FactB\n" + "end\n" + "declare FactB\n" + "    fieldA: FactA\n" + "end\n" + "rule R1 when\n" + "   $fieldA : FactA( $fieldB : fieldB )\n" + "   FactB( this == $fieldB, fieldA == $fieldA )\n" + "then\n" + "end";
    KieBase kbase = new KieHelper().addContent(rule, ResourceType.DRL).build();
    KieSession ksession = kbase.newKieSession();
    FactType aType = kbase.getFactType("org.drools.compiler.test", "FactA");
    Object a = aType.newInstance();
    FactType bType = kbase.getFactType("org.drools.compiler.test", "FactB");
    Object b = bType.newInstance();
    aType.set(a, "fieldB", b);
    bType.set(b, "fieldA", a);
    ksession.insert(a);
    ksession.insert(b);
    int rules = ksession.fireAllRules();
    assertEquals(1, rules);
}
Also used : KieBase(org.kie.api.KieBase) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) FactType(org.kie.api.definition.type.FactType) Test(org.junit.Test)

Example 8 with KieHelper

use of org.kie.internal.utils.KieHelper in project drools by kiegroup.

the class TypeDeclarationTest method testCircularDeclarationWithExtension.

@Test
public void testCircularDeclarationWithExtension() throws Exception {
    // DROOLS-640
    String drl = "package org.drools.compiler.test\n" + "declare FactA\n" + "    fieldB: FactB\n" + "end\n" + "declare FactB extends FactA end\n" + "rule R1 when\n" + "   $a : FactA( )\n" + "   $b : FactB( this == $a.fieldB )\n" + "then\n" + "end";
    KieBase kbase = new KieHelper().addContent(drl, ResourceType.DRL).build();
    KieSession ksession = kbase.newKieSession();
    FactType aType = kbase.getFactType("org.drools.compiler.test", "FactA");
    FactType bType = kbase.getFactType("org.drools.compiler.test", "FactB");
    Object a = aType.newInstance();
    Object b = bType.newInstance();
    aType.set(a, "fieldB", b);
    ksession.insert(a);
    ksession.insert(b);
    int rules = ksession.fireAllRules();
    assertEquals(1, rules);
}
Also used : KieBase(org.kie.api.KieBase) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) FactType(org.kie.api.definition.type.FactType) Test(org.junit.Test)

Example 9 with KieHelper

use of org.kie.internal.utils.KieHelper in project drools by kiegroup.

the class TypeDeclarationTest method testRecursiveDeclaration.

@Test
public void testRecursiveDeclaration() throws Exception {
    String rule = "package org.drools.compiler\n" + "declare Node\n" + "    value: String\n" + "    parent: Node\n" + "end\n" + "rule R1 when\n" + "   $parent: Node( value == \"parent\" )\n" + "   $child: Node( $value : value, parent == $parent )\n" + "then\n" + "   System.out.println( $value );\n" + "end";
    KieBase kbase = new KieHelper().addContent(rule, ResourceType.DRL).build();
    KieSession ksession = kbase.newKieSession();
    FactType nodeType = kbase.getFactType("org.drools.compiler", "Node");
    Object parent = nodeType.newInstance();
    nodeType.set(parent, "value", "parent");
    ksession.insert(parent);
    Object child = nodeType.newInstance();
    nodeType.set(child, "value", "child");
    nodeType.set(child, "parent", parent);
    ksession.insert(child);
    int rules = ksession.fireAllRules();
    assertEquals(1, rules);
}
Also used : KieBase(org.kie.api.KieBase) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) FactType(org.kie.api.definition.type.FactType) Test(org.junit.Test)

Example 10 with KieHelper

use of org.kie.internal.utils.KieHelper in project drools by kiegroup.

the class WorkingMemoryLoggerTest method testLogAllBoundVariables.

@Test
public void testLogAllBoundVariables() throws Exception {
    // BZ-1271909
    final String drl = "import " + Message.class.getCanonicalName() + "\n" + "rule \"Hello World\" no-loop\n" + "    when\n" + "        $messageInstance : Message( $myMessage : message )\n" + "    then\n" + "        update($messageInstance);\n" + "end\n";
    final KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().newKieSession();
    final WorkingMemoryInMemoryLogger logger = new WorkingMemoryInMemoryLogger((WorkingMemory) ksession);
    final Message message = new Message();
    message.setMessage("Hello World");
    ksession.insert(message);
    ksession.fireAllRules();
    for (final LogEvent logEvent : logger.getLogEvents()) {
        if (logEvent instanceof ActivationLogEvent) {
            assertTrue(((ActivationLogEvent) logEvent).getDeclarations().contains("$messageInstance"));
            assertTrue(((ActivationLogEvent) logEvent).getDeclarations().contains("$myMessage"));
        }
    }
}
Also used : WorkingMemoryInMemoryLogger(org.drools.core.audit.WorkingMemoryInMemoryLogger) Message(org.drools.compiler.Message) LogEvent(org.drools.core.audit.event.LogEvent) ActivationLogEvent(org.drools.core.audit.event.ActivationLogEvent) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) ActivationLogEvent(org.drools.core.audit.event.ActivationLogEvent) Test(org.junit.Test)

Aggregations

KieHelper (org.kie.internal.utils.KieHelper)467 Test (org.junit.Test)427 KieSession (org.kie.api.runtime.KieSession)355 ArrayList (java.util.ArrayList)194 KieBase (org.kie.api.KieBase)152 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)77 Person (org.drools.compiler.Person)61 FactHandle (org.kie.api.runtime.rule.FactHandle)55 List (java.util.List)44 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)41 RuleUnitExecutor (org.kie.api.runtime.rule.RuleUnitExecutor)38 InternalRuleUnitExecutor (org.drools.core.impl.InternalRuleUnitExecutor)37 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)34 Man (org.drools.compiler.oopath.model.Man)29 Child (org.drools.compiler.oopath.model.Child)24 Woman (org.drools.compiler.oopath.model.Woman)23 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)23 InternalFactHandle (org.drools.core.common.InternalFactHandle)18 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)18 PseudoClockScheduler (org.drools.core.time.impl.PseudoClockScheduler)18