1 package org.simantics.modeling.ui.scl.expressions;
3 import java.util.ArrayList;
4 import java.util.Collection;
7 import java.util.TreeSet;
9 import org.simantics.Simantics;
10 import org.simantics.db.ReadGraph;
11 import org.simantics.db.Resource;
12 import org.simantics.db.Statement;
13 import org.simantics.db.common.request.UniqueRead;
14 import org.simantics.db.exception.DatabaseException;
15 import org.simantics.db.layer0.util.Layer0Utils;
16 import org.simantics.layer0.Layer0;
17 import org.simantics.modeling.ModelingUtils;
18 import org.simantics.structural.stubs.StructuralResource2;
20 public class UsedSCLExpressionsRequest extends UniqueRead<Collection<SCLExpressionTableEntry>> {
23 public Collection<SCLExpressionTableEntry> perform(ReadGraph graph) throws DatabaseException {
24 Collection<SCLExpressionTableEntry> result = new ArrayList<>();
25 Layer0 L0 = Layer0.getInstance(graph);
27 Set<Resource> indexRoots = new TreeSet<Resource>();
28 for(Resource ontology : Layer0Utils.listOntologies(graph)) {
29 if (graph.isInstanceOf(ontology, L0.SharedOntology)) {
30 indexRoots.add(ontology);
34 for(Resource child : graph.getObjects(Simantics.getProjectResource(), L0.ConsistsOf)) {
35 if (graph.isInstanceOf(child, L0.IndexRoot)) {
36 indexRoots.add(child);
40 for (Resource ontology : indexRoots) {
41 // List<Resource> modules = ModelingUtils.searchByTypeShallow(graph, ontology, L0.SCLModule);
42 // for (Resource module : modules) {
43 // String definition = graph.getPossibleRelatedValue2(module, L0.SCLModule_definition);
44 // Matcher matcher = pattern.matcher(definition);
45 // while (matcher.find()) {
46 // int index = matcher.start();
49 // while ((pos = definition.indexOf("\n", pos) + 1) > 0 && pos <= index) {
52 // String position = graph.getPossibleURI(module) + ":" + line;
53 // int lineBeginIndex = definition.lastIndexOf("\n", index);
54 // if (lineBeginIndex == -1) lineBeginIndex = 0;
55 // int lineEndIndex = definition.indexOf("\n", index);
56 // if (lineEndIndex <= 0) lineEndIndex = definition.length();
57 // String content = definition.substring(lineBeginIndex, lineEndIndex).trim();
59 // result.add(new SCLExpressionTableEntry(content, position));
63 StructuralResource2 STR = StructuralResource2.getInstance(graph);
65 List<Resource> components = ModelingUtils.searchByTypeShallow(graph, ontology, STR.Component);
66 for (Resource component : components) {
67 for (Statement propertyStatement : graph.getStatements(component, L0.HasProperty)) {
68 if (graph.isInstanceOf(propertyStatement.getObject(), L0.SCLValue)) {
69 Resource sclValue = propertyStatement.getObject();
70 String expression = graph.getPossibleRelatedValue2(sclValue, L0.SCLValue_expression);
71 Resource source = graph.getPossibleObject(sclValue, L0.PropertyOf);
73 String uri = graph.getPossibleURI(source);
74 String pred = graph.getRelatedValue2(propertyStatement.getPredicate(), L0.HasName);
77 // SCLReporting.print();
78 // SCLReporting.printError(expression + "\n");
80 result.add(new SCLExpressionTableEntry(expression, uri + "#" + pred, source));