org.expasy.jpl.commons.base.cond
Class ConditionInterpreter<T>

java.lang.Object
  extended by org.expasy.jpl.commons.base.cond.ConditionInterpreter<T>
All Implemented Interfaces:
Interpreter<Condition<T>>

public final class ConditionInterpreter<T>
extends Object
implements Interpreter<Condition<T>>

ConditionInterpreter translates condition type expressions into Condition. It helps building complex ConditionImpls.

Expressions are represented as an expression tree. [conditional] Expression trees represent code in a tree-like data structure, where each node is a expression over condition.

A complex conditional expression is an expression of simple condition operands (defined internally as variable (CVAR)) and classic unary (OR) and binary operators (AND, OR). Here is the grammar below:

EXPR := COND | NOT? EXPR
COND := CVAR BOP COND
CVAR := \w+
BOP := AND | OR
NOT := '!'
AND := '&'
OR := '|'

How to create a complex condition ?
  1. register simple conditions in engine as variables (CVAR).
  2. compile engine with a given string expression of defined variables (CVAR) and logical operators (NOT, AND and OR).
  3. get and test the condition as a simple condition Condition.
Example:
 ConditionInterpreter<Double> interpreter =
        ConditionInterpreter.newInstance();
 
 engine.register("c1", new ConditionImpl.Builder<Double, Double>(0.)
   .operator(OperatorGreaterThan.newInstance()).build());
engine.register("c2", new ConditionImpl.Builder<Double, Double>(10.) .operator(OperatorLowerThan.newInstance()).build());
Condition<Double> condition = interpreter.translate("c1 & c2"); Assert.assertTrue(condition.evaluate(4.)); Assert.assertFalse(condition.evaluate(14.));

Version:
1.0
Author:
nikitin
See Also:
ConditionImpl

Method Summary
 Condition<T> getCondition(String name)
          Get the condition given its name.
 String getNextConditionName()
           
 int getNumOfCondition()
           
static
<T> ConditionInterpreter<T>
newInstance()
           
 ConditionInterpreter<T> register(Condition<T> condition)
          Add anonymous condition instance in the condition table.
 ConditionInterpreter<T> register(String condition)
          Add anonymous condition.
 ConditionInterpreter<T> register(String conditionName, Condition<T> condition)
          Add a named condition in the condition table.
 ConditionInterpreter<T> register(String conditionName, String condition)
           
 void removeAllConditions()
          Remove all conditions.
 Condition<T> removeCondition(String name)
          Remove the condition.
 void traceInternalConditions()
          Display the internal content of defined conditions variables.
 Condition<T> translate(String expression)
          Parse expression and return a tree conditional expression.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

newInstance

public static <T> ConditionInterpreter<T> newInstance()

register

public ConditionInterpreter<T> register(String conditionName,
                                        Condition<T> condition)
Add a named condition in the condition table.

Parameters:
conditionName - the condition name.
condition - the condition instance.

register

public ConditionInterpreter<T> register(Condition<T> condition)
Add anonymous condition instance in the condition table.

Do not forget to get

Parameters:
condition - the condition instance.

register

public ConditionInterpreter<T> register(String conditionName,
                                        String condition)
                                 throws OperatorManager.InvalidOperatorException
Throws:
OperatorManager.InvalidOperatorException

register

public ConditionInterpreter<T> register(String condition)
                                 throws OperatorManager.InvalidOperatorException
Add anonymous condition.

Parameters:
condition - the string format condition.
Returns:
the engine.
Throws:
OperatorManager.InvalidOperatorException

getNextConditionName

public String getNextConditionName()
Returns:
the next generated condition name.

removeCondition

public Condition<T> removeCondition(String name)
Remove the condition.

Parameters:
name - the condition name.
Returns:
the removed condition.

removeAllConditions

public void removeAllConditions()
Remove all conditions.


getCondition

public Condition<T> getCondition(String name)
Get the condition given its name.

Parameters:
name - the condition name.
Returns:
the fetched condition.

getNumOfCondition

public int getNumOfCondition()
Returns:
the number of stored condition.

traceInternalConditions

public void traceInternalConditions()
Display the internal content of defined conditions variables.


translate

public Condition<T> translate(String expression)
                       throws ParseException
Parse expression and return a tree conditional expression.

Specified by:
translate in interface Interpreter<Condition<T>>
Parameters:
expression - the expression to evaluate.
Returns:
a tree expression of conditions.
Throws:
ParseException - if invalid expression.


Copyright (c) 2012 Swiss Institute of Bioinformatics. All rights reserved.