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;
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;
}
}
}
}
+ 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);
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;
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.