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//// TreeToString
047/**
048 * The TreeToString actor gets the whole description of a tree (including tree
049 * name, tree score, leaf set, and Newick) and transforms it into a single
050 * string.
051 * 
052 * @author Zhijie Guan
053 * @version $Id: TreeToString.java 24234 2010-05-06 05:21:26Z welker $
054 */
055
056public class TreeToString extends TypedAtomicActor {
057
058        /**
059         * Construct a TreeToString actor with the given container and name.
060         * 
061         * @param container
062         *            The container.
063         * @param name
064         *            The name of this actor.
065         * @exception IllegalActionException
066         *                If the entity cannot be contained by the proposed
067         *                container.
068         * @exception NameDuplicationException
069         *                If the container already has an actor with this name.
070         */
071
072        public TreeToString(CompositeEntity container, String name)
073                        throws NameDuplicationException, IllegalActionException {
074                super(container, name);
075
076                // construct input port inputTree
077                inputTree = new TypedIOPort(this, "inputTree", true, false);
078                inputTree.setDisplayName("Tree");
079                inputTree.setTypeEquals(BaseType.GENERAL);
080
081                // construct output put outputString
082                outputString = new TypedIOPort(this, "outputString", false, true);
083                outputString.setDisplayName("Tree Expressed in a String");
084                // Set the type constraint.
085                outputString.setTypeEquals(BaseType.GENERAL);
086
087                _attachText("_iconDescription", "<svg>\n" + "<rect x=\"0\" y=\"0\" "
088                                + "width=\"60\" height=\"20\" " + "style=\"fill:white\"/>\n"
089                                + "</svg>\n");
090
091        }
092
093        // /////////////////////////////////////////////////////////////////
094        // // ports and parameters ////
095
096        /**
097         * The whole tree expression to pass to the TreeToString actor. This
098         * expression may include the tree name, tree score, leaf set, and/or
099         * Newick. This port is an input port of type GENERAL.
100         */
101        public TypedIOPort inputTree = null;
102
103        /**
104         * The single tree string expression to represent a tree. This port is an
105         * output port of type GENERAL.
106         */
107        public TypedIOPort outputString = null;
108
109        // /////////////////////////////////////////////////////////////////
110        // // functional variables ////
111
112        // /////////////////////////////////////////////////////////////////
113        // // public methods ////
114
115        /**
116         * Send the single string description of a tree to the output port.
117         * 
118         * @exception IllegalActionException
119         *                If it is thrown by the send() method sending out the
120         *                token.
121         */
122        public void fire() throws IllegalActionException {
123                super.fire();
124
125                if (inputTree.hasToken(0)) {
126                        // translate the tree expression using the TreeWrapper class
127                        outputString.send(0, new StringToken(TreeWrapper
128                                        .asString((Tree) ((ObjectToken) inputTree.get(0))
129                                                        .getValue())));
130                }
131
132        }
133}