org.qtitools.qti.node.expression
Class AbstractExpression

java.lang.Object
  extended by org.qtitools.qti.node.AbstractNode
      extended by org.qtitools.qti.node.AbstractObject
          extended by org.qtitools.qti.node.expression.AbstractExpression
All Implemented Interfaces:
java.io.Serializable, Expression, ExpressionParent, XmlNode, XmlObject, Validatable
Direct Known Subclasses:
And, AnyN, BaseValue, ContainerSize, Contains, CustomOperator, Delete, Divide, DurationGte, DurationLt, Equal, EqualRounded, FieldValue, Gt, Gte, Index, Inside, IntegerDivide, IntegerModulus, IntegerToFloat, IsNull, ItemSubset, LookupExpression, Lt, Lte, MapResponse, MapResponsePoint, Match, Member, Multiple, Not, Null, Or, Ordered, PatternMatch, Power, Product, RandomExpression, RecordEx, Round, StringMatch, Substring, Subtract, Sum, Truncate

public abstract class AbstractExpression
extends AbstractObject
implements Expression

Abstract super class for all expressions.

Author:
Jiri Kajaba
See Also:
Cardinality, BaseType, Serialized Form

Field Summary
protected static org.slf4j.Logger logger
          Expression logger.
 
Fields inherited from interface org.qtitools.qti.node.expression.Expression
DISPLAY_NAME
 
Fields inherited from interface org.qtitools.qti.node.XmlNode
INDENT, NEW_LINE, SIMPLE_NAME_INNER_SEPARATOR, SIMPLE_NAMES_SEPARATOR
 
Constructor Summary
AbstractExpression(ExpressionParent parent)
          Constructs expression.
 
Method Summary
 Value evaluate()
          Evaluates this expression.
protected abstract  Value evaluateSelf(int depth)
          Evaluates this expression.
 BaseType getBaseType()
          Gets baseType of evaluated result.
 Cardinality getCardinality()
          Gets cardinality of evaluated result.
 java.util.List<Expression> getChildren()
          Gets all children of this expression.
protected  Expression getFirstChild()
          Returns first subexpression.
 ExpressionParent getParent()
          Gets parent of this node or null (if node is root; for example AssessmentTest).
protected  BaseType[] getParentRequiredBaseTypes()
          Gets list of all acceptable baseTypes for this expression from its parent.
protected  Cardinality[] getParentRequiredCardinalities()
          Gets list of all acceptable cardinalities for this expression from its parent.
 BaseType[] getProducedBaseTypes()
          Gets list of all possible produced baseTypes after evaluation (possible baseTypes of evaluated result).
 Cardinality[] getProducedCardinalities()
          Gets list of all possible produced cardinalities after evaluation (possible cardinalities of evaluated result).
protected  BaseType[] getProducedNumericalBaseTypes()
          Gets list of all possible produced baseTypes after evaluation (possible baseTypes of evaluated result).
 BaseType[] getRequiredBaseTypes(int index)
          Gets list of all acceptable baseTypes which can child expression at given position produce.
 Cardinality[] getRequiredCardinalities(int index)
          Gets list of all acceptable cardinalities which can child expression at given position produce.
protected  BaseType[] getRequiredSameBaseTypes(int index, boolean includeParent)
          Gets list of all acceptable baseTypes which can child expression at given position produce.
protected  Cardinality[] getRequiredSameCardinalities(int index, boolean includeParent)
          Gets list of all acceptable cardinalities which can child expression at given position produce.
protected  Expression getSecondChild()
          Returns second subexpression.
 ExpressionType getType()
          Gets expression type of this expression.
 Value getValue()
          Gets evaluated result or null if this expression is not evaluated yet.
protected  boolean isAnyChildNull()
          Returns true if any subexpression is NULL; false otherwise.
 boolean isNull()
          Returns true if evaluated result of this expression is NULL; false otherwise.
 boolean isVariable()
          Returns true if value of evaluation can change every evaluation call; false otherwise.
 void reset()
          Resets this expression and all its children state (set same state like before first evaluation).
 java.lang.String toString()
           
 ValidationResult validate()
          Validates this object.
 
Methods inherited from class org.qtitools.qti.node.AbstractObject
getParentItem, getParentResult, getParentTest, setParent
 
Methods inherited from class org.qtitools.qti.node.AbstractNode
attrToXmlString, bodyToXmlString, getAttributes, getClassTag, getFullName, getIndent, getNode, getNode, getNode, getNodeGroups, getParentRoot, getSimpleName, getSourceFile, getSourceNode, getSourceString, getSourceUrl, hasChildNodes, load, load, load, load, loadAttributes, readChild, readChildren, setParent, setSourceFile, setSourceNode, setSourceString, setSourceUrl, toXmlString, toXmlString, validateAttributes, validateChildren
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.qtitools.qti.node.XmlObject
getParentItem, getParentResult, getParentTest, setParent
 
