dol: initial dol commit
[jump.git] / dol / src / dol / datamodel / architecture / ArchiResource.java
1 /* $Id: ArchiResource.java 1 2010-02-24 13:03:05Z haidw $ */
2 package dol.datamodel.architecture;
3
4 import java.util.Iterator;
5 import java.util.StringTokenizer;
6 import java.util.Vector;
7
8 import dol.visitor.ArchiVisitor;
9
10 /**
11  * This class is the basic class which abstracts an architectural resource
12  * of a generic architecture. The architectural resource has a name and a
13  * list of included nodes.
14  */
15 public class ArchiResource implements Cloneable {
16     /**
17      *  Constructor to create an architectural resource with a name and
18      *  an empty node list.
19      */
20     public ArchiResource(String name) {
21         _name = name;
22         _basename = name;
23         _nodeList = new Vector<Node>();
24         _cfgList = new Vector<Configuration>();
25     }
26
27     /**
28      * Accept a Visitor
29      *
30      * @param x visitor object
31      */
32     public void accept(ArchiVisitor x) {
33         x.visitComponent(this);
34     }
35
36     /**
37      * Clone this architectural resource.
38      *
39      * @return new instance of the architectural resource.
40      */
41     @SuppressWarnings("unchecked")
42     public Object clone() {
43         try {
44             ArchiResource newObj = (ArchiResource) super.clone();
45             newObj.setName(_name);
46             newObj.setBasename(_basename);
47             newObj.setNodeList((Vector)_nodeList.clone() );
48             newObj.setCfgList((Vector)_cfgList.clone() );
49             return (newObj);
50         } catch (CloneNotSupportedException e) {
51             System.out.println("Error Clone not Supported");
52         }
53         return null;
54     }
55
56     /**
57      * Get the name of this architectural resource.
58      *
59      * @return name of the architectural resource
60      */
61     public String getName() {
62         return _name;
63     }
64
65     /**
66      * Set the name of this architectural resource.
67      *
68      * @param name name of the architectural resource
69      */
70     public void setName(String name) {
71         _name = name;
72     }
73
74     /**
75      * Get the basename of this architectural resource.
76      *
77      * @return basename of the architectural resource
78      */
79     public String getBasename() {
80         return _basename;
81     }
82
83     /**
84      * Set the basename of this architectural resource.
85      *
86      * @param basename name of the architectural resource
87      */
88     public void setBasename(String basename) {
89         _basename = basename;
90     }
91
92     /**
93      * Get the iterator indices of this process.
94      *
95      * @return range
96      */
97     public Vector<Integer> getIteratorIndices() {
98         Vector<Integer> indices = new Vector<Integer>();
99         StringTokenizer tokenizer =
100             new StringTokenizer(_name.replaceAll(_basename, ""), "_");
101         while (tokenizer.hasMoreTokens()) {
102             indices.add(Integer.valueOf(tokenizer.nextToken()));
103         }
104         return indices;
105     }
106     
107     /**
108      * Get the list of nodes of this architectural resource.
109      *
110      * @return list of nodes
111      */
112     public Vector<Node> getNodeList() {
113         return _nodeList;
114     }
115
116     /**
117      * Set the list of nodes of this architectural resource.
118      *
119      * @param nodeList nodes list
120      */
121     public void setNodeList(Vector<Node> nodeList) {
122         _nodeList = nodeList;
123     }
124
125     /**
126      * Get the hierarchical parent of this architectural resource.
127      *
128      * @return parent of this architectural resource
129      */
130     public ArchiResource getParentResource() {
131         return _parentResource;
132     }
133
134     /**
135      * Set the hierarchical parent of this architectural resource.
136      *
137      * @param parentResource new parent
138      */
139     public void setParentResource(ArchiResource parentResource) {
140         _parentResource = parentResource;
141     }
142
143     /**
144      * Return a string representation of the architectural resource.
145      *
146      * @return string representation of the architectural resource
147      */
148     public String toString() {
149         return "ArchiResource: " + _name;
150     }
151
152     /**
153      * Return a node (which has a specific name). Return null when node
154      * cannot be found.
155      *
156      * @param name of the node to search for
157      * @return node with the specified name
158      */
159     public Node getNode(String name) {
160         Iterator<Node> i;
161         i = _nodeList.iterator();
162         while (i.hasNext()) {
163             Node node = i.next();
164             if (node.getName().equals(name)) {
165                 return node;
166             }
167          }
168          return null;
169      }
170
171     /**
172      * Return a node. Return null when node cannot be found.
173      *
174      * @return node
175      */
176     public Node getNode() {
177         Iterator<Node> i;
178         i = _nodeList.iterator();
179         while (i.hasNext()) {
180             Node node = i.next();
181             return node;
182          }
183          return null;
184      }
185
186     /**
187      * Has this resource nodes?
188      *
189      * @return boolean value
190      */
191     public boolean hasNodes() {
192         return !_nodeList.isEmpty();
193     }
194
195
196     /**
197      * Return the first node of the nodelist.
198      *
199      * @return the first node of the nodelist.
200      */
201     public Node getFirstNode() {
202         return (Node) _nodeList.firstElement();
203     }
204
205     /**
206      * Get the list of configurations of this resource.
207      *
208      * @return list of configurations
209      */
210     public Vector<Configuration> getCfgList() {
211         return _cfgList;
212     }
213
214     /**
215      * Set the list of configurations of this resource.
216      *
217      * @param cfgList configuration list
218      */
219     public void setCfgList(Vector<Configuration> cfgList) {
220         _cfgList = cfgList;
221     }
222
223     /**
224      * Return a configuration which has a specific name. Return
225      * null when configuration cannot be found.
226      *
227      * @param name name of the configuration to search for
228      * @return configuration with the specified name
229      */
230      public Configuration getCfg(String name) {
231           for (Configuration config : _cfgList) {
232               if (config.getName().equals(name)) {
233                  return config;
234               }
235           }
236           return null;
237      }
238
239
240     /** list of the configurations of the ArchiResource */
241     protected Vector<Configuration> _cfgList = null;
242
243     /** name of the architectural resource */
244     protected String _name = null;
245
246     /** basename of the architectural resource, if no basename, store the name */
247     protected String _basename = null;
248
249     /** list of the nodes, paths of the architectural resource */
250     protected Vector<Node> _nodeList = null;
251
252     /** parent resource of this architectural resource */
253     protected ArchiResource _parentResource = null;
254 }