aboutsummaryrefslogtreecommitdiffstats
path: root/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/dse/SolutionCopier.xtend
blob: 7c30bbb59dd3b9af8df54fab104835a6826ee352 (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
package hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.dse

import com.google.common.collect.ImmutableList
import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialInterpretation
import java.util.LinkedHashMap
import java.util.List
import java.util.Map
import org.eclipse.emf.ecore.EObject
import org.eclipse.emf.ecore.util.EcoreUtil
import org.eclipse.viatra.dse.base.ThreadContext
import org.eclipse.xtend.lib.annotations.Accessors
import org.eclipse.xtend.lib.annotations.FinalFieldsConstructor

@FinalFieldsConstructor
class CopiedSolution {
	@Accessors val PartialInterpretation partialInterpretations
	@Accessors val Map<EObject, EObject> trace
	@Accessors val long copierRuntime
	@Accessors var boolean current = true
}

/**
 * Based on {@link SolutionStore.BestSolutionSaver}.
 * 
 * Will also automatically fill any missing numerical values in the saved solutions
 * using the supplied {@link NumericSolver}.
 */
class SolutionCopier {
	val copiedSolutions = new LinkedHashMap<Object, CopiedSolution>

	@Accessors NumericRefinementUnit numericSolver
	long startTime = System.nanoTime
	@Accessors(PUBLIC_GETTER) long totalCopierRuntime = 0

	def void copySolution(ThreadContext context, Object solutionId) {
		val existingCopy = copiedSolutions.get(solutionId)
		if (existingCopy === null) {
			val copyStart = System.nanoTime
			val solution = context.model as PartialInterpretation
			val copier = new EcoreUtil.Copier
			val copiedPartialInterpretation = copier.copy(solution) as PartialInterpretation
			copier.copyReferences
			totalCopierRuntime += System.nanoTime - copyStart
			val copierRuntime = System.nanoTime - startTime
			val copiedSolution = new CopiedSolution(copiedPartialInterpretation, copier, copierRuntime)
			numericSolver?.fillSolutionCopy(copiedSolution.trace)
			copiedSolutions.put(solutionId, copiedSolution)
		} else {
			existingCopy.current = true
		}
	}

	def void markAsObsolete(Object solutionId) {
		val copiedSolution = copiedSolutions.get(solutionId)
		if (copiedSolution === null) {
			throw new IllegalStateException("No solution to mark as obsolete for state code: " + solutionId)
		}
		copiedSolution.current = false
	}

	def List<PartialInterpretation> getPartialInterpretations(boolean currentOnly) {
		getListOfCopiedSolutions(currentOnly).map[partialInterpretations]
	}

	def List<Map<EObject, EObject>> getTraces(boolean currentOnly) {
		getListOfCopiedSolutions(currentOnly).map[trace]
	}

	def List<Long> getAllCopierRuntimes(boolean currentOnly) {
		getListOfCopiedSolutions(currentOnly).map[copierRuntime]
	}

	def List<CopiedSolution> getListOfCopiedSolutions(boolean currentOnly) {
		val values = copiedSolutions.values
		val filteredSolutions = if (currentOnly) {
				values.filter[current]
			} else {
				values
			}
		ImmutableList.copyOf(filteredSolutions)
	}
}