Dresden OCL Toolkit

tudresden.ocl.codegen
Class ProceduralCodeGenerator

java.lang.Object
  extended bytudresden.ocl.parser.analysis.AnalysisAdapter
      extended bytudresden.ocl.parser.analysis.DepthFirstAdapter
          extended bytudresden.ocl.codegen.ProceduralCodeGenerator
All Implemented Interfaces:
Analysis, CodeGenerator, Switch
Direct Known Subclasses:
JavaCodeGenerator

public abstract class ProceduralCodeGenerator
extends DepthFirstAdapter
implements CodeGenerator

Subclasses should overwrite only inXxx/outXxx/caseXxx methods of nodes "below" AConstraintBody (below means nodes such that their production can be derived from the production constraint_body). The methods inAConstraintBody and outAConstraintBody may be overwritten, but not caseAConstraintBody. This is not enforced by declaring methods final to allow those who know what they are doing to change behaviour in these methods.

After leaving outAConstraintBody subclasses must leave a variable name for the AConstraintBody's Expression object in the map ncm. This name is then taken as the translation for the contraint body.


Field Summary
protected  StringBuffer code
          the Java code for the expression
(package private)  String constrainedOperation
           
(package private)  String constrainedType
           
(package private)  int constraintKind
          one of CodeFragment.PRE, CodeFragment.POST, CodeFragement.INV
protected  ArrayList fragments
           
protected  LinkedList fragmentStack
           
(package private)  int indent
           
(package private)  int initialIndent
           
(package private)  boolean newLine
           
private  NodeNameMap nnm
          maps Nodes to variable names for nodes; can be accessed through getVariable(Node n)
(package private)  String[][] parameters
          Formal parameters of the constrainted operation if a operation is constrainted; null otherwise.
parameters[i][j] contains the i-th parameters name if i==0 and the i-th parameters type if i==1
protected  StringBuffer preCode
          Java code that is nessacary to evaluate @pre time expressions; will often be null (if the OCL expression contains no @pre)
(package private)  boolean preCodeIsValid
           
(package private)  ArrayList preVariables
          the list of variable determined in preCode and used in code
(package private)  HashMap preVarTypes
          maps pre variables to their types; the key set of this map must contain exactly those elements that are in preVariables
protected  ProceduralCodeFragment topOfStack
           
protected  OclTree tree
           
(package private)  boolean writeToPreCode
           
(package private)  boolean writeToStandardCode
           
 
Fields inherited from class tudresden.ocl.parser.analysis.AnalysisAdapter
 
Constructor Summary
ProceduralCodeGenerator()
           
 
Method Summary
private  void addParameter(ArrayList parameterNames, ArrayList parameterTypes, AFormalParameter fp)
           
 void addPreVariable(String var, String type)
           
protected  void appendCode(String s)
           
 void assurePreCode()
          make the preCode become an own fragment
protected  void beginNewFragment(String name, String type, String operation, int kind)
           
 void caseAConstraintBody(AConstraintBody cb)
           
protected  void decreaseIndent(int i)
           
protected  void endFragment(String resultVariable)
           
 CodeFragment[] getCode(OclTree tree)
           
protected abstract  String getTransferCode(String var, String type)
           
protected  String getVariable(Node n)
          get a variable name for a AST node; this method either returns the name stored in the map nnm (if the map contains a mapping for node) or creates a new name, stores it in nnm and returns it
 void inAClassifierContext(AClassifierContext cc)
           
 void inAInvStereotype(AInvStereotype is)
           
 void inAOperationContext(AOperationContext oc)
           
 void inAPostStereotype(APostStereotype is)
           
 void inAPreStereotype(APreStereotype is)
           
protected  void increaseIndent(int i)
           
protected  void reachThrough(Node from, Node to)
          set the variable name representing node from to the variable name of node to
protected abstract  void requireTreeInvariants()
          gives subclasses the opportunity to check if the OclTree requires some invariants, using OclTree's requireInvariant() method
 void setInitialIndent(int initialIndent)
           
protected  void setVariable(Node n, String var)
          set the variable for a node
 void writeToBothCodes()
           
 void writeToPreCodeOnly()
           
 void writeToStandardCodeOnly()
           
 
