From 3f9b1c92cc35fa4ed9672a2b8601f4c22af24921 Mon Sep 17 00:00:00 2001 From: Kristóf Marussy Date: Sun, 7 Apr 2019 13:46:36 +0200 Subject: Infrastructure for objective functions --- .../viatrasolver/reasoner/dse/SolutionCopier.xtend | 74 ++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/dse/SolutionCopier.xtend (limited to 'Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/dse/SolutionCopier.xtend') 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 @@ +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 trace + @Accessors val long copierRuntime + @Accessors var boolean current = true +} + +class SolutionCopier { + val copiedSolutions = new LinkedHashMap + + 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) + 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 getPartialInterpretations(boolean currentOnly) { + getListOfCopiedSolutions(currentOnly).map[partialInterpretations] + } + + def List> getTraces(boolean currentOnly) { + getListOfCopiedSolutions(currentOnly).map[trace] + } + + def List getAllCopierRuntimes(boolean currentOnly) { + getListOfCopiedSolutions(currentOnly).map[copierRuntime] + } + + def List getListOfCopiedSolutions(boolean currentOnly) { + val values = copiedSolutions.values + val filteredSolutions = if (currentOnly) { + values.filter[current] + } else { + values + } + ImmutableList.copyOf(filteredSolutions) + } +} -- cgit v1.2.3-54-g00ecf