KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > clif > scenario > util > isac > engine > behavior > BehaviorManager


1 /*
2  * CLIF is a Load Injection Framework
3  * Copyright (C) 2004 France Telecom R&D
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * CLIF
20  *
21  * Contact: clif@objectweb.org
22  */

23 package org.objectweb.clif.scenario.util.isac.engine.behavior;
24
25 import java.util.Hashtable JavaDoc;
26 import java.util.Vector JavaDoc;
27
28 import org.objectweb.clif.scenario.util.isac.engine.behavior.node.ExecutableNode;
29 import org.objectweb.clif.scenario.util.isac.engine.behavior.node.description.ChoiceDescription;
30 import org.objectweb.clif.scenario.util.isac.engine.behavior.node.description.IfDescription;
31 import org.objectweb.clif.scenario.util.isac.engine.behavior.node.description.PreemptiveDescription;
32 import org.objectweb.clif.scenario.util.isac.engine.behavior.node.description.SampleDescription;
33 import org.objectweb.clif.scenario.util.isac.engine.behavior.node.description.TestDescription;
34 import org.objectweb.clif.scenario.util.isac.engine.behavior.node.description.TimerDescription;
35 import org.objectweb.clif.scenario.util.isac.engine.behavior.node.description.WhileDescription;
36 import org.objectweb.clif.scenario.util.isac.util.SessionObjectHashtable;
37 import org.w3c.dom.Element JavaDoc;
38 import org.w3c.dom.NamedNodeMap JavaDoc;
39 import org.w3c.dom.Node JavaDoc;
40 import org.w3c.dom.NodeList JavaDoc;
41
42 /**
43  * This is the implementation of the behavior manager, this manager will be able
44  * to read a behavior in a tree generated with DOM.
45  *
46  * @author JC Meillaud
47  * @author A Peyrard
48  */

