001/* Test for ChangeRequest in the DE domain.
002
003 Copyright (c) 1998-2014 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.kernel.util.test.system;
029
030import java.util.Collections;
031import java.util.Enumeration;
032
033import ptolemy.actor.IORelation;
034import ptolemy.actor.Manager;
035import ptolemy.actor.TypedCompositeActor;
036import ptolemy.actor.lib.Clock;
037import ptolemy.actor.lib.Recorder;
038import ptolemy.domains.de.kernel.DEDirector;
039import ptolemy.domains.de.lib.Merge;
040import ptolemy.kernel.util.ChangeRequest;
041import ptolemy.kernel.util.IllegalActionException;
042import ptolemy.kernel.util.KernelException;
043import ptolemy.kernel.util.NameDuplicationException;
044
045///////////////////////////////////////////////////////////////////
046//// TestDE
047
048/**
049 Test for ChangeRequest in the DE domain.
050
051 @author  Edward A. Lee
052 @version $Id$
053 @since Ptolemy II 10.0
054 @Pt.ProposedRating Red (eal)
055 @Pt.AcceptedRating Red (reviewmoderator)
056 @see ptolemy.kernel.util.ChangeRequest
057 */
058public class TestDE {
059    /** Constructor.
060     */
061    public TestDE() throws IllegalActionException, NameDuplicationException {
062        _top = new TypedCompositeActor();
063        _top.setName("top");
064        _manager = new Manager();
065        _director = new DEDirector();
066        _top.setDirector(_director);
067        _top.setManager(_manager);
068
069        _clock = new Clock(_top, "clock");
070        _clock.values.setExpression("{1.0}");
071        _clock.offsets.setExpression("{0.0}");
072        _clock.period.setExpression("1.0");
073        _rec = new Recorder(_top, "rec");
074        IORelation relation = (IORelation) _top.connect(_clock.output,
075                _rec.input);
076        relation.setWidth(1);
077    }
078
079    ///////////////////////////////////////////////////////////////////
080    ////                         public methods                    ////
081
082    /** Double the period of the clock.  Note that this will take
083     *  after the next event is processed, because the next firing
084     *  has already been queued.
085     */
086    public void doublePeriod() {
087        // Create an anonymous inner class
088        ChangeRequest change = new ChangeRequest(this, "test") {
089            @Override
090            protected void _execute() throws Exception {
091                _clock.period.setExpression("2.0");
092                _clock.period.validate();
093            }
094        };
095
096        _top.requestChange(change);
097    }
098
099    /** Finish a run.  Return the time of the output events.
100     */
101    public Enumeration finish() throws KernelException {
102        while (_director.getModelTime().getDoubleValue() <= 10.0) {
103            _manager.iterate();
104        }
105
106        _manager.wrapup();
107        return Collections.enumeration(_rec.getTimeHistory());
108    }
109
110    /** Insert a new clock.
111     */
112    public void insertClock() {
113        // Create an anonymous inner class
114        ChangeRequest change = new ChangeRequest(this, "test2") {
115            @Override
116            protected void _execute() throws Exception {
117                _clock.output.unlinkAll();
118                _rec.input.unlinkAll();
119
120                Clock clock2 = new Clock(_top, "clock2");
121                clock2.values.setExpression("{2.0}");
122                clock2.offsets.setExpression("{0.5}");
123                clock2.period.setExpression("2.0");
124
125                Merge merge = new Merge(_top, "merge");
126                _top.connect(_clock.output, merge.input);
127                _top.connect(clock2.output, merge.input);
128                _top.connect(merge.output, _rec.input);
129
130                // Any pre-existing input port whose connections
131                // are modified needs to have this method called.
132                _rec.input.createReceivers();
133            }
134        };
135
136        _top.requestChange(change);
137    }
138
139    /** Start a run.
140     */
141    public void start() throws KernelException {
142        _manager.initialize();
143
144        // Process up to time 1.0
145        while (_director.getModelTime().getDoubleValue() <= 1.0) {
146            _manager.iterate();
147        }
148    }
149
150    ///////////////////////////////////////////////////////////////////
151    ////                         private variables                 ////
152    private Manager _manager;
153
154    private Recorder _rec;
155
156    private Clock _clock;
157
158    private TypedCompositeActor _top;
159
160    private DEDirector _director;
161}