Package ptolemy.graph

Examples of ptolemy.graph.DirectedGraph


     *  base class, we simply instantiate an empty Graph and return it.
     *  @param compositeActor the Ptolemy II model that will be converted.
     *  @return the empty graph that is to hold the converted model.
     */
    protected Graph _initializeGraph(CompositeActor compositeActor) {
        return new DirectedGraph();
    }
View Full Code Here


    /** The computation associated with this analyzer.
     *
     *  @return The result of the computation.
     */
    protected Object _compute() {
        DirectedGraph graph = (DirectedGraph) graph();
        ArrayList queue = new ArrayList();

        //HashMap color = new HashMap();
        double[] distance = new double[graph.nodeCount()];
        _predecessor = new int[graph.nodeCount()];

        for (Iterator nodes = graph.nodes().iterator(); nodes.hasNext();) {
            Node node = (Node) nodes.next();

            if (node != _startNode) {
                //color.put(node, Color.white);
                distance[graph.nodeLabel(node)] = -Double.MIN_VALUE;
                _predecessor[graph.nodeLabel(node)] = -1;
            } else {
                distance[graph.nodeLabel(node)] = 0.0;
            }
        }

        queue.add(_startNode);
        _predecessor[graph.nodeLabel(_startNode)] = -1;

        while (!queue.isEmpty()) {
            Node u = (Node) queue.get(0);
            Collection successors = graph.successors(u);

            if (successors != null) {
                for (Iterator successorNodes = successors.iterator(); successorNodes
                        .hasNext();) {
                    Node v = (Node) successorNodes.next();
                    double predecessorDistance = distance[graph().nodeLabel(u)];
                    double actualDistance = distance[graph().nodeLabel(v)];
                    Collection edgeCollection = graph.predecessorEdges(v, u);
                    Iterator edges = edgeCollection.iterator();
                    double connectingEdgeCost = -Double.MAX_VALUE;

                    while (edges.hasNext()) {
                        Edge edge = (Edge) edges.next();

                        if (_edgeLengths.toDouble(edge) > connectingEdgeCost) {
                            connectingEdgeCost = _edgeLengths.toDouble(edge);
                        }
                    }

                    if ((actualDistance < (predecessorDistance + connectingEdgeCost))) {
                        distance[graph.nodeLabel(v)] = predecessorDistance
                                + connectingEdgeCost;
                        _predecessor[graph.nodeLabel(v)] = graph.nodeLabel(u);
                    }

                    if (v != _startNode) {
                        queue.add(v);
                    }
View Full Code Here

    //        }
    //    }
    // Return the length of edge with maximum/minimum length between
    // the two nodes.
    private double _getCost(Node u, Node v) {
        DirectedGraph directedCyclicGraph = (DirectedGraph) graph();
        Collection edgeCollection = directedCyclicGraph.predecessorEdges(v, u);
        Iterator edges = edgeCollection.iterator();
        double weight = -Double.MAX_VALUE;

        if (!_maximumAnalysis) {
            weight = Double.MAX_VALUE;
View Full Code Here

     */
    protected Object _compute() {
        _delayNodeList = new ArrayList();
        _maximumProfitToCostRatioCycle = new ArrayList();

        DirectedGraph originalGraph = (DirectedGraph) graph();

        // Build a new graph with the delays as nodes added to the previous
        // graph.
        DirectedGraph graphPlusDelaysAsNodes = (DirectedGraph) originalGraph
                .cloneAs(new DirectedGraph());
        Object[] edges = graphPlusDelaysAsNodes.edges().toArray();
        HashMap edgeProfitsMap = new HashMap();

        for (int j = 0; j < edges.length; j++) {
            Edge edge = (Edge) edges[j];
            Node source = edge.source();
            Node sink = edge.sink();

            //_edgeCostsMap.put(edge, _edgeCosts.toInt());
            // For all the edges that have at least one delay
            if (_edgeCosts.toInt(edge) != 0) {
                graphPlusDelaysAsNodes.removeEdge(edge);

                int delays = _edgeCosts.toInt(edge);

                for (int i = 0; i < delays; i++) {
                    Node addedNode = graphPlusDelaysAsNodes.addNodeWeight("D"
                            + j + i);
                    _delayNodeList.add(addedNode);

                    Edge addedEdge = graphPlusDelaysAsNodes.addEdge(source,
                            addedNode);
                    edgeProfitsMap.put(addedEdge, Double.valueOf(0.0));
                    source = addedNode;
                }

                Edge lastAddedEdge = graphPlusDelaysAsNodes.addEdge(source,
                        sink);
                edgeProfitsMap.put(lastAddedEdge, Double.valueOf(_edgeProfits
                        .toDouble(edge)));
            } else {
                edgeProfitsMap.put(edge, Double.valueOf(_edgeProfits
                        .toDouble(edge)));
            }
        }

        HashMap D = new HashMap(_delayNodeList.size());
        edges = graphPlusDelaysAsNodes.edges().toArray();

        // compute the first order longest path matrix
        HashMap predecessorMap = new HashMap();

        for (Iterator delayNodes = _delayNodeList.iterator(); delayNodes
                .hasNext();) {
            Node delayNode = (Node) delayNodes.next();
            DirectedGraph thisRoundGraph = (DirectedGraph) graphPlusDelaysAsNodes
                    .clone();
            HashMap delayGraphProfitMap = new HashMap();

            for (int j = 0; j < edges.length; j++) {
                Edge edge = (Edge) edges[j];
                Node source = edge.source();
                Node sink = edge.sink();

                if (sink == delayNode) {
                    predecessorMap.put(delayNode, source);
                }

                if (_delayNodeList.contains(source)
                        || _delayNodeList.contains(sink)) {
                    if (source == delayNode) {
                        delayGraphProfitMap.put(edge, edgeProfitsMap.get(edge));
                    }

                    if (sink == delayNode) {
                        thisRoundGraph.removeEdge(edge);
                    }

                    if ((source != delayNode) && (sink != delayNode)) {
                        if (_delayNodeList.contains(source)) {
                            thisRoundGraph.removeEdge(edge);
                        } else {
                            delayGraphProfitMap.put(edge, edgeProfitsMap
                                    .get(edge));
                        }
                    }
                } else {
                    delayGraphProfitMap.put(edge, edgeProfitsMap.get(edge));
                }
            }

            SingleSourceLongestPathAnalysis longestPath = null;
            longestPath = new SingleSourceLongestPathAnalysis(thisRoundGraph,
                    delayNode, new ToDoubleMapMapping(delayGraphProfitMap));
            D.put(delayNode, longestPath);
        }

        _makeFirstOrderLongestPathMatrix(D, graphPlusDelaysAsNodes,
                predecessorMap);

        // create the delay graph on which the maximum cycle mean is going to
        // be executed.
        DirectedGraph delayGraph = new DirectedGraph();
        HashMap delayGraphEdgeProfits = new HashMap();

        for (int i = 0; i < _delayNodeList.size(); i++) {
            delayGraph.addNode((Node) _delayNodeList.get(i));
        }

        for (int i = 0; i < _delayNodeList.size(); i++) {
            for (int j = 0; j < _delayNodeList.size(); j++) {
                Node source = (Node) _delayNodeList.get(i);
                Node sink = (Node) _delayNodeList.get(j);

                if (_firstOrderLongestPathMatrix[i][j] >= 0) {
                    if (!((source == sink) && (_firstOrderLongestPathMatrix[i][j] == 0))) {
                        Edge addedEdge = delayGraph.addEdge(source, sink);
                        delayGraphEdgeProfits.put(addedEdge, Double
                                .valueOf(_firstOrderLongestPathMatrix[i][j]));
                    }
                }
            }
View Full Code Here

            // Sort according to dependencies.
            List locallyModifiedAttributeList;

            try {
                DirectedGraph graph = _constAnalysis.getDependencyGraph();
                locallyModifiedAttributeList = Arrays.asList(Graph
                        .weightArray(graph.topologicalSort(graph
                                .nodes(locallyModifiedAttributeSet))));
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
View Full Code Here

     */
    public static void main(String[] args) {
        TestGraphReader tester = new TestGraphReader();
        CompositeActor toplevel = tester._readGraph(args);
        GraphReader graphReader = new GraphReader();
        DirectedGraph graph = (DirectedGraph) (graphReader.convert(toplevel));
        tester._printGraph(graph);
    }
View Full Code Here

     *  _constructDependencyGraph() to build a starting point.
     *  @return A complete dependency graph.
     */
    protected final DirectedGraph _constructConnectedDependencyGraph() {
        // construct new directed graph
        DirectedGraph dependencyGraph = _constructDisconnectedDependencyGraph();

        // For each input and output port pair, add a directed
        // edge going from the input port to the output port.
        Iterator inputs = ((Actor) getContainer()).inputPortList()
                .listIterator();

        while (inputs.hasNext()) {
            IOPort inputPort = (IOPort) inputs.next();
            Iterator outputs = ((Actor) getContainer()).outputPortList()
                    .listIterator();

            while (outputs.hasNext()) {
                // add an edge from the input port to the output port
                dependencyGraph.addEdge(inputPort, outputs.next());
            }
        }

        return dependencyGraph;
    }
View Full Code Here

     *  _constructDependencyGraph() to build a starting point.
     *  @return A dependency graph with nodes for ports but no edges.
     */
    protected final DirectedGraph _constructDisconnectedDependencyGraph() {
        // construct new directed graph
        DirectedGraph dependencyGraph = new DirectedGraph();

        // include all the externally visible ports of the associated actor
        // as nodes in the graph
        Iterator inputs = ((Actor) getContainer()).inputPortList()
                .listIterator();

        while (inputs.hasNext()) {
            IOPort input = (IOPort) inputs.next();
            dependencyGraph.addNodeWeight(input);
        }

        Iterator outputs = ((Actor) getContainer()).outputPortList()
                .listIterator();

        while (outputs.hasNext()) {
            IOPort output = (IOPort) outputs.next();
            dependencyGraph.addNodeWeight(output);
        }

        return dependencyGraph;
    }
View Full Code Here

        // We do not need the validity checking because this method is
        // only called from the _constructDependencyGraph() method, which
        // again can only be accessed from the _validate() method.
        // The _validate() method does the validity checking already
        // and gets the read access of workspace.
        DirectedGraph dependencyGraph = _dependencyGraph;

        // Note we can not use iterator here because the edges() method
        // returns an unmodifiableList. The removeEdge() method will cause
        // a concurrentModification exception.
        Object[] edges = dependencyGraph.edges().toArray();

        for (int i = 0; i < edges.length; i++) {
            Edge edge = (Edge) edges[i];

            if (edge.source().getWeight().equals(inputPort)
                    && edge.sink().getWeight().equals(outputPort)) {
                dependencyGraph.removeEdge(edge);
            }
        }
    }
View Full Code Here

        for (Iterator variables = variableSet.iterator(); variables.hasNext();) {
            Variable variable = (Variable) variables.next();
            _variableToChangeContext.put(variable, model);
        }

        _dependencyGraph = new DirectedGraph();

        _collectConstraints(model);
        _analyzeAllVariables();
    }
View Full Code Here

TOP

Related Classes of ptolemy.graph.DirectedGraph

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.