aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/generator/src/main/java/tools/refinery/generator/ModelGenerator.java
blob: 5b44c10aa03194e9e671db00695d7dcb1e9724f7 (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
/*
 * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/>
 *
 * SPDX-License-Identifier: EPL-2.0
 */
package tools.refinery.generator;

import tools.refinery.language.semantics.ProblemTrace;
import tools.refinery.store.dse.strategy.BestFirstStoreManager;
import tools.refinery.store.map.Version;
import tools.refinery.store.model.ModelStore;
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;

public class ModelGenerator extends ModelFacade {
	private final Version initialVersion;

	private int randomSeed = 0;

	private boolean lastGenerationSuccessful;

	public ModelGenerator(ProblemTrace problemTrace, ModelStore store, ModelSeed modelSeed) {
		super(problemTrace, store, modelSeed, Concreteness.CANDIDATE);
		initialVersion = getModel().commit();
	}

	public int getRandomSeed() {
		return randomSeed;
	}

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

	public boolean isLastGenerationSuccessful() {
		return lastGenerationSuccessful;
	}

	// This method only makes sense if it returns {@code true} on success.
	@SuppressWarnings("BooleanMethodIsAlwaysInverted")
	public boolean tryGenerate() {
		lastGenerationSuccessful = false;
		randomSeed++;
		var bestFirst = new BestFirstStoreManager(getModelStore(), 1);
		bestFirst.startExploration(initialVersion, randomSeed);
		var solutions = bestFirst.getSolutionStore().getSolutions();
		if (solutions.isEmpty()) {
			return false;
		}
		getModel().restore(solutions.get(0).version());
		lastGenerationSuccessful = true;
		return true;
	}

	public void generate() {
		if (!tryGenerate()) {
			throw new UnsatisfiableProblemException();
		}
	}

	@Override
	public <A, C> PartialInterpretation<A, C> getPartialInterpretation(PartialSymbol<A, C> partialSymbol) {
		if (!lastGenerationSuccessful) {
			throw new IllegalStateException("No generated model is available");
		}
		return super.getPartialInterpretation(partialSymbol);
	}
}