public void testNewSessionFailBefore() throws Exception { KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add(ResourceFactory.newClassPathResource("sampleFailBefore.bpmn"), ResourceType.BPMN2); KnowledgeBase kbase = kbuilder.newKnowledgeBase(); SessionManagerFactory factory = new NewSessionSessionManagerFactory(kbase); SessionManager sessionManager = factory.getSessionManager(); try { ProcessInstance processInstance = sessionManager.getKnowledgeSession().startProcess("com.sample.bpmn.hello", null); fail("Started process instance " + processInstance.getId()); } catch (RuntimeException e) { // do nothing } // TODO: whenever transaction fails, do we need to dispose? can we? // sessionManager.dispose(); List<Status> statusses = new ArrayList<Status>(); statusses.add(Status.Reserved); sessionManager = factory.getSessionManager(); List<TaskSummary> tasks = sessionManager.getTaskService().getTasksAssignedAsPotentialOwner("mary", "en-UK"); assertEquals(0, tasks.size()); sessionManager.dispose(); factory.dispose(); }
public void testNewSessionDispose() throws Exception { KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add(ResourceFactory.newClassPathResource("sample.bpmn"), ResourceType.BPMN2); KnowledgeBase kbase = kbuilder.newKnowledgeBase(); SessionManagerFactory factory = new NewSessionSessionManagerFactory(kbase); final SessionManager sessionManager = factory.getSessionManager(); UserTransaction ut = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction"); ut.begin(); sessionManager.getKnowledgeSession().startProcess("com.sample.bpmn.hello", null); TransactionManagerServices.getTransactionManager() .getTransaction() .registerSynchronization( new Synchronization() { public void beforeCompletion() {} public void afterCompletion(int status) { try { sessionManager.dispose(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); ut.commit(); factory.dispose(); }
public void testSingletonSessionMemory() throws Exception { for (int i = 0; i < 1000; i++) { KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add(ResourceFactory.newClassPathResource("sample.bpmn"), ResourceType.BPMN2); KnowledgeBase kbase = kbuilder.newKnowledgeBase(); SessionManagerFactory factory = new SingletonSessionManagerFactory(kbase); SessionManager sessionManager = factory.getSessionManager(); sessionManager.dispose(); factory.dispose(); System.gc(); Thread.sleep(100); System.gc(); System.out.println(Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()); } }
public void testNewSessionFailAfter() throws Exception { KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add(ResourceFactory.newClassPathResource("sampleFailAfter.bpmn"), ResourceType.BPMN2); KnowledgeBase kbase = kbuilder.newKnowledgeBase(); SessionManagerFactory factory = new NewSessionSessionManagerFactory(kbase); SessionManager sessionManager = factory.getSessionManager(); ProcessInstance processInstance = sessionManager.getKnowledgeSession().startProcess("com.sample.bpmn.hello", null); long workItemId = ((HumanTaskNodeInstance) ((WorkflowProcessInstance) processInstance).getNodeInstances().iterator().next()) .getWorkItemId(); long taskId = sessionManager.getTaskService().getTaskByWorkItemId(workItemId).getId(); sessionManager.getTaskService().claim(taskId, "mary"); List<Status> statusses = new ArrayList<Status>(); statusses.add(Status.Reserved); List<TaskSummary> tasks = sessionManager.getTaskService().getTasksOwned("mary", statusses, "en-UK"); assertEquals(1, tasks.size()); taskId = tasks.get(0).getId(); UserTransaction ut = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction"); try { ut.begin(); sessionManager.getTaskService().start(taskId, "mary"); sessionManager.getTaskService().complete(taskId, "mary", null); fail("Task completed"); } catch (RuntimeException e) { // do nothing } ut.rollback(); // TODO: whenever transaction fails, do we need to dispose? can we? // sessionManager.dispose(); sessionManager = factory.getSessionManager(); tasks = sessionManager.getTaskService().getTasksOwned("mary", statusses, "en-UK"); assertEquals(1, tasks.size()); sessionManager.dispose(); factory.dispose(); }
public void testNewSession() throws Exception { KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add(ResourceFactory.newClassPathResource("sample.bpmn"), ResourceType.BPMN2); KnowledgeBase kbase = kbuilder.newKnowledgeBase(); SessionManagerFactory factory = new NewSessionSessionManagerFactory(kbase); completedStart = 0; for (int i = 0; i < nbThreadsProcess; i++) { new StartProcessRunnable(factory, i).run(); } completedTask = 0; for (int i = 0; i < nbThreadsTask; i++) { new Thread(new CompleteTaskRunnable(factory, i)).start(); } while (completedStart < nbThreadsProcess || completedTask < nbThreadsTask) { Thread.sleep(100); } factory.dispose(); System.out.println("Done"); }
public static void main(final String[] args) { final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add( ResourceFactory.newClassPathResource( "WorkItemConsequence1.drl", WorkItemConsequenceExample1.class), ResourceType.DRL); final KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); kbase.addKnowledgePackages(kbuilder.getKnowledgePackages()); if (kbuilder.hasErrors()) { throw new RuntimeException("Compilation error.\n" + kbuilder.getErrors().toString()); } final StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession(); ksession .getWorkItemManager() .registerWorkItemHandler("EmailWorkItemHandler", new EmailWorkItemHandler()); // KnowledgeRuntimeLogger logger = KnowledgeRuntimeLoggerFactory.newFileLogger(ksession, // "log/WorkItemConsequence.log"); // Brains does not cause concern PersonLocation brains = new PersonLocation("Brains", 5); ksession.insert(brains); ksession.fireAllRules(); // Gargamel is too far away to care PersonLocation gargamel = new PersonLocation("Gargamel", 10); FactHandle gargamelFactHandle = ksession.insert(gargamel); ksession.fireAllRules(); // Uh oh, Gargamel moves closer gargamel.setDistance(5); ksession.update(gargamelFactHandle, gargamel); ksession.fireAllRules(); // logger.close(); ksession.dispose(); // Stateful rule session must always be disposed when finished }
@Test public void testSubruleSubsumption1() throws Exception { VerifierBuilder vBuilder = VerifierBuilderFactory.newVerifierBuilder(); Verifier verifier = vBuilder.newVerifier(); verifier.addResourcesToVerify( ResourceFactory.newClassPathResource("SubsumptantSubRules1.drl", getClass()), ResourceType.DRL); // for ( VerifierError error : verifier.getMissingClasses() ) { // System.out.println( error.getMessage() ); // } assertFalse(verifier.hasErrors()); boolean noProblems = verifier.fireAnalysis(); assertTrue(noProblems); Collection<Object> subsumptionList = ((VerifierImpl) verifier) .getKnowledgeSession() .getObjects(new ClassObjectFilter(Subsumption.class)); int count = 0; for (Object object : subsumptionList) { // System.out.println( " * " + ((Subsumption) object) ); if (((VerifierComponent) ((Subsumption) object).getLeft()) .getVerifierComponentType() .equals(VerifierComponentType.SUB_RULE)) { // System.out.println( " ** " + ((SubRule) ((Subsumption) // object).getLeft()).getItems() + " - " + ((SubRule) ((Subsumption) // object).getRight()).getItems() ); count++; } } assertEquals(2, count); verifier.dispose(); }
@Test public void testGlobal() throws Exception { Options xjcOpts = new Options(); xjcOpts.setSchemaLanguage(Language.XMLSCHEMA); KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); JaxbConfiguration jaxbConf = KnowledgeBuilderFactory.newJaxbConfiguration(xjcOpts, "xsd"); kbuilder.add( ResourceFactory.newClassPathResource("order.xsd", getClass()), ResourceType.XSD, jaxbConf); assertFalse(kbuilder.hasErrors()); kbuilder.add( ResourceFactory.newClassPathResource("test_Jaxb.drl", getClass()), ResourceType.DRL); KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); kbase.addKnowledgePackages(kbuilder.getKnowledgePackages()); assertFalse(kbuilder.hasErrors()); assertFalse(kbuilder.hasErrors()); StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession(); KnowledgeRuntimeCommand setGlobalStage = PipelineFactory.newStatefulKnowledgeSessionSetGlobal("order"); JAXBContext jaxbCtx = KnowledgeBuilderHelper.newJAXBContext( jaxbConf.getClasses().toArray(new String[jaxbConf.getClasses().size()]), kbase); Unmarshaller unmarshaller = jaxbCtx.createUnmarshaller(); Transformer transformer = PipelineFactory.newJaxbFromXmlTransformer(unmarshaller); transformer.setReceiver(setGlobalStage); Pipeline pipeline = PipelineFactory.newStatefulKnowledgeSessionPipeline(ksession); pipeline.setReceiver(transformer); String xml = StringUtils.readFileAsString( new InputStreamReader(getClass().getResourceAsStream("order.xml"))); ResultHandlerImpl resultHandler = new ResultHandlerImpl(); pipeline.insert(xml, resultHandler); // now round trip that global Action executeResult = PipelineFactory.newExecuteResultHandler(); Action assignAsResult = PipelineFactory.newAssignObjectAsResult(); assignAsResult.setReceiver(executeResult); // transformer = PipelineFactory.newXStreamToXmlTransformer( xstream ); Marshaller marshaller = jaxbCtx.createMarshaller(); transformer = PipelineFactory.newJaxbToXmlTransformer(marshaller); transformer.setReceiver(assignAsResult); KnowledgeRuntimeCommand getGlobalStage = PipelineFactory.newStatefulKnowledgeSessionGetGlobal(); getGlobalStage.setReceiver(transformer); pipeline = PipelineFactory.newStatefulKnowledgeSessionPipeline(ksession); pipeline.setReceiver(getGlobalStage); resultHandler = new ResultHandlerImpl(); pipeline.insert("order", resultHandler); assertEqualsIgnoreWhitespace(xml, (String) resultHandler.getObject()); }