Package ptolemy.actor.ptalon
Class AbstractPtalonEvaluator
- java.lang.Object
-
- ptolemy.actor.ptalon.AbstractPtalonEvaluator
-
- Direct Known Subclasses:
PtalonEvaluator
public abstract class AbstractPtalonEvaluator extends java.lang.ObjectA helper class to store information, like variable scope info, for the Ptalon compiler. This class manages references to all elements created as a result of parsing a Ptalon file, and whether they have been instantiated in Ptolemy.- Since:
- Ptolemy II 6.1
- Version:
- $Id$
- Author:
- Adam Cataldo, Elaine Cheong
- Pt.AcceptedRating:
- Yellow (celaine)
- Pt.ProposedRating:
- Yellow (celaine)
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected classAbstractPtalonEvaluator.IfTreeThis is a representation of an if/else construct in Ptalon.protected classAbstractPtalonEvaluator.PtalonExpressionScopeFIXME comment
-
Field Summary
Fields Modifier and Type Field Description protected PtalonActor_actorThe actor in which this PtalonCompilerInfo is used.protected AbstractPtalonEvaluator.IfTree_currentIfTreeSome descendent of the root tree to which new input symbols should be added.protected java.util.Hashtable<java.lang.String,java.net.URL>_importsA list of the import symbols and their corresponding files.protected AbstractPtalonEvaluator.PtalonExpressionScope_scopeThe expression scope for this code manager.protected java.util.Map<java.lang.String,TypedIOPort>_transparentRelationsMaps names of transparent relations to ports, which should be multiports.
-
Constructor Summary
Constructors Constructor Description AbstractPtalonEvaluator(PtalonActor actor)Create a new AbstractPtalonEvaluator in the specified PtalonActor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected static java.lang.String_getIndentPrefix(int level)Return a number of spaces that is proportional to the argument.protected int_getTimesEntered()Return the number of times the current if tree has been entered.protected java.lang.String_getType(java.lang.String symbol)Return the type associated with the given symbol in the current scope.protected java.lang.String_getTypeForScope(java.lang.String symbol)Return the type associated with the given symbol in the current scope.protected boolean_inNewWhileIteration()Return true if in a new iteration of a while block.protected boolean_isPreservingTransformation()protected void_processAttributes(NamedObj object)protected void_resetParameters(boolean reset)Set whether to reset parameters when the actor is populated and the parameters already exist for the actor.protected void_setPreservingTransformation(boolean b)voidaddActorParameter(java.lang.String name)Add a PtalonParameter to the PtalonActor with the specified name.voidaddActorParameter(java.lang.String name, java.lang.String expression)Add an invisible PtalonParameter to the PtalonActor with the specified name.voidaddInPort(java.lang.String name)Add a TypedIOPort to the PtalonActor with the specified name, and input flow type.voidaddOutPort(java.lang.String name)Add a TypedIOPort to the PtalonActor with the specified name, and output flow type.voidaddParameter(java.lang.String name)Add a Parameter to the PtalonActor with the specified name.voidaddParameter(java.lang.String name, java.lang.String expression)Add an invisible Parameter to the PtalonActor with the specified name and the given expression as its value.voidaddPort(java.lang.String name)Add a TypedIOPort to the PtalonActor with the specified name.voidaddRelation(java.lang.String name)Add a TypedIORelation to the PtalonActor with the specified name.voidaddSymbol(java.lang.String name, java.lang.String type)Add a symbol with the given name and type to the symbol table at the current level of the if-tree hierarchy.voidaddTransparentRelation(java.lang.String name)Add a transparent relation to the PtalonActor with the specified name.voidassignInternalParameters()Assign any internal parameters in the order they were set.voidenterForScope(java.lang.String scope, PtalonAST forBlock, PtalonPopulator populator)Enter the named for-block subscope.voidenterIfScope(java.lang.String scope)Enter the named subscope.voidenterTransformation(boolean incremental)booleanevaluateBoolean(java.lang.String expression)Evaluate the given expression and return its boolean value.TokenevaluateExpression(java.lang.String expression)Evaluate the given expression and return the corresponding token.voidevaluateForScope()Evaluate the current for block, assuming there is one.java.lang.StringevaluateString(java.lang.String expression)Evaluate the given input expression and return a string representation of it, or null, if there is some reason it cannot be evaluated.voidexitForScope()Exit the current for scope.voidexitIfScope()Exit the current if scope.voidexitTransformation()java.lang.StringgetMappedName(java.lang.String symbol)Get the unique name for the symbol in the PtalonActor.booleanhasUnassignedParameters()Return whether there are unassigned parameters to be handled with theassignInternalParameters()method.booleaninScope(java.lang.String symbol)Return true if the given symbol exists in the current scope.booleanisCreated(java.lang.String symbol)Return true if an entity was created in PtalonActor for the given symbol.booleanisForReady()Return true if the boolean for the current conditional is ready to be entered.booleanisIfReady()Return true if the boolean for the current conditional is ready to be entered.booleanisReady()Return true if the current piece of code is ready to be entered.voidnegateObject(java.lang.String name)voidoptionalObject(java.lang.String name)java.lang.StringpopForStatement()Pop out of the scope of the current for statement and into its container block's scope.java.lang.StringpopIfStatement()Pop out of the scope of the current if statement and into its container block's scope.voidpreserveObject(java.lang.String name)voidpushForStatement(java.lang.String variable, java.lang.String initExpr, java.lang.String satExpr)Push into the scope of a new for statement contained as a sub-block of the current (FIXME: if or for) statement.voidpushIfStatement()Push into the scope of a new if statement contained as a sub-block of the current if statement.voidremoveObject(java.lang.String name)voidsetActiveBranch(boolean branch)Set the active branch for the current if statement.voidsetActorSymbol(java.lang.String symbol)Set the symbol in the PtalonCode which represents this AbstractPtalonEvaluator's actor.voidsetCurrentBranch(boolean branch)Set the current branch that's being walked.voidsetNextExpression(java.lang.String nextExpr)Set the next expression for the current for statement scope, assuming the current scope is a for statement and not an if statement.voidstartAtTop()Prepare the compiler to start at the outermost scope of the Ptalon program during run time.
-
-
-
Field Detail
-
_actor
protected PtalonActor _actor
The actor in which this PtalonCompilerInfo is used.
-
_currentIfTree
protected AbstractPtalonEvaluator.IfTree _currentIfTree
Some descendent of the root tree to which new input symbols should be added.
-
_imports
protected java.util.Hashtable<java.lang.String,java.net.URL> _imports
A list of the import symbols and their corresponding files.
-
_scope
protected AbstractPtalonEvaluator.PtalonExpressionScope _scope
The expression scope for this code manager.
-
_transparentRelations
protected java.util.Map<java.lang.String,TypedIOPort> _transparentRelations
Maps names of transparent relations to ports, which should be multiports. A key may map to null if no port has been assigned to it.
-
-
Constructor Detail
-
AbstractPtalonEvaluator
public AbstractPtalonEvaluator(PtalonActor actor)
Create a new AbstractPtalonEvaluator in the specified PtalonActor.- Parameters:
actor- The actor to manage the code for.
-
-
Method Detail
-
addActorParameter
public void addActorParameter(java.lang.String name) throws PtalonRuntimeExceptionAdd a PtalonParameter to the PtalonActor with the specified name.- Parameters:
name- The name of the parameter.- Throws:
PtalonRuntimeException- If the symbol does not exist, or if the symbol already has a parameter associated with it, or if an IllegalActionException is thrown trying to create the parameter.
-
addActorParameter
public void addActorParameter(java.lang.String name, java.lang.String expression) throws PtalonRuntimeExceptionAdd an invisible PtalonParameter to the PtalonActor with the specified name.- Parameters:
name- The name of the parameter.expression- The expression representing the parameter.- Throws:
PtalonRuntimeException- If the symbol does not exist, or if the symbol already has a parameter associated with it, or if an IllegalActionException is thrown trying to create the parameter.
-
addInPort
public void addInPort(java.lang.String name) throws PtalonRuntimeExceptionAdd a TypedIOPort to the PtalonActor with the specified name, and input flow type.- Parameters:
name- The name of the port.- Throws:
PtalonRuntimeException- If the symbol does not exist, or if the symbol already has a port associated with it, or if an IllegalActionException is thrown trying to create the port.
-
addOutPort
public void addOutPort(java.lang.String name) throws PtalonRuntimeExceptionAdd a TypedIOPort to the PtalonActor with the specified name, and output flow type.- Parameters:
name- The name of the port.- Throws:
PtalonRuntimeException- If the symbol does not exist, or if the symbol already has a port associated with it, or if an IllegalActionException is thrown trying to create the port.
-
addParameter
public void addParameter(java.lang.String name) throws PtalonRuntimeExceptionAdd a Parameter to the PtalonActor with the specified name.- Parameters:
name- The name of the parameter.- Throws:
PtalonRuntimeException- If the symbol does not exist, or if the symbol already has a parameter associated with it, or if an IllegalActionException is thrown trying to create the parameter.
-
addParameter
public void addParameter(java.lang.String name, java.lang.String expression) throws PtalonRuntimeExceptionAdd an invisible Parameter to the PtalonActor with the specified name and the given expression as its value.- Parameters:
name- The name of the parameter.expression- The expression representing the parameter.- Throws:
PtalonRuntimeException- If the symbol does not exist, or if the symbol already has a parameter associated with it, or if an IllegalActionException is thrown trying to create the parameter.
-
addPort
public void addPort(java.lang.String name) throws PtalonRuntimeExceptionAdd a TypedIOPort to the PtalonActor with the specified name.- Parameters:
name- The name of the port.- Throws:
PtalonRuntimeException- If the symbol does not exist, or if the symbol already has a port associated with it, or if an IllegalActionException is thrown trying to create the port.
-
addRelation
public void addRelation(java.lang.String name) throws PtalonRuntimeExceptionAdd a TypedIORelation to the PtalonActor with the specified name.- Parameters:
name- The name of the relation.- Throws:
PtalonRuntimeException- If the symbol does not exist, or if the symbol already has a relation associated with it, or if an IllegalActionException is thrown trying to create the relation.
-
addSymbol
public void addSymbol(java.lang.String name, java.lang.String type) throws PtalonScopeExceptionAdd a symbol with the given name and type to the symbol table at the current level of the if-tree hierarchy.- Parameters:
name- The symbol name.type- The symbol type.- Throws:
PtalonScopeException- If a symbol with this name has already been added somewhere in the current scope.
-
addTransparentRelation
public void addTransparentRelation(java.lang.String name) throws PtalonRuntimeExceptionAdd a transparent relation to the PtalonActor with the specified name. A transparent relation is not the same as a relation. Rather, it provides a means for connecting multiple ports to an input port. It is transparent in that if this transparent relation is connected to an input port I, then any connections made to the transparent relation will be as if they were connected directly to I, instead of through a relation.- Parameters:
name- The name of the relation.- Throws:
PtalonRuntimeException- If in a new for iteration but no containing for block is found.
-
assignInternalParameters
public void assignInternalParameters() throws PtalonRuntimeExceptionAssign any internal parameters in the order they were set.- Throws:
PtalonRuntimeException- If there is any trouble assigning parameter values.
-
enterForScope
public void enterForScope(java.lang.String scope, PtalonAST forBlock, PtalonPopulator populator) throws PtalonRuntimeExceptionEnter the named for-block subscope.- Parameters:
scope- The named subscope.forBlock- The AST for the subscope.populator- The PtalonPopulator that called this statement.- Throws:
PtalonRuntimeException- If the subscope does not exist.
-
enterIfScope
public void enterIfScope(java.lang.String scope) throws PtalonRuntimeExceptionEnter the named subscope.- Parameters:
scope- The named subscope.- Throws:
PtalonRuntimeException- If the subscope does not exist.
-
enterTransformation
public void enterTransformation(boolean incremental) throws PtalonRuntimeException- Throws:
PtalonRuntimeException
-
evaluateBoolean
public boolean evaluateBoolean(java.lang.String expression) throws PtalonRuntimeExceptionEvaluate the given expression and return its boolean value. The expression should return a boolean value, otherwise an exception is thrown.- Parameters:
expression- The expression to evaluate.- Returns:
- The boolean result of evaluation.
- Throws:
PtalonRuntimeException- If the result is not a boolean.
-
evaluateExpression
public Token evaluateExpression(java.lang.String expression) throws PtalonRuntimeException
Evaluate the given expression and return the corresponding token.- Parameters:
expression- The expression to evaluate.- Returns:
- The result of evaluation.
- Throws:
PtalonRuntimeException- If unable to evaluate the expression.
-
evaluateForScope
public void evaluateForScope() throws PtalonRuntimeExceptionEvaluate the current for block, assuming there is one.- Throws:
PtalonRuntimeException- If there is any trouble evaluating this for block.
-
evaluateString
public java.lang.String evaluateString(java.lang.String expression)
Evaluate the given input expression and return a string representation of it, or null, if there is some reason it cannot be evaluated.- Parameters:
expression- The input expression.- Returns:
- The evaluated value, or null if evaluation is not possible.
-
exitForScope
public void exitForScope() throws PtalonRuntimeExceptionExit the current for scope.- Throws:
PtalonRuntimeException- If not in a for-block scope.
-
exitIfScope
public void exitIfScope() throws PtalonRuntimeExceptionExit the current if scope.- Throws:
PtalonRuntimeException- If already at the top-level if scope.
-
exitTransformation
public void exitTransformation() throws PtalonRuntimeException- Throws:
PtalonRuntimeException
-
getMappedName
public java.lang.String getMappedName(java.lang.String symbol) throws PtalonRuntimeExceptionGet the unique name for the symbol in the PtalonActor.- Parameters:
symbol- The symbol to test.- Returns:
- The unique name.
- Throws:
PtalonRuntimeException- If no such symbol exists.
-
hasUnassignedParameters
public boolean hasUnassignedParameters()
Return whether there are unassigned parameters to be handled with theassignInternalParameters()method.- Returns:
- true if there are unassigned parameters, and false otherwise.
-
inScope
public boolean inScope(java.lang.String symbol)
Return true if the given symbol exists in the current scope.- Parameters:
symbol- The symbol to test.- Returns:
- true If the given symbol exists in the current scope.
-
isCreated
public boolean isCreated(java.lang.String symbol) throws PtalonRuntimeExceptionReturn true if an entity was created in PtalonActor for the given symbol. This symbol is assumed to be in the current scope.- Parameters:
symbol- The symbol to test.- Returns:
- true If an entity was created for this symbol.
- Throws:
PtalonRuntimeException- If the symbol is not in the current scope.
-
isForReady
public boolean isForReady() throws PtalonRuntimeExceptionReturn true if the boolean for the current conditional is ready to be entered. It is ready when all ports, parameters, and relations in the containing scope have been created, when all parameters in the containing scope have been assigned values, and when in a branch of an if-block or for-block that is active.- Returns:
- true If the current for-block scope is ready to be entered.
- Throws:
PtalonRuntimeException- If it is thrown trying to access a parameter.
-
isIfReady
public boolean isIfReady() throws PtalonRuntimeExceptionReturn true if the boolean for the current conditional is ready to be entered. It is ready when all ports, parameters, and relations in the containing scope have been created, when all parameters in the containing scope have been assigned values, and when in a branch of an if-block that is active.- Returns:
- true If the current if-block scope is ready to be entered.
- Throws:
PtalonRuntimeException- If it is thrown trying to access a parameter.
-
isReady
public boolean isReady() throws PtalonRuntimeExceptionReturn true if the current piece of code is ready to be entered. This is used by port, parameter, and relation declarations only. It is ready when all ports, parameters, and relations in the containing scope have been created, when all parameters in the containing scope have been assigned values, and when in a branch of an if-block that is active.- Returns:
- true If the current if-block scope is ready to be entered.
- Throws:
PtalonRuntimeException- If it is thrown trying to access a parameter.
-
negateObject
public void negateObject(java.lang.String name) throws PtalonRuntimeException- Throws:
PtalonRuntimeException
-
optionalObject
public void optionalObject(java.lang.String name) throws PtalonRuntimeException- Throws:
PtalonRuntimeException
-
popForStatement
public java.lang.String popForStatement() throws PtalonScopeExceptionPop out of the scope of the current for statement and into its container block's scope.- Returns:
- The unique name of the for-statement block being exited.
- Throws:
PtalonScopeException- If the current scope is already the outermost scope.
-
popIfStatement
public java.lang.String popIfStatement() throws PtalonScopeExceptionPop out of the scope of the current if statement and into its container block's scope.- Returns:
- The unique name of the if-statement block being exited.
- Throws:
PtalonScopeException- If the current scope is already the outermost scope.
-
preserveObject
public void preserveObject(java.lang.String name) throws PtalonRuntimeException- Throws:
PtalonRuntimeException
-
pushForStatement
public void pushForStatement(java.lang.String variable, java.lang.String initExpr, java.lang.String satExpr)Push into the scope of a new for statement contained as a sub-block of the current (FIXME: if or for) statement.- Parameters:
variable- The variable associated with the for statement.initExpr- The expression representing the initial value for the variable.satExpr- The expression evaluated before executing the for statement body.
-
pushIfStatement
public void pushIfStatement()
Push into the scope of a new if statement contained as a sub-block of the current if statement.
-
removeObject
public void removeObject(java.lang.String name) throws PtalonRuntimeException- Throws:
PtalonRuntimeException
-
setActiveBranch
public void setActiveBranch(boolean branch)
Set the active branch for the current if statement.- Parameters:
branch- The branch to set.
-
setActorSymbol
public void setActorSymbol(java.lang.String symbol) throws PtalonScopeExceptionSet the symbol in the PtalonCode which represents this AbstractPtalonEvaluator's actor.- Parameters:
symbol- The name of this actor in the Ptalon file.- Throws:
PtalonScopeException- If the symbol has been added already, or if there is some problem accessing its associated file.
-
setCurrentBranch
public void setCurrentBranch(boolean branch)
Set the current branch that's being walked.- Parameters:
branch- True if the true branch is being walked.
-
setNextExpression
public void setNextExpression(java.lang.String nextExpr)
Set the next expression for the current for statement scope, assuming the current scope is a for statement and not an if statement.- Parameters:
nextExpr- The expression to represent the next statement.
-
startAtTop
public void startAtTop()
Prepare the compiler to start at the outermost scope of the Ptalon program during run time.
-
_getIndentPrefix
protected static java.lang.String _getIndentPrefix(int level)
Return a number of spaces that is proportional to the argument. If the argument is negative or zero, return an empty string.- Parameters:
level- The level of indenting represented by the spaces.- Returns:
- A string with zero or more spaces.
-
_getTimesEntered
protected int _getTimesEntered()
Return the number of times the current if tree has been entered.- Returns:
- The number of times the current if tree has been entered.
-
_getType
protected java.lang.String _getType(java.lang.String symbol) throws PtalonScopeExceptionReturn the type associated with the given symbol in the current scope.- Parameters:
symbol- The symbol under test.- Returns:
- The type associated with the given symbol.
- Throws:
PtalonScopeException- If the symbol is not in the current scope.
-
_getTypeForScope
protected java.lang.String _getTypeForScope(java.lang.String symbol) throws PtalonScopeExceptionReturn the type associated with the given symbol in the current scope. This is the same as getType, but it is used to avoid a name conflict in PtalonEvaluator.PtalonExpressionScope- Parameters:
symbol- The symbol under test.- Returns:
- The type associated with the given symbol.
- Throws:
PtalonScopeException- If the symbol is not in the current scope.
-
_inNewWhileIteration
protected boolean _inNewWhileIteration()
Return true if in a new iteration of a while block.- Returns:
- true If in a new iteration of a while block.
-
_isPreservingTransformation
protected boolean _isPreservingTransformation()
-
_processAttributes
protected void _processAttributes(NamedObj object) throws PtalonRuntimeException
- Parameters:
object-- Throws:
PtalonRuntimeException
-
_resetParameters
protected void _resetParameters(boolean reset)
Set whether to reset parameters when the actor is populated and the parameters already exist for the actor.- Parameters:
reset- Whether to reset parameters.
-
_setPreservingTransformation
protected void _setPreservingTransformation(boolean b)
-
-