use of org.kie.api.runtime.KieContainer in project opennms by OpenNMS.
the class DroolsNorthbounder method initializeDroolsEngine.
/**
* Initialize drools engine.
*
* @throws Exception the exception
*/
private void initializeDroolsEngine() throws Exception {
KieServices ks = KieServices.Factory.get();
KieFileSystem kFileSystem = ks.newKieFileSystem();
for (String ruleFile : m_engine.getRuleFiles()) {
LOG.debug("Loading rules file: {}", ruleFile);
kFileSystem.write("src/main/resources/" + ruleFile, ks.getResources().newFileSystemResource(new File(ruleFile)));
}
KieBuilder kbuilder = ks.newKieBuilder(kFileSystem);
kbuilder.buildAll();
if (kbuilder.getResults().hasMessages(org.kie.api.builder.Message.Level.ERROR)) {
LOG.warn("Unable to initialize Drools engine: {}", kbuilder.getResults().getMessages(Level.ERROR));
throw new IllegalStateException("Unable to initialize Drools engine: " + kbuilder.getResults().getMessages(Level.ERROR));
}
KieContainer kContainer = ks.newKieContainer(ks.getRepository().getDefaultReleaseId());
AssertBehaviour behaviour = AssertBehaviour.determineAssertBehaviour(m_engine.getAssertBehaviour());
RuleBaseConfiguration ruleBaseConfig = new RuleBaseConfiguration();
ruleBaseConfig.setAssertBehaviour(behaviour);
ruleBaseConfig.setEventProcessingMode(EventProcessingOption.STREAM);
m_kieBase = kContainer.newKieBase(ruleBaseConfig);
m_kieSession = m_kieBase.newKieSession();
m_kieSession.setGlobal("engine", this);
unmarshallStateFromDisk(true);
ApplicationContext ctx = m_context;
if (m_engine.getAppContext() != null) {
ctx = new FileSystemXmlApplicationContext(new String[] { m_engine.getAppContext() }, m_context);
}
for (Global global : m_engine.getGlobals()) {
m_kieSession.setGlobal(global.getName(), global.constructValue(ctx));
}
new Thread(() -> {
Logging.putPrefix(getName());
LOG.debug("Starting task thread for {}", getName());
m_kieSession.fireUntilHalt();
LOG.debug("Stopping task thread for {}", getName());
}, "FireTask-" + getName()).start();
}
use of org.kie.api.runtime.KieContainer in project drools by kiegroup.
the class XSDResourceTest method testXSDResourceNotBreakingCompilation.
@Test
public void testXSDResourceNotBreakingCompilation() {
final KieContainer kcontainer = KieServices.Factory.get().getKieClasspathContainer();
final KieBase kieBase = kcontainer.getKieBase("xsdKieBase");
Assertions.assertThat(kieBase).as("Created KieBase with XSD should not be null").isNotNull();
}
use of org.kie.api.runtime.KieContainer in project drools by kiegroup.
the class IncrementalCompilationTest method testEventDeclarationInSeparatedDRL.
@Test
public void testEventDeclarationInSeparatedDRL() throws Exception {
// DROOLS-1241
String drl1 = "import " + SimpleEvent.class.getCanonicalName() + ";\n" + "declare SimpleEvent\n" + " @role( event )\n" + " @timestamp( timestamp )\n" + " @expires( 2d )\n" + "end\n";
String drl2 = "import " + SimpleEvent.class.getCanonicalName() + ";\n" + "global java.util.List list;\n" + "rule R1 when\n" + " $s:SimpleEvent(code==\"MY_CODE\") over window:time( 1s )\n" + "then\n" + " list.add(\"MY_CODE\");\n" + "end\n";
String drl3 = "import " + SimpleEvent.class.getCanonicalName() + ";\n" + "global java.util.List list;\n" + "rule R2 when\n" + " $s:SimpleEvent(code==\"YOUR_CODE\") over window:time( 1s )\n" + "then\n" + " list.add(\"YOUR_CODE\");\n" + "end\n";
KieServices ks = KieServices.Factory.get();
KieModuleModel kproj = ks.newKieModuleModel();
KieBaseModel kieBaseModel1 = kproj.newKieBaseModel("KBase1").setDefault(true).setEventProcessingMode(EventProcessingOption.STREAM);
KieSessionModel ksession1 = kieBaseModel1.newKieSessionModel("KSession1").setDefault(true).setType(KieSessionModel.KieSessionType.STATEFUL).setClockType(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-cep-upgrade", "1.1.1");
KieModule km = deployJar(ks, createKJar(ks, kproj, releaseId1, null, drl1, drl2));
KieContainer kc = ks.newKieContainer(km.getReleaseId());
KieSession ksession = kc.newKieSession();
List<String> list = new ArrayList<String>();
ksession.setGlobal("list", list);
ksession.insert(new SimpleEvent("1", "MY_CODE", 0));
ksession.insert(new SimpleEvent("2", "YOUR_CODE", 0));
ksession.fireAllRules();
assertEquals(1, list.size());
assertEquals("MY_CODE", list.get(0));
list.clear();
ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-cep-upgrade", "1.1.2");
// the null drl placeholder is used to have the same drl with a different file name
// this causes the removal and readdition of both rules
km = deployJar(ks, createKJar(ks, kproj, releaseId2, null, drl1, drl2, drl3));
Results results = kc.updateToVersion(releaseId2);
assertEquals(0, results.getMessages().size());
ksession.fireAllRules();
assertEquals(1, list.size());
assertEquals("YOUR_CODE", list.get(0));
}
use of org.kie.api.runtime.KieContainer in project drools by kiegroup.
the class IncrementalCompilationTest method testKeepBuilderConfAfterIncrementalUpdate.
@Test
public void testKeepBuilderConfAfterIncrementalUpdate() throws Exception {
// DROOLS-1282
String drl1 = "import " + DummyEvent.class.getCanonicalName() + "\n" + "rule R1 when\n" + " DummyEvent() @watch(id)\n" + "then end\n";
String drl2 = "import " + DummyEvent.class.getCanonicalName() + "\n" + "rule R1 when\n" + " DummyEvent() @watch(*)\n" + "then end\n";
KieServices ks = KieServices.Factory.get();
KieModuleModel kproj = ks.newKieModuleModel().setConfigurationProperty(PropertySpecificOption.PROPERTY_NAME, PropertySpecificOption.ALWAYS.toString());
ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-property-reactive-upgrade", "1");
KieModule km = deployJar(ks, createKJar(ks, kproj, releaseId1, null, drl1));
KieContainer container = ks.newKieContainer(releaseId1);
container.newKieSession();
ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-property-reactive-upgrade", "2");
km = deployJar(ks, createKJar(ks, kproj, releaseId2, null, drl2));
Results results = container.updateToVersion(releaseId2);
assertEquals(0, results.getMessages().size());
}
use of org.kie.api.runtime.KieContainer in project drools by kiegroup.
the class IncrementalCompilationTest method testIncrementalCompilationWithNewEvent.
@Test
public void testIncrementalCompilationWithNewEvent() throws Exception {
// DROOLS-1395
String drl1 = "package fr.edf.distribution.brms.common.test\n" + "import " + DummyEvent.class.getCanonicalName() + "\n" + "declare DummyEvent\n" + " @role( event )\n" + " @timestamp( eventTimestamp )\n" + "end\n" + "rule \"RG_TEST_1\"\n" + " when\n" + " $event: DummyEvent ()\n" + " then\n" + " System.out.println(\"RG_TEST_1 fired\");\n" + " retract($event);\n" + "end";
String drl2 = "package fr.edf.distribution.brms.common.test\n" + "import " + DummyEvent.class.getCanonicalName() + "\n" + "import " + OtherDummyEvent.class.getCanonicalName() + "\n" + "declare DummyEvent\n" + " @role( event )\n" + " @timestamp( eventTimestamp )\n" + "end\n" + "declare OtherDummyEvent\n" + " @role( event )\n" + " @timestamp( eventTimestamp )\n" + "end\n" + "rule \"RG_TEST_2\"\n" + " when\n" + " $event: DummyEvent ()\n" + " $other : OtherDummyEvent(id == $event.id, this after $event)\n" + " then\n" + " System.out.println(\"RG_TEST_2 fired\");\n" + " retract($other);\n" + "end\n" + "\n" + "rule \"RG_TEST_1\"\n" + " when\n" + " $event: DummyEvent ()\n" + " then\n" + " System.out.println(\"RG_TEST_1 fired\");\n" + " retract($event);\n" + "end\n";
KieServices ks = KieServices.Factory.get();
KieModuleModel kproj = ks.newKieModuleModel();
KieBaseModel kieBaseModel1 = kproj.newKieBaseModel("KBase1").setDefault(true).setEventProcessingMode(EventProcessingOption.STREAM);
KieSessionModel ksession1 = kieBaseModel1.newKieSessionModel("KSession1").setDefault(true).setType(KieSessionModel.KieSessionType.STATEFUL).setClockType(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-upgrade", "1.0.0");
deployJar(ks, createKJar(ks, kproj, releaseId1, null, drl1));
KieContainer kc = ks.newKieContainer(releaseId1);
KieSession kieSession = kc.newKieSession();
DummyEvent evt = new DummyEvent("evt");
kieSession.insert(evt);
assertEquals(1, kieSession.fireAllRules());
ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade", "2.0.0");
deployJar(ks, createKJar(ks, kproj, releaseId2, null, drl2));
kc.updateToVersion(releaseId2);
evt = new DummyEvent("evt");
kieSession.insert(evt);
OtherDummyEvent other = new OtherDummyEvent("evt");
kieSession.insert(other);
assertEquals(1, kieSession.fireAllRules());
}
Aggregations