001: /**
002: *
003: * Bonita
004: * Copyright (C) 1999 Bull S.A.
005: * Bull 68 route de versailles 78434 Louveciennes Cedex France
006: * Further information: bonita@objectweb.org
007: *
008: * This library is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU Lesser General Public
010: * License as published by the Free Software Foundation; either
011: * version 2.1 of the License, or any later version.
012: *
013: * This library is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * Lesser General Public License for more details.
017: *
018: * You should have received a copy of the GNU Lesser General Public
019: * License along with this library; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
021: * USA
022: *
023: --------------------------------------------------------------------------
024: * Class ArbitraryCycles.java created by Jordi Anguela - June 2006
025: --------------------------------------------------------------------------
026: */package hero.client.samples.iterations.src;
027:
028: import hero.client.test.SimpleCallbackHandler;
029: import hero.interfaces.Constants;
030: import hero.interfaces.ProjectSession;
031: import hero.interfaces.ProjectSessionHome;
032: import hero.interfaces.ProjectSessionUtil;
033:
034: import javax.security.auth.login.LoginContext;
035:
036: public class ArbitraryCycles {
037: static public void main(String[] args) throws Exception {
038: try {
039: // User Admin login
040: char[] password = { 't', 'o', 't', 'o' };
041: SimpleCallbackHandler handler = new SimpleCallbackHandler(
042: "admin", password);
043: LoginContext lc = new LoginContext("TestClient", handler);
044: lc.login();
045:
046: ProjectSessionHome projectSessionh = ProjectSessionUtil
047: .getHome();
048: ProjectSession ps = projectSessionh.create();
049: System.out.print("\n Creating ArbitraryCycles model");
050: ps.initModel("ArbitraryCycles");
051:
052: // Set Properties
053: ps.setProperty("condition1", "50"); // % to do 1st iteration
054: ps.setProperty("condition2", "50"); // % to do 2nd iteration
055: ps.setProperty("randomNum", "0"); // random number who decides if we iterate or not
056:
057: // Set Nodes
058: ps.addNode("A", hero.interfaces.Constants.Nd.AND_JOIN_NODE);
059: ps
060: .addNode(
061: "B",
062: hero.interfaces.Constants.Nd.AND_JOIN_AUTOMATIC_NODE);
063: ps
064: .addNode(
065: "C",
066: hero.interfaces.Constants.Nd.AND_JOIN_AUTOMATIC_NODE);
067: ps
068: .addNode(
069: "D",
070: hero.interfaces.Constants.Nd.AND_JOIN_AUTOMATIC_NODE);
071: ps
072: .addNode(
073: "E",
074: hero.interfaces.Constants.Nd.AND_JOIN_AUTOMATIC_NODE);
075: ps
076: .addNode(
077: "F",
078: hero.interfaces.Constants.Nd.AND_JOIN_AUTOMATIC_NODE);
079:
080: ps.setNodeTraditional("A");
081: ps.setNodeTraditional("B");
082: ps.setNodeTraditional("C");
083: ps.setNodeTraditional("D");
084: ps.setNodeTraditional("E");
085: ps.setNodeTraditional("F");
086:
087: // Set Edges
088: ps.addEdge("A", "B");
089: ps.addEdge("B", "C");
090: ps.addEdge("C", "D");
091: String fromDtoE = ps.addEdge("D", "E");
092: String fromEtoF = ps.addEdge("E", "F");
093:
094: // Set transition conditions
095: ps
096: .setEdgeCondition(fromDtoE,
097: "(new Integer(randomNum).intValue() >= new Integer(condition1).intValue())");
098: ps
099: .setEdgeCondition(fromEtoF,
100: "(new Integer(randomNum).intValue() >= new Integer(condition2).intValue())");
101:
102: // Set Hook
103: String generateRandomScript = "import hero.interfaces.*;\n"
104: + "import java.util.Random; \n"
105: + "beforeTerminate(Object b,Object n) {\n\n\n"
106: + " hero.interfaces.ProjectSessionHome pHome = (hero.interfaces.ProjectSessionHome) hero.interfaces.ProjectSessionUtil.getHome(); \n"
107: + " hero.interfaces.ProjectSession prjSession = pHome.create(); \n"
108: + " prjSession.initModel(n.getBnProject().getName()); \n"
109: + " String name = n.getName(); \n"
110: + " Random generator = new Random(System.currentTimeMillis()); \n"
111: + " int newRnd = generator.nextInt(100); \n"
112: + " prjSession.setProperty(\"randomNum\", Integer.toString(newRnd)); \n"
113: + " System.out.print(\" --> BeforeTerminate, node '\" + name + \"' new random value : \" + newRnd); \n"
114: + " BnProjectPropertyValue conditionProp; \n"
115: + " if (name.equals(\"D\")) { \n"
116: + " conditionProp = prjSession.getProperty(\"condition1\"); \n"
117: + " if (newRnd < Integer.parseInt(conditionProp.getTheValue())) { \n"
118: + " System.out.println(\" --> ITERATE --> next activity 'B'\"); \n"
119: + " } else { \n"
120: + " System.out.println(\" --> DON'T ITERATE --> next activity 'E'\"); \n"
121: + " } \n"
122: + " } else { \n"
123: + " conditionProp = prjSession.getProperty(\"condition2\"); \n"
124: + " if (newRnd < Integer.parseInt(conditionProp.getTheValue())) { \n"
125: + " System.out.println(\" --> ITERATE --> next activity 'C'\"); \n"
126: + " } else { \n"
127: + " System.out.println(\" --> DON'T ITERATE --> next activity 'F'\"); \n"
128: + " } \n" + " } \n" + "} \n";
129: ps
130: .addNodeInterHook(
131: "D",
132: "Generates random number and saves it in 'randomNum' var",
133: Constants.Nd.BEFORETERMINATE,
134: Constants.Hook.BSINTERACTIVE,
135: generateRandomScript);
136: ps
137: .addNodeInterHook(
138: "E",
139: "Generates random number and saves it in 'randomNum' var",
140: Constants.Nd.BEFORETERMINATE,
141: Constants.Hook.BSINTERACTIVE,
142: generateRandomScript);
143:
144: // Set Iterations
145: ps
146: .addIteration("D", "B",
147: "(new Integer(randomNum).intValue() < new Integer(condition1).intValue())");
148: ps
149: .addIteration("E", "C",
150: "(new Integer(randomNum).intValue() < new Integer(condition2).intValue())");
151:
152: // Set activity roles
153: ps.addRole("Administrator", "The system administator role");
154: ps.setNodeRole("A", "Administrator");
155: //ps.setNodeRole("B", "Administrator"); // uncomment if you want to execute them manually
156: //ps.setNodeRole("C", "Administrator");
157: //ps.setNodeRole("D", "Administrator");
158: //ps.setNodeRole("E", "Administrator");
159: //ps.setNodeRole("F", "Administrator");
160:
161: // Set role mapper
162: ps.addRoleMapper("Administrator", "AdministratorMapper",
163: Constants.Mapper.PROPERTIES);
164:
165: // Checks if the model is correctly defined
166: ps.checkModelDefinition();
167:
168: System.out.println(" [ OK ]");
169: } catch (Exception e) {
170: System.out.println("\n\n [ ERROR ] : " + e);
171: e.printStackTrace();
172: }
173: }
174: }
|