Search in sources :

Example 56 with KieSession

use of org.kie.api.runtime.KieSession in project drools by kiegroup.

the class TruthMaintenanceTest method testTMSwithQueries.

@Test(timeout = 10000)
public void testTMSwithQueries() {
    String str = "" + "package org.drools.compiler.test;\n" + "\n" + "global java.util.List list; \n" + "\n" + "declare Bean\n" + "    str : String\n" + "end\n" + "\n" + "query bean ( String $s )\n" + "    Bean(  $s ; )\n" + "end\n" + "\n" + "\n" + "rule \"init\"\n" + "when\n" + "then\n" + "    insert( new Bean(\"AAA\") );\n" + "    insert( \"x\" );\n" + "end\n" + "\n" + "rule \"LogicIn\"\n" + "when\n" + "    String( this == \"x\" )\n" + "    ?bean(  \"AAA\" ; )\n" + "then\n" + "    insertLogical(\"y\");\n" + "    retract(\"x\");\n" + "end " + "\n" + "rule \"Never\"\n" + "salience -999\n" + "when\n" + "    $s : String( this == \"y\" )\n" + "then\n" + "    list.add($s);\n" + "end";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    KieSession kSession = createKnowledgeSession(kbase);
    List list = new ArrayList();
    kSession.setGlobal("list", list);
    kSession.fireAllRules();
    assertEquals(0, list.size());
// System.err.println(reportWMObjects(kSession));
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 57 with KieSession

use of org.kie.api.runtime.KieSession 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 58 with KieSession

use of org.kie.api.runtime.KieSession 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 59 with KieSession

use of org.kie.api.runtime.KieSession 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 60 with KieSession

use of org.kie.api.runtime.KieSession in project drools by kiegroup.

the class UnmarshallingTest method testMarshallWithNot.

@Test
public void testMarshallWithNot() throws Exception {
    String whenBenNotVilgaxRule = "import " + getClass().getCanonicalName() + ".*\n" + "rule one\n" + "when\n" + "   Ben()\n" + "   not(Vilgax())\n" + "then\n" + "   //System.out.println(\"Ben!\");\n" + "end\n" + "\n" + "rule two\n" + "when\n" + "   Ben()\n" + "then\n" + "   //System.out.println(\"Vilgax..\");\n" + "end\n";
    KieBase knowledgeBase = initializeKnowledgeBase(whenBenNotVilgaxRule);
    // Initialize Knowledge session and insert Ben
    KieSession ksession = knowledgeBase.newKieSession();
    ksession.insert(new Ben());
    // Marshall
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    MarshallerFactory.newMarshaller(knowledgeBase).marshall(baos, ksession);
    // Clean up
    // - mimicing when a session is reloaded from a database.
    ksession.dispose();
    // Re-initialize
    knowledgeBase = initializeKnowledgeBase(whenBenNotVilgaxRule);
    // Unmarshall
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    try {
        ksession = MarshallerFactory.newMarshaller(knowledgeBase).unmarshall(bais);
    } catch (Throwable t) {
        t.printStackTrace();
        fail(t.getClass().getSimpleName() + " thrown when trying to unmarshall (see stack trace in output).");
    }
    int rules = ksession.fireAllRules();
    Assert.assertEquals(2, rules);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Aggregations

KieSession (org.kie.api.runtime.KieSession)5328 Test (org.junit.Test)4824 KieBase (org.kie.api.KieBase)2414 ArrayList (java.util.ArrayList)2317 List (java.util.List)1105 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)769 FactHandle (org.kie.api.runtime.rule.FactHandle)598 Person (org.drools.modelcompiler.domain.Person)519 HashMap (java.util.HashMap)416 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)415 KieServices (org.kie.api.KieServices)382 KieHelper (org.kie.internal.utils.KieHelper)355 KieContainer (org.kie.api.runtime.KieContainer)298 RuntimeEngine (org.kie.api.runtime.manager.RuntimeEngine)265 InternalFactHandle (org.drools.core.common.InternalFactHandle)259 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)234 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)234 ReleaseId (org.kie.api.builder.ReleaseId)232 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)229 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)207