]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.db/src/org/simantics/scl/db/UsedSCLExpressionsRequest.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.scl.db / src / org / simantics / scl / db / UsedSCLExpressionsRequest.java
1 package org.simantics.scl.db;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.Collections;
6 import java.util.List;
7 import java.util.Set;
8 import java.util.TreeSet;
9
10 import org.simantics.Simantics;
11 import org.simantics.db.ReadGraph;
12 import org.simantics.db.Resource;
13 import org.simantics.db.Statement;
14 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
15 import org.simantics.db.common.request.UniqueRead;
16 import org.simantics.db.exception.DatabaseException;
17 import org.simantics.db.layer0.QueryIndexUtils;
18 import org.simantics.db.layer0.util.Layer0Utils;
19 import org.simantics.layer0.Layer0;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 public class UsedSCLExpressionsRequest extends UniqueRead<Collection<SCLExpressionTableEntry>> {
24
25     private static final Logger LOGGER = LoggerFactory.getLogger(UsedSCLExpressionsRequest.class);
26
27     private static final String structuralResourceComponentURI = "http://www.simantics.org/Structural-1.2/Component";
28
29     @Override
30     public Collection<SCLExpressionTableEntry> perform(ReadGraph graph) throws DatabaseException {
31         Collection<SCLExpressionTableEntry> result = new ArrayList<>();
32         Layer0 L0 = Layer0.getInstance(graph);
33         
34         Set<Resource> indexRoots = new TreeSet<Resource>();
35         for(Resource ontology : Layer0Utils.listOntologies(graph)) {
36             if (graph.isInstanceOf(ontology, L0.SharedOntology)) {
37                 indexRoots.add(ontology);
38             }
39         }
40         for(Resource child : graph.getObjects(Simantics.getProjectResource(), L0.ConsistsOf)) {
41             if (graph.isInstanceOf(child, L0.IndexRoot)) {
42                 indexRoots.add(child);
43             }
44         }
45
46         Resource componentResource = graph.getPossibleResource(structuralResourceComponentURI);
47         if (componentResource != null) {
48             for (Resource ontology : indexRoots) {
49                 List<Resource> components = QueryIndexUtils.searchByTypeShallow(graph, ontology, componentResource);
50                 for (Resource component : components) {
51                     for (Statement propertyStatement : graph.getStatements(component, L0.HasProperty)) {
52                         if (graph.isInstanceOf(propertyStatement.getObject(), L0.SCLValue)) {
53                             Resource sclValue = propertyStatement.getObject();
54                             String expression = graph.getPossibleRelatedValue2(sclValue, L0.SCLValue_expression);
55                             Resource source = graph.getPossibleObject(sclValue, L0.PropertyOf);
56                             if (source != null) {
57                                 String uri = graph.getPossibleURI(source);
58                                 String pred = graph.getRelatedValue2(propertyStatement.getPredicate(), L0.HasName);
59     
60                                 if (uri != null) {
61                                     result.add(new SCLExpressionTableEntry(expression, uri + "#" + pred, source));
62                                 }
63                             }
64                         }
65                     }
66                 }
67             }
68         } else {
69             LOGGER.info("{} is not available for finding expressions in component properties", structuralResourceComponentURI);
70         }
71         return result;
72     }
73
74     public static Collection<SCLExpressionTableEntry> execute() {
75         try {
76             return Simantics.getSession().syncRequest(new UsedSCLExpressionsRequest(), TransientCacheListener.instance());
77         } catch (DatabaseException e) {
78             e.printStackTrace();
79             return Collections.emptyList();
80         }
81     }
82
83 }