Search in sources :

Example 41 with KieFileSystem

use of org.kie.api.builder.KieFileSystem in project drools by kiegroup.

the class KieBuilderTest method testMultipleKBaseWithDrlError.

@Test
public void testMultipleKBaseWithDrlError() {
    // RHBRMS-2651
    final String drl = "package org.drools.compiler;\n" + "rule \"test\"\n" + "  when\n" + "    Smurf\n" + "  then\n" + "end";
    final KieServices ks = KieServices.Factory.get();
    final KieModuleModel kproj = ks.newKieModuleModel();
    kproj.newKieBaseModel("kbase1").newKieSessionModel("ksession1").setDefault(true);
    kproj.newKieBaseModel("kbase2").newKieSessionModel("ksession2");
    final ReleaseId releaseId = ks.newReleaseId("org.kie", "test-kie-builder", "1.0.0");
    final KieFileSystem kfs = ks.newKieFileSystem().generateAndWritePomXML(releaseId).writeKModuleXML(kproj.toXML());
    final Resource drlResource = ResourceFactory.newByteArrayResource(drl.getBytes()).setResourceType(ResourceType.DRL).setSourcePath("kbase1/drl1.drl");
    kfs.write("src/main/resources/org/drools/compiler/drl1.drl", drlResource);
    final KieBuilder kb = ks.newKieBuilder(kfs).buildAll();
    final List<org.kie.api.builder.Message> messages = kb.getResults().getMessages(org.kie.api.builder.Message.Level.ERROR);
    assertEquals(4, messages.size());
    assertTrue(messages.get(0).toString().contains("kbase1"));
    assertTrue(messages.get(1).toString().contains("kbase1"));
    assertTrue(messages.get(2).toString().contains("kbase2"));
    assertTrue(messages.get(3).toString().contains("kbase2"));
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) Message(org.drools.compiler.Message) KieModuleModel(org.kie.api.builder.model.KieModuleModel) Resource(org.kie.api.io.Resource) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) InternalKieBuilder(org.kie.internal.builder.InternalKieBuilder) KieBuilder(org.kie.api.builder.KieBuilder) Test(org.junit.Test)

Example 42 with KieFileSystem

use of org.kie.api.builder.KieFileSystem in project drools by kiegroup.

the class KieCompilationCacheTest method testCacheWigAccumulate.

@Test
public void testCacheWigAccumulate() throws Exception {
    String drl1 = "package org.drools.compiler.integrationtests\n" + "rule R11 when\n" + "   Number() from accumulate(String(), \n" + "              init(int x = 0;)," + "              action(x++;)," + "              reverse(x--;)," + "              result(x))\n" + "then\n" + "end\n";
    KieServices ks = KieServices.Factory.get();
    ReleaseId releaseId = ks.newReleaseId("org.kie", "cache-accumulate", "1.0-SNAPSHOT");
    KieFileSystem kfs = ks.newKieFileSystem().generateAndWritePomXML(releaseId).write("src/main/resources/KBase1/org/pkg1/r1.drl", drl1).writeKModuleXML(createKieProjectWithPackagesAnd2KieBases(ks).toXML());
    ks.newKieBuilder(kfs).buildAll();
    InternalKieModule kieModule = (InternalKieModule) ks.getRepository().getKieModule(releaseId);
    byte[] jar = kieModule.getBytes();
    MemoryFileSystem mfs = MemoryFileSystem.readFromJar(jar);
    File file = mfs.getFile(KieBuilderImpl.getCompilationCachePath(releaseId, "KBase1"));
    assertNotNull(file);
    Resource jarRes = ks.getResources().newByteArrayResource(jar);
    KieModule km = ks.getRepository().addKieModule(jarRes);
    KieSession ksession = ks.newKieContainer(km.getReleaseId()).newKieSession("KSession1");
    ksession.insert(new String("Hello World"));
    assertEquals(1, ksession.fireAllRules());
}
Also used : MemoryFileSystem(org.drools.compiler.compiler.io.memory.MemoryFileSystem) KieFileSystem(org.kie.api.builder.KieFileSystem) Resource(org.kie.api.io.Resource) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) ReleaseId(org.kie.api.builder.ReleaseId) File(org.drools.compiler.compiler.io.File) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieModule(org.kie.api.builder.KieModule) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) Test(org.junit.Test)

