ptolemy.actor.lib.gui
Class InteractiveShell

java.lang.Object
  extended by ptolemy.kernel.util.NamedObj
      extended by ptolemy.kernel.InstantiableNamedObj
          extended by ptolemy.kernel.Entity
              extended by ptolemy.kernel.ComponentEntity
                  extended by ptolemy.actor.AtomicActor
                      extended by ptolemy.actor.TypedAtomicActor
                          extended by ptolemy.actor.lib.gui.InteractiveShell
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, Actor, Executable, FiringsRecordable, Placeable, Initializable, TypedActor, ShellInterpreter, Changeable, Debuggable, DebugListener, Derivable, Instantiable, ModelErrorHandler, MoMLExportable, Moveable, Nameable

public class InteractiveShell
extends TypedAtomicActor
implements Placeable, ShellInterpreter

This actor creates a command shell on the screen, sending commands that are typed by the user to its output port, and reporting strings received at its input by displaying them. Each time it fires, it reads the input, displays it, then displays a command prompt (which by default is ">>"), and waits for a command to be typed. The command is terminated by an enter or return character, which then results in the command being produced on the output. In a typical use of this actor, it will be preceded by a SampleDelay actor which will provide an initial welcome message or instructions. The output will then be routed to some subsystem for processing, and the result will be fed back to the input.

If the user types "quit" or "exit" (without the quotation marks) on the prompt, then this actor's postfire() method will return false. Depending on the domain, this can result in the model execution stopping (in SDF, for example) or in subsequent firings of this actor being skipped (in DE, for example).

Note that because of complexities in Swing, if you resize the display window, then, unlike the plotters, the new size will not be persistent. That is, if you save the model and then re-open it, the new size is forgotten. The position, however, is persistent.

Since:
Ptolemy II 1.0
Version:
$Id: InteractiveShell.java 57046 2010-01-27 23:35:53Z cxh $
Author:
Edward A. Lee
See Also:
Serialized Form
Accepted Rating:
Red (cxh)
Proposed Rating:
Yellow (eal)

Nested Class Summary
 class InteractiveShell.ShellFrame
          The frame that is created by an instance of ShellTableau.
 class InteractiveShell.ShellTableau
          Version of ExpressionShellTableau that records the size of the display when it is closed.
 
Nested classes/interfaces inherited from class ptolemy.kernel.Entity
Entity.ContainedObjectsIterator
 
Field Summary
private  java.awt.Container _container
          Container into which this plot should be placed.
private  boolean _firstTime
          Indicator of the first time through.
private  TableauFrame _frame
          Frame into which plot is placed, if any.
private  java.util.List<java.lang.String> _outputValues
          The list of strings to send to the output.
private  boolean _returnFalseInPostfire
          Flag indicating that "exit" or "quit" has been entered.
private  InteractiveShell.ShellTableau _tableau
          The version of ExpressionShellTableau that creates a Shell window.
private  WindowPropertiesAttribute _windowProperties
           
 TypedIOPort input
          The input port.
 TypedIOPort output
          The output port.
 PortParameter prompt
          The prompt.
 ShellTextArea shell
          The shell window object.
 
Fields inherited from class ptolemy.actor.AtomicActor
_actorFiringListeners, _initializables, _notifyingActorFiring, _stopRequested
 
Fields inherited from class ptolemy.kernel.util.NamedObj
_changeListeners, _changeLock, _changeRequests, _debugging, _debugListeners, _elementName, _isPersistent, _verbose, _workspace, ATTRIBUTES, CLASSNAME, COMPLETE, CONTENTS, DEEP, FULLNAME, LINKS
 
Fields inherited from interface ptolemy.actor.Executable
COMPLETED, NOT_READY, STOP_ITERATING
 
Constructor Summary
InteractiveShell(CompositeEntity container, java.lang.String name)
          Construct an actor with the given container and name.
 
Method Summary
protected  void _exportMoMLContents(java.io.Writer output, int depth)
          Write a MoML description of the contents of this object.
private  void _remove()
          Remove the shell from the current container, if there is one.
 java.lang.Object clone(Workspace workspace)
          Clone the actor into the specified workspace.
 java.lang.String evaluateCommand(java.lang.String command)
          Evaluate the specified command.
 void fire()
          Read and display the input, then wait for user input and produce the user data on the output.
 java.lang.String getOutput()
          Get the output string to be sent.
 void initialize()
          If the shell has not already been created, create it.
 boolean isCommandComplete(java.lang.String command)
          Return true if the specified command is complete (ready to be interpreted).
 void place(java.awt.Container container)
          Specify the container into which this shell should be placed.
 boolean postfire()
          Override the base class to return false if the user has typed "quit" or "exit".
 void setContainer(CompositeEntity container)
          Override the base class to remove the shell from its graphical container if the argument is null.
 void setDisplayName(java.lang.String name)
          Set a name to present to the user.
 void setName(java.lang.String name)
          Set or change the name.
 void setOutput(java.lang.String value)
          Specify an output string to be sent.
 void stop()
          Override the base class to call notifyAll() to get out of any waiting.
 void wrapup()
          Override the base class to make the shell uneditable.
 
