aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/generator/src/main/java/tools/refinery/generator/ModelGenerator.java
blob: 4c3e52f3d1613a846f10bfbb248a20788e33b9da (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
 * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/>
 *
 * SPDX-License-Identifier: EPL-2.0
 */
package tools.refinery.generator;

import org.eclipse.xtext.naming.QualifiedName;
import tools.refinery.language.model.problem.Relation;
import tools.refinery.store.dse.strategy.BestFirstStoreManager;
import tools.refinery.store.dse.transition.VersionWithObjectiveValue;
import tools.refinery.store.map.Version;
import tools.refinery.store.model.Model;
import tools.refinery.store.model.ModelStore;
import tools.refinery.store.reasoning.ReasoningAdapter;
import tools.refinery.store.reasoning.ReasoningStoreAdapter;
import tools.refinery.store.reasoning.interpretation.PartialInterpretation;
import tools.refinery.store.reasoning.literal.Concreteness;
import tools.refinery.store.reasoning.representation.PartialSymbol;
import tools.refinery.store.reasoning.seed.ModelSeed;
import tools.refinery.store.representation.TruthValue;

import java.util.Collection;

public class ModelGenerator {
	private final ProblemTrace problemTrace;
	private final ModelStore store;
	private final Model model;
	private final ReasoningAdapter reasoningAdapter;
	private final Version initialVersion;

	private int randomSeed = 1;

	public ModelGenerator(ProblemTrace problemTrace, ModelStore store, ModelSeed modelSeed) {
		this.problemTrace = problemTrace;
		this.store = store;
		model = store.getAdapter(ReasoningStoreAdapter.class).createInitialModel(modelSeed);
		reasoningAdapter = model.getAdapter(ReasoningAdapter.class);
		initialVersion = model.commit();
	}

	public ProblemTrace getProblemTrace() {
		return problemTrace;
	}

	public ModelStore getModelStore() {
		return store;
	}

	public Model getModel() {
		return model;
	}

	public int getRandomSeed() {
		return randomSeed;
	}

	public void setRandomSeed(int randomSeed) {
		this.randomSeed = randomSeed;
	}

	public Collection<Version> run(int maxNumberOfSolutions) {
		var bestFirst = new BestFirstStoreManager(store, maxNumberOfSolutions);
		int currentRandomSeed = randomSeed;
		// Increment random seed even if generation is unsuccessful.
		randomSeed++;
		bestFirst.startExploration(initialVersion, currentRandomSeed);
		return bestFirst.getSolutionStore()
				.getSolutions()
				.stream()
				.map(VersionWithObjectiveValue::version)
				.toList();
	}

	public boolean tryRun() {
		var iterator = run(1).iterator();
		if (!iterator.hasNext()) {
			return false;
		}
		model.restore(iterator.next());
		return true;
	}

	public <A, C> PartialInterpretation<A, C> getCandidateInterpretation(PartialSymbol<A, C> partialSymbol) {
		return reasoningAdapter.getPartialInterpretation(Concreteness.CANDIDATE, partialSymbol);
	}

	public PartialInterpretation<TruthValue, Boolean> getCandidateInterpretation(Relation relation) {
		return getCandidateInterpretation(problemTrace.getPartialRelation(relation));
	}

	public PartialInterpretation<TruthValue, Boolean> getCandidateInterpretation(QualifiedName qualifiedName) {
		return getCandidateInterpretation(problemTrace.getPartialRelation(qualifiedName));
	}

	public PartialInterpretation<TruthValue, Boolean> getCandidateInterpretation(String qualifiedName) {
		return getCandidateInterpretation(problemTrace.getPartialRelation(qualifiedName));
	}

	public static ModelGeneratorBuilder standaloneBuilder() {
		var injector = StandaloneInjectorHolder.getInjector();
		return injector.getInstance(ModelGeneratorBuilder.class);
	}
}