Package ptolemy.graph

Examples of ptolemy.graph.DirectedAcyclicGraph


    protected Schedule _getSchedule() {
        // FIXME: should check whether graph is connected
        // FIXME: should check whether multiple output ports are
        //        connected to the same broadcast relation.
        // Clear the graph
        DirectedAcyclicGraph dag = new DirectedAcyclicGraph();

        GRDirector director = (GRDirector) getContainer();

        if (director == null) {
            return null;
        }

        // If there is no container, there are no actors
        CompositeActor container = (CompositeActor) (director.getContainer());

        if (container == null) {
            return null;
        }

        CompositeActor castContainer = container;

        // First, include all actors as nodes in the graph.
        // get all the contained actors.
        List entities = castContainer.deepEntityList();
        Iterator actors = entities.iterator();
        int actorCount = entities.size();

        while (actors.hasNext()) {
            Actor actor = (Actor) actors.next();
            dag.addNodeWeight(actor);
        }

        actors = castContainer.deepEntityList().iterator();

        while (actors.hasNext()) {
            Actor actor = (Actor) actors.next();

            // Find the successors of the actor
            Set successors = new HashSet();
            Iterator outports = actor.outputPortList().iterator();

            while (outports.hasNext()) {
                IOPort outPort = (IOPort) outports.next();
                Iterator sinkPorts = outPort.sinkPortList().iterator();

                while (sinkPorts.hasNext()) {
                    IOPort sinkPort = (IOPort) sinkPorts.next();

                    if (sinkPort.isOutput()) {
                        // Skip this port, since its part of the container
                        continue;
                    }

                    Actor sinkActor = (Actor) sinkPort.getContainer();
                    successors.add(sinkActor);
                }
            }

            // Add the edge in the DAG
            Iterator succeedingActors = successors.iterator();

            while (succeedingActors.hasNext()) {
                Actor connectedActor = (Actor) succeedingActors.next();
                dag.addEdge(actor, connectedActor);
            }
        }

        // NOTE: The following may be a very costly test, which is why
        // it is done at the end.  However, this means that we cannot
        // report an actor in the directed cycle.  Probably DirectedGraph
        // should be modified to enable such reporting.
        if (!dag.isAcyclic()) {
            Object[] cycleNodes = dag.cycleNodes();
            StringBuffer names = new StringBuffer();

            for (int i = 0; i < cycleNodes.length; i++) {
                if (cycleNodes[i] instanceof Nameable) {
                    if (i > 0) {
                        names.append(", ");
                    }

                    names.append(((Nameable) cycleNodes[i]).getFullName());
                }
            }

            throw new NotSchedulableException(this, "GR graph is not acyclic: "
                    + names.toString());
        }

        if (dag.top() == null) {
            // FIXME: throw exception here
        }

        Schedule schedule = new Schedule();
        Object[] sorted = dag.topologicalSort();

        for (int counter = 0; counter < actorCount; counter++) {
            Firing firing = new Firing();
            firing.setActor((Actor) sorted[counter]);
            schedule.add(firing);
View Full Code Here


                    + " The results may contain unknowns.  This optimized "
                    + "scheduler does not handle this model. Try the "
                    + "randomized scheduler instead.");
        }

        DirectedAcyclicGraph dependencyGraph = functionDependency
                .getDetailedDependencyGraph().toDirectedAcyclicGraph();

        if (_debugging) {
            _debug("## dependency graph is:" + dependencyGraph.toString());
        }

        Object[] sort = dependencyGraph.topologicalSort();

        if (_debugging) {
            _debug("## Result of topological sort (highest depth to lowest):");
        }
