dol: initial dol commit
[jump.git] / dol / src / dol / datamodel / pn / ProcessNetwork.java
1 /* $Id: ProcessNetwork.java 1 2010-02-24 13:03:05Z haidw $ */
2 package dol.datamodel.pn;
3
4 import java.util.Vector;
5
6 import dol.visitor.PNVisitor;
7
8
9 /**
10  * This class defines a process network.
11  */
12 public class ProcessNetwork extends Resource {
13
14     /**
15      *  Constructor to create a process network with a name,
16      *  empty process list and empty channel list.
17      */
18     public ProcessNetwork(String name) {
19         super(name);
20         _processList = new Vector<Process>();
21         _channelList = new Vector<Channel>();
22         _varList = new Vector<Variable>();
23         _connectionList = new Vector<Connection>();
24     }
25
26     /**
27      * Accept a visitor
28      *
29      * @param x visitor object
30      */
31     public void accept(PNVisitor x) {
32         x.visitComponent(this);
33     }
34
35     /**
36      * Clone this ProcessNetwork.
37      *
38      * @return new instance of the ProcessNetwork
39      */
40     @SuppressWarnings("unchecked")
41     public Object clone() {
42         ProcessNetwork newObj = (ProcessNetwork) super.clone();
43         newObj.setProcessList((Vector<Process>)_processList.clone());
44         newObj.setChannelList((Vector<Channel>)_channelList.clone());
45         newObj.setVarList((Vector<Variable>)_varList.clone());
46         newObj.setConnectionList((Vector<Connection>)_connectionList.clone());
47         return (newObj);
48     }
49
50     /**
51      * Get the process list of a ProcessNetwork.
52      *
53      * @return  the process list
54      */
55     public Vector<Process> getProcessList() {
56         return _processList;
57     }
58
59     /**
60      * Get the process basenames.
61      * 
62      * @return process basenames
63      */
64     public Vector<String> getProcessBasenames() {
65         Vector<String> basenames = new Vector<String>();
66         for (Process p : _processList) {
67             String basename = p.getBasename();
68             if (!basenames.contains(basename)) {
69                 basenames.add(basename);
70             }
71         }
72         return basenames;
73     }
74     
75     /**
76      * Set the process list of a ProcessNetwork.
77      *
78      * @param  processList The new list
79      */
80     public void setProcessList(Vector<Process> processList) {
81         _processList = processList;
82     }
83
84     /**
85      * Get the channel list of a ProcessNetwork
86      *
87      * @return  the channel list
88      */
89     public Vector<Channel> getChannelList() {
90         return _channelList;
91     }
92
93     /**
94      * Set the channel list of a ProcessNetwork
95      *
96      * @param  channelList The new list
97      */
98     public void setChannelList(Vector<Channel> channelList) {
99         _channelList = channelList;
100     }
101
102     public Vector<Variable> getVarList() { return _varList; }
103     public void setVarList(Vector<Variable> list) { _varList = list; }
104
105     public void setConnectionList(Vector<Connection> list) { _connectionList = list; }
106     public Vector<Connection> getConnectionList() { return _connectionList; }
107
108
109     /**
110      * Return a description of the ProcessNetwork.
111      *
112      * @return  a description of the ProcessNetwork.
113      */
114     public String toString() {
115         return "ProcessNetwork: " + getName();
116     }
117
118     /**
119      * Return a process which has a specific name. Return null if
120      * process cannot be found.
121      *
122      * @param  name the name of the process to search for.
123      * @return  the process with the specific name.
124      */
125     public Process getProcess(String name) {
126         for (Process process : _processList) {
127             if (process.getName().equals(name)) {
128                 return process;
129             }
130         }
131         return null;
132     }
133
134     /**
135      * Return a channel which has a specific name. Return null if
136      * not found.
137      *
138      * @param  name the name of the channel to search for.
139      * @return  the channel with the specific name.
140      */
141     public Channel getChannel(String name) {
142         for (Channel c : _channelList) {
143             if (c.getName().equals(name)) {
144                 return c;
145             }
146         }
147         return null;
148     }
149
150     /**
151      * In this version, we only deal with the flattened process network.
152      * Connection infomation is filled to each process and channel for fast
153      * iteration.
154      */
155     public void fillPortPeerInfo() throws Exception {
156         for (Connection c : getConnectionList()) {
157             Port originPort = c.getOriginPort();
158             Port targetPort = c.getTargetPort();
159             originPort.setPeerResource(c.getTarget());
160             originPort.setPeerPort(targetPort);
161             targetPort.setPeerResource(c.getOrigin());
162             targetPort.setPeerPort(originPort);
163         }
164
165         //for each sw channel, determine which processes it connects
166         for (Connection c : getConnectionList()) {
167             if (c.getOrigin() instanceof Process) {
168                 Channel channel = (Channel)c.getTarget();
169                 channel.setOrigin((Process)c.getOrigin());
170             }
171             else if (c.getOrigin() instanceof Channel) {
172                 Channel channel = (Channel)c.getOrigin();
173                 channel.setTarget((Process)c.getTarget());
174             }
175         }
176     }
177
178     /** list of the processes in the ProcessNetwork. */
179     protected Vector<Process> _processList = null;
180
181     /** list of the channels in the ProcessNetwork. */
182     protected Vector<Channel> _channelList = null;
183
184     /** list of variables in the ProcessNetwork */
185     protected Vector<Variable> _varList = null;
186
187     /** list of connections in the ProcessNetwork */
188     protected Vector<Connection> _connectionList = null;
189 }