001/*
002Below is the copyright agreement for the Ptolemy II system.
003
004Copyright (c) 2009-2014 The Regents of the University of California.
005All rights reserved.
006
007Permission is hereby granted, without written agreement and without
008license or royalty fees, to use, copy, modify, and distribute this
009software and its documentation for any purpose, provided that the above
010copyright notice and the following two paragraphs appear in all copies
011of this software.
012
013IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
014FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
015ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
016THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
017SUCH DAMAGE.
018
019THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
020INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
021MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
022PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
023CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
024ENHANCEMENTS, OR MODIFICATIONS.
025 */
026//package gov.nasa.gsfc.giovanni;
027package ptolemy.actor.lib.test;
028
029import ptolemy.actor.TypedAtomicActor;
030import ptolemy.actor.TypedIOPort;
031import ptolemy.data.StringToken;
032import ptolemy.data.type.BaseType;
033import ptolemy.kernel.CompositeEntity;
034import ptolemy.kernel.util.IllegalActionException;
035import ptolemy.kernel.util.NameDuplicationException;
036
037/**
038 * Name: LoopTest.java Purpose: The purpose of this actor is
039 *   prove that output from another actor can be input to this actor.
040 *
041 * input : counter : start of the loop, will loop until it's stopped (# 75)
042 xmlResults :
043 * output : .
044 *
045 *@author Jim Amrhein/Mark Ruebens Giovannii/GSFC Greenbelt
046@version $Id$
047@since Ptolemy II 8.0
048 *
049 */
050
051public class LoopTest2 extends TypedAtomicActor {
052
053    boolean continueLooping = true;
054    /**
055     * The Point (Lat,Long) the user selected from the map
056     */
057    public TypedIOPort counter = new TypedIOPort(this, "counter", true, false);
058
059    public TypedIOPort xmlResults = new TypedIOPort(this, "XML Results", true,
060            false);
061
062    /**
063     */
064
065    public TypedIOPort searchOutput = new TypedIOPort(this, "searchOutput",
066            false, true);
067
068    public TypedIOPort foundResultsOutput = new TypedIOPort(this,
069            "found Results Output", false, true);
070
071    public LoopTest2(CompositeEntity container, String name)
072            throws NameDuplicationException, IllegalActionException {
073
074        super(container, name);
075        counter.setTypeEquals(BaseType.STRING);
076        searchOutput.setTypeEquals(BaseType.STRING);
077        foundResultsOutput.setTypeEquals(BaseType.STRING);
078        xmlResults.setTypeEquals(BaseType.STRING);
079    }
080
081    /**
082     *
083     */
084    @Override
085    public boolean prefire() throws IllegalActionException {
086        return super.prefire();
087    }
088
089    /**
090     *
091     */
092    @Override
093    public void fire() throws IllegalActionException {
094        super.fire();
095
096        if (counter.getWidth() > 0 && counter.hasToken(0)) {
097            String counterStr = ((StringToken) counter.get(0)).stringValue();
098            int counterInt = -1;
099            try {
100                counterInt = Integer.parseInt(counterStr);
101            } catch (NumberFormatException ex) {
102                throw new IllegalActionException(this, ex,
103                        "Could not convert \"" + counterStr
104                                + "\" to an integer.");
105            }
106            System.out.println("counter input  is " + counterInt);
107
108            // if (xmlResults.getWidth() <= 0) { }
109
110            // see if I can have the loop stop itself before it reaches
111            // the limit of 75 "searches"
112            if (xmlResults.getWidth() > 0 && xmlResults.hasToken(0)) {
113                String xmlResultsStr = ((StringToken) xmlResults.get(0))
114                        .stringValue();
115                System.out.println("#" + xmlResultsStr + "# \n");
116                if (xmlResultsStr.equals("Results Found")) {
117                    System.out.println("Got data, set to " + xmlResultsStr);
118                    foundResultsOutput
119                            .broadcast(new StringToken(xmlResultsStr));
120                    continueLooping = false;
121                }
122            } else {
123                System.out.println("counter is " + counterInt);
124                searchOutput.broadcast(new StringToken("Search " + counterInt));
125                if (counterInt == 75) {
126                    System.out.println("Ending this " + counterInt);
127                    continueLooping = false;
128                }
129
130            }
131
132        }
133    }
134
135    /**
136     * Post fire the actor. Return false to indicate that the process has
137     * finished. If it returns true, the process will continue indefinitely.
138     *
139     *@return the value of the continueLooping flag.
140     */
141    @Override
142    public boolean postfire() throws IllegalActionException {
143        return continueLooping;
144        //                return super.postfire();
145    }
146
147}