]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.modeling/src/org/simantics/modeling/scl/ontologymodule/OntologyModule.java
Merge "Use proper environment to resolve SCL types in ontology module"
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / scl / ontologymodule / OntologyModule.java
index ec698e670514984fa42128d3211238d503a93a35..96ee4218236f1719a9888657c4a3b3ed5111b8a8 100644 (file)
@@ -2,7 +2,6 @@ package org.simantics.modeling.scl.ontologymodule;
 
 import java.util.Arrays;
 import java.util.Collection;
-import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -12,8 +11,11 @@ import org.simantics.Simantics;
 import org.simantics.databoard.Bindings;
 import org.simantics.db.ReadGraph;
 import org.simantics.db.Resource;
+import org.simantics.db.common.request.IndexRoot;
 import org.simantics.db.common.uri.UnescapedChildMapOfResource;
 import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.util.EnvironmentRequest;
+import org.simantics.db.layer0.util.RuntimeEnvironmentRequest;
 import org.simantics.db.request.Read;
 import org.simantics.layer0.Layer0;
 import org.simantics.scl.compiler.common.names.Name;
@@ -30,7 +32,9 @@ import org.simantics.scl.compiler.elaboration.macros.MacroRule;
 import org.simantics.scl.compiler.elaboration.modules.SCLValue;
 import org.simantics.scl.compiler.elaboration.relations.SCLEntityType;
 import org.simantics.scl.compiler.elaboration.relations.SCLRelation;
+import org.simantics.scl.compiler.environment.Environment;
 import org.simantics.scl.compiler.environment.filter.NamespaceFilter;
+import org.simantics.scl.compiler.environment.specification.EnvironmentSpecification;
 import org.simantics.scl.compiler.errors.Locations;
 import org.simantics.scl.compiler.module.ImportDeclaration;
 import org.simantics.scl.compiler.module.LazyModule;
@@ -41,6 +45,7 @@ import org.simantics.scl.compiler.types.Types;
 import org.simantics.scl.compiler.types.exceptions.SCLTypeParseException;
 import org.simantics.scl.compiler.types.kinds.Kinds;
 import org.simantics.scl.runtime.SCLContext;
+import org.simantics.utils.datastructures.Pair;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -50,6 +55,7 @@ import gnu.trove.procedure.TObjectProcedure;
 public class OntologyModule extends LazyModule {
     private static final Logger LOGGER = LoggerFactory.getLogger(OntologyModule.class);
     
+    public static final String SCL_TYPES_NAME = "SCLTypes";
     private static final String DB_MODULE = "Simantics/DB";
     private static final String VARIABLE_MODULE = "Simantics/Variable";
     private static final Collection<ImportDeclaration> DEPENDENCIES = Arrays.asList(
@@ -60,15 +66,38 @@ public class OntologyModule extends LazyModule {
     private static final TCon BROWSABLE = Types.con(DB_MODULE, "Browsable");
     private static final TCon VARIABLE = Types.con(VARIABLE_MODULE, "Variable");
     
-    Resource ontology;
-    String defaultLocalName;
-    THashMap<Resource,Map<String,Resource>> childMaps = new THashMap<Resource,Map<String,Resource>>();
+    private Resource ontology;
+    private String defaultLocalName;
+    private THashMap<Resource,Map<String,Resource>> childMaps = new THashMap<Resource,Map<String,Resource>>();
+    private List<ImportDeclaration> importDeclarations;
+    private Environment environment;
     
     public OntologyModule(ReadGraph graph, String moduleName) throws DatabaseException {
         super(moduleName);
         ontology = graph.getResource(moduleName);
         readDefaultLocalName(graph);
         childMaps.put(ontology, createLocalMap(graph, ontology));
+        Pair<EnvironmentSpecification, Environment> pair = graph.syncRequest(new Read<Pair<EnvironmentSpecification, Environment>>() {
+            @Override
+            public Pair<EnvironmentSpecification, Environment> perform(ReadGraph graph) throws DatabaseException {
+                Resource indexRoot = graph.syncRequest(new IndexRoot(ontology));
+                return graph.syncRequest(new EnvironmentRequest(indexRoot) {
+                    @Override
+                    protected void fillEnvironmentSpecification(EnvironmentSpecification environmentSpecification) {
+                        /*if(!moduleName.equals("http://www.simantics.org/Layer0-1.1")) { // Prevent cyclic dependencies
+                            environmentSpecification.importModule(DB_MODULE, "");
+                            environmentSpecification.importModule(VARIABLE_MODULE, "");
+                        }*/
+                    }
+                    @Override
+                    protected String getRootModuleName() {
+                        return SCL_TYPES_NAME;
+                    }
+                });
+            }
+        });
+        this.importDeclarations = pair.first.imports;
+        this.environment = pair.second;
     }
     
     private void readDefaultLocalName(ReadGraph graph) throws DatabaseException {
@@ -85,8 +114,7 @@ public class OntologyModule extends LazyModule {
 
     @Override
     public List<ImportDeclaration> getDependencies() {
-        //return DEPENDENCIES;
-        return Collections.emptyList();
+        return importDeclarations;
     }
     
     private static interface ResourceSearchResult {}
@@ -180,7 +208,7 @@ public class OntologyModule extends LazyModule {
     
     @FunctionalInterface
     private static interface ResourceFunctionGenerator {
-        SCLValue createValue(Name name, Resource resource);
+        SCLValue createValue(Name name, Resource resource, Environment environment);
     }
     
     private static class RelatedValueMacroRule implements MacroRule {
@@ -245,8 +273,8 @@ public class OntologyModule extends LazyModule {
     private final static HashMap<String, ResourceFunctionGenerator> VALUE_GENERATOR_MAP = new HashMap<>();
     static {
         TVar A = Types.var(Kinds.STAR);
-        VALUE_GENERATOR_MAP.put("value", (name, resource) -> {
-            SCLRelationInfo relationInfo = SCLRelationInfoRequest.getRelationInfo(resource);
+        VALUE_GENERATOR_MAP.put("value", (name, resource, environment) -> {
+            SCLRelationInfo relationInfo = SCLRelationInfoRequest.getRelationInfo(resource, environment);
             if(relationInfo == null)
                 return null;
             
@@ -255,8 +283,8 @@ public class OntologyModule extends LazyModule {
             value.setMacroRule(new RelatedValueMacroRule(resource, relationInfo, false));
             return value;
         });
-        VALUE_GENERATOR_MAP.put("possibleValue", (name, resource) -> {
-            SCLRelationInfo relationInfo = SCLRelationInfoRequest.getRelationInfo(resource);
+        VALUE_GENERATOR_MAP.put("possibleValue", (name, resource, environment) -> {
+            SCLRelationInfo relationInfo = SCLRelationInfoRequest.getRelationInfo(resource, environment);
             if(relationInfo == null)
                 return null;
             
@@ -284,7 +312,7 @@ public class OntologyModule extends LazyModule {
             if(generator == null)
                 return null;
             else
-                return generator.createValue(Name.create(getName(), name), resourceAndSuffix.resource);
+                return generator.createValue(Name.create(getName(), name), resourceAndSuffix.resource, environment);
         }
         else
             return null;
@@ -469,6 +497,7 @@ public class OntologyModule extends LazyModule {
         childMaps.clear();
         childMaps = null;
         ontology = null;
+        environment = null;
     }
     
     @Override