diff options
Diffstat (limited to 'subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/statespace/internal/SolutionStoreImpl.java')
-rw-r--r-- | subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/statespace/internal/SolutionStoreImpl.java | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/statespace/internal/SolutionStoreImpl.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/statespace/internal/SolutionStoreImpl.java new file mode 100644 index 00000000..43548eaa --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/statespace/internal/SolutionStoreImpl.java | |||
@@ -0,0 +1,52 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse.transition.statespace.internal; | ||
7 | |||
8 | import tools.refinery.store.dse.transition.VersionWithObjectiveValue; | ||
9 | import tools.refinery.store.dse.transition.statespace.SolutionStore; | ||
10 | |||
11 | import java.util.ArrayList; | ||
12 | import java.util.List; | ||
13 | import java.util.PriorityQueue; | ||
14 | |||
15 | |||
16 | public class SolutionStoreImpl implements SolutionStore { | ||
17 | final int maxNumberSolutions; | ||
18 | public static final int UNLIMITED = -1; | ||
19 | final PriorityQueue<VersionWithObjectiveValue> solutions; | ||
20 | |||
21 | public SolutionStoreImpl(int maxNumberSolutions) { | ||
22 | this.maxNumberSolutions = maxNumberSolutions; | ||
23 | solutions = new PriorityQueue<>(ObjectivePriorityQueueImpl.c1.reversed()); | ||
24 | } | ||
25 | |||
26 | |||
27 | @Override | ||
28 | public synchronized boolean submit(VersionWithObjectiveValue version) { | ||
29 | boolean removeLast = hasEnoughSolution(); | ||
30 | solutions.add(version); | ||
31 | if(removeLast) { | ||
32 | var last = solutions.poll(); | ||
33 | return last != version; | ||
34 | } else { | ||
35 | return true; | ||
36 | } | ||
37 | } | ||
38 | |||
39 | @Override | ||
40 | public List<VersionWithObjectiveValue> getSolutions() { | ||
41 | return new ArrayList<>(solutions); | ||
42 | } | ||
43 | |||
44 | @Override | ||
45 | public boolean hasEnoughSolution() { | ||
46 | if (maxNumberSolutions == UNLIMITED) { | ||
47 | return false; | ||
48 | } else { | ||
49 | return solutions.size() >= maxNumberSolutions; | ||
50 | } | ||
51 | } | ||
52 | } | ||