aboutsummaryrefslogtreecommitdiffstats
path: root/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/api/SolutionTrajectory.java
blob: d1a410655345b25b84b6f0f82ddf103d663975ef (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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*******************************************************************************
 * Copyright (c) 2010-2014, Miklos Foldenyi, Andras Szabolcs Nagy, Abel Hegedus, Akos Horvath, Zoltan Ujhelyi and Daniel Varro
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v. 2.0 which is available at
 * http://www.eclipse.org/legal/epl-v20.html.
 * 
 * SPDX-License-Identifier: EPL-2.0
 *******************************************************************************/
package org.eclipse.viatra.dse.api;

import java.lang.reflect.InvocationTargetException;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.function.Consumer;

import org.eclipse.emf.common.notify.Notifier;
import org.eclipse.emf.edit.command.ChangeCommand;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.viatra.dse.base.DseIdPoolHelper;
import org.eclipse.viatra.dse.designspace.api.IBacktrackListener;
import org.eclipse.viatra.dse.objectives.Fitness;
import org.eclipse.viatra.dse.statecode.IStateCoder;
import org.eclipse.viatra.dse.statecode.IStateCoderFactory;
import org.eclipse.viatra.dse.util.EMFHelper;
import org.eclipse.viatra.query.runtime.api.AdvancedViatraQueryEngine;
import org.eclipse.viatra.query.runtime.api.IPatternMatch;
import org.eclipse.viatra.query.runtime.api.ViatraQueryEngine;
import org.eclipse.viatra.query.runtime.api.ViatraQueryMatcher;
import org.eclipse.viatra.query.runtime.emf.EMFScope;
import org.eclipse.viatra.query.runtime.matchers.ViatraQueryRuntimeException;
import org.eclipse.viatra.query.runtime.matchers.util.Preconditions;
import org.eclipse.viatra.transformation.runtime.emf.rules.batch.BatchTransformationRule;

import com.google.common.util.concurrent.UncheckedExecutionException;

/**
 * A SolutionTrajectory represents a trajectory (i.e. sequence of transformation
 * rule applications), which can transform the initial model to a desired state.
 * An instance of this class holds the the actual rule sequence and the
 * corresponding activation codes. Furthermore it can be used to perform the
 * transformation on a given model (if possible).
 * <p>
 * It is also possible to undo the transformation if initialized with an editing
 * domain.
 * <p>
 * The instance of this class can be reused for different models.
 * 
 * @author Andras Szabolcs Nagy
 *
 */
public class SolutionTrajectory {

	private final List<Object> activationCodes;
	private final List<BatchTransformationRule<?, ?>> transformationRules;
	private final IStateCoderFactory stateCoderFactory;
	private Fitness fitness;
	private Solution solution;

	private ViatraQueryEngine engine;
	private Notifier model;
	private EditingDomain editingDomain;
	private IStateCoder stateCoder;
	private IBacktrackListener listener;

	private int currentIndex;

	public SolutionTrajectory(final List<Object> activationCodes,
			final List<BatchTransformationRule<?, ?>> transformationRules, final IStateCoderFactory stateCoderFactory,
			final IBacktrackListener backtrackListener) {
		Objects.requireNonNull(transformationRules, "Parameter transformationRules cannot be null!");
		Objects.requireNonNull(stateCoderFactory, "Parameter stateCoderFactory cannot be null!");
		Objects.requireNonNull(activationCodes, "Parameter activations cannot be null!");
		Preconditions.checkState(transformationRules.size() == activationCodes.size(),
				"The two List parameters must be the same in size.");

		this.activationCodes = activationCodes;
		this.transformationRules = transformationRules;
		this.stateCoderFactory = stateCoderFactory;
		this.listener = backtrackListener;
		currentIndex = 0;
	}

	/**
	 * Initialize this SolutionTrajectory for transforming the model along the
	 * trajectory.
	 * 
	 * @param model The model.
	 * @throws ViatraQueryRuntimeException If the VIATRA Query fails to initialize.
	 */
	public void setModel(Notifier model) {
		editingDomain = null;
		EMFScope scope = new EMFScope(model);
		this.engine = ViatraQueryEngine.on(scope);
		this.model = model;
		stateCoder = stateCoderFactory.createStateCoder();
		stateCoder.init(model);
		currentIndex = 0;
		DseIdPoolHelper.INSTANCE.disposeByThread();
		DseIdPoolHelper.INSTANCE.registerRules(rule -> {
			int id = 0;
			for (BatchTransformationRule<?, ?> r : transformationRules.subList(0, currentIndex)) {
				if (r.equals(rule)) {
					id++;
				}
			}
			return id;
		}, new HashSet<BatchTransformationRule<?, ?>>(transformationRules));
	}

	/**
	 * Initialize this SolutionTrajectory for transforming the given model along the
	 * trajectory.
	 * <p>
	 * The transformation will be reversible by creating an {@link EditingDomain} on
	 * the model.
	 * 
	 * @param modelRoot The root of the model.
	 * @throws ViatraQueryRuntimeException If the VIATRA Query fails to initialize.
	 */
	public void setModelWithEditingDomain(Notifier modelRoot) {
		setModel(modelRoot);
		editingDomain = EMFHelper.createEditingDomain(model);
	}

	/**
	 * Transforms the given model along the trajectory.
	 * 
	 * @param modelRoot The root of the model.
	 * @throws ViatraQueryRuntimeException If the VIATRA Query fails to initialize.
	 */
	public void doTransformation(Notifier modelRoot) {
		setModel(modelRoot);
		doTransformation();
	}

	/**
	 * Transforms the given model along the trajectory.
	 * <p>
	 * The transformation will be reversible by creating an {@link EditingDomain} on
	 * the model.
	 * 
	 * @param modelRoot The root of the model.
	 * @throws ViatraQueryRuntimeException If the VIATRA Query fails to initialize.
	 */
	public void doTransformationUndoable(Notifier modelRoot) {
		setModelWithEditingDomain(modelRoot);
		doTransformation();
	}

	/**
	 * Transforms the given model along the trajectory. To initialize the model call
	 * the {@link SolutionTrajectory#setModel(Notifier)} method.
	 * 
	 * @throws Exception                   If the activation to fire is not found.
	 *                                     Possible problems: wrong model, bad state
	 *                                     serializer.
	 * @throws ViatraQueryRuntimeException If the VIATRA Query fails to initialize.
	 */
	public void doTransformation() {
		while (doNextTransformation())
			;
	}

	/**
	 * Transforms the given model by one step to the solution (makes one step in the
	 * trajectory). To initialize the model call the
	 * {@link SolutionTrajectory#setModel(Notifier)} method.
	 * 
	 * @throws ViatraQueryRuntimeException
	 */
	public boolean doNextTransformation() {
		if (currentIndex >= activationCodes.size()) {
			return false;
		} else {
			doNextTransformation(currentIndex);
			currentIndex++;
			return true;
		}
	}

	@SuppressWarnings("unchecked")
	private void doNextTransformation(int index) {
		Objects.requireNonNull(model, "The model cannot be null! Use the setModel method.");

		// cast for the ".process(match)" method.
		BatchTransformationRule<?, ?> tr = transformationRules.get(index);
		Object activationCode = activationCodes.get(index);

		ViatraQueryMatcher<?> matcher = tr.getPrecondition().getMatcher(engine);

		boolean isActivationFound = false;
		for (final IPatternMatch match : matcher.getAllMatches()) {
			Object matchHash = stateCoder.createActivationCode(match);
			if (matchHash.equals(activationCode)) {
				@SuppressWarnings("rawtypes")
				final Consumer action = tr.getAction();

				if (editingDomain == null) {
					action.accept(match);
				} else {
					ChangeCommand cc = new ChangeCommand(model) {
						@Override
						protected void doExecute() {
							action.accept(match);
						}
					};
					long start = System.nanoTime();
					editingDomain.getCommandStack().execute(cc);
					listener.forwardWorked(System.nanoTime() - start);
				}

				isActivationFound = true;
				break;
			}
		}
		if (!isActivationFound) {
			throw new UncheckedExecutionException(
					"Activation was not found for transformation! Possible cause: wrong model, bad state coder. index: "
							+ index + " Activation code: " + activationCode,
					null);
		}
	}

	/**
	 * Call this method to undo the last transformation.
	 * 
	 * @return True, if it was successful.
	 */
	public boolean undoLastTransformation() {
		Objects.requireNonNull(editingDomain, "To be able to undo the transformation initialize with editing domain.");
		long start = System.nanoTime();
		boolean result;
		
		if (currentIndex > 0) {
			try {
				((AdvancedViatraQueryEngine) engine).delayUpdatePropagation(() -> {
					editingDomain.getCommandStack().undo();
					return null;
				});
			} catch (InvocationTargetException e) {
				throw new RuntimeException(e);
			}
			currentIndex--;
			result = true;
		}
		result = false;
		listener.backtrackWorked(System.nanoTime() - start);
		return result;
	}

	/**
	 * Call this method to undo the transformation.
	 */
	public void undoTransformation() {
		while (undoLastTransformation())
			;
	}

	public List<Object> getActivationCodes() {
		return activationCodes;
	}

	public List<BatchTransformationRule<?, ?>> getTransformationRules() {
		return transformationRules;
	}

	public IStateCoderFactory getStateCoderFactory() {
		return stateCoderFactory;
	}

	public ViatraQueryEngine getEngine() {
		return engine;
	}

	public Notifier getModel() {
		return model;
	}

	public IStateCoder getStateCoder() {
		return stateCoder;
	}

	public int getCurrentIndex() {
		return currentIndex;
	}

	public int getTrajectoryLength() {
		return activationCodes.size();
	}

	public Fitness getFitness() {
		return fitness;
	}

	public void setFitness(Fitness fitness) {
		this.fitness = fitness;
	}

	public String toPrettyString() {
		StringBuilder sb = new StringBuilder();
		sb.append("Fitness: ");
		sb.append(fitness.toString());
		sb.append(" | Trajectory (");
		sb.append(activationCodes.size());
		sb.append("): ");
		for (Object object : activationCodes) {
			sb.append(object.toString());
			sb.append(" | ");
		}
		return sb.toString();
	}

	@Override
	public int hashCode() {
		return activationCodes.hashCode();
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj instanceof SolutionTrajectory) {
			SolutionTrajectory that = (SolutionTrajectory) obj;
			return activationCodes.equals(that.activationCodes);
		}
		return false;
	}

	public Solution getSolution() {
		return solution;
	}

	public void setSolution(Solution solution) {
		this.solution = solution;
	}
}