001/* A source for testing iterations and microsteps.
002 Copyright (c) 1999-2014 The Regents of the University of California.
003 All rights reserved.
004 Permission is hereby granted, without written agreement and without
005 license or royalty fees, to use, copy, modify, and distribute this
006 software and its documentation for any purpose, provided that the above
007 copyright notice and the following two paragraphs appear in all copies
008 of this software.
009
010 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
011 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
012 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
013 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
014 SUCH DAMAGE.
015
016 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
017 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
018 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
019 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
020 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
021 ENHANCEMENTS, OR MODIFICATIONS.
022
023 PT_COPYRIGHT_VERSION_2
024 COPYRIGHTENDKEY
025
026 @ProposedRating Red (eal)
027 @AcceptedRating Red (cxh)
028 */
029package ptolemy.domains.de.lib.test;
030
031import ptolemy.actor.Director;
032import ptolemy.actor.TypedAtomicActor;
033import ptolemy.actor.TypedIOPort;
034import ptolemy.actor.util.Time;
035import ptolemy.data.DoubleToken;
036import ptolemy.data.type.BaseType;
037import ptolemy.kernel.CompositeEntity;
038import ptolemy.kernel.util.IllegalActionException;
039import ptolemy.kernel.util.NameDuplicationException;
040import ptolemy.kernel.util.Workspace;
041
042/**
043 This actor fires itself five times at each time instant,
044 then repeats the cycle one time unit later.  It outputs a ramp,
045 starting at zero and incrementing by one each time.
046 If there is an input, it adds the input to the ramp output.
047 This actor is designed to test two features of the DE scheduler.
048 First, that an iteration processes all events of a given time
049 stamp.  And second, that self-scheduling events are processed
050 in the proper order, with proper priorities.  To do these
051 tests, connect two of these actors in cascade.
052 */
053public class TestSource extends TypedAtomicActor {
054    public TestSource(Workspace workspace) {
055        super(workspace);
056    }
057
058    public TestSource(CompositeEntity container, String name)
059            throws NameDuplicationException, IllegalActionException {
060        super(container, name);
061        input = new TypedIOPort(this, "input", true, false);
062        input.setTypeEquals(BaseType.DOUBLE);
063        output = new TypedIOPort(this, "output", false, true);
064        output.setTypeEquals(BaseType.DOUBLE);
065
066        _attachText("_iconDescription",
067                "<svg>\n" + "<rect x=\"0\" y=\"0\" "
068                        + "width=\"60\" height=\"20\" "
069                        + "style=\"fill:white\"/>\n" + "</svg>\n");
070    }
071
072    public TypedIOPort input;
073
074    public TypedIOPort output;
075
076    // NOTE: No clone() method, so don't clone this.
077    @Override
078    public void fire() throws IllegalActionException {
079        super.fire();
080        double increment = 0.0;
081
082        if (input.isOutsideConnected() && input.hasToken(0)) {
083            DoubleToken in = (DoubleToken) input.get(0);
084            increment = in.doubleValue();
085        }
086
087        output.broadcast(new DoubleToken(value + increment));
088        value += 1.0;
089
090        Director director = getDirector();
091        Time time = director.getModelTime();
092        count++;
093
094        if (count >= 5) {
095            _fireAt(time.add(1.0));
096            count = 0;
097        } else {
098            _fireAt(time);
099        }
100    }
101
102    @Override
103    public void initialize() throws IllegalActionException {
104        value = 0.0;
105        count = 0;
106
107        Director director = getDirector();
108        _fireAt(director.getModelTime());
109    }
110
111    private double value = 0.0;
112
113    private int count = 0;
114}