use of org.drools.compiler.Message in project drools by kiegroup.
the class KieHelloWorldTest method testHelloWorldWithPackages.
@Test
public void testHelloWorldWithPackages() throws Exception {
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", createDrl("R1")).write("src/main/resources/KBase1/org/pkg2/r2.drl", createDrl("R2")).writeKModuleXML(createKieProjectWithPackages(ks, "org.pkg1").toXML());
ks.newKieBuilder(kfs).buildAll();
KieSession ksession = ks.newKieContainer(releaseId).newKieSession("KSession1");
ksession.insert(new Message("Hello World"));
int count = ksession.fireAllRules();
assertEquals(1, count);
}
use of org.drools.compiler.Message in project drools by kiegroup.
the class KieHelloWorldTest method testHelloWorldWithResource.
@Test
public void testHelloWorldWithResource() throws Exception {
// DROOLS-351
KieServices ks = KieServices.Factory.get();
KieFileSystem kfs = ks.newKieFileSystem().write(ks.getResources().newReaderResource(new StringReader(createDrl("R1"))).setResourceType(ResourceType.DRL).setSourcePath("src/main/resources/r1.txt"));
ks.newKieBuilder(kfs).buildAll();
KieSession ksession = ks.newKieContainer(ks.getRepository().getDefaultReleaseId()).newKieSession();
ksession.insert(new Message("Hello World"));
int count = ksession.fireAllRules();
assertEquals(1, count);
}
use of org.drools.compiler.Message in project drools by kiegroup.
the class KieLoggersTest method testDeclarativeKieFileLogger.
@Test
public void testDeclarativeKieFileLogger() throws Exception {
String drl = "package org.drools.integrationtests\n" + "import org.drools.compiler.Message;\n" + "rule \"Hello World\"\n" + " when\n" + " m : Message( myMessage : message )\n" + " then\n" + "end";
String fileName = "target/testKieFileLogger";
File file = new File(fileName + ".log");
if (file.exists()) {
file.delete();
}
KieServices ks = KieServices.Factory.get();
KieModuleModel kproj = ks.newKieModuleModel();
kproj.newKieBaseModel("KBase1").newKieSessionModel("KSession1").setFileLogger(fileName);
KieFileSystem kfs = ks.newKieFileSystem();
kfs.writeKModuleXML(kproj.toXML());
kfs.write("src/main/resources/KBase1/rule.drl", drl);
KieModule kieModule = ks.newKieBuilder(kfs).buildAll().getKieModule();
KieContainer kieContainer = ks.newKieContainer(kieModule.getReleaseId());
KieSession ksession = kieContainer.newKieSession("KSession1");
ksession.insert(new Message("Hello World"));
int fired = ksession.fireAllRules();
assertEquals(1, fired);
// disposing the ksession also flushes and closes the logger
ksession.dispose();
file = new File(fileName + ".log");
assertTrue(file.exists());
file.delete();
}
use of org.drools.compiler.Message in project drools by kiegroup.
the class KieLoggersTest method testDeclarativeKieConsoleLogger.
@Test
public void testDeclarativeKieConsoleLogger() throws Exception {
String drl = "package org.drools.integrationtests\n" + "import org.drools.compiler.Message;\n" + "rule \"Hello World\"\n" + " when\n" + " m : Message( myMessage : message )\n" + " then\n" + "end";
KieServices ks = KieServices.Factory.get();
KieModuleModel kproj = ks.newKieModuleModel();
kproj.newKieBaseModel("KBase1").newKieSessionModel("KSession1").setConsoleLogger("logger");
KieFileSystem kfs = ks.newKieFileSystem();
kfs.writeKModuleXML(kproj.toXML());
kfs.write("src/main/resources/KBase1/rule.drl", drl);
KieModule kieModule = ks.newKieBuilder(kfs).buildAll().getKieModule();
KieContainer kieContainer = ks.newKieContainer(kieModule.getReleaseId());
KieSession ksession = kieContainer.newKieSession("KSession1");
ksession.insert(new Message("Hello World"));
int fired = ksession.fireAllRules();
assertEquals(1, fired);
KieRuntimeLogger logger = ksession.getLogger();
assertNotNull(logger);
logger.close();
}
use of org.drools.compiler.Message in project drools by kiegroup.
the class KieLoggersTest method testKieFileLoggerWithImmediateFlushing.
@Test
public void testKieFileLoggerWithImmediateFlushing() throws Exception {
// DROOLS-991
String drl = "package org.drools.integrationtests\n" + "import org.drools.compiler.Message;\n" + "rule \"Hello World\"\n" + " when\n" + " m : Message( myMessage : message )\n" + " then\n" + "end";
// get the resource
Resource dt = ResourceFactory.newByteArrayResource(drl.getBytes()).setTargetPath("org/drools/integrationtests/hello.drl");
// create the builder
KieSession ksession = getKieSession(dt);
String fileName = "target/testKieFileLogger";
File file = new File(fileName + ".log");
if (file.exists()) {
file.delete();
}
// Setting maxEventsInMemory to 0 makes all events to be immediately flushed to the file
KieRuntimeLogger logger = KieServices.Factory.get().getLoggers().newFileLogger(ksession, fileName, 0);
ksession.insert(new Message("Hello World"));
int fired = ksession.fireAllRules();
assertEquals(1, fired);
// check that the file has been populated before closing it
file = new File(fileName + ".log");
assertTrue(file.exists());
assertTrue(file.length() > 0);
logger.close();
file.delete();
}
Aggregations