001/* An actor whose execution is specified by a Jython script.
002
003Copyright (c) 2013 The Regents of the University of California.
004All rights reserved.
005Permission is hereby granted, without written agreement and without
006license or royalty fees, to use, copy, modify, and distribute this
007software and its documentation for any purpose, provided that the above
008copyright notice and the following two paragraphs appear in all copies
009of this software.
010
011IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
012FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
013ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
014THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
015SUCH DAMAGE.
016
017THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
018INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
019MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
020PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
021CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
022ENHANCEMENTS, OR MODIFICATIONS.
023
024*/
025package org.kepler.scriptengine;
026
027import javax.script.ScriptException;
028
029import ptolemy.kernel.CompositeEntity;
030import ptolemy.kernel.util.Attribute;
031import ptolemy.kernel.util.IllegalActionException;
032import ptolemy.kernel.util.NameDuplicationException;
033import ptolemy.kernel.util.StringAttribute;
034import ptolemy.kernel.util.Workspace;
035
036/** An actor whose execution is controlled by a Jython script.
037 *
038 *  The purpose of this actor is to check backwards-compatibility
039 *  with the Ptolemy Python actor.
040 * 
041 *  @see ptolemy.actor.lib.python.PythonScript
042 * 
043 *  @author Daniel Crawl
044 *  @version $Id: Jython.java 33884 2015-09-11 18:07:23Z crawl $
045 */
046public class Jython extends ScriptEngineActor {
047
048    /** Construct a new Jython for a specified workspace. */
049        public Jython(Workspace workspace) {
050        super(workspace);
051    }
052
053    /** Construct a new Jython with the given container and name. */
054        public Jython(CompositeEntity container, String name)
055            throws IllegalActionException, NameDuplicationException {
056        super(container, name);
057        
058        // the ptolemy PythonScript actor uses "Main" for the class name.
059        actorClassName.setExpression("Main");
060        
061        _editorFactory.syntaxStyle.setExpression("text/python");
062
063        language.setToken("python");
064        
065        script.setExpression("class Main:\n" +
066                        "  \"hello\"\n" +
067                        "  def fire(self):\n" +
068                        "    print(\"in fire\")\n");
069    }
070
071        /** React to changes in by attributes named "jythonClassName". 
072         *  Changes in other attributes are handled by parent classes.
073         */
074    @Override
075    public void attributeChanged(Attribute attribute) throws IllegalActionException {
076     
077        // the ptolemy PythonScript actor uses a parameter named "jythonClassName"
078        // to hold the class name in the script.
079        if(attribute.getName().equals("jythonClassName") &&
080                (attribute instanceof StringAttribute)) {
081            actorClassName.setToken(((StringAttribute)attribute).getExpression());
082        } else {
083            super.attributeChanged(attribute);
084        }
085        
086    }
087    
088    ///////////////////////////////////////////////////////////////////
089    ////                         protected methods                 ////
090
091    /** Create an instance of the actor object in the script.
092     *  @return the actor instance.
093     */
094    @Override
095    protected Object _createActorInstance(String actorClassNameStr) throws ScriptException {
096        _engine.eval(_ACTOR_INSTANCE_NAME + " = " + actorClassNameStr + "()");
097        return _engine.get(_ACTOR_INSTANCE_NAME);
098    }  
099
100    /** Put the given object to the actor instance in the script.
101     *  @param name the name of the object to put.
102     *  @param object the object to put.
103     */
104    @Override
105    protected void _putObjectToActorInstance(String name, Object object) throws ScriptException {
106        String globalName = "_yyy_" + name;
107        _engine.put(globalName, object);
108        _engine.eval(_ACTOR_INSTANCE_NAME + "." + name + " = " + globalName);
109    }
110}