]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.modeling/src/org/simantics/modeling/scl/ontologymodule/OntologyModule.java
Removed extra modules OntologyModule imported to namespace
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / scl / ontologymodule / OntologyModule.java
index ec698e670514984fa42128d3211238d503a93a35..aa1cffe23508cc2d51b32cb9a9222ac09edded53 100644 (file)
@@ -1,8 +1,8 @@
 package org.simantics.modeling.scl.ontologymodule;
 
+import java.util.ArrayList;
 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 +12,10 @@ 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.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,25 +55,46 @@ 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(
-            new ImportDeclaration(DB_MODULE, null),
-            new ImportDeclaration(VARIABLE_MODULE, null)
-            );
     private static final TCon RESOURCE = Types.con(DB_MODULE, "Resource");
     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<>();
+    private ArrayList<ImportDeclaration> importDeclarations = new ArrayList<>();
+    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;
+                    }
+                });
+            }
+        });
+        for(ImportDeclaration decl : pair.first.imports)
+            importDeclarations.add(decl.hidden());
+        this.environment = pair.second;
     }
     
     private void readDefaultLocalName(ReadGraph graph) throws DatabaseException {
@@ -85,8 +111,7 @@ public class OntologyModule extends LazyModule {
 
     @Override
     public List<ImportDeclaration> getDependencies() {
-        //return DEPENDENCIES;
-        return Collections.emptyList();
+        return importDeclarations;
     }
     
     private static interface ResourceSearchResult {}
@@ -94,7 +119,11 @@ public class OntologyModule extends LazyModule {
         public final Resource resource;
         public JustResource(Resource resource) {
             this.resource = resource;
-        }   
+        }
+        @Override
+        public String toString() {
+            return "JustResource(" + resource + ")";
+        }
     }
     private static class ResourceAndSuffix implements ResourceSearchResult {
         public final Resource resource;
@@ -102,7 +131,11 @@ public class OntologyModule extends LazyModule {
         public ResourceAndSuffix(Resource resource, String suffix) {
             this.resource = resource;
             this.suffix = suffix;
-        }  
+        }
+        @Override
+        public String toString() {
+            return "ResourceAndSuffix(" + resource + ", " + suffix + ")";
+        }
     }
     
     private ResourceSearchResult getResourceOrSuffixedResource(String name) {
@@ -180,7 +213,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 +278,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 +288,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 +317,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 +502,7 @@ public class OntologyModule extends LazyModule {
         childMaps.clear();
         childMaps = null;
         ontology = null;
+        environment = null;
     }
     
     @Override