Dresden OCL Toolkit

tudresden.ocl.codegen.decl
Class DeclarativeCodeGenerator

java.lang.Object
  extended bytudresden.ocl.parser.analysis.AnalysisAdapter
      extended bytudresden.ocl.parser.analysis.ReversedDepthFirstAdapter
          extended bytudresden.ocl.codegen.decl.DeclarativeCodeGenerator
All Implemented Interfaces:
Analysis, CodeGenerator, Switch
Direct Known Subclasses:
ILSQLCodeGenerator, SQLCodeGenerator

public class DeclarativeCodeGenerator
extends ReversedDepthFirstAdapter
implements CodeGenerator

This class provides functionality to generate declarative code such as SQL by traversing the abstract syntax tree in reversed depth first order. The basic concept is simple substring replacing in a StringBuffer. Therefore placeholders are going to be created and inserted at the proper positions within the buffer. The task of replacing these placeholders with target code is up to the nodes of the abstract syntax tree. To associate nodes of the syntax tree with such tasks, a Hashtable is used provided by AnalysisAdapter. It contains the nodes as keys and the tasks (placeholders) as elements. It can be accessed by using the setIn and getIn methodes of Analysis Adapter. To implement a code generator using the explained strategy, it is necessary to derive a subclass from DeclarativeCodeGenerator and implement all inA... methodes of the nodes that are destined to produce code. If a node is not able to produce complete target code, new unique placeholders should be inserted at the critical positions. The task to replace these placeholders should be forwarded to subnodes using the proper methodes of this class. DeclarativeCodeGenerator does not produce any code or codefragments. It only creates the first task at the start node and delegates it to the leaf nodes. Code generation and CodeFragment handling is up to the derived subclasses.

Author:
Sten Loecher

Field Summary
protected  CodeAgent ca
          the code agent that provides code templates
protected  StringBuffer code
          the temporary code buffer, later inserted into code fragments
protected  Vector fragments
          the container for the produced code fragments
private  int taskCounter
          the counter for the creation of unique tasks
protected  OclTree theTree
          the abstract syntax tree
 
Fields inherited from class tudresden.ocl.parser.analysis.AnalysisAdapter
 
Constructor Summary
DeclarativeCodeGenerator(String rules)
           
 
Method Summary
 void defaultIn(Node node)
          Automated task delegation for all nodes.
 CodeFragment[] getCode(OclTree tree)
           
(package private)  String getUniqueTask()
           
 void inAConstraint(AConstraint node)
          Creates the first task, initializes the code buffer with it and delegates it to all subnodes.
(package private)  String replaceInString(String source, String oldStr, String newStr)
          A replacement methode for Strings.
(package private)  void replaceTask(String task, String template)
          Replaces all occurences of the task with the given template in the code buffer.
(package private)  void setRules(String rules)
           
(package private)  void transferTask(Node from, LinkedList to)
          Delegates a task to an list of subnodes.
(package private)  void transferTask(Node from, Node to)
          Delegates a task to an subnode.
 
Methods inherited from class tudresden.ocl.parser.analysis.ReversedDepthFirstAdapter
caseAActualParameterList, caseAActualParameterListTail, caseAAdditiveExpression, caseAAdditiveExpressionTail, caseAAndLogicalOperator, caseAArrowPostfixExpressionTailBegin, caseABagCollectionKind, caseABagCollectionType, caseABarFcpHelper, caseABooleanLiteral, caseAClassifierContext, caseAClassifierContextBody, caseAClassifierHead, caseACollectionCollectionKind, caseACollectionCollectionType, caseACollectionTypeName, caseAColonFcpHelper, caseACommaFcpHelper, caseAConcreteFeatureCallParameters, caseAConstraint, caseAConstraintBody, 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, defaultOut, inAActualParameterList, inAActualParameterListTail, inAAdditiveExpression, inAAdditiveExpressionTail, inAAndLogicalOperator, inAArrowPostfixExpressionTailBegin, inABagCollectionKind, inABagCollectionType, inABarFcpHelper, inABooleanLiteral, inAClassifierContext, inAClassifierContextBody, inAClassifierHead, inACollectionCollectionKind, inACollectionCollectionType, inACollectionTypeName, inAColonFcpHelper, inACommaFcpHelper, inAConcreteFeatureCallParameters, 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, inAInvStereotype, inAIterateDeclarator, inAIterateFcpHelper, inALetExpression, inALetExpressionTypeDeclaration, inAListExpressionListOrRangeTail, inALitColPrimaryExpression, inALiteralCollection, inALiteralPrimaryExpression, inALogicalExpression, inALogicalExpressionTail, inALteqRelationalOperator, inALtRelationalOperator, inAMinusAddOperator, inAMinusUnaryOperator, inAMultiplicativeExpression, inAMultiplicativeExpressionTail, inAMultMultiplyOperator, inANamePathNameBegin, inANamePathNameEnd, inANEqualRelationalOperator, inANonCollectionTypeName, inANotUnaryOperator, inAOperationContext, inAOperationContextBody, inAOrLogicalOperator, inAParenthesesPrimaryExpression, inAPathName, inAPathNameTail, inAPathSimpleTypeSpecifier, inAPathTypeName, inAPathTypeNameTail, inAPlusAddOperator, inAPostfixExpression, inAPostfixExpressionTail, inAPostfixUnaryExpression, inAPostStereotype, inAPreStereotype, 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

theTree

protected OclTree theTree
the abstract syntax tree


fragments

protected Vector fragments
the container for the produced code fragments


code

protected StringBuffer code
the temporary code buffer, later inserted into code fragments


ca

protected CodeAgent ca
the code agent that provides code templates


taskCounter

private int taskCounter
the counter for the creation of unique tasks

Constructor Detail

DeclarativeCodeGenerator

public DeclarativeCodeGenerator(String rules)
Method Detail

setRules

void setRules(String rules)

getUniqueTask

String getUniqueTask()
Returns:
a new unique task (placeholder)

replaceTask

void replaceTask(String task,
                 String template)
Replaces all occurences of the task with the given template in the code buffer. If task is contained in the template it is not going to be replaced.

Parameters:
task - the placeholder
template - the code template

replaceInString

String replaceInString(String source,
                       String oldStr,
                       String newStr)
A replacement methode for Strings.

Parameters:
source - the string containing substrings that equal to oldStr
oldStr - the substring to be replaced
newStr - the replacement
Returns:
A String with replaced occurences of oldStr with newStr

transferTask

void transferTask(Node from,
                  LinkedList to)
Delegates a task to an list of subnodes.

Parameters:
from - the owner of the task
to - the list of recievers

transferTask

void transferTask(Node from,
                  Node to)
Delegates a task to an subnode.

Parameters:
from - the owner of the task
to - the reciever

inAConstraint

public void inAConstraint(AConstraint node)
Creates the first task, initializes the code buffer with it and delegates it to all subnodes.

Overrides:
inAConstraint in class ReversedDepthFirstAdapter

defaultIn

public void defaultIn(Node node)
Automated task delegation for all nodes.

Overrides:
defaultIn in class ReversedDepthFirstAdapter

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.