001/* Butterfly demo
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
027 */
028package ptolemy.domains.sdf.demo.Butterfly;
029
030import ptolemy.actor.TypedCompositeActor;
031import ptolemy.actor.TypedIOPort;
032import ptolemy.actor.TypedIORelation;
033import ptolemy.actor.lib.Expression;
034import ptolemy.actor.lib.Ramp;
035import ptolemy.actor.lib.conversions.PolarToCartesian;
036import ptolemy.actor.lib.gui.XYPlotter;
037import ptolemy.domains.sdf.kernel.SDFDirector;
038import ptolemy.kernel.util.IllegalActionException;
039import ptolemy.kernel.util.NameDuplicationException;
040import ptolemy.kernel.util.Workspace;
041import ptolemy.plot.Plot;
042
043//////////////////////////////////////////////////////////////////////////
044//// Butterfly
045
046/**
047 This class defines a Ptolemy II model that traces an elaborate curve
048 called the butterfly curve.
049 It was described by T. Fay, <i>American Mathematical Monthly</i>, 96(5),
050 May, 1989.  Although users will usually prefer to define models using
051 MoML, this class illustrates how to define a model in Java.
052
053 <p>To run this model, use:</p>
054 <pre>
055java -classpath $PTII ptolemy.actor.gui.CompositeActorApplication -class ptolemy.domains.sdf.demo.Butterfly.Butterfly
056 </pre>
057
058 @author Christopher Hylands and Edward A. Lee
059 @version $Id$
060 @since Ptolemy II 0.4
061 @Pt.ProposedRating Yellow (eal)
062 @Pt.AcceptedRating Red (reviewmoderator)
063 */
064public class Butterfly extends TypedCompositeActor {
065    /** Construct a Butterfly with a name and a container.
066     *  The container argument must not be null, or a
067     *  NullPointerException will be thrown.  This actor will use the
068     *  workspace of the container for synchronization and version counts.
069     *  If the name argument is null, then the name is set to the empty string.
070     *  Increment the version of the workspace.  This actor will have no
071     *  local director initially, and its executive director will be simply
072     *  the director of the container.
073     *
074     *  @param workspace The workspace in which to create the Butterfly.
075     *  @exception IllegalActionException If the container is incompatible
076     *   with this actor.
077     *  @exception NameDuplicationException If the name coincides with
078     *   an actor already in the container.
079     */
080    public Butterfly(Workspace workspace)
081            throws IllegalActionException, NameDuplicationException {
082        super(workspace);
083        setName("Butterfly");
084
085        // Create the director, and set the number of iterations to execute.
086        SDFDirector director = new SDFDirector(this, "director");
087        director.iterations.setExpression("2400");
088        setDirector(director);
089
090        // Create the actors, and set their parameters.
091        // First, the source, which counts up from 0.0 in steps of pi/100.
092        Ramp ramp = new Ramp(this, "Ramp");
093        ramp.step.setExpression("PI/100.0");
094
095        // Next, the expression, for which we have to create an input port.
096        Expression expression = new Expression(this, "Expression");
097        TypedIOPort expInput = new TypedIOPort(expression, "ramp");
098        expInput.setInput(true);
099        expression.expression.setExpression("-2.0*cos(4.0*ramp) + "
100                + "exp(cos(ramp)) + (sin(ramp/12.0) * (sin(ramp/12.0))^4)");
101
102        // Next, a conversion to use the ramp as an angle specifier,
103        // and the output of the expression as the vector length.
104        PolarToCartesian polarToCartesian = new PolarToCartesian(this,
105                "Polar to Cartesian");
106
107        // Finally, the plotter.
108        XYPlotter xyPlotter = new XYPlotter(this, "xyPlotter");
109        xyPlotter.plot = new Plot();
110        xyPlotter.plot.setGrid(false);
111        xyPlotter.plot.setXRange(-3, 4);
112        xyPlotter.plot.setYRange(-4, 4);
113
114        // Make the connections.
115        // The ports are public members of these classes.
116        // The first connection is a three way connection, so we have
117        // to create a relation and then link to it.
118        TypedIORelation node = (TypedIORelation) newRelation("node");
119        ramp.output.link(node);
120        expInput.link(node);
121        polarToCartesian.angle.link(node);
122
123        // The rest of the connections are point-to-point, so we can use
124        // the connect() method.
125        connect(expression.output, polarToCartesian.magnitude);
126        connect(polarToCartesian.x, xyPlotter.inputX);
127        connect(polarToCartesian.y, xyPlotter.inputY);
128    }
129}