org.problets.domain.model.expression
Class Expression

java.lang.Object
  extended by org.problets.domain.model.DomainModel
      extended by org.problets.domain.model.expression.Expression
All Implemented Interfaces:
java.lang.Cloneable
Direct Known Subclasses:
StandAloneExpression

public class Expression
extends DomainModel

Expression Creates an expression model from a string representation. Works efficiently even for trivial expressions

Author:
Joshua Go, Amruth Kumar

Field Summary
static int IDENTITY
           
static int MAX_PRECEDENCE_LEVELS
           
 
Fields inherited from class org.problets.domain.model.DomainModel
BOOLEAN, BREAKPOINT, CHARACTER, DOUBLE_PRECISION_REAL, ERROR_TYPE, EVAL_STATUS, expACTION, expASSIGNMENT, expAUTO_DEALLOCATION, expCALL, expCONDITION, expDECLARATION, expDEREFERENCING, expDYNAMIC_ALLOCATION, expDYNAMIC_ALLOCATION_INITIALIZATION, expDYNAMIC_DEALLOCATION, expELSE_CLAUSE, expERROR, expEXECUTION, expIF_CLAUSE, expINITIALIZATION, expINPUT, expPARAMETER_PASSING, expPRINT, expQUESTIONABLE, expREFERENCING, expRETURN, expRUNTIME_ERROR, expSEMANTIC_ERROR, expSYNTAX_ERROR, expUPDATE, MAX_OBJECT_IDENTITY, MIN_OBJECT_IDENTITY, QUADRUPLE_PRECISION_REAL, RUNTIME_ERROR, SEMANTIC_ERROR, SIGNED_INTEGER, SIGNED_LONG, SIGNED_SHORT, SINGLE_PRECISION_REAL, SYNTAX_ERROR, UNDEFINED_TYPE, UNSIGNED_INTEGER, UNSIGNED_LONG, UNSIGNED_SHORT, WARNING
 
Constructor Summary
Expression(NodeModel inRoot)
          Creates an expression of the given pre-constructed tree - used by ParenthesizedStudentNodeView to create a copy of the expression within parentheses
Expression(java.lang.String inExpression)
          Constructor for expressions involving only literal constants in standalone expressions, OR expressions created by getCode() method for printing purposes only (not evaluation)
Expression(java.lang.String inExpression, ActivationRecord inActivationRecord)
          Constructor for expressions involving variables also
 
Method Summary
 boolean containsOperator(java.lang.String operatorSymbol)
          Returns true if the expression contains the operator, and false otherwise - Used by ExpressionViewExpert to generate explanation that LHS of &&/|| is evaluated before RHS.
 Value evaluate()
          Meant to be used by code which wants only the expression to be evaluated and not any associated Scope Call Scope.preProcessFunctionCalls() before calling this, in order to handle embedded function calls
 Value evaluate(RunEnv runEnv, Behavior behavior, int codeLineNumber, boolean stepFlag)
          Called by expressions in code - ensures function calls are properly handled
 void explainObject(Behavior behavior, int lineNumber)
          This must be called after the expession is evaluated first.
 void explainPrinting(RunEnv runEnv, Behavior behavior, java.lang.String statement, int event, int lineNumber, boolean abbreviate)
          Method to explain expression in an output statement - assume this happens only in code, not standalone
 void explainProcess(RunEnv runEnv, Behavior behavior, java.lang.String statement, int event, int lineNumber, boolean abbreviate)
          Explains the execution of expressions in CODE, not standalone expressions.
 ActivationRecord getActivationRecord()
          Returns a reference to the activation record for this expression if one exists
 void getCode(java.util.Vector codeText, java.lang.String statement, int lineNumber)
          Adds the code for the expression to codeText and adds variables in the expression to the line objects for the line
 java.util.Vector getExpressionVariables()
          Returns all the variables that participate in this expression so that they can be added as line objects for this line of code - when variables are initialized, in conditions of if/if-else and loops
 java.util.Vector getFunctionCalls()
          Returns all the function calls in an expression as a vector of FunctionCallOperands
 int getNumOperators()
          Returns the number of operators in this expression
 Operator getOperatorFromSymbol(java.lang.String currentToken, boolean prefixFlag)
          Given an operator symbol, returns the corresponding operator object
 java.lang.String getOperatorListString()
          Returns a string representation of the operators in an expression, including operators in embedded parentheses
 java.util.Vector getOperatorPrecedenceTable()
          Returns a Vector representing the precedence table of the operators for this expression - called by ExpressionTutor.explain().
