001/*
002 * Copyright (c) 2004-2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: welker $'
006 * '$Date: 2010-05-06 05:21:26 +0000 (Thu, 06 May 2010) $' 
007 * '$Revision: 24234 $'
008 * 
009 * Permission is hereby granted, without written agreement and without
010 * license or royalty fees, to use, copy, modify, and distribute this
011 * software and its documentation for any purpose, provided that the above
012 * copyright notice and the following two paragraphs appear in all copies
013 * of this software.
014 *
015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
019 * SUCH DAMAGE.
020 *
021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
026 * ENHANCEMENTS, OR MODIFICATIONS.
027 *
028 */
029
030package org.cipres.kepler;
031
032// Ptolemy package
033import org.cipres.CipresIDL.api1.Tree;
034import org.cipres.datatypes.TreeWrapper;
035
036import ptolemy.actor.TypedAtomicActor;
037import ptolemy.actor.TypedIOPort;
038import ptolemy.data.ObjectToken;
039import ptolemy.data.StringToken;
040import ptolemy.data.type.BaseType;
041import ptolemy.kernel.CompositeEntity;
042import ptolemy.kernel.util.IllegalActionException;
043import ptolemy.kernel.util.NameDuplicationException;
044
045//////////////////////////////////////////////////////////////////////////
046//// TreeParser
047/**
048 * The TreeParser actor parses tree data structure into the tree name, tree
049 * score, leaf set, and Newick, and sends them to different ouput ports.
050 * 
051 * @author Zhijie Guan
052 * @version $Id: TreeParser.java 24234 2010-05-06 05:21:26Z welker $
053 */
054
055public class TreeParser extends TypedAtomicActor {
056
057        /**
058         * Construct a TreeParser actor with the given container and name.
059         * 
060         * @param container
061         *            The container.
062         * @param name
063         *            The name of this actor.
064         * @exception IllegalActionException
065         *                If the entity cannot be contained by the proposed
066         *                container.
067         * @exception NameDuplicationException
068         *                If the container already has an actor with this name.
069         */
070
071        public TreeParser(CompositeEntity container, String name)
072                        throws NameDuplicationException, IllegalActionException {
073                super(container, name);
074
075                // construct the input port inputTree
076                inputTree = new TypedIOPort(this, "inputTree", true, false);
077                inputTree.setDisplayName("Tree");
078
079                // construct the output ports outputName, outputScore, outputLeafSet,
080                // and outputNewick
081                outputName = new TypedIOPort(this, "outputName", false, true);
082                outputName.setDisplayName("Tree Name");
083                outputScore = new TypedIOPort(this, "outputScore", false, true);
084                outputScore.setDisplayName("Tree Score");
085                outputLeafSet = new TypedIOPort(this, "outputLeafSet", false, true);
086                outputLeafSet.setDisplayName("Tree Leaf Set");
087                outputNewick = new TypedIOPort(this, "outputNewick", false, true);
088                outputNewick.setDisplayName("Tree Expression (Newick)");
089                // Set the type constraint.
090                outputName.setTypeEquals(BaseType.GENERAL);
091                outputScore.setTypeEquals(BaseType.GENERAL);
092                outputLeafSet.setTypeEquals(BaseType.GENERAL);
093                outputNewick.setTypeEquals(BaseType.GENERAL);
094
095                _attachText("_iconDescription", "<svg>\n" + "<rect x=\"0\" y=\"0\" "
096                                + "width=\"60\" height=\"20\" " + "style=\"fill:white\"/>\n"
097                                + "</svg>\n");
098
099        }
100
101        // /////////////////////////////////////////////////////////////////
102        // // ports and parameters ////
103
104        /**
105         * The tree description in CIPRes tree data structure is passed to the
106         * TreeParser actor through this input port. This port is an input port of
107         * type GENERAL.
108         */
109        public TypedIOPort inputTree = null;
110
111        /**
112         * The detailed information of a tree is parsed and sent to different output
113         * ports. The tree name is sent to the outputName port. The type of this
114         * port will be set to GENERAL.
115         */
116        public TypedIOPort outputName = null;
117
118        /**
119         * The tree score is sent to the outputScore port. The type of this port
120         * will be set to GENERAL.
121         */
122        public TypedIOPort outputScore = null;
123
124        /**
125         * The tree's leaf set is sent to the outputLeafSet port. The type of this
126         * port will be set to GENERAL.
127         */
128        public TypedIOPort outputLeafSet = null;
129
130        /**
131         * The tree's newick expression is sent to the outputNewick port. The type
132         * of this port will be set to GENERAL.
133         */
134        public TypedIOPort outputNewick = null;
135
136        // /////////////////////////////////////////////////////////////////
137        // // functional variables ////
138        // CIPRes IDL tree data structure
139        private org.cipres.CipresIDL.api1.Tree _aTree;
140
141        // /////////////////////////////////////////////////////////////////
142        // // public methods ////
143
144        /**
145         * Parse the tree and send various sub data structure into corresponding
146         * ports.
147         * 
148         * @exception IllegalActionException
149         *                If it is thrown by the send() method sending out the
150         *                token.
151         */
152        public void fire() throws IllegalActionException {
153                super.fire();
154
155                if (inputTree.hasToken(0)) {
156                        // get the tree in CIPRes IDL data structure
157                        _aTree = (Tree) ((ObjectToken) inputTree.get(0)).getValue();
158                        // send the sub data structure to different ports
159                        outputName.send(0, new StringToken(_aTree.m_name));
160                        outputScore.send(0, new StringToken(TreeWrapper
161                                        .scoreToString(_aTree.m_score)));
162                        String leafSet = "";
163                        for (int i = 0; i < _aTree.m_leafSet.length; i++) {
164                                leafSet += _aTree.m_leafSet[i] + " ";
165                        }
166                        outputLeafSet.send(0, new StringToken(leafSet));
167                        outputNewick.send(0, new StringToken(_aTree.m_newick));
168                }
169        }
170}