blob: 29f807144f17df17949cf0898238db48502be656 (
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
|
/*
* SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/>
*
* SPDX-License-Identifier: EPL-2.0
*/
package tools.refinery.generator;
import com.google.inject.Inject;
import com.google.inject.Provider;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.xtext.diagnostics.Severity;
import org.eclipse.xtext.resource.IResourceFactory;
import org.eclipse.xtext.resource.XtextResourceSet;
import org.eclipse.xtext.util.LazyStringInputStream;
import org.eclipse.xtext.validation.CheckMode;
import org.eclipse.xtext.validation.IResourceValidator;
import tools.refinery.language.model.problem.Problem;
import tools.refinery.store.util.CancellationToken;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
public class ProblemLoader {
@Inject
private Provider<XtextResourceSet> resourceSetProvider;
@Inject
private IResourceFactory resourceFactory;
@Inject
private IResourceValidator resourceValidator;
private CancellationToken cancellationToken = CancellationToken.NONE;
public ProblemLoader cancellationToken(CancellationToken cancellationToken) {
this.cancellationToken = cancellationToken;
return this;
}
public Problem loadString(String problemString) throws IOException {
try (var stream = new LazyStringInputStream(problemString)) {
return loadStream(stream);
}
}
public Problem loadStream(InputStream inputStream) throws IOException {
var resourceSet = resourceSetProvider.get();
var resource = resourceFactory.createResource(URI.createFileURI("__synthetic.problem"));
resourceSet.getResources().add(resource);
resource.load(inputStream, Map.of());
return loadResource(resource);
}
public Problem loadFile(File file) throws IOException {
return loadFile(file.getAbsolutePath());
}
public Problem loadFile(String filePath) throws IOException {
return loadUri(URI.createFileURI(filePath));
}
public Problem loadUri(URI uri) throws IOException {
var resourceSet = resourceSetProvider.get();
var resource = resourceFactory.createResource(uri);
resourceSet.getResources().add(resource);
resource.load(Map.of());
return loadResource(resource);
}
public Problem loadResource(Resource resource) {
var issues = resourceValidator.validate(resource, CheckMode.ALL, () -> {
cancellationToken.checkCancelled();
return Thread.interrupted();
});
cancellationToken.checkCancelled();
var errors = issues.stream()
.filter(issue -> issue.getSeverity() == Severity.ERROR)
.toList();
if (!errors.isEmpty()) {
throw new ValidationErrorsException(resource.getURI(), errors);
}
if (resource.getContents().isEmpty() || !(resource.getContents().get(0) instanceof Problem problem)) {
throw new IllegalArgumentException("Model generation problem not found in resource " + resource.getURI());
}
return problem;
}
}
|