aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/language-web/src/main/java/tools/refinery/language/web/generator/ModelGenerationWorker.java
blob: a3b6ca8228c72fc4a3bc4b0d5111bcaf575fd5ea (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
 * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/>
 *
 * SPDX-License-Identifier: EPL-2.0
 */
package tools.refinery.language.web.generator;

import com.google.inject.Inject;
import org.eclipse.xtext.service.OperationCanceledManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tools.refinery.generator.ModelGenerator;
import tools.refinery.generator.ModelGeneratorFactory;
import tools.refinery.language.web.semantics.metadata.MetadataCreator;
import tools.refinery.generator.ProblemLoader;
import tools.refinery.generator.ValidationErrorsException;
import tools.refinery.language.web.semantics.PartialInterpretation2Json;
import tools.refinery.language.web.xtext.server.ThreadPoolExecutorServiceProvider;
import tools.refinery.language.web.xtext.server.push.PushWebDocument;
import tools.refinery.store.util.CancellationToken;

import java.io.IOException;
import java.util.UUID;
import java.util.concurrent.*;

public class ModelGenerationWorker implements Runnable {
	private static final Logger LOG = LoggerFactory.getLogger(ModelGenerationWorker.class);

	private final UUID uuid = UUID.randomUUID();

	private PushWebDocument state;

	private String text;

	private volatile boolean timedOut;

	private volatile boolean cancelled;

	@Inject
	private OperationCanceledManager operationCanceledManager;

	@Inject
	private ProblemLoader problemLoader;

	@Inject
	private ModelGeneratorFactory generatorFactory;

	@Inject
	private MetadataCreator metadataCreator;

	@Inject
	private PartialInterpretation2Json partialInterpretation2Json;

	private final Object lockObject = new Object();

	private ExecutorService executorService;

	private ScheduledExecutorService scheduledExecutorService;

	private int randomSeed;

	private long timeoutSec;

	private Future<?> future;

	private ScheduledFuture<?> timeoutFuture;

	private final CancellationToken cancellationToken = () -> {
		if (cancelled || Thread.interrupted()) {
			operationCanceledManager.throwOperationCanceledException();
		}
	};

	@Inject
	public void setExecutorServiceProvider(ThreadPoolExecutorServiceProvider provider) {
		executorService = provider.get(ModelGenerationService.MODEL_GENERATION_EXECUTOR);
		scheduledExecutorService = provider.getScheduled(ModelGenerationService.MODEL_GENERATION_TIMEOUT_EXECUTOR);
	}

	public void setState(PushWebDocument state, int randomSeed, long timeoutSec) {
		this.state = state;
		this.randomSeed = randomSeed;
		this.timeoutSec = timeoutSec;
		text = state.getText();
	}

	public UUID getUuid() {
		return uuid;
	}

	public void start() {
		synchronized (lockObject) {
			LOG.debug("Enqueueing model generation: {}", uuid);
			future = executorService.submit(this);
		}
	}

	public void startTimeout() {
		synchronized (lockObject) {
			LOG.debug("Starting model generation: {}", uuid);
			cancellationToken.checkCancelled();
			timeoutFuture = scheduledExecutorService.schedule(() -> cancel(true), timeoutSec, TimeUnit.SECONDS);
		}
	}

	// We catch {@code Throwable} to handle {@code OperationCancelledError}, but we rethrow fatal JVM errors.
	@SuppressWarnings("squid:S1181")
	@Override
	public void run() {
		startTimeout();
		notifyResult(new ModelGenerationStatusResult(uuid, "Initializing model generator"));
		ModelGenerationResult result;
		try {
			result = doRun();
		} catch (Throwable e) {
			if (operationCanceledManager.isOperationCanceledException(e)) {
				var message = timedOut ? "Model generation timed out" : "Model generation cancelled";
				LOG.debug("{}: {}", message, uuid);
				notifyResult(new ModelGenerationErrorResult(uuid, message));
			} else if (e instanceof Error error) {
				// Make sure we don't try to recover from any fatal JVM errors.
				throw error;
			} else {
				LOG.debug("Model generation error", e);
				notifyResult(new ModelGenerationErrorResult(uuid, e.toString()));
			}
			return;
		}
		notifyResult(result);
	}

	private void notifyResult(ModelGenerationResult result) {
		state.notifyPrecomputationListeners(ModelGenerationService.SERVICE_NAME, result);
	}

	public ModelGenerationResult doRun() throws IOException {
		cancellationToken.checkCancelled();
		var problem = problemLoader.cancellationToken(cancellationToken).loadString(text);
		ModelGenerator generator;
		try {
			generator = generatorFactory.cancellationToken(cancellationToken).createGenerator(problem);
		} catch (ValidationErrorsException e) {
			var errors = e.getErrors();
			if (errors != null && !errors.isEmpty()) {
				return new ModelGenerationErrorResult(uuid, "Validation error: " + errors.get(0).getMessage());
			}
			throw e;
		}
		notifyResult(new ModelGenerationStatusResult(uuid, "Generating model"));
		generator.setRandomSeed(randomSeed);
		if (!generator.tryGenerate()) {
			return new ModelGenerationErrorResult(uuid, "Problem is unsatisfiable");
		}
		notifyResult(new ModelGenerationStatusResult(uuid, "Saving generated model"));
		cancellationToken.checkCancelled();
		metadataCreator.setProblemTrace(generator.getProblemTrace());
		var nodesMetadata = metadataCreator.getNodesMetadata(generator.getModel(), false);
		cancellationToken.checkCancelled();
		var relationsMetadata = metadataCreator.getRelationsMetadata();
		cancellationToken.checkCancelled();
		var partialInterpretation = partialInterpretation2Json.getPartialInterpretation(generator, cancellationToken);
		return new ModelGenerationSuccessResult(uuid, nodesMetadata, relationsMetadata, partialInterpretation);
	}

	public void cancel() {
		cancel(false);
	}

	public void cancel(boolean timedOut) {
		synchronized (lockObject) {
			LOG.trace("Cancelling model generation: {}", uuid);
			this.timedOut = timedOut;
			cancelled = true;
			if (future != null) {
				future.cancel(true);
				future = null;
			}
			if (timeoutFuture != null) {
				timeoutFuture.cancel(true);
				timeoutFuture = null;
			}
		}
	}
}