com.tensegrity.generic.constraint
Class ConstraintParser

java.lang.Object
  extended bycom.tensegrity.generic.constraint.ConstraintParser

public final class ConstraintParser
extends java.lang.Object

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.

Version:
$Id: ConstraintParser.java,v 1.44 2006/01/26 17:11:06 MichaelKegel Exp $
Author:
S. Rutz
See Also:
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

ConstraintParser

public ConstraintParser()
Constructs a constraint parser instance.

Method Detail

parse

public Constraint parse(java.lang.String input)
                 throws ConstraintParserException
Parses a constraint expression that is given as a string and returns a constraint representing it if the string represents a valid constraint.

Parameters:
input - the string representing a constraint expression. It is not allowed to specify null as parsing input.
Returns:
the root constraint of the parsed constraint expression.
Throws:
ConstraintParserException - is thrown if the parsing produces any kind of error. This can be syntactical errors or semantic errors.

isConstraint

public boolean isConstraint(java.lang.Object object)
Checks whether the object is a constraint.

Parameters:
object - the object that is to be verified.
Returns:
true, if the given object is a constraint; false otherwise.


Copyright © 2005 Tensegrity Software GmbH. All Rights Reserved. Date of creation: 09.06.2006.