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 15 : Multiple Instances Without a Priori Runtime Knowledge
024: * Author : Jordi Anguela
025: * Date : 2006/03/01
026: --------------------------------------------------------------------------
027: * Example
028: * This example is based on the previous pattern but know we have an extra
029: * activity called 'add_books' that will add two new sub
030: * processes.
031: */package hero.client.samples.patterns.src;
032:
033: import hero.client.test.SimpleCallbackHandler;
034: import hero.interfaces.Constants;
035: import hero.interfaces.ProjectSession;
036: import hero.interfaces.ProjectSessionHome;
037: import hero.interfaces.ProjectSessionUtil;
038:
039: import javax.security.auth.login.LoginContext;
040:
041: public class Pattern15MultipleInstancesWithoutRuntimeKnowledge {
042:
043: public static void main(String[] args) {
044: try {
045: // User Admin login
046: char[] password = { 't', 'o', 't', 'o' };
047: SimpleCallbackHandler handler = new SimpleCallbackHandler(
048: "admin", password);
049: LoginContext lc = new LoginContext("TestClient", handler);
050: lc.login();
051:
052: // Project Session Bean creation
053: ProjectSessionHome prjHome = (ProjectSessionHome) ProjectSessionUtil
054: .getHome();
055: ProjectSession prjSession = prjHome.create();
056:
057: // Model creation
058: System.out
059: .print("\n Pattern 15 - Multiple Instances Without a Priori Runtime Knowledge");
060: prjSession
061: .initModel("Pattern 15 - Multiple Instances Without a Priori Runtime Knowledge");
062:
063: // Add activities and set them to Non anticipable
064: prjSession.addNode("order_books",
065: hero.interfaces.Constants.Nd.AND_JOIN_NODE);
066: prjSession.addNode("add_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) 2");
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) 2");
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) 2\"); \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 Hook to 'add_books' activity to create two more sub proceses
138: String createSubProcesses2Script = "import hero.interfaces.*;\n"
139: + "afterStart(Object engine, Object currentNode) {\n\n\n"
140: + " hero.interfaces.ProjectSessionHome pHome = (hero.interfaces.ProjectSessionHome) hero.interfaces.ProjectSessionUtil.getHome(); \n"
141: + " hero.interfaces.ProjectSession prjSession = pHome.create(); \n"
142: + " prjSession.initModel(currentNode.getBnProject().getName()); \n"
143: + " for (int i = 3; i <= 4; i++) { \n"
144: + " String nodeName = \"check_book\" + Integer.toString(i); \n"
145: + " prjSession.addNodeSubProcess(nodeName, \"Check book avaliability (SubProcess) 2\"); \n"
146: + " prjSession.setNodeProperty(nodeName, \"book_name\", nodeName, true); \n"
147: + " prjSession.addEdge(nodeName, \"shipping_process\"); \n"
148: + " prjSession.setNodeRole(nodeName, \"Executor\"); \n"
149: + " } \n" + "}";
150: prjSession.addNodeInterHook("add_books", "add_books Hook",
151: hero.interfaces.Constants.Nd.AFTERSTART,
152: hero.interfaces.Constants.Hook.BSINTERACTIVE,
153: createSubProcesses2Script);
154:
155: // Add Hook to 'shipping_process' activity to cancel the generation of more sub proceses when it starts
156: String cancelScript = "import hero.interfaces.*; \n"
157: + "beforeStart(Object engine, Object currentNode) { \n"
158: + " UserSessionHome userHome = UserSessionUtil.getHome(); \n"
159: + " UserSession userSession = userHome.create(); \n"
160: + " userSession.cancelActivity(currentNode.getBnProject().getName(), \"add_books\"); \n "
161: + "}";
162: prjSession.addNodeInterHook("shipping_process",
163: "shipping_process Hook",
164: hero.interfaces.Constants.Nd.BEFORESTART,
165: hero.interfaces.Constants.Hook.BSINTERACTIVE,
166: cancelScript);
167:
168: // Add and set a role for the user admin to execute the activities
169: prjSession.addRole("Executor",
170: "Rol that enables to execute the activities");
171: prjSession.setUserRole("admin", "Executor");
172: prjSession.setNodeRole("order_books", "Executor");
173: prjSession.setNodeRole("add_books", "Executor");
174: prjSession.setNodeRole("check_book0", "Executor");
175: prjSession.setNodeRole("shipping_process", "Executor");
176: prjSession.setNodeRole("send_bill", "Executor");
177:
178: // Check model definition
179: prjSession.checkModelDefinition();
180:
181: System.out.println(" [ OK ]");
182: } catch (Exception e) {
183: System.out.println("\n\n [ ERROR ] : " + e);
184: e.printStackTrace();
185: } // Maybe something is wrong
186: }
187: }
|