Package ptolemy.graph

Examples of ptolemy.graph.Graph


     @exception RuntimeException If the deep entity list of the
     *  composite actor contains an entry that is not an AtomicActor.
     */
    public Graph convert(CompositeActor compositeActor) {
        // Instantiate an empty graph.
        Graph graph = _initializeGraph(compositeActor);

        // Add all deeply-contained actors to the graph
        Iterator actors = compositeActor.deepEntityList().iterator();

        while (actors.hasNext()) {
            Object entity = actors.next();

            if (entity instanceof AtomicActor
                    || entity instanceof CompositeActor) {
                Actor actor = (Actor) entity;
                Node newNode = graph.addNodeWeight(_computeNodeWeight(actor));
                _actorMap.put(actor, newNode);
                _processNewNode(graph, newNode, actor);
            } else {
                throw new RuntimeException("Unsupported deep entity type: "
                        + entity.getClass().getName() + " (value = " + entity
                        + ")");
            }
        }

        // Convert each connection in the model to a graph edge
        actors = compositeActor.deepEntityList().iterator();

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

            // Connect the current actor to each of its sinks
            Iterator outPorts = source.outputPortList().iterator();

            while (outPorts.hasNext()) {
                IOPort outPort = (IOPort) (outPorts.next());
                Iterator inPorts = outPort.deepConnectedInPortList().iterator();

                while (inPorts.hasNext()) {
                    IOPort inPort = (IOPort) (inPorts.next());
                    Actor sink = (Actor) (inPort.getContainer());

                    if (graph.containsNode((Node) (_actorMap.get(sink)))) {
                        if (_debug) {
                            System.out.println("Adding edge from " + source
                                    + " to " + sink);
                        }

                        Edge newEdge = graph.addEdge((Node) (_actorMap
                                .get(source)), (Node) (_actorMap.get(sink)),
                                _computeEdgeWeight(outPort, inPort));
                        _processNewEdge(graph, newEdge, outPort, inPort);
                    }
                }
View Full Code Here


        _graph = graph;

        boolean tempCloneWeights = _cloneWeights;
        _cloneWeights = cloneWeights;

        Graph result = (Graph) _result();
        _cloneWeights = tempCloneWeights;
        return result;
    }
View Full Code Here

     *
     *  @return The mirror graph as an {@link Object}.
     */
    protected Object _compute() {
        String nameClone = "clone";
        Graph mirrorGraph = null;

        try {
            // Kepler (jdk1.4?) requires this cast
            mirrorGraph = (Graph) (_graph.getClass().newInstance());
        } catch (Exception exception) {
            throw new RuntimeException("Could not create an empty graph from "
                    + "this one.\n" + exception + "\n");
        }

        // create new nodes for the mirror
        Iterator nodes = graph().nodes().iterator();

        while (nodes.hasNext()) {
            Node node = (Node) nodes.next();
            Node mirrorNode = null;

            if (!node.hasWeight()) {
                mirrorNode = new Node();
            } else {
                Object mirrorWeight = null;

                try {
                    // Clone weights of any type of object.
                    if (_cloneWeights) {
                        Object oldWeight = node.getWeight();

                        if (oldWeight instanceof Cloneable) {
                            /* Since clone() of Object is protected, it can't
                             be called publicly. The class Method is used
                             here to call public clone(). */
                            Class[] argumentTypes = {};
                            Method method = oldWeight.getClass().getMethod(
                                    nameClone, argumentTypes);

                            // Cast to (Object []) so as to avoid varargs call.
                            mirrorWeight = method.invoke(oldWeight,
                                    (Object[]) null);
                        } else {
                            throw new RuntimeException();
                        }
                    } else {
                        mirrorWeight = node.getWeight();
                    }
                } catch (Throwable throwable) {
                    /* Exception due to non-Cloneable weights or
                     weights without public clone(). */
                    throw new AnalysisException(
                            "Can not clone the node weight.\n", throwable);
                }

                mirrorNode = new Node(mirrorWeight);
            }

            mirrorGraph.addNode(mirrorNode);
            _originalVersion.put(mirrorNode, node);
            _transformedVersion.put(node, mirrorNode);
        }

        // create new edges for the mirror
        Iterator edges = graph().edges().iterator();

        while (edges.hasNext()) {
            Edge edge = (Edge) edges.next();
            Edge mirrorEdge = null;
            Node mirrorSource = (Node) _transformedVersion.get(edge.source());
            Node mirrorSink = (Node) _transformedVersion.get(edge.sink());

            if (!edge.hasWeight()) {
                mirrorEdge = new Edge(mirrorSource, mirrorSink);
            } else {
                Object mirrorWeight = null;

                try {
                    // Clone weights of any type of object.
                    if (_cloneWeights) {
                        Object oldWeight = edge.getWeight();

                        if (oldWeight instanceof Cloneable) {
                            /* Since clone() of Object is protected, it can't
                             be called publicly. The class Method is used
                             here to call public clone(). */
                            Class[] argumentTypes = {};
                            Method method = oldWeight.getClass().getMethod(
                                    nameClone, argumentTypes);

                            // Cast to (Object []) so as to avoid varargs call.
                            mirrorWeight = method.invoke(oldWeight,
                                    (Object[]) null);
                        } else {
                            throw new RuntimeException();
                        }
                    } else {
                        mirrorWeight = edge.getWeight();
                    }
                } catch (Throwable throwable) {
                    /* Exception due to non-Cloneable weights or
                     weights without public clone(). */
                    throw new RuntimeException(
                            "Can not clone the edge weight.\n", throwable);
                }

                mirrorEdge = new Edge(mirrorSource, mirrorSink, mirrorWeight);
            }

            mirrorGraph.addEdge(mirrorEdge);
            _originalVersion.put(mirrorEdge, edge);
            _transformedVersion.put(edge, mirrorEdge);
        }

        return mirrorGraph;
View Full Code Here

     @return The mirror graph as an {@link Object}.
     */
    protected Object _compute() {
        // When removing nodes and edges, we have to be careful about
        // concurrent modification problems with the associated iterators.
        Graph graph = graph();
        Graph subgraph = graph.subgraph(_nodeCollection);
        graph.addNode(_superNode);

        HashSet nodesToRemove = new HashSet(_nodeCollection);

        // Remove all edges that are inside the induced subgraph.
View Full Code Here

TOP

Related Classes of ptolemy.graph.Graph

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.