From 346cd36c02aad1e33652a67e03b1508cc58e723f Mon Sep 17 00:00:00 2001 From: =?utf8?q?Hannu=20Niemist=C3=B6?= Date: Mon, 6 Nov 2017 18:07:52 +0200 Subject: [PATCH] (refs #7604) Improvements to graph handling in SCL source modules This commit also improves exceptions printed by CommandSession. Change-Id: Ifda4468ae31b8ea08656b2dd60f4255c19d11ee3 --- .../scl/GraphModuleSourceRepository.java | 63 +++++++++---------- .../scl/OntologyModuleSourceRepository.java | 9 +-- .../scl/compiler/commands/CommandSession.java | 4 +- .../src/org/simantics/Simantics.java | 10 +++ 4 files changed, 45 insertions(+), 41 deletions(-) diff --git a/bundles/org.simantics.modeling/src/org/simantics/modeling/scl/GraphModuleSourceRepository.java b/bundles/org.simantics.modeling/src/org/simantics/modeling/scl/GraphModuleSourceRepository.java index 09f7694dd..b6d6caefd 100644 --- a/bundles/org.simantics.modeling/src/org/simantics/modeling/scl/GraphModuleSourceRepository.java +++ b/bundles/org.simantics.modeling/src/org/simantics/modeling/scl/GraphModuleSourceRepository.java @@ -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 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 getAllModules(ReadGraph graph) throws DatabaseException { + THashSet result = new THashSet(); + 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 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 procedure) { - THashSet moduleURIs; try { - moduleURIs = Simantics.getSession().syncRequest(new Read>() { + THashSet moduleURIs = Simantics.getAvailableRequestProcessor().syncRequest(new Read>() { @Override public THashSet perform(ReadGraph graph) throws DatabaseException { - THashSet result = new THashSet(); - 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 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); diff --git a/bundles/org.simantics.modeling/src/org/simantics/modeling/scl/OntologyModuleSourceRepository.java b/bundles/org.simantics.modeling/src/org/simantics/modeling/scl/OntologyModuleSourceRepository.java index 1e9df2e19..b91e90d61 100644 --- a/bundles/org.simantics.modeling/src/org/simantics/modeling/scl/OntologyModuleSourceRepository.java +++ b/bundles/org.simantics.modeling/src/org/simantics/modeling/scl/OntologyModuleSourceRepository.java @@ -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; diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/commands/CommandSession.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/commands/CommandSession.java index 9ff2ebabb..46cd0cb45 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/commands/CommandSession.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/commands/CommandSession.java @@ -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); diff --git a/bundles/org.simantics/src/org/simantics/Simantics.java b/bundles/org.simantics/src/org/simantics/Simantics.java index b382fba91..c19efde85 100644 --- a/bundles/org.simantics/src/org/simantics/Simantics.java +++ b/bundles/org.simantics/src/org/simantics/Simantics.java @@ -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. -- 2.43.2