OpenASIP  2.0
MachineEditPartFactory.cc
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2009 Tampere University.
3 
4  This file is part of TTA-Based Codesign Environment (TCE).
5 
6  Permission is hereby granted, free of charge, to any person obtaining a
7  copy of this software and associated documentation files (the "Software"),
8  to deal in the Software without restriction, including without limitation
9  the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  and/or sell copies of the Software, and to permit persons to whom the
11  Software is furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in
14  all copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  DEALINGS IN THE SOFTWARE.
23  */
24 /**
25  * @file MachineEditPartFactory.cc
26  *
27  * Definition of MachineEditPartFactory class.
28  *
29  * @author Ari Metsähalme 2003 (ari.metsahalme-no.spam-tut.fi)
30  * @note rating: yellow
31  * @note reviewed Jul 13 2004 by vpj, ll, jn, am
32  */
33 
34 #include <set>
35 
36 #include "Application.hh"
37 #include "ContainerTools.hh"
39 #include "Machine.hh"
40 #include "FunctionUnit.hh"
41 #include "ControlUnit.hh"
42 #include "RegisterFile.hh"
43 #include "ImmediateUnit.hh"
44 #include "Port.hh"
45 #include "Socket.hh"
46 #include "Bus.hh"
47 #include "Bridge.hh"
48 #include "FUFactory.hh"
49 #include "RFFactory.hh"
50 #include "IUFactory.hh"
51 #include "SocketFactory.hh"
52 #include "BusFactory.hh"
53 #include "GCUFactory.hh"
54 #include "BridgeFactory.hh"
55 #include "EditPart.hh"
56 #include "ContentsFigure.hh"
57 #include "BidirBridgeFigure.hh"
58 #include "UnitContainerFigure.hh"
59 #include "BusContainerFigure.hh"
60 #include "SocketContainerFigure.hh"
61 #include "BusChainFigure.hh"
62 #include "ConnectionEditPart.hh"
63 
64 #include "EditPolicyFactory.hh"
65 
66 using std::vector;
67 using std::set;
68 using namespace TTAMachine;
69 
70 /**
71  * The Constructor.
72  */
74  EditPolicyFactory& editPolicyFactory) :
75  EditPartFactory(editPolicyFactory) {
76 
77  registerFactory(new GCUFactory(editPolicyFactory));
78  registerFactory(new FUFactory(editPolicyFactory));
79  registerFactory(new IUFactory(editPolicyFactory));
80  registerFactory(new RFFactory(editPolicyFactory));
81  registerFactory(new BridgeFactory(editPolicyFactory));
82  registerFactory(new SocketFactory(editPolicyFactory));
83  registerFactory(new BusFactory(editPolicyFactory));
84 }
85 
86 
87 /**
88  * The Destructor.
89  */
91 }
92 
93 /**
94  * Delegates to registered factories to create the EditPart.
95  *
96  * @param component Component of which to create the corresponding
97  * EditPart.
98  * @return NULL if none of the factories can create the EditPart.
99  */
100 EditPart*
102 
103  EditPart* ep = EditPartFactory::checkCache(component);
104 
105  if (ep != NULL) {
106  return ep;
107  }
108 
109  vector<Factory*>::const_iterator i = factories_.begin();
110 
111  for (; i != factories_.end(); i++) {
112  ep = (*i)->createEditPart(component);
113  if (ep != NULL) {
115  return ep;
116  }
117  }
118 
119  return NULL;
120 }
121 
122 /**
123  * Overloaded implementation for creating the contents EditPart from the
124  * machine.
125  *
126  * @param machine Machine to create the EditPart from.
127  * @return The created contents EditPart corresponding to the machine.
128  */
129 EditPart*
131 
132  EditPart* contents = new EditPart();
133  contents->setFigure(new ContentsFigure());
134 
135  contents->addChild(getUnits(machine));
136  contents->addChild(getSockets(machine));
137  contents->addChild(getBusChains(machine));
138 
139  return contents;
140 }
141 
142 /**
143  * Navigates through the machine and creates a unit container EditPart.
144  *
145  * Returns a NULL pointer if there are no units in the given machine.
146  *
147  * @param machine Machine to navigate through.
148  * @return An EditPart containing EditParts for all the units in the
149  * given machine or NULL if there aren't any.
150  */
151 EditPart*
153 
157 
158  ControlUnit* gcu = machine->controlUnit();
159  EditPart* units = NULL;
160 
161  if (fuNav.count() > 0 || rfNav.count() > 0 || iuNav.count() > 0 ||
162  gcu != NULL) {
163 
164  units = new EditPart();
165  units->setFigure(new UnitContainerFigure());
166 
167  for (int i = 0; i < fuNav.count(); i++) {
168  FunctionUnit* fu = fuNav.item(i);
169  EditPart* ep = createEditPart(fu);
170  assert(ep != NULL);
171  units->addChild(ep);
172  }
173 
174  for (int i = 0; i < rfNav.count(); i++) {
175  RegisterFile* rf = rfNav.item(i);
176  EditPart* ep = createEditPart(rf);
177  assert(ep != NULL);
178  units->addChild(ep);
179  }
180 
181  for (int i = 0; i < iuNav.count(); i++) {
182  ImmediateUnit* iu = iuNav.item(i);
183  EditPart* ep = createEditPart(iu);
184  assert(ep != NULL);
185  units->addChild(ep);
186  }
187 
188  if (gcu != NULL) {
189  EditPart* ep = createEditPart(gcu);
190  assert(ep != NULL);
191  units->addChild(ep);
192  }
193  }
194 
195  return units;
196 }
197 
198 /**
199  * Navigates through the machine and creates a socket container EditPart.
200  *
201  * Returns a NULL pointer if there are no sockets in the given machine.
202  *
203  * @param machine Machine to navigate through.
204  * @return An EditPart containing EditParts for all the sockets in the
205  * given machine or NULL if there aren't any.
206  */
207 EditPart*
209 
211  EditPart* sockets = NULL;
212 
213  if (socketNav.count() > 0) {
214 
215  sockets = new EditPart();
216  sockets->setFigure(new SocketContainerFigure());
217 
218  for (int i = 0; i < socketNav.count(); i++) {
219  Socket* socket = socketNav.item(i);
220  EditPart* ep = createEditPart(socket);
221  assert(ep != NULL);
222  sockets->addChild(ep);
223  }
224  }
225 
226  return sockets;
227 }
228 
229 /**
230  * Navigates through the machine and creates a bus container EditPart.
231  *
232  * Returns a NULL pointer if there are no buses in the given machine.
233  *
234  * @param machine Machine to navigate through.
235  * @return An EditPart containing EditParts for all the buses and bus
236  * chains in the given machine or NULL if there aren't any.
237  */
238 EditPart*
240 
242 
243  EditPart* busContainer = NULL;
244 
245  if (busNav.count() > 0) {
246 
247  set<Bus*> sorted;
248  busContainer = new EditPart();
249  busContainer->setFigure(new BusContainerFigure());
250  vector<Bus*> buses;
251 
252  for (int i = 0; i < busNav.count(); i++) {
253  buses.push_back(busNav.item(i));
254  }
255 
256  // get bridges
257 
259  vector<Bridge*> bridges;
260 
261  for (int i = 0; i < bridgeNav.count(); i++) {
262  bridges.push_back(bridgeNav.item(i));
263  }
264 
265  // sort the bus chains
266 
267  for (unsigned int i = 0; i < buses.size(); i++) {
268 
269  if (!ContainerTools::containsValue(sorted, buses[i])) {
270 
271  sorted.insert(buses[i]);
272  Bus* first = buses[i];
273 
274  // browse to the beginning of the chain
275  while (first->hasPreviousBus()) {
276  first = first->previousBus();
277  }
278 
279  // start adding buses to the chain
280 
281  if (first->hasNextBus()) {
282 
283  EditPart* busChain = new EditPart();
284  busChain->setFigure(new BusChainFigure());
285 
286  while (true) {
287  sorted.insert(first);
288 
289  // find also the bridge(s) in between
290  ConnectionEditPart* bridgeEditPart = NULL;
291  ConnectionEditPart* biDirBridge = NULL;
292 
293  if (first->hasNextBus()) {
295  bridgeEditPart, biDirBridge, bridges, first);
296  }
297 
298  // add the bus and the following bridge to the chain
299 
300  EditPart* nextEditPart = createEditPart(first);
301  assert(nextEditPart != NULL);
302  busChain->addChild(nextEditPart);
303 
304  if (biDirBridge != NULL) {
305  busChain->addChild(biDirBridge);
306  } else if (bridgeEditPart != NULL) {
307  busChain->addChild(bridgeEditPart);
308  }
309 
310  if (first->hasNextBus()) {
311  first = first->nextBus();
312  } else {
313  break;
314  }
315  }
316 
317  busContainer->addChild(busChain);
318 
319  } else {
320  // add a single bus
321  EditPart* busEditPart = createEditPart(buses[i]);
322  assert(busEditPart != NULL);
323  busContainer->addChild(busEditPart);
324  }
325  }
326  }
327  }
328 
329  return busContainer;
330 }
331 
332 /**
333  * Creates an EditPart for a bidirectional bridge.
334  *
335  * @param bridge EditPart representing one of the bridges.
336  * @param opposite The opposite bridge.
337  * @return EditPart for the bidirectional bridge.
338  */
341  ConnectionEditPart* bridge,
342  Bridge* opposite) {
343 
344  ConnectionEditPart* biDirBridge = new ConnectionEditPart();
345  biDirBridge->setSelectable(true);
348  biDirBridge->setFigure(fig);
349  EditPart* oppositeBridge = createEditPart(opposite);
350  bridge->setSelectable(false);
351  oppositeBridge->setSelectable(false);
352 
353  biDirBridge->setModel(bridge->model());
354  biDirBridge->setSource(bridge->source());
355  biDirBridge->setTarget(bridge->target());
356 
357  biDirBridge->installEditPolicy(
359 
360  biDirBridge->addChild(bridge);
361  biDirBridge->addChild(oppositeBridge);
362 
363  return biDirBridge;
364 }
365 
366 /**
367  * Finds and returns the bridge or bridges that are connected to a bus.
368  *
369  * @param bridgeEditPart Where to store one bridge, no change if no
370  bridge found.
371  * @param biDirBridge Where to store the other bridge, no change if no other
372  * bridge found.
373  * @param bridges Which bridges to search.
374  * @param bus Bus to which the searched bridges should be connected.
375  */
376 void
378  ConnectionEditPart*& bridgeEditPart,
379  ConnectionEditPart*& biDirBridge,
380  vector<Bridge*>& bridges,
381  Bus* bus) {
382 
383  unsigned int j = 0;
384 
385  while (j < bridges.size()) {
386  if (connected(bridges[j], bus, bus->nextBus())) {
387  bridgeEditPart = dynamic_cast<ConnectionEditPart*>(
388  createEditPart(bridges[j]));
389  break;
390  }
391  j++;
392  }
393 
394  // check if the bridge is bidirectional
395  j++;
396  while (j < bridges.size()) {
397  if (connected(bridges[j], bus, bus->nextBus())) {
398  biDirBridge =
399  createBiDirBridge(bridgeEditPart, bridges[j]);
400  break;
401  }
402  j++;
403  }
404 }
405 
406 /**
407  * Tells whether a bridge is connected to two specific buses.
408  *
409  * @param bridge The bridge.
410  * @param bus1 One bus.
411  * @param bus2 The other bus.
412  * @return True if the bridge is connected to the buses, false otherwise.
413  */
414 bool
416  const Bridge* bridge,
417  const Bus* bus1,
418  const Bus* bus2) {
419 
420  return (bridge->sourceBus() == bus1 &&
421  bridge->destinationBus() == bus2) ||
422  (bridge->destinationBus() == bus1 &&
423  bridge->sourceBus() == bus2);
424 }
ConnectionEditPart::setSource
void setSource(EditPart *source)
Definition: ConnectionEditPart.cc:56
BridgeFigure::setDirection
void setDirection(BridgeFigure::Direction direction)
EditPolicyFactory
Definition: EditPolicyFactory.hh:46
TTAMachine::Bridge::sourceBus
Bus * sourceBus() const
TTAMachine::Bridge::destinationBus
Bus * destinationBus() const
FUFactory
Definition: FUFactory.hh:45
IUFactory
Definition: IUFactory.hh:45
MachineEditPartFactory::findConnectedBridges
void findConnectedBridges(ConnectionEditPart *&bridgeEditPart, ConnectionEditPart *&biDirBridge, std::vector< TTAMachine::Bridge * > &bridges, TTAMachine::Bus *bus)
Definition: MachineEditPartFactory.cc:377
machine
TTAMachine::Machine * machine
the architecture definition of the estimated processor
Definition: EstimatorCmdLineUI.cc:59
ContentsFigure.hh
GCUFactory
Definition: GCUFactory.hh:45
BridgeFactory
Definition: BridgeFactory.hh:45
TTAMachine::Bridge
Definition: Bridge.hh:51
EditPart::setSelectable
void setSelectable(bool selectable)
MachineEditPartFactory::createEditPart
virtual EditPart * createEditPart(TTAMachine::MachinePart *component)
Definition: MachineEditPartFactory.cc:101
TTAMachine::Bus
Definition: Bus.hh:53
EditPartFactory::checkCache
EditPart * checkCache(const TTAMachine::MachinePart *component) const
Definition: EditPartFactory.cc:72
EditPart::installEditPolicy
void installEditPolicy(EditPolicy *editpolicy)
Definition: EditPart.cc:247
BusContainerFigure
Definition: BusContainerFigure.hh:47
BusChainFigure.hh
SocketContainerFigure
Definition: SocketContainerFigure.hh:46
ImmediateUnit.hh
RFFactory.hh
ConnectionEditPart::target
EditPart * target() const
EditPolicyFactory::createBridgeEditPolicy
virtual EditPolicy * createBridgeEditPolicy()
BridgeFigure::DIR_BIDIR
@ DIR_BIDIR
Definition: BridgeFigure.hh:49
BusContainerFigure.hh
TTAMachine::Machine::Navigator::count
int count() const
BusChainFigure
Definition: BusChainFigure.hh:47
UnitContainerFigure.hh
TTAMachine::Bus::hasPreviousBus
virtual bool hasPreviousBus() const
Definition: Bus.cc:473
Socket.hh
MachineEditPartFactory::~MachineEditPartFactory
virtual ~MachineEditPartFactory()
Definition: MachineEditPartFactory.cc:90
ConnectionEditPart::source
EditPart * source() const
ContentsFigure
Definition: ContentsFigure.hh:49
EditPartFactory::registerFactory
void registerFactory(Factory *factory)
assert
#define assert(condition)
Definition: Application.hh:86
TTAMachine::FunctionUnit
Definition: FunctionUnit.hh:55
EditPartFactory::writeToCache
void writeToCache(EditPart *editPart)
Definition: EditPartFactory.cc:89
Port.hh
SocketFactory
Definition: SocketFactory.hh:47
TTAMachine::Machine::controlUnit
virtual ControlUnit * controlUnit() const
Definition: Machine.cc:345
EditPolicyFactory.hh
ConnectionEditPart.hh
TTAMachine::Machine::bridgeNavigator
virtual BridgeNavigator bridgeNavigator() const
Definition: Machine.cc:404
IUFactory.hh
EditPart.hh
TTAMachine::Machine::immediateUnitNavigator
virtual ImmediateUnitNavigator immediateUnitNavigator() const
Definition: Machine.cc:416
BridgeFactory.hh
TTAMachine::ControlUnit
Definition: ControlUnit.hh:50
MachineEditPartFactory::MachineEditPartFactory
MachineEditPartFactory(EditPolicyFactory &editPolicyFactory)
Definition: MachineEditPartFactory.cc:73
Application.hh
BidirBridgeFigure
Definition: BidirBridgeFigure.hh:46
EditPartFactory
Definition: EditPartFactory.hh:48
UnitContainerFigure
Definition: UnitContainerFigure.hh:46
TTAMachine::Machine::functionUnitNavigator
virtual FunctionUnitNavigator functionUnitNavigator() const
Definition: Machine.cc:380
EditPart::setModel
void setModel(TTAMachine::MachinePart *model)
BidirBridgeFigure.hh
TTAMachine::Socket
Definition: Socket.hh:53
SocketFactory.hh
TTAMachine::MachinePart
Definition: MachinePart.hh:57
FUFactory.hh
MachineEditPartFactory::createBiDirBridge
ConnectionEditPart * createBiDirBridge(ConnectionEditPart *bridge, TTAMachine::Bridge *opposite)
Definition: MachineEditPartFactory.cc:340
EditPart
Definition: EditPart.hh:60
Machine.hh
Bus.hh
TTAMachine::Machine::socketNavigator
virtual SocketNavigator socketNavigator() const
Definition: Machine.cc:368
SocketContainerFigure.hh
GCUFactory.hh
EditPart::model
TTAMachine::MachinePart * model() const
BusFactory
Definition: BusFactory.hh:45
MachineEditPartFactory::getUnits
EditPart * getUnits(TTAMachine::Machine *machine)
Definition: MachineEditPartFactory.cc:152
TTAMachine::Bus::hasNextBus
virtual bool hasNextBus() const
Definition: Bus.cc:488
TTAMachine::Machine::registerFileNavigator
virtual RegisterFileNavigator registerFileNavigator() const
Definition: Machine.cc:450
TTAMachine::Bus::previousBus
virtual Bus * previousBus() const
Definition: Bus.cc:518
RegisterFile.hh
MachineEditPartFactory.hh
RFFactory
Definition: RFFactory.hh:45
MachineEditPartFactory::getSockets
EditPart * getSockets(TTAMachine::Machine *machine)
Definition: MachineEditPartFactory.cc:208
ControlUnit.hh
TTAMachine::Machine::busNavigator
virtual BusNavigator busNavigator() const
Definition: Machine.cc:356
EditPart::setFigure
void setFigure(Figure *figure)
MachineEditPartFactory::connected
bool connected(const TTAMachine::Bridge *bridge, const TTAMachine::Bus *bus1, const TTAMachine::Bus *bus2)
Definition: MachineEditPartFactory.cc:415
ConnectionEditPart
Definition: ConnectionEditPart.hh:42
ConnectionEditPart::setTarget
void setTarget(EditPart *target)
Definition: ConnectionEditPart.cc:71
ContainerTools::containsValue
static bool containsValue(const ContainerType &aContainer, const ElementType &aKey)
TTAMachine::Machine::Navigator::item
ComponentType * item(int index) const
TTAMachine::RegisterFile
Definition: RegisterFile.hh:47
EditPart::addChild
void addChild(EditPart *child)
Definition: EditPart.cc:260
MachineEditPartFactory::getBusChains
EditPart * getBusChains(TTAMachine::Machine *machine)
Definition: MachineEditPartFactory.cc:239
TTAMachine
Definition: Assembler.hh:48
EditPartFactory::editPolicyFactory_
EditPolicyFactory & editPolicyFactory_
Factory which creates edit policies for edit parts.
Definition: EditPartFactory.hh:64
TTAMachine::Bus::nextBus
virtual Bus * nextBus() const
Definition: Bus.cc:501
TTAMachine::Machine::Navigator
Definition: Machine.hh:186
Bridge.hh
BusFactory.hh
TTAMachine::Machine
Definition: Machine.hh:73
FunctionUnit.hh
EditPartFactory::factories_
std::vector< Factory * > factories_
Registered factories.
Definition: EditPartFactory.hh:60
ContainerTools.hh
TTAMachine::ImmediateUnit
Definition: ImmediateUnit.hh:50