KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
ksession = kbase.newStatefulKnowledgeSession();
MyHumanChangingValuesSimulatorWorkItemHandler humanActivitiesSimHandler = new MyHumanChangingValuesSimulatorWorkItemHandler();
ksession.getWorkItemManager().registerWorkItemHandler("Human Task", humanActivitiesSimHandler);
MyTrackingSystemMock trackingSystem = new MyTrackingSystemMock();
StartVehicleTrackingMockSystem trackingSystemHandler = new StartVehicleTrackingMockSystem(trackingSystem);
ksession.getWorkItemManager().registerWorkItemHandler("VehicleTrackingSystem", trackingSystemHandler);
KnowledgeRuntimeLoggerFactory.newConsoleLogger(ksession);
Emergency emergency = new Emergency("555-1234");
emergency.setType("Heart Attack");
Map<String, Object> parameters = new HashMap<String, Object>();
parameters.put("emergency", emergency);
WorkflowProcessInstance process = (WorkflowProcessInstance) ksession
.startProcess("com.wordpress.salaboy.bpmn2.SimpleEmergencyService", parameters);
// My Emergency and My Process are both inserted as Facts / Truths in my Knowledge Session
// Now Emergency and the Process Instance can be used by the inference engine
ksession.insert(emergency);
ksession.insert(process);
// Is the Process still Active?
Assert.assertEquals(ProcessInstance.STATE_ACTIVE, process.getState());
// Is there a running node instance?
Assert.assertEquals(1, process.getNodeInstances().size());
// Is the process stopped in the "Ask for Emergency Information" activity?
Assert.assertEquals("Ask for Emergency Information", process.getNodeInstances().iterator().next().getNodeName());
// Lets check the value of the emergency.getRevision(), it should be 1
Assert.assertEquals(1, ((Emergency) process.getVariable("emergency")).getRevision());
System.out.println("Completing the first Activity");
//Complete the first human activity
humanActivitiesSimHandler.completeWorkItem();
// Is the Process still Active?
Assert.assertEquals(ProcessInstance.STATE_ACTIVE, process.getState());
//I need to call the FireAllRules method because I have a RuleTask inside the business process
int fired = ksession.fireAllRules();
// Now that I have rules being evaluated, at least one should fire
Assert.assertEquals(1, fired);
// Lets check the value of the vehicle variable it should be an Ambulance => Heart Attack
Vehicle selectedVehicle = ((Vehicle) process.getVariable("vehicle"));
Assert.assertTrue(selectedVehicle instanceof Ambulance);
// Is the Process still Active?
Assert.assertEquals(ProcessInstance.STATE_ACTIVE, process.getState());
// Is there a running node instance?
Assert.assertEquals(1, process.getNodeInstances().size());
// Is the process stopped in the "Dispatch Vehicle" activity?
Assert.assertEquals("Dispatch Vehicle", process.getNodeInstances().iterator().next().getNodeName());
// Lets check the value of the emergency.getRevision(), it should be 1
Assert.assertEquals(2, ((Emergency) process.getVariable("emergency")).getRevision());
System.out.println("Completing the second Activity");
//Complete the second human activity
humanActivitiesSimHandler.completeWorkItem();
//Start Tracking and Reporting are automatic -> Check the report in the console
//We can also check against the tracking system itself
Assert.assertEquals("Vehicle "+selectedVehicle.getId()+" Located at 5th and A Avenue"
, trackingSystem.queryVehicleStatus(selectedVehicle.getId()));