org.ocltf.translation
Class BaseTranslator

java.lang.Object
  extended byorg.ocltf.parser.analysis.AnalysisAdapter
      extended byorg.ocltf.parser.analysis.DepthFirstAdapter
          extended byorg.ocltf.translation.BaseTranslator
All Implemented Interfaces:
org.ocltf.parser.analysis.Analysis, org.ocltf.parser.node.Switch, Translator
Direct Known Subclasses:
TraceTranslator

public abstract class BaseTranslator
extends org.ocltf.parser.analysis.DepthFirstAdapter
implements Translator

The "base" translator which all Translator's should extend, provides some basic functionality, such as the retrieveal of translation fragments from the translation template file, pre-processing, post-processing, etc.

The primary methods (in addition to methods you'll extend to handle expression parsing) to take note of when extending this class are:

Author:
Chad Brandon

Field Summary
protected  org.apache.commons.logging.Log logger
          The apache common's Log instance that can be used by all decendant classes.
 
Fields inherited from interface org.ocltf.translation.Translator
CONTEXT_ELEMENT
 
Constructor Summary
BaseTranslator()
           
 
Method Summary
protected  java.lang.Object getContextElement()
          Returns the context element for the expression being translated.
protected  Expression getExpression()
          Returns the current value of the Expression.
protected  java.lang.String getTranslationFragment(java.lang.String fragmentName)
          Finds the "fragment" with the specified fragmentName from the library translation file.
protected  void handleException(java.lang.String methodName, java.lang.Exception ex)
          Handles the exception in the default way
protected  void handleTranslationFragment(org.ocltf.parser.node.Node node)
          Calls the handlerMethod defined on the <fragment/> element if fragmentName matches one the fragments defined within the current translation file.
 void inAClassifierContextDeclaration(org.ocltf.parser.node.AClassifierContextDeclaration declaration)
          Sets the element type which represents the context of the expression for expressions having classifiers as their context.
 void inADefClassifierExpressionBody(org.ocltf.parser.node.ADefClassifierExpressionBody expressionBody)
          Sets the kind and name of the expression for def expressions.
 void inAInvClassifierExpressionBody(org.ocltf.parser.node.AInvClassifierExpressionBody expressionBody)
          Sets the kind and name of the expression for inv expressions.
 void inAOperationContextDeclaration(org.ocltf.parser.node.AOperationContextDeclaration declaration)
          Sets the element type which represents the context of the expression for expressions having operations as their context.
 void inAOperationExpressionBody(org.ocltf.parser.node.AOperationExpressionBody operationExpressionBody)
          Sets the kind and name of the expression for operation contexts.
protected  void postProcess()
          Performs any post processing.
protected  void preProcess()
          Performs any initlization.
protected  void process(java.lang.String expression)
          Parses the expression and applies this Translator to it.
 Expression translate(java.lang.String translationName, java.lang.Object contextElement, java.lang.String expression)
          Translates the OCL into a translated Expression instance.
 
Methods inherited from class org.ocltf.parser.analysis.DepthFirstAdapter
caseAActualParameterList, caseAAdditiveExpression, caseAAdditiveExpressionTail, caseAAndLogicalOperator, caseAArrowPropertyCallExpressionTail, caseAAttributeOrAssociationContextDeclaration, caseAAttributeOrAssociationExpressionBody, caseABagCollectionKind, caseABagCollectionType, caseABarFeatureCallParameterOption, caseABodyOperationStereotype, caseABooleanPrimitiveLit, caseAClassifierContextDeclaration, caseACollectionCollectionKind, caseACollectionCollectionType, caseACollectionLit, caseACollectionLiteral, caseACollectionType, caseAColonFeatureCallParameterOption, caseACommaExpression, caseACommaFeatureCallParameterOption, caseAConcreteFeatureCallParameters, caseAContextDeclaration, caseADeclaratorTail, caseADefClassifierExpressionBody, caseADeriveInitialOrDerivedValue, caseADivMultiplyOperator, caseADotPropertyCallExpressionTail, caseAEqualExpression, caseAEqualRelationalOperator, caseAExpMessageArgument, caseAExpressionListOrRange, caseAFeatureCall, caseAFeatureCallParameters, caseAFeaturePrimaryExpression, caseAGteqRelationalOperator, caseAGtRelationalOperator, caseAIfExpression, caseAIfPrimaryExpression, caseAImpliesLogicalOperator, caseAInitInitialOrDerivedValue, caseAInLetExpSub, caseAIntegerPrimitiveLit, caseAInvClassifierExpressionBody, caseAIsMarkedPre, caseAIsSentMessageExp, caseAIterateDeclarator, caseAIterateFeatureCallParameterOption, caseALetExp, caseALetExpression, caseALetVariableDelaration, caseAListExpressionListOrRangeTail, caseALiteralPrimaryExpression, caseALogicalExp, caseALogicalExpression, caseALogicalExpressionTail, caseALteqRelationalOperator, caseALtRelationalOperator, caseAMessageArguments, caseAMessageArgumentsTail, caseAMessageExpression, caseAMessageMessageExp, caseAMinusAddOperator, caseAMinusUnaryOperator, caseAMultiplicativeExpression, caseAMultiplicativeExpressionTail, caseAMultMultiplyOperator, caseANotEqualRelationalOperator, caseANotUnaryOperator, caseAOperation, caseAOperationContextDeclaration, caseAOperationDefinitionExpression, caseAOperationExpressionBody, caseAOrderedsetCollectionType, caseAOrLogicalOperator, caseAPackageDeclaration, caseAParenthesesPrimaryExpression, caseAPathName, caseAPathNameTail, caseAPlusAddOperator, caseAPostOperationStereotype, caseAPreOperationStereotype, caseAPrimitiveLiteral, caseAPropertyCallExpression, caseAQualifiers, caseARangeExpressionListOrRangeTail, caseARealPrimitiveLit, caseARelationalExpression, caseARelationalExpressionTail, caseAScopeOperatorName, caseASequenceCollectionKind, caseASequenceCollectionType, caseASetCollectionKind, caseASetCollectionType, caseASimpleType, caseAStandardDeclarator, caseAStringPrimitiveLit, caseATupleLit, caseATupleLiteral, caseATupleType, caseATupletypeType, caseATypeDeclaration, caseATypeMessageArgument, caseAUnaryExpression, caseAVariableDeclaration, caseAVariableDeclarationLetExpSub, caseAVariableDeclarationList, caseAVariableDeclarationListTail, caseAVariableDefinitionExpression, caseAXorLogicalOperator, caseStart, defaultIn, defaultOut, inAActualParameterList, inAAdditiveExpression, inAAdditiveExpressionTail, inAAndLogicalOperator, inAArrowPropertyCallExpressionTail, inAAttributeOrAssociationContextDeclaration, inAAttributeOrAssociationExpressionBody, inABagCollectionKind, inABagCollectionType, inABarFeatureCallParameterOption, inABodyOperationStereotype, inABooleanPrimitiveLit, inACollectionCollectionKind, inACollectionCollectionType, inACollectionLit, inACollectionLiteral, inACollectionType, inAColonFeatureCallParameterOption, inACommaExpression, inACommaFeatureCallParameterOption, inAConcreteFeatureCallParameters, inAContextDeclaration, inADeclaratorTail, inADeriveInitialOrDerivedValue, inADivMultiplyOperator, inADotPropertyCallExpressionTail, inAEqualExpression, inAEqualRelationalOperator, inAExpMessageArgument, inAExpressionListOrRange, inAFeatureCall, inAFeatureCallParameters, inAFeaturePrimaryExpression, inAGteqRelationalOperator, inAGtRelationalOperator, inAIfExpression, inAIfPrimaryExpression, inAImpliesLogicalOperator, inAInitInitialOrDerivedValue, inAInLetExpSub, inAIntegerPrimitiveLit, inAIsMarkedPre, inAIsSentMessageExp, inAIterateDeclarator, inAIterateFeatureCallParameterOption, inALetExp, inALetExpression, inALetVariableDelaration, inAListExpressionListOrRangeTail, inALiteralPrimaryExpression, inALogicalExp, inALogicalExpression, inALogicalExpressionTail, inALteqRelationalOperator, inALtRelationalOperator, inAMessageArguments, inAMessageArgumentsTail, inAMessageExpression, inAMessageMessageExp, inAMinusAddOperator, inAMinusUnaryOperator, inAMultiplicativeExpression, inAMultiplicativeExpressionTail, inAMultMultiplyOperator, inANotEqualRelationalOperator, inANotUnaryOperator, inAOperation, inAOperationDefinitionExpression, inAOrderedsetCollectionType, inAOrLogicalOperator, inAPackageDeclaration, inAParenthesesPrimaryExpression, inAPathName, inAPathNameTail, inAPlusAddOperator, inAPostOperationStereotype, inAPreOperationStereotype, inAPrimitiveLiteral, inAPropertyCallExpression, inAQualifiers, inARangeExpressionListOrRangeTail, inARealPrimitiveLit, inARelationalExpression, inARelationalExpressionTail, inAScopeOperatorName, inASequenceCollectionKind, inASequenceCollectionType, inASetCollectionKind, inASetCollectionType, inASimpleType, inAStandardDeclarator, inAStringPrimitiveLit, inATupleLit, inATupleLiteral, inATupleType, inATupletypeType, inATypeDeclaration, inATypeMessageArgument, inAUnaryExpression, inAVariableDeclaration, inAVariableDeclarationLetExpSub, inAVariableDeclarationList, inAVariableDeclarationListTail, inAVariableDefinitionExpression, inAXorLogicalOperator, inStart, outAActualParameterList, outAAdditiveExpression, outAAdditiveExpressionTail, outAAndLogicalOperator, outAArrowPropertyCallExpressionTail, outAAttributeOrAssociationContextDeclaration, outAAttributeOrAssociationExpressionBody, outABagCollectionKind, outABagCollectionType, outABarFeatureCallParameterOption, outABodyOperationStereotype, outABooleanPrimitiveLit, outAClassifierContextDeclaration, outACollectionCollectionKind, outACollectionCollectionType, outACollectionLit, outACollectionLiteral, outACollectionType, outAColonFeatureCallParameterOption, outACommaExpression, outACommaFeatureCallParameterOption, outAConcreteFeatureCallParameters, outAContextDeclaration, outADeclaratorTail, outADefClassifierExpressionBody, outADeriveInitialOrDerivedValue, outADivMultiplyOperator, outADotPropertyCallExpressionTail, outAEqualExpression, outAEqualRelationalOperator, outAExpMessageArgument, outAExpressionListOrRange, outAFeatureCall, outAFeatureCallParameters, outAFeaturePrimaryExpression, outAGteqRelationalOperator, outAGtRelationalOperator, outAIfExpression, outAIfPrimaryExpression, outAImpliesLogicalOperator, outAInitInitialOrDerivedValue, outAInLetExpSub, outAIntegerPrimitiveLit, outAInvClassifierExpressionBody, outAIsMarkedPre, outAIsSentMessageExp, outAIterateDeclarator, outAIterateFeatureCallParameterOption, outALetExp, outALetExpression, outALetVariableDelaration, outAListExpressionListOrRangeTail, outALiteralPrimaryExpression, outALogicalExp, outALogicalExpression, outALogicalExpressionTail, outALteqRelationalOperator, outALtRelationalOperator, outAMessageArguments, outAMessageArgumentsTail, outAMessageExpression, outAMessageMessageExp, outAMinusAddOperator, outAMinusUnaryOperator, outAMultiplicativeExpression, outAMultiplicativeExpressionTail, outAMultMultiplyOperator, outANotEqualRelationalOperator, outANotUnaryOperator, outAOperation, outAOperationContextDeclaration, outAOperationDefinitionExpression, outAOperationExpressionBody, outAOrderedsetCollectionType, outAOrLogicalOperator, outAPackageDeclaration, outAParenthesesPrimaryExpression, outAPathName, outAPathNameTail, outAPlusAddOperator, outAPostOperationStereotype, outAPreOperationStereotype, outAPrimitiveLiteral, outAPropertyCallExpression, outAQualifiers, outARangeExpressionListOrRangeTail, outARealPrimitiveLit, outARelationalExpression, outARelationalExpressionTail, outAScopeOperatorName, outASequenceCollectionKind, outASequenceCollectionType, outASetCollectionKind, outASetCollectionType, outASimpleType, outAStandardDeclarator, outAStringPrimitiveLit, outATupleLit, outATupleLiteral, outATupleType, outATupletypeType, outATypeDeclaration, outATypeMessageArgument, outAUnaryExpression, outAVariableDeclaration, outAVariableDeclarationLetExpSub, outAVariableDeclarationList, outAVariableDeclarationListTail, outAVariableDefinitionExpression, outAXorLogicalOperator, outStart
 
Methods inherited from class org.ocltf.parser.analysis.AnalysisAdapter
caseEOF, caseTAnd, caseTApostrophe, caseTArrow, caseTAt, caseTAttr, caseTBag, caseTBar, caseTBlank, caseTBody, caseTBoolean, caseTCollection, caseTColon, caseTComma, caseTComment, caseTContext, caseTDef, caseTDerive, caseTDiv, caseTDot, caseTElse, caseTEndif, caseTEndpackage, caseTEnum, caseTEqual, caseTGt, caseTGteq, caseTIf, caseTImplies, caseTIn, caseTInit, caseTInt, caseTInv, caseTIsSentOperator, caseTLBrace, caseTLBracket, caseTLet, caseTLParen, caseTLt, caseTLteq, caseTMessageOperator, caseTMinus, caseTMult, caseTName, caseTNewLine, caseTNot, caseTNotEqual, caseTOper, caseTOr, caseTOrderedset, caseTPackage, caseTPlus, caseTPost, caseTPre, caseTRange, caseTRBrace, caseTRBracket, caseTReal, caseTRParen, caseTScopeOperator, caseTSemicolon, caseTSequence, caseTSet, caseTStringLit, caseTTab, caseTThen, caseTTuple, caseTTupletype, caseTUnknown, 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

logger

protected org.apache.commons.logging.Log logger
The apache common's Log instance that can be used by all decendant classes.

Constructor Detail

BaseTranslator

public BaseTranslator()
Method Detail

getExpression

protected Expression getExpression()
Returns the current value of the Expression. Subclasses will update the translatedExpression of this object as translation occurs.

Returns:
Expression

getContextElement

protected java.lang.Object getContextElement()
Returns the context element for the expression being translated.

Returns:
the context element.

handleTranslationFragment

protected void handleTranslationFragment(org.ocltf.parser.node.Node node)
Calls the handlerMethod defined on the <fragment/> element if fragmentName matches one the fragments defined within the current translation file.

A handlerMethod must have two arguments:

  1. The first argument must be a java.lang.String which will be the body of the corresponding kind element for the matching fragment. (i.e. if 'context LegalAgreement inv: allInstances -> isUnique(documentTitle')' is being translated, then the body of the element <kind name="inv"/> would be returned)
  2. The second argument is of type java.lang.Object and is the node that is currently being parsed at the time the matching of the fragmentName occurred.

For example this handlerMethod might be defined within your translation file to handle the 'allInstances' expression:

      <fragment name="(\s*${elementName}\s*\.)?\s*allInstances.*"
                   handlerMethod="handleAllInstances">
          <kind name="body">
              from $completeElementName as $lowerCaseElementName 
          </kind>
      </fragment>
      

And the implementation of the handleAllInstances method would be:

      public void handleAllInstances(String translation, Object node) {
          //some handling code
      }
      

Parameters:
node - the node being parsed, the toString value of this node is what is matched against the translation fragment. We also need to pass the node to our handlerMethod so that it can be used it for additional processing (if we need it).
See Also:
getTranslationFragment(java.lang.String)

getTranslationFragment

protected java.lang.String getTranslationFragment(java.lang.String fragmentName)
Finds the "fragment" with the specified fragmentName from the library translation file.

IMPORTANT: as a best practice, it is recommended that you use handleTranslationFragment(java.lang.String, Node node) if at all possible (instead of this method), it will help your code be cleaner and the methods smaller and more maintainable.

Will retrieve the contents of the fragment kind that corresponds to the kind of expression currently being translated. (i.e. if 'context LegalAgreement inv: allInstances -> isUnique(documentTitle')' is being translated, then the body of the element <kind name="inv"> would be returned).

NOTE:You would use this method instead of handleTranslationFragment(java.lang.String, Node node) if you just want to retrieve the value of the fragment and don't want to have a handlerMethod which actually handles the processing of the output. For example you may want to add a fragment called 'constraintTail' which would always be added to your translation at the end of the constraint, the 'tail'. There isn't any part of the expression that matches this, but you still want to store it in a translation template since it could be different between translations within your Translation-Library.

Parameters:
fragmentName - the name of the fragment to retrieve from the translation
Returns:
String the output String from the translated fragment.
See Also:
handleTranslationFragment(Node node)

preProcess

protected void preProcess()
Performs any initlization. Subclasses should override this method if they want to provide any initilization before translation begins.


translate

public Expression translate(java.lang.String translationName,
                            java.lang.Object contextElement,
                            java.lang.String expression)
Description copied from interface: Translator
Translates the OCL into a translated Expression instance.

Specified by:
translate in interface Translator
Parameters:
translationName - the library and translation to lookup perform the translation (i.e. sql.Oracle9i --> library to use would be "sql" and translation from the sql library would be 'Oracle9i').
contextElement - the element in the model to which the OCL constraint applies.
expression - the OCL expression to translate.
Returns:
Expression
See Also:
NOTE: null is allowed for contextElement (even though it isn't within ExpressionTranslator.translate() since the TraceTranslator doesn't need a contextElement and we don't want to slow down the trace by having to read and load a model each time.

process

protected void process(java.lang.String expression)
                throws java.io.IOException
Parses the expression and applies this Translator to it.

Parameters:
expression - - the expression to process.
Throws:
java.io.IOException - if an IO error occurs during processing.

handleException

protected void handleException(java.lang.String methodName,
                               java.lang.Exception ex)
Handles the exception in the default way

Parameters:
methodName -
ex -

postProcess

protected void postProcess()
Performs any post processing. Subclasses should override to perform any final cleanup/processing.


inAInvClassifierExpressionBody

public void inAInvClassifierExpressionBody(org.ocltf.parser.node.AInvClassifierExpressionBody expressionBody)
Sets the kind and name of the expression for inv expressions. If subclasses override this method, they MUST call this method before their own implementation.

Parameters:
expressionBody -

inADefClassifierExpressionBody

public void inADefClassifierExpressionBody(org.ocltf.parser.node.ADefClassifierExpressionBody expressionBody)
Sets the kind and name of the expression for def expressions. If subclasses override this method, they MUST call this method before their own implementation.

Parameters:
expressionBody -

inAOperationExpressionBody

public void inAOperationExpressionBody(org.ocltf.parser.node.AOperationExpressionBody operationExpressionBody)
Sets the kind and name of the expression for operation contexts. If subclasses override this method, they MUST call this method before their own implementation.

Parameters:
operationExpressionBody -

inAOperationContextDeclaration

public void inAOperationContextDeclaration(org.ocltf.parser.node.AOperationContextDeclaration declaration)
Sets the element type which represents the context of the expression for expressions having operations as their context. If subclasses override this method, they MUST call this method before their own implementation.

Parameters:
declaration - the AOperationContextDeclaration instance from which we retrieve the element type.

inAClassifierContextDeclaration

public void inAClassifierContextDeclaration(org.ocltf.parser.node.AClassifierContextDeclaration declaration)
Sets the element type which represents the context of the expression for expressions having classifiers as their context. If subclasses override this method, they MUST call this method before their own implementation.

Parameters:
declaration - the AClassifierContextDeclaration instance from which we retrieve the element type.


Copyright © 2003-2004 Chad Brandon. All Rights Reserved.