public class OclParser extends Object
The OclParser object implements a parser and intermediate code generator for a subset of OCL. The latter is roughly characterized by the following traits
The OclParser object is created by means of a no-argument constructor and can then repeatedly be used to translate OCL expressions by invoking the parseOcl() function. In case of a successful parse the resulting syntax tree is returned as a OclNode.Expression object, null if the parse was not successful. Various methods allow to inquire the number of generated diagnostics, the diagnostics themselves, the contents of all OCL comments detected, and debug output representing syntax tree representations.
The set of built-in operations recognized by the OclParser can be extended by means of the method pair addOperation(...) and removeOperation(...).
The OclParser works in two phases:
The output of the second phase is used in ShapeChange Targets to generate OCL equivalents in various target bindings.
OclParser objects are not designed for concurrent use. Each thread will require its own OclParser object.
Constructor and Description |
---|
OclParser()
The OclParser object is created.
|
Modifier and Type | Method and Description |
---|---|
void |
addEnvironmentVariable(String name,
OclNode.DataType dt,
OclNode initValue)
This method adds a new variable declaration to the set of so-called
environment variables.
|
void |
addOperation(String name,
OclNode.DataType objtype,
OclNode.DataType rettype,
int noOfArgs)
This method adds an operation to the set of operations the
parser recognizes as valid OCL operations.
|
String |
debugOclNodes()
Obtain a syntactical representation of the OclNode result for
debugging purposes.
|
String |
debugTempNodes()
Obtain a syntactical representation of the TempNode intermediate
for debugging purposes.
|
String[] |
getComments()
This methods inquires the payload of all comments encountered in parsing
the OCL constraint.
|
MessageCollection |
getMessageCollection()
Inquire the current message collection for this OclParser.
|
int |
getNumberOfComments()
This inquires the number of comments encountered in the OCL expression.
|
int |
getNumberOfMessages()
Inquire the current number of messages for this OclParser.
|
OclNode.Expression |
parseOcl(Readable inOcl,
Info ctx)
Perform a full parse of an OCL expression addressed by a Readable.
|
OclNode.Expression |
parseOcl(Readable inOcl,
Info ctx,
String type,
String name)
Perform a full parse of an OCL expression addressed by a Readable.
|
void |
removeEnvironmentVariable(String name)
This one removes an environment variable by name.
|
void |
removeOperation(String name,
OclNode.DataType objtype,
int noOfArgs)
By this method you can remove operations from the set of operations
the parser recognizes as valid OCL operations.
|
public OclParser()
public OclNode.Expression parseOcl(Readable inOcl, Info ctx)
Perform a full parse of an OCL expression addressed by a Readable. The output is returned as an OclNode.OclExpression.
Parsing starts at the condition type keyword, such as inv:, derive:, etc.
inOcl
- Readable to fetch the OCL input from.ctx
- Object context in UML model, mostly ClassInfopublic OclNode.Expression parseOcl(Readable inOcl, Info ctx, String type, String name)
Perform a full parse of an OCL expression addressed by a Readable. The output is returned as an OclNode.OclExpression.
This alternate version of the function starts parsing directly at the pure OCL statement. Condition type and name of the constraint have to be passed along as parameters.
inOcl
- Readable to fetch the OCL input from.ctx
- Object context in UML model, mostly ClassInfotype
- Condition type, such as inv, init, derive ...name
- The name of the constraint (may be empty)public int getNumberOfComments()
public String[] getComments()
public int getNumberOfMessages()
public MessageCollection getMessageCollection()
public String debugTempNodes()
Obtain a syntactical representation of the TempNode intermediate for debugging purposes.
public String debugOclNodes()
Obtain a syntactical representation of the OclNode result for debugging purposes.
public void addEnvironmentVariable(String name, OclNode.DataType dt, OclNode initValue)
This method adds a new variable declaration to the set of so-called environment variables. An environment variable is another variable declaration in addition to self which is automatically set up from the context of an expression.
This is an extension to OCL, which is sometimes useful to add predefined references to a set of constraints. It works like a big let, which surrounds the expression.
name
- Name of the variable to be declareddt
- The datatype of the variableinitValue
- Its initial valuepublic void removeEnvironmentVariable(String name)
This one removes an environment variable by name.
name
- The name of the environment variablepublic void addOperation(String name, OclNode.DataType objtype, OclNode.DataType rettype, int noOfArgs)
This method adds an operation to the set of operations the parser recognizes as valid OCL operations. The operation is identified by its name, the number of parameters and the type of object it can be applied to.
name
- Name of operationobjtype
- Datatype the operation can be applied to. If
given as null, the operation can be applied to any type.rettype
- The return type of the operationnoOfArgs
- The number of parameters. If given as -1 any
number of parameters is permittedpublic void removeOperation(String name, OclNode.DataType objtype, int noOfArgs)
By this method you can remove operations from the set of operations the parser recognizes as valid OCL operations. The operations to be removed are identified by by their names, the number of parameters and the type of object it can be applied to.
name
- The name of the operations to be removedobjtype
- The Datatype the operation can be applied to. Can be
specified as null with a wildcard meaning.noOfArgs
- The number of parameters. If given as -1 any
number of parameters will be eligible for removal.Copyright © 2017. All rights reserved.