Skip to main content
summaryrefslogtreecommitdiffstats
blob: 05422507be6ea17fed4cad78184adcda3fdf39e5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*******************************************************************************
 * Copyright (c) 2015, 2016 Willink Transformations 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
 ******************************************************************************/
package org.eclipse.qvtd.compiler;

import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.URIUtil;
import org.eclipse.emf.codegen.ecore.genmodel.GenModel;
import org.eclipse.emf.codegen.ecore.genmodel.GenPackage;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.plugin.EcorePlugin;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.xmi.XMLResource;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.ocl.pivot.OperationCallExp;
import org.eclipse.ocl.pivot.internal.resource.StandaloneProjectMap;
import org.eclipse.ocl.pivot.internal.utilities.PivotUtilInternal;
import org.eclipse.ocl.pivot.utilities.ClassUtil;
import org.eclipse.ocl.pivot.utilities.XMIUtil;
import org.eclipse.qvtd.compiler.internal.qvtc2qvtu.QVTuConfiguration;
import org.eclipse.qvtd.compiler.internal.qvtr2qvtc.QVTr2QVTc;
import org.eclipse.qvtd.compiler.internal.utilities.JavaSourceFileObject;
import org.eclipse.qvtd.pivot.qvtbase.utilities.QVTbaseEnvironmentFactory.CreateStrategy;
import org.eclipse.qvtd.pivot.qvtbase.utilities.QVTbaseUtil;
import org.eclipse.qvtd.pivot.qvtimperative.ImperativeTransformation;
import org.eclipse.qvtd.pivot.qvtimperative.evaluation.QVTiEnvironmentFactory;
import org.eclipse.qvtd.pivot.qvtrelation.utilities.QVTrEnvironmentFactory;
import org.eclipse.qvtd.pivot.qvtrelation.utilities.QVTrelationUtil;

/**
 * The QVTcCompilerChain supports generation of a QVTi Transformation from a QVTc Transformation.
 */
public class QVTrCompilerChain extends AbstractCompilerChain
{
	protected static class Xtext2QVTrCompilerStep extends AbstractCompilerStep
	{
		public Xtext2QVTrCompilerStep(@NonNull CompilerChain compilerChain) {
			super(compilerChain, QVTR_STEP);
		}

		public @NonNull Resource execute(@NonNull URI txURI) throws IOException {
			//			Transformation loadTransformation = QVTrelationUtil.loadTransformation(environmentFactory, txURI, false);
			//			Resource rResource = ClassUtil.nonNullState(loadTransformation.eResource());
			Resource rResource = QVTrelationUtil.loadTransformations(environmentFactory, txURI, false);
			// FIXME Following code fixes up missing source. Should be fixed earlier.
			List<OperationCallExp> missingOperationCallSources = QVTbaseUtil.rewriteMissingOperationCallSources(environmentFactory, rResource);
			if (missingOperationCallSources != null) {
				System.err.println("Missing OperationCallExp sources  were fixed up for '" + txURI + "'");
			}
			compiled(rResource);
			//			saveResource(rResource, QVTR_STEP); -- causes test failures
			return rResource;
		}
	}

	protected static class QVTr2QVTcCompilerStep extends AbstractCompilerStep
	{
		public QVTr2QVTcCompilerStep(@NonNull CompilerChain compilerChain) {
			super(compilerChain, QVTC_STEP);
		}

