blob: df783533d8bcb5040d7668e50da4bf84a5bea064 [file] [log] [blame]
Ed Willinkda1d6d32014-12-06 12:19:12 +00001/*******************************************************************************
2 * Copyright (c) 2013 Willink Transformations Ltd., University of York and others.
3 * All rights reserved. This program and the accompanying materials
Ed Willink2b4d8be2018-06-13 11:53:02 +01004 * are made available under the terms of the Eclipse Public License v2.0
Ed Willinkda1d6d32014-12-06 12:19:12 +00005 * which accompanies this distribution, and is available at
Ed Willink2b4d8be2018-06-13 11:53:02 +01006 * http://www.eclipse.org/legal/epl-v20.html
Ed Willinkda1d6d32014-12-06 12:19:12 +00007 *
8 * Contributors:
9 * Adolfo Sanchez-Barbudo Herrera (University of York) - initial API and implementation
10 *******************************************************************************/
11package org.eclipse.ocl.examples.build.xtend
12
13import org.eclipse.emf.ecore.EClass
14import org.eclipse.emf.ecore.EPackage
Ed Willinkda1d6d32014-12-06 12:19:12 +000015import java.util.List
16import java.util.ArrayList
17import org.eclipse.ocl.xtext.base.cs2as.Continuation
Ed Willink26704252014-12-05 17:14:01 +000018import org.eclipse.ocl.pivot.Element
Ed Willink881d0332013-09-17 15:43:01 +010019import org.eclipse.emf.codegen.ecore.genmodel.GenPackage
Ed Willinkda1d6d32014-12-06 12:19:12 +000020import org.eclipse.ocl.xtext.base.cs2as.CS2ASConversion
21import java.util.Collections
Ed Willink5a6db182013-07-10 18:07:52 +010022
Ed Willink4f0cefa2018-06-17 08:26:42 +010023 class GenerateCS2ASVisitors extends GenerateCSVisitors
Ed Willinkda1d6d32014-12-06 12:19:12 +000024{
Ed Willink70051092015-12-23 19:11:07 +000025 override void generateVisitors(/*@NonNull*/ GenPackage genPackage) {
Ed Willinkda1d6d32014-12-06 12:19:12 +000026 super.generateVisitors(genPackage);
27 if (isDerived()) {
28 generateContainmentVisitor(genPackage);
29 generatePreOrderVisitor(genPackage);
30 generatePostOrderVisitor(genPackage);
31 generateLeft2RightVisitor(genPackage);
32 }
33 }
34
Ed Willink70051092015-12-23 19:11:07 +000035 protected def void generateContainmentVisitor(/*@NonNull*/ GenPackage genPackage) {
Ed Willinkda1d6d32014-12-06 12:19:12 +000036 var EPackage csPackage = genPackage.getEcorePackage();
37 var String visitorVariant = "Containment";
38 var String resultTypeName = "Continuation<?>";
39 var String className = "Abstract" + projectPrefix + visitorVariant + "Visitor";
40 var String extendedClass = superProjectPrefix + visitorVariant + "Visitor";
41 // var String extendedClass = if (superVisitorClassName.length() == 0) {
42// "AbstractExtending" + visitableClassName;
43// } else {
44// visitorPrefix + superVisitorClassName;
45// }
46 var String interfaceName = visitorClassName +'<'+resultTypeName+'>';
47 var List<Class<?>> additionalImports = new ArrayList();
48 additionalImports.add(typeof(Continuation));
49 csPackage.generateContextfulAbstractExtendingVisitor(className, extendedClass,
50 interfaceName, resultTypeName, additionalImports);
51 }
52
Ed Willink70051092015-12-23 19:11:07 +000053 protected def void generatePreOrderVisitor(/*@NonNull*/ GenPackage genPackage) {
Ed Willinkda1d6d32014-12-06 12:19:12 +000054 var EPackage csPackage = genPackage.getEcorePackage();
55 var String visitorVariant = "PreOrder";
56 var String resultTypeName = "Continuation<?>";
57 var String className = "Abstract" + projectPrefix + visitorVariant + "Visitor";
58 var String extendedClass = superProjectPrefix + visitorVariant + "Visitor";
59 var String interfaceName = visitorClassName +'<'+resultTypeName+'>';
60 var List<Class<?>> additionalImports = new ArrayList();
61 additionalImports.add(typeof(Continuation));
62 csPackage.generateContextfulAbstractExtendingVisitor(className, extendedClass,
63 interfaceName, resultTypeName, additionalImports);
64 }
65
Ed Willink70051092015-12-23 19:11:07 +000066 protected def void generatePostOrderVisitor(/*@NonNull*/ GenPackage genPackage) {
Ed Willinkda1d6d32014-12-06 12:19:12 +000067 var EPackage csPackage = genPackage.getEcorePackage();
68 var String visitorVariant = "PostOrder";
69 var String resultTypeName = "Continuation<?>";
70 var String className = "Abstract" + projectPrefix + visitorVariant + "Visitor";
71 var String extendedClass = superProjectPrefix + visitorVariant + "Visitor";
72 var String interfaceName = visitorClassName +'<'+resultTypeName+'>';
73 var List<Class<?>> additionalImports = new ArrayList();
74 additionalImports.add(typeof(Continuation));
75 csPackage.generateContextfulAbstractExtendingVisitor(className, extendedClass,
76 interfaceName, resultTypeName, additionalImports);
77 }
78
Ed Willink70051092015-12-23 19:11:07 +000079 protected def void generateLeft2RightVisitor(/*@NonNull*/ GenPackage genPackage) {
Ed Willinkda1d6d32014-12-06 12:19:12 +000080 var EPackage csPackage = genPackage.getEcorePackage();
81 var String visitorVariant = "Left2Right";
82 var String resultTypeName = "Element";
83 var String className = "Abstract" + projectPrefix + visitorVariant + "Visitor";
84 var String extendedClass = superProjectPrefix + visitorVariant + "Visitor";
85 var String interfaceName = visitorClassName +'<'+resultTypeName+'>';
86 var List<Class<?>> additionalImports = new ArrayList();
87 additionalImports.add(typeof(Element));
88 csPackage.generateContextfulAbstractExtendingVisitor(className, extendedClass,
89 interfaceName, resultTypeName, additionalImports);
90 }
91
92 /**
93 * Assumptions to be considered:
94 * - the package of the extended visitor of generated visitors need to be qualified as follows:
95 * <code> «superProjectName».cs2as </code>
96 */
Ed Willink70051092015-12-23 19:11:07 +000097 protected def void generateContextfulAbstractExtendingVisitor(/*@NonNull*/ EPackage ePackage,
98 /*@NonNull*/ String className, /*@NonNull*/ String extendedClassName, /*@NonNull*/ String interfaceName,
99 /*@NonNull*/ String resultTypeName, /*@NonNull*/ List<Class<?>> additionalImports) {
Ed Willinkda1d6d32014-12-06 12:19:12 +0000100 var List<String> allImports = new ArrayList<String>();
101 allImports.add(typeof(CS2ASConversion).getName());
102 allImports.add(superProjectName + ".cs2as." +extendedClassName);
103 for (Class<?> anImport : additionalImports) {
104 allImports.add(anImport.getName());
105 }
106 Collections.sort(allImports);
107 var boolean needsOverride = needsOverride();
108 var MergeWriter writer = new MergeWriter(outputFolder + className + ".java");
109 writer.append('''
110 «ePackage.generateHeader(visitorPackageName)»
111
112 import org.eclipse.jdt.annotation.NonNull;
113 import org.eclipse.jdt.annotation.Nullable;
114 «FOR anImport : allImports»
115 import «anImport»;
116 «ENDFOR»
117
118 /**
119 * An «className» provides a default implementation for each
120 * visitXxx method that delegates to the visitYyy method of the first
121 * super class, (or transitively its first super class first super class
122 * until a non-interface super-class is found). In the absence of any
123 * suitable first super class, the method delegates to visiting().
124 */
125 public abstract class «className»
126 extends «extendedClassName»
127 implements «interfaceName»
128 {
129 /**
130 * Initializes me with an initial value for my result.
Ed Willink42896aa2021-03-06 17:47:43 +0000131 *
Ed Willinkda1d6d32014-12-06 12:19:12 +0000132 * @param context my initial result value
133 */
134 protected «className»(@NonNull CS2ASConversion context) {
135 super(context);
136 }
137 «FOR eClass : getSortedEClasses(ePackage)»
138 «var EClass firstSuperClass = eClass.firstSuperClass(eClass)»
139
140 «IF needsOverride»
141 @Override
142 «ENDIF»
Ed Willinkda0fc682016-01-05 13:04:43 +0000143 public «emitNullable(resultTypeName)» visit«eClass.name»(«emitNonNull(modelPackageName + "." + eClass.name)» csElement) {
Ed Willinkda1d6d32014-12-06 12:19:12 +0000144 «IF firstSuperClass == eClass»
145 return visiting(csElement);
146 «ELSE»
147 return visit«firstSuperClass.name»(csElement);
148 «ENDIF»
149 }
150 «ENDFOR»
151 }
152 ''');
153 writer.close();
154 }
155}