]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.structural2/src/org/simantics/structural2/Functions.java
Resolving a variable for name that has no Resource representation fails.
[simantics/platform.git] / bundles / org.simantics.structural2 / src / org / simantics / structural2 / Functions.java
index d9d8857af77210acfa2be0f1d170981f547ca2a5..f3f9344aa2893cce478123b95d079c53a7b35fe1 100644 (file)
@@ -27,6 +27,7 @@ import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
 import org.simantics.db.common.request.ObjectsWithType;
 import org.simantics.db.common.request.PossibleIndexRoot;
+import org.simantics.db.common.request.PossibleObjectWithType;
 import org.simantics.db.common.request.ResourceRead;
 import org.simantics.db.common.uri.UnescapedChildMapOfResource;
 import org.simantics.db.exception.DatabaseException;
@@ -47,6 +48,7 @@ import org.simantics.db.layer0.variable.Variable;
 import org.simantics.db.layer0.variable.VariableMap;
 import org.simantics.db.layer0.variable.VariableMapImpl;
 import org.simantics.db.layer0.variable.VariableNode;
+import org.simantics.db.service.CollectionSupport;
 import org.simantics.issues.common.IssueUtils;
 import org.simantics.layer0.Layer0;
 import org.simantics.scl.reflection.annotations.SCLValue;
@@ -66,14 +68,12 @@ import org.simantics.structural2.queries.ConnectionPointMapOfResource;
 import org.simantics.structural2.queries.PossibleConnectionPointInfo;
 import org.simantics.structural2.scl.CompileStructuralValueRequest;
 import org.simantics.structural2.scl.procedural.CompileProceduralComponentTypeRequest;
+import org.simantics.structural2.utils.StructuralUtils.StructuralComponentClass;
 import org.simantics.structural2.variables.Connection;
-import org.simantics.structural2.variables.ConnectionBrowser;
 import org.simantics.structural2.variables.StandardProceduralChildVariable;
-import org.simantics.structural2.variables.VariableConnectionPointDescriptor;
 import org.simantics.utils.datastructures.MapList;
 
 import gnu.trove.map.hash.THashMap;
