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 17 : Interleaved Parallel Routing
024: * Author : Jordi Anguela
025: * Date : 2006/03/01
026: --------------------------------------------------------------------------
027: * Example
028: * In this example we have 3 activies: physical_test, mental_test and
029: * skill_test that we want to execute but not at the same time.
030: * We will use an iteration to execute the three activities and after
031: * that continues the process execution.
032: --------------------------------------------------------------------------
033: */package hero.client.samples.patterns.src;
034:
035: import hero.client.test.SimpleCallbackHandler;
036: import hero.interfaces.ProjectSession;
037: import hero.interfaces.ProjectSessionHome;
038: import hero.interfaces.ProjectSessionUtil;
039:
040: import javax.security.auth.login.LoginContext;
041:
042: public class Pattern17InterleavedParallelRouting {
043:
044: public static void main(String[] args) {
045: try {
046: // User Admin login
047: char[] password = { 't', 'o', 't', 'o' };
048: SimpleCallbackHandler handler = new SimpleCallbackHandler(
049: "admin", password);
050: LoginContext lc = new LoginContext("TestClient", handler);
051: lc.login();
052:
053: // Project Session Bean creation
054: ProjectSessionHome prjHome = (ProjectSessionHome) ProjectSessionUtil
055: .getHome();
056: ProjectSession prjSession = prjHome.create();
057:
058: // Model creation
059: System.out
060: .print("\n Pattern 17 - Interleaved Parallel Routing");
061: prjSession
062: .initModel("Pattern 17 - Interleaved Parallel Routing");
063:
064: // Add activities and set them to Non anticipable
065: prjSession.addNode("select_avaliable_test",
066: hero.interfaces.Constants.Nd.AND_JOIN_NODE);
067: prjSession.addNode("physical_test",
068: hero.interfaces.Constants.Nd.AND_JOIN_NODE);
069: prjSession.addNode("mental_test",
070: hero.interfaces.Constants.Nd.AND_JOIN_NODE);
071: prjSession.addNode("skill_test",
072: hero.interfaces.Constants.Nd.AND_JOIN_NODE);
073: prjSession.addNode("decide",
074: hero.interfaces.Constants.Nd.OR_JOIN_NODE);
075: prjSession.addNode("tests_report",
076: hero.interfaces.Constants.Nd.AND_JOIN_NODE);
077: prjSession.setNodeTraditional("select_avaliable_test");
078: prjSession.setNodeTraditional("physical_test");
079: prjSession.setNodeTraditional("mental_test");
080: prjSession.setNodeTraditional("skill_test");
081: prjSession.setNodeTraditional("decide");
082: prjSession.setNodeTraditional("tests_report");
083:
084: // Add transitions between activities
085: String toPhysicalTest = prjSession.addEdge(
086: "select_avaliable_test", "physical_test");
087: String toMentalTest = prjSession.addEdge(
088: "select_avaliable_test", "mental_test");
089: String toSkillTest = prjSession.addEdge(
090: "select_avaliable_test", "skill_test");
091: prjSession.addEdge("physical_test", "decide");
092: prjSession.addEdge("mental_test", "decide");
093: prjSession.addEdge("skill_test", "decide");
094: String toTestReport = prjSession.addEdge("decide",
095: "tests_report");
096:
097: // Add a Project Property and set transition conditions
098: prjSession.setProperty("test", "");
099: prjSession.setProperty("physical_test", "not_passed");
100: prjSession.setProperty("mental_test", "not_passed");
101: prjSession.setProperty("skill_test", "not_passed");
102: prjSession.setEdgeCondition(toPhysicalTest,
103: "test.equals(\"1\")");
104: prjSession.setEdgeCondition(toMentalTest,
105: "test.equals(\"2\")");
106: prjSession.setEdgeCondition(toSkillTest,
107: "test.equals(\"3\")");
108: prjSession
109: .setEdgeCondition(
110: toTestReport,
111: "physical_test.equals(\"passed\") && mental_test.equals(\"passed\") && skill_test.equals(\"passed\")");
112:
113: // Add hook that simulates the avaliability of the tests
114: String simulateAvalibilityHookScript = "import hero.interfaces.*;\n"
115: + "import java.util.Random; \n"
116: + "beforeTerminate(Object engine, Object currentNode) { \n"
117: + " hero.interfaces.ProjectSessionHome pHome = (hero.interfaces.ProjectSessionHome) hero.interfaces.ProjectSessionUtil.getHome(); \n"
118: + " hero.interfaces.ProjectSession prjSession = pHome.create(); \n"
119: + " prjSession.initModel(currentNode.getBnProject().getName()); \n"
120: + " String physicalPassed = prjSession.getProperty(\"physical_test\").getTheValue(); \n"
121: + " String mentalPassed = prjSession.getProperty(\"mental_test\").getTheValue(); \n"
122: + " String skillPassed = prjSession.getProperty(\"skill_test\").getTheValue(); \n"
123: + " Random generator = new Random(System.currentTimeMillis()); \n"
124: + " boolean avaliableTest = false; \n"
125: + " int testNum; \n"
126: + " System.out.println(\" Physical : \" + physicalPassed); \n"
127: + " System.out.println(\" Mental : \" + mentalPassed); \n"
128: + " System.out.println(\" Skill : \" + skillPassed); \n"
129: + " while (avaliableTest == false) { \n"
130: + " testNum = generator.nextInt(3) + 1; \n"
131: + " System.out.println(\" --> Test : \" + testNum); \n"
132: + " if (((testNum == 1) && (physicalPassed.equals(\"not_passed\")))"
133: + " || ((testNum == 2) && (mentalPassed.equals(\"not_passed\")))"
134: + " || ((testNum == 3) && (skillPassed.equals(\"not_passed\")))) { \n"
135: + " avaliableTest = true; \n"
136: + " } \n"
137: + " } \n"
138: + " prjSession.setProperty(\"test\", Integer.toString(testNum)); \n"
139: + " System.out.println(\" --> Tests remaining : \" + testNum); \n"
140: + "} \n";
141: prjSession.addNodeInterHook("select_avaliable_test",
142: "Select test Hook",
143: hero.interfaces.Constants.Nd.BEFORETERMINATE,
144: hero.interfaces.Constants.Hook.BSINTERACTIVE,
145: simulateAvalibilityHookScript);
146:
147: // Add hook that set each test as passed
148: String testPassedHookScript = "import hero.interfaces.*;\n"
149: + "import java.util.Random; \n"
150: + "beforeTerminate(Object engine, Object currentNode) { \n"
151: + " hero.interfaces.ProjectSessionHome pHome = (hero.interfaces.ProjectSessionHome) hero.interfaces.ProjectSessionUtil.getHome(); \n"
152: + " hero.interfaces.ProjectSession prjSession = pHome.create(); \n"
153: + " prjSession.initModel(currentNode.getBnProject().getName()); \n"
154: + " String nodeName = currentNode.getName(); \n"
155: + " prjSession.setProperty(nodeName, \"passed\"); \n"
156: + " System.out.println(\" --> Test \" + nodeName + \" : passed\"); \n"
157: + "} \n";
158: prjSession.addNodeInterHook("physical_test",
159: "physical_test Hook",
160: hero.interfaces.Constants.Nd.BEFORETERMINATE,
161: hero.interfaces.Constants.Hook.BSINTERACTIVE,
162: testPassedHookScript);
163: prjSession.addNodeInterHook("mental_test",
164: "mental_test Hook",
165: hero.interfaces.Constants.Nd.BEFORETERMINATE,
166: hero.interfaces.Constants.Hook.BSINTERACTIVE,
167: testPassedHookScript);
168: prjSession.addNodeInterHook("skill_test",
169: "skill_test Hook",
170: hero.interfaces.Constants.Nd.BEFORETERMINATE,
171: hero.interfaces.Constants.Hook.BSINTERACTIVE,
172: testPassedHookScript);
173:
174: // Add iteration until all tests are passed
175: prjSession
176: .addIteration(
177: "decide",
178: "select_avaliable_test",
179: "(physical_test.equals(\"not_passed\") || mental_test.equals(\"not_passed\") || skill_test.equals(\"not_passed\"))");
180:
181: // Add and set a role for the user admin to execute the activities
182: prjSession.addRole("Executor",
183: "Rol that enables to execute the activities");
184: prjSession.setUserRole("admin", "Executor");
185: prjSession.setNodeRole("select_avaliable_test", "Executor");
186: prjSession.setNodeRole("physical_test", "Executor");
187: prjSession.setNodeRole("mental_test", "Executor");
188: prjSession.setNodeRole("skill_test", "Executor");
189: prjSession.setNodeRole("decide", "Executor");
190: prjSession.setNodeRole("tests_report", "Executor");
191:
192: // Check model definition
193: prjSession.checkModelDefinition();
194:
195: System.out.println(" [ OK ]");
196: } catch (Exception e) {
197: System.out.println("\n\n [ ERROR ] : " + e);
198: e.printStackTrace();
199: } // Maybe something is wrong
200: }
201: }
|