Methods inherited from class ptolemy.actor.TypedAtomicActor
_addPort, _fireAt, _fireAt, attributeTypeChanged, clone, newPort, typeConstraintList, typeConstraints
 
Methods inherited from class ptolemy.actor.AtomicActor
_actorFiring, _actorFiring, addActorFiringListener, addInitializable, connectionsChanged, createReceivers, declareDelayDependency, getCausalityInterface, getDirector, getExecutiveDirector, getManager, inputPortList, isFireFunctional, isStrict, iterate, newReceiver, outputPortList, prefire, preinitialize, pruneDependencies, recordFiring, removeActorFiringListener, removeDependency, removeInitializable, stopFire, terminate
 
Methods inherited from class ptolemy.kernel.ComponentEntity
_adjustDeferrals, _checkContainer, _getContainedObject, _propagateExistence, getContainer, instantiate, isAtomic, isOpaque, moveDown, moveToFirst, moveToIndex, moveToLast, moveUp, propagateExistence
 
Methods inherited from class ptolemy.kernel.Entity
_description, _removePort, _validateSettables, connectedPortList, connectedPorts, containedObjectsIterator, getAttribute, getPort, getPorts, linkedRelationList, linkedRelations, portList, removeAllPorts, setClassDefinition, uniqueName
 
Methods inherited from class ptolemy.kernel.InstantiableNamedObj
_setParent, exportMoML, getChildren, getElementName, getParent, getPrototypeList, isClassDefinition, isWithinClassDefinition
 
Methods inherited from class ptolemy.kernel.util.NamedObj
_addAttribute, _adjustOverride, _attachText, _cloneFixAttributeFields, _debug, _debug, _debug, _debug, _debug, _getIndentPrefix, _isMoMLSuppressed, _markContentsDerived, _propagateValue, _recordDecoratedAttributes, _removeAttribute, _splitName, _stripNumericSuffix, addChangeListener, addDebugListener, attributeChanged, attributeList, attributeList, deepContains, depthInHierarchy, description, description, event, executeChangeRequests, exportMoML, exportMoML, exportMoML, exportMoML, exportMoMLPlain, getAttribute, getAttributes, getChangeListeners, getClassName, getDecoratorAttribute, getDecoratorAttributes, getDerivedLevel, getDerivedList, getDisplayName, getFullName, getModelErrorHandler, getName, getName, getSource, handleModelError, isDeferringChangeRequests, isOverridden, isPersistent, lazyContainedObjectsIterator, message, propagateValue, propagateValues, removeChangeListener, removeDebugListener, requestChange, setClassName, setDeferringChangeRequests, setDerivedLevel, setModelErrorHandler, setPersistent, setSource, sortContainedObjects, toplevel, toString, validateSettables, workspace
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface ptolemy.actor.Actor
createReceivers, getCausalityInterface, getDirector, getExecutiveDirector, getManager, inputPortList, newReceiver, outputPortList
 
Methods inherited from interface ptolemy.actor.Executable
isFireFunctional, isStrict, iterate, prefire, stopFire, terminate
 
Methods inherited from interface ptolemy.actor.Initializable
addInitializable, preinitialize, removeInitializable
 
Methods inherited from interface ptolemy.kernel.util.Nameable
description, getContainer, getDisplayName, getFullName, getName, getName
 
Methods inherited from interface ptolemy.kernel.util.Derivable
getDerivedLevel, getDerivedList, propagateValue
 

Field Detail

input

public TypedIOPort input
The input port.


output

public TypedIOPort output
The output port.


prompt

public PortParameter prompt
The prompt. The initial default is the string ">> ". Double quotes are not necessary. If you would like to have no prompt (aka, the empty string), create a Parameter that has the value "" (for example foo) and then set the value of the prompt parameter to $foo.


shell

public ShellTextArea shell
The shell window object.


_container

private java.awt.Container _container
Container into which this plot should be placed.


_firstTime

private boolean _firstTime
Indicator of the first time through.


_frame

private TableauFrame _frame
Frame into which plot is placed, if any.


_outputValues

private java.util.List<java.lang.String> _outputValues
The list of strings to send to the output.


_returnFalseInPostfire

private boolean _returnFalseInPostfire
Flag indicating that "exit" or "quit" has been entered.


_tableau

private InteractiveShell.ShellTableau _tableau
The version of ExpressionShellTableau that creates a Shell window.


_windowProperties

private WindowPropertiesAttribute _windowProperties
Constructor Detail

InteractiveShell

public InteractiveShell(CompositeEntity container,
                        java.lang.String name)
                 throws IllegalActionException,
                        NameDuplicationException
Construct an actor with the given container and name.

Parameters:
container - The container.
name - The name of this actor.
Throws:
IllegalActionException - If the actor cannot be contained by the proposed container.
NameDuplicationException - If the container already has an actor with this name.
Method Detail

clone

public java.lang.Object clone(Workspace workspace)
                       throws java.lang.CloneNotSupportedException
Clone the actor into the specified workspace.