		public @NonNull Resource execute(@NonNull Resource rResource) throws IOException {
			CreateStrategy savedStrategy = environmentFactory.setCreateStrategy(QVTrEnvironmentFactory.CREATE_STRATEGY);
			try {
				//		URI classURI = getURI(CLASS_STEP, URI_KEY);
				URI classURI = compilerChain.getOption(CLASS_STEP, URI_KEY);
				URI qvtcURI = compilerChain.getURI(QVTC_STEP, URI_KEY);
				URI traceURI = compilerChain.getURI(TRACE_STEP, URI_KEY);
				URI genModelURI = compilerChain.getURI(GENMODEL_STEP, URI_KEY);
				Map<@NonNull String, @Nullable String> traceOptions = compilerChain.getOption(TRACE_STEP, TRACE_OPTIONS_KEY);
				String traceNsURI = traceOptions != null ? traceOptions.get(TRACE_NS_URI) : null;
				Resource cResource = createResource(qvtcURI);
				Resource traceResource = createResource(PivotUtilInternal.getASURI(traceURI));
				QVTr2QVTc t = new QVTr2QVTc(environmentFactory, rResource, cResource);
				if (traceNsURI != null) {
					t.setTraceNsURI(traceNsURI);
				}
				t.prepare();
				//
				t.transformToTracePackages();
				Map<Object, Object> saveOptions = compilerChain.getOption(TRACE_STEP, SAVE_OPTIONS_KEY);
				if (saveOptions == null) {
					saveOptions = XMIUtil.createSaveOptions();
				}
				t.saveTrace(traceResource, traceURI, genModelURI, traceOptions, saveOptions);
				assertNoResourceErrors("Trace save", traceResource);
				compiled(TRACE_STEP, cResource);
				//
				t.transformToCoreTransformations();
				saveOptions = compilerChain.getOption(QVTR_STEP, SAVE_OPTIONS_KEY);
				if (saveOptions == null) {
					saveOptions = XMIUtil.createSaveOptions();
				}
				t.saveCore(cResource, saveOptions);
				assertNoResourceErrors("Core save", cResource);
				compiled(QVTC_STEP, cResource);

				if (getOption(DEBUG_KEY) == Boolean.TRUE) {
					URI txURI = cResource.getURI();
					if (txURI != null) {
						URI inputURI = txURI;
						URI serializedURI = txURI.trimFileExtension().appendFileExtension("serialized.qvtc");
						doQVTcSerializeAndLoad(inputURI, serializedURI);
					}
				}
				// FIXME next few lines should be classURI != null, but a test fails in combination (? due to global registry leakage)
				saveOptions = compilerChain.getOption(GENMODEL_STEP, SAVE_OPTIONS_KEY);
				if (saveOptions == null) {
					saveOptions = XMIUtil.createSaveOptions();
				}
				saveOptions.put(XMLResource.OPTION_USE_ENCODED_ATTRIBUTE_STYLE, Boolean.TRUE);
				Collection<@NonNull ? extends GenPackage> usedGenPackages = compilerChain.getOption(GENMODEL_STEP, GENMODEL_USED_GENPACKAGES_KEY);
				GenModel genModel = t.saveGenModel(traceResource, traceURI, genModelURI, compilerChain.getOption(GENMODEL_STEP, GENMODEL_OPTIONS_KEY), saveOptions, usedGenPackages);

				if (classURI != null) {
					t.generateModels(genModel);
					String binProjectName = t.getProjectName(traceURI);
					File binFile;
					String objectPath;
					String sourcePathPrefix;
					List<@NonNull String> classpathProjects;
					if (EcorePlugin.IS_ECLIPSE_RUNNING) {
						IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
						IProject binProject = root.getProject(binProjectName);
						IFolder binFolder = binProject.getFolder("bin");
						binFile = URIUtil.toFile(binFolder.getLocationURI());
						objectPath = binFile.toString()/*.replace(".", "/")*/.replace("\\", "/");		// FIXME deduce/parameterize bin
						IFile genIFile = root.getFile(new Path(genModel.getModelDirectory()));
						File genFile = URIUtil.toFile(genIFile.getLocationURI());
						sourcePathPrefix = genFile.getAbsolutePath().replace("\\", "/");
						classpathProjects = JavaSourceFileObject.createClasspathProjectList("org.eclipse.emf.common", "org.eclipse.emf.ecore", "org.eclipse.jdt.annotation", "org.eclipse.ocl.pivot", "org.eclipse.osgi", "org.eclipse.qvtd.runtime");
					}
					else {
						ResourceSet resourceSet = environmentFactory.getResourceSet();
						URI normalizedClassURI = resourceSet.getURIConverter().normalize(classURI);
						objectPath = normalizedClassURI.toFileString();
						URI location = ClassUtil.nonNullState(((StandaloneProjectMap)environmentFactory.getProjectManager()).getLocation(binProjectName));
						binFile = new File(location.appendSegment("bin").toFileString());
						URI genModelDirectoryURI = URI.createPlatformResourceURI(genModel.getModelDirectory(), true);
						sourcePathPrefix = resourceSet.getURIConverter().normalize(genModelDirectoryURI).toFileString() + "/";
						classpathProjects = null;
					}
					assert objectPath != null;
					binFile.mkdir();
					for (GenPackage genPackage : genModel.getGenPackages()) {
						String basePackage = genPackage.getBasePackage();
						String sourcePath = sourcePathPrefix + (basePackage != null ? ("/" + basePackage.replace(".", "/")) : "");
						JavaSourceFileObject.compileClasses(sourcePath, objectPath, classpathProjects);
					}
				}
				compiled(GENMODEL_STEP, cResource);
				return cResource;
			}
			finally {
				environmentFactory.setCreateStrategy(savedStrategy);
			}
		}
	}

	protected final @NonNull Xtext2QVTrCompilerStep xtext2qvtrCompilerStep;
	protected final @NonNull QVTr2QVTcCompilerStep qvtr2qvtcCompilerStep;

	public QVTrCompilerChain(@NonNull QVTiEnvironmentFactory environmentFactory, @NonNull URI txURI, @Nullable Map<@NonNull String, @Nullable Map<@NonNull Key<Object>, @Nullable Object>> options) {
		super(environmentFactory, txURI, options);
		this.xtext2qvtrCompilerStep = createXtext2QVTrCompilerStep();
		this.qvtr2qvtcCompilerStep = createQVTr2QVTcCompilerStep();
	}

	@Override
	public @NonNull ImperativeTransformation compile(@NonNull String enforcedOutputName) throws IOException {
		Resource rResource = xtext2qvtrCompilerStep.execute(txURI);
		Resource cResource = qvtr2qvtcCompilerStep.execute(rResource);
		QVTuConfiguration qvtuConfiguration = createQVTuConfiguration(cResource, QVTuConfiguration.Mode.ENFORCE, enforcedOutputName);
		//		setOption(QVTU_STEP, QVTU_CONFIGURATION_KEY, qvtuConfiguration);
		Resource pResource = qvtc2qvtp(cResource, qvtuConfiguration);
		return qvtp2qvti(pResource);
	}

	protected @NonNull QVTr2QVTcCompilerStep createQVTr2QVTcCompilerStep() {
		return new QVTr2QVTcCompilerStep(this);
	}

	protected @NonNull Xtext2QVTrCompilerStep createXtext2QVTrCompilerStep() {
		return new Xtext2QVTrCompilerStep(this);
	}
}

Back to the top