cdx.scorebot.scoring.logic
Class Context

java.lang.Object
  extended by cdx.scorebot.scoring.logic.Context

public class Context
extends java.lang.Object

stores information about the current state when processing a scoring rule's logic

Author:
David Underhill

Constructor Summary
Context(ScoringRule rule)
          instantiates a Context to store state for the specified rule (and its needed test results)
 
Method Summary
protected  void addVariable(Identifier iden, Literal value)
          adds a variable to the context
protected  void bonus(int bonus, java.lang.String title, java.lang.String msg)
          creates a test score event with the specified bonus (will be reported)
protected  void bonus(int bonus, java.lang.String title, java.lang.String msg, boolean known)
          creates a test score event with the specified bonus
protected  boolean compareDates(java.lang.String date1, OperatorType booleanOp, java.lang.String date2)
          Compare two dates with the specified boolean operator.
protected  int count(java.lang.String testName, int numResultsToLookAt, java.lang.String resultName, OperatorType operator, Literal operand2)
          counts the number of a times a given result meets a given condition for some number of iterations of the test.
protected  void echo(java.lang.String msg)
          prints the specified message to standard out
protected  void echoErr(java.lang.String errMsg)
          prints the specified message to standard error
protected  int floor(Literal literal)
          Convert a double to a floored integer.
protected  Literal getTestOptionValue(java.lang.String testName, int testNum, java.lang.String optionName)
          gets the value of a test option
protected  Literal getTestResultValue(java.lang.String testName, int testNum, java.lang.String resultName)
          gets the value of a test result
 java.util.Vector<TestScoreEvent> getTestScoreEvents()
          gets any test score events generated by in this context
protected  Literal getVariableValue(java.lang.String varName)
          gets the specified variable; if a variable with the specified name isn't found, then a UndefinedVariableException will be thrown.
 boolean hasAllNeededResults()
          determines whether or not all TestNumPairs are fully populated
static boolean mayAssign(LiteralType type, LiteralType valueType)
          Returns true if the specified value type may be assigned to the specified type.
 void penalty(int penalty, java.lang.String title, java.lang.String msg)
          creates a test score event with the specified penalty (and flags it as unreported).
protected  void penalty(int penalty, java.lang.String title, java.lang.String msg, boolean known)
          creates a test score event with the specified penalty
protected  void setVariableValue(java.lang.String varName, Literal value)
           
protected  java.lang.String tostring(Literal literal)
          Convert a literal to its string representation
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Context

public Context(ScoringRule rule)
instantiates a Context to store state for the specified rule (and its needed test results)

Parameters:
rule - the rule this context is used by (this is referenced in generated score events and it contains the current test results too)
Method Detail

getTestOptionValue

protected Literal getTestOptionValue(java.lang.String testName,
                                     int testNum,
                                     java.lang.String optionName)
                              throws UndefinedTestException,
                                     TestUnavailableException,
                                     UndefinedTestOptionException,
                                     TypeMismatchException
gets the value of a test option

