use of org.kie.api.runtime.StatelessKieSession in project jbpm by kiegroup.
the class MBeansMonitoringWithJBpmTest method testRulesAndProcesses.
@Test
public void testRulesAndProcesses() throws Exception {
String drl = "package org.drools.droolsjbpm_integration_testmgt\n" + "rule S\n" + "when\n" + " $s: String() \n" + "then\n" + " try { Thread.sleep(10); } catch (Exception e) { }\n" + " System.out.println($s); \n" + "end\n";
String process = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> \n" + "<definitions id=\"Definition\"\n" + " targetNamespace=\"http://www.example.org/MinimalExample\"\n" + " typeLanguage=\"http://www.java.com/javaTypes\"\n" + " expressionLanguage=\"http://www.mvel.org/2.0\"\n" + " xmlns=\"http://www.omg.org/spec/BPMN/20100524/MODEL\"\n" + " xmlns:xs=\"http://www.w3.org/2001/XMLSchema-instance\"\n" + " xs:schemaLocation=\"http://www.omg.org/spec/BPMN/20100524/MODEL BPMN20.xsd\"\n" + " xmlns:bpmndi=\"http://www.omg.org/spec/BPMN/20100524/DI\"\n" + " xmlns:dc=\"http://www.omg.org/spec/DD/20100524/DC\"\n" + " xmlns:di=\"http://www.omg.org/spec/DD/20100524/DI\"\n" + " xmlns:tns=\"http://www.jboss.org/drools\">\n" + "\n" + " <process processType=\"Private\" isExecutable=\"true\" id=\"com.sample.HelloWorld\" name=\"Hello World\" >\n" + "\n" + " <!-- nodes -->\n" + " <startEvent id=\"_a1\" name=\"StartProcess\" />\n" + " <scriptTask id=\"_a2\" name=\"Hello\" >\n" + " <script>System.out.println(\"Hello World\");</script>\n" + " </scriptTask>\n" + " <endEvent id=\"_a3\" name=\"EndProcess\" >\n" + " <terminateEventDefinition/>\n" + " </endEvent>\n" + "\n" + " <!-- connections -->\n" + " <sequenceFlow id=\"_a1-_a2\" sourceRef=\"_a1\" targetRef=\"_a2\" />\n" + " <sequenceFlow id=\"_a2-_a3\" sourceRef=\"_a2\" targetRef=\"_a3\" />\n" + "\n" + " </process>" + " <process processType=\"Private\" isExecutable=\"true\" id=\"com.sample.CiaoWorld\" name=\"Ciao World\" >\n" + "\n" + " <!-- nodes -->\n" + " <startEvent id=\"_b1\" name=\"StartProcess\" />\n" + " <scriptTask id=\"_b2\" name=\"Ciao\" >\n" + " <script>System.out.println(\"Ciao World\");</script>\n" + " </scriptTask>\n" + " <endEvent id=\"_b3\" name=\"EndProcess\" >\n" + " <terminateEventDefinition/>\n" + " </endEvent>\n" + "\n" + " <!-- connections -->\n" + " <sequenceFlow id=\"_b1-_b2\" sourceRef=\"_b1\" targetRef=\"_b2\" />\n" + " <sequenceFlow id=\"_b2-_b3\" sourceRef=\"_b2\" targetRef=\"_b3\" />\n" + "\n" + " </process>" + "</definitions>";
KieServices ks = KieServices.Factory.get();
KieModuleModel kproj = ks.newKieModuleModel();
KieBaseModel kieBaseModel1 = kproj.newKieBaseModel(KBASE1).setDefault(true).setEventProcessingMode(EventProcessingOption.STREAM);
KieSessionModel ksessionModel1 = kieBaseModel1.newKieSessionModel(KSESSION1).setDefault(true).setType(KieSessionModel.KieSessionType.STATEFUL).setClockType(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
KieSessionModel ksessionModel2 = kieBaseModel1.newKieSessionModel(KSESSION2).setDefault(false).setType(KieSessionModel.KieSessionType.STATELESS);
ReleaseId releaseId1 = ks.newReleaseId("org.kie.test", "mbeans", "1.0.0");
KieFileSystem kfs = ks.newKieFileSystem();
kfs.writeKModuleXML(kproj.toXML());
kfs.generateAndWritePomXML(releaseId1);
kfs.write("src/main/resources/r" + 1 + ".drl", drl);
kfs.write("src/main/resources/p" + 1 + ".bpmn2", process);
KieBuilder kb = ks.newKieBuilder(kfs).buildAll();
if (kb.getResults().hasMessages(org.kie.api.builder.Message.Level.ERROR)) {
for (org.kie.api.builder.Message result : kb.getResults().getMessages()) {
LOG.error(result.getText());
}
}
InternalKieModule kieModule = (InternalKieModule) ks.getRepository().getKieModule(releaseId1);
KieContainer kc = ks.newKieContainer(KCONTAINER1, releaseId1);
KieBase kiebase = kc.getKieBase(KBASE1);
Properties ksessionConfigProps = new Properties();
ksessionConfigProps.setProperty("drools.processSignalManagerFactory", DefaultSignalManagerFactory.class.getName());
ksessionConfigProps.setProperty("drools.processInstanceManagerFactory", DefaultProcessInstanceManagerFactory.class.getName());
KieSessionConfiguration sessionConf = ks.newKieSessionConfiguration(ksessionConfigProps);
sessionConf.setOption(ForceEagerActivationOption.YES);
KieSession ksession = kc.newKieSession(KSESSION1, sessionConf);
MBeanServer mbserver = ManagementFactory.getPlatformMBeanServer();
KieSessionMonitoringMXBean aggrMonitor = JMX.newMXBeanProxy(mbserver, DroolsManagementAgent.createObjectNameBy(KCONTAINER1, KBASE1, KieSessionType.STATEFUL, KSESSION1), KieSessionMonitoringMXBean.class);
long tft = 0;
print(aggrMonitor);
checkAgendaTotals(aggrMonitor, 0, 0, 0);
checkTotalFactCount(aggrMonitor, 0);
tft = checkTotalFiringTimeGEQ(aggrMonitor, tft);
checkTotalSessions(aggrMonitor, 1);
assertNull(aggrMonitor.getStatsForProcess("com.sample.HelloWorld"));
assertNull(aggrMonitor.getStatsForProcess("com.sample.CiaoWorld"));
assertFalse(aggrMonitor.getStatsByProcess().containsKey("com.sample.HelloWorld"));
assertFalse(aggrMonitor.getStatsByProcess().containsKey("com.sample.CiaoWorld"));
ksession.insert("Ciao");
ksession.startProcess("com.sample.HelloWorld");
ksession.startProcess("com.sample.HelloWorld");
ksession.startProcess("com.sample.CiaoWorld");
ksession.fireAllRules();
print(aggrMonitor);
checkAgendaTotals(aggrMonitor, 1, 0, 1);
checkTotalFactCount(aggrMonitor, 1);
tft = checkTotalFiringTimeGEQ(aggrMonitor, tft + 1);
checkTotalSessions(aggrMonitor, 1);
checkStatsForProcess(aggrMonitor, "com.sample.HelloWorld", 2, 2, 6);
checkStatsForProcess(aggrMonitor, "com.sample.CiaoWorld", 1, 1, 3);
ksession.fireAllRules();
print(aggrMonitor);
checkAgendaTotals(aggrMonitor, 1, 0, 1);
checkTotalFactCount(aggrMonitor, 1);
tft = checkTotalFiringTimeGEQ(aggrMonitor, tft);
checkTotalSessions(aggrMonitor, 1);
checkStatsForProcess(aggrMonitor, "com.sample.HelloWorld", 2, 2, 6);
checkStatsForProcess(aggrMonitor, "com.sample.CiaoWorld", 1, 1, 3);
LOG.debug("---");
KieSession ksession2 = kc.newKieSession(KSESSION1, sessionConf);
ksession2.insert("Ciao");
ksession.startProcess("com.sample.HelloWorld");
ksession.startProcess("com.sample.HelloWorld");
ksession.startProcess("com.sample.CiaoWorld");
ksession2.fireAllRules();
print(aggrMonitor);
checkAgendaTotals(aggrMonitor, 2, 0, 2);
checkTotalFactCount(aggrMonitor, 2);
tft = checkTotalFiringTimeGEQ(aggrMonitor, tft + 1);
checkTotalSessions(aggrMonitor, 2);
checkStatsForProcess(aggrMonitor, "com.sample.HelloWorld", 4, 4, 12);
checkStatsForProcess(aggrMonitor, "com.sample.CiaoWorld", 2, 2, 6);
LOG.debug("--- NOW for the STATELESS ---");
tft = 0;
StatelessKieSession stateless = kc.newStatelessKieSession(KSESSION2, sessionConf);
StatelessKieSessionMonitoringMXBean aggrMonitor2 = JMX.newMXBeanProxy(mbserver, DroolsManagementAgent.createObjectNameBy(KCONTAINER1, KBASE1, KieSessionType.STATELESS, KSESSION2), StatelessKieSessionMonitoringMXBean.class);
print(aggrMonitor2);
checkAgendaTotals(aggrMonitor2, 0, 0, 0);
checkRuleRuntimeTotals(aggrMonitor2, 0, 0);
tft = checkTotalFiringTimeGEQ(aggrMonitor2, tft);
checkTotalSessions(aggrMonitor2, 0);
List<Command> cmds = new ArrayList<Command>();
cmds.add(CommandFactory.newInsert("Ciao"));
cmds.add(CommandFactory.newStartProcess("com.sample.HelloWorld"));
cmds.add(CommandFactory.newStartProcess("com.sample.HelloWorld"));
cmds.add(CommandFactory.newStartProcess("com.sample.CiaoWorld"));
BatchExecutionCommand batch = CommandFactory.newBatchExecution(cmds);
stateless.execute(batch);
print(aggrMonitor2);
checkAgendaTotals(aggrMonitor2, 1, 0, 1);
checkRuleRuntimeTotals(aggrMonitor2, 1, 0);
tft = checkTotalFiringTimeGEQ(aggrMonitor2, tft + 1);
checkTotalSessions(aggrMonitor2, 1);
checkStatsForProcess(aggrMonitor2, "com.sample.HelloWorld", 2, 2, 6);
checkStatsForProcess(aggrMonitor2, "com.sample.CiaoWorld", 1, 1, 3);
stateless.execute(batch);
print(aggrMonitor2);
checkAgendaTotals(aggrMonitor2, 2, 0, 2);
checkRuleRuntimeTotals(aggrMonitor2, 2, 0);
tft = checkTotalFiringTimeGEQ(aggrMonitor2, tft + 1);
checkTotalSessions(aggrMonitor2, 2);
checkStatsForProcess(aggrMonitor2, "com.sample.HelloWorld", 4, 4, 12);
checkStatsForProcess(aggrMonitor2, "com.sample.CiaoWorld", 2, 2, 6);
StatelessKieSession stateless2 = kc.newStatelessKieSession(KSESSION2, sessionConf);
LOG.debug(stateless + " " + stateless2);
checkTotalSessions(aggrMonitor2, 2);
stateless2.execute(batch);
print(aggrMonitor2);
checkAgendaTotals(aggrMonitor2, 3, 0, 3);
checkRuleRuntimeTotals(aggrMonitor2, 3, 0);
tft = checkTotalFiringTimeGEQ(aggrMonitor2, tft + 1);
checkTotalSessions(aggrMonitor2, 3);
checkStatsForProcess(aggrMonitor2, "com.sample.HelloWorld", 6, 6, 18);
checkStatsForProcess(aggrMonitor2, "com.sample.CiaoWorld", 3, 3, 9);
}
use of org.kie.api.runtime.StatelessKieSession in project snomed-drools by IHTSDO.
the class RuleExecutor method execute.
/**
* Validate a concept using drools rules available to this executor.
* A temporary identifier should be assigned to the concepts or any of it's descriptions and relationships
* if the component is new and does not yet have an SCTID. The identifier of the component is used to identify invalid content.
*
* Passing services in with every invocation of this method allows the implementation to capture content context. For example
* services relevant to the content branch being worked on.
* @param ruleSetNames The rule sets to use during validation.
* @param concepts The concepts to be validated.
* @param conceptService An implementation of the ConceptService class for use in validation rules.
* @param descriptionService An implementation of the DescriptionService class for use in validation rules.
* @param relationshipService An implementation of the RelationshipService class for use in validation rules.
* @param includePublishedComponents Include the published components of the given concept in results if found to be invalid.
* Published content will be used during validation regardless just not returned.
* @param includeInferredRelationships Include the inferred relationships of the given concept during validation and
* in results if found to be invalid.
* @return A list of content found to be invalid is returned.
*/
public List<InvalidContent> execute(Set<String> ruleSetNames, Collection<? extends Concept> concepts, ConceptService conceptService, DescriptionService descriptionService, RelationshipService relationshipService, boolean includePublishedComponents, boolean includeInferredRelationships) throws RuleExecutorException {
if (failedToInitialize)
throw new RuleExecutorException("Unable to complete request: rule engine failed to initialize.");
for (Concept concept : concepts) {
assertComponentIdsPresent(concept);
}
Date start = new Date();
Set<Component> components = new HashSet<>();
final List<InvalidContent> invalidContent = new ArrayList<>();
for (String ruleSetName : ruleSetNames) {
final KieContainer kieContainer = assertionGroupContainers.get(ruleSetName);
if (kieContainer == null) {
throw new RuleExecutorException("Rule set not found for name '" + ruleSetName + "'");
}
final StatelessKieSession session = kieContainer.newStatelessKieSession();
session.setGlobal("invalidContent", invalidContent);
session.setGlobal("conceptService", conceptService);
session.setGlobal("descriptionService", descriptionService);
session.setGlobal("relationshipService", relationshipService);
int total = concepts.size();
int complete = 0;
for (Concept concept : concepts) {
// Load components into working set
components.clear();
addConcept(components, concept, includeInferredRelationships);
// Execute rules on working set
session.execute(components);
complete++;
if (complete % 10000 == 0) {
logger.info("{} of {} concepts validated.", complete, total);
}
}
logger.info("Rule execution took {} seconds", (new Date().getTime() - start.getTime()) / 1000);
}
if (!includePublishedComponents) {
Set<InvalidContent> publishedInvalidContent = new HashSet<>();
for (InvalidContent invalidContentItem : invalidContent) {
logger.info("invalidContentItem : {}, {}, {}, {}, {}", invalidContentItem.getConceptId(), invalidContentItem.isIgnorePublishedCheck(), invalidContentItem.isPublished(), invalidContentItem.getSeverity(), invalidContentItem.getMessage());
if (!invalidContentItem.isIgnorePublishedCheck() && invalidContentItem.isPublished()) {
publishedInvalidContent.add(invalidContentItem);
}
}
invalidContent.removeAll(publishedInvalidContent);
}
return invalidContent;
}
use of org.kie.api.runtime.StatelessKieSession in project drools by kiegroup.
the class KieLoggersTest method testKieConsoleLoggerStateless.
@Test
public void testKieConsoleLoggerStateless() 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";
// get the resource
Resource dt = ResourceFactory.newByteArrayResource(drl.getBytes()).setTargetPath("org/drools/integrationtests/hello.drl");
// create the builder
StatelessKieSession ksession = getStatelessKieSession(dt);
KieRuntimeLogger logger = KieServices.Factory.get().getLoggers().newConsoleLogger(ksession);
AgendaEventListener ael = mock(AgendaEventListener.class);
ksession.addEventListener(ael);
ksession.execute(new Message("Hello World"));
verify(ael).afterMatchFired(any(AfterMatchFiredEvent.class));
logger.close();
}
use of org.kie.api.runtime.StatelessKieSession in project drools by kiegroup.
the class KieLoggersTest method testDeclarativeKieConsoleLoggerStateless.
@Test
public void testDeclarativeKieConsoleLoggerStateless() 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").setType(KieSessionModel.KieSessionType.STATELESS).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());
StatelessKieSession ksession = kieContainer.newStatelessKieSession("KSession1");
ksession.execute(new Message("Hello World"));
KieRuntimeLogger logger = ksession.getLogger();
assertNotNull(logger);
logger.close();
}
use of org.kie.api.runtime.StatelessKieSession in project drools by kiegroup.
the class MBeansMonitoringTest method testKieClasspathMBeans.
@Test
public void testKieClasspathMBeans() throws Exception {
MBeanServer mbserver = ManagementFactory.getPlatformMBeanServer();
KieServices ks = KieServices.Factory.get();
KieContainer kc = ks.getKieClasspathContainer("myID");
KieContainerMonitorMXBean c1Monitor = JMX.newMXBeanProxy(mbserver, DroolsManagementAgent.createObjectNameBy("myID"), KieContainerMonitorMXBean.class);
KieBase kb = kc.getKieBase("org.kie.monitoring.kbase1");
KieSession statefulKieSession = kc.newKieSession("org.kie.monitoring.kbase1.ksession1");
StatelessKieSession statelessKieSession = kc.newStatelessKieSession("org.kie.monitoring.kbase1.ksession2");
KieSessionMonitoringMXBean statefulKieSessionMonitor = JMX.newMXBeanProxy(mbserver, DroolsManagementAgent.createObjectNameBy("myID", "org.kie.monitoring.kbase1", KieSessionType.STATEFUL, "org.kie.monitoring.kbase1.ksession1"), KieSessionMonitoringMXBean.class);
StatelessKieSessionMonitoringMXBean statelessKieSessionMonitor = JMX.newMXBeanProxy(mbserver, DroolsManagementAgent.createObjectNameBy("myID", "org.kie.monitoring.kbase1", KieSessionType.STATEFUL, "org.kie.monitoring.kbase1.ksession1"), StatelessKieSessionMonitoringMXBean.class);
assertEquals("myID", c1Monitor.getContainerId());
assertTrue(c1Monitor.getConfiguredReleaseId().sameGAVof(KieContainerMonitorMXBean.CLASSPATH_KIECONTAINER_RELEASEID));
assertTrue(c1Monitor.getResolvedReleaseId().sameGAVof(KieContainerMonitorMXBean.CLASSPATH_KIECONTAINER_RELEASEID));
assertEquals("org.kie.monitoring.kbase1.ksession1", statefulKieSessionMonitor.getKieSessionName());
assertEquals("org.kie.monitoring.kbase1", statefulKieSessionMonitor.getKieBaseId());
assertEquals("org.kie.monitoring.kbase1.ksession1", statelessKieSessionMonitor.getKieSessionName());
assertEquals("org.kie.monitoring.kbase1", statelessKieSessionMonitor.getKieBaseId());
KieContainer kc2 = ks.newKieClasspathContainer("myID2");
KieContainerMonitorMXBean c2Monitor = JMX.newMXBeanProxy(mbserver, DroolsManagementAgent.createObjectNameBy("myID2"), KieContainerMonitorMXBean.class);
KieBase kb2 = kc2.getKieBase("org.kie.monitoring.kbase1");
KieSession statefulKieSession2 = kc2.newKieSession("org.kie.monitoring.kbase1.ksession1");
StatelessKieSession statelessKieSession2 = kc2.newStatelessKieSession("org.kie.monitoring.kbase1.ksession2");
KieSessionMonitoringMXBean statefulKieSessionMonitor2 = JMX.newMXBeanProxy(mbserver, DroolsManagementAgent.createObjectNameBy("myID2", "org.kie.monitoring.kbase1", KieSessionType.STATEFUL, "org.kie.monitoring.kbase1.ksession1"), KieSessionMonitoringMXBean.class);
StatelessKieSessionMonitoringMXBean statelessKieSessionMonitor2 = JMX.newMXBeanProxy(mbserver, DroolsManagementAgent.createObjectNameBy("myID2", "org.kie.monitoring.kbase1", KieSessionType.STATEFUL, "org.kie.monitoring.kbase1.ksession1"), StatelessKieSessionMonitoringMXBean.class);
assertEquals("myID2", c2Monitor.getContainerId());
assertTrue(c2Monitor.getConfiguredReleaseId().sameGAVof(KieContainerMonitorMXBean.CLASSPATH_KIECONTAINER_RELEASEID));
assertTrue(c2Monitor.getResolvedReleaseId().sameGAVof(KieContainerMonitorMXBean.CLASSPATH_KIECONTAINER_RELEASEID));
assertEquals("org.kie.monitoring.kbase1.ksession1", statefulKieSessionMonitor2.getKieSessionName());
assertEquals("org.kie.monitoring.kbase1", statefulKieSessionMonitor2.getKieBaseId());
assertEquals("org.kie.monitoring.kbase1.ksession1", statelessKieSessionMonitor2.getKieSessionName());
assertEquals("org.kie.monitoring.kbase1", statelessKieSessionMonitor2.getKieBaseId());
kc.dispose();
kc2.dispose();
}
Aggregations