Methods inherited from class tudresden.ocl.parser.analysis.DepthFirstAdapter
caseAActualParameterList, caseAActualParameterListTail, caseAAdditiveExpression, caseAAdditiveExpressionTail, caseAAndLogicalOperator, caseAArrowPostfixExpressionTailBegin, caseABagCollectionKind, caseABagCollectionType, caseABarFcpHelper, caseABooleanLiteral, caseAClassifierContext, caseAClassifierContextBody, caseAClassifierHead, caseACollectionCollectionKind, caseACollectionCollectionType, caseACollectionTypeName, caseAColonFcpHelper, caseACommaFcpHelper, caseAConcreteFeatureCallParameters, caseAConstraint, caseAContextDeclaration, caseADeclaratorTail, caseADeclaratorTypeDeclaration, caseADivMultiplyOperator, caseADotPostfixExpressionTailBegin, caseAEmptyFeatureCallParameters, caseAEnumerationType, caseAEnumerationTypeTail, caseAEnumLiteral, caseAEnumSimpleTypeSpecifier, caseAEqualRelationalOperator, caseAExpression, caseAExpressionListOrRange, caseAExpressionListTail, caseAFeatureCall, caseAFeatureCallParameters, caseAFeaturePrimaryExpression, caseAFormalParameter, caseAFormalParameterList, caseAFormalParameterListTail, caseAGteqRelationalOperator, caseAGtRelationalOperator, caseAIfExpression, caseAIfPrimaryExpression, caseAImpliesLogicalOperator, caseAIntegerLiteral, caseAInvStereotype, caseAIterateDeclarator, caseAIterateFcpHelper, caseALetExpression, caseALetExpressionTypeDeclaration, caseAListExpressionListOrRangeTail, caseALitColPrimaryExpression, caseALiteralCollection, caseALiteralPrimaryExpression, caseALogicalExpression, caseALogicalExpressionTail, caseALteqRelationalOperator, caseALtRelationalOperator, caseAMinusAddOperator, caseAMinusUnaryOperator, caseAMultiplicativeExpression, caseAMultiplicativeExpressionTail, caseAMultMultiplyOperator, caseANamePathNameBegin, caseANamePathNameEnd, caseANEqualRelationalOperator, caseANonCollectionTypeName, caseANotUnaryOperator, caseAOperationContext, caseAOperationContextBody, caseAOrLogicalOperator, caseAParenthesesPrimaryExpression, caseAPathName, caseAPathNameTail, caseAPathSimpleTypeSpecifier, caseAPathTypeName, caseAPathTypeNameTail, caseAPlusAddOperator, caseAPostfixExpression, caseAPostfixExpressionTail, caseAPostfixUnaryExpression, caseAPostStereotype, caseAPreStereotype, caseAQualifiers, caseARangeExpressionListOrRangeTail, caseARealLiteral, caseARelationalExpression, caseARelationalExpressionTail, caseAReturnTypeDeclaration, caseASequenceCollectionKind, caseASequenceCollectionType, caseASetCollectionKind, caseASetCollectionType, caseAStandardDeclarator, caseAStringLiteral, caseATimeExpression, caseATypeNamePathNameBegin, caseATypeNamePathNameEnd, caseAUnaryUnaryExpression, caseAXorLogicalOperator, caseStart, defaultIn, defaultOut, inAActualParameterList, inAActualParameterListTail, inAAdditiveExpression, inAAdditiveExpressionTail, inAAndLogicalOperator, inAArrowPostfixExpressionTailBegin, inABagCollectionKind, inABagCollectionType, inABarFcpHelper, inABooleanLiteral, inAClassifierContextBody, inAClassifierHead, inACollectionCollectionKind, inACollectionCollectionType, inACollectionTypeName, inAColonFcpHelper, inACommaFcpHelper, inAConcreteFeatureCallParameters, inAConstraint, inAConstraintBody, inAContextDeclaration, inADeclaratorTail, inADeclaratorTypeDeclaration, inADivMultiplyOperator, inADotPostfixExpressionTailBegin, inAEmptyFeatureCallParameters, inAEnumerationType, inAEnumerationTypeTail, inAEnumLiteral, inAEnumSimpleTypeSpecifier, inAEqualRelationalOperator, inAExpression, inAExpressionListOrRange, inAExpressionListTail, inAFeatureCall, inAFeatureCallParameters, inAFeaturePrimaryExpression, inAFormalParameter, inAFormalParameterList, inAFormalParameterListTail, inAGteqRelationalOperator, inAGtRelationalOperator, inAIfExpression, inAIfPrimaryExpression, inAImpliesLogicalOperator, inAIntegerLiteral, inAIterateDeclarator, inAIterateFcpHelper, inALetExpression, inALetExpressionTypeDeclaration, inAListExpressionListOrRangeTail, inALitColPrimaryExpression, inALiteralCollection, inALiteralPrimaryExpression, inALogicalExpression, inALogicalExpressionTail, inALteqRelationalOperator, inALtRelationalOperator, inAMinusAddOperator, inAMinusUnaryOperator, inAMultiplicativeExpression, inAMultiplicativeExpressionTail, inAMultMultiplyOperator, inANamePathNameBegin, inANamePathNameEnd, inANEqualRelationalOperator, inANonCollectionTypeName, inANotUnaryOperator, inAOperationContextBody, inAOrLogicalOperator, inAParenthesesPrimaryExpression, inAPathName, inAPathNameTail, inAPathSimpleTypeSpecifier, inAPathTypeName, inAPathTypeNameTail, inAPlusAddOperator, inAPostfixExpression, inAPostfixExpressionTail, inAPostfixUnaryExpression, inAQualifiers, inARangeExpressionListOrRangeTail, inARealLiteral, inARelationalExpression, inARelationalExpressionTail, inAReturnTypeDeclaration, inASequenceCollectionKind, inASequenceCollectionType, inASetCollectionKind, inASetCollectionType, inAStandardDeclarator, inAStringLiteral, inATimeExpression, inATypeNamePathNameBegin, inATypeNamePathNameEnd, inAUnaryUnaryExpression, inAXorLogicalOperator, inStart, outAActualParameterList, outAActualParameterListTail, outAAdditiveExpression, outAAdditiveExpressionTail, outAAndLogicalOperator, outAArrowPostfixExpressionTailBegin, outABagCollectionKind, outABagCollectionType, outABarFcpHelper, outABooleanLiteral, outAClassifierContext, outAClassifierContextBody, outAClassifierHead, outACollectionCollectionKind, outACollectionCollectionType, outACollectionTypeName, outAColonFcpHelper, outACommaFcpHelper, outAConcreteFeatureCallParameters, outAConstraint, outAConstraintBody, outAContextDeclaration, outADeclaratorTail, outADeclaratorTypeDeclaration, outADivMultiplyOperator, outADotPostfixExpressionTailBegin, outAEmptyFeatureCallParameters, outAEnumerationType, outAEnumerationTypeTail, outAEnumLiteral, outAEnumSimpleTypeSpecifier, outAEqualRelationalOperator, outAExpression, outAExpressionListOrRange, outAExpressionListTail, outAFeatureCall, outAFeatureCallParameters, outAFeaturePrimaryExpression, outAFormalParameter, outAFormalParameterList, outAFormalParameterListTail, outAGteqRelationalOperator, outAGtRelationalOperator, outAIfExpression, outAIfPrimaryExpression, outAImpliesLogicalOperator, outAIntegerLiteral, outAInvStereotype, outAIterateDeclarator, outAIterateFcpHelper, outALetExpression, outALetExpressionTypeDeclaration, outAListExpressionListOrRangeTail, outALitColPrimaryExpression, outALiteralCollection, outALiteralPrimaryExpression, outALogicalExpression, outALogicalExpressionTail, outALteqRelationalOperator, outALtRelationalOperator, outAMinusAddOperator, outAMinusUnaryOperator, outAMultiplicativeExpression, outAMultiplicativeExpressionTail, outAMultMultiplyOperator, outANamePathNameBegin, outANamePathNameEnd, outANEqualRelationalOperator, outANonCollectionTypeName, outANotUnaryOperator, outAOperationContext, outAOperationContextBody, outAOrLogicalOperator, outAParenthesesPrimaryExpression, outAPathName, outAPathNameTail, outAPathSimpleTypeSpecifier, outAPathTypeName, outAPathTypeNameTail, outAPlusAddOperator, outAPostfixExpression, outAPostfixExpressionTail, outAPostfixUnaryExpression, outAPostStereotype, outAPreStereotype, outAQualifiers, outARangeExpressionListOrRangeTail, outARealLiteral, outARelationalExpression, outARelationalExpressionTail, outAReturnTypeDeclaration, outASequenceCollectionKind, outASequenceCollectionType, outASetCollectionKind, outASetCollectionType, outAStandardDeclarator, outAStringLiteral, outATimeExpression, outATypeNamePathNameBegin, outATypeNamePathNameEnd, outAUnaryUnaryExpression, outAXorLogicalOperator, outStart
 
