Skip to content

Commit

Permalink
Fixed issue with caching and multiple threads.
Browse files Browse the repository at this point in the history
  • Loading branch information
mwever committed Feb 22, 2019
1 parent c665f04 commit 01eebae
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions softwareconfiguration/hasco/src/main/java/hasco/core/HASCO.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeoutException;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -74,7 +75,7 @@ public class HASCO<ISearch extends GraphSearchInput<N, A>, N, A, V extends Compa
private final List<HASCOSolutionCandidate<V>> listOfAllRecognizedSolutions = new ArrayList<>();
private int numUnparametrizedSolutions = -1;
private final Set<UnparametrizedComponentInstance> returnedUnparametrizedComponentInstances = new HashSet<>();
private Map<EvaluatedSearchSolutionCandidateFoundEvent<N, A, V>, HASCOSolutionEvent<V>> hascoSolutionEventCache = new HashMap<>();
private Map<EvaluatedSearchSolutionCandidateFoundEvent<N, A, V>, HASCOSolutionEvent<V>> hascoSolutionEventCache = new ConcurrentHashMap<>();

/* runtime variables of algorithm */
private final TimeRecordingEvaluationWrapper<V> timeGrabbingEvaluationWrapper;
Expand Down Expand Up @@ -168,6 +169,8 @@ public void receiveSearchEvent(final AlgorithmEvent event) {

@Subscribe
public void receiveSolutionCandidateFoundEvent(final EvaluatedSearchSolutionCandidateFoundEvent<N, A, V> solutionEvent) throws InterruptedException, TimeoutException, AlgorithmException {

System.err.println(Thread.currentThread().getName() + ": Received solution event in search listener " + solutionEvent);
EvaluatedSearchGraphPath<N, A, V> searchPath = solutionEvent.getSolutionCandidate();
Plan plan = HASCO.this.planningGraphGeneratorDeriver.getPlan(searchPath.getNodes());
ComponentInstance objectInstance = Util.getSolutionCompositionForPlan(HASCO.this.getInput().getComponents(), HASCO.this.planningProblem.getCorePlanningProblem().getInit(), plan, true);
Expand All @@ -190,6 +193,8 @@ public void receiveSolutionCandidateFoundEvent(final EvaluatedSearchSolutionCand
HASCO.this.updateBestSeenSolution(solution);
HASCO.this.listOfAllRecognizedSolutions.add(solution);
HASCOSolutionEvent<V> hascoSolutionEvent = new HASCOSolutionEvent<>(HASCO.this.getId(), solution);
HASCO.this.hascoSolutionEventCache.put(solutionEvent, hascoSolutionEvent);
System.err.println(Thread.currentThread().getName() + ": Post solution event in search listener " + solutionEvent);
HASCO.this.post(hascoSolutionEvent);
}

Expand Down Expand Up @@ -221,8 +226,12 @@ public void receiveSolutionCandidateFoundEvent(final EvaluatedSearchSolutionCand

/* otherwise, if a solution has been found, we announce this finding to our listeners and memorize if it is a new best candidate */
else if (searchEvent instanceof EvaluatedSearchSolutionCandidateFoundEvent) {
System.err.println(Thread.currentThread().getName() + ": Solution event in HASCO.nextWithException " + searchEvent);
HASCOSolutionEvent<V> hascoSolutionEvent = this.hascoSolutionEventCache.remove(searchEvent);
assert (hascoSolutionEvent != null) : "Hasco solution event has not been seen yet or cannot be retrieved from cache.";
assert (hascoSolutionEvent != null) : "Hasco solution event has not been seen yet or cannot be retrieved from cache. " + this.hascoSolutionEventCache;
if (hascoSolutionEvent == null) {
System.exit(0);
}
this.logger.info("Received new solution with score {} from search, communicating this solution to the HASCO listeners. Number of returned unparametrized solutions is now {}/{}.", hascoSolutionEvent.getScore(),
this.returnedUnparametrizedComponentInstances.size(), this.numUnparametrizedSolutions);
return hascoSolutionEvent;
Expand Down

0 comments on commit 01eebae

Please sign in to comment.