ptolemy.domains.fsm.kernel
Class ParseTreeEvaluatorForGuardExpression

java.lang.Object
  extended byptolemy.data.expr.AbstractParseTreeVisitor
      extended byptolemy.data.expr.ParseTreeEvaluator
          extended byptolemy.domains.fsm.kernel.ParseTreeEvaluatorForGuardExpression
All Implemented Interfaces:
ptolemy.data.expr.ParseTreeVisitor

public class ParseTreeEvaluatorForGuardExpression
extends ptolemy.data.expr.ParseTreeEvaluator

A ParseTreeEvaluatorForGuardExpression contains a relation list which is used to indicate the value change of the relations embedded in a guard expression. It extends ParseTreeEvaluator. It is specially designed for guard expressions associated with transitions of FSM. It visits a parse tree and evaluates it into a token.

The ParseTreeEvaluatorForGuardExpression has two operation modes: construction and updat. During construction mode, the relation list expands. During update mode, the relation list doesn't expand but has its element updated.

During the parse tree evaluation, if the visiting node is a leaf node and the evaluated token is a boolean token, or the visiting node is a relational node, the evaluator decides the 'difference' and 'relationType' of the node, and stores these information into the relation list.

The 'difference' of a node is calculated this way:

For a leaf node evaluated as a boolean token, the difference is 0. For a relation node, (scalarLeft relationOperator scalarRight), the difference is the absolute double value of (scalarLeft - scalarRight).

The 'relationType' of a node has 5 different values with meaning: 1: true; 2: false; 3: equal/inequal; 4: less_than: 5: bigger_than. It is calculated this way:

For a leaf node evaluated as a boolean token, the relationType is assigned according to the boolean value of the result token, 1 for true and 2 for false. For a relation node, (scalarLeft relationOperator scalarRight), the relationType depends on the relationOperator. If the relationOperator is '==' or '!=', rhe relationType can be 3 indicating the two scalars equal or not equal, 4 indicating the left scalar is less than the right one, and 5 to indicate left scalar is bigger than the right one. For other kind of relationOperators, the relationType is assigned according to the boolean value of the relation, i.e., 1 for true and 2 for false.

If the evaluator is in construction mode, the node information is added into a relationList, if it is in update mode, the according element of a relationList gets updated.

Note, this evaluator does not use short-circuit evaluation on logical nodes.

Since:
Ptolemy II 4.0
Version:
$Id
Author:
Haiyang Zheng
See Also:
RelationList, ParseTreeEvaluator
Accepted Rating:
Red (hyzheng)
Proposed Rating:
Red (hyzheng)

Field Summary
 
Fields inherited from class ptolemy.data.expr.ParseTreeEvaluator
_evaluatedChildToken
 
Constructor Summary
ParseTreeEvaluatorForGuardExpression(RelationList relationList, double errorTolerance)
          Construct a parse tree evaluator for guard expression.
 
Method Summary
 ptolemy.data.Token evaluateParseTree(ptolemy.data.expr.ASTPtRootNode node, ptolemy.data.expr.ParserScope scope)
          Evaluate the parse tree with the specified root node using the specified scope to resolve the values of variables.
 void setConstructionMode(boolean mode)
          Set the mode of parse tree evaluator.
 void visitLeafNode(ptolemy.data.expr.ASTPtLeafNode node)
          Visit the leaf node.
 void visitLogicalNode(ptolemy.data.expr.ASTPtLogicalNode node)
          Visit the logical node.
 void visitRelationalNode(ptolemy.data.expr.ASTPtRelationalNode node)
          Visit the relation node.
 
Methods inherited from class ptolemy.data.expr.ParseTreeEvaluator
_assert, _evaluateAllChildren, _evaluateArrayIndex, _evaluateChild, _evaluateMatrixIndex, _evaluateParseTree, _functionCall, _methodCall, _trace, _traceEnter, _traceLeave, evaluateParseTree, traceParseTreeEvaluation, visitArrayConstructNode, visitBitwiseNode, visitFunctionalIfNode, visitFunctionApplicationNode, visitFunctionDefinitionNode, visitMatrixConstructNode, visitMethodCallNode, visitPowerNode, visitProductNode, visitRecordConstructNode, visitShiftNode, visitSumNode, visitUnaryNode
 
Methods inherited from class ptolemy.data.expr.AbstractParseTreeVisitor
_unsupportedVisitException, _visitAllChildren, _visitChild, visitAssignmentNode
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ParseTreeEvaluatorForGuardExpression

public ParseTreeEvaluatorForGuardExpression(RelationList relationList,
                                            double errorTolerance)
Construct a parse tree evaluator for guard expression. The relation list is used to store the information of the relation nodes and leaf nodes with boolean tokens. If the relation list is empty, the evaluator is in construction mode, otherwise, it is in update mode.

Parameters:
relationList - The relation list.
errorTolerance - The errorTolerance.
Method Detail

evaluateParseTree

public ptolemy.data.Token evaluateParseTree(ptolemy.data.expr.ASTPtRootNode node,
                                            ptolemy.data.expr.ParserScope scope)
                                     throws ptolemy.kernel.util.IllegalActionException
Evaluate the parse tree with the specified root node using the specified scope to resolve the values of variables.

Parameters:
node - The root of the parse tree.
scope - The scope for evaluation.
Returns:
The result of evaluation.
Throws:
ptolemy.kernel.util.IllegalActionException - If an error occurs during evaluation.

setConstructionMode

public void setConstructionMode(boolean mode)
Set the mode of parse tree evaluator. If the given flag is true, then the relation list will be populated, based on the nodes in the parse tree. If the given flag is false, then the list will simply be updated with new information

Parameters:
mode - The mode of the parse tree evaluator.

visitLeafNode

public void visitLeafNode(ptolemy.data.expr.ASTPtLeafNode node)
                   throws ptolemy.kernel.util.IllegalActionException
Visit the leaf node. It is evaluated the same way as normal parse tree evaluator, except that if the the result is a boolean token, the information about the node (the difference and relationType) is either added into the relation list or used to update the according element in the relation list, depending on the evaluator mode.

Parameters:
node - The leaf node to be evaluated.
Throws:
ptolemy.kernel.util.IllegalActionException - If the super class method visitLeafNode throws the IllegalActionException.

visitLogicalNode

public void visitLogicalNode(ptolemy.data.expr.ASTPtLogicalNode node)
                      throws ptolemy.kernel.util.IllegalActionException
Visit the logical node. This visitor does not use short-circuit evaluation. It evaluates both the nodes beside the logical operator.

Parameters:
node - The logical node to be evaluated.
Throws:
ptolemy.kernel.util.IllegalActionException - If the super class method visitLogicalNode throws the IllegalActionException.

visitRelationalNode

public void visitRelationalNode(ptolemy.data.expr.ASTPtRelationalNode node)
                         throws ptolemy.kernel.util.IllegalActionException
Visit the relation node. The evaluation part is the same as normal parseTreeEvaluator, except that information about each relation (the difference and relationType) is either added into the relation list or used to update the according element in the relation list, depending on the evaluator mode.

Parameters:
node - The relation node to be evaluated.
Throws:
ptolemy.kernel.util.IllegalActionException - If the super class method visitRelationNode throws the IllegalActionException.