Methods inherited from class tudresden.ocl.parser.analysis.AnalysisAdapter
caseEOF, caseTAnd, caseTApostroph, caseTArrow, caseTAt, caseTBar, caseTBlank, caseTBool, caseTChannel, caseTColon, caseTComma, caseTComment, caseTContext, caseTDcolon, caseTDdot, caseTDiv, caseTDot, caseTEndif, caseTEnum, caseTEqual, caseTGt, caseTGteq, caseTImplies, caseTInt, caseTLBrace, caseTLBracket, caseTLPar, caseTLt, caseTLteq, caseTMinus, caseTMult, caseTName, caseTNEqual, caseTNewLine, caseTNot, caseTOr, caseTPlus, caseTRBrace, caseTRBracket, caseTReal, caseTRPar, caseTSemicolon, caseTSimpleTypeName, caseTStringLit, caseTTab, caseTTBag, caseTTCollection, caseTTElse, caseTTIf, caseTTIn, caseTTInv, caseTTLet, caseTTPost, caseTTPre, caseTTSequence, caseTTSet, caseTTThen, caseTXor, defaultCase, getIn, getOut, setIn, setOut
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

fragments

protected ArrayList fragments

fragmentStack

protected LinkedList fragmentStack

topOfStack

protected ProceduralCodeFragment topOfStack

