diff options
Diffstat (limited to 'Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/dse/SolutionCopier.xtend')
-rw-r--r-- | Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/dse/SolutionCopier.xtend | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/dse/SolutionCopier.xtend b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/dse/SolutionCopier.xtend new file mode 100644 index 00000000..d036257d --- /dev/null +++ b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/dse/SolutionCopier.xtend | |||
@@ -0,0 +1,74 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.dse | ||
2 | |||
3 | import com.google.common.collect.ImmutableList | ||
4 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialInterpretation | ||
5 | import java.util.LinkedHashMap | ||
6 | import java.util.List | ||
7 | import java.util.Map | ||
8 | import org.eclipse.emf.ecore.EObject | ||
9 | import org.eclipse.emf.ecore.util.EcoreUtil | ||
10 | import org.eclipse.viatra.dse.base.ThreadContext | ||
11 | import org.eclipse.xtend.lib.annotations.Accessors | ||
12 | import org.eclipse.xtend.lib.annotations.FinalFieldsConstructor | ||
13 | |||
14 | @FinalFieldsConstructor | ||
15 | class CopiedSolution { | ||
16 | @Accessors val PartialInterpretation partialInterpretations | ||
17 | @Accessors val Map<EObject, EObject> trace | ||
18 | @Accessors val long copierRuntime | ||
19 | @Accessors var boolean current = true | ||
20 | } | ||
21 | |||
22 | class SolutionCopier { | ||
23 | val copiedSolutions = new LinkedHashMap<Object, CopiedSolution> | ||
24 | |||
25 | long startTime = System.nanoTime | ||
26 | @Accessors(PUBLIC_GETTER) long totalCopierRuntime = 0 | ||
27 | |||
28 | def void copySolution(ThreadContext context, Object solutionId) { | ||
29 | val existingCopy = copiedSolutions.get(solutionId) | ||
30 | if (existingCopy === null) { | ||
31 | val copyStart = System.nanoTime | ||
32 | val solution = context.model as PartialInterpretation | ||
33 | val copier = new EcoreUtil.Copier | ||
34 | val copiedPartialInterpretation = copier.copy(solution) as PartialInterpretation | ||
35 | copier.copyReferences | ||
36 | totalCopierRuntime += System.nanoTime - copyStart | ||
37 | val copierRuntime = System.nanoTime - startTime | ||
38 | val copiedSolution = new CopiedSolution(copiedPartialInterpretation, copier, copierRuntime) | ||
39 | copiedSolutions.put(solutionId, copiedSolution) | ||
40 | } else { | ||
41 | existingCopy.current = true | ||
42 | } | ||
43 | } | ||
44 | |||
45 | def void markAsObsolete(Object solutionId) { | ||
46 | val copiedSolution = copiedSolutions.get(solutionId) | ||
47 | if (copiedSolution === null) { | ||
48 | throw new IllegalStateException("No solution to mark as obsolete for state code: " + solutionId) | ||
49 | } | ||
50 | copiedSolution.current = false | ||
51 | } | ||
52 | |||
53 | def List<PartialInterpretation> getPartialInterpretations(boolean currentOnly) { | ||
54 | getListOfCopiedSolutions(currentOnly).map[partialInterpretations] | ||
55 | } | ||
56 | |||
57 | def List<Map<EObject, EObject>> getTraces(boolean currentOnly) { | ||
58 | getListOfCopiedSolutions(currentOnly).map[trace] | ||
59 | } | ||
60 | |||
61 | def List<Long> getAllCopierRuntimes(boolean currentOnly) { | ||
62 | getListOfCopiedSolutions(currentOnly).map[copierRuntime] | ||
63 | } | ||
64 | |||
65 | def List<CopiedSolution> getListOfCopiedSolutions(boolean currentOnly) { | ||
66 | val values = copiedSolutions.values | ||
67 | val filteredSolutions = if (currentOnly) { | ||
68 | values.filter[current] | ||
69 | } else { | ||
70 | values | ||
71 | } | ||
72 | ImmutableList.copyOf(filteredSolutions) | ||
73 | } | ||
74 | } | ||