package org.simantics.modeling.ui.scl.expressions; import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.Set; import java.util.TreeSet; import org.simantics.Simantics; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.Statement; import org.simantics.db.common.request.UniqueRead; import org.simantics.db.exception.DatabaseException; import org.simantics.db.layer0.util.Layer0Utils; import org.simantics.layer0.Layer0; import org.simantics.modeling.ModelingUtils; import org.simantics.structural.stubs.StructuralResource2; public class UsedSCLExpressionsRequest extends UniqueRead> { @Override public Collection perform(ReadGraph graph) throws DatabaseException { Collection result = new ArrayList<>(); Layer0 L0 = Layer0.getInstance(graph); Set indexRoots = new TreeSet(); for(Resource ontology : Layer0Utils.listOntologies(graph)) { if (graph.isInstanceOf(ontology, L0.SharedOntology)) { indexRoots.add(ontology); } } for(Resource child : graph.getObjects(Simantics.getProjectResource(), L0.ConsistsOf)) { if (graph.isInstanceOf(child, L0.IndexRoot)) { indexRoots.add(child); } } for (Resource ontology : indexRoots) { // List modules = ModelingUtils.searchByTypeShallow(graph, ontology, L0.SCLModule); // for (Resource module : modules) { // String definition = graph.getPossibleRelatedValue2(module, L0.SCLModule_definition); // Matcher matcher = pattern.matcher(definition); // while (matcher.find()) { // int index = matcher.start(); // int line = 1; // int pos = 0; // while ((pos = definition.indexOf("\n", pos) + 1) > 0 && pos <= index) { // line++; // } // String position = graph.getPossibleURI(module) + ":" + line; // int lineBeginIndex = definition.lastIndexOf("\n", index); // if (lineBeginIndex == -1) lineBeginIndex = 0; // int lineEndIndex = definition.indexOf("\n", index); // if (lineEndIndex <= 0) lineEndIndex = definition.length(); // String content = definition.substring(lineBeginIndex, lineEndIndex).trim(); // // result.add(new SCLExpressionTableEntry(content, position)); // } // } StructuralResource2 STR = StructuralResource2.getInstance(graph); List components = ModelingUtils.searchByTypeShallow(graph, ontology, STR.Component); for (Resource component : components) { for (Statement propertyStatement : graph.getStatements(component, L0.HasProperty)) { if (graph.isInstanceOf(propertyStatement.getObject(), L0.SCLValue)) { Resource sclValue = propertyStatement.getObject(); String expression = graph.getPossibleRelatedValue2(sclValue, L0.SCLValue_expression); Resource source = graph.getPossibleObject(sclValue, L0.PropertyOf); if (source != null) { String uri = graph.getPossibleURI(source); String pred = graph.getRelatedValue2(propertyStatement.getPredicate(), L0.HasName); if (uri != null) { // SCLReporting.print(); // SCLReporting.printError(expression + "\n"); result.add(new SCLExpressionTableEntry(expression, uri + "#" + pred, source)); } } } } } } return result; } }