| [** |
| * <copyright> |
| * |
| * Copyright (c) 2011,2012 E.D.Willink and others. |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * Contributors: |
| * E.D.Willink - initial API and implementation |
| * |
| * </copyright> |
| **/] |
| [comment encoding = UTF-8 /] |
| [module org::eclipse::ocl::examples::codegen::tables::model2bodies('http://www.eclipse.org/ocl/3.1.0/Pivot','http://www.eclipse.org/emf/2002/GenModel')/] |
| |
| [import org::eclipse::ocl::examples::codegen::common::constantTemplates/] |
| [import org::eclipse::ocl::examples::codegen::common::elementQueries/] |
| [import org::eclipse::ocl::examples::codegen::common::emitQueries/] |
| [import org::eclipse::ocl::examples::codegen::common::expressionTemplates/] |
| [import org::eclipse::ocl::examples::codegen::common::genPackageQueries/] |
| [import org::eclipse::ocl::examples::codegen::common::nameQueries/] |
| [import org::eclipse::ocl::examples::codegen::common::pivotQueries/] |
| [import org::eclipse::ocl::examples::codegen::common::symbolTemplates/] |
| |
| [template public generateBodies(genModel : GenModel)] |
| [comment @main /] |
| [for (genPackage : GenPackage | genModel.genPackages)] |
| [let pPackage : Package = genPackage.getPivotPackage()] |
| [for (pType : Type | pPackage.ownedType->select(ownedRule->notEmpty() or getOperations().ownedRule->notEmpty() or getProperties().ownedRule->notEmpty()))] |
| [generateType(genPackage, pPackage, pType)/] |
| [/for] |
| [/let] |
| [/for] |
| [/template] |
| |
| [template public generateType(genPackage : GenPackage, pPackage : Package, pType : Type)] |
| [let packagePathPrefix : String = if genPackage.basePackage->notEmpty() and genPackage.basePackage.size() > 0 then genPackage.basePackage.replaceAll('\\.', '/') + '/' else '' endif] |
| [let packagePrefix : String = packagePathPrefix.replaceAll('/', '.')] |
| [let className : String = pType.name + genPackage.getBodiesClassSuffix()] |
| [file (packagePathPrefix + pPackage.name + '/' + genPackage.getBodiesPackageName() + '/' + className + '.java', false, 'UTF-8')] |
| /** |
| * [genPackage.getCopyright(' * ')/] |
| ************************************************************************* |
| * This code is 100% auto-generated |
| * from: [pPackage.name/] |
| * using: org.eclipse.ocl.examples.codegen.tables.model2tables.mtl |
| * |
| * Do not edit it. |
| */ |
| package [genPackage.getQualifiedBodiesPackageName()/]; |
| |
| [prefixImports(knownImports(), genPackage.generateTypeClass(pPackage, pType))/] |
| [/file] |
| [/let] |
| [/let] |
| [/let] |
| [/template] |
| |
| [template public generateTypeClass(genPackage : GenPackage, pPackage : Package, pType : Type)] |
| [let className : String = pType.name + genPackage.getBodiesClassSuffix()] |
| /** |
| * [className/] provides the Java implementation bodies of OCL-defined [pType.name/] operations and properties. |
| */ |
| @SuppressWarnings("nls") |
| public class [className/] |
| { |
| [for (pConstraint : Constraint | pType.ownedRule->sortedBy(name)) before ('\n') separator ('\n')] |
| [let expInOcl : ExpressionInOcl = pConstraint.specification.getExpressionInOcl(pType)] |
| [expInOcl.generateInvariant(genPackage, pPackage, pType, pConstraint)/] |
| [/let] |
| [/for] |
| [if (pType.oclIsKindOf(Class))] |
| [for (pOperation : Operation | pType.oclAsType(Class).getOperations()->sortedBy(getMoniker())) before ('\n') separator ('\n')] |
| [for (pConstraint : Constraint | pOperation.ownedRule) separator ('\n')] |
| [let expInOcl : ExpressionInOcl = pConstraint.specification.getExpressionInOcl(pOperation)] |
| [if (expInOcl.contextVariable->notEmpty())] |
| [if (false and pConstraint.stereotype = 'body')] |
| public static [pOperation.type.name/] [pOperation.name/]([pType.name/] self[for (parameter : Parameter | pOperation.ownedParameter)], [parameter.type.name/] [parameter.name/][/for]) { |
| final <%ExecutorManager%> evaluator = new <%EcoreExecutorManager%>(this, null, <%[pType.getQualifiedTablesClassName(genPackage)/]%>.LIBRARY); |
| final <%ValueFactory%> valueFactory = evaluator.getValueFactory(); |
| Library[genPackage.emitOperationArity(pOperation.ownedParameter->size())/] codeGen = [pType.name + '_' + pOperation.name + '_' + pConstraint.stereotype + '_' + pConstraint.name/].INSTANCE; |
| try { |
| final <%Value%> result = codeGen.evaluate(evaluator, null, valueFactory.valueOf(self)[for (parameter : Parameter | pOperation.ownedParameter)], valueFactory.valueOf([parameter.name/])[/for]); |
| return ([pOperation.type.name/]) result.asEcoreObject(); |
| } catch (<%InvalidValueException%> e) { |
| throw new WrappedException(e); |
| } |
| } |
| |
| [/if] |
| [/if] |
| [expInOcl.generateOperationConstraint(genPackage, pPackage, pType, pOperation, pConstraint)/] |
| [/let] |
| [/for] |
| [/for] |
| [for (pProperty : Property | pType.oclAsType(Class).getProperties()->sortedBy(name)) before ('\n') separator ('\n')] |
| [for (pConstraint : Constraint | pProperty.ownedRule) separator ('\n')] |
| [let expInOcl : ExpressionInOcl = pConstraint.specification.getExpressionInOcl(pProperty)] |
| [expInOcl.generatePropertyConstraint(genPackage, pPackage, pType, pProperty, pConstraint)/] |
| [/let] |
| [/for] |
| [/for] |
| [/if] |
| } |
| [/let] |
| [/template] |
| |
| [template public generateInvariant(expInOcl : ExpressionInOcl, genPackage : GenPackage, pPackage : Package, pType : Type, pConstraint : Constraint)] |
| [let className : String = '_invariant_' + pConstraint.name] |
| [let arity : Integer = expInOcl.parameterVariable->size()] |
| /** |
| * Implementation of the [pType.name/] '[pConstraint.name/]' invariant. |
| */ |
| public static class [className/] extends <%Abstract[genPackage.emitOperationArity(arity)/]%> |
| { |
| public static [className/] INSTANCE = new [className/](); |
| [if (expInOcl.contextVariable->notEmpty())] |
| [let constants : String = expInOcl.bodyExpression.gatherConstants(genPackage, expInOcl)] |
| |
| /* |
| [expInOcl.prettyPrint().trim()/] |
| */ |
| public <%Value%> evaluate(<%DomainEvaluator%> evaluator, <%DomainType%> returnType, final <%Value%> [defineSymbolName(expInOcl.contextVariable, 'self')/][if expInOcl.parameterVariable->notEmpty()] |
| [for (parameter : Variable | expInOcl.parameterVariable)] |
| , final <%Value%> [defineSymbolName(parameter, parameter.name)/][/for][/if]) throws <%InvalidValueException%> { |
| [expInOcl.emitConstants(constants)/] |
| [expInOcl.bodyExpression.emitExpression(pType, genPackage, expInOcl)/] |
| return [expInOcl.bodyExpression.symbolName(expInOcl)/]; |
| } |
| [if (expInOcl.messageExpression->notEmpty())] |
| public static class Message extends <%Abstract[genPackage.emitOperationArity(arity)/]%> |
| { |
| public static [className/] INSTANCE = new [className/](); |
| |
| public <%Value%> evaluate(<%DomainEvaluator%> evaluator, <%DomainType%> returnType, final <%Value%> [defineSymbolName(expInOcl.contextVariable, 'self')/][if expInOcl.parameterVariable->notEmpty()] |
| [for (parameter : Variable | expInOcl.parameterVariable)] |
| , <%Value%> [defineSymbolName(parameter, parameter.name)/][/for][/if]) throws <%InvalidValueException%> { |
| [expInOcl.messageExpression.emitExpression(pType, genPackage, expInOcl)/] |
| return [expInOcl.messageExpression.symbolName(expInOcl)/]; |
| } |
| } |
| [/if][/let][else] |
| [expInOcl.generateErrorMessage()/] |
| [/if] |
| }[/let][/let][/template] |
| |
| [template public generateOperationConstraint(expInOcl : ExpressionInOcl, genPackage : GenPackage, pPackage : Package, pType : Type, pOperation : Operation, pConstraint : Constraint)] |
| [let arity : Integer = expInOcl.parameterVariable->size()] |
| [let className : String = '_' + pOperation.name + '_' + pConstraint.stereotype + '_' + pConstraint.name] |
| /** |
| * Implementation of the [pType.name/]::[pOperation.name/] '[pConstraint.name/]' <[pConstraint.stereotype/]>. |
| */ |
| public static class [className/] extends <%Abstract[genPackage.emitOperationArity(arity)/]%> |
| { |
| public static [className/] INSTANCE = new [className/](); |
| |
| [if (expInOcl.contextVariable->notEmpty())] |
| [let constants : String = expInOcl.bodyExpression.gatherConstants(genPackage, expInOcl)] |
| /* |
| [expInOcl.prettyPrint().trim()/] |
| */ |
| public <%Value%> evaluate(<%DomainEvaluator%> evaluator, <%DomainType%> returnType, final <%Value%> [defineSymbolName(expInOcl.contextVariable, 'self')/][if expInOcl.parameterVariable->notEmpty()] |
| [for (parameter : Variable | expInOcl.parameterVariable)] |
| , final <%Value%> [defineSymbolName(parameter, parameter.name)/][/for][/if]) throws <%InvalidValueException%> { |
| [expInOcl.emitConstants(constants)/] |
| [expInOcl.bodyExpression.emitExpression(pType, genPackage, expInOcl)/] |
| return [expInOcl.bodyExpression.symbolName(expInOcl)/]; |
| } |
| [/let] |
| [else] |
| [expInOcl.generateErrorMessage()/] |
| [/if] |
| }[/let][/let][/template] |
| |
| [template public generatePropertyConstraint(expInOcl : ExpressionInOcl, genPackage : GenPackage, pPackage : Package, pType : Type, pProperty : Property, pConstraint : Constraint)] |
| [let className : String = '_' + pProperty.name + '_' + pConstraint.stereotype + '_' + pConstraint.name] |
| /** |
| * Implementation of the [pType.name/]::[pProperty.name/] '[pConstraint.name/]' <[pConstraint.stereotype/]>. |
| */ |
| public static class [className/] extends <%AbstractProperty%> |
| { |
| public static [className/] INSTANCE = new [className/](); |
| |
| [if (expInOcl.contextVariable->notEmpty())] |
| [let constants : String = expInOcl.bodyExpression.gatherConstants(genPackage, expInOcl)] |
| /* |
| [expInOcl.prettyPrint().trim()/] |
| */ |
| public <%Value%> evaluate(<%DomainEvaluator%> evaluator, <%DomainType%> returnType, final <%Value%> [defineSymbolName(expInOcl.contextVariable, 'self')/], <%DomainProperty%> property) throws <%InvalidValueException%> { |
| [expInOcl.emitConstants(constants)/] |
| [expInOcl.bodyExpression.emitExpression(pType, genPackage, expInOcl)/] |
| return [expInOcl.bodyExpression.symbolName(expInOcl)/]; |
| }[/let] |
| [else] |
| [expInOcl.generateErrorMessage()/] |
| [/if] |
| }[/let][/template] |
| |
| [template public generateErrorMessage(expInOcl : ExpressionInOcl)] |
| /* |
| [expInOcl.messageExpression.oclAsType(StringLiteralExp).stringSymbol/] |
| */ |
| [/template] |