View Full Code Here

    /** Perform a topological sort on the directed graph and use the result
     *  to set the depth for each IO port. A new Hashtable is created each
     *  time this method is called.
     */
    private void _computePortDepth() throws IllegalActionException {
        DirectedAcyclicGraph portsGraph = _constructDirectedGraph();
        _verbose = true;
        if (_debugging && _verbose) {
            _debug("## ports graph is:" + portsGraph.toString());
        }

        // NOTE: this topologicalSort can be smarter.
        // In particular, the dependency between ports belonging
        // to the same actor may be considered.
        Object[] sort = portsGraph.topologicalSort();
        int numberOfPorts = sort.length;

        if (_debugging && _verbose) {
            _debug("## Result of topological sort (highest depth to lowest):");
        }
View Full Code Here

    // directed edges representing their dependencies. The directed graph
    // is returned.
    private DirectedAcyclicGraph _constructDirectedGraph()
            throws IllegalActionException {
        // Clear the graph
        DirectedAcyclicGraph portsGraph = new DirectedAcyclicGraph();

        Nameable container = getContainer();

        // If the container is not composite actor, there are no actors.
        if (!(container instanceof CompositeActor)) {
View Full Code Here

        // In the following, we first try to resolve the signal types of
        // the ports of the rest actors including nonCTSubsystems
        // by propagating known signal types.
        // First make sure that there is no causality loop of arithmetic
        // actors. This makes the graph reachability algorithms terminate.
        DirectedAcyclicGraph arithmeticGraph = _toGraph(arithmeticActors);

        if (!arithmeticGraph.isAcyclic()) {
            Object[] cycleNodes = arithmeticGraph.cycleNodes();
            LinkedList nodesAsList = new LinkedList();
            StringBuffer inCycle = new StringBuffer("Cycle includes: ");
            for (int i = 0; i < cycleNodes.length; i++) {
                inCycle.append(((NamedObj) cycleNodes[i]).getFullName());
                if (i < cycleNodes.length - 1) {
                    inCycle.append(", ");
                }
                nodesAsList.add(cycleNodes[i]);
            }
            throw new NotSchedulableException(nodesAsList, null,
                    "Algebraic loop. " + inCycle.toString());
        }

        // We do not allow loops of dynamic actors, either.
        DirectedAcyclicGraph dynamicGraph = _toGraph(dynamicActors);

        // FIXME: Why is this disallowed? If we change this, change the class
        // comment (at the end) also.
        if (!dynamicGraph.isAcyclic()) {
            throw new NotSchedulableException(
                    "Loops of dynamic actors (e.g. integrators) "
                            + "are not allowed in the CT domain. You may insert a "
                            + "Scale actor with factor 1.");
        }

        // Now we propagate the signal types by topological sort.
        // Notice that signal types of the output ports of the dynamic actors,
        // source actors, waveform generators, event generators, and sequence
        // actors have already been propagated by one step.
        // So, we start with arithmetic actors.
        Object[] sortedArithmeticActors = arithmeticGraph.topologicalSort();

        for (int i = 0; i < sortedArithmeticActors.length; i++) {
            Actor actor = (Actor) sortedArithmeticActors[i];

            // Note that the signal type of the input ports should be set
            // already. If all input ports are CONTINUOUS, and the
            // output ports are UNKNOWN, then all output ports should be
            // CONTINUOUS. If all input ports are DISCRETE, and the
            // output ports are UNKNOWN, then all output ports should be
            // DISCRETE. If some input ports are continuous and some
            // input ports are discrete, then the output port type must
            // be set manually, which means they can not been resolved.
            Iterator inputPorts = actor.inputPortList().iterator();
            CTReceiver.SignalType knownInputType = UNKNOWN;
            boolean needManuallySetType = true;

            while (inputPorts.hasNext()) {
                IOPort inputPort = (IOPort) inputPorts.next();

                if (inputPort.getWidth() != 0) {
                    CTReceiver.SignalType inputType = _signalTypeMap
                            .getType(inputPort);

                    if (inputType == UNKNOWN) {
                        throw new NotSchedulableException("Cannot resolve "
                                + "signal type for port "
                                + inputPort.getFullName()
                                + ". If you are certain about the signal type"
                                + ", you can set them manually.\n"
                                + " To do this, you can add a parameter "
                                + "called \'signalType\' with value "
                                + "\'\"CONTINUOUS\"\' or \'\"DISCRETE\"\'"
                                + " to a port.");
                    } else if (knownInputType == UNKNOWN) {
                        knownInputType = inputType;
                        needManuallySetType = false;
                    } else if (knownInputType != inputType) {
                        needManuallySetType = true;
                        break;
                    }
                }
            }

            Iterator outputPorts = actor.outputPortList().iterator();

            while (outputPorts.hasNext()) {
                IOPort outputPort = (IOPort) outputPorts.next();

                if (outputPort.getWidth() != 0) {
                    CTReceiver.SignalType outputType = _signalTypeMap
                            .getType(outputPort);

                    if (outputType == UNKNOWN) {
                        if (needManuallySetType) {
                            throw new NotSchedulableException(
                                    "Cannot resolve "
                                            + "signal type for port "
                                            + outputPort.getFullName()
                                            + ".\n To set the signal type manually, "
                                            + "add a parameter with name \'signalType\'"
                                            + " and a string value \'\"CONTINUOUS\"\' "
                                            + "or \'\"DISCRETE\"\'.");
                        } else {
                            _signalTypeMap.setType(outputPort, knownInputType);
                        }
                    }

                    _signalTypeMap.propagateType(outputPort);
                }
            }
        }

        // Set the "signalType" parameters in the model
        // to display the signal types.
        _setPortSignalTypes(_signalTypeMap);

        // Output the signal type resolution result to the debugger.
        if (_debugging) {
            _debug("Resolved signal types: {\n" + _signalTypeMap.toString()
                    + "}");
        }

        // Now the signal types of all ports are resolved and stored in the
        // SignalTypes table. We classify continuous and discrete actors.
        // NOTE: An actor is continuous if it has continuous ports;
        // an actor is discrete if it has discrete ports. Under this
        // rule, the set of continuous actors and discrete actors may have
        // a non-empty intersection set.
        discreteActors = _signalTypeMap.getDiscreteActors();
        continuousActors = _signalTypeMap.getContinuousActors();

        // NOTE: There is a situation that the signal types of all the input
        // and output ports of a CT composite actor are derived as "DISCRETE".
        // In such case, we still need to include this actor into the set of
        // continuous actors, because there may be some continuous actors
        // hidden inside that CT composite actor.
        // To avoid introducing duplication:
        continuousActors.removeAll(ctSubsystems);
        continuousActors.addAll(ctSubsystems);

        // NOTE: There is a situation that the signal types of all the input
        // and output ports of a CT composite actor are derived as "CONTINUOUS".
        // In such case, we still need to include this actor into the set of
        // discrete actors, because there may be some discrete actors
        // hidden inside that CT composite actor.
        // To avoid introducing duplication:
        discreteActors.removeAll(ctSubsystems);
        discreteActors.addAll(ctSubsystems);

        // FIXME: the following statement does not make sense.
        // At this point, since the ports of all actors have their signal types
        // resolved, a nonCTSubsystem will be clarified based on
        // the signal types of its input and output ports.
        Iterator subsystems = nonCTSubsystems.iterator();

        while (subsystems.hasNext()) {
            CompositeActor subsystem = (CompositeActor) subsystems.next();

            if (discreteActors.contains(subsystem)
                    && continuousActors.contains(subsystem)) {
                // NOTE:
                // For a non-CT composite actor, it can not be a
                // waveform generator or an event generator.
                // Because the transformation of different type of signals
                // can only be made in CT models.
                waveformGenerators.add(subsystem);

                // remove the current subsystem from both the continuous
                // and discrete actor clusters.
                discreteActors.remove(subsystem);
                continuousActors.remove(subsystem);
            }
        }

        // Now, all actors are classified.
        // Notice that by now, we have all the discrete actors, but
        // they are not in the topological order. Sort them and
        // create the discrete schedule.
        DirectedAcyclicGraph discreteGraph = _toGraph(discreteActors);
        Object[] discreteSorted = discreteGraph.topologicalSort();

        for (int i = 0; i < discreteSorted.length; i++) {
            Actor actor = (Actor) discreteSorted[i];

            // We want to distinguish the waveform and event generators,
            // which have at least one input or output declared as CONTINUOUS
            // signal type, from purely discrete (continuous) actors, whose
            // ports are all DISCRETE (CONTINUOUS).
            if (continuousActors.contains(actor)) {
                if (actor instanceof CTCompositeActor) {
                    // We add CT composite actors into the list of discrete
                    // actors because a CTComposite actor can be anything.
                    discreteActorSchedule.add(new Firing(actor));
                } else {
                    // the following code removes event generators
                    // and waveform generators from the list of purely
                    // continuous actors.
                    // NOTE: we only remove actors that are declared to be
                    // event generators or waveform generators. Some actors,
                    // such as TriggeredContinuousClock actor, are still treated
                    // as purely continuous actors even though they have
                    // discrete inputs, because they produce continuous signals.
                    // TESTIT: Clock3, Clock5, and Clock6 in ct/lib/test/auto.
                    if ((actor instanceof CTEventGenerator)
                            || (actor instanceof CTWaveformGenerator)) {
                        continuousActors.remove(actor);
                    }
                }

                continue;
            }

            // We add purely discrete actors (discrete -> discrete) into list.
            discreteActorSchedule.add(new Firing(actor));
        }

        // Create the schedule for waveform generators.
        Iterator generators = waveformGenerators.iterator();

        while (generators.hasNext()) {
            Actor generator = (Actor) generators.next();
            waveformGeneratorSchedule.add(new Firing(generator));
        }

        // Schedule event generators so that they are executed topologically.
        // Treat them as sink actors from the point of view of a
        // continuous phase of execution.
        if (!eventGenerators.isEmpty()) {
            DirectedAcyclicGraph eventGraph = _toGraph(eventGenerators);
            Object[] eventSorted = eventGraph.topologicalSort();

            for (int i = 0; i < eventSorted.length; i++) {
                Actor actor = (Actor) eventSorted[i];

                // If this actor is both an event generator and
View Full Code Here

     *  in this method, so the caller should check.
     *  @param list The list of actors to be scheduled.
     *  @return A graph representation of the actors.
     */
    private DirectedAcyclicGraph _toArithmeticGraph(List list) {
        DirectedAcyclicGraph graph = new DirectedAcyclicGraph();

        // Create the nodes.
        Iterator actors = list.iterator();

        while (actors.hasNext()) {
            Actor actor = (Actor) actors.next();
            graph.addNodeWeight(actor);
        }

        // Create the edges.
        actors = list.iterator();

        while (actors.hasNext()) {
            Actor actor = (Actor) actors.next();

            // CTCompositeActor is basically everything,
            // it may be an event generator, or a state transition
            // actor.
            if ((actor instanceof CTCompositeActor)
                    || (!(actor instanceof CTDynamicActor) && !(actor instanceof CTEventGenerator))) {
                // Find the successors of the actor
                Iterator successors = successorList(actor).iterator();

                while (successors.hasNext()) {
                    Actor successor = (Actor) successors.next();

                    if (list.contains(successor)) {
                        graph.addEdge(actor, successor);
                    }
                }
            }
        }

View Full Code Here

     *  corresponding nodes.
     *  @param list The list of actors to be converted to a graph.
     *  @return A graph representation of the actors.
     */
    private DirectedAcyclicGraph _toGraph(List list) {
        DirectedAcyclicGraph g = new DirectedAcyclicGraph();

        // Create the nodes.
        Iterator actors = list.iterator();

        while (actors.hasNext()) {
            Actor a = (Actor) actors.next();
            g.addNodeWeight(a);
        }

        // Create the edges.
        actors = list.iterator();

        while (actors.hasNext()) {
            Actor a = (Actor) actors.next();

            // Find the successors of a
            Iterator successors = successorList(a).iterator();

            while (successors.hasNext()) {
                Actor s = (Actor) successors.next();

                if (list.contains(s)) {
                    g.addEdge(a, s);
                }
            }
        }

        return g;
View Full Code Here

        ///////////////////////////////////////////////////////////////
        ////                    private constructor                ////
        // the constructor is private so only the outer class can use it.
        private TheTypeLattice() {
            synchronized (TypeLattice.class) {
                _basicLattice = new DirectedAcyclicGraph();

                StructuredType arrayRep = (new ArrayType(BaseType.UNKNOWN))
                        ._getRepresentative();

                String[] labels = new String[0];
View Full Code Here

                            + names.toString() + "\n"
                            + " The results may contain unknowns.  This "
                            + "scheduler cannot handle this model.");
        }

        DirectedAcyclicGraph dependencyGraph = functionDependency
                .getDetailedDependencyGraph().toDirectedAcyclicGraph();

        if (_debugging) {
            _debug("## dependency graph is:" + dependencyGraph.toString());
        }

        boolean useOptimizedSchedule = false;
        if (useOptimizedSchedule) {
            return _constructSchedule(dependencyGraph);
View Full Code Here

TOP

Related Classes of ptolemy.graph.DirectedAcyclicGraph

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.