001/*
002 * Copyright (c) 2002-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.geon;
031
032import ptolemy.actor.TypedAtomicActor;
033import ptolemy.actor.TypedIOPort;
034import ptolemy.data.StringToken;
035import ptolemy.data.expr.Variable;
036import ptolemy.data.type.ArrayType;
037import ptolemy.data.type.BaseType;
038import ptolemy.kernel.CompositeEntity;
039import ptolemy.kernel.util.IllegalActionException;
040import ptolemy.kernel.util.NameDuplicationException;
041
042//////////////////////////////////////////////////////////////////////////
043//// StringToPolygon
044/**
045 * This actor reads a string and outputs an array of coordinates and a string of
046 * region. This is a domain specific actor used within the GEON mineral
047 * classifier. The string is of the following format:
048 * {{{x1,y1},{x2,y2},...},region}.
049 * 
050 * @author Efrat Jaeger
051 * @version $Id: StringToPolygon.java 24234 2010-05-06 05:21:26Z welker $
052 * @since Ptolemy II 3.0.2
053 */
054public class StringToPolygon extends TypedAtomicActor {
055
056        public TypedIOPort input = new TypedIOPort(this, "input", true, false);
057        public TypedIOPort region = new TypedIOPort(this, "region", false, true);
058        public TypedIOPort coordinates = new TypedIOPort(this, "coordinates",
059                        false, true);
060
061        public StringToPolygon(CompositeEntity container, String name)
062                        throws IllegalActionException, NameDuplicationException {
063                super(container, name);
064
065                input.setTypeEquals(BaseType.STRING);
066                coordinates.setTypeEquals(new ArrayType(new ArrayType(BaseType.INT)));
067                region.setTypeEquals(BaseType.STRING);
068
069                _expressionEvaluator = new Variable(this, "_expressionEvaluator");
070
071                _attachText("_iconDescription", "<svg>\n" + "<rect x=\"0\" y=\"0\" "
072                                + "width=\"60\" height=\"20\" " + "style=\"fill:white\"/>\n"
073                                + "</svg>\n");
074        }
075
076        // /////////////////////////////////////////////////////////////////
077        // // public methods ////
078
079        /**
080         * Clone the actor into the specified workspace.
081         * 
082         * @return A new actor.
083         * @exception CloneNotSupportedException
084         *                If a derived class contains an attribute that cannot be
085         *                cloned.
086         */
087        /*
088         * public Object clone(Workspace workspace) throws
089         * CloneNotSupportedException { LineReader newObject =
090         * (LineReader)super.clone(workspace); newObject._currentLine = null;
091         * newObject._reachedEOF = false; newObject._reader = null; return
092         * newObject; }
093         */
094
095        /**
096         * Output the data lines into an array.
097         * 
098         * @exception IllegalActionException
099         *                If there's no director.
100         */
101
102        public void fire() throws IllegalActionException {
103                int i = 0;
104                int numBrackets = 1;
105                inputValue = ((StringToken) input.get(0)).stringValue();
106                if (inputValue.charAt(i++) == '{') {
107                        while (numBrackets != 0) {
108                                if (inputValue.charAt(i) == '{')
109                                        numBrackets++;
110                                if (inputValue.charAt(i) == '}')
111                                        numBrackets--;
112                                i++;
113                        }
114                        _expressionEvaluator.setExpression(inputValue.substring(0, i));
115                        coordinates.broadcast(_expressionEvaluator.getToken());
116
117                        region.broadcast(new StringToken(inputValue.substring(i, inputValue
118                                        .length())));
119
120                } else
121                        throw new IllegalActionException("Illegal String!");
122
123        }
124
125        /**
126         * Post fire the actor. Return false to indicated that the process has
127         * finished. If it returns true, the process will continue indefinitely.
128         */
129
130        public boolean postfire() {
131                return true;
132        }
133
134        // /////////////////////////////////////////////////////////////////
135        // // protected members ////
136
137        /** Cache of most recently read data. */
138        protected String inputValue;
139
140        // /////////////////////////////////////////////////////////////////
141        // // private members ////
142
143        /** Variable used to evaluate expressions. */
144        private Variable _expressionEvaluator;
145
146}