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