use of org.kie.api.builder.KieFileSystem in project drools by kiegroup.
the class LifecycleTest method initSession.
@Before
public void initSession() {
String drlString = "package org.jboss.brms\n" + "import org.drools.compiler.StockTick\n" + "declare StockTick\n" + " @role( event )\n" + " @expires( 4s )\n" + "end\n" + "rule \"TestEventReceived\"\n" + " when\n" + " $event : StockTick() over window:time(4s) from entry-point EventStream\n" + " then\n" + " // do something;\n" + "end";
KieServices ks = KieServices.Factory.get();
KieFileSystem kfs = ks.newKieFileSystem();
final Resource drl = ks.getResources().newByteArrayResource(drlString.getBytes()).setTargetPath("org/jboss/brms/lifecycle.drl");
kfs.write(drl);
KieModuleModel kmoduleModel = ks.newKieModuleModel();
kmoduleModel.newKieBaseModel("defaultKBase").addPackage("*").setEventProcessingMode(EventProcessingOption.STREAM).newKieSessionModel("defaultKieSession").setDefault(true).setClockType(ClockTypeOption.get("pseudo"));
kfs.writeKModuleXML(kmoduleModel.toXML());
KieBuilder builder = ks.newKieBuilder(kfs).buildAll();
assertEquals(0, builder.getResults().getMessages().size());
this.kieSession = ks.newKieContainer(ks.getRepository().getDefaultReleaseId()).newKieSession();
}
use of org.kie.api.builder.KieFileSystem in project drools by kiegroup.
the class ExtendsTest method testDeclareExtendsWithFullyQualifiedName.
@Test
public void testDeclareExtendsWithFullyQualifiedName() {
String drl = "package org.drools.extends.test; \n" + "" + "declare org.drools.extends.test.Foo end \n" + "declare org.drools.extends.test.Bar extends org.drools.extends.test.Foo end \n" + "";
KieServices kieServices = KieServices.Factory.get();
KieFileSystem kfs = kieServices.newKieFileSystem();
kfs.write(kieServices.getResources().newByteArrayResource(drl.getBytes()).setSourcePath("test.drl").setResourceType(ResourceType.DRL));
KieBuilder kieBuilder = kieServices.newKieBuilder(kfs);
kieBuilder.buildAll();
assertFalse(kieBuilder.getResults().hasMessages(Message.Level.ERROR));
}
use of org.kie.api.builder.KieFileSystem in project drools by kiegroup.
the class I18nTest method testKieFileSystem.
@Test
public void testKieFileSystem() {
String str = "package org.drools.compiler.i18ntest;\n" + "import org.drools.compiler.I18nPerson;\n" + "\n" + "global java.util.List list;\n" + "rule \"名称 is 山田花子\"\n" + " when\n" + " p : I18nPerson( 名称 == \"山田花子\" )\n" + " then\n" + " list.add( \"名称は山田花子です\" );\n" + "end\n";
KieServices ks = KieServices.Factory.get();
KieFileSystem kfs = ks.newKieFileSystem().write("src/main/resources/r1.drl", str);
ks.newKieBuilder(kfs).buildAll();
KieSession ksession = ks.newKieContainer(ks.getRepository().getDefaultReleaseId()).newKieSession();
List list = new ArrayList();
ksession.setGlobal("list", list);
I18nPerson i18nPerson = new I18nPerson();
i18nPerson.set名称("山田花子");
ksession.insert(i18nPerson);
ksession.fireAllRules();
assertTrue(list.contains("名称は山田花子です"));
ksession.dispose();
}
use of org.kie.api.builder.KieFileSystem in project drools by kiegroup.
the class Misc2Test method testWiringClassOnPackageMerge.
@Test
public void testWiringClassOnPackageMerge() throws Exception {
String drl_init = "package init;\n" + "import org.kie.test.TestObject\n" + "rule RInit when\n" + "then\n" + " TestObject obj1 = new TestObject();\n" + " TestObject obj2 = new TestObject();" + " obj1.add(obj2);" + " insert(obj1);\n" + "end";
String drl1 = "package p1;\n" + "import org.kie.test.TestObject\n" + "global java.util.List list;\n" + "rule R1 when\n" + " $obj : TestObject( $objs : objects )\n" + " $s : Object() from $objs\n" + "then\n" + " list.add(\"R1\");\n" + "end";
String drl2 = "package p2;\n" + "import org.kie.test.TestObject\n" + "global java.util.List list;\n" + "rule R2 when\n" + " $obj : TestObject( $objs : objects )\n" + " $s : TestObject() from $objs\n" + "then\n" + " list.add(\"R2\");\n" + "end";
String javaSrc = "package org.kie.test;\n" + "import java.util.*;\n" + "\n" + "public class TestObject {\n" + " private final List<TestObject> objects = new ArrayList<TestObject>();\n" + "\n" + " public List<TestObject> getObjects() {\n" + " return objects;\n" + " }\n" + " public void add(TestObject obj) {\n" + " objects.add(obj);" + " }" + "}\n";
String path = "org/kie/test/MyRuleUnit";
KieServices ks = KieServices.Factory.get();
KieFileSystem kfs = ks.newKieFileSystem();
kfs.writeKModuleXML(ks.newKieModuleModel().toXML()).write("src/main/resources/a.drl", drl_init).write("src/main/resources/b.drl", drl1).write("src/main/resources/c.drl", drl2).write("src/main/java/org/kie/test/TestObject.java", javaSrc);
ks.newKieBuilder(kfs).buildAll();
KieContainer kcontainer = ks.newKieContainer(ks.getRepository().getDefaultReleaseId());
KieSession kSession = kcontainer.newKieSession();
List<String> list = new ArrayList<String>();
kSession.setGlobal("list", list);
kSession.fireAllRules();
assertEquals(2, list.size());
assertTrue(list.contains("R1"));
assertTrue(list.contains("R2"));
}
use of org.kie.api.builder.KieFileSystem in project drools by kiegroup.
the class Misc2Test method testIsAWith2KContainers.
@Test
public void testIsAWith2KContainers() {
// BZ-996056
String str = "import org.drools.compiler.Person\n" + "\n" + "global java.util.List students\n" + "\n" + "declare trait Student\n" + " school : String\n" + "end\n" + "\n" + "rule \"create student\" \n" + " when\n" + " $student : Person( age < 26, this not isA Student )\n" + " then\n" + " Student s = don( $student, Student.class );\n" + " s.setSchool(\"Masaryk University\");\n" + " update( $student );\n" + "end\n" + "\n" + "rule \"found student\"\n" + " salience 10\n" + " when\n" + " student : Person( this isA Student )\n" + " then\n" + " students.add(student);\n" + "end";
KieServices ks = KieServices.Factory.get();
KieFileSystem kfs = ks.newKieFileSystem();
kfs.write("src/main/resources/isA.drl", str);
KieBuilder kbuilder = ks.newKieBuilder(kfs);
kbuilder.buildAll();
assertEquals(0, kbuilder.getResults().getMessages().size());
ks.newKieContainer(kbuilder.getKieModule().getReleaseId()).getKieBase();
KieSession ksession = ks.newKieContainer(kbuilder.getKieModule().getReleaseId()).newKieSession();
assertNotNull(ksession);
List students = new ArrayList();
ksession.setGlobal("students", students);
ksession.insert(new Person("tom", 20));
ksession.fireAllRules();
assertEquals(1, students.size());
}
Aggregations