001/* Mutations example: Figure 15.7 from the de chapter
002
003 Copyright (c) 1999-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 */
027package ptolemy.domains.de.lib.test;
028
029import ptolemy.actor.Manager;
030import ptolemy.actor.TypedCompositeActor;
031import ptolemy.actor.lib.Clock;
032import ptolemy.actor.lib.Recorder;
033import ptolemy.domains.de.kernel.DEDirector;
034import ptolemy.domains.de.lib.Merge;
035import ptolemy.kernel.util.ChangeRequest;
036import ptolemy.kernel.util.IllegalActionException;
037import ptolemy.kernel.util.NameDuplicationException;
038
039/**
040 @author Edward A. Lee
041 @version $Id$
042 @Pt.ProposedRating Red (eal)
043 @Pt.AcceptedRating Red (cxh)
044 */
045public class Mutate {
046    public Manager manager;
047
048    private Recorder _rec;
049
050    private Clock _clock;
051
052    private TypedCompositeActor _top;
053
054    private DEDirector _director;
055
056    public Mutate() throws IllegalActionException, NameDuplicationException {
057        _top = new TypedCompositeActor();
058        _top.setName("top");
059        manager = new Manager();
060        _director = new DEDirector();
061        _top.setDirector(_director);
062        _top.setManager(manager);
063
064        _clock = new Clock(_top, "clock");
065        _clock.values.setExpression("[1.0]");
066        _clock.offsets.setExpression("[0.0]");
067        _clock.period.setExpression("1.0");
068        _rec = new Recorder(_top, "recorder");
069        _top.connect(_clock.output, _rec.input);
070    }
071
072    public void insertClock() {
073        // Create an anonymous inner class
074        ChangeRequest change = new ChangeRequest(_top, "test2") {
075            @Override
076            public void _execute()
077                    throws IllegalActionException, NameDuplicationException {
078                _clock.output.unlinkAll();
079                _rec.input.unlinkAll();
080
081                Clock clock2 = new Clock(_top, "clock2");
082                clock2.values.setExpression("[2.0]");
083                clock2.offsets.setExpression("[0.5]");
084                clock2.period.setExpression("2.0");
085
086                Merge merge = new Merge(_top, "merge");
087                _top.connect(_clock.output, merge.input);
088                _top.connect(clock2.output, merge.input);
089                _top.connect(merge.output, _rec.input);
090
091                // Any pre-existing input port whose connections
092                // are modified needs to have this method called.
093                _rec.input.createReceivers();
094                _director.invalidateSchedule();
095            }
096        };
097
098        _top.requestChange(change);
099    }
100}