Example 43 with KieFileSystem

use of org.kie.api.builder.KieFileSystem in project drools by kiegroup.

the class KieCompilationCacheTest method testHelloWorldWithPackagesAnd2KieBases.

@Test
public void testHelloWorldWithPackagesAnd2KieBases() throws Exception {
    String drl1 = "package org.drools.compiler.integrationtests\n" + "import " + Message.class.getCanonicalName() + "\n" + "rule R11 when\n" + "   $m : Message( message == \"Hello World\" )\n" + "then\n" + "end\n" + "rule R12 when\n" + "   $m : Message( message == \"Hi Universe\" )\n" + "then\n" + "end\n";
    String drl2 = "package org.drools.compiler.integrationtests\n" + "import " + Message.class.getCanonicalName() + "\n" + "rule R21 when\n" + "   $m : Message( message == \"Hello World\" )\n" + "then\n" + "end\n" + "rule R22 when\n" + "   $m : Message( message == \"Aloha Earth\" )\n" + "then\n" + "end\n";
    KieServices ks = KieServices.Factory.get();
    ReleaseId releaseId = ks.newReleaseId("org.kie", "hello-world", "1.0-SNAPSHOT");
    KieFileSystem kfs = ks.newKieFileSystem().generateAndWritePomXML(releaseId).write("src/main/resources/KBase1/org/pkg1/r1.drl", drl1).write("src/main/resources/KBase1/org/pkg2/r2.drl", drl2).writeKModuleXML(createKieProjectWithPackagesAnd2KieBases(ks).toXML());
    ks.newKieBuilder(kfs).buildAll();
    InternalKieModule kieModule = (InternalKieModule) ks.getRepository().getKieModule(releaseId);
    byte[] jar = kieModule.getBytes();
    MemoryFileSystem mfs = MemoryFileSystem.readFromJar(jar);
    File file = mfs.getFile(KieBuilderImpl.getCompilationCachePath(releaseId, "KBase1"));
    assertNotNull(file);
    file = mfs.getFile(KieBuilderImpl.getCompilationCachePath(releaseId, "KBase2"));
    assertNotNull(file);
    Resource jarRes = ks.getResources().newByteArrayResource(jar);
    KieModule km = ks.getRepository().addKieModule(jarRes);
    KieSession ksession = ks.newKieContainer(km.getReleaseId()).newKieSession("KSession1");
    ksession.insert(new Message("Hello World"));
    assertEquals(1, ksession.fireAllRules());
    ksession = ks.newKieContainer(km.getReleaseId()).newKieSession("KSession1");
    ksession.insert(new Message("Hi Universe"));
    assertEquals(1, ksession.fireAllRules());
    ksession = ks.newKieContainer(km.getReleaseId()).newKieSession("KSession1");
    ksession.insert(new Message("Aloha Earth"));
    assertEquals(0, ksession.fireAllRules());
    ksession = ks.newKieContainer(km.getReleaseId()).newKieSession("KSession2");
    ksession.insert(new Message("Hello World"));
    assertEquals(1, ksession.fireAllRules());
    ksession = ks.newKieContainer(km.getReleaseId()).newKieSession("KSession2");
    ksession.insert(new Message("Hi Universe"));
    assertEquals(0, ksession.fireAllRules());
    ksession = ks.newKieContainer(km.getReleaseId()).newKieSession("KSession2");
    ksession.insert(new Message("Aloha Earth"));
    assertEquals(1, ksession.fireAllRules());
}
Also used : MemoryFileSystem(org.drools.compiler.compiler.io.memory.MemoryFileSystem) KieFileSystem(org.kie.api.builder.KieFileSystem) Message(org.drools.compiler.Message) Resource(org.kie.api.io.Resource) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) ReleaseId(org.kie.api.builder.ReleaseId) File(org.drools.compiler.compiler.io.File) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieModule(org.kie.api.builder.KieModule) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) Test(org.junit.Test)

