aboutsummaryrefslogtreecommitdiffstats
path: root/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/base/DesignSpaceManager.java
blob: 7e7a6e510d4b8948f75ccbcd0a5e6a14ec15c63e (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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
/*******************************************************************************
 * 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.base;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.apache.log4j.Logger;
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.api.DSEException;
import org.eclipse.viatra.dse.api.SolutionTrajectory;
import org.eclipse.viatra.dse.designspace.api.IBacktrackListener;
import org.eclipse.viatra.dse.designspace.api.IDesignSpace;
import org.eclipse.viatra.dse.designspace.api.TrajectoryInfo;
import org.eclipse.viatra.dse.objectives.ActivationFitnessProcessor;
import org.eclipse.viatra.dse.statecode.IStateCoder;
import org.eclipse.viatra.dse.visualizer.IExploreEventHandler;
import org.eclipse.viatra.query.runtime.api.AdvancedViatraQueryEngine;
import org.eclipse.viatra.query.runtime.api.IPatternMatch;
import org.eclipse.viatra.transformation.evm.api.Activation;
import org.eclipse.viatra.transformation.evm.api.Context;
import org.eclipse.viatra.transformation.evm.api.resolver.ChangeableConflictSet;
import org.eclipse.viatra.transformation.runtime.emf.rules.batch.BatchTransformationRule;

public class DesignSpaceManager implements IBacktrackListener {

	private final IStateCoder stateCoder;
	private final EditingDomain domain;
	private Notifier model;

	private IDesignSpace designSpace;

	private final TrajectoryInfo trajectory;

	// the occurence vector callback
	private List<IExploreEventHandler> handlers;

	// Dummy context for evm
	private final Context evmContext = Context.create();

	private Logger logger = Logger.getLogger(this.getClass());

	private boolean isNewState = false;
	private Map<BatchTransformationRule<?, ?>, ActivationFitnessProcessor> activationFitnessProcessors;
	private Map<BatchTransformationRule<?, ?>, String> activationFitnessProcessorNames;
	private ThreadContext context;

	private ActivationCodesConflictSet activationCodes;
	private ChangeableConflictSet conflictSet;

	private AdvancedViatraQueryEngine engine;

	private Random random = new Random();

	private long forwardTime = 0;
	private long backtrackingTime = 0;

	public DesignSpaceManager(ThreadContext context) {

		this.context = context;
		model = context.getModel();
		designSpace = context.getGlobalContext().getDesignSpace();
		domain = context.getEditingDomain();

		conflictSet = context.getConflictResolver().getLastCreatedConflictSet();

		stateCoder = context.getStateCoder();
		Object initialStateId = stateCoder.createStateCode();
		designSpace.addState(null, null, initialStateId);

		activationCodes = context.getActivationCodesConflictSet();

		engine = (AdvancedViatraQueryEngine) context.getQueryEngine();

		this.trajectory = new TrajectoryInfo(initialStateId);

		logger.debug("DesignSpaceManager initialized with initial model: " + initialStateId);
	}

	public void fireActivation(final Object transition) {
		if (fireActivationSilent(transition)) {
			return;
		}

		StringBuilder sb = new StringBuilder();
		sb.append(
				"A retrieved Transition SHOULD have a matching Activation. Possible causes: the state serializer is faulty; the algorithm choosed a wrong Transition.");
		sb.append("\nSought transition: ");
		sb.append(transition);
		Object currentStateId = getCurrentState();
		sb.append("\nCurrent known state: ");
		sb.append(currentStateId);
		Object actualStateId = stateCoder.createStateCode();
		sb.append("\nActual state: ");
		sb.append((actualStateId.equals(currentStateId) ? "same as current" : actualStateId));
		sb.append("\n");
		sb.append(trajectory);
		sb.append("\nAvailable transitions:");
		for (Activation<?> act : conflictSet.getNextActivations()) {
			IPatternMatch match = (IPatternMatch) act.getAtom();
			Object code = generateMatchCode(match);
			sb.append("\n\t");
			sb.append(code);
		}

		throw new DSEException(sb.toString());
	}

	public boolean tryFireActivation(final Object transition) {
		return fireActivationSilent(transition);
	}

	private boolean fireActivationSilent(final Object transition) {
		final Activation<?> activation = getActivationById(transition);

		if (activation == null) {
			return false;
		}

		BatchTransformationRule<?, ?> rule = getRuleByActivation(activation);

		Map<String, Double> measureCosts = new HashMap<String, Double>();
		if (activationFitnessProcessors != null && activationFitnessProcessors.containsKey(rule)) {
			IPatternMatch match = (IPatternMatch) activation.getAtom();
			ActivationFitnessProcessor processor = activationFitnessProcessors.get(rule);
			double fitness = processor.process(match);
			measureCosts.put(activationFitnessProcessorNames.get(rule), fitness);
		}

		ChangeCommand rc = new ChangeCommand(model) {
			@Override
			protected void doExecute() {
				activation.fire(evmContext);
			}
		};

		Object previousState = trajectory.getCurrentStateId();

		long start = System.nanoTime();
		try {
			engine.delayUpdatePropagation(() -> {
				domain.getCommandStack().execute(rc);
				return null;
			});
		} catch (InvocationTargetException e) {
			throw new RuntimeException(e);
		}
		forwardTime += System.nanoTime() - start;

		Object newStateId = stateCoder.createStateCode();
		updateActivationCodes();

		if (designSpace != null) {
			isNewState = !designSpace.isTraversed(newStateId);
			designSpace.addState(previousState, transition, newStateId);
		}

		trajectory.addStep(transition, rule, newStateId, measureCosts);

		if (handlers != null) {
			for (IExploreEventHandler iExploreEventHandler : handlers) {
				iExploreEventHandler.transitionFired(transition);
			}
		}

		logger.debug("Executed activation: " + transition);/*
															 * + " from state: " + previousState + " to " + newStateId);
															 */

		return true;
	}

	public boolean executeRandomActivationId() {
		Collection<Object> transitions = getTransitionsFromCurrentState();
		int size = transitions.size();
		if (size == 0) {
			return false;
		}

		int index = random.nextInt(size);
		Iterator<Object> iterator = transitions.iterator();
		for (int i = 0; i < index; ++i) {
			iterator.next();
		}
		Object transition = iterator.next();

		fireActivation(transition);
		return true;
	}

	public int executeTrajectory(Object[] trajectoryToExecute) {
		return executeTrajectory(trajectoryToExecute, 0, trajectoryToExecute.length, false, true);
	}

	public int executeTrajectory(Object[] trajectoryToExecute, int fromIncludedIndex, int toExcludedIndex) {
		return executeTrajectory(trajectoryToExecute, fromIncludedIndex, toExcludedIndex, false, true);
	}

	public int executeTrajectoryByTrying(Object[] trajectoryToExecute) {
		return executeTrajectory(trajectoryToExecute, 0, trajectoryToExecute.length, true, true);
	}

	public int executeTrajectoryByTrying(Object[] trajectoryToExecute, int fromIncludedIndex, int toExcludedIndex) {
		return executeTrajectory(trajectoryToExecute, fromIncludedIndex, toExcludedIndex, true, true);
	}

	public int executeTrajectoryWithoutStateCoding(Object[] trajectoryToExecute) {
		return executeTrajectory(trajectoryToExecute, 0, trajectoryToExecute.length, false, false);
	}

	public int executeTrajectoryWithoutStateCoding(Object[] trajectoryToExecute, int fromIncludedIndex,
			int toExcludedIndex) {
		return executeTrajectory(trajectoryToExecute, fromIncludedIndex, toExcludedIndex, false, false);
	}

	public int executeTrajectoryByTryingWithoutStateCoding(Object[] trajectoryToExecute) {
		return executeTrajectory(trajectoryToExecute, 0, trajectoryToExecute.length, true, false);
	}

	public int executeTrajectoryByTryingWithoutStateCoding(Object[] trajectoryToExecute, int fromIncludedIndex,
			int toExcludedIndex) {
		return executeTrajectory(trajectoryToExecute, fromIncludedIndex, toExcludedIndex, true, false);
	}

	private int executeTrajectory(Object[] trajectoryToExecute, int fromIncludedIndex, int toExcludedIndex,
			boolean tryAllActivations, boolean createStateCode) {
		logger.debug("Executing trajectory.");
		int unsuccesfulIndex = -1;
		if (tryAllActivations) {
			unsuccesfulIndex = 0;
		}
		for (int i = fromIncludedIndex; i < toExcludedIndex; i++) {
			Object activationId = trajectoryToExecute[i];
			final Activation<?> activation = getActivationById(activationId);

			if (activation == null) {
				logger.debug("Couldn't execute activation: " + activationId);
				if (tryAllActivations) {
					unsuccesfulIndex++;
					continue;
				} else {
					unsuccesfulIndex = i;
					logger.debug("Trajectory execution stopped at index " + i + "/" + trajectoryToExecute.length);
					break;
				}
			}

			BatchTransformationRule<?, ?> rule = getRuleByActivation(activation);

			Map<String, Double> measureCosts = new HashMap<String, Double>();
			if (activationFitnessProcessors != null && activationFitnessProcessors.containsKey(rule)) {
				IPatternMatch match = (IPatternMatch) activation.getAtom();
				ActivationFitnessProcessor processor = activationFitnessProcessors.get(rule);
				double fitness = processor.process(match);
				measureCosts.put(activationFitnessProcessorNames.get(rule), fitness);
			}

			ChangeCommand rc = new ChangeCommand(model) {
				@Override
				protected void doExecute() {
					activation.fire(evmContext);
				}
			};
			
			long start = System.nanoTime();
			try {
				engine.delayUpdatePropagation(() -> {
					domain.getCommandStack().execute(rc);
					return null;
				});
			} catch (InvocationTargetException e) {
				throw new RuntimeException(e);
			}
			forwardTime += System.nanoTime() - start;

			Object newStateId = null;
			if (createStateCode) {
				newStateId = stateCoder.createStateCode();
			}
			updateActivationCodes();

			trajectory.addStep(activationId, rule, newStateId, measureCosts);

			logger.debug("Activation executed: " + activationId);
		}
		if (!createStateCode) {
			trajectory.modifyLastStateCode(stateCoder.createStateCode());
		}
		logger.debug("Trajectory execution finished.");
		return unsuccesfulIndex;

	}

	public Object getTransitionByActivation(Activation<?> activation) {
		return activationCodes.getActivationId(activation);
	}

	public Activation<?> getActivationById(Object activationId) {
		return activationCodes.getActivation(activationId);
	}

	public BatchTransformationRule<?, ?> getRuleByActivation(Activation<?> activation) {
		return context.getGlobalContext().getSpecificationRuleMap().get(activation.getInstance().getSpecification());
	}

	public BatchTransformationRule<?, ?> getRuleByActivationId(Object activationId) {
		return getRuleByActivation(getActivationById(activationId));
	}

	/**
	 * Returns true if the given state is not owned by this crawler.
	 * 
	 **/
	public boolean isNewModelStateAlreadyTraversed() {
		return !isNewState;
	}

	public List<Object> getTrajectoryFromRoot() {
		return trajectory.getTrajectory();
	}

	public Collection<Object> getTransitionsFromCurrentState() {
		return activationCodes.getCurrentActivationCodes();
	}

	public Collection<Object> getUntraversedTransitionsFromCurrentState() {
		if (designSpace == null) {
			throw new DSEException("Unsupported without a design space");
		}
		Object currentState = trajectory.getCurrentStateId();
		Collection<Object> traversedIds = designSpace.getActivationIds(currentState);

		List<Object> untraversedTransitions = new ArrayList<>();
		for (Object activationId : activationCodes.getCurrentActivationCodes()) {
			if (!traversedIds.contains(activationId)) {
				untraversedTransitions.add(activationId);
			}
		}

		return untraversedTransitions;
	}

	public boolean undoLastTransformation() {

		if (!trajectory.canStepBack()) {
			return false;
		}

		long start = System.nanoTime();
		try {
			engine.delayUpdatePropagation(() -> {
				domain.getCommandStack().undo();
				return null;
			});
		} catch (InvocationTargetException e) {
			throw new RuntimeException(e);
		}
		updateActivationCodes();

		Object lastActivationId = trajectory.getLastActivationId();

		trajectory.backtrack();

		if (handlers != null) {
			for (IExploreEventHandler iExploreEventHandler : handlers) {
				iExploreEventHandler.undo(lastActivationId);
			}
		}

		logger.debug("Backtrack.");
		backtrackingTime += System.nanoTime() - start;

		return true;
	}

	public void backtrackXTimes(int steps) {
		long start = System.nanoTime();
		try {
			engine.delayUpdatePropagation(() -> {
				for (int i = steps; i > 0; i--) {
					domain.getCommandStack().undo();
					trajectory.backtrack();
				}
				return null;
			});
		} catch (InvocationTargetException e) {
			throw new RuntimeException(e);
		}
		backtrackingTime += System.nanoTime() - start;
		updateActivationCodes();
		logger.debug("Backtracked " + steps + " times.");
	}

	public int backtrackUntilLastCommonActivation(Object[] newTrajectory) {
		long start = System.nanoTime();
		Iterator<Object> currentTrajectoryIterator = trajectory.getTrajectory().iterator();
		if (!currentTrajectoryIterator.hasNext()) {
			return 0;
		}
		int indexOfLastCommonActivation = 0;
		for (Object activationCode : newTrajectory) {
			if (currentTrajectoryIterator.hasNext()) {
				Object activationCodeFromCurrent = currentTrajectoryIterator.next();
				if (activationCodeFromCurrent.equals(activationCode)) {
					indexOfLastCommonActivation++;
				} else {
					break;
				}
			} else {
				// current trajectory is smaller
				break;
			}
		}
		int numberOfBacktracks = trajectory.getDepth() - indexOfLastCommonActivation;
		if (numberOfBacktracks > 0) {
			try {
				engine.delayUpdatePropagation(() -> {
					for (int i = numberOfBacktracks; i > 0; i--) {
						domain.getCommandStack().undo();
						trajectory.backtrack();
					}
					return null;
				});
			} catch (InvocationTargetException e) {
				throw new RuntimeException(e);
			}
		}
		backtrackingTime += System.nanoTime() - start;
		updateActivationCodes();
		logger.debug("Backtracked " + numberOfBacktracks + " times.");
		return indexOfLastCommonActivation;
	}

	public void executeTrajectoryWithMinimalBacktrack(Object[] trajectory) {
		executeTrajectoryWithMinimalBacktrack(trajectory, trajectory.length);
	}

	public void executeTrajectoryWithMinimalBacktrack(Object[] trajectory, int toExcludedIndex) {
		int fromIndex = backtrackUntilLastCommonActivation(trajectory);
		executeTrajectory(trajectory, fromIndex, toExcludedIndex, false, true);
	}

	public void executeTrajectoryWithMinimalBacktrackWithoutStateCoding(Object[] trajectory) {
		executeTrajectoryWithMinimalBacktrackWithoutStateCoding(trajectory, trajectory.length);
	}

	public void executeTrajectoryWithMinimalBacktrackWithoutStateCoding(Object[] trajectory, int toExcludedIndex) {
		int fromIndex = backtrackUntilLastCommonActivation(trajectory);
		executeTrajectory(trajectory, fromIndex, toExcludedIndex, false, false);
		Object stateCode = stateCoder.createStateCode();
		this.trajectory.modifyLastStateCode(stateCode);
	}

	public void undoUntilRoot() {
		long start = System.nanoTime();
		try {
			engine.delayUpdatePropagation(() -> {
				while (trajectory.canStepBack()) {
					domain.getCommandStack().undo();
					trajectory.backtrack();
				}
				return null;
			});
		} catch (InvocationTargetException e) {
			throw new RuntimeException(e);
		}
		backtrackingTime += System.nanoTime() - start;
		updateActivationCodes();
		logger.debug("Backtracked to root.");
	}

	private Object generateMatchCode(IPatternMatch match) {
		return stateCoder.createActivationCode(match);
	}

	public Object getCurrentState() {
		return trajectory.getCurrentStateId();
	}

	public SolutionTrajectory createSolutionTrajectroy() {
		return trajectory.createSolutionTrajectory(context.getGlobalContext().getStateCoderFactory(), this);
	}

	public TrajectoryInfo getTrajectoryInfo() {
		return trajectory;
	}

	public void setDesignSpace(IDesignSpace designSpace) {
		this.designSpace = designSpace;
	}

	public IDesignSpace getDesignSpace() {
		return designSpace;
	}

	public void registerExploreEventHandler(IExploreEventHandler handler) {
		if (handler == null) {
			return;
		}
		if (handlers == null) {
			handlers = new ArrayList<IExploreEventHandler>();
		}
		handlers.add(handler);
	}

	public void deregisterExploreEventHandler(IExploreEventHandler handler) {
		if (handler == null) {
			return;
		}
		if (handlers != null) {
			handlers.remove(handler);
		}
	}

	public void registerActivationCostProcessor(String name, BatchTransformationRule<?, ?> rule,
			ActivationFitnessProcessor activationFitnessProcessor) {
		if (activationFitnessProcessors == null || activationFitnessProcessorNames == null) {
			activationFitnessProcessors = new HashMap<BatchTransformationRule<?, ?>, ActivationFitnessProcessor>();
			activationFitnessProcessorNames = new HashMap<BatchTransformationRule<?, ?>, String>();
		}
		activationFitnessProcessors.put(rule, activationFitnessProcessor);
		activationFitnessProcessorNames.put(rule, name);
	}

	public boolean isCurentStateInTrajectory() {
		Object currentStateId = trajectory.getCurrentStateId();
		List<Object> stateTrajectory = trajectory.getStateTrajectory();
		int size = stateTrajectory.size();
		for (int i = 0; i < size - 1; i++) {
			Object stateId = stateTrajectory.get(i);
			if (currentStateId.equals(stateId)) {
				return true;
			}
		}
		return false;
	}

	public IStateCoder getStateCoder() {
		return stateCoder;
	}

	private void updateActivationCodes() {
		activationCodes.updateActivationCodes();
	}
	
	public long getForwardTime() {
		return forwardTime;
	}

	public long getBacktrackingTime() {
		return backtrackingTime;
	}
	
	@Override
	public void forwardWorked(long nanos) {
		forwardTime += nanos;
	}

	@Override
	public void backtrackWorked(long nanos) {
		backtrackingTime += nanos;
	}
}