49 public class BehaviorManager {
50     // attribute
51
// attributes initialized in this class
52
private Hashtable JavaDoc behaviorTable;
53     // references of tables shared with others manager, only used in the init
54
private Hashtable JavaDoc methodNameConversionTable;
55
56     private Hashtable JavaDoc sessionObjectPluginNameTable;
57
58     private Hashtable JavaDoc sessionObjectTable;
59     // in this table, all the object with are classed by behavior, where they are used
60
private Hashtable JavaDoc sessionObjectForABehavior ;
61     // temporary attributes used in the behavior analyze
62
private SampleDescription analyzedSampleDescription;
63
64     private TestDescription analyzedTestDescription;
65
66     private TimerDescription analyzedTimerDescription;
67     
68     // we will store in this attribute the session object ids used in the analized behavior
69
private Vector JavaDoc analyzedUsedSessionObjects ;
70     
71     private ExecutableNode currentNode;
72
73     /**
74      * Constructor, build a new behavior manager
75      *
76      * @param mnct
77      * The reference to the method name conversion table
78      * @param sopn
79      * The reference to the session object plugin name table
80      * @param sot
81      * The reference to the session object table
82      * @param sofab The reference of the table which will store hashtable wich store all session object
83      * for each behavior
84      */

85     public BehaviorManager(Hashtable JavaDoc mnct, Hashtable JavaDoc sopn, Hashtable JavaDoc sot, Hashtable JavaDoc sofab) {
86         // init the attribute
87
this.behaviorTable = new Hashtable JavaDoc();
88         // init the references
89
this.methodNameConversionTable = mnct;
90         this.sessionObjectPluginNameTable = sopn;
91         this.sessionObjectTable = sot;
92         this.sessionObjectForABehavior = sofab ;
93     }
94
95     /**
96      * This method permit to add a new behavior to the manager This method will
97      * analyze the behavior tree and build an executable tree
98      *
99      * @param node
100      * The root node of the executable tree
101      */

102     public void addBehavior(Node node) {
103         // get the behavior id
104
NamedNodeMap JavaDoc attributes = node.getAttributes();
105         String JavaDoc id = attributes.getNamedItem("id").getNodeValue();
106         // create a new behavior executable tree
107
ExecutableNode root = new ExecutableNode(
108                 org.objectweb.clif.scenario.util.isac.util.tree.Node.BEHAVIOR,
109                 null);
110         // init the temporary vector storing the ids of session objects used in the
111
// analized behaviors
112
this.analyzedUsedSessionObjects = new Vector JavaDoc() ;
113         // visit the tree
114
this.visitBehaviorNode(node, root);
115         // add the created tree to the table
116
this.behaviorTable.put(id, root);
117         // create a CloneableHashtable to store all the cloneable session object for the analyzed behavior
118
SessionObjectHashtable cloneableHashtable = new SessionObjectHashtable() ;
119         // for each session object used
120
for (int i=0;i<this.analyzedUsedSessionObjects.size();i++) {
121             // get the id of the so
122
String JavaDoc sessionObjectId = (String JavaDoc)this.analyzedUsedSessionObjects.elementAt(i) ;
123             // add an entry in the table
124
cloneableHashtable.put(sessionObjectId, this.sessionObjectTable.get(sessionObjectId)) ;
125         }
126         // add the created hashtable in the table which store all the session object for a behavior
127
this.sessionObjectForABehavior.put(id,cloneableHashtable) ;
128     }
129
130     /**
131      * This method visit recursively the behavior tree
132      *
133      * @param node
134      * The root tree node, of the tree generated with DOM
135      * @param parent
136      * The parent node to add new nodes created
137      */

138     private void visitBehaviorNode(Node node, ExecutableNode parent) {
139         // executable which store the child, if we create one
140
ExecutableNode child = null;
141         // switch between node type
142
String JavaDoc tagName = null;
143         switch (node.getNodeType()) {
144         case Node.ELEMENT_NODE:
145             tagName = ((Element JavaDoc) node).getTagName();
146             // switch between tag names
147
if (tagName
148                     .equals(org.objectweb.clif.scenario.util.isac.util.tree.Node.SAMPLE)) {
149                 // get the sample name
150
NamedNodeMap JavaDoc attributes = node.getAttributes();
151                 String JavaDoc methodName = attributes.getNamedItem("name")
152                         .getNodeValue();
153                 String JavaDoc usedSessionObject = attributes.getNamedItem("use")
154                         .getNodeValue();
155                 // if the session object is not in the vector storing the session object used, add it
156
if (!this.analyzedUsedSessionObjects.contains(usedSessionObject))
157                     this.analyzedUsedSessionObjects.add(usedSessionObject) ;
158                 // get the plugin name for this session object
159
String JavaDoc pluginName = (String JavaDoc) this.sessionObjectPluginNameTable
160                         .get(usedSessionObject);
161                 // create the key to get the method in the conversion table
162
String JavaDoc key = pluginName + "." + tagName + "." + methodName;
163                 // get the number of the method
164
int number = ((Integer JavaDoc) this.methodNameConversionTable.get(key))
165                         .intValue();
166                 // init the description
167
SampleDescription sampleDescription = new SampleDescription(
168                         usedSessionObject, number);
169                 // init the executable node
170
child = new ExecutableNode(tagName, sampleDescription);
171                 // add the node to the parent node given in parameter
172
parent.addChild(child);
173                 // store references of the node and the description
174
this.analyzedSampleDescription = sampleDescription;
175                 this.currentNode = child;
176             }
177             if (tagName
178                     .equals(org.objectweb.clif.scenario.util.isac.util.tree.Node.TIMER)) {
179                 // get the timer name
180
NamedNodeMap JavaDoc attributes = node.getAttributes();
181                 String JavaDoc methodName = attributes.getNamedItem("name")
182                         .getNodeValue();
183                 String JavaDoc usedSessionObject = attributes.getNamedItem("use")
184                         .getNodeValue();
185                 // if the session object is not in the vector storing the session object used, add it
186
if (!this.analyzedUsedSessionObjects.contains(usedSessionObject))
187                     this.analyzedUsedSessionObjects.add(usedSessionObject) ;
188                 // get the plugin name for this session object
189
String JavaDoc pluginName = (String JavaDoc) this.sessionObjectPluginNameTable
190                         .get(usedSessionObject);
191                 // create the key to get the method in the conversion table
192
String JavaDoc key = pluginName + "." + tagName + "." + methodName;
193                 // get the number of the method
194
int number = ((Integer JavaDoc) this.methodNameConversionTable.get(key))
195                         .intValue();
196                 // init the description
197
TimerDescription timerDescription = new TimerDescription(
198                         usedSessionObject, number);
199                 // init child node
200
child = new ExecutableNode(tagName, timerDescription);
201                 // add the node to the parent node given in parameter
202
parent.addChild(child);
203                 // store references of the node and the description
204
this.analyzedTimerDescription = timerDescription;
205                 this.currentNode = child;
206             }
207             if (tagName
208                     .equals(org.objectweb.clif.scenario.util.isac.util.tree.Node.IF)) {
209                 // init an empty test description
210
TestDescription condition = new TestDescription();
211                 // init an if description
212
IfDescription ifDescription = new IfDescription(condition);
213                 child = new ExecutableNode(tagName, ifDescription);
214                 // add the node to the parent node given in parameter
215
parent.addChild(child);
216                 // store references of the node and the description
217
this.analyzedTestDescription = condition;
218                 this.currentNode = child;
219             }
220             if (tagName
221                     .equals(org.objectweb.clif.scenario.util.isac.util.tree.Node.WHILE)) {
222                 // init an empty test description
223
TestDescription condition = new TestDescription();
224                 // init an while description
225
WhileDescription whileDescription = new WhileDescription(
226                         condition);
227                 child = new ExecutableNode(tagName, whileDescription);
228                 // add the node to the parent node given in parameter
229
parent.addChild(child);
230                 // store references of the node and the description
231
this.analyzedTestDescription = condition;
232                 this.currentNode = child;
233             }
234             if (tagName
235                     .equals(org.objectweb.clif.scenario.util.isac.util.tree.Node.PREEMPTIVE)) {
236                 // init an empty test description
237
TestDescription condition = new TestDescription();
238                 // init an if description
239
PreemptiveDescription preemptiveDescription = new PreemptiveDescription(
240                         condition);
241                 child = new ExecutableNode(tagName, preemptiveDescription);
242                 // add the node to the parent node given in parameter
243
parent.addChild(child);
244                 // store references of the node and the description
245
this.analyzedTestDescription = condition;
246                 this.currentNode = child;
247             }
248             if (tagName
249                     .equals(org.objectweb.clif.scenario.util.isac.util.tree.Node.CONDITION)) {
250                 // get the test name
251
NamedNodeMap JavaDoc attributes = node.getAttributes();
252                 String JavaDoc methodName = attributes.getNamedItem("name")
253                         .getNodeValue();
254                 String JavaDoc usedSessionObject = attributes.getNamedItem("use")
255                         .getNodeValue();
256                 // if the session object is not in the vector storing the session object used, add it
257
if (!this.analyzedUsedSessionObjects.contains(usedSessionObject))
258                     this.analyzedUsedSessionObjects.add(usedSessionObject) ;
259                 // get the plugin name for this session object
260
String JavaDoc pluginName = (String JavaDoc) this.sessionObjectPluginNameTable
261                         .get(usedSessionObject);
262                 // create the key to get the method in the conversion table
263
String JavaDoc key = pluginName
264                         + "."
265                         + org.objectweb.clif.scenario.util.isac.util.tree.Node.TEST
266                         + "." + methodName;
267                 // get the number of the method
268
int number = ((Integer JavaDoc) this.methodNameConversionTable.get(key))
269                         .intValue();
270                 // set the attributes to the description
271
this.analyzedTestDescription.setMethodNumber(number);
272                 this.analyzedTestDescription.setSessionObject(usedSessionObject);
273             }
274             if (tagName
275                     .equals(org.objectweb.clif.scenario.util.isac.util.tree.Node.NCHOICE)) {
276                 // init the child
277
child = new ExecutableNode(tagName, null);
278                 // add the child
279
parent.addChild(child);
280             }
281             if (tagName
282                     .equals(org.objectweb.clif.scenario.util.isac.util.tree.Node.THEN)) {
283                 // init the child
284
child = new ExecutableNode(tagName, null);
285                 // add the child
286
parent.addChild(child);
287             }
288             if (tagName
289                     .equals(org.objectweb.clif.scenario.util.isac.util.tree.Node.ELSE)) {
290                 // init the child
291
child = new ExecutableNode(tagName, null);
292                 // add the child
293
parent.addChild(child);
294             }
295             if (tagName
296                     .equals(org.objectweb.clif.scenario.util.isac.util.tree.Node.CHOICE)) {
297                 // get the proba value
298
NamedNodeMap JavaDoc attributes = node.getAttributes();
299                 String JavaDoc proba = attributes.getNamedItem("proba").getNodeValue();
300                 // create a new description
301
ChoiceDescription choiceDescription = new ChoiceDescription(
302                         (new Integer JavaDoc(proba)).intValue());
303                 // init the child node
304
child = new ExecutableNode(tagName, choiceDescription);
305                 // add the child
306
parent.addChild(child);
307             }
308             if (tagName
309                     .equals(org.objectweb.clif.scenario.util.isac.util.tree.Node.PARAMS)) {
310                 // do nothing
311
}
312             if (tagName
313                     .equals(org.objectweb.clif.scenario.util.isac.util.tree.Node.PARAM)) {
314                 // get the parameter value
315
NamedNodeMap JavaDoc attributes = node.getAttributes();
316                 String JavaDoc value = attributes.getNamedItem("value").getNodeValue();
317                 String JavaDoc name = attributes.getNamedItem("name").getNodeValue();
318                 if (currentNode
319                         .getType()
320                         .equals(
321                                 org.objectweb.clif.scenario.util.isac.util.tree.Node.SAMPLE))
322                     this.analyzedSampleDescription.addParamsValue(name,value);
323                 else if (currentNode
324                         .getType()
325                         .equals(
326                                 org.objectweb.clif.scenario.util.isac.util.tree.Node.TIMER))
327                     this.analyzedTimerDescription.addParamsValue(name,value);
328                 else if (org.objectweb.clif.scenario.util.isac.util.tree.Node
329                         .isControllerNode(currentNode.getType()))
330                     this.analyzedTestDescription.addParamsValue(name,value);
331             }
332         }
333         // analyse the child
334
if (node.hasChildNodes()) {
335             NodeList JavaDoc children = node.getChildNodes();
336             for (int i = 0; i < children.getLength(); i++) {
337                 Node tempNode = children.item(i);
338                 if (tempNode.getNodeType() == Node.ELEMENT_NODE) {
339                     if (child == null)
340                         visitBehaviorNode(tempNode, parent);
341                     else
342                         visitBehaviorNode(tempNode, child);
343                 }
344             }
345         }
346     }
347
348     /**
349      * This method permit to get a behavior tree
350      *
351      * @param id
352      * The id of the behavior
353      * @return The behavior tree
354      */

355     public ExecutableNode getBehavior(String JavaDoc id) {
356         return (ExecutableNode) this.behaviorTable.get(id);
357     }
358
359 }
Popular Tags