001: /**
002: * Bonita
003: * Copyright (C) 1999 Bull S.A.
004: * Bull 68 route de versailles 78434 Louveciennes Cedex France
005: * Further information: bonita@objectweb.org
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
020: * USA
021: --------------------------------------------------------------------------
022: * BONITA Workflow Patterns sample
023: * Pattern 14 : Multiple Instances With a Priori Runtime Knowledge
024: * Author : Jordi Anguela
025: * Date : 2006/03/01
026: --------------------------------------------------------------------------
027: * Example
028: * In this example we suppose that only in runtime we know how many
029: * activities have to done: in our case to check the avaliability of N books
030: * A sub process implementing this logic is designed in the model and the
031: * N-1 sub process to check the other books will be created from a hook
032: * during the runtime when the books order is done.
033: */package hero.client.samples.patterns.src;
034:
035: import hero.client.test.SimpleCallbackHandler;
036: import hero.interfaces.Constants;
037: import hero.interfaces.ProjectSession;
038: import hero.interfaces.ProjectSessionHome;
039: import hero.interfaces.ProjectSessionUtil;
040:
041: import javax.security.auth.login.LoginContext;
042:
043: public class Pattern14MultipleInstancesWithRuntimeKnowledge {
044:
045: public static void main(String[] args) {
046: try {
047: // User Admin login
048: char[] password = { 't', 'o', 't', 'o' };
049: SimpleCallbackHandler handler = new SimpleCallbackHandler(
050: "admin", password);
051: LoginContext lc = new LoginContext("TestClient", handler);
052: lc.login();
053:
054: // Project Session Bean creation
055: ProjectSessionHome prjHome = (ProjectSessionHome) ProjectSessionUtil
056: .getHome();
057: ProjectSession prjSession = prjHome.create();
058:
059: // Model creation
060: System.out
061: .print("\n Pattern 14 - Multiple Instances With a Priori Runtime Knowledge");
062: prjSession
063: .initModel("Pattern 14 - Multiple Instances With a Priori Runtime Knowledge");
064:
065: // Add activities and set them to Non anticipable
066: prjSession.addNode("order_books",
067: hero.interfaces.Constants.Nd.AND_JOIN_NODE);
068: prjSession.addNode("shipping_process",
069: hero.interfaces.Constants.Nd.AND_JOIN_NODE);
070: prjSession.addNode("send_bill",
071: hero.interfaces.Constants.Nd.AND_JOIN_NODE);
072: prjSession.setNodeTraditional("order_books");
073: prjSession.setNodeTraditional("shipping_process");
074: prjSession.setNodeTraditional("send_bill");
075: prjSession.addEdge("shipping_process", "send_bill");
076:
077: // Add Project Properties : we want to have 3 instances of the subprocess in total.
078: // One created in the model and the other two created during runtime.
079: prjSession.setProperty("number_of_sub_processes", "2");
080:
081: // SubProcess definition
082: ProjectSession prjSession2 = prjHome.create();
083: prjSession2
084: .initModel("Check book avaliability (SubProcess)");
085: prjSession2.addNode("check_avaliability",
086: Constants.Nd.AND_JOIN_NODE);
087: prjSession2.setNodeTraditional("check_avaliability");
088: String update_stockHookScript = "import hero.interfaces.*;\n"
089: + "beforeStart(Object engine, Object currentNode) {\n\n\n"
090: + " hero.interfaces.ProjectSessionHome pHome = (hero.interfaces.ProjectSessionHome) hero.interfaces.ProjectSessionUtil.getHome(); \n"
091: + " hero.interfaces.ProjectSession prjSession = pHome.create(); \n"
092: + " prjSession.initModel(currentNode.getBnProject().getName()); \n"
093: + " BnProjectPropertyValue bookNameProp = prjSession.getProperty(\"book_name\"); \n"
094: + " System.out.println(\" ---> check_avaliability for : \" + bookNameProp.getTheValue()); \n"
095: + "}";
096: prjSession2.addNodeInterHook("check_avaliability",
097: "check_avaliability Hook",
098: hero.interfaces.Constants.Nd.BEFORESTART,
099: hero.interfaces.Constants.Hook.BSINTERACTIVE,
100: update_stockHookScript);
101: prjSession2
102: .addRole("Executor",
103: "Rol that enables to execute the activities inside the SubProcess");
104: prjSession2.setUserRole("admin", "Executor");
105: prjSession2.setNodeRole("check_avaliability", "Executor");
106: prjSession2.remove();
107:
108: // SubProcess instantiation
109: prjSession.addNodeSubProcess("check_book0",
110: "Check book avaliability (SubProcess)");
111: prjSession.setNodeProperty("check_book0", "book_name",
112: "check_book0", true);
113: prjSession.addEdge("order_books", "check_book0");
114: prjSession.addEdge("check_book0", "shipping_process");
115:
116: // Add Hook to 'order_books' activity to create a SubProces for each book
117: String createSubProcessesScript = "import hero.interfaces.*;\n"
118: + "beforeStart(Object engine, Object currentNode) {\n\n\n"
119: + " hero.interfaces.ProjectSessionHome pHome = (hero.interfaces.ProjectSessionHome) hero.interfaces.ProjectSessionUtil.getHome(); \n"
120: + " hero.interfaces.ProjectSession prjSession = pHome.create(); \n"
121: + " prjSession.initModel(currentNode.getBnProject().getName()); \n"
122: + " BnProjectPropertyValue numberOfSubProcessesProp = prjSession.getProperty(\"number_of_sub_processes\"); \n"
123: + " int numberOfSubProcesses = Integer.parseInt(numberOfSubProcessesProp.getTheValue()); \n"
124: + " for (int i = 1; i <= numberOfSubProcesses; i++) { \n"
125: + " String nodeName = \"check_book\" + Integer.toString(i); \n"
126: + " prjSession.addNodeSubProcess(nodeName, \"Check book avaliability (SubProcess)\"); \n"
127: + " prjSession.setNodeProperty(nodeName, \"book_name\", nodeName, true); \n"
128: + " prjSession.addEdge(nodeName, \"shipping_process\"); \n"
129: + " prjSession.setNodeRole(nodeName, \"Executor\"); \n"
130: + " } \n" + "}";
131: prjSession.addNodeInterHook("check_book0",
132: "check_book0 Hook",
133: hero.interfaces.Constants.Nd.BEFORESTART,
134: hero.interfaces.Constants.Hook.BSINTERACTIVE,
135: createSubProcessesScript);
136:
137: // Add and set a role for the user admin to execute the activities
138: prjSession.addRole("Executor",
139: "Rol that enables to execute the activities");
140: prjSession.setUserRole("admin", "Executor");
141: prjSession.setNodeRole("order_books", "Executor");
142: prjSession.setNodeRole("check_book0", "Executor");
143: prjSession.setNodeRole("shipping_process", "Executor");
144: prjSession.setNodeRole("send_bill", "Executor");
145:
146: // Check model definition
147: prjSession.checkModelDefinition();
148:
149: System.out.println(" [ OK ]");
150: } catch (Exception e) {
151: System.out.println("\n\n [ ERROR ] : " + e);
152: e.printStackTrace();
153: } // Maybe something is wrong
154: }
155: }
|