Skip to main content
summaryrefslogtreecommitdiffstats
blob: a90cf90b1c0ad1b485820a1903618ab16149d9a3 (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
/*******************************************************************************
 * Copyright (c) 2013 The University of York, 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:
 *     Horacio Hoyos - initial API and implementation
 ******************************************************************************/
package org.eclipse.qvtd.compiler.internal.etl;

import java.net.URI;
import java.util.ArrayList;

import org.eclipse.epsilon.common.parse.problem.ParseProblem;
import org.eclipse.epsilon.eol.IEolExecutableModule;
import org.eclipse.epsilon.eol.exceptions.EolRuntimeException;
import org.eclipse.epsilon.eol.models.IModel;
import org.eclipse.epsilon.flock.FlockModule;
import org.eclipse.epsilon.flock.FlockResult;
import org.eclipse.epsilon.flock.IFlockContext;
import org.eclipse.epsilon.flock.execution.exceptions.FlockUnsupportedModelException;

/**
 * The FlockTask is used to execute Epsilon Flock scripts in standalone mode.
 */
public class FlockTask extends EpsilonTask {
	
	/** The original model. */
	private IModel originalModel;
	
	/** The migrated model. */
	private IModel migratedModel;
	
	/**
	 * Instantiates a new flock task.
	 *
	 * @param flockSourceURI the etl source uri
	 */
	public FlockTask(URI flockSourceURI) {
		super();
		this.sourceURI = flockSourceURI;
		models = new ArrayList<IModel>();
	}

	/**
	 * Instantiates a new flock task.
	 *
	 * @param flockSourcePath the etl source path
	 */
	public FlockTask(String flockSourcePath) {
		super();
		this.sourceURI = URI.create(flockSourcePath);
		models = new ArrayList<IModel>();
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.qvtd.build.etl.EpsilonTask#createModule()
	 */
	@Override
	public IEolExecutableModule createModule() {
		
		return new FlockModule();
	}

	/**
	 * Gets the original model.
	 *
	 * @return the original model
	 */
	public IModel getOriginalModel() {
		return originalModel;
	}

	/**
	 * Sets the original model.
	 *
	 * @param originalModel the new original model
	 */
	public void setOriginalModel(IModel originalModel) {
		this.originalModel = originalModel;
		addModel(originalModel);
	}

	/**
	 * Gets the migrated model.
	 *
	 * @return the migrated model
	 */
	public IModel getMigratedModel() {
		return migratedModel;
	}

	/**
	 * Sets the migrated model.
	 *
	 * @param migratedModel the new migrated model
	 */
	public void setMigratedModel(IModel migratedModel) {
		this.migratedModel = migratedModel;
		addModel(migratedModel);
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.qvtd.build.etl.EpsilonTask#execute()
	 */
	@Override
	public void execute() throws QvtMtcExecutionException {
		
		module = createModule();
		try {
			module.parse(sourceURI);
		} catch (Exception e) {
			throw new QvtMtcExecutionException("There was an error loading the Flock script.", e.getCause());
		}
		if (module.getParseProblems().size() > 0) {
			StringBuilder sb = new StringBuilder();
			for (ParseProblem problem : module.getParseProblems()) {
				sb.append(problem.toString() + "\\n");
			}
			throw new QvtMtcExecutionException("Parse errors occured: " + sb.toString());
		}
		for (IModel model : getModels()) {
			module.getContext().getModelRepository().addModel(model);
		}
		
		try {
			((IFlockContext)module.getContext()).setOriginalModel(originalModel);
			((IFlockContext)module.getContext()).setMigratedModel(migratedModel);
		} catch (FlockUnsupportedModelException e) {
			throw new QvtMtcExecutionException(e.getMessage(),e.getCause());
		}
		preProcess();
		try {
			result = module.execute();
		} catch (EolRuntimeException e) {
			throw new QvtMtcExecutionException(e.getMessage(),e.getCause());
		}
		postProcess();
		for (IModel model : getModels()) {
			if (model.isStoredOnDisposal()) {
				try {
					model.store();
				}
				catch (Exception e) {
					throw new QvtMtcExecutionException(e.getMessage(),e.getCause());
				}
			}
			module.getContext().getModelRepository().removeModel(model);
		}
	}

	@Override
	public void postProcess() {
		// TODO Auto-generated method stub
		super.postProcess();
		((FlockResult)result).printWarnings(System.out);
	}
	
	
	
}

Back to the top