Overrides:
clone in class AtomicActor
Parameters:
workspace - The workspace for the new object.
Returns:
A new actor.
Throws:
java.lang.CloneNotSupportedException - If a derived class has an attribute that cannot be cloned.
See Also:
NamedObj.exportMoML(Writer, int, String), NamedObj.setDeferringChangeRequests(boolean)

evaluateCommand

public java.lang.String evaluateCommand(java.lang.String command)
                                 throws java.lang.Exception
Evaluate the specified command.

Specified by:
evaluateCommand in interface ShellInterpreter
Parameters:
command - The command.
Returns:
The return value of the command, or null if there is none.
Throws:
java.lang.Exception - If something goes wrong processing the command.
See Also:
ShellTextArea.returnResult(String)

fire

public void fire()
          throws IllegalActionException
Read and display the input, then wait for user input and produce the user data on the output. If the user input is "quit" or "exit", then set a flag that causes postfire() to return false.

Specified by:
fire in interface Executable
Overrides:
fire in class AtomicActor
Throws:
IllegalActionException - If producing the output causes an exception.

getOutput

public java.lang.String getOutput()
Get the output string to be sent. This does not return until a value is entered on the shell by the user.

Returns:
The output string to be sent.
See Also:
setOutput(String)

initialize

public void initialize()
                throws IllegalActionException
If the shell has not already been created, create it. Then wait for user input and produce it on the output.

Specified by:
initialize in interface Initializable
Overrides:
initialize in class AtomicActor
Throws:
IllegalActionException - If the parent class throws it.

isCommandComplete

public boolean isCommandComplete(java.lang.String command)
Return true if the specified command is complete (ready to be interpreted).

Specified by:
isCommandComplete in interface ShellInterpreter
Parameters:
command - The command.
Returns:
True.

place

public void place(java.awt.Container container)
Specify the container into which this shell should be placed. This method needs to be called before the first call to initialize(). Otherwise, the shell will be placed in its own frame. The background of the plot is set equal to that of the container (unless it is null).

Specified by:
place in interface Placeable
Parameters:
container - The container into which to place the shell, or null to specify that a new shell should be created.

postfire

public boolean postfire()
                 throws IllegalActionException
Override the base class to return false if the user has typed "quit" or "exit".

Specified by:
postfire in interface Executable
Overrides:
postfire in class AtomicActor
Returns:
False if the user has typed "quit" or "exit".
Throws:
IllegalActionException - If the superclass throws it.

setContainer

public void setContainer(CompositeEntity container)
                  throws IllegalActionException,
                         NameDuplicationException
Override the base class to remove the shell from its graphical container if the argument is null.

Overrides:
setContainer in class AtomicActor
Parameters:
container - The proposed container.
Throws:
IllegalActionException - If the base class throws it.
NameDuplicationException - If the base class throws it.
See Also:
ComponentEntity.getContainer()

setDisplayName

public void setDisplayName(java.lang.String name)
Set a name to present to the user.

If the Plot window has been rendered, then the title of the Plot window will be updated to the value of the name parameter.

Overrides:
setDisplayName in class NamedObj
Parameters:
name - A name to present to the user.
See Also:
NamedObj.getDisplayName()

setName

public void setName(java.lang.String name)
             throws IllegalActionException,
                    NameDuplicationException
Set or change the name. If a null argument is given the name is set to an empty string. Increment the version of the workspace. This method is write-synchronized on the workspace.

If the Plot window has been rendered, then the title of the Plot window will be updated to the value of the name parameter.

Specified by:
setName in interface Nameable
Overrides:
setName in class ComponentEntity
Parameters:
name - The new name.
Throws:
IllegalActionException - If the name contains a period or if the object is a derived object and the name argument does not match the current name.
NameDuplicationException - Not thrown in this base class. May be thrown by derived classes if the container already contains an object with this name.
See Also:
NamedObj.getName(), NamedObj.getName(NamedObj)

setOutput

public void setOutput(java.lang.String value)
Specify an output string to be sent. This method appends the specified string to a queue. Strings are retrieved from the queue by getOutput().

Parameters:
value - An output string to be sent.
See Also:
getOutput()

stop

public void stop()
Override the base class to call notifyAll() to get out of any waiting.

Specified by:
stop in interface Executable
Overrides:
stop in class AtomicActor

wrapup

public void wrapup()
            throws IllegalActionException
Override the base class to make the shell uneditable.

Specified by:
wrapup in interface Initializable
Overrides:
wrapup in class AtomicActor
Throws:
IllegalActionException - If the parent class throws it.

_exportMoMLContents

protected void _exportMoMLContents(java.io.Writer output,
                                   int depth)
                            throws java.io.IOException
Write a MoML description of the contents of this object. This overrides the base class to make sure that the current frame properties, if there is a frame, are recorded.

Overrides:
_exportMoMLContents in class Entity
Parameters:
output - The output stream to write to.
depth - The depth in the hierarchy, to determine indenting.
Throws:
java.io.IOException - If an I/O error occurs.
See Also:
NamedObj.exportMoML(Writer, int)

_remove

private void _remove()
Remove the shell from the current container, if there is one.