Example 44 with KieFileSystem

use of org.kie.api.builder.KieFileSystem in project drools by kiegroup.

the class KieDefaultPackageTest method testModelInDefaultPackage2.

@Test
public void testModelInDefaultPackage2() throws Exception {
    // This Model will be in the "default package"
    String model_drl = "" + "declare Smurf\n" + "Field1 : String\n" + "end\n";
    // This DRL is in a named package, but imports the model (trying with defaultPkg prefix)
    String drl = "" + "package org.smurf\n" + "import defaultpkg.Smurf\n" + "rule \"test\"\n" + "when\n" + "Smurf()\n" + "then\n" + "end";
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem();
    kfs.write("src/main/resources/model.drl", model_drl);
    kfs.write("src/main/resources/drl.drl", drl);
    KieBuilder builder = ks.newKieBuilder(kfs).buildAll();
    assertEquals(0, builder.getResults().getMessages().size());
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) KieServices(org.kie.api.KieServices) KieBuilder(org.kie.api.builder.KieBuilder) Test(org.junit.Test)

Example 45 with KieFileSystem

use of org.kie.api.builder.KieFileSystem in project drools by kiegroup.

the class KieHelloWorldTest method testHelloWorldWithPackagesAnd2KieBases.

@Test
public void testHelloWorldWithPackagesAnd2KieBases() throws Exception {
    String drl1 = "package org.drools.compiler.integrationtests\n" + "import " + Message.class.getCanonicalName() + "\n" + "rule R11 when\n" + "   $m : Message( message == \"Hello World\" )\n" + "then\n" + "end\n" + "rule R12 when\n" + "   $m : Message( message == \"Hi Universe\" )\n" + "then\n" + "end\n";
    String drl2 = "package org.drools.compiler.integrationtests\n" + "import " + Message.class.getCanonicalName() + "\n" + "rule R21 when\n" + "   $m : Message( message == \"Hello World\" )\n" + "then\n" + "end\n" + "rule R22 when\n" + "   $m : Message( message == \"Aloha Earth\" )\n" + "then\n" + "end\n";
    KieServices ks = KieServices.Factory.get();
    ReleaseId releaseId = ks.newReleaseId("org.kie", "hello-world", "1.0-SNAPSHOT");
    KieFileSystem kfs = ks.newKieFileSystem().generateAndWritePomXML(releaseId).write("src/main/resources/KBase1/org/pkg1/r1.drl", drl1).write("src/main/resources/KBase1/org/pkg2/r2.drl", drl2).writeKModuleXML(createKieProjectWithPackagesAnd2KieBases(ks).toXML());
    ks.newKieBuilder(kfs).buildAll();
    KieSession ksession = ks.newKieContainer(releaseId).newKieSession("KSession1");
    ksession.insert(new Message("Hello World"));
    assertEquals(1, ksession.fireAllRules());
    ksession = ks.newKieContainer(releaseId).newKieSession("KSession1");
    ksession.insert(new Message("Hi Universe"));
    assertEquals(1, ksession.fireAllRules());
    ksession = ks.newKieContainer(releaseId).newKieSession("KSession1");
    ksession.insert(new Message("Aloha Earth"));
    assertEquals(0, ksession.fireAllRules());
    ksession = ks.newKieContainer(releaseId).newKieSession("KSession2");
    ksession.insert(new Message("Hello World"));
    assertEquals(1, ksession.fireAllRules());
    ksession = ks.newKieContainer(releaseId).newKieSession("KSession2");
    ksession.insert(new Message("Hi Universe"));
    assertEquals(0, ksession.fireAllRules());
    ksession = ks.newKieContainer(releaseId).newKieSession("KSession2");
    ksession.insert(new Message("Aloha Earth"));
    assertEquals(1, ksession.fireAllRules());
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) Message(org.drools.compiler.Message) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) ReleaseId(org.kie.api.builder.ReleaseId) 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