use of org.drools.builder.KnowledgeBuilder in project jBPM5-Developer-Guide by Salaboy.
the class ServiceTaskTest method initializeSession.
private void initializeSession() {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(new ClassPathResource("HospitalInvokeInsuranceScenarioV1-data.bpmn"), ResourceType.BPMN2);
if (kbuilder.hasErrors()) {
KnowledgeBuilderErrors errors = kbuilder.getErrors();
for (KnowledgeBuilderError error : errors) {
System.out.println(">>> Error:" + error.getMessage());
}
throw new IllegalStateException(">>> Knowledge couldn't be parsed! ");
}
KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
session = kbase.newStatefulKnowledgeSession();
KnowledgeRuntimeLoggerFactory.newConsoleLogger(session);
session.addEventListener(new DefaultAgendaEventListener() {
@Override
public void afterRuleFlowGroupActivated(org.drools.event.rule.RuleFlowGroupActivatedEvent event) {
session.fireAllRules();
}
});
}
use of org.drools.builder.KnowledgeBuilder in project jBPM5-Developer-Guide by Salaboy.
the class AsyncWorkItemDontWaitForCompletionTest method initializeSession.
private void initializeSession() {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
// We are using a proces containing a single Task. The task is configured
// to avoid waiting until the completion of the external system.
kbuilder.add(new ClassPathResource("async-work-item-nowait.bpmn"), ResourceType.BPMN2);
if (kbuilder.hasErrors()) {
KnowledgeBuilderErrors errors = kbuilder.getErrors();
for (KnowledgeBuilderError error : errors) {
System.out.println(">>> Error:" + error.getMessage());
}
throw new IllegalStateException(">>> Knowledge couldn't be parsed! ");
}
KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
session = kbase.newStatefulKnowledgeSession();
KnowledgeRuntimeLoggerFactory.newConsoleLogger(session);
session.addEventListener(new DefaultAgendaEventListener() {
@Override
public void afterRuleFlowGroupActivated(org.drools.event.rule.RuleFlowGroupActivatedEvent event) {
session.fireAllRules();
}
});
}
use of org.drools.builder.KnowledgeBuilder in project jBPM5-Developer-Guide by Salaboy.
the class ProcessAndHumanTasksTest method initializeSession.
// init the Knowledge Session
private StatefulKnowledgeSession initializeSession() {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(new ClassPathResource("com/salaboy/jbpm5/dev/guide/controller/simple-review-process.bpmn"), ResourceType.BPMN2);
if (kbuilder.hasErrors()) {
KnowledgeBuilderErrors errors = kbuilder.getErrors();
for (KnowledgeBuilderError error : errors) {
System.out.println(">>> Error:" + error.getMessage());
}
throw new IllegalStateException(">>> Knowledge couldn't be parsed! ");
}
return kbuilder.newKnowledgeBase().newStatefulKnowledgeSession();
}
use of org.drools.builder.KnowledgeBuilder in project jBPM5-Developer-Guide by Salaboy.
the class MyFirstjBPM5ProcessTest method test.
@Test
public void test() {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(new ClassPathResource("firstDummyProcess.bpmn"), ResourceType.BPMN2);
if (kbuilder.hasErrors()) {
for (KnowledgeBuilderError error : kbuilder.getErrors()) {
System.out.println(">>> Error:" + error.getMessage());
}
fail(">>> Knowledge couldn't be parsed! ");
}
KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
// Uncomment to see all the logs
// KnowledgeRuntimeLoggerFactory.newConsoleLogger(ksession);
// We have our Session, now let's play with our Dummy Process
ProcessInstance processInstance = ksession.createProcessInstance("DummyProcess", null);
assertEquals(processInstance.getState(), ProcessInstance.STATE_PENDING);
ksession.startProcessInstance(processInstance.getId());
assertEquals(processInstance.getState(), ProcessInstance.STATE_COMPLETED);
}
use of org.drools.builder.KnowledgeBuilder in project jBPM5-Developer-Guide by Salaboy.
the class AdvancedProcessAndRulesIntegrationTest method processVariablesAutoMappingPlusCastNoSafeCheckTest.
@Test
public void processVariablesAutoMappingPlusCastNoSafeCheckTest() throws InterruptedException {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(new ClassPathResource("evolution/mapping.drl"), ResourceType.DRL);
kbuilder.add(new ClassPathResource("evolution/scoring_processVariables_wider.drl"), ResourceType.DRL);
kbuilder.add(new ClassPathResource("evolution/process-variables-decision.bpmn"), ResourceType.BPMN2);
if (kbuilder.hasErrors()) {
for (KnowledgeBuilderError error : kbuilder.getErrors()) {
System.out.println(">>> Error:" + error.getMessage());
}
fail(">>> Knowledge couldn't be parsed! ");
}
KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
final StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
KnowledgeRuntimeLoggerFactory.newConsoleLogger(ksession);
Person person = new Person("Salaboy", 28);
RatesToday ratesToday = new RatesToday(1, 100);
Map<String, Object> params = new HashMap<String, Object>();
params.put("person", person);
params.put("ratesToday", ratesToday);
ProcessInstance processInstance = ksession.createProcessInstance("com.salaboy.process.SimpleDecision", params);
System.out.println("Variables: " + ((WorkflowProcessInstanceImpl) processInstance).getVariables());
assertEquals(processInstance.getState(), ProcessInstance.STATE_PENDING);
final FactHandle processHandle = ksession.insert(processInstance);
ksession.addEventListener(new DefaultAgendaEventListener() {
@Override
public void activationCreated(ActivationCreatedEvent event) {
System.out.println("Firing All the Rules! " + event);
((StatefulKnowledgeSession) event.getKnowledgeRuntime()).fireAllRules();
}
@Override
public void afterRuleFlowGroupActivated(RuleFlowGroupActivatedEvent event) {
System.out.println("Firing All the Rules! " + event);
((StatefulKnowledgeSession) event.getKnowledgeRuntime()).fireAllRules();
}
});
ksession.addEventListener(new DefaultProcessEventListener() {
@Override
public void beforeProcessStarted(ProcessStartedEvent event) {
((StatefulKnowledgeSession) event.getKnowledgeRuntime()).fireAllRules();
}
@Override
public void afterProcessStarted(ProcessStartedEvent event) {
((StatefulKnowledgeSession) event.getKnowledgeRuntime()).fireAllRules();
}
@Override
public void afterProcessCompleted(ProcessCompletedEvent event) {
((StatefulKnowledgeSession) event.getKnowledgeRuntime()).retract(processHandle);
}
});
ksession.startProcessInstance(processInstance.getId());
// // If you want to query the process variables while the process Instance is running you can do:
// // But remember that the activities inside the process are all sync.
// QueryResults queryResults = ksession.getQueryResults("allProcessVariables", new Object[]{});
// Iterator<QueryResultsRow> iterator = queryResults.iterator();
//
// QueryResultsRow ratesRow = iterator.next();
// assertEquals(ratesToday, ((ProcessVariable) ratesRow.get("$pv")).getValue());
//
// QueryResultsRow personRow = iterator.next();
// assertEquals(person, ((ProcessVariable) personRow.get("$pv")).getValue());
assertEquals(processInstance.getState(), ProcessInstance.STATE_COMPLETED);
QueryResults queryResults = ksession.getQueryResults("allProcessVariables", new Object[] {});
// The Process Variables are automatically retracted when the Process Instance is Completed
assertEquals(0, queryResults.size());
}
Aggregations