001/* An application for testing the conversion of Ptolemy models into
002 weighted graphs.
003
004 Copyright (c) 2003-2013 The University of Maryland
005 All rights reserved.
006 Permission is hereby granted, without written agreement and without
007 license or royalty fees, to use, copy, modify, and distribute this
008 software and its documentation for any purpose, provided that the above
009 copyright notice and the following two paragraphs appear in all copies
010 of this software.
011
012 IN NO EVENT SHALL THE UNIVERSITY OF MARYLAND BE LIABLE TO ANY PARTY
013 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
014 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
015 THE UNIVERSITY OF MARYLAND HAS BEEN ADVISED OF THE POSSIBILITY OF
016 SUCH DAMAGE.
017
018 THE UNIVERSITY OF MARYLAND SPECIFICALLY DISCLAIMS ANY WARRANTIES,
019 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
020 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
021 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
022 MARYLAND HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
023 ENHANCEMENTS, OR MODIFICATIONS.
024
025 */
026package ptolemy.actor.test;
027
028import java.util.Collection;
029import java.util.Iterator;
030
031import ptolemy.actor.CompositeActor;
032import ptolemy.actor.GraphReader;
033import ptolemy.graph.DirectedGraph;
034import ptolemy.graph.Node;
035import ptolemy.kernel.util.NamedObj;
036import ptolemy.moml.MoMLParser;
037
038///////////////////////////////////////////////////////////////////
039//// TestGraphReader
040
041/** An application for testing the conversion of Ptolemy models into
042 weighted graphs.
043 <p>
044 Usage: <code>java ptolemy.actor.test <em>xmlFileName</em></code>,
045 <p>
046 where <code><em>xmlFileName</em></code> is the name of a MoML file that
047 contains a Ptolemy II specification. This application converts the
048 specification into a weighted graph representation, and prints out information
049 about this weighted graph.
050
051 @author Shuvra S. Bhattacharyya
052 @version $Id$
053 @since Ptolemy II 4.0
054 @Pt.ProposedRating Red (cxh)
055 @Pt.AcceptedRating Red (cxh)
056 */
057public class TestGraphReader {
058    // Make the constructor protected to prevent instantiation of this class
059    // outside of subclasses.
060    protected TestGraphReader() {
061    }
062
063    /** Convert a MoML file that contains a Ptolemy II specification into a
064     *  weighted graph representation, and display information about
065     *  the weighted graph.
066     *  @param args The name of the MoML file.
067     */
068    public static void main(String[] args) {
069        TestGraphReader tester = new TestGraphReader();
070        CompositeActor toplevel = tester._readGraph(args);
071        GraphReader graphReader = new GraphReader();
072        DirectedGraph graph = (DirectedGraph) graphReader.convert(toplevel);
073        tester._printGraph(graph);
074    }
075
076    ///////////////////////////////////////////////////////////////////
077    ////                         protected methods                 ////
078
079    /** Print information about a graph to standard output. This method
080     *  is called by {@link #main(String[])} to display information about the
081     *  model that is read. It should be overridden to change the way this
082     *  information is displayed.
083     *
084     * @param graph The graph for which information is to be printed.
085     */
086    protected void _printGraph(DirectedGraph graph) {
087        System.out.println(graph.toString());
088
089        // Determine the source nodes
090        Collection sourceCollection = graph.sourceNodes();
091        System.out
092                .println("Number of source nodes = " + sourceCollection.size());
093
094        Iterator sources = sourceCollection.iterator();
095        int sourceNumber = 1;
096
097        while (sources.hasNext()) {
098            System.out.println("source #" + sourceNumber++ + ": "
099                    + ((Node) sources.next()).getWeight());
100            System.out.println();
101        }
102
103        // Determine the sink nodes
104        Collection sinkCollection = graph.sinkNodes();
105        System.out.println("Number of sink nodes = " + sinkCollection.size());
106
107        Iterator sinks = sinkCollection.iterator();
108        int sinkNumber = 1;
109
110        while (sinks.hasNext()) {
111            System.out.println("sink #" + sinkNumber++ + ": "
112                    + ((Node) sinks.next()).getWeight());
113            System.out.println();
114        }
115    }
116
117    /** Convert a MoML file that contains a Ptolemy II specification into a
118     *  composite actor representation.
119     *  @param args The name of the MoML file.
120     *  @return The composite actor representation.
121     */
122    protected CompositeActor _readGraph(String[] args) {
123        if (args.length != 1) {
124            throw new RuntimeException(
125                    "TestGraphReader expects exactly one " + "argument.");
126        }
127
128        // The Ptolemy II model returned by the Java parser.
129        NamedObj toplevel;
130
131        try {
132            MoMLParser parser = new MoMLParser();
133            toplevel = parser.parseFile(args[0]);
134        } catch (Exception ex) {
135            throw new RuntimeException(ex.getMessage()
136                    + "Exception raised from the MoML parser\n");
137        }
138
139        if (!(toplevel instanceof CompositeActor)) {
140            throw new RuntimeException("Top level must be a CompositeActor "
141                    + "(in this case, it is '" + (toplevel == null ? "null"
142                            : toplevel.getClass().getName())
143                    + "')\n");
144        }
145
146        return (CompositeActor) toplevel;
147    }
148}