View for used SCL expressions
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / scl / expressions / UsedSCLExpressionsRequest.java
1 package org.simantics.modeling.ui.scl.expressions;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.List;
6 import java.util.Set;
7 import java.util.TreeSet;
8
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;
19
20 public class UsedSCLExpressionsRequest extends UniqueRead<Collection<SCLExpressionTableEntry>> {
21
22     @Override
23     public Collection<SCLExpressionTableEntry> perform(ReadGraph graph) throws DatabaseException {
24         Collection<SCLExpressionTableEntry> result = new ArrayList<>();
25         Layer0 L0 = Layer0.getInstance(graph);
26         
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);
31             }
32         }
33         
34         for(Resource child : graph.getObjects(Simantics.getProjectResource(), L0.ConsistsOf)) {
35             if (graph.isInstanceOf(child, L0.IndexRoot)) {
36                 indexRoots.add(child);
37             }
38         }
39
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();
47 //                    int line = 1;
48 //                    int pos = 0;
49 //                    while ((pos = definition.indexOf("\n", pos) + 1) > 0 && pos <= index) {
50 //                        line++;
51 //                    }
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();
58 //
59 //                    result.add(new SCLExpressionTableEntry(content, position));
60 //                }
61 //            }
62             
63             StructuralResource2 STR = StructuralResource2.getInstance(graph);
64             
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);
72                         if (source != null) {
73                             String uri = graph.getPossibleURI(source);
74                             String pred = graph.getRelatedValue2(propertyStatement.getPredicate(), L0.HasName);
75
76                             if (uri != null) {
77 //                                SCLReporting.print();
78 //                                SCLReporting.printError(expression + "\n");
79                                 
80                                 result.add(new SCLExpressionTableEntry(expression, uri + "#" + pred, source));
81                             }
82                         }
83                     }
84                 }
85             }
86         }
87         return result;
88     }
89
90 }