Parameters:
testName - name of the test whose result is to be fetched
testNum - what test to access (0 = most recent result, 1 = 1 run ago, etc.)
optionName - name of the configuration option which is to be fetched
Returns:
value of the requested option in the specified test
Throws:
UndefinedTestException - occurs if the requested test is not tracked in this context
TestUnavailableException - occurs if the test is found, but it hasn't run testNum number of times yet
UndefinedTestOptionException - occurs if the test is found, but the option isn't a valid option
TypeMismatchException - occurs if the option value is not a literal (shouldn't happen)

getTestResultValue

protected Literal getTestResultValue(java.lang.String testName,
                                     int testNum,
                                     java.lang.String resultName)
                              throws UndefinedTestException,
                                     TestUnavailableException,
                                     UndefinedTestResultException,
                                     TypeMismatchException
gets the value of a test result

Parameters:
testName - name of the test whose result is to be fetched
resultName - name of the result to fetch
Throws:
UndefinedTestException - occurs if the requested test is not tracked in this context
TestUnavailableException - occurs if the test is found, but it hasn't run testNum number of times yet
UndefinedTestResultException - occurs if the test is found, but the result requested isn't a valid result
TypeMismatchException - occurs if the option value is not a literal (shouldn't happen)

addVariable

protected void addVariable(Identifier iden,
                           Literal value)
                    throws RedefinedVariableException
adds a variable to the context

Parameters:
iden - the name of the variable
value - the value of the variable
Throws:
RedefinedVariableException - occurs if the specified variable's name has already been declared

getVariableValue

protected Literal getVariableValue(java.lang.String varName)
                            throws UndefinedVariableException
gets the specified variable; if a variable with the specified name isn't found, then a UndefinedVariableException will be thrown.

Parameters:
varName - the name of the variable whose value is to be returned
Returns:
value of the requested variable
Throws:
UndefinedVariableException - occurs if the specified variable's name has never been declared

setVariableValue

protected void setVariableValue(java.lang.String varName,
                                Literal value)
                         throws UndefinedVariableException,
                                TypeMismatchException
Throws:
UndefinedVariableException
TypeMismatchException

mayAssign

public static boolean mayAssign(LiteralType type,
                                LiteralType valueType)
Returns true if the specified value type may be assigned to the specified type. This is always true if the value type is the same as specified type. This is also true if value is a Double and value is an Integer.

Parameters:
type - the type of the variable to set
valueType - the type of value to assign to the variable
Returns:
true if the specified value type may be assigned to the specified type

compareDates

protected boolean compareDates(java.lang.String date1,
                               OperatorType booleanOp,
                               java.lang.String date2)
                        throws IllegalOperandException
Compare two dates with the specified boolean operator. Dates must either be 'now' (to get the current date and time) or be specified in a valid date format (ex: dd-mmm-yyyy hh:nn).

Parameters:
date1 - date on the left side of the operator
booleanOp - the boolean operator to compare the dates with
date2 - date on the right side of the operator
Returns:
false if either date is invalid; otherwise, the boolean value of date1 booleanOp date2
Throws:
IllegalOperandException - thrown if an invalid operator (i.e. not a boolean operator) is supplied

count

protected int count(java.lang.String testName,
                    int numResultsToLookAt,
                    java.lang.String resultName,
                    OperatorType operator,
                    Literal operand2)
             throws UndefinedTestException,
                    TestUnavailableException,
                    UndefinedTestResultException,
                    IllegalOperandException,
                    TypeMismatchException
counts the number of a times a given result meets a given condition for some number of iterations of the test. Specifically, count the number of tests from the most recent test (0) to numTests (inclusive) whose the result meets the condition: result operator operand2. If the condition is true, count is incremented by 1, otherwise 0.

Parameters:
testName - name of the test to check
numResultsToLookAt - number of most recent tests to check
resultName - name of the result to check
operator - operator to use when testing the result
operand2 - second operand for when testing the result
Returns:
the number of tests which met the specified condition
Throws:
UndefinedTestException - occurs if the requested test is not tracked in this context
TestUnavailableException - occurs if the test is found, but it hasn't run testNum number of times yet
UndefinedTestResultException - occurs if the test is found, but the result requested isn't a valid result
IllegalOperandException - occurs if o1 and o2 are not defined for the specified operator
TypeMismatchException - occurs if the specified operator returns a type other than Boolean

floor

protected int floor(Literal literal)
             throws IllegalArgValException
Convert a double to a floored integer.

Parameters:
literal - the literal to convert (must be a Double)
Returns:
floored integer representation of the specified literal
Throws:
IllegalArgValException

tostring

protected java.lang.String tostring(Literal literal)
Convert a literal to its string representation

Parameters:
literal - the literal to convert
Returns:
string representation of literal

echo

protected void echo(java.lang.String msg)
prints the specified message to standard out

Parameters:
msg - the message to print

echoErr

protected void echoErr(java.lang.String errMsg)
prints the specified message to standard error

Parameters:
errMsg - the message to print

bonus

protected void bonus(int bonus,
                     java.lang.String title,
                     java.lang.String msg)
creates a test score event with the specified bonus (will be reported)

Parameters:
bonus - number of points to add
title - a descriptive heading for the penalty
msg - why points are being added

bonus

protected void bonus(int bonus,
                     java.lang.String title,
                     java.lang.String msg,
                     boolean known)
creates a test score event with the specified bonus

Parameters:
bonus - number of points to add
title - a descriptive heading for the penalty
msg - why points are being added
known - If true, then all constituent TestEvents will be flagged as reported. Otherwise, they will be flagged as not reported.

penalty

public void penalty(int penalty,
                    java.lang.String title,
                    java.lang.String msg)
creates a test score event with the specified penalty (and flags it as unreported).

Parameters:
penalty - number of points to deduct
title - a descriptive heading for the penalty
msg - why points are being deducted

penalty

protected void penalty(int penalty,
                       java.lang.String title,
                       java.lang.String msg,
                       boolean known)
creates a test score event with the specified penalty

Parameters:
penalty - number of points to deduct
title - a descriptive heading for the penalty
msg - why points are being deducted
known - If true, then all constituent TestEvents will be flagged as reported. Otherwise, they will be flagged as not reported.

hasAllNeededResults

public boolean hasAllNeededResults()
determines whether or not all TestNumPairs are fully populated


getTestScoreEvents

public java.util.Vector<TestScoreEvent> getTestScoreEvents()
gets any test score events generated by in this context