|
|||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||||
java.lang.Objectcom.tensegrity.generic.constraint.ConstraintParser
Class ConstraintParser can be used to parse strings representing
Constraint expressions. Instantiating the parser is inexpensive
and can be used to parse as many Constraint expressions as needed.
The ConstraintParser is a 2-pass parser. First, a syntax analysis
is performed during which a Constraint expression is transformed
into an intermediate RPN representation (Reverse Polish Notation, also known as
postfix notation). This intermediate representation is then converted into a
tree of constraints, with the most outer expression becoming the root node. A
reference to this root node is returned to the caller by the parser. Since the
parser performs a full lexical analysis on its input, one can safely add extra
white spaces without altering the meaning of a Constraint expression.
A recursive descent parser that accepts/denies constraint strings.
The following grammar denotes valid syntax for constraints:
expr ::= term rest
rest ::= '&&' term rest
| '||' term rest
| '==' term rest
| '!=' term rest
| '<' term rest
| '<=' term rest
| '>' term rest
| '>=' term rest
| 'instanceof' term rest
| 'like' term rest
| 'in' '(' inlist ')'
| apply term
| epsilon
term ::= identifier
| integer
| double
| string
| '!' expr
| '(' expr ')'
inlist ::= string inlistseparator inlist
| integer inlistseparator inlist
| double inlistseparator inlist
| epsilon
inlistseparator ::= '|'
Internally this parser works by recursive descent. However a tree representation of the parsed constrains is not generated directly. An intermediate stack representation is generated, which in turn is converted to the final constraint-tree representation for the given constraint expression that is parsed. It would as well be possible to omit the intermediate representation, but as it is implemented now, there is such a representation. Like all things, this may have pros and cons. Internally the stack representation is also referred to as RPN-list.
This class implements the "Flyweight" pattern, as all input that
passed to the method parse(String) is first checked against
a pool of already known constraints. Constraints are immutable, so sharing
them is perfectly legal. All the intrinsic state of all the constraints
is immutable.
Constraint,
ConstraintPool| Constructor Summary | |
ConstraintParser()
Constructs a constraint parser instance. |
|
| Method Summary | |
boolean |
isConstraint(java.lang.Object object)
Checks whether the object is a constraint. |
Constraint |
parse(java.lang.String input)
Parses a constraint expression that is given as a string and returns a constraint representing it if the string represents a valid constraint. |
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
public ConstraintParser()
| Method Detail |
public Constraint parse(java.lang.String input)
throws ConstraintParserException
input - the string representing a constraint expression. It is not
allowed to specify null as parsing input.
ConstraintParserException - is thrown if the parsing produces
any kind of error. This can be syntactical errors or semantic
errors.public boolean isConstraint(java.lang.Object object)
object - the object that is to be verified.
true, if the given object is a constraint;
false otherwise.
|
|||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||||