]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.structural2/src/org/simantics/structural2/Functions.java
Avoid duplicate evaluation of procedural component type requests
[simantics/platform.git] / bundles / org.simantics.structural2 / src / org / simantics / structural2 / Functions.java
index d9d8857af77210acfa2be0f1d170981f547ca2a5..8bf4e3df68b6ac62dc30b2575ddb3eb75b1b9bf2 100644 (file)
@@ -23,11 +23,14 @@ import org.simantics.db.Resource;
 import org.simantics.db.Statement;
 import org.simantics.db.WriteGraph;
 import org.simantics.db.common.issue.StandardIssue;
 import org.simantics.db.Statement;
 import org.simantics.db.WriteGraph;
 import org.simantics.db.common.issue.StandardIssue;
+import org.simantics.db.common.primitiverequest.IsInstanceOf;
 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.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.request.ResourceRead;
+import org.simantics.db.common.request.TernaryRead;
 import org.simantics.db.common.uri.UnescapedChildMapOfResource;
 import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.layer0.function.All;
 import org.simantics.db.common.uri.UnescapedChildMapOfResource;
 import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.layer0.function.All;
@@ -47,11 +50,14 @@ 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.layer0.variable.VariableMap;
 import org.simantics.db.layer0.variable.VariableMapImpl;
 import org.simantics.db.layer0.variable.VariableNode;
+import org.simantics.db.layer0.variable.Variables;
+import org.simantics.db.service.CollectionSupport;
 import org.simantics.issues.common.IssueUtils;
 import org.simantics.layer0.Layer0;
 import org.simantics.scl.reflection.annotations.SCLValue;
 import org.simantics.scl.runtime.SCLContext;
 import org.simantics.scl.runtime.function.Function1;
 import org.simantics.issues.common.IssueUtils;
 import org.simantics.layer0.Layer0;
 import org.simantics.scl.reflection.annotations.SCLValue;
 import org.simantics.scl.runtime.SCLContext;
 import org.simantics.scl.runtime.function.Function1;
+import org.simantics.scl.runtime.tuple.Tuple2;
 import org.simantics.simulation.ontology.SimulationResource;
 import org.simantics.simulator.variable.NodeManager;
 import org.simantics.structural.stubs.StructuralResource2;
 import org.simantics.simulation.ontology.SimulationResource;
 import org.simantics.simulator.variable.NodeManager;
 import org.simantics.structural.stubs.StructuralResource2;
@@ -66,14 +72,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.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.Connection;
-import org.simantics.structural2.variables.ConnectionBrowser;
 import org.simantics.structural2.variables.StandardProceduralChildVariable;
 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 org.simantics.utils.datastructures.MapList;
 
 import gnu.trove.map.hash.THashMap;
