]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
(refs #7604) Improvements to graph handling in SCL source modules 02/1202/1
authorHannu Niemistö <hannu.niemisto@semantum.fi>
Mon, 6 Nov 2017 16:07:52 +0000 (18:07 +0200)
committerHannu Niemistö <hannu.niemisto@semantum.fi>
Mon, 6 Nov 2017 16:07:52 +0000 (18:07 +0200)
This commit also improves exceptions printed by CommandSession.

Change-Id: Ifda4468ae31b8ea08656b2dd60f4255c19d11ee3

bundles/org.simantics.modeling/src/org/simantics/modeling/scl/GraphModuleSourceRepository.java
bundles/org.simantics.modeling/src/org/simantics/modeling/scl/OntologyModuleSourceRepository.java
bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/commands/CommandSession.java
bundles/org.simantics/src/org/simantics/Simantics.java

index 09f7694dd482e5f1ce53ab784160ec3a078a2967..b6d6caefd25c6a93de3eca1990e39fdb62cfbbd6 100644 (file)
@@ -5,7 +5,6 @@ import java.util.Collection;
 
 import org.simantics.Simantics;
 import org.simantics.db.ReadGraph;
-import org.simantics.db.RequestProcessorSpecific;
 import org.simantics.db.Resource;
 import org.simantics.db.WriteGraph;
 import org.simantics.db.common.request.UnaryRead;
@@ -26,36 +25,33 @@ import org.simantics.scl.osgi.internal.OsgiJavaReferenceValidatorFactory;
 import org.simantics.scl.runtime.SCLContext;
 import org.simantics.scl.runtime.tuple.Tuple0;
 import org.simantics.structural2.utils.StructuralUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import gnu.trove.procedure.TObjectProcedure;
 import gnu.trove.set.hash.THashSet;
 
 public enum GraphModuleSourceRepository implements ModuleSourceRepository {
-    INSTANCE;
+    INSTANCE; 
     
+    private static final Logger LOGGER = LoggerFactory.getLogger(OntologyModuleSourceRepository.class);
+
     private static final OsgiJavaReferenceValidatorFactory REFERENCE_VALIDATOR_FACTORY = new OsgiJavaReferenceValidatorFactory(Activator.getContext().getBundle());
 
     @Override
     public ModuleSource getModuleSource(final String moduleName, UpdateListener listener) {
         if(!moduleName.startsWith("http://"))
-            return null;
-
-        Object graph = SCLContext.getCurrent().get("graph");
-        RequestProcessorSpecific requestProcessor;
-        if(graph instanceof ReadGraph)
-            requestProcessor = (ReadGraph)graph;
-        else
-            requestProcessor = Simantics.getSession();
+            return null; // Don't do a graph request if this cannot be a resource
 
         Read<ModuleSource> request = new ReadModuleSource(moduleName);
 
         try {
             if(listener != null)
-                return requestProcessor.syncRequest(request, new ModuleListener(listener, moduleName));
+                return Simantics.getAvailableRequestProcessor().syncRequest(request, new ModuleListener(listener, moduleName));
             else
-                return requestProcessor.syncRequest(request);
+                return Simantics.getAvailableRequestProcessor().syncRequest(request);
         } catch (DatabaseException e) {
-            e.printStackTrace();
+            LOGGER.error("Failed to read graph module " + moduleName + ".", e);
             return null;
         }
     }
@@ -175,31 +171,34 @@ public enum GraphModuleSourceRepository implements ModuleSourceRepository {
         }
     }
 
+    private THashSet<String> getAllModules(ReadGraph graph) throws DatabaseException {
+        THashSet<String> result = new THashSet<String>(); 
+        Resource projectResource = Simantics.getProjectResource();
+        Layer0 L0 = Layer0.getInstance(graph);
+        for(Resource model : graph.getObjects(projectResource, L0.ConsistsOf)) {
+            if(graph.isInstanceOf(model, L0.IndexRoot)) {
+                for(Resource module : ModelingUtils.searchByType(graph, model, L0.SCLModule))
+                    result.add(graph.getURI(module));
+            }
+        }
+        
+        Collection<Resource> ontologies = Simantics.applySCL("Simantics/SharedOntologies", "getSharedOntologies", graph, Tuple0.INSTANCE);
+        for (Resource ontology : ontologies) {
+            for(Resource module : ModelingUtils.searchByType(graph, ontology, L0.SCLModule))
+                result.add(graph.getURI(module));
+        }
+        
+        return result;
+    }
+    
     @Override
     public void forAllModules(TObjectProcedure<String> procedure) {
-        THashSet<String> moduleURIs;
         try {
-            moduleURIs = Simantics.getSession().syncRequest(new Read<THashSet<String>>() {
+            THashSet<String> moduleURIs = Simantics.getAvailableRequestProcessor().syncRequest(new Read<THashSet<String>>() {
                 @Override
                 public THashSet<String> perform(ReadGraph graph)
                         throws DatabaseException {
-                    THashSet<String> result = new THashSet<String>(); 
-                    Resource projectResource = Simantics.getProjectResource();
-                    Layer0 L0 = Layer0.getInstance(graph);
-                    for(Resource model : graph.getObjects(projectResource, L0.ConsistsOf)) {
-                        if(graph.isInstanceOf(model, L0.IndexRoot)) {
-                            for(Resource module : ModelingUtils.searchByType(graph, model, L0.SCLModule))
-                                result.add(graph.getURI(module));
-                        }
-                    }
-                    
-                    Collection<Resource> ontologies = Simantics.applySCL("Simantics/SharedOntologies", "getSharedOntologies", graph, Tuple0.INSTANCE);
-                    for (Resource ontology : ontologies) {
-                        for(Resource module : ModelingUtils.searchByType(graph, ontology, L0.SCLModule))
-                            result.add(graph.getURI(module));
-                    }
-                    
-                    return result;
+                    return getAllModules(graph);
                 }
             });
             moduleURIs.forEach(procedure);
index 1e9df2e192ae1a2fe0f9ab952ce4e3c15eb313fb..b91e90d61c42ce83f8ccb8f3ecb74d8cdb80ff87 100644 (file)
@@ -12,7 +12,6 @@ import org.simantics.scl.compiler.module.repository.UpdateListener;
 import org.simantics.scl.compiler.source.ModuleSource;
 import org.simantics.scl.compiler.source.PrecompiledModuleSource;
 import org.simantics.scl.compiler.source.repository.ModuleSourceRepository;
-import org.simantics.scl.runtime.SCLContext;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -42,14 +41,8 @@ public enum OntologyModuleSourceRepository implements ModuleSourceRepository {
         if(!moduleName.startsWith("http://"))
             return null; // Don't do a graph request if this cannot be a resource
         
-        ReadGraph graph = (ReadGraph)SCLContext.getCurrent().get("graph");
-        
         try {
-            if(graph != null) {
-                return new PrecompiledModuleSource(new OntologyModule(graph, moduleName), -1.0);
-            }
-
-            return Simantics.getSession().syncRequest(new ModuleSourceRequest(moduleName));
+            return Simantics.getAvailableRequestProcessor().syncRequest(new ModuleSourceRequest(moduleName));
         } catch(DatabaseException e) {
             LOGGER.error("Failed to read ontology module " + moduleName + ".", e);
             return null;
index 9ff2ebabb0b3492c27814e0d0dd013b552b50044..46cd0cb457d8a2567cfe90b5c13dff6bc55f0a2c 100644 (file)
@@ -598,7 +598,9 @@ public class CommandSession {
             b.append("\tat ");
             if("_SCL_Module".equals(fileName)
                     || "_SCL_TypeClassInstance".equals(fileName))
-                b.append(NameMangling.demangle(methodName))
+                b.append(className)
+                .append('.')
+                .append(NameMangling.demangle(methodName))
                 .append('(').append(element.getLineNumber()).append(')');
             else
                 b.append(element);
index b382fba9151798aa6f414b6b1f65071ebcc21212..c19efde85913a10c0ed6befba804d01f90b1fab7 100644 (file)
@@ -24,6 +24,8 @@ import org.simantics.SimanticsPlatform.RecoveryPolicy;
 import org.simantics.application.arguments.IArguments;
 import org.simantics.application.arguments.SimanticsArguments;
 import org.simantics.db.ReadGraph;
+import org.simantics.db.RequestProcessor;
+import org.simantics.db.RequestProcessorSpecific;
 import org.simantics.db.Resource;
 import org.simantics.db.Session;
 import org.simantics.db.WriteGraph;
@@ -283,6 +285,14 @@ public class Simantics {
             throw new IllegalStateException("Session unavailable, no database session open");
         return ctx.getSession();
     }
+    
+    public static RequestProcessor getAvailableRequestProcessor() {
+        Object graph = SCLContext.getCurrent().get("graph");
+        if(graph instanceof ReadGraph)
+            return (RequestProcessor)graph;
+        else
+            return Simantics.getSession();
+    }
 
     /**
      * Returns the database Session bound to the currently active context.