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 16 : Deferred Choice
024: * Author : Jordi Anguela
025: * Date : 2006/03/01
026: --------------------------------------------------------------------------
027: * Example
028: * In this example we have two activities avaliable: load_truck1 and
029: * load_truck2. select_avaliable_resource activity has hook that decides
030: * which truck is avaliable (the decision is taken at random using a hook)
031: --------------------------------------------------------------------------
032: */package hero.client.samples.patterns.src;
033:
034: import hero.client.test.SimpleCallbackHandler;
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 Pattern16DeferredChoice {
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.print("\n Pattern 16 - Deferred Choice");
059: prjSession.initModel("Pattern 16 - Deferred Choice");
060:
061: // Add activities and set them to Non anticipable
062: prjSession.addNode("select_avaliable_resource",
063: hero.interfaces.Constants.Nd.AND_JOIN_NODE);
064: prjSession.addNode("load_truck1",
065: hero.interfaces.Constants.Nd.AND_JOIN_NODE);
066: prjSession.addNode("load_truck2",
067: hero.interfaces.Constants.Nd.AND_JOIN_NODE);
068: prjSession.setNodeTraditional("select_avaliable_resource");
069: prjSession.setNodeTraditional("load_truck1");
070: prjSession.setNodeTraditional("load_truck2");
071:
072: // Add transitions between activities
073: String toTruck1 = prjSession.addEdge(
074: "select_avaliable_resource", "load_truck1");
075: String toTruck2 = prjSession.addEdge(
076: "select_avaliable_resource", "load_truck2");
077:
078: // Add a Project Property and set transition conditions
079: prjSession.setProperty("truck_number", "0");
080: prjSession.setEdgeCondition(toTruck1,
081: "truck_number.equals(\"1\")");
082: prjSession.setEdgeCondition(toTruck2,
083: "truck_number.equals(\"2\")");
084:
085: // Set a hook that simulates trucks avaliability
086: String simulateAvalibilityHookScript = "import hero.interfaces.*;\n"
087: + "import java.util.Random; \n"
088: + "beforeTerminate(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: + " Random generator = new Random(System.currentTimeMillis()); \n"
093: + " int truck = generator.nextInt(2) + 1; \n"
094: + " prjSession.setProperty(\"truck_number\", Integer.toString(truck)); \n"
095: + " System.out.println(\" --> Truck avaliable : \" + truck); \n"
096: + "} \n";
097: prjSession.addNodeInterHook("select_avaliable_resource",
098: "select_avaliable_resource hook",
099: hero.interfaces.Constants.Nd.BEFORETERMINATE,
100: hero.interfaces.Constants.Hook.BSINTERACTIVE,
101: simulateAvalibilityHookScript);
102:
103: // Add and set a role for the user admin to execute the activities
104: prjSession.addRole("Executor",
105: "Rol that enables to execute the activities");
106: prjSession.setUserRole("admin", "Executor");
107: prjSession.setNodeRole("select_avaliable_resource",
108: "Executor");
109: prjSession.setNodeRole("load_truck1", "Executor");
110: prjSession.setNodeRole("load_truck2", "Executor");
111:
112: // Check model definition
113: prjSession.checkModelDefinition();
114:
115: System.out.println(" [ OK ]");
116: } catch (Exception e) {
117: System.out.println("\n\n [ ERROR ] : " + e);
118: e.printStackTrace();
119: } // Maybe something is wrong
120: }
121: }
|