-import gnu.trove.set.hash.THashSet;
 
 public class Functions {
        
 
 public class Functions {
        
@@ -153,39 +157,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() {
 
        @SCLValue(type="ValueAccessor")
        public static final ValueAccessor connectionValueAccessor = new ValueAccessor() {
 
@@ -213,7 +184,7 @@ public class Functions {
                @Override
                public Object getValue(ReadGraph graph, Variable context) throws DatabaseException {
                        StandardGraphPropertyVariable variable = (StandardGraphPropertyVariable)context; 
                @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
                }
 
                @Override
@@ -233,10 +204,10 @@ public class Functions {
        
                public Variable getPossibleConnectionPointFromContext(ReadGraph graph, Variable variable, Resource context, String name) throws DatabaseException {
 
        
                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;
                        if(cp == null) return null;
-                       else return new StandardGraphPropertyVariable(graph, variable, cp);
+                       else return new StandardGraphPropertyVariable(graph, variable, cp.predicate);
                        
                }
                
                        
                }
                
@@ -244,16 +215,16 @@ public class Functions {
                        
                        if(graph.isImmutable(context)) {
 
                        
                        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());
                                
                                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();
                                        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;
                                }
                                
                                return map;
@@ -313,78 +284,30 @@ public class Functions {
                
        };
 
                
        };
 
-       static class StructuralChildMapOfResource extends ResourceRead<Map<String, Resource>> {
-
-               public StructuralChildMapOfResource(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>> {
+       static class StructuralRunContext extends ResourceRead<Resource> {
 
 
-               public StructuralRunChildMapOfResource(Resource resource) {
+               public StructuralRunContext(Resource resource) {
                        super(resource);
                }
 
                        super(resource);
                }
 
-               public Map<String, Resource> fromContext(ReadGraph graph, Resource context) throws DatabaseException {
-                       return graph.sync(new StructuralChildMapOfResource(context));
-               }
-               
                @Override
                @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)) {
 
                        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)) {
                        }
                        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;
+
                }
                
        }
                }
                
        }
@@ -410,10 +333,11 @@ public class Functions {
 
     public static List<SubstructureElement> getProceduralDesc(ReadGraph graph, final Variable context) throws DatabaseException {
         StructuralResource2 STR = StructuralResource2.getInstance(graph);
 
     public static List<SubstructureElement> getProceduralDesc(ReadGraph graph, final Variable context) throws DatabaseException {
         StructuralResource2 STR = StructuralResource2.getInstance(graph);
-        final Resource type = context.getPossibleType(graph);
+        Variable config = Variables.getConfigurationVariable(graph, context);
+        final Resource type = config.getPossibleType(graph);
         if(type != null) {
             if(graph.isInstanceOf(type, STR.ProceduralComponentType)) {
         if(type != null) {
             if(graph.isInstanceOf(type, STR.ProceduralComponentType)) {
-                return graph.syncRequest(new SubstructureRequest(context));
+                return graph.syncRequest(new SubstructureRequest(config));
             }
         }
         return null;
             }
         }
         return null;
@@ -488,60 +412,260 @@ 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;
+               }
+
+       }
+
+    private static class StructuralOverrideDataWalkRequest
+            extends TernaryRead<Variable, Resource, Resource, StructuralOverrideData> {
+
+        public StructuralOverrideDataWalkRequest(Variable component, Resource actualRepresents, Resource actualType) {
+            super(component, actualRepresents, actualType);
+        }
+
+        @Override
+        public StructuralOverrideData perform(ReadGraph graph) throws DatabaseException {
+            Variable component = parameter;
+            Resource actualRepresents = parameter2;
+            Resource actualType = parameter3;
+//            System.err.println(component.getURI(graph));
+            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), TransientCacheListener.instance());
+                    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 graph.syncRequest(new StructuralOverrideDataWalkRequest(parent, represents, actualType), TransientCacheListener.instance());
+        }
+
+    }
+
+       public static class StructuralOverrideDataRequest extends VariableRead<StructuralOverrideData> {
+
+               public StructuralOverrideDataRequest(Variable component) {
+                       super(component);
+               }
+               
+               @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());
+                               if (graph.syncRequest(new IsInstanceOf(actualType, StructuralResource2.getInstance(graph).ReplaceableDefinedComponentType), TransientCacheListener.instance()) ) {
+                                   return graph.syncRequest(new StructuralOverrideDataWalkRequest(variable, null, actualType), TransientCacheListener.instance());
+                               } else {
+                                   // can not have replaceable type 
+                                   return null;
+                               }
+                       }
+                       throw new DatabaseException("No type for " + variable.getURI(graph));
+               } else {
+                       StructuralResource2 STR = StructuralResource2.getInstance(graph);
+                   Resource possibleType = graph.getPossibleType(represents, STR.Component);
+                   if (possibleType == null) {
+                       possibleType = graph.getPossibleType(represents, Layer0.getInstance(graph).Entity);
+                       if (possibleType == null)
+                               return null;
+                   }
+                   if (graph.syncRequest(new IsInstanceOf(possibleType, STR.ReplaceableDefinedComponentType), TransientCacheListener.instance()) ) {
+                       return graph.syncRequest(new StructuralOverrideDataWalkRequest(variable, represents, possibleType), TransientCacheListener.instance());
+                   } else {
+                       return null;
+                   }
+               }
+                       
+               }
+               
+       }
+
+
+    private static class StructureTypeAndChildMapRequest extends ResourceRead<Tuple2> {
+
+        protected StructureTypeAndChildMapRequest(Resource resource) {
+            super(resource);
+        }
+
+        @Override
+        public Tuple2 perform(ReadGraph graph) throws DatabaseException {
+            StructuralComponentClass clazz = StructuralComponentClass.get(graph, resource);
+            if (StructuralComponentClass.DEFINED.equals(clazz)) {
+                StructuralResource2 STR = StructuralResource2.getInstance(graph);
+                Resource def = graph.getSingleObject(resource, STR.IsDefinedBy);
+                Map<String, Resource> children = graph.syncRequest(new UnescapedChildMapOfResource(def), TransientCacheListener.instance());
+                return new Tuple2(clazz, children);
+            }
+            return new Tuple2(clazz, null);
+        }
+        
+    }
+
        @SCLValue(type = "VariableMap")
        public static VariableMap structuralChildDomainChildren = new VariableMapImpl() {
        @SCLValue(type = "VariableMap")
        public static VariableMap structuralChildDomainChildren = new VariableMapImpl() {
-       
+
                @Override
                public Variable getVariable(ReadGraph graph, Variable context, String name) throws DatabaseException {
                @Override
                public Variable getVariable(ReadGraph graph, Variable context, String name) throws DatabaseException {
+                       StructuralResource2 STR = StructuralResource2.getInstance(graph);
+                       Resource type = context.getPossibleType(graph, STR.Component);
+                       if(type == null) return null;
+                   
+                       Tuple2 result = graph.syncRequest(new StructureTypeAndChildMapRequest(type), TransientCacheListener.instance());
+                       StructuralComponentClass clazz = (StructuralComponentClass) result.c0;
                        
                        
-               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));
+                       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)) {
+                           Map<String, Resource> children = (Map<String, Resource>) result.c1;
                 Resource child = children.get(name);
                 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), TransientCacheListener.instance());