-import gnu.trove.set.hash.THashSet;
 
 public class Functions {
        
@@ -153,39 +153,6 @@ public class Functions {
                
        };
        
-       static class ConnectionImpl implements Connection {
-
-               final private StandardGraphPropertyVariable connectionPoint;
-               
-               public ConnectionImpl(StandardGraphPropertyVariable connectionPoint) {
-                       this.connectionPoint = connectionPoint;
-               }
-               
-               @Override
-               public Collection<Variable> getConnectionPoints(ReadGraph graph, Resource relationType) throws DatabaseException {
-                   Set<Variable> result = new THashSet<Variable>();
-                   for(VariableConnectionPointDescriptor desc : ConnectionBrowser.flatten(graph, connectionPoint.parent, connectionPoint.property.predicate, relationType)) {
-                       result.add(desc.getVariable(graph));
-                   }
-                   return result;
-               }
-               
-               @Override
-               public Collection<String> getConnectionPointURIs(ReadGraph graph, Resource relationType) throws DatabaseException {
-                   Set<String> result = new THashSet<String>();
-                   for(VariableConnectionPointDescriptor desc : ConnectionBrowser.flatten(graph, connectionPoint.parent, connectionPoint.property.predicate, relationType)) {
-                       result.add(desc.getURI(graph));
-                   }
-                   return result;
-               }
-
-               @Override
-               public Collection<VariableConnectionPointDescriptor> getConnectionPointDescriptors(ReadGraph graph, Resource relationType) throws DatabaseException {
-                   return ConnectionBrowser.flatten(graph, connectionPoint.parent, connectionPoint.property.predicate, relationType);
-               }
-
-       }
-       
        @SCLValue(type="ValueAccessor")
        public static final ValueAccessor connectionValueAccessor = new ValueAccessor() {
 
@@ -213,7 +180,7 @@ public class Functions {
                @Override
                public Object getValue(ReadGraph graph, Variable context) throws DatabaseException {
                        StandardGraphPropertyVariable variable = (StandardGraphPropertyVariable)context; 
-                       return new ConnectionImpl(variable);
+                       return new ConnectionImpl(context.getParent(graph), variable.property.predicate);
                }
 
                @Override
@@ -233,10 +200,10 @@ public class Functions {
        
                public Variable getPossibleConnectionPointFromContext(ReadGraph graph, Variable variable, Resource context, String name) throws DatabaseException {
 
-                       Map<String, Resource> connectionPoints = graph.syncRequest(new ConnectionPointMapOfResource(graph, context), TransientCacheAsyncListener.<Map<String,Resource>>instance());
-                       Resource cp = connectionPoints.get(name);
+                       Map<String, PropertyInfo> connectionPoints = graph.syncRequest(new ConnectionPointMapOfResource(graph, context), TransientCacheAsyncListener.<Map<String,PropertyInfo>>instance());
+                       PropertyInfo cp = connectionPoints.get(name);
                        if(cp == null) return null;
-                       else return new StandardGraphPropertyVariable(graph, variable, cp);
+                       else return new StandardGraphPropertyVariable(graph, variable, cp.predicate);
                        
                }
                
@@ -244,16 +211,16 @@ public class Functions {
                        
                        if(graph.isImmutable(context)) {
 
-                               Map<String, Resource> cps = graph.syncRequest(new ConnectionPointMapOfResource(graph, context), TransientCacheAsyncListener.<Map<String,Resource>>instance());
+                               Map<String, PropertyInfo> cps = graph.syncRequest(new ConnectionPointMapOfResource(graph, context), TransientCacheAsyncListener.<Map<String,PropertyInfo>>instance());
                                if(cps.size() == 0) return map;
                                
                                if(map == null) map = new THashMap<String,Variable>(cps.size());
                                
-                               for(Map.Entry<String, Resource> entry : cps.entrySet()) {
+                               for(Map.Entry<String, PropertyInfo> entry : cps.entrySet()) {
                                        String name = entry.getKey();
-                                       Resource cp = entry.getValue();
-                                       if(needSynchronized && !graph.isInstanceOf(cp, STR.SynchronizedConnectionRelation)) continue;
-                                       map.put(name, new StandardGraphPropertyVariable(graph, variable, cp));
+                                       PropertyInfo cp = entry.getValue();
+                                       if(needSynchronized && !graph.isInstanceOf(cp.predicate, STR.SynchronizedConnectionRelation)) continue;
+                                       map.put(name, new StandardGraphPropertyVariable(graph, variable, cp.predicate));
                                }
                                
                                return map;
@@ -313,78 +280,30 @@ public class Functions {
                
        };
 
-       static class StructuralChildMapOfResource extends ResourceRead<Map<String, Resource>> {
+       static class StructuralRunContext extends ResourceRead<Resource> {
 
-               public StructuralChildMapOfResource(Resource resource) {
+               public StructuralRunContext(Resource resource) {
                        super(resource);
                }
 
                @Override
-               public Map<String, Resource> perform(ReadGraph graph) throws DatabaseException {
-                       StructuralResource2 STR = StructuralResource2.getInstance(graph);
-                       Resource type = graph.getPossibleType(resource, STR.Component);
-                       if(type != null) {
-                               Resource definition = graph.getPossibleObject(type, STR.IsDefinedBy);
-                               if(definition != null) {
-                                       Map<String, Resource> map = graph.syncRequest(new UnescapedChildMapOfResource(definition));
-                                       if (!map.isEmpty())
-                                               return map;
-                               } 
-                       }
-                       Map<String, Resource> directChildren = graph.syncRequest(new UnescapedChildMapOfResource(resource));
-                       return directChildren;
-               }
-
-       }
-
-    static class StructuralChildMapOfResourceT extends ResourceRead<Map<String, Resource>> {
-
-        public StructuralChildMapOfResourceT(Resource resource) {
-            super(resource);
-        }
-
-        @Override
-        public Map<String, Resource> perform(ReadGraph graph) throws DatabaseException {
-            StructuralResource2 STR = StructuralResource2.getInstance(graph);
-            Resource definition = graph.getPossibleObject(resource, STR.IsDefinedBy);
-            if(definition != null) {
-                Map<String, Resource> map = graph.syncRequest(new UnescapedChildMapOfResource(definition));
-                if (!map.isEmpty())
-                    return map;
-            } 
-            return Collections.emptyMap();
-        }
-
-    }
-
-       static class StructuralRunChildMapOfResource extends ResourceRead<Map<String, Resource>> {
-
-               public StructuralRunChildMapOfResource(Resource resource) {
-                       super(resource);
-               }
-
-               public Map<String, Resource> fromContext(ReadGraph graph, Resource context) throws DatabaseException {
-                       return graph.sync(new StructuralChildMapOfResource(context));
-               }
-               
-               @Override
-               public Map<String, Resource> perform(ReadGraph graph) throws DatabaseException {
+               public Resource perform(ReadGraph graph) throws DatabaseException {
 
                        Layer0 L0 = Layer0.getInstance(graph);
                        SimulationResource SIMU = SimulationResource.getInstance(graph);
                        Resource model = graph.sync(new PossibleIndexRoot(resource));
                        if(graph.isInstanceOf(model, L0.RVIContext)) {
-                               return fromContext(graph, model);
+                               return model;
                        }
                        Resource configuration = graph.getPossibleObject(model, SIMU.HasConfiguration);
                        if(configuration != null) {
                                if(graph.isInstanceOf(configuration, L0.RVIContext)) {
-                                       return fromContext(graph, configuration);
+                                       return configuration;
                                }
                        }
-                       
-                       return Collections.emptyMap();
-                       
+
+                       return null;
+
                }
                
        }
@@ -488,6 +407,143 @@ public class Functions {
             }
         }
         
+       public static class StructuralTypeOverrideMap extends ResourceRead<Map<Resource,Resource>> {
+
+               protected StructuralTypeOverrideMap(Resource composite) {
+                       super(composite);
+               }
+
+               @Override
+               public Map<Resource, Resource> perform(ReadGraph graph) throws DatabaseException {
+                       
+                       Layer0 L0 = Layer0.getInstance(graph);
+                       
+                       StructuralResource2 STR = StructuralResource2.getInstance(graph);
+                       
+                       CollectionSupport cs = graph.getService(CollectionSupport.class);
+                       
+                       Map<Resource,Resource> result = null;
+                       
+                       for(Resource override : graph.getObjects(resource, STR.HasTypeOverride)) {
+                               Resource original = graph.getSingleObject(override, STR.TypeOverride_HasOriginalType);
+                               Resource replacement = graph.getSingleObject(override, STR.TypeOverride_HasReplacementType);
+                               if(result == null) result = cs.createMap(Resource.class);
+                               result.put(original, replacement);
+                       }
+                       
+                       if(result == null) return Collections.emptyMap();
+                       
+                       return result;
+                       
+               }
+               
+       }
+       
+       public static class StructuralOverrideData {
+               @Override
+               public int hashCode() {
+                       final int prime = 31;
+                       int result = 1;
+                       result = prime * result + ((actualRepresents == null) ? 0 : actualRepresents.hashCode());
+                       result = prime * result + ((actualType == null) ? 0 : actualType.hashCode());
+                       result = prime * result + ((overrideType == null) ? 0 : overrideType.hashCode());
+                       return result;
+               }
+               @Override
+               public boolean equals(Object obj) {
+                       if (this == obj)
+                               return true;
+                       if (obj == null)
+                               return false;
+                       if (getClass() != obj.getClass())
+                               return false;
+                       StructuralOverrideData other = (StructuralOverrideData) obj;
+                       if (actualRepresents == null) {
+                               if (other.actualRepresents != null)
+                                       return false;
+                       } else if (!actualRepresents.equals(other.actualRepresents))
+                               return false;
+                       if (actualType == null) {
+                               if (other.actualType != null)
+                                       return false;
+                       } else if (!actualType.equals(other.actualType))
+                               return false;
+                       if (overrideType == null) {
+                               if (other.overrideType != null)
+                                       return false;
+                       } else if (!overrideType.equals(other.overrideType))
+                               return false;
+                       return true;
+               }
+               Resource actualRepresents;
+               Resource actualType;
+               Resource overrideType;
+               public StructuralOverrideData(Resource actualRepresents, Resource actualType, Resource overrideType) {
+                       this.actualRepresents = actualRepresents;
+                       this.actualType = actualType;
+                       this.overrideType = overrideType;
+               }
+
+               public static StructuralOverrideData compute(ReadGraph graph, Variable context) throws DatabaseException {
+                       return graph.syncRequest(new StructuralOverrideDataRequest(context));
+               }
+               
+               public Resource type() {
+                       if(overrideType != null)
+                               return overrideType;
+                       return actualType;
+               }
+
+               public Resource represents() {
+                       return actualRepresents;
+               }
+
+       }
+       
+       public static class StructuralOverrideDataRequest extends VariableRead<StructuralOverrideData> {
+
+               public StructuralOverrideDataRequest(Variable component) {
+                       super(component);
+               }
+
+               public StructuralOverrideData walk(ReadGraph graph, Variable component, Resource actualRepresents, Resource actualType) throws DatabaseException {
+               Resource represents = component.getPossibleRepresents(graph);
+               if(represents != null) {
+                       Layer0 L0 = Layer0.getInstance(graph);
+                       StructuralResource2 STR = StructuralResource2.getInstance(graph);
+                       Resource container = graph.syncRequest(new PossibleObjectWithType(represents, L0.PartOf, STR.Composite));
+                       if(container != null) {
+                       Map<Resource,Resource> overrides = graph.syncRequest(new StructuralTypeOverrideMap(container));
+                       Resource override = overrides.get(actualType);
+                       if(override != null) {
+                               return new StructuralOverrideData(actualRepresents, actualType, override);
+                       }
+                       }
+               }
+               Variable parent = component.getParent(graph);
+               if(parent == null) return new StructuralOverrideData(actualRepresents, actualType, null);
+               else return walk(graph, parent, represents, actualType);
+               }
+               
+               @Override
+               public StructuralOverrideData perform(ReadGraph graph) throws DatabaseException {
+
+               Resource represents = variable.getPossibleRepresents(graph);
+               if(represents == null) {
+                       String uri = variable.getPossiblePropertyValue(graph, "typeURI");
+                       if(uri != null) {
+                               Resource actualType = graph.syncRequest(new org.simantics.db.common.primitiverequest.Resource(uri), TransientCacheAsyncListener.<Resource>instance()); 
+                               return walk(graph, variable, null, actualType);
+                       }
+                       throw new DatabaseException("No type for " + variable.getURI(graph));
+               } else {
+                       return walk(graph, variable, represents, graph.getPossibleType(represents, Layer0.getInstance(graph).Entity));
+               }
+                       
+               }
+               
+       }
+
        
        @SCLValue(type = "VariableMap")
        public static VariableMap structuralChildDomainChildren = new VariableMapImpl() {
@@ -495,53 +551,63 @@ public class Functions {
                @Override
                public Variable getVariable(ReadGraph graph, Variable context, String name) throws DatabaseException {
                        
-               final Resource type = context.getPossibleType(graph);
-               if(type != null) {
-                       StructuralResource2 STR = StructuralResource2.getInstance(graph);
-                   if(graph.isInstanceOf(type, STR.ProceduralComponentType)) {
-                           Map<String,Variable> map = graph.syncRequest(new ProceduralSubstructureRequest(context),
-                               TransientCacheListener.<Map<String,Variable>>instance());
-                           if(map != null) return map.get(name);
-                   }
-               }
-
-                   Resource represents = context.getPossibleRepresents(graph);
-            if(represents == null) {
-                Map<String, Resource> children = graph.syncRequest(new StructuralChildMapOfResourceT(type));
+                       Resource type = context.getPossibleType(graph);
+                       if(type == null) return null;
+                       
+                       StructuralComponentClass clazz = StructuralComponentClass.get(graph, type);
+                       if(StructuralComponentClass.PROCEDURAL.equals(clazz)) {
+                   Map<String,Variable> map = graph.syncRequest(new ProceduralSubstructureRequest(context),
+                        TransientCacheListener.<Map<String,Variable>>instance());
+                   if(map != null) return map.get(name);
+                   return null;
+                       } else if (StructuralComponentClass.DEFINED.equals(clazz)) {
+                               StructuralResource2 STR = StructuralResource2.getInstance(graph);
+                               Resource def = graph.getSingleObject(type, STR.IsDefinedBy);
+                Map<String, Resource> children = graph.syncRequest(new UnescapedChildMapOfResource(def));
                 Resource child = children.get(name);
-                return All.getStandardChildDomainChildVariable(graph, context, child, name);
-            }
-            Map<String, Resource> children = graph.syncRequest(new StructuralChildMapOfResource(represents));
-            Resource child = children.get(name);
-            return All.getStandardChildDomainChildVariable(graph, context, child, name);
+                if(child == null) return null;
+                return StandardChildDomainChildren.getStandardChildDomainChildVariable(graph, context, child, name);
+                       } else {
+                               Resource represents = context.getPossibleRepresents(graph);
+                               if(represents == null) return null;
+                Map<String, Resource> children = graph.syncRequest(new UnescapedChildMapOfResource(represents));
+                Resource child = children.get(name);
+                return StandardChildDomainChildren.getStandardChildDomainChildVariable(graph, context, child, name);
+                       }
+
                }
 
                @Override
                public Map<String, Variable> getVariables(ReadGraph graph, Variable context, Map<String, Variable> map) throws DatabaseException {
                        
-               final Resource type = context.getPossibleType(graph);
-               if(type != null) {
-                       StructuralResource2 STR = StructuralResource2.getInstance(graph);
-                   if(graph.isInstanceOf(type, STR.ProceduralComponentType)) {
-                       Map<String,Variable> mapPrime = graph.syncRequest(new ProceduralSubstructureRequest(context),
-                                       TransientCacheListener.<Map<String,Variable>>instance());
-                       if(mapPrime != null) {
-                               if(map != null) {
-                                       map.putAll(mapPrime);
-                                       return map;
-                               }
-                               else
-                                       return mapPrime;
-                       }
-                   }
-               }
-            Resource represents = context.getPossibleRepresents(graph);
-            if(represents == null) {
-                Map<String, Resource> children = graph.syncRequest(new StructuralChildMapOfResourceT(type));
+                       Resource type = context.getPossibleType(graph);
+                       if(type == null) return null;
+                       
+                       StructuralComponentClass clazz = StructuralComponentClass.get(graph, type);
+                       if(StructuralComponentClass.PROCEDURAL.equals(clazz)) {
+               Map<String,Variable> mapPrime = graph.syncRequest(new ProceduralSubstructureRequest(context),
+                               TransientCacheListener.<Map<String,Variable>>instance());
+               if(mapPrime != null) {
+                       if(map != null) {
+                               map.putAll(mapPrime);
+                               return map;
+                       }
+                       else
+                               return mapPrime;
+               }
+               return map;
+                       } else if (StructuralComponentClass.DEFINED.equals(clazz)) {
+                               StructuralResource2 STR = StructuralResource2.getInstance(graph);
+                               Resource def = graph.getSingleObject(type, STR.IsDefinedBy);
+                Map<String, Resource> children = graph.syncRequest(new UnescapedChildMapOfResource(def));
                 return StandardChildDomainChildren.getStandardChildDomainChildVariables(graph, context, children, map);
-            }
-            Map<String, Resource> children = graph.syncRequest(new StructuralChildMapOfResource(represents));
-            return StandardChildDomainChildren.getStandardChildDomainChildVariables(graph, context, children, map);
+                       } else {
+                               Resource represents = context.getPossibleRepresents(graph);
+                               if(represents == null) return null;
+                Map<String, Resource> children = graph.syncRequest(new UnescapedChildMapOfResource(represents));
+                return StandardChildDomainChildren.getStandardChildDomainChildVariables(graph, context, children, map);
+                       }
+            
                }
                
        };
@@ -551,15 +617,18 @@ public class Functions {
        
                @Override
                public Variable getVariable(ReadGraph graph, Variable context, String name) throws DatabaseException {
-                   Map<String, Resource> children = graph.syncRequest(new StructuralRunChildMapOfResource(context.getRepresents(graph)));
+                       Resource ctx = graph.syncRequest(new StructuralRunContext(context.getRepresents(graph)));
+                       if(ctx == null) return null;
+                   Map<String, Resource> children = graph.syncRequest(new UnescapedChildMapOfResource(ctx));
                        Resource child = children.get(name);
             return StandardChildDomainChildren.getStandardChildDomainChildVariable(graph, context, child, name);
                }
 
                @Override
                public Map<String, Variable> getVariables(ReadGraph graph, Variable context, Map<String, Variable> map) throws DatabaseException {
-            StandardGraphChildVariable variable = (StandardGraphChildVariable)context;
-            Map<String,Resource> children = graph.syncRequest(new StructuralRunChildMapOfResource(variable.resource));
+                       Resource ctx = graph.syncRequest(new StructuralRunContext(context.getRepresents(graph)));
+                       if(ctx == null) return map;
+                   Map<String, Resource> children = graph.syncRequest(new UnescapedChildMapOfResource(ctx));
                    return StandardChildDomainChildren.getStandardChildDomainChildVariables(graph, context, children, map);
                }
                
@@ -765,38 +834,6 @@ public class Functions {
        
     }
     
-    static class DefinedUCInterfaceMap extends ResourceRead<Collection<InterfaceResolution>> {
-
-       public DefinedUCInterfaceMap(Resource resource) {
-               super(resource);
-       }
-
-       @Override
-       public Collection<InterfaceResolution> perform(ReadGraph graph)
-                       throws DatabaseException {
-
-               StructuralResource2 STR = StructuralResource2.getInstance(graph);
-               Resource definition = graph.getPossibleObject(resource, STR.IsDefinedBy);
-               if(definition != null) {
-                       Collection<InterfaceResolution> result = new ArrayList<InterfaceResolution>();
-                       Layer0 L0 = Layer0.getInstance(graph);
-                       for(Resource cp : graph.syncRequest(new ObjectsWithType(resource, L0.ConsistsOf, STR.ConnectionRelation))) {
-                                       String cpName = graph.getRelatedValue(cp, L0.HasName, Bindings.STRING);
-                               for(Resource conn : graph.getObjects(cp, STR.IsBoundBy)) {
-                                       Statement stm = graph.getPossibleStatement(conn, STR.Connects);
-                                       if(stm == null) continue;
-                                       Resource component = stm.getObject();
-                                       String componentName = graph.getRelatedValue(component, L0.HasName, Bindings.STRING);
-                                       result.add(new InterfaceResolution(cp, cpName, componentName, graph.getInverse(stm.getPredicate())));
-                               }
-                       }
-                       return result;
-               }
-               return null;
-       }
-
-    }
-    
     public static final Collection<InterfaceResolution> BUILTIN_STRUCTURAL_CPS = new ArrayList<InterfaceResolution>();
 
        @SCLValue(type = "ReadGraph -> Resource -> Variable -> a")
@@ -808,12 +845,12 @@ public class Functions {
         SCLContext sclContext = SCLContext.getCurrent();
         Object oldGraph = sclContext.get("graph");
         try {
-            Function1<Variable,Object> exp = graph.syncRequest(new CompileStructuralValueRequest(graph, context) {
+            Function1<Object,Object> exp = graph.syncRequest(new CompileStructuralValueRequest(graph, context) {
                 protected String getExpressionText(ReadGraph graph) throws DatabaseException {
                     return expression;
                 }
             },
-            TransientCacheListener.<Function1<Variable,Object>>instance());
+            TransientCacheListener.instance());
             sclContext.put("graph", graph);
             return exp.apply(context);
         } catch (DatabaseException e) {