Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 7e664840de42b20732b7d848b123e8720d59a26a (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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*****************************************************************************
 * Copyright (c) 2010, 2014 CEA LIST 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:
 *  Camille Letavernier (CEA LIST) camille.letavernier@cea.fr - Initial API and implementation
 *  Thibault Le Ouay t.leouay@sherpa-eng.com - Strategy improvement of generated files
 *  Christian W. Damus (CEA) - bug 422257
 *
 *****************************************************************************/
package org.eclipse.papyrus.customization.properties.generation.generators;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.emf.common.util.BasicDiagnostic;
import org.eclipse.emf.common.util.Diagnostic;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.m2m.qvt.oml.BasicModelExtent;
import org.eclipse.m2m.qvt.oml.ExecutionContextImpl;
import org.eclipse.m2m.qvt.oml.ExecutionDiagnostic;
import org.eclipse.m2m.qvt.oml.ModelExtent;
import org.eclipse.m2m.qvt.oml.TransformationExecutor;
import org.eclipse.papyrus.customization.properties.generation.Activator;
import org.eclipse.papyrus.infra.emf.utils.EMFHelper;
import org.eclipse.papyrus.infra.properties.contexts.Context;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;

/**
 * An Abstract generator based on QVTO transformations.
 * Subclasses should specify the .qvto file and ModelExtents, as well as the
 * SWT widgets allowing the user to chose the input models.
 *
 * @author Camille Letavernier
 */
public abstract class AbstractQVTGenerator implements IGenerator, Listener {

	/**
	 * The Contexts created by the transformation.
	 */
	protected List<Context> generatedContexts;

	/**
	 * The output ModelExtent
	 */
	protected ModelExtent out;

	private Set<Listener> listeners = new HashSet<Listener>();

	private int strategy;

	private Collection<ResourceSet> scratchResourceSets;

	public List<Context> generate(List<URI> targetURI) {

		switch (strategy) {
		case 0:
			generatedContexts = generateSameFile(targetURI);
			break;
		case 1:
			generatedContexts = generateSameFile(targetURI);
			break;
		case 2:
			generatedContexts = generateDifferentFile(targetURI);
			break;
		default:
			generatedContexts = null;
		}
		return generatedContexts;
	}

	public void dispose() {
		if (scratchResourceSets != null) {
			for (ResourceSet next : scratchResourceSets) {
				EMFHelper.unload(next);
			}
			scratchResourceSets = null;
		}
	}



	/**
	 * @return the list of in/out/inout ModelExtents (including the OutContextExtent)
	 *         Implementors should ensure they add the outContextExtent to the list.
	 */
	abstract protected List<ModelExtent> getModelExtents();

	/**
	 * @return the ModelExtent containing the generated context
	 */
	protected ModelExtent getOutContextExtent() {
		if (out == null) {
			out = new BasicModelExtent();
		}

		return out;
	}

	/**
	 * @return the URI of the QVTO transformation file.
	 */
	abstract protected URI getTransformationURI();

	/**
	 * Loads the EObject from the given URI.
	 *
	 * @param uri
	 *            The URI from which the EObject is loaded
	 * @return
	 * 		The loaded EObject, or null if an error occured
	 * @throws IOException
	 *             If the URI isn't a valid EObject
	 */
	protected EObject loadEMFModel(URI uri) throws IOException {
		ResourceSet resourceSet = createResourceSet();
		try {
			Resource resource = resourceSet.getResource(uri, true);
			if (resource != null) {
				if (!resource.getContents().isEmpty()) {
					return resource.getContents().get(0);
				}
			}
		} catch (Exception ex) {
			throw new IOException(ex.toString());
		}

		return null;
	}

	protected final ResourceSet createResourceSet() {
		ResourceSet result = new ResourceSetImpl();
		if (scratchResourceSets == null) {
			scratchResourceSets = new ArrayList<ResourceSet>();
		}
		scratchResourceSets.add(result);
		return result;
	}

	public void addListener(Listener listener) {
		listeners.add(listener);
	}

	public void removeListener(Listener listener) {
		listeners.remove(listener);
	}

	public void handleEvent(Event event) {
		for (Listener listener : listeners) {
			listener.handleEvent(event);
		}
	}

	/**
	 * Return the generated Context from a list of EObjects
	 *
	 * @param outObjects
	 *            The list of EObjects from which the context will be retrieved
	 * @return
	 * 		The main generated context
	 */
	protected List<Context> getContexts(List<EObject> outObjects) {
		List<Context> result = new LinkedList<Context>();

		for (Object objectResult : outObjects) {
			if (objectResult instanceof Context) {
				result.add((Context) objectResult);
			}
		}

		return result;
	}


	public abstract IObservableValue getObservableValue();

	public void setStrategy(int strategy) {
		this.strategy = strategy;
	}

	private List<Context> generateSameFile(List<URI> targetURI) {

		URI transformationURI = getTransformationURI();

		TransformationExecutor executor = new TransformationExecutor(transformationURI);
		Diagnostic diagnostic = executor.loadTransformation();
		if (diagnostic.getSeverity() != Diagnostic.OK) {
			Activator.log.warn("Cannot load the transformation : " + transformationURI);
			return generatedContexts = null;
		}
		List<ModelExtent> extents = getModelExtents();


		ExecutionContextImpl context = new ExecutionContextImpl();
		context.setConfigProperty("keepModeling", true); //$NON-NLS-1$

		// context.setLog(new WriterLog(new OutputStreamWriter(System.out)));

		ExecutionDiagnostic result = executor.execute(context, extents.toArray(new ModelExtent[0]));

		if (result.getSeverity() == org.eclipse.emf.common.util.Diagnostic.OK) {
			List<EObject> outObjects = getOutContextExtent().getContents();
			Object objectResult = outObjects.get(0);
			if (!(objectResult instanceof Context)) {
				return null;
			}

			ResourceSet resourceSet = createResourceSet();
			Resource contextResource = resourceSet.createResource(targetURI.get(0));
			contextResource.getContents().addAll(outObjects);

			return generatedContexts = getContexts(outObjects);
		} else {
			IStatus status = BasicDiagnostic.toIStatus(result);
			Activator.log.warn(String.format("%s : %s", status.getPlugin(), status.getMessage()));
		}
		return generatedContexts = null;
	}

	protected abstract List<ModelExtent> getModelExtents(int i);

	private List<Context> generateDifferentFile(List<URI> targetURI) {

		URI transformationURI = getTransformationURI();

		TransformationExecutor executor = new TransformationExecutor(transformationURI);
		Diagnostic diagnostic = executor.loadTransformation();
		if (diagnostic.getSeverity() != Diagnostic.OK) {
			Activator.log.warn("Cannot load the transformation : " + transformationURI);
			return generatedContexts = null;
		}
		List<ModelExtent> extents = null;
		ExecutionContextImpl context = new ExecutionContextImpl();
		context.setConfigProperty("keepModeling", true); //$NON-NLS-1$
		// context.setLog(new WriterLog(new OutputStreamWriter(System.out)));
		List<Context> temp = new LinkedList<Context>();

		for (int i = 0; i < targetURI.size(); i++) {
			extents = getModelExtents(i);



			ExecutionDiagnostic result = executor.execute(context, extents.toArray(new ModelExtent[0]));

			if (result.getSeverity() == org.eclipse.emf.common.util.Diagnostic.OK) {
				List<EObject> outObjects = getOutContextExtent().getContents();
				Object objectResult = outObjects.get(0);
				if (!(objectResult instanceof Context)) {
					return null;
				}
				ResourceSet resourceSet = createResourceSet();
				Resource contextResource = resourceSet.createResource(targetURI.get(i));
				contextResource.getContents().addAll(outObjects);
				temp.addAll(getContexts(outObjects));

			} else {
				IStatus status = BasicDiagnostic.toIStatus(result);
				Activator.log.warn(String.format("%s : %s", status.getPlugin(), status.getMessage()));
			}
		}

		return temp;

	}



}

Back to the top