001/*
002 *    $RCSfile$
003 *
004 *     $Author: welker $
005 *       $Date: 2010-05-06 05:21:26 +0000 (Thu, 06 May 2010) $
006 *   $Revision: 24234 $
007 *
008 *  For Details: http://kepler-project.org
009 *
010 * Copyright (c) 2007 The Regents of the University of California.
011 * All rights reserved.
012 *
013 * Permission is hereby granted, without written agreement and without
014 * license or royalty fees, to use, copy, modify, and distribute this
015 * software and its documentation for any purpose, provided that the
016 * above copyright notice and the following two paragraphs appear in
017 * all copies of this software.
018 *
019 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
020 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
021 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
022 * IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY
023 * OF SUCH DAMAGE.
024 *
025 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
026 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
027 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
028 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY
029 * OF CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT,
030 * UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
031 */
032package org.kepler.monitor.test;
033
034import java.awt.geom.Rectangle2D;
035
036import org.kepler.monitor.FigureUpdater;
037import org.kepler.monitor.MonitorIcon;
038import org.kepler.monitor.MonitoredStatus;
039import org.kepler.monitor.MonitoredStatus.State;
040import org.kepler.monitor.figure.TrafficLightFigure;
041
042import ptolemy.actor.lib.Transformer;
043import ptolemy.data.expr.StringParameter;
044import ptolemy.kernel.CompositeEntity;
045import ptolemy.kernel.util.Attribute;
046import ptolemy.kernel.util.IllegalActionException;
047import ptolemy.kernel.util.NameDuplicationException;
048
049/**
050 * This actor demonstrates the {@link TrafficLightFigure} by simulating some
051 * task that depends on the arrival of tokens.
052 * 
053 * In this demo, the figure is only displayed during execution.
054 * 
055 * @see org.kepler.monitor.TrafficLightFigure
056 * 
057 * @author Carlos Rueda
058 * @version $Id: ActorWithTrafficLight.java 24234 2010-05-06 05:21:26Z welker $
059 */
060public class ActorWithTrafficLight extends Transformer {
061
062        /** Delay for the timer */
063        public StringParameter timerDelay;
064
065        public ActorWithTrafficLight(CompositeEntity container, String name)
066                        throws IllegalActionException, NameDuplicationException {
067                super(container, name);
068
069                timerDelay = new StringParameter(this, "timerDelay");
070                timerDelay.setExpression("" + _delay);
071                timerDelay.setDisplayName("Delay to set WAITING state (ms)");
072
073                _icon = new MonitorIcon(this, "_icon");
074                _icon.setText("ActorWithTrafficLight");
075
076                _status = new MonitoredStatus();
077                _figure = new TrafficLightFigure(3, new Rectangle2D.Double(5, 3, 6,
078                                3 * 6));
079                _status.getPropertyTimer().setDelay(_delay);
080                _figUpdater = new FigureUpdater();
081                _figUpdater.setFigure(_figure);
082        }
083
084        /**
085         * Starts the figure.
086         */
087        public void initialize() throws IllegalActionException {
088                super.initialize();
089                _icon.setFigure(_figure);
090                _status.addPropertyChangeListener(_figUpdater);
091                _status.getPropertyTimer().start();
092        }
093
094        public void attributeChanged(Attribute attribute)
095                        throws IllegalActionException {
096                if (attribute == timerDelay) {
097                        try {
098                                _delay = Integer.parseInt(timerDelay.stringValue());
099                                _status.getPropertyTimer().setDelay(_delay);
100                        } catch (NumberFormatException e) {
101                                throw new IllegalActionException("NumberFormatException: " + e);
102                        }
103                } else {
104                        super.attributeChanged(attribute);
105                }
106        }
107
108        /**
109         * Broadcasts the input token.
110         */
111        public void fire() throws IllegalActionException {
112                super.fire();
113                if (input.hasToken(0)) {
114                        _status.setProperty(State.STATE, State.RUNNING);
115                        output.broadcast(input.get(0));
116                }
117        }
118
119        /** Stops the figure. */
120        public void wrapup() throws IllegalActionException {
121                super.wrapup();
122                _figure.setState(State.IDLE);
123                _status.getPropertyTimer().stop();
124                _status.removePropertyChangeListener(_figUpdater);
125                _icon.setFigure(null);
126        }
127
128        // /////////////////////////////////////////////////////////////////
129        // // private variables ////
130
131        private MonitoredStatus _status;
132        private TrafficLightFigure _figure;
133        private FigureUpdater _figUpdater;
134        private MonitorIcon _icon;
135        private int _delay = 1000;
136
137        private static final long serialVersionUID = 1L;
138}