Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 11ca76a0e265ab2bf31acc9beb1a4451dd29f68e (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
285
286
287
288
289
290
291
/*******************************************************************************
 * Copyright (c) 2008, 2011 Obeo.
 * 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:
 *     Obeo - initial API and implementation
 *******************************************************************************/
package org.eclipse.emf.eef.modelingBot.interpreter;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.runtime.CoreException;
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.util.Diagnostician;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.emf.eef.components.PropertiesEditionContext;
import org.eclipse.emf.eef.extended.editor.ReferenceableObject;
import org.eclipse.emf.eef.modelingBot.Action;
import org.eclipse.emf.eef.modelingBot.DetailsPage;
import org.eclipse.emf.eef.modelingBot.IModelingBot;
import org.eclipse.emf.eef.modelingBot.ModelingBot;
import org.eclipse.emf.eef.modelingBot.Processing;
import org.eclipse.emf.eef.modelingBot.PropertiesView;
import org.eclipse.emf.eef.modelingBot.Scenario;
import org.eclipse.emf.eef.modelingBot.Sequence;
import org.eclipse.emf.eef.modelingBot.SequenceType;
import org.eclipse.emf.eef.modelingBot.Wizard;
import org.eclipse.emf.eef.modelingBot.EEFActions.Add;
import org.eclipse.emf.eef.modelingBot.EEFActions.Cancel;
import org.eclipse.emf.eef.modelingBot.EEFActions.OpenEEFEditor;
import org.eclipse.emf.eef.modelingBot.EEFActions.Remove;
import org.eclipse.emf.eef.modelingBot.EEFActions.SetAttribute;
import org.eclipse.emf.eef.modelingBot.EEFActions.SetReference;
import org.eclipse.emf.eef.modelingBot.EEFActions.Unset;
import org.eclipse.emf.eef.modelingBot.EclipseActions.CloseEditor;
import org.eclipse.emf.eef.modelingBot.EclipseActions.CloseProject;
import org.eclipse.emf.eef.modelingBot.EclipseActions.CreateModel;
import org.eclipse.emf.eef.modelingBot.EclipseActions.CreateProject;
import org.eclipse.emf.eef.modelingBot.EclipseActions.RemoveProject;
import org.eclipse.emf.eef.modelingBot.EclipseActions.Save;
import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException;

/**
 * Interpreter for SWTEEFBot or BatchModelingBot.
 * 
 * @author <a href="mailto:nathalie.lepine@obeo.fr">Nathalie Lepine</a>
 */
public class EEFInterpreter implements IModelingBotInterpreter {

	/**
	 * Editing domain.
	 */
	private EditingDomain editingDomain;

	/**
	 * Map ReFerenceableObject -> EObject created.
	 */
	private Map<ReferenceableObject, EObject> refObjectToEObjectMap = new HashMap<ReferenceableObject, EObject>();

	/**
	 * Map Sequence -> boolean to know of the sequence has been canceled.
	 */
	private Map<Sequence, Boolean> mapSequenceToCancel = new HashMap<Sequence, Boolean>();

	/**
	 * Modeling bot.
	 */
	private IModelingBot bot;

	/**
	 * PropertiesEditionContext.
	 */
	private PropertiesEditionContext propertiesEditionContext;

	/**
	 * Create the interpreter.
	 * 
	 * @param editingDomain
	 *            editing domain
	 * @param bot
	 *            the modelingbot
	 */
	public EEFInterpreter(IModelingBot bot, EditingDomain editingDomain) {
		super();
		this.editingDomain = editingDomain;
		this.bot = bot;
	}

	/**
	 * @return the map of ReferenceableObject
	 */
	public Map<ReferenceableObject, EObject> getRefObjectToEObject() {
		return refObjectToEObjectMap;
	}

	/**
	 * Set the object corresponding to the referenceable object.
	 * 
	 * @param refObjectToEObject
	 *            ReferenceableObject
	 */
	public void setRefObjectToEObject(Map<ReferenceableObject, EObject> refObjectToEObject) {
		this.refObjectToEObjectMap = refObjectToEObject;
	}

	/**
	 * Returns the referenced object.
	 * 
	 * @param ref
	 *            ReferenceableObject
	 * @return the object corresponding to the referenceable object
	 */
	public EObject getEObjectFromReferenceableEObject(ReferenceableObject ref) {
		return refObjectToEObjectMap.get(ref);
	}

	/**
	 * Dispose the maps of the interpreter.
	 */
	public void dispose() {
		refObjectToEObjectMap.clear();
		mapSequenceToCancel.clear();
		refObjectToEObjectMap = null;
		mapSequenceToCancel = null;
	}

	/**
	 * Get the loaded resource.
	 * 
	 * @param path
	 *            path of the model
	 * @return the resource loaded
	 */
	public Resource loadModel(String path) {
		final URI fileURI = URI.createPlatformPluginURI(path, true);
		final Resource resource = editingDomain.getResourceSet().getResource(fileURI, true);
		assertNotNull("The modeling bot resource can not be loaded.", resource);
		return resource;
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.emf.eef.modelingBot.interpreter.IModelingBotInterpreter#runModelingBot(java.lang.String,
	 *      org.eclipse.emf.eef.modelingBot.IModelingBot)
	 */
	public void runModelingBot(String path) throws CoreException, IOException {
		final Resource modelingBotResource = loadModel(path);
		EcoreUtil.resolveAll(modelingBotResource.getResourceSet());
		assertFalse("The modeling bot resource is empty.", modelingBotResource.getContents().isEmpty());
		assertTrue("The modeling bot model contains errors, correct them first", modelingBotResource.getErrors().isEmpty());
		final ModelingBot mbot = (ModelingBot)modelingBotResource.getContents().get(0);
		final Diagnostic diag = Diagnostician.INSTANCE.validate(mbot);
		assertTrue("The modeling bot model contains errors, correct them first", diag.getSeverity() == Diagnostic.OK);
		assertNotNull("The modeling bot resource is empty.", mbot);
		propertiesEditionContext = mbot.getPropertiesEditionContext();
		for (Sequence sequence : mbot.getSequences()) {
			if (sequence instanceof Scenario) {
				final Scenario scenario = (Scenario)sequence;
				runSequence(scenario);
			}
		}
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.emf.eef.modelingBot.interpreter.IModelingBotInterpreter#runSequence(org.eclipse.emf.eef.modelingBot.Sequence)
	 */
	public void runSequence(Sequence sequence) {
		for (Processing processing : sequence.getProcessings()) {
			if (processing instanceof Action) {
				runAction((Action)processing);
			} else if (processing instanceof DetailsPage) {
				bot.setSequenceType(SequenceType.DETAILS_PAGE);
				runSequence((DetailsPage)processing);
			} else if (processing instanceof PropertiesView) {
				bot.setSequenceType(SequenceType.PROPERTIES_VIEW);
				runSequence((PropertiesView)processing);
			} else if (processing instanceof Wizard) {
				bot.setSequenceType(SequenceType.WIZARD);
				runSequence((Wizard)processing);
				finishBatchEditing(processing);
			}
		}
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.emf.eef.modelingBot.interpreter.IModelingBotInterpreter#finishBatchEditing(org.eclipse.emf.eef.modelingBot.Processing)
	 */
	public void finishBatchEditing(Processing processing) {
		final Boolean hasCanceled = mapSequenceToCancel.get(processing);
		if (hasCanceled == null || !hasCanceled) {
			try {
				bot.validateBatchEditing();
			} catch (WidgetNotFoundException e) {
				// Cancel has been done
			}
		}
		mapSequenceToCancel.remove(processing);
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.emf.eef.modelingBot.interpreter.IModelingBotInterpreter#runAction(org.eclipse.emf.eef.modelingBot.Action)
	 */
	public void runAction(Action action) {
		if (action instanceof CreateProject) {
			bot.createProject(((CreateProject)action).getProjectName());
		} else if (action instanceof OpenEEFEditor) {
			bot.openEEFEditor(((OpenEEFEditor)action).getEditorName());
		} else if (action instanceof CreateModel) {
			final EObject addedObject = bot.createModel(((CreateModel)action).getPath(),
					((CreateModel)action).getModelName(), ((CreateModel)action).getRoot());
			addModelMap((CreateModel)action, addedObject);
		} else if (action instanceof Add) {
			final EObject addedObject = bot.add(((Add)action).getPropertiesEditionElement(),
					((Add)action).getReferenceableObject(), ((Add)action).getEContainingFeature(),
					((Add)action).getType());
			addActionMap((Add)action, addedObject);
		} else if (action instanceof SetAttribute) {
			bot.set(((SetAttribute)action).getPropertiesEditionElement(),
					((SetAttribute)action).getReferenceableObject(), ((SetAttribute)action).getEContainingFeature(),
					((SetAttribute)action).getValue());
		} else if (action instanceof SetReference) {
			bot.set(((SetReference)action).getPropertiesEditionElement(),
					((SetReference)action).getReferenceableObject(), ((SetReference)action).getEContainingFeature(),
					((SetReference)action).getValue());
		} else if (action instanceof Save) {
			bot.save();
		} else if (action instanceof CloseEditor) {
			bot.closeEditor(((CloseEditor)action).getPath());
		} else if (action instanceof CloseProject) {
			bot.closeProject(((CloseProject)action).getProjectName());
		} else if (action instanceof RemoveProject) {
			bot.removeProject(((RemoveProject)action).getProjectName());
		} else if (action instanceof Cancel) {
			mapSequenceToCancel.put((Sequence)action.eContainer(), true);
			bot.cancel();
		} else if (action instanceof Unset) {
			bot.unset(((Unset)action).getPropertiesEditionElement(), ((Unset)action).getReferenceableObject(),
					((Unset)action).getFeature());
		} else if (action instanceof Remove) {
			bot.remove(((Remove)action).getPropertiesEditionElement(), ((Remove)action).getReferenceableObject());
			refObjectToEObjectMap.remove(((Remove)action).getReferenceableObject());
		}
	}

	private void addActionMap(ReferenceableObject action, EObject obj) {
		refObjectToEObjectMap.put(action, obj);
	}

	private void addModelMap(CreateModel action, EObject obj) {
		refObjectToEObjectMap.put(action, obj);
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.emf.eef.modelingBot.interpreter.IModelingBotInterpreter#getPropertiesEditionContext()
	 */
	public PropertiesEditionContext getPropertiesEditionContext() {
		return propertiesEditionContext;
	}

	/**
	 * @param pec
	 *            the propertiesEditionContext to set
	 */
	public void setPropertiesEditionContext(PropertiesEditionContext pec) {
		this.propertiesEditionContext = pec;
	}

}

Back to the top