de.interactive_instruments.ShapeChange.Ocl
Class OclParser

java.lang.Object
  extended by de.interactive_instruments.ShapeChange.Ocl.OclParser

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.

Version:
0.1
Author:
Reinhard Erstling (c) interactive instruments GmbH, Bonn, Germany

Constructor Summary
OclParser()
          The OclParser object is created.
 
Method Summary
 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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

OclParser

public OclParser()
The OclParser object is created. It can be serially reused to do parsing operations. Multiple threads require multiple Parsers.

Method Detail

parseOcl

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.

Parameters:
inOcl - Readable to fetch the OCL input from.
ctx - Object context in UML model, mostly ClassInfo
Returns:
OclNode.OclExpression object being the starting point of a syntax tree. In case of error this is null.

parseOcl

public 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.

Parameters:
inOcl - Readable to fetch the OCL input from.
ctx - Object context in UML model, mostly ClassInfo
type - Condition type, such as inv, init, derive ...
name - The name of the constraint (may be empty)
Returns:
OclNode.OclExpression object being the starting point of a syntax tree. In case of error this is null.

getNumberOfComments

public int getNumberOfComments()
This inquires the number of comments encountered in the OCL expression.

Returns:
Number of comments in expression.

getComments

public String[] getComments()
This methods inquires the payload of all comments encountered in parsing the OCL constraint. The comments will be presented in the sequence as found in the expression and will be stripped from surrounding blanks.

Returns:
Array of comments encountered.

getNumberOfMessages

public int getNumberOfMessages()
Inquire the current number of messages for this OclParser.

Returns:
Number of messages.

getMessageCollection

public MessageCollection getMessageCollection()
Inquire the current message collection for this OclParser.

Returns:
MessageCollection object

debugTempNodes

public String debugTempNodes()

Obtain a syntactical representation of the TempNode intermediate for debugging purposes.

Returns:
String with syntactical structure

debugOclNodes

public String debugOclNodes()

Obtain a syntactical representation of the OclNode result for debugging purposes.

Returns:
String of syntactical structure.

addEnvironmentVariable

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.

Parameters:
name - Name of the variable to be declared
dt - The datatype of the variable
initValue - Its initial value

removeEnvironmentVariable

public void removeEnvironmentVariable(String name)

This one removes an environment variable by name.

Parameters:
name - The name of the environment variable

addOperation

public 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.

Parameters:
name - Name of operation
objtype - 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 operation
noOfArgs - The number of parameters. If given as -1 any number of parameters is permitted

removeOperation

public 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.

Parameters:
name - The name of the operations to be removed
objtype - 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 © 2015. All rights reserved.