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 12 : Multiple Instances witout Synchronization
024: * Author : Jordi Anguela
025: * Date : 2006/03/01
026: --------------------------------------------------------------------------
027: * Example
028: * In this example we have an activity called 'order_books' that simulates
029: * a book order. It instantiates three sub processes to manage each book.
030: * These sub process are independent of next activity 'send_bill' and
031: * between them.
032: --------------------------------------------------------------------------
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 Pattern12MultipleInstancesWithoutSynchronization {
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 12 - Multiple Instances without Synchronization");
062: prjSession
063: .initModel("Pattern 12 - Multiple Instances without Synchronization");
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("send_bill",
069: hero.interfaces.Constants.Nd.AND_JOIN_NODE);
070: prjSession.setNodeTraditional("order_books");
071: prjSession.setNodeTraditional("send_bill");
072: prjSession.addEdge("order_books", "send_bill");
073:
074: // Add Project Properties and set transition conditions
075: prjSession.setProperty("books", "3");
076:
077: // SubProcess definition
078: ProjectSession prjSession2 = prjHome.create();
079: prjSession2.initModel("Process book order (SubProcess)");
080: prjSession2.addNode("update_stock",
081: Constants.Nd.AND_JOIN_NODE);
082: prjSession2
083: .addNode("send_book", Constants.Nd.AND_JOIN_NODE);
084: prjSession2.setNodeTraditional("update_stock");
085: prjSession2.setNodeTraditional("send_book");
086: prjSession2.addEdge("update_stock", "send_book");
087: String update_stockHookScript = "import hero.interfaces.*;\n"
088: + "beforeStart(Object engine, Object currentNode) {\n\n\n"
089: + " hero.interfaces.ProjectSessionHome pHome = (hero.interfaces.ProjectSessionHome) hero.interfaces.ProjectSessionUtil.getHome(); \n"
090: + " hero.interfaces.ProjectSession prjSession = pHome.create(); \n"
091: + " prjSession.initModel(currentNode.getBnProject().getName()); \n"
092: + " BnProjectPropertyValue bookNameProp = prjSession.getProperty(\"book_name\"); \n"
093: + " System.out.println(\" ---> update_stock : \" + bookNameProp.getTheValue()); \n"
094: + "}";
095: prjSession2.addNodeInterHook("update_stock",
096: "update_stock Hook",
097: hero.interfaces.Constants.Nd.BEFORESTART,
098: hero.interfaces.Constants.Hook.BSINTERACTIVE,
099: update_stockHookScript);
100: prjSession2
101: .addRole("Executor",
102: "Rol that enables to execute the activities inside the SubProcess");
103: prjSession2.setUserRole("admin", "Executor");
104: prjSession2.setNodeRole("update_stock", "Executor");
105: prjSession2.setNodeRole("send_book", "Executor");
106: prjSession2.remove();
107:
108: // Add Hook to 'Order books' activity to create a SubProces for each book
109: String approveHookScript = "import hero.interfaces.*;\n"
110: + "beforeTerminate(Object engine, Object currentNode) {\n\n\n"
111: + " hero.interfaces.ProjectSessionHome pHome = (hero.interfaces.ProjectSessionHome) hero.interfaces.ProjectSessionUtil.getHome(); \n"
112: + " hero.interfaces.ProjectSession prjSession = pHome.create(); \n"
113: + " prjSession.initModel(currentNode.getBnProject().getName()); \n"
114: + " BnProjectPropertyValue booksProp = prjSession.getProperty(\"books\"); \n"
115: + " int books = Integer.parseInt(booksProp.getTheValue()); \n"
116: + " for (int i = 0; i < books; i++) { \n"
117: + " String nodeName = \"book\" + Integer.toString(i); \n"
118: + " prjSession.addNodeSubProcess(nodeName, \"Process book order (SubProcess)\"); \n"
119: + " prjSession.setNodeProperty(nodeName, \"book_name\", nodeName, true); \n"
120: + " prjSession.addEdge(\"order_books\", nodeName); \n"
121: + " prjSession.setNodeRole(nodeName, \"Executor\"); \n"
122: + " } \n" + "}";
123: prjSession.addNodeInterHook("order_books",
124: "Order books Hook",
125: hero.interfaces.Constants.Nd.BEFORETERMINATE,
126: hero.interfaces.Constants.Hook.BSINTERACTIVE,
127: approveHookScript);
128:
129: // Add and set a role for the user admin to execute the activities
130: prjSession.addRole("Executor",
131: "Rol that enables to execute the activities");
132: prjSession.setUserRole("admin", "Executor");
133: prjSession.setNodeRole("order_books", "Executor");
134: prjSession.setNodeRole("send_bill", "Executor");
135:
136: // Check model definition
137: prjSession.checkModelDefinition();
138:
139: System.out.println(" [ OK ]");
140: } catch (Exception e) {
141: System.out.println("\n\n [ ERROR ] : " + e);
142: e.printStackTrace();
143: } // Maybe something is wrong
144: }
145: }
|