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();
}
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"));
}
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);
}
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());
}
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"));
}
Aggregations