LinkedList ports = new LinkedList();
// Assign depths to ports based on the topological sorting result.
for (int i = 0; i <= (numberOfPorts - 1); i++) {
IOPort ioPort = (IOPort) sort[i];
ports.add(ioPort);
int depth = i;
Actor portContainer = (Actor) ioPort.getContainer();
// The ports of the composite actor that contains
// this director are set to the highest depth
// (the lowest priority).
if (ioPort.isOutput() && portContainer.equals(getContainer())) {
depth += numberOfPorts;
}
// Insert the hashtable entry.
_portToDepth.put(ioPort, Integer.valueOf(depth));
if (_debugging && _verbose) {
_debug(((Nameable) ioPort).getFullName(), "depth: " + depth);
}
}
if (_debugging && _verbose) {
_debug("## adjusting port depths to "
+ "make opaque composite actors and "
+ "actors with no output ports strict.");
}
HashSet actorsWithPortDepthsAdjusted = new HashSet();
// Adjusts the depths according to:
// - If an output depends on several inputs directly,
// all inputs must have the same depth, the biggest one.
for (int i = sort.length - 1; i >= 0; i--) {
IOPort ioPort = (IOPort) sort[i];
// Get the container actor of the current output port.
Actor portContainer = (Actor) ioPort.getContainer();
// Skip the ports of the container. Their depths are handled
// by the upper level executive director of this container.
if (portContainer.equals(getContainer())) {
continue;
}
// For opaque composite actors and for actors with
// no output ports, adjust the depths of all input ports
// to match the maximum depth of the input ports.
// If an actor has no output ports, then the default
// dependencies of all input ports to all output ports
// will not have been created. Hence, there is no mechanism
// provided by this default to elevate the depth of all
// the input ports to the maximum of those depths.
// Thus, we conservatively assume that any actor with
// no output ports should be treated as if it had one
// output port (say, representing its state), and that
// all input ports depend on that output port.
// This will not be necessary in a version of DE
// that implements a strict fixed-point semantics.
if (ioPort.isInput()) {
if (portContainer.outputPortList().size() == 0
|| (portContainer instanceof CompositeActor && ((CompositeActor) portContainer)
.isOpaque())) {
List inputPorts = portContainer.inputPortList();
if (inputPorts.size() <= 1) {
// If the sink actor has only one input port, there is
// no need to adjust its depth.
continue;
}
if (actorsWithPortDepthsAdjusted.contains(portContainer)) {
// The depths of the input ports of this acotr
// have been adjusted.
continue;
} else {
actorsWithPortDepthsAdjusted.add(portContainer);
}
Iterator inputsIterator = inputPorts.iterator();
// Iterate all input ports of the sink or composite actor
// to find the largest depth.
int maximumPortDepth = -1;
while (inputsIterator.hasNext()) {
IOPort input = (IOPort) inputsIterator.next();
int inputPortDepth = ports.indexOf(input);
if (maximumPortDepth < inputPortDepth) {
maximumPortDepth = inputPortDepth;
}
}
// Set the depths of the input ports to the maximum one.
inputsIterator = inputPorts.iterator();
while (inputsIterator.hasNext()) {
IOPort input = (IOPort) inputsIterator.next();
if (_debugging && _verbose) {
_debug(((Nameable) input).getFullName(),
"depth is adjusted to: " + maximumPortDepth);
}
// Insert the hashtable entry.
_portToDepth.put(input, Integer
.valueOf(maximumPortDepth));
}
}
}
// For an output port, adjust the depths of all the
// input ports on which it depends to match the largest
// depth of those input ports.
// FIXME: The following is really problematic. Check the
// DESchedulingTest3.xml as example (NOTE: I can't
// find this example. EAL).
if (ioPort.isOutput()) {
// Get the function dependency of the container actor
FunctionDependency functionDependency = portContainer
.getFunctionDependency();
List inputPorts = functionDependency
.getInputPortsDependentOn(ioPort);
Iterator inputsIterator = inputPorts.iterator();
// Iterate all input ports the current output depends on to
// find their maximum depth.
int maximumPortDepth = -1;
while (inputsIterator.hasNext()) {
IOPort input = (IOPort) inputsIterator.next();
int inputPortDepth = ports.indexOf(input);
if (maximumPortDepth < inputPortDepth) {
maximumPortDepth = inputPortDepth;
}
}
// Set the depths of the input ports to the maximum one.
inputsIterator = inputPorts.iterator();
while (inputsIterator.hasNext()) {
IOPort input = (IOPort) inputsIterator.next();
if (_debugging && _verbose) {
_debug(((Nameable) input).getFullName(),
"depth is adjusted to: " + maximumPortDepth);
}
// Insert the hashtable entry.