Methods inherited from interface org.qtitools.qti.node.XmlNode
getAttributes, getClassTag, getFullName, getNodeGroups, getParentRoot, getSimpleName, getSourceFile, getSourceNode, getSourceString, getSourceUrl, hasChildNodes, load, load, load, load, setSourceFile, setSourceNode, setSourceString, setSourceUrl, toXmlString, toXmlString
 

Field Detail

logger

protected static org.slf4j.Logger logger
Expression logger. Used with all expressions.

Constructor Detail

AbstractExpression

public AbstractExpression(ExpressionParent parent)
Constructs expression.

Parameters:
parent - parent of this expression
Method Detail

getParent

public ExpressionParent getParent()
Description copied from interface: XmlNode
Gets parent of this node or null (if node is root; for example AssessmentTest).

While testing some nodes (for example expressions) don't have properly set parent, but it is usable only for testing. (Some nodes cannot exists without parent even for testing).

Specified by:
getParent in interface Expression
Specified by:
getParent in interface XmlNode
Specified by:
getParent in interface XmlObject
Overrides:
getParent in class AbstractObject
Returns:
parent of this node or null (if node is root; for example AssessmentTest)

getType

public ExpressionType getType()
Description copied from interface: Expression
Gets expression type of this expression.

Specified by:
getType in interface Expression
Returns:
expression type of this expression

isVariable

public boolean isVariable()
Description copied from interface: Expression
Returns true if value of evaluation can change every evaluation call; false otherwise. It checks this expression and all of its children (if any child is variable, this expression becomes variable too). Example of variable expressions: variable and outcome expressions.

Specified by:
isVariable in interface Expression
Returns:
true if value of evaluation can change every evaluation call; false otherwise

getRequiredCardinalities

public Cardinality[] getRequiredCardinalities(int index)
Description copied from interface: ExpressionParent
Gets list of all acceptable cardinalities which can child expression at given position produce.

For example delete expression returns single cardinality for index 0 and list of multiple and ordered cardinality for index 1.

Result of this method can change in time.

Static example is expression or. Expression or accepts only single cardinality for any index.

Dynamic example is expression match. Expression match accepts any cardinality of its children, but this cardinality must be same for all its children.

Specified by:
getRequiredCardinalities in interface ExpressionParent
Parameters:
index - position of child expression in this parent
Returns:
list of all possible cardinalities which can child expression at given position produce

getRequiredSameCardinalities

protected Cardinality[] getRequiredSameCardinalities(int index,
                                                     boolean includeParent)
Gets list of all acceptable cardinalities which can child expression at given position produce.

This method is used when same cardinality is required (contains, match).

Parameters:
index - position of child expression in this parent
includeParent - whether parent requirements should be used during calculation
Returns:
list of all acceptable cardinalities which can child expression at given position produce
See Also:
getRequiredCardinalities(int)

getRequiredBaseTypes

public BaseType[] getRequiredBaseTypes(int index)
Description copied from interface: ExpressionParent
Gets list of all acceptable baseTypes which can child expression at given position produce.

Result of this method can change in time.

Static example is expression or. Expression or accepts only boolean baseType for any index.

Dynamic example is expression match. Expression match accepts any baseType of its children. but this baseType must be same for all its children.

Specified by:
getRequiredBaseTypes in interface ExpressionParent
Parameters:
index - position of child expression in this parent
Returns:
list of all acceptable baseTypes which can child expression at given position produce

getRequiredSameBaseTypes

protected BaseType[] getRequiredSameBaseTypes(int index,
                                              boolean includeParent)
Gets list of all acceptable baseTypes which can child expression at given position produce.

This method is used when same baseType is required (contains, delete, index, match, ...).

Parameters:
index - position of child expression in this parent
includeParent - whether parent requirements should be used during calculation
Returns:
list of all acceptable baseTypes which can child expression at given position produce
See Also:
getRequiredBaseTypes(int)

getProducedCardinalities

public Cardinality[] getProducedCardinalities()
Description copied from interface: Expression
Gets list of all possible produced cardinalities after evaluation (possible cardinalities of evaluated result). Result of this method can change in time.

Static example is expression or. Expression or can produce only single cardinality and it cannot change.

Dynamic example is expression variable. Expression variable can produce any cardinality before evaluation. After evaluation it produces cardinality of its result (and it can change every evaluation call!).

Null expression, empty containers (multiple, ordered, record), or NULL values produces all cardinalities. So they are compatible with anything.

Specified by:
getProducedCardinalities in interface Expression
Returns:
list of all possible produced cardinalities after evaluation

getProducedBaseTypes

public BaseType[] getProducedBaseTypes()
Description copied from interface: Expression
Gets list of all possible produced baseTypes after evaluation (possible baseTypes of evaluated result). Result of this method can change in time.

