ptolemy.domains.fsm.kernel
Class ParseTreeEvaluatorForGuardExpression

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

public class ParseTreeEvaluatorForGuardExpression
extends ParseTreeEvaluator

This class extends the ParseTreeEvaluator class. It is specially designed for guard expressions associated with transitions of FSM. An object of this class is a visitor that visits a parse tree of a guard expression and evaluates it into a token. Meanwhile, this visitor stores the type and difference of all relations of the guard expression into a relation list. Here a relation means an expression that does not contain a logical operator.

This visitor has two modes of operation: construction mode and update mode. During the construction mode, this visitor constructs a relation list where each element of the list corresponds to a relation of the guard expression. The order of the elements is fixed and it is the same as the order that the relations appear in the guard expression. If the guard expression changes, the relation list will be reconstructed. During the update mode, the relation list gets updated only. The order of the elements get updated is the same order the relations of the guard expression get evaluated.

When this visitor evaluates the parse tree, 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 visiting node is treated as a relation. The visitor evaluates the 'difference' and 'relationType' of this relation, and stores the evaluation results into the corresponding element in the relation list.

The 'difference' of a relation is calculated in the following way. For a leaf node evaluated as a boolean token, the difference is 0. This situation corresponds to the "true", or "false", or "x_isPresent" elements in a guard expression. For a relational node with the format (scalarLeft relationOperator scalarRight), the difference is the absolute double value of (scalarLeft - scalarRight). For details of relation type, see RelationType.

If the evaluator is in the construction mode, the relation information is added into the relation list, if it is in the update mode, the corresponding element of the relation List gets updated.

Note, this evaluator does not use short-circuit evaluation on logical nodes, meaning all nodes will be evaluated.

Since:
Ptolemy II 4.0
Version:
$Id: ParseTreeEvaluatorForGuardExpression.java 57040 2010-01-27 20:52:32Z cxh $
Author:
Haiyang Zheng
See Also:
RelationList, ParseTreeEvaluator
Accepted Rating:
Red (hyzheng)
Proposed Rating:
Red (hyzheng)

Field Summary
private  java.util.LinkedList<java.lang.String> _absentDiscreteVariables
           
private  boolean _constructingRelationList
           
private  double _difference
           
private  double _errorTolerance
           
private  int _relationIndex
           
private  RelationList _relationList
           
private  int _relationType
           
private  ParseTreeFreeVariableCollector _variableCollector
           
 
Fields inherited from class ptolemy.data.expr.ParseTreeEvaluator
_evaluatedChildToken, _typeInference
 
Constructor Summary
ParseTreeEvaluatorForGuardExpression(RelationList relationList, double errorTolerance)
          Construct a parse tree evaluator for a guard expression of the given relation list and the error tolerance for evaluation the relations.
 
Method Summary
 Token evaluateParseTree(ASTPtRootNode node, ParserScope scope)
          Evaluate the parse tree with the specified root node using the specified scope to resolve the values of variables.
 RelationList getRelationList()
          Return the list of relations (boolean-valued expressions that do not contain a logical operator, such as comparison expressions) in the guard expression of this transition.
 void setConstructionMode()
          Set parse tree evaluator to be in construction mode.
 void visitLeafNode(ASTPtLeafNode node)
          Visit the leaf node.
 void visitLogicalNode(ASTPtLogicalNode node)
          Visit the logical node.
 void visitRelationalNode(ASTPtRelationalNode node)
          Visit the relation node.
 
Methods inherited from class ptolemy.data.expr.ParseTreeEvaluator
_assert, _evaluateAllChildren, _evaluateArrayIndex, _evaluateChild, _evaluateMatrixIndex, _functionCall, _methodCall, _trace, _traceEnter, _traceLeave, evaluateParseTree, traceParseTreeEvaluation, visitArrayConstructNode, visitBitwiseNode, visitFunctionalIfNode, visitFunctionApplicationNode, visitFunctionDefinitionNode, visitMatrixConstructNode, visitMethodCallNode, visitPowerNode, visitProductNode, visitRecordConstructNode, visitShiftNode, visitSumNode, visitUnaryNode, visitUnionConstructNode
 
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
 

Field Detail

_absentDiscreteVariables

private java.util.LinkedList<java.lang.String> _absentDiscreteVariables

_constructingRelationList

private boolean _constructingRelationList

_difference

private double _difference

_errorTolerance

private double _errorTolerance

_relationIndex

private int _relationIndex

_relationList

private RelationList _relationList

_relationType

private int _relationType

_variableCollector

private ParseTreeFreeVariableCollector _variableCollector
Constructor Detail

ParseTreeEvaluatorForGuardExpression

public ParseTreeEvaluatorForGuardExpression(RelationList relationList,
                                            double errorTolerance)
Construct a parse tree evaluator for a guard expression of the given relation list and the error tolerance for evaluation the relations. The relation stores the information of the relation. After the parse tree evaluator is created, it is always in construction mode.

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

evaluateParseTree

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

Overrides:
evaluateParseTree in class ParseTreeEvaluator
Parameters:
node - The root of the parse tree.
scope - The scope for evaluation.
Returns:
The result of evaluation.
Throws:
IllegalActionException - If an error occurs during evaluation.

getRelationList

public RelationList getRelationList()
Return the list of relations (boolean-valued expressions that do not contain a logical operator, such as comparison expressions) in the guard expression of this transition.

Returns:
The list of relations.

setConstructionMode

public void setConstructionMode()
Set parse tree evaluator to be in construction mode. Clear the relation list and the relation list will be populated, based on the nodes in the parse tree.


visitLeafNode

public void visitLeafNode(ASTPtLeafNode node)
                   throws 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 corresponding element in the relation list, depending on the evaluator mode.

Specified by:
visitLeafNode in interface ParseTreeVisitor
Overrides:
visitLeafNode in class ParseTreeEvaluator
Parameters:
node - The leaf node to be evaluated.
Throws:
IllegalActionException - If the super class method visitLeafNode throws the IllegalActionException.

visitLogicalNode

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

Specified by:
visitLogicalNode in interface ParseTreeVisitor
Overrides:
visitLogicalNode in class ParseTreeEvaluator
Parameters:
node - The logical node to be evaluated.
Throws:
IllegalActionException - If the super class method visitLogicalNode throws the IllegalActionException.

visitRelationalNode

public void visitRelationalNode(ASTPtRelationalNode node)
                         throws 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 corresponding element in the relation list, depending on the evaluator mode.

Specified by:
visitRelationalNode in interface ParseTreeVisitor
Overrides:
visitRelationalNode in class ParseTreeEvaluator
Parameters:
node - The relation node to be evaluated.
Throws:
IllegalActionException - If the super class method visitRelationNode throws the IllegalActionException.