static java.lang.String[] getPossibleErrors()
          Method to return errors for variables May want to post-process based on Student Model
 java.util.Vector getPreliminaryCode()
          Method that returns variable declarations that precede an expression as a vector of strings (not CodeLine objects)
 java.lang.String getProcessedExpression()
          Returns the expression after replacing < with <<> for proper printing in code and explanation when the text is printed formatted
 NodeModel getRoot()
           
 java.lang.String getType()
          Returns the default data type - the returned value is reliable if the expression contains at least one variable - the default type is taken to be that of the variable
static boolean grade(java.lang.String actualTemplate, java.lang.String studentCode)
          Grades studentCode according to actualTemplate and inserts explanation of grading into explanation
 boolean isArithmetic()
          Returns true if this is an arithmetic expression, and false otherwise
 boolean isAssignment()
          Returns true if this is an assignment expression, and false otherwise
 boolean isBitwise()
          Returns true if this is a bit-wise expression, and false otherwise
 boolean isConstant()
           
static boolean isLiteralExpression(java.lang.String studentCode)
          Method returns whether the studentCode is a literal expression that does not involve any variables, function calls, etc.
 boolean isLogical()
          Returns true if this is a logical expression, and false otherwise
 boolean isRelational()
          Returns true if this is a relational expression, and false otherwise
static boolean isValidOperator(java.lang.String operator)
           
 boolean isVariable()
           
static void main(java.lang.String[] args)
           
 java.lang.String printExpression()
          Returns the expression - appropriate for expression evaluation tutors where large whitespace is desired between operands
static void sortExplanation(java.util.Vector explanation)
           
static java.util.Vector tokenize(java.lang.String statement)
           
 java.lang.String toString()
          Returns the expression - appropriate for printing expressions in explanation - not extra whitespaces to distract the reader
 
Methods inherited from class org.problets.domain.model.DomainModel
clone, getId, getIdName
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

MAX_PRECEDENCE_LEVELS

public static final int MAX_PRECEDENCE_LEVELS
See Also:
Constant Field Values

IDENTITY

public static final int IDENTITY
See Also:
Constant Field Values
Constructor Detail

Expression

public Expression(java.lang.String inExpression)
Constructor for expressions involving only literal constants in standalone expressions, OR expressions created by getCode() method for printing purposes only (not evaluation)


Expression

public Expression(java.lang.String inExpression,
                  ActivationRecord inActivationRecord)
Constructor for expressions involving variables also


Expression

public Expression(NodeModel inRoot)
Creates an expression of the given pre-constructed tree - used by ParenthesizedStudentNodeView to create a copy of the expression within parentheses

Method Detail

tokenize

public static java.util.Vector tokenize(java.lang.String statement)

isValidOperator

public static boolean isValidOperator(java.lang.String operator)

getOperatorFromSymbol

public Operator getOperatorFromSymbol(java.lang.String currentToken,
                                      boolean prefixFlag)
Given an operator symbol, returns the corresponding operator object


isVariable

public boolean isVariable()

isConstant

public boolean isConstant()

getRoot

public NodeModel getRoot()

getPossibleErrors

public static java.lang.String[] getPossibleErrors()
Method to return errors for variables May want to post-process based on Student Model


getActivationRecord

public ActivationRecord getActivationRecord()
Returns a reference to the activation record for this expression if one exists


getOperatorListString

public java.lang.String getOperatorListString()
Returns a string representation of the operators in an expression, including operators in embedded parentheses


getNumOperators

public int getNumOperators()
Returns the number of operators in this expression


