2 Replies Latest reply on Aug 14, 2008 1:10 PM by steve.j.hall

    token on tasks instance created at runtime

    memius

      I try to create some task instances at runtime (so not declared in the process definition).
      Let's say that I created them in process P.

      When I try to add a task variable on such a task instance, I get the problem that I need a token...
      Now ... I am not really sure how I should create this token (since the task instance isn't really connected to anything in the process, it is just a task that the user should see and that is related to the process).
      So, what I did is create a with new Token(P).

      Now ... when I try to save this token I saw that save(Token token) is implemented as : save(token.getProcessInstance);
      So the token isn't saved and when I try to add a variable to the task instance I get the error that the token is not saved yet.

      So, how should I deal with this kind of stuff ?
      My question is not that specific, since I am not sure if what I am doing is correct, so, maybe I don't even need a token, or I could save it otherwise ... you tell me... at least ... I hope so ;)

      Sincerely,
      Dieter D'haeyere.

        • 1. Re: token on tasks instance created at runtime
          kukeltje

          l think there are examples of this in the source in SVN. With examples I mean the testcases. The testcase for workflowpattern for the runtime creation of tasks would be your best shot. Can you have a look there? If you get something working would make a nice wiki page

          • 2. Re: token on tasks instance created at runtime
            steve.j.hall

            Thanks for posting this question. It helped me look at some alternatives in a test case I had been working on.

            The example shows two alternatives:
            1. Loding and updating a process at runtime to add a task to a TaskNode, persisting the process change to the database. Later loading the process instance including the new task we added via our program.
            2. Adding a task to a TaskNode at runtime, where the task is not defined in the process definition. In the code below see: Task rtTask = new Task("RuntimeTask");

            In this example node 'r' is a TaskNode.

            I have checked the database and see that the task instances and their variables are persisted as one would hope.

            private void deployProcessDefinition(){
            // load the process definition from a file
            ProcessDefinition processDefinition = ProcessDefinition.parseXmlResource("pm/processdefinition.xml");
            assertNotNull("Definition should not be null", processDefinition);

            // create a delegate and an event
            org.jbpm.instantiation.Delegation del = new org.jbpm.instantiation.Delegation(new com.sample.action.StartPklblTaskActionHandler());
            Action actn = new Action(del);
            Event evt = new Event(Event.EVENTTYPE_TASK_START);
            evt.addAction(actn);
            del.setClassName("com.sample.action.StartPklblTaskActionHandler");
            del.setConfigType("bean");

            // create a task and assign the event
            Task pklblTask = new Task("PackingLabel");
            pklblTask.setSignalling(true);
            pklblTask.setBlocking(true);
            pklblTask.addEvent(evt);

            // can set a breakpoint here to look at the tasks the process knows about.
            Map taskMap = processDefinition.getTaskMgmtDefinition().getTasks();



            JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext();
            try{
            // Deploy and persist to db
            jbpmContext.deployProcessDefinition(processDefinition);
            jbpmContext.close();

            // find and load the process from the database then
            // add a task via the program execution and persist to db
            jbpmContext = jbpmConfiguration.createJbpmContext();
            GraphSession gs = jbpmContext.getGraphSession();
            ProcessDefinition pd = gs.findProcessDefinition("hello world", 1);
            // get the task node named r
            // add a task to it via the API
            TaskNode rNode = (TaskNode) pd.getNode("r");
            del.setProcessDefinition(pd);
            pklblTask.setProcessDefinition(pd);
            rNode.addTask(pklblTask);
            pklblTask.setTaskMgmtDefinition(pd.getTaskMgmtDefinition());
            jbpmContext.close();
            } catch(Exception e){
            e.printStackTrace();
            fail();
            }
            }

            private void emulateUserOne(){
            JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext();

            try{
            // load the process definition
            ProcessDefinition pd = findProcessDefinition(jbpmContext, "hello world", 1);

            // create a process instance
            ProcessInstance pi = new ProcessInstance(pd);
            TaskMgmtInstance tmi = pi.getTaskMgmtInstance();
            pi.setStart(new Date());

            TaskMgmtSession tms = jbpmContext.getTaskMgmtSession();
            ContextInstance ci = pi.getContextInstance();
            ci.setVariable("lotId", "100009");

            Token rootToken = pi.getRootToken();
            ExecutionContext exc = new ExecutionContext(rootToken);

            System.out.println("currentNode: " + rootToken.getNode().getName());
            TaskNode rNode = (TaskNode) pd.getNode("r");
            Set rNodeTasks = rNode.getTasks();

            Task rtTask = new Task("RuntimeTask");
            rtTask.setSignalling(true);
            rtTask.setBlocking(true);
            rNodeTasks.add(rtTask);

            rootToken.signal();
            System.out.println("currentNode: " + rootToken.getNode().getName());



            Node node = rootToken.getNode();
            if(node instanceof TaskNode){
            Collection taskInstances = tmi.getTaskInstances();
            Iterator iter = taskInstances.iterator();
            while(iter.hasNext()){
            TaskInstance ti = (TaskInstance) iter.next();
            ti.start();
            ti.setVariable(ti.getName() + "_LongVar", new Long("9999"));
            ti.setVariable(ti.getName() + "_StringVar", "TaskInstanceStringValue");
            Thread.currentThread().sleep(2000);
            ti.end();
            }
            }

            System.out.println("currentNode: " + rootToken.getNode().getName());
            node = rootToken.getNode();

            if(node instanceof TaskNode){
            TaskNode tn = (TaskNode) node;
            // fetch all task instances even those that are finished.
            Collection taskInstances = tmi.getTaskInstances();
            Iterator iter = taskInstances.iterator();
            while(iter.hasNext()){
            TaskInstance ti = (TaskInstance) iter.next();
            if(ti.getStart() == null){
            ti.start();
            }
            Thread.currentThread().sleep(2000);

            if(ti.getEnd() == null){
            ti.end();
            }
            }
            }

            // persist
            jbpmContext.save(pi);
            } catch (Exception e){
            e.printStackTrace();
            fail();
            } finally {
            try{
            jbpmContext.close();
            } catch (Exception e){
            e.printStackTrace();
            fail();
            }
            }
            }