nnm

private NodeNameMap nnm
maps Nodes to variable names for nodes; can be accessed through getVariable(Node n)


tree

protected OclTree tree

code

protected StringBuffer code
the Java code for the expression


preCode

protected StringBuffer preCode
Java code that is nessacary to evaluate @pre time expressions; will often be null (if the OCL expression contains no @pre)


writeToPreCode

boolean writeToPreCode

writeToStandardCode

boolean writeToStandardCode

preCodeIsValid

boolean preCodeIsValid

preVariables

ArrayList preVariables
the list of variable determined in preCode and used in code


preVarTypes

HashMap preVarTypes
maps pre variables to their types; the key set of this map must contain exactly those elements that are in preVariables


indent

int indent

initialIndent

int initialIndent

newLine

boolean newLine

constrainedType

String constrainedType

constrainedOperation

String constrainedOperation

parameters

String[][] parameters
Formal parameters of the constrainted operation if a operation is constrainted; null otherwise.
parameters[i][j] contains the i-th parameters name if i==0 and the i-th parameters type if i==1


constraintKind

int constraintKind
one of CodeFragment.PRE, CodeFragment.POST, CodeFragement.INV

Constructor Detail

ProceduralCodeGenerator

public ProceduralCodeGenerator()
Method Detail

beginNewFragment

protected void beginNewFragment(String name,
                                String type,
                                String operation,
                                int kind)
Parameters:
operation - is null for invariants

endFragment

protected void endFragment(String resultVariable)

getVariable

protected String getVariable(Node n)
get a variable name for a AST node; this method either returns the name stored in the map nnm (if the map contains a mapping for node) or creates a new name, stores it in nnm and returns it

Returns:
the name of the variable that is the target code translation of node

setVariable

protected void setVariable(Node n,
                           String var)
set the variable for a node


reachThrough

protected void reachThrough(Node from,
                            Node to)
set the variable name representing node from to the variable name of node to


appendCode

protected void appendCode(String s)

setInitialIndent

public void setInitialIndent(int initialIndent)

increaseIndent

protected void increaseIndent(int i)

decreaseIndent

protected void decreaseIndent(int i)

inAClassifierContext

public void inAClassifierContext(AClassifierContext cc)
Overrides:
inAClassifierContext in class DepthFirstAdapter

inAOperationContext

public void inAOperationContext(AOperationContext oc)
Overrides:
inAOperationContext in class DepthFirstAdapter

addParameter

private void addParameter(ArrayList parameterNames,
                          ArrayList parameterTypes,
                          AFormalParameter fp)

caseAConstraintBody

public final void caseAConstraintBody(AConstraintBody cb)
Specified by:
caseAConstraintBody in interface Analysis
Overrides:
caseAConstraintBody in class DepthFirstAdapter

inAInvStereotype

public void inAInvStereotype(AInvStereotype is)
Overrides:
inAInvStereotype in class DepthFirstAdapter

inAPreStereotype

public void inAPreStereotype(APreStereotype is)
Overrides:
inAPreStereotype in class DepthFirstAdapter

inAPostStereotype

public void inAPostStereotype(APostStereotype is)
Overrides:
inAPostStereotype in class DepthFirstAdapter

getTransferCode

protected abstract String getTransferCode(String var,
                                          String type)

requireTreeInvariants

protected abstract void requireTreeInvariants()
gives subclasses the opportunity to check if the OclTree requires some invariants, using OclTree's requireInvariant() method


assurePreCode

public void assurePreCode()
make the preCode become an own fragment


writeToPreCodeOnly

public void writeToPreCodeOnly()

writeToStandardCodeOnly

public void writeToStandardCodeOnly()

writeToBothCodes

public void writeToBothCodes()

addPreVariable

public void addPreVariable(String var,
                           String type)

getCode

public CodeFragment[] getCode(OclTree tree)
Specified by:
getCode in interface CodeGenerator

Dresden OCL Toolkit

Submit a bug
Developed at the Dresden University of Technology.
This software is published under the GNU Lesser General Public License.