001/*
002 * Copyright (c) 2004-2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: welker $'
006 * '$Date: 2010-05-06 05:21:26 +0000 (Thu, 06 May 2010) $' 
007 * '$Revision: 24234 $'
008 * 
009 * Permission is hereby granted, without written agreement and without
010 * license or royalty fees, to use, copy, modify, and distribute this
011 * software and its documentation for any purpose, provided that the above
012 * copyright notice and the following two paragraphs appear in all copies
013 * of this software.
014 *
015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
019 * SUCH DAMAGE.
020 *
021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
026 * ENHANCEMENTS, OR MODIFICATIONS.
027 *
028 */
029
030package org.kepler.actor.io;
031
032import ptolemy.actor.TypedAtomicActor;
033import ptolemy.actor.TypedIOPort;
034import ptolemy.data.Token;
035import ptolemy.data.expr.Parameter;
036import ptolemy.kernel.CompositeEntity;
037import ptolemy.kernel.util.IllegalActionException;
038import ptolemy.kernel.util.NameDuplicationException;
039import ptolemy.kernel.util.StringAttribute;
040import ptolemy.kernel.util.Workspace;
041
042/**
043 * This actor will replicate a token for each time fire() is called. this is
044 * useful if the director is iterating and you want the same token to be sent
045 * over a port for each iteration.
046 */
047public class ParameterSetter extends TypedAtomicActor {
048        Token t = null;
049        TypedIOPort input;
050        TypedIOPort output;
051        CompositeEntity container;
052        StringAttribute paramName;
053
054        /**
055         * const.
056         */
057        public ParameterSetter() {
058                super();
059                init();
060        }
061
062        /**
063         * const.
064         */
065        public ParameterSetter(CompositeEntity container, String name)
066                        throws IllegalActionException, NameDuplicationException {
067                super(container, name);
068                init();
069        }
070
071        /**
072         * const
073         */
074        public ParameterSetter(Workspace w) {
075                super(w);
076                init();
077        }
078
079        /**
080         * init the ports
081         */
082        private void init() {
083                try {
084                        input = new TypedIOPort(this, "Input", true, false);
085                        output = new TypedIOPort(this, "Output", false, true);
086                        container = (CompositeEntity) getContainer();
087                        paramName = new StringAttribute(this, "Parameter Name");
088                } catch (Exception e) {
089                        input = null;
090                        output = null;
091                        System.out.println("Error adding ports to TokenIterator: "
092                                        + e.getMessage());
093                }
094        }
095
096        /**
097         * fire
098         */
099        public void fire() throws IllegalActionException {
100                // System.out.println("firing");
101                if (t == null) {
102                        // System.out.println("getting token from input");
103                        t = input.get(0);
104                        String paramNameStr = paramName.getExpression();
105                        if (paramNameStr == null || paramNameStr.equals("")) {
106                                throw new IllegalActionException(
107                                                "You must set the parameter name in "
108                                                                + "the config for ParameterSetter.");
109                        }
110                        Parameter p = (Parameter) container.getAttribute(paramNameStr);
111                        if (p == null) {
112                                throw new IllegalActionException("The parameter "
113                                                + paramNameStr
114                                                + " does not exist.  Please create it and try again.");
115                        }
116                        p.setToken(t);
117                }
118                // System.out.println("sending token to output");
119                output.send(0, t);
120        }
121
122}