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

import com.google.inject.Provider;
import tools.refinery.language.model.problem.Problem;
import tools.refinery.language.semantics.ProblemTrace;
import tools.refinery.language.semantics.SolutionSerializer;
import tools.refinery.logic.AbstractValue;
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 final Provider<SolutionSerializer> solutionSerializerProvider;
	private long randomSeed = 1;
	private boolean lastGenerationSuccessful;

	ModelGenerator(ProblemTrace problemTrace, ModelStore store, ModelSeed modelSeed,
				   Provider<SolutionSerializer> solutionSerializerProvider) {
		super(problemTrace, store, modelSeed, Concreteness.CANDIDATE);
		this.solutionSerializerProvider = solutionSerializerProvider;
		initialVersion = getModel().commit();
	}

	public long getRandomSeed() {
		return randomSeed;
	}

	public void setRandomSeed(long 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.getFirst().version());
		lastGenerationSuccessful = true;
		return true;
	}

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

	@Override
	public <A extends AbstractValue<A, C>, C> PartialInterpretation<A, C> getPartialInterpretation(
			PartialSymbol<A, C> partialSymbol) {
		checkSuccessfulGeneration();
		return super.getPartialInterpretation(partialSymbol);
	}

	public Problem serializeSolution() {
		checkSuccessfulGeneration();
		var serializer = solutionSerializerProvider.get();
		return serializer.serializeSolution(getProblemTrace(), getModel());
	}

	private void checkSuccessfulGeneration() {
		if (!lastGenerationSuccessful) {
			throw new IllegalStateException("No generated model is available");
		}
	}
}