1 package org.simantics.modeling.scl.issue;
3 import java.util.ArrayList;
4 import java.util.HashSet;
6 import java.util.Objects;
8 import java.util.TreeSet;
9 import java.util.concurrent.ConcurrentHashMap;
11 import org.eclipse.jface.viewers.StructuredSelection;
12 import org.eclipse.swt.widgets.Display;
13 import org.eclipse.swt.widgets.Shell;
14 import org.simantics.Simantics;
15 import org.simantics.db.AsyncReadGraph;
16 import org.simantics.db.Disposable;
17 import org.simantics.db.ReadGraph;
18 import org.simantics.db.Resource;
19 import org.simantics.db.common.procedure.adapter.AsyncListenerAdapter;
20 import org.simantics.db.common.procedure.adapter.DisposableListener;
21 import org.simantics.db.common.procedure.adapter.DisposableSyncListener;
22 import org.simantics.db.common.request.TernaryRead;
23 import org.simantics.db.common.request.UnaryRead;
24 import org.simantics.db.common.request.UniqueRead;
25 import org.simantics.db.exception.DatabaseException;
26 import org.simantics.db.layer0.util.Layer0Utils;
27 import org.simantics.db.layer0.variable.Variable;
28 import org.simantics.db.layer0.variable.Variables;
29 import org.simantics.layer0.Layer0;
30 import org.simantics.modeling.ModelingUtils;
31 import org.simantics.scl.compiler.errors.CompilationError;
32 import org.simantics.scl.compiler.errors.Locations;
33 import org.simantics.scl.osgi.issues.SCLIssueProviderFactory;
34 import org.simantics.scl.osgi.issues.SCLIssueProviderFactory.SCLIssueProvider;
35 import org.simantics.scl.osgi.issues.SCLIssuesTableEntry;
36 import org.simantics.scl.runtime.SCLContext;
37 import org.simantics.scl.runtime.function.Function1;
38 import org.simantics.structural.stubs.StructuralResource2;
39 import org.simantics.ui.workbench.action.DefaultActions;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
43 public class SCLExpressionIssueProvider implements SCLIssueProvider {
45 public static class SCLExpressionIssueProviderFactory implements SCLIssueProviderFactory {
48 public SCLIssueProvider getSCLIssueProvider() {
49 return new SCLExpressionIssueProvider();
54 private static final Logger LOGGER = LoggerFactory.getLogger(SCLExpressionIssueProvider.class);
55 private boolean disposed = false;
56 private ComponentSyncListenerAdapter listener;
58 SCLExpressionIssueProvider() {
62 public void listenIssues(Runnable callback) {
63 listener = new ComponentSyncListenerAdapter(callback);
64 Simantics.getSession().asyncRequest(new ComponentRequest(), listener);
68 public List<SCLIssuesTableEntry> getIssues() {
69 return listener.getIssues();
73 public void dispose() {
78 private static void openResource(Shell shell, Resource resource) {
79 DefaultActions.performDefaultAction(shell, new StructuredSelection(resource));
82 private static class ComponentRequest extends UniqueRead<Set<Resource>> {
85 public Set<Resource> perform(ReadGraph graph) throws DatabaseException {
86 Layer0 L0 = Layer0.getInstance(graph);
87 Set<Resource> indexRoots = new TreeSet<Resource>();
88 for(Resource ontology : Layer0Utils.listOntologies(graph)) {
89 if (graph.isInstanceOf(ontology, L0.SharedOntology)) {
90 indexRoots.add(ontology);
94 for(Resource child : graph.getObjects(Simantics.getProjectResource(), L0.ConsistsOf)) {
95 if (graph.isInstanceOf(child, L0.IndexRoot)) {
96 indexRoots.add(child);
100 StructuralResource2 STR = StructuralResource2.getInstance(graph);
102 Set<Resource> allComponents = new HashSet<>();
103 for (Resource ontology : indexRoots) {
104 List<Resource> components = ModelingUtils.searchByTypeShallow(graph, ontology, STR.Component);
105 allComponents.addAll(components);
107 return allComponents;
111 private static class SCLValueRequest extends UnaryRead<Resource, Set<ResourceHolder>> {
113 public SCLValueRequest(Resource parameter) {
118 public Set<ResourceHolder> perform(ReadGraph graph) throws DatabaseException {
119 Layer0 L0 = Layer0.getInstance(graph);
120 Set<ResourceHolder> results = new HashSet<>();
121 for (Resource predicate : graph.getPredicates(parameter)) {
122 if (graph.isSubrelationOf(predicate, L0.HasProperty)) {
123 for (Resource object : graph.getObjects(parameter, predicate)) {
124 if (graph.isInstanceOf(object, L0.SCLValue)) {
125 results.add(new ResourceHolder(parameter, predicate, object));
134 private static class SCLExpressionValidationRequest extends TernaryRead<Resource, Resource, Resource, SCLIssuesTableEntry> {
136 public SCLExpressionValidationRequest(Resource component, Resource predicate, Resource object) {
137 super(component, predicate, object);
141 public SCLIssuesTableEntry perform(ReadGraph graph) throws DatabaseException {
142 Resource type = graph.getPossibleType(parameter3, Layer0.getInstance(graph).SCLValue);
146 if (!graph.hasStatement(parameter))
149 Variable componentVariable = Variables.getPossibleVariable(graph, parameter);
150 if (componentVariable == null) {
151 // Resource might be deleted already and therefore no URI available for variable building
154 Variable propertyVariable = componentVariable.getProperty(graph, parameter2);
156 Variable typeVariable = Variables.getVariable(graph, type);
158 Function1<Variable, String> func = typeVariable.getPossiblePropertyValue(graph, "validator");
160 // No validator available
161 if (LOGGER.isTraceEnabled())
162 LOGGER.trace("No validator available for " + typeVariable.getURI(graph));
166 SCLContext sclContext = SCLContext.getCurrent();
167 Object oldGraph = sclContext.get("graph");
169 sclContext.put("graph", graph);
170 String validatorValue = func.apply(propertyVariable);
171 if (validatorValue != null && !validatorValue.isEmpty()) {
172 return new SCLIssuesTableEntry(propertyVariable.getURI(graph), new CompilationError(Locations.NO_LOCATION, validatorValue.replace("\n", " "))) {
174 public void openLocation() {
175 openResource(Display.getCurrent().getActiveShell(), parameter);
179 } catch (Throwable t) {
180 LOGGER.error("Failed to invoke type validator function " + func, t);
182 sclContext.put("graph", oldGraph);
188 private static class ComponentSyncListenerAdapter extends AsyncListenerAdapter<Set<Resource>> implements Disposable {
190 private ConcurrentHashMap<Resource, SCLValueDisposableSyncListener> currentlyListening = new ConcurrentHashMap<>();
191 private boolean disposed;
192 private Runnable callback;
194 public ComponentSyncListenerAdapter(Runnable callback) {
195 this.callback = callback;
199 public void execute(AsyncReadGraph graph, Set<Resource> newComponents) {
200 if (currentlyListening.isEmpty() && newComponents.isEmpty()) {
201 // we can stop here as nothing will change
205 Set<Resource> removedComponents = new HashSet<>(currentlyListening.keySet());
206 removedComponents.removeAll(newComponents);
208 Set<Resource> addedComponents = new HashSet<>(newComponents);
209 addedComponents.removeAll(currentlyListening.keySet());
211 for (Resource removedComponent : removedComponents) {
213 DisposableSyncListener<?> listener = currentlyListening.remove(removedComponent);
217 for (Resource addedComponent : addedComponents) {
218 SCLValueDisposableSyncListener listener = new SCLValueDisposableSyncListener(callback);
219 currentlyListening.put(addedComponent, listener);
220 graph.asyncRequest(new SCLValueRequest(addedComponent), listener);
224 public List<SCLIssuesTableEntry> getIssues() {
225 List<SCLIssuesTableEntry> issues = new ArrayList<>();
226 for (SCLValueDisposableSyncListener listener : currentlyListening.values()) {
227 List<SCLIssuesTableEntry> listenerIssues = listener.getIssues();
228 if (listenerIssues != null && !listenerIssues.isEmpty())
229 issues.addAll(listenerIssues);
235 public void dispose() {
236 currentlyListening.values().forEach(l -> l.dispose());
237 currentlyListening.clear();
238 this.disposed = true;
242 public boolean isDisposed() {
247 private static class SCLValueDisposableSyncListener extends DisposableSyncListener<Set<ResourceHolder>> {
249 private Runnable callback;
250 private ConcurrentHashMap<ResourceHolder, SCLIssuesTableEntryDisposableListener> currentlyListeningSCLValues = new ConcurrentHashMap<>();
252 public SCLValueDisposableSyncListener(Runnable callback) {
253 this.callback = callback;
257 public void execute(ReadGraph graph, Set<ResourceHolder> newComponents) throws DatabaseException {
258 if (currentlyListeningSCLValues.isEmpty() && newComponents.isEmpty()) {
259 // we can stop here as nothing will change
264 Set<ResourceHolder> removedComponents = new HashSet<>(currentlyListeningSCLValues.keySet());
265 removedComponents.removeAll(newComponents);
267 Set<ResourceHolder> addedComponents = new HashSet<>(newComponents);
268 addedComponents.removeAll(currentlyListeningSCLValues.keySet());
270 for (ResourceHolder removedComponent : removedComponents) {
272 DisposableListener<?> listener = currentlyListeningSCLValues.remove(removedComponent);
276 for (ResourceHolder sclValue : addedComponents) {
277 SCLIssuesTableEntryDisposableListener listener = new SCLIssuesTableEntryDisposableListener(callback);
278 currentlyListeningSCLValues.put(sclValue, listener);
279 graph.syncRequest(new SCLExpressionValidationRequest(sclValue.component, sclValue.predicate, sclValue.object), listener);
281 if (callback != null) {
286 public List<SCLIssuesTableEntry> getIssues() {
287 if (currentlyListeningSCLValues.isEmpty())
289 List<SCLIssuesTableEntry> issues = new ArrayList<>();
290 for (SCLIssuesTableEntryDisposableListener listener : currentlyListeningSCLValues.values()) {
291 if (listener.getResult() != null)
292 issues.add(listener.getResult());
298 public void exception(ReadGraph graph, Throwable throwable) throws DatabaseException {
299 LOGGER.error("Could not listen", throwable);
303 public void dispose() {
304 currentlyListeningSCLValues.values().forEach(l -> l.dispose());
305 currentlyListeningSCLValues.clear();
311 private static class SCLIssuesTableEntryDisposableListener extends DisposableListener<SCLIssuesTableEntry> {
313 private SCLIssuesTableEntry result;
314 private Runnable callback;
316 public SCLIssuesTableEntryDisposableListener(Runnable callback) {
317 this.callback = callback;
321 public void execute(SCLIssuesTableEntry result) {
322 if (!Objects.equals(this.result, result)) {
323 this.result = result;
324 if (callback != null) {
331 public void exception(Throwable t) {
335 public SCLIssuesTableEntry getResult() {
340 private static class ResourceHolder {
346 public ResourceHolder(Resource component, Resource predicate, Resource object) {
347 this.component = Objects.requireNonNull(component);
348 this.predicate = Objects.requireNonNull(predicate);
349 this.object = Objects.requireNonNull(object);
353 public int hashCode() {
354 final int prime = 31;
356 result = prime * result + component.hashCode();
357 result = prime * result + object.hashCode();
358 result = prime * result + predicate.hashCode();
363 public boolean equals(Object obj) {
368 if (getClass() != obj.getClass())
370 ResourceHolder other = (ResourceHolder) obj;
371 if (!component.equals(other.component))
373 if (!object.equals(other.object))
375 if (!predicate.equals(other.predicate))