+                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 {
                        
                }
 
                @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), TransientCacheListener.instance());
                 return StandardChildDomainChildren.getStandardChildDomainChildVariables(graph, context, children, map);
                 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 +675,18 @@ public class Functions {
        
                @Override
                public Variable getVariable(ReadGraph graph, Variable context, String name) throws DatabaseException {
        
                @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 {
                        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);
                }
                
                    return StandardChildDomainChildren.getStandardChildDomainChildVariables(graph, context, children, map);
                }
                
@@ -624,7 +751,7 @@ public class Functions {
 
                for(Resource req : requiredConnections) {
                        if(!connections.contains(req)) {
 
                for(Resource req : requiredConnections) {
                        if(!connections.contains(req)) {
-                               result.add(new StandardIssue(sr.ConnectionConstraint_ErrorIssue, component, req));
+                               result.add(new StandardIssue(sr.ConnectionValidationConstraint_ErrorIssue, component, req));
                        }
                }
 
                        }
                }
 
@@ -743,7 +870,7 @@ public class Functions {
                                
                        }
 
                                
                        }
 
-                       final Collection<InterfaceResolution> interfaces = graph.syncRequest(new DefinedUCInterfaceMap(type));
+                       final Collection<InterfaceResolution> interfaces = graph.syncRequest(new DefinedUCInterfaceMap(type), TransientCacheListener.instance());
                        if(interfaces != null) return interfaces;
 
                }
                        if(interfaces != null) return interfaces;
 
                }
@@ -765,38 +892,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")
     public static final Collection<InterfaceResolution> BUILTIN_STRUCTURAL_CPS = new ArrayList<InterfaceResolution>();
 
        @SCLValue(type = "ReadGraph -> Resource -> Variable -> a")
@@ -808,12 +903,12 @@ public class Functions {
         SCLContext sclContext = SCLContext.getCurrent();
         Object oldGraph = sclContext.get("graph");
         try {
         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;
                 }
             },
                 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) {
             sclContext.put("graph", graph);
             return exp.apply(context);
         } catch (DatabaseException e) {