use of org.kie.api.builder.KieModule in project drools by kiegroup.
the class SimulateTestBase method createKJarWithMultipleResources.
public static ReleaseId createKJarWithMultipleResources(String id, String[] resources, ResourceType[] types) {
KieServices ks = KieServices.Factory.get();
KieModuleModel kproj = ks.newKieModuleModel();
KieFileSystem kfs = ks.newKieFileSystem();
for (int i = 0; i < resources.length; i++) {
String res = resources[i];
String type = types[i].getDefaultExtension();
kfs.write("src/main/resources/" + id.replaceAll("\\.", "/") + "/org/test/res" + i + "." + type, res);
}
KieBaseModel kBase1 = kproj.newKieBaseModel(id).setEqualsBehavior(EqualityBehaviorOption.EQUALITY).setEventProcessingMode(EventProcessingOption.STREAM);
KieSessionModel ksession1 = kBase1.newKieSessionModel(id + ".KSession1").setDefault(true).setType(KieSessionModel.KieSessionType.STATEFUL).setClockType(ClockTypeOption.get("pseudo"));
kfs.writeKModuleXML(kproj.toXML());
KieBuilder kieBuilder = ks.newKieBuilder(kfs).buildAll();
assertTrue(kieBuilder.getResults().getMessages().isEmpty());
KieModule kieModule = kieBuilder.getKieModule();
return kieModule.getReleaseId();
}
use of org.kie.api.builder.KieModule in project drools by kiegroup.
the class NegativePatternsTest method prepareKieSession.
@Before
public void prepareKieSession() {
String drl = "package org.drools.compiler.integrationtests\n" + "\n" + "import org.drools.compiler.integrationtests.NegativePatternsTest.TestEvent\n" + "\n" + "declare TestEvent\n" + " @role( event )\n" + " @expires( 22ms )\n" + "end\n" + "\n" + "rule \"SingleAbsence\"\n" + " duration( 18ms )\n" + " when\n" + " not ( TestEvent ( ) from entry-point EventStream )\n" + " then\n" + " // consequence\n" + "end\n" + "\n" + "rule \"SingleConstrained\"\n" + " duration( 18ms )\n" + " when\n" + " not ( TestEvent ( name == \"EventA\" ) from entry-point EventStream )\n" + " then\n" + " // consequence\n" + "end\n" + "\n" + "rule \"MultipleEvents\"\n" + " duration( 18ms )\n" + " when\n" + " TestEvent ( name == \"EventA\" ) over window:time( 22ms ) from entry-point EventStream\n" + " not ( TestEvent ( name == \"EventB\" ) over window:time( 22ms ) from entry-point EventStream )\n" + " then\n" + " // consequence\n" + "end\n" + "\n" + "rule \"MultipleEntryPoints\"\n" + " duration( 18ms )\n" + " when\n" + " not (\n" + " TestEvent( name == \"EventA\" ) from entry-point EventStream\n" + " )\n" + " not (\n" + " TestEvent( name == \"EventB\" ) from entry-point OtherStream\n" + " )\n" + " then\n" + " // consequence\n" + "end\n";
KieServices ks = KieServices.Factory.get();
KieModuleModel kieModule = ks.newKieModuleModel();
KieBaseModel defaultBase = kieModule.newKieBaseModel("defaultKBase").setDefault(true).addPackage("*").setEventProcessingMode(EventProcessingOption.STREAM);
defaultBase.newKieSessionModel(DEFAULT_SESSION_NAME).setClockType(ClockTypeOption.get("pseudo")).setDefault(true);
KieFileSystem kfs = ks.newKieFileSystem().write("src/main/resources/r1.drl", drl);
kfs.writeKModuleXML(kieModule.toXML());
KieModule builtModule = ks.newKieBuilder(kfs).buildAll().getKieModule();
ks.getRepository().addKieModule(builtModule);
ksession = ks.newKieContainer(ks.getRepository().getDefaultReleaseId()).newKieSession(DEFAULT_SESSION_NAME);
firedRulesListener = new TrackingAgendaEventListener();
ksession.addEventListener(firedRulesListener);
}
use of org.kie.api.builder.KieModule in project drools by kiegroup.
the class KieBuilderTest method testStatelessSessionDeclarativeChannelRegistration.
@Test
public void testStatelessSessionDeclarativeChannelRegistration() {
final String drl1 = "package org.drools.compiler\n" + "rule R1 when\n" + " $m : Message()\n" + "then\n" + "end\n";
final String kmodule = "<kmodule xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \n" + " xmlns=\"http://www.drools.org/xsd/kmodule\">\n" + " <kbase name=\"kbase1\">\n" + " <ksession name=\"ksession1\" default=\"true\" type=\"stateless\">\n" + " <channels>\n" + " <channel name=\"testChannel\" type=\"org.drools.compiler.integrationtests.KieBuilderTest$MockChannel\" />\n" + " </channels>\n" + " </ksession>" + " </kbase>\n" + "</kmodule>";
final KieServices ks = KieServices.Factory.get();
final ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-kie-builder", "1.0.0");
final Resource r1 = ResourceFactory.newByteArrayResource(drl1.getBytes()).setResourceType(ResourceType.DRL).setSourcePath("kbase1/drl1.drl");
final KieModule km = createAndDeployJar(ks, kmodule, releaseId1, r1);
KieContainer kieContainer = ks.newKieContainer(km.getReleaseId());
StatelessKieSession statelessKieSession = kieContainer.newStatelessKieSession();
assertEquals(1, statelessKieSession.getChannels().size());
assertTrue(statelessKieSession.getChannels().keySet().contains("testChannel"));
}
use of org.kie.api.builder.KieModule 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.KieModule 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());
}
Aggregations