Search in sources :

Example 46 with KnowledgeBuilder

use of org.kie.internal.builder.KnowledgeBuilder in project drools by kiegroup.

the class RemoveRuleTest method buildKnowledgePackage.

private Collection<KiePackage> buildKnowledgePackage(String ruleName, String rule) {
    String str = "";
    str += "package org.kie \n";
    str += "import " + A.class.getCanonicalName() + "\n";
    str += "import " + B.class.getCanonicalName() + "\n";
    str += "import " + C.class.getCanonicalName() + "\n";
    str += "import " + D.class.getCanonicalName() + "\n";
    str += "import " + E.class.getCanonicalName() + "\n";
    str += "global java.util.List list \n";
    int i = 0;
    str += "rule " + ruleName + "  when \n";
    str += rule;
    str += "then \n";
    str += " list.add( kcontext.getMatch() );\n";
    str += "end \n";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
    assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
    return kbuilder.getKnowledgePackages();
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder)

Example 47 with KnowledgeBuilder

use of org.kie.internal.builder.KnowledgeBuilder in project drools by kiegroup.

the class AsmGeneratorTest method testOr.

@Test
public void testOr() {
    String s = "package org.kie.test\n" + "import org.drools.compiler.Person\n" + "import org.drools.compiler.Cheese\n" + "global java.util.List list\n" + "rule r1 when\n" + "    s1 : String( this == 's1' )\n" + "    Cheese( $type : type == \"stilton\", $price : price ) or\n" + "    ( Cheese( $type : type == \"brie\", $price : price ) and Person( name == \"bob\", likes == $type ) )\n" + "then\n" + "    // *2 are missed out on purpose to make sure we only resolved required declarations\n" + "    list.add( \"test3\"+$type +\":\"+ new Integer( $price ) ); \n" + "end\n";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(s.getBytes()), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    KieSession ksession = createKnowledgeSession(kbase);
    List list = new ArrayList();
    ksession.setGlobal("list", list);
    ksession.insert("s1");
    ksession.insert(new Person("bob", "brie"));
    ksession.insert(new Cheese("stilton"));
    ksession.insert(new Cheese("brie"));
    ksession.insert(new Person("s2"));
    ksession.fireAllRules();
    assertEquals(2, list.size());
    assertTrue(list.contains("test3brie:0"));
    assertTrue(list.contains("test3stilton:0"));
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Cheese(org.drools.compiler.Cheese) Person(org.drools.compiler.Person) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 48 with KnowledgeBuilder

use of org.kie.internal.builder.KnowledgeBuilder in project drools by kiegroup.

the class DescrBuilderTest method compilePkgDescr.

private KiePackage compilePkgDescr(PackageDescr pkg, String pkgName) {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newDescrResource(pkg), ResourceType.DESCR);
    assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
    if (pkgName == null) {
        Collection<KiePackage> kpkgs = kbuilder.getKnowledgePackages();
        assertEquals(1, kpkgs.size());
        return kpkgs.iterator().next();
    }
    return ((KnowledgeBuilderImpl) kbuilder).getPackage(pkgName);
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KiePackage(org.kie.api.definition.KiePackage) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl)

Example 49 with KnowledgeBuilder

use of org.kie.internal.builder.KnowledgeBuilder in project drools by kiegroup.

the class Misc2Test method testSelfChangingRuleSet.

@Test
public void testSelfChangingRuleSet() {
    // DROOLS-92
    String str = "package org.drools.compiler.integrationtests;\n" + "" + "import org.drools.compiler.integrationtests.Misc2Test.Foo2; \n" + "" + "global java.util.List list; \n" + "\n" + "rule \"Prep\" \n" + "when \n" + "  $packs : java.util.Collection() \n" + "then \n" + "   ((org.drools.core.impl.InternalKnowledgeBase)drools.getKieRuntime().getKieBase()).addPackages( $packs );" + "end \n" + "" + "rule \"Self-change\"\n" + "when\n" + "  String( this == \"go\" )\n" + "then\n" + "   ((org.drools.core.impl.InternalKnowledgeBase)drools.getKieRuntime().getKieBase()).removeRule( \"org.drools.compiler.integrationtests\", \"React\" ); \n" + "end\n" + "\n" + "\n" + "rule \"Insert\"\n" + "when\n" + "  $i : Integer()\n" + "then\n" + "  Foo2 foo = new Foo2();\n " + "  foo.setX( $i ); \n" + "  insert( foo );\n" + "end\n" + "" + "";
    String str2 = "package org.drools.compiler.integrationtests;\n" + "" + "import org.drools.compiler.integrationtests.Misc2Test.Foo2; \n" + "global java.util.List list;\n " + "rule \"React\"\n" + "when\n" + "  $b : Foo2( x < 10 )\n" + "then\n" + "  System.out.println( \" Foo2 is in \" + $b.getX() );" + "  list.add( $b ); \n" + "end\n";
    KnowledgeBuilder knowledgeBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    knowledgeBuilder.add(new ByteArrayResource(str2.getBytes()), ResourceType.DRL);
    System.out.println(knowledgeBuilder.getErrors());
    KieBase kbase = loadKnowledgeBaseFromString(str);
    KieSession ksession = kbase.newKieSession();
    List list = new ArrayList();
    ksession.setGlobal("list", list);
    ksession.insert(knowledgeBuilder.getKnowledgePackages());
    ksession.insert(new Integer(1));
    ksession.fireAllRules();
    ksession.insert("go");
    ksession.fireAllRules();
    ksession.insert(new Integer(2));
    ksession.fireAllRules();
    assertEquals(1, list.size());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) Arrays.asList(java.util.Arrays.asList) ArrayList(java.util.ArrayList) List(java.util.List) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) Test(org.junit.Test)

Example 50 with KnowledgeBuilder

use of org.kie.internal.builder.KnowledgeBuilder in project drools by kiegroup.

the class Misc2Test method testStackOverflowInMvel.

@Test
@Ignore("This test is supposed to cause a StackOverflow inside mvel but this not always happens")
public void testStackOverflowInMvel() {
    // DROOLS-1542
    String str1 = "import " + Person.class.getName() + ";\n" + "rule R1 when\n" + " $p : Person( ";
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < 10000; i++) {
        sb.append("name == \"John-" + i + "\" || ");
    }
    String str2 = " age == 20 )\n" + "then end\n";
    String drl = str1 + sb.toString() + str2;
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(drl.getBytes()), ResourceType.DRL);
    assertTrue(kbuilder.getErrors().toString().contains("StackOverflowError"));
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)376 Test (org.junit.Test)302 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)231 KieSession (org.kie.api.runtime.KieSession)149 ArrayList (java.util.ArrayList)112 List (java.util.List)65 ByteArrayResource (org.drools.core.io.impl.ByteArrayResource)52 KiePackage (org.kie.api.definition.KiePackage)39 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)38 FactHandle (org.kie.api.runtime.rule.FactHandle)36 FactType (org.kie.api.definition.type.FactType)27 KieBaseConfiguration (org.kie.api.KieBaseConfiguration)24 Person (org.drools.compiler.Person)23 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)23 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)22 InternalFactHandle (org.drools.core.common.InternalFactHandle)19 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)17 TraitableBean (org.drools.core.factmodel.traits.TraitableBean)17 StringReader (java.io.StringReader)15 Resource (org.kie.api.io.Resource)15