cancel
Showing results for 
Search instead for 
Did you mean: 

Programmatic process definition and deployment (without xml)

hieu_ho
Champ in-the-making
Champ in-the-making
Hi everyone

I'm new to Activiti and wanted to see if it will work for a project we're migrating.

I couldn't find a good example of creating a processDefinition and deploying it programatically.  I took point from ProcessDefinitionsTest, and a few others, but they seem to be deploying a process by reading from an existing xml resource or from a process string. (ie. DeploymentBuilder.addClasspathResource and addString)

I have a requirement where I am reading from external sources (ie. lists from db and flat files).  I want to avoid having to preprocess and convert them all to bpmn xml files.  I have List objects created from them, and would like to see if I can create a process definition from it, using something similar to the following


ProcessDefinitionBuilder processDefinitionBuilder = new ProcessDefinitionBuilder(someName);

processDefinitionBuilder = processDefinitionBuilder.createActivity(top.getName())
                                                               .initial()
                                                               .behavior(new BaseServiceTaskHandler("start node"))
                                                               .transition(startName)
                                                               .endActivity();

for (int i = 0; i < myChecklist.size(); i++) {
                    if(i<myChecklist.size() - 1) {
                        processDefinitionBuilder = processDefinitionBuilder.createActivity(myChecklist.get(i).getName())
                                                             .behavior(new ReminderActivityHandler())
                                                             .transition(myChecklist.get(i + 1).getName())
                                                             .endActivity();
                    } else {
                        processDefinitionBuilder = processDefinitionBuilder.createActivity(top.getRoutine().get(i).getName())
                                                             .endActivity();
                    }
                }

PvmProcessDefinition processDefinition = processDefinitionBuilder.buildProcessDefinition();


How do I deploy this now?  Would this be the best way to do this?  Is there an example you can point me to?

Thanks
12 REPLIES 12

hieu_ho
Champ in-the-making
Champ in-the-making
So, not sure if this is correct usage, but as it turns out, if I do the folllowing, I'm able to start my process for specific objects.


PvmProcessInstance processInstance = processDefinition.createProcessInstance();
processInstance.setVariable("userId", "myTest");
processInstance.start();


To test it, I went ahead and looked up the activities that I ended up with.  I got the expected activity.


List activeIds = processInstance.findActiveActivityIds();
Assert.assertNotNull(activeIds);

PvmActivity activity = processInstance.getActivity();

ronald_van_kuij
Champ on-the-rise
Champ on-the-rise
If it works, it is technically correct usage, but the 'api's us use are not public, so they might change without any notice and things stop working. In that regard it is bad practice.

hieu_ho
Champ in-the-making
Champ in-the-making
Thanks Ronald

can you clarify what you mean by not public?  I took these examples from PvmTest and ProcessDefinitionTest in the examples project.  Do you mean these methods are not part of services api?  Are there "public" ones I should be using?

Thanks

Hieu

ronald_van_kuij
Champ on-the-rise
Champ on-the-rise
They are in the impl package…  And afaik, there are no public classes/interfaces that you can use to achieve what you want. So either accept that things can change between releases or look at other ways to achieve what you want.

artur_karczmarc
Champ in-the-making
Champ in-the-making
I try to create a process definition without using the bpmn20.xml resource file, since I need to generate process definitions in my application dynamically. After creating process definition with ProcessDefinitionBuilder object and starting a process instance, I try to query for a task from the created process instance, but it fails. Could you point me out what I'm doing wrong, or show me any example of such dynamic process definition generation and execution as mentioned above?

Here is the source code I'm writing, the assertion at the bottom of the code fails, since no tasks are being found:


@Test
public void createProcessDefinition() {
ProcessDefinitionBuilder processDefinitionBuilder = new ProcessDefinitionBuilder();

//a map with tasks descriptions
List<Map<String, String>> activities = new ArrayList<Map<String, String>>();
Map<String, String> activity;

activity = new HashMap<String, String>();
activity.put("id", "UT1");
activity.put("name", "User task 1");
activities.add(activity);

activity = new HashMap<String, String>();
activity.put("id", "UT2");
activity.put("name", "User task 2");
activities.add(activity);

activity = new HashMap<String, String>();
activity.put("id", "UT3");
activity.put("name", "User task 3");
activities.add(activity);


// creating process definition
int len = activities.size();
for (int i = 0; i < len; i++) {
  processDefinitionBuilder.createActivity(activities.get(i).get("id"));
  if (i == 0) {
   processDefinitionBuilder.initial();
  }
 
  processDefinitionBuilder.behavior(new ActivitiWaitState());
 
  if (i + 1 < len) {
   processDefinitionBuilder.transition(activities.get(i+1).get("id"));
  }
}
PvmProcessDefinition processDefinition = processDefinitionBuilder.buildProcessDefinition();

PvmProcessInstance processInstance = processDefinition.createProcessInstance();
processInstance.start();

TaskQuery taskQuery = activitiTaskService.createTaskQuery();
List<Task> tasks = taskQuery.list();
Assert.assertTrue(tasks.size() > 0);
}

ronald_van_kuij
Champ on-the-rise
Champ on-the-rise
Processes need to be 'deployed' to be really useful. Why not create xml via java and deploy that?

artur_karczmarc
Champ in-the-making
Champ in-the-making
Thank you for your answer. I just wondered if it is posible to create, deploy and run a process without creating xml file, since I have a group of objects in my app, which then I will have to transform to XML only to transform it back to some objects.

I tried creating the xml file in java and then deploying it from String in memory, everything works fine Smiley Wink

ronald_van_kuij
Champ on-the-rise
Champ on-the-rise
I doubt you have all activiti objects, so what is the difference?

artur_karczmarc
Champ in-the-making
Champ in-the-making
Okay, thanks, I'll do it the xml-way Smiley Wink