Static example is expression or. Expression or can produce only boolean baseType and it cannot change.

Dynamic example is expression variable. Expression variable can produce any baseType before evaluation. After evaluation it produces baseType of its result (and it can change every evaluation call!).

Null expression, empty containers (multiple, ordered, record), or NULL values produces all baseTypes. So they are compatible with anything.

Specified by:
getProducedBaseTypes in interface Expression
Returns:
list of all possible produced baseTypes after evaluation

getProducedNumericalBaseTypes

protected BaseType[] getProducedNumericalBaseTypes()
Gets list of all possible produced baseTypes after evaluation (possible baseTypes of evaluated result).

This method is used in numerical expressions (product, subtract, sum).

  1. if any of children doesn't produce integer nor float, result is empty set
  2. if one of children produces only float, result is float
  3. if none of all children produces float, result is integer
  4. otherwise result is set of integer and float

Returns:
list of all possible produced baseTypes after evaluation (possible baseTypes of evaluated result)
See Also:
getProducedBaseTypes()

getParentRequiredCardinalities

protected Cardinality[] getParentRequiredCardinalities()
Gets list of all acceptable cardinalities for this expression from its parent.
  1. evaluates index of this expression in parent
  2. calls parent's getRequiredCardinalities(index) method
If this expression doesn't have any parent (it is legal for testing, but not for real use case), returns list of all cardinalities.

Returns:
list of all acceptable cardinalities for this expression from its parent

getParentRequiredBaseTypes

protected BaseType[] getParentRequiredBaseTypes()
Gets list of all acceptable baseTypes for this expression from its parent.
  1. evaluates index of this expression in parent
  2. calls parent's getRequiredBaseTypes(index) method
If this expression doesn't have any parent (it is legal for testing, but not for real use case), returns list of all baseTypes.

Returns:
list of all acceptable baseTypes for this expression from its parent

validate

public ValidationResult validate()
Description copied from interface: Validatable
Validates this object.

Specified by:
validate in interface Validatable
Overrides:
validate in class AbstractNode
Returns:
validation result

getChildren

public java.util.List<Expression> getChildren()
Description copied from interface: Expression
Gets all children of this expression.

Specified by:
getChildren in interface Expression
Returns:
all children of this expression

isAnyChildNull

protected boolean isAnyChildNull()
Returns true if any subexpression is NULL; false otherwise.

Returns:
true if any subexpression is NULL; false otherwise

getFirstChild

protected Expression getFirstChild()
Returns first subexpression. This is convenient method only. Use this method instead of getChildren().get(0).

Returns:
first subexpression

getSecondChild

protected Expression getSecondChild()
Returns second subexpression. This is convenient method only. Use this method instead of getChildren().get(1).

Returns:
second subexpression

reset

public void reset()
Description copied from interface: Expression
Resets this expression and all its children state (set same state like before first evaluation).

Specified by:
reset in interface Expression

evaluate

public Value evaluate()
Description copied from interface: Expression
Evaluates this expression.
  1. evaluates all subexpressions (calls evaluate method for every subexpression)
  2. validates this expression (calls validate method)
  3. evaluates this expression (calls evaluateSelf method)

Specified by:
evaluate in interface Expression
Returns:
result of evaluation

evaluateSelf

protected abstract Value evaluateSelf(int depth)
Evaluates this expression. All children must be already evaluated. Contains no checks.

Parameters:
depth - depth of this expression in expression tree (root's depth = 0)
Returns:
result of evaluation

isNull

public boolean isNull()
               throws java.lang.NullPointerException
Description copied from interface: Expression
Returns true if evaluated result of this expression is NULL; false otherwise.

Specified by:
isNull in interface Expression
Returns:
true if evaluated result of this expression is NULL; false otherwise
Throws:
java.lang.NullPointerException - if this expression is not evaluated yet

getCardinality

public Cardinality getCardinality()
                           throws java.lang.NullPointerException
Description copied from interface: Expression
Gets cardinality of evaluated result.

Specified by:
getCardinality in interface Expression
Returns:
cardinality of evaluated result
Throws:
java.lang.NullPointerException - if this expression is not evaluated yet

getBaseType

public BaseType getBaseType()
                     throws java.lang.NullPointerException
Description copied from interface: Expression
Gets baseType of evaluated result.

Specified by:
getBaseType in interface Expression
Returns:
baseType of evaluated result
Throws:
java.lang.NullPointerException - if this expression is not evaluated yet

getValue

public Value getValue()
               throws java.lang.NullPointerException
Description copied from interface: Expression
Gets evaluated result or null if this expression is not evaluated yet.

Specified by:
getValue in interface Expression
Returns:
evaluated result or null if this expression is not evaluated yet
Throws:
java.lang.NullPointerException

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object


Copyright © 2009. All Rights Reserved.