getOperatorPrecedenceTable

public java.util.Vector getOperatorPrecedenceTable()
Returns a Vector representing the precedence table of the operators for this expression - called by ExpressionTutor.explain().


isArithmetic

public boolean isArithmetic()
Returns true if this is an arithmetic expression, and false otherwise


isRelational

public boolean isRelational()
Returns true if this is a relational expression, and false otherwise


isAssignment

public boolean isAssignment()
Returns true if this is an assignment expression, and false otherwise


isLogical

public boolean isLogical()
Returns true if this is a logical expression, and false otherwise


isBitwise

public boolean isBitwise()
Returns true if this is a bit-wise expression, and false otherwise


containsOperator

public boolean containsOperator(java.lang.String operatorSymbol)
Returns true if the expression contains the operator, and false otherwise - Used by ExpressionViewExpert to generate explanation that LHS of &&/|| is evaluated before RHS. Will not work correctly for postfix inc/dec operators


getExpressionVariables

public java.util.Vector getExpressionVariables()
Returns all the variables that participate in this expression so that they can be added as line objects for this line of code - when variables are initialized, in conditions of if/if-else and loops


getType

public java.lang.String getType()
Returns the default data type - the returned value is reliable if the expression contains at least one variable - the default type is taken to be that of the variable


getFunctionCalls

public java.util.Vector getFunctionCalls()
Returns all the function calls in an expression as a vector of FunctionCallOperands


getCode

public void getCode(java.util.Vector codeText,
                    java.lang.String statement,
                    int lineNumber)
Adds the code for the expression to codeText and adds variables in the expression to the line objects for the line


getPreliminaryCode

public java.util.Vector getPreliminaryCode()
Method that returns variable declarations that precede an expression as a vector of strings (not CodeLine objects)


evaluate

public Value evaluate()
Meant to be used by code which wants only the expression to be evaluated and not any associated Scope Call Scope.preProcessFunctionCalls() before calling this, in order to handle embedded function calls


evaluate

public Value evaluate(RunEnv runEnv,
                      Behavior behavior,
                      int codeLineNumber,
                      boolean stepFlag)
Called by expressions in code - ensures function calls are properly handled


grade

public static boolean grade(java.lang.String actualTemplate,
                            java.lang.String studentCode)
Grades studentCode according to actualTemplate and inserts explanation of grading into explanation


isLiteralExpression

public static boolean isLiteralExpression(java.lang.String studentCode)
Method returns whether the studentCode is a literal expression that does not involve any variables, function calls, etc.


explainProcess

public void explainProcess(RunEnv runEnv,
                           Behavior behavior,
                           java.lang.String statement,
                           int event,
                           int lineNumber,
                           boolean abbreviate)
Explains the execution of expressions in CODE, not standalone expressions. Call Scope.preProcessFunctionCalls() before calling this, in order to handle embedded function calls.


explainPrinting

public void explainPrinting(RunEnv runEnv,
                            Behavior behavior,
                            java.lang.String statement,
                            int event,
                            int lineNumber,
                            boolean abbreviate)
Method to explain expression in an output statement - assume this happens only in code, not standalone


explainObject

public void explainObject(Behavior behavior,
                          int lineNumber)
This must be called after the expession is evaluated first. The method explains the individual steps in the execution of the expression. If the expression evaluated correctly, it explains each step. If it did not, the method explains only up to the first incorrect operator. This method is called by ExpressionTutor.explain();


sortExplanation

public static void sortExplanation(java.util.Vector explanation)

main

public static void main(java.lang.String[] args)

getProcessedExpression

public java.lang.String getProcessedExpression()
Returns the expression after replacing < with <<> for proper printing in code and explanation when the text is printed formatted


printExpression

public java.lang.String printExpression()
Returns the expression - appropriate for expression evaluation tutors where large whitespace is desired between operands


toString

public java.lang.String toString()
Returns the expression - appropriate for printing expressions in explanation - not extra whitespaces to distract the reader

Overrides:
toString in class java.lang.Object