Search in sources :

Example 51 with KieFileSystem

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();
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) KieModuleModel(org.kie.api.builder.model.KieModuleModel) Resource(org.kie.api.io.Resource) KieServices(org.kie.api.KieServices) KieBuilder(org.kie.api.builder.KieBuilder) Before(org.junit.Before)

Example 52 with KieFileSystem

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));
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) KieServices(org.kie.api.KieServices) KieBuilder(org.kie.api.builder.KieBuilder) Test(org.junit.Test)

Example 53 with KieFileSystem

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();
}
Also used : I18nPerson(org.drools.compiler.I18nPerson) KieFileSystem(org.kie.api.builder.KieFileSystem) ArrayList(java.util.ArrayList) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 54 with KieFileSystem

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"));
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) ArrayList(java.util.ArrayList) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 55 with KieFileSystem

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());
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) ArrayList(java.util.ArrayList) KieServices(org.kie.api.KieServices) 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) KieBuilder(org.kie.api.builder.KieBuilder) Person(org.drools.compiler.Person) Test(org.junit.Test)

Aggregations

KieFileSystem (org.kie.api.builder.KieFileSystem)297 KieServices (org.kie.api.KieServices)241 Test (org.junit.Test)211 KieBuilder (org.kie.api.builder.KieBuilder)179 KieSession (org.kie.api.runtime.KieSession)101 KieModuleModel (org.kie.api.builder.model.KieModuleModel)79 KieContainer (org.kie.api.runtime.KieContainer)74 ReleaseId (org.kie.api.builder.ReleaseId)64 Results (org.kie.api.builder.Results)60 InternalKieBuilder (org.kie.internal.builder.InternalKieBuilder)53 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)39 KieBase (org.kie.api.KieBase)38 IncrementalResults (org.kie.internal.builder.IncrementalResults)36 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)32 KieBaseModel (org.kie.api.builder.model.KieBaseModel)31 ArrayList (java.util.ArrayList)30 Message (org.kie.api.builder.Message)26 KieModule (org.kie.api.builder.KieModule)22 Message (org.drools.compiler.Message)20 Resource (org.kie.api.io.Resource)18