1 package org.simantics.scl.db;
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.Collections;
8 import java.util.TreeSet;
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;
23 public class UsedSCLExpressionsRequest extends UniqueRead<Collection<SCLExpressionTableEntry>> {
25 private static final Logger LOGGER = LoggerFactory.getLogger(UsedSCLExpressionsRequest.class);
27 private static final String structuralResourceComponentURI = "http://www.simantics.org/Structural-1.2/Component";
30 public Collection<SCLExpressionTableEntry> perform(ReadGraph graph) throws DatabaseException {
31 Collection<SCLExpressionTableEntry> result = new ArrayList<>();
32 Layer0 L0 = Layer0.getInstance(graph);
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);
40 for(Resource child : graph.getObjects(Simantics.getProjectResource(), L0.ConsistsOf)) {
41 if (graph.isInstanceOf(child, L0.IndexRoot)) {
42 indexRoots.add(child);
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);
57 String uri = graph.getPossibleURI(source);
58 String pred = graph.getRelatedValue2(propertyStatement.getPredicate(), L0.HasName);
61 result.add(new SCLExpressionTableEntry(expression, uri + "#" + pred, source));
69 LOGGER.info("{} is not available for finding expressions in component properties", structuralResourceComponentURI);
74 public static Collection<SCLExpressionTableEntry> execute() {
76 return Simantics.getSession().syncRequest(new UsedSCLExpressionsRequest(), TransientCacheListener.instance());
77 } catch (DatabaseException e) {
79 return Collections.emptyList();