aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/statespace/internal/SolutionStoreImpl.java
blob: cc48864fbea921d5f94f1a553ac269de8a56087d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
 * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/>
 *
 * SPDX-License-Identifier: EPL-2.0
 */
package tools.refinery.store.dse.transition.statespace.internal;

import tools.refinery.store.dse.transition.VersionWithObjectiveValue;
import tools.refinery.store.dse.transition.statespace.SolutionStore;

import java.util.ArrayList;
import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;

public class SolutionStoreImpl implements SolutionStore {
	final int maxNumberSolutions;
	public static final int UNLIMITED = -1;
	final SortedSet<VersionWithObjectiveValue> solutions;

	public SolutionStoreImpl(int maxNumberSolutions) {
		this.maxNumberSolutions = maxNumberSolutions;
		solutions = new TreeSet<>(ObjectivePriorityQueueImpl.c1);
	}


	@Override
	public synchronized boolean submit(VersionWithObjectiveValue version) {
		boolean removeLast = hasEnoughSolution();
		solutions.add(version);
		if(removeLast) {
			var last = solutions.last();
			solutions.remove(last);
			return last != version;
		} else {
			return true;
		}
	}

	@Override
	public List<VersionWithObjectiveValue> getSolutions() {
		return new ArrayList<>(solutions);
	}

	@Override
	public boolean hasEnoughSolution() {
		if (maxNumberSolutions == UNLIMITED) {
			return false;
		} else {
			return solutions.size() >= maxNumberSolutions;
		}
	}
}