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 | 82 |
1 files changed, 82 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..888eda18 --- /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,82 @@ | |||
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 | /** | ||
23 | * Based on {@link SolutionStore.BestSolutionSaver}. | ||
24 | * | ||
25 | * Will also automatically fill any missing numerical values in the saved solutions | ||
26 | * using the supplied {@link NumericSolver}. | ||
27 | */ | ||
28 | class SolutionCopier { | ||
29 | val copiedSolutions = new LinkedHashMap<Object, CopiedSolution> | ||
30 | |||
31 | @Accessors NumericSolver numericSolver | ||
32 | long startTime = System.nanoTime | ||
33 | @Accessors(PUBLIC_GETTER) long totalCopierRuntime = 0 | ||
34 | |||
35 | def void copySolution(ThreadContext context, Object solutionId) { | ||
36 | val existingCopy = copiedSolutions.get(solutionId) | ||
37 | if (existingCopy === null) { | ||
38 | val copyStart = System.nanoTime | ||
39 | val solution = context.model as PartialInterpretation | ||
40 | val copier = new EcoreUtil.Copier | ||
41 | val copiedPartialInterpretation = copier.copy(solution) as PartialInterpretation | ||
42 | copier.copyReferences | ||
43 | totalCopierRuntime += System.nanoTime - copyStart | ||
44 | val copierRuntime = System.nanoTime - startTime | ||
45 | val copiedSolution = new CopiedSolution(copiedPartialInterpretation, copier, copierRuntime) | ||
46 | //numericSolver?.fillSolutionCopy(copiedSolution.trace) | ||
47 | copiedSolutions.put(solutionId, copiedSolution) | ||
48 | } else { | ||
49 | existingCopy.current = true | ||
50 | } | ||
51 | } | ||
52 | |||
53 | def void markAsObsolete(Object solutionId) { | ||
54 | val copiedSolution = copiedSolutions.get(solutionId) | ||
55 | if (copiedSolution === null) { | ||
56 | throw new IllegalStateException("No solution to mark as obsolete for state code: " + solutionId) | ||
57 | } | ||
58 | copiedSolution.current = false | ||
59 | } | ||
60 | |||
61 | def List<PartialInterpretation> getPartialInterpretations(boolean currentOnly) { | ||
62 | getListOfCopiedSolutions(currentOnly).map[partialInterpretations] | ||
63 | } | ||
64 | |||
65 | def List<Map<EObject, EObject>> getTraces(boolean currentOnly) { | ||
66 | getListOfCopiedSolutions(currentOnly).map[trace] | ||
67 | } | ||
68 | |||
69 | def List<Long> getAllCopierRuntimes(boolean currentOnly) { | ||
70 | getListOfCopiedSolutions(currentOnly).map[copierRuntime] | ||
71 | } | ||
72 | |||
73 | def List<CopiedSolution> getListOfCopiedSolutions(boolean currentOnly) { | ||
74 | val values = copiedSolutions.values | ||
75 | val filteredSolutions = if (currentOnly) { | ||
76 | values.filter[current] | ||
77 | } else { | ||
78 | values | ||
79 | } | ||
80 | ImmutableList.copyOf(filteredSolutions) | ||
81 | } | ||
82 | } | ||