001/* For testing the various methods of PNDirector.
002
003 Copyright (c) 1997-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.domains.pn.kernel.test;
029
030import ptolemy.actor.AtomicActor;
031import ptolemy.actor.CompositeActor;
032import ptolemy.actor.IOPort;
033import ptolemy.data.IntToken;
034import ptolemy.kernel.util.IllegalActionException;
035import ptolemy.kernel.util.NameDuplicationException;
036
037//////////////////////////////////////////////////////////////////////////
038//// TestDirector
039
040/**
041 This object implements a thread that obtains read permission to
042 a workspace three times sequentially, then calls workspace.wait(obj) on an
043 object and exits. The object "obj" on which the wait method is called is an
044 inner class of TestWorkspace2 and has a thread of its own. This thread gets a
045 write access on the workspace, after the TestWorkspace2 object calls wait(obj)
046 on it. Then it gives up the write access and returns.
047 To use it, create an instance and then call its start() method.
048 To obtain a profile of what it did, call its profile() method.
049 That will return only after the thread completes.
050 NOTE: This is a very primitive test.  It does not check very much.
051
052 @author Mudit Goel, Edward A. Lee
053 @version $Id$
054 @since Ptolemy II 0.3
055 @Pt.ProposedRating Green (mudit)
056 @Pt.AcceptedRating Red (mudit)
057
058 */
059public class TestDirector extends AtomicActor {
060    public TestDirector(CompositeActor container, String name)
061            throws NameDuplicationException, IllegalActionException {
062        super(container, name);
063        input = new IOPort(this, "input", true, false);
064        output = new IOPort(this, "output", false, true);
065    }
066
067    /** Clear the profile accumulated till now.
068     */
069    public void clearProfile() {
070        profile = "";
071    }
072
073    /** Start a thread for an instance of the inner class "Notification",
074     *  obtain read access on the workspace 3 times, call wait(obj) on the
075     *  workspace, ask the inner class to get a write access on the workspace
076     *  and return after relinquishing the read accesses on the workspace.
077     *  This method is synchronized both on this class and the inner class
078     */
079    @Override
080    public synchronized void fire() throws IllegalActionException {
081        int i = 0;
082
083        for (i = 0; i < 2; i++) {
084            output.broadcast(new IntToken(i));
085            profile += "broadcast new token " + i + "\n";
086        }
087
088        for (i = 0; i < 2; i++) {
089            int ans = ((IntToken) input.get(0)).intValue();
090            profile += "received new token " + ans + "\n";
091        }
092
093        try {
094            ((CompositeActor) getContainer()).workspace().getReadAccess();
095
096            // an actor should not call director.wrapup()
097            //((PNDirector)getDirector()).wrapup();
098        } finally {
099            ((CompositeActor) getContainer()).workspace().doneReading();
100        }
101
102        output.broadcast(new IntToken(i));
103        profile += "broadcast new token " + i + "\n";
104    }
105
106    /** Return a profile which contains the various actions performed by this
107     *  object.
108     */
109    public synchronized String getProfile() {
110        return profile;
111    }
112
113    @Override
114    public boolean postfire() {
115        return false;
116    }
117
118    public IOPort input;
119
120    public IOPort output;
121
122    public String profile = "";
123}