<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Stories of an Activiti Core Developer</title>
	<atom:link href="http://stacktrace.be/blog/feed/" rel="self" type="application/rss+xml" />
	<link>http://stacktrace.be/blog</link>
	<description>Post about Activiti BPM and the technology-landscape in general by Frederik Heremans, Activiti Core-Developer and proud Alfresco employee.</description>
	<lastBuildDate>Tue, 12 Mar 2013 13:36:27 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Dynamic Process Creation and Deployment in 100 Lines of Code</title>
		<link>http://stacktrace.be/blog/2013/03/dynamic-process-creation-and-deployment-in-100-lines/</link>
		<comments>http://stacktrace.be/blog/2013/03/dynamic-process-creation-and-deployment-in-100-lines/#comments</comments>
		<pubDate>Mon, 11 Mar 2013 09:00:22 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[activiti]]></category>
		<category><![CDATA[bpm]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://stacktrace.be/blog/?p=26</guid>
		<description><![CDATA[We recently released the 5.12 version of Activiti and it&#8217;s packed with a lot of new features and improvements. As of the 5.11 version, it&#8217;s possible to build BPMN 2.0 processes using a POJO-model. In the latest release, we embrace that POJO-model even more and use it in the core of Activiti as a means of [...]]]></description>
				<content:encoded><![CDATA[<p>We recently <a title="Activiti 5.12 released" href="http://bpmn20inaction.blogspot.co.uk/2013/03/activiti-512-released.html" target="_blank">released the 5.12 version of Activiti</a> and it&#8217;s packed with a lot of new features and improvements. As of the 5.11 version, it&#8217;s possible to build BPMN 2.0 processes using a POJO-model. In the latest release, we embrace that POJO-model even more and use it in the core of Activiti as a means of retrieving and deploying process-definitions (on top of the existing deployment formats) using the API.</p>
<p>Combined with other features of Activiti this allows us to build a process, deploy it, start it, test it and retrieve the process definition diagram in under 100 lines of code. By leveraging the new activiti-bpmn-autolayout module, the process elements can be automatically layout, getting the graphical information (BPMN-DI) for free.</p>
<h2>The code</h2>
<p>I started off with an <a title="Activiti unit test template" href="https://github.com/Activiti/activiti-unit-test-template" target="_blank">Activiti unit-test template</a>, which uses a default Activiti-engine running on an in-memory H2 database. The code is written as a simple unit-test, using the built-in JUnit 4 support to have a fully initialized engine and API ready to use when the test starts to run. Full version of the code below <a title="Project on Github" href="https://github.com/frederikheremans/activiti-dynamic-process" target="_blank">can be found on Github</a>.</p><pre class="crayon-plain-tag">@Test
public void testDynamicDeploy() throws Exception {
  // 1. Build up the model from scratch
  BpmnModel model = new BpmnModel();
  Process process = new Process();
  model.addProcess(process);
  process.setId(&quot;my-process&quot;);

  process.addFlowElement(createStartEvent());
  process.addFlowElement(createUserTask(&quot;task1&quot;, &quot;First task&quot;, &quot;fred&quot;));
  process.addFlowElement(createUserTask(&quot;task2&quot;, &quot;Second task&quot;, &quot;john&quot;));
  process.addFlowElement(createEndEvent());

  process.addFlowElement(createSequenceFlow(&quot;start&quot;, &quot;task1&quot;));
  process.addFlowElement(createSequenceFlow(&quot;task1&quot;, &quot;task2&quot;));
  process.addFlowElement(createSequenceFlow(&quot;task2&quot;, &quot;end&quot;));

  // 2. Generate graphical information
  new BpmnAutoLayout(model).execute();

  // 3. Deploy the process to the engine
  Deployment deployment = activitiRule.getRepositoryService().createDeployment()
    .addBpmnModel(&quot;dynamic-model.bpmn&quot;, model).name(&quot;Dynamic process deployment&quot;)
    .deploy();

  // 4. Start a process instance
  ProcessInstance processInstance = activitiRule.getRuntimeService()
    .startProcessInstanceByKey(&quot;my-process&quot;);

  // 5. Check if task is available
  List tasks = activitiRule.getTaskService().createTaskQuery()
    .processInstanceId(processInstance.getId()).list();

  Assert.assertEquals(1, tasks.size());
  Assert.assertEquals(&quot;First task&quot;, tasks.get(0).getName());
  Assert.assertEquals(&quot;fred&quot;, tasks.get(0).getAssignee());

  // 6. Save process diagram to a file  
  InputStream processDiagram = activitiRule.getRepositoryService()
    .getProcessDiagram(processInstance.getProcessDefinitionId());
  FileUtils.copyInputStreamToFile(processDiagram, new File(&quot;target/diagram.png&quot;));

  // 7. Save resulting BPMN xml to a file
  InputStream processBpmn = activitiRule.getRepositoryService()
    .getResourceAsStream(deployment.getId(), &quot;dynamic-model.bpmn&quot;);
  FileUtils.copyInputStreamToFile(processBpmn, 
    new File(&quot;target/process.bpmn20.xml&quot;));
}

protected UserTask createUserTask(String id, String name, String assignee) {
  UserTask userTask = new UserTask();
  userTask.setName(name);
  userTask.setId(id);
  userTask.setAssignee(assignee);
  return userTask;
}

protected SequenceFlow createSequenceFlow(String from, String to) {
  SequenceFlow flow = new SequenceFlow();
  flow.setSourceRef(from);
  flow.setTargetRef(to);
  return flow;
}

protected StartEvent createStartEvent() {
  StartEvent startEvent = new StartEvent();
  startEvent.setId(&quot;start&quot;);
  return startEvent;
}

protected EndEvent createEndEvent() {
  EndEvent endEvent = new EndEvent();
  endEvent.setId(&quot;end&quot;);
  return endEvent;
}</pre><p></p>
<ol>
<li>Using the BPMN-model, we create a simple process containing a start-event, 2 usertaks, an end-event and the nessecairy flows connecting them.</li>
<li>We use the BpmnAutoLayout class, found in the activiti-bpmn-autolayout module, to make sure all processes in the BpmnModel have a graphical representation defined.</li>
<li>Using the new addBpmnModel(&#8230;) method on DeploymentBuilder, we make sure out created process gets deployed in the engine.</li>
<li>We start a new instance of our process by using the process-key we defined in our process.</li>
<li>Fetch all waiting tasks for the started process and check if the task&#8217;s name and assignee are correct.</li>
<li>To check what the process actually looks like, we save the diagram-image (created based on the BPMN-DI information):<a href="http://stacktrace.be/blog/wp-content/uploads/2013/03/diagram.png" rel="lightbox[26]"><img class="size-full wp-image-51" title="Diagram of the resulting process-definition" alt="Process-definition diagram" src="http://stacktrace.be/blog/wp-content/uploads/2013/03/diagram.png" width="420" height="70" /></a></li>
<li>Finally, save the BPMN 2.0 xml representation of this process. This allows us to, for example, further refine the process in other modeling tools like Activiti Designer.</li>
</ol>
<h2>Possibilities</h2>
<p>For demonstration purposes I created a relatively simple process, but you can imagine the potential if you consider that the POJO-model allows you to use all supported BPMN 2.0 constructs as well as all Activiti-specific extentions.</p>
<p>Using this approach you can create processes at runtime without the need for a design-tool or having to juggle around with XML. It can be used, for example, to create a process-model based on your own &#8220;intermediate model&#8221; or &#8220;building-blocks&#8221;, hiding complexity to end-users without sacrificing the richness of the BPMN language.</p>
]]></content:encoded>
			<wfw:commentRss>http://stacktrace.be/blog/2013/03/dynamic-process-creation-and-deployment-in-100-lines/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
