001/* Compute the projection of an interface automaton to another one.
002
003 Copyright (c) 1999-2016 The Regents of the University of California.
004 All rights reserved.
005 Permission is hereby granted, without written agreement and without
006 license or royalty fees, to use, copy, modify, and distribute this
007 software and its documentation for any purpose, provided that the above
008 copyright notice and the following two paragraphs appear in all copies
009 of this software.
010
011 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
012 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
013 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
014 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
015 SUCH DAMAGE.
016
017 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
018 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
019 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
020 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
021 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
022 ENHANCEMENTS, OR MODIFICATIONS.
023
024 PT_COPYRIGHT_VERSION_2
025 COPYRIGHTENDKEY
026 */
027package ptolemy.domains.modal.kernel.test;
028
029import java.net.URL;
030
031import ptolemy.actor.gui.ConfigurationApplication;
032import ptolemy.domains.modal.kernel.ia.InterfaceAutomaton;
033import ptolemy.moml.MoMLParser;
034import ptolemy.util.StringUtilities;
035
036///////////////////////////////////////////////////////////////////
037//// Project
038
039/**
040 Compute the projection of an interface automaton to another one.
041 This class reads the MoML description of two automata, computes the projection
042 of the first one to the second, then writes the MoML description of the
043 projection to stdout. The usage is:
044 <pre>
045 java ptolemy.domains.modal.kernel.test.Project <i>first_automaton.xml</i> <i>second_automaton.xml</i>
046 </pre>
047
048 @author Yuhong Xiong
049 @version $Id$
050 @since Ptolemy II 8.0
051 @Pt.ProposedRating Red (yuhong)
052 @Pt.AcceptedRating Red (reviewmoderator)
053 */
054public class Project {
055    /** Compute the projection of the first automaton to the second one and
056     *  write the result to stdout.
057     *  @param firstMoML The MoML file name for the first interface automaton.
058     *  @param secondMoML The MoML file name for the second interface automaton.
059     *  @exception Exception If the specified automata cannot be constructed
060     *   or are not consistent.
061     */
062    public Project(String firstMoML, String secondMoML) throws Exception {
063        // Construct the first automaton
064        URL url = ConfigurationApplication.specToURL(firstMoML);
065
066        // following the comments in MoMLApplication, use the same URL for
067        // the two arguments (base and URL) to parse().
068        MoMLParser parser = new MoMLParser();
069        InterfaceAutomaton firstAutomaton = (InterfaceAutomaton) parser
070                .parse(url, url);
071        firstAutomaton.addPorts();
072
073        // Construct the second automaton
074        url = ConfigurationApplication.specToURL(secondMoML);
075
076        // following the comments in MoMLApplication, use the same URL for
077        // the two arguments (base and URL) to parse().  Also, a new instance
078        // of MoMLParser must be used to parse each file, otherwise
079        // the same automaton will be returned the second time parse() is
080        // called.
081        parser = new MoMLParser();
082
083        InterfaceAutomaton secondAutomaton = (InterfaceAutomaton) parser
084                .parse(url, url);
085        secondAutomaton.addPorts();
086
087        // Compute the projection and write result
088        firstAutomaton.project(secondAutomaton);
089        System.out.println(firstAutomaton.exportMoML());
090    }
091
092    ///////////////////////////////////////////////////////////////////
093    ////                         public methods                    ////
094
095    /** Pass the command line arguments to the constructor. The command line
096     *  arguments are two MoML files for interface automaton.
097     *  @param args The command line arguments.
098     */
099    public static void main(String[] args) {
100        if (args.length != 2) {
101            System.out.println("Usage: java ptolemy.domains.modal.kernel."
102                    + "test.Project <first_automaton.xml> <second_automaton.xml>");
103            System.out.println("This program computes the projection of the "
104                    + "first automaton to the second one.");
105            StringUtilities.exit(1);
106        } else {
107            try {
108                new Project(args[0], args[1]);
109            } catch (Exception exception) {
110                System.out.println(exception.getClass().getName() + ": "
111                        + exception.getMessage());
112                exception.printStackTrace();
113            }
114        }
115    }
116}