]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Do not compute replaceable type for non ReplaceableDefinedComponentTypes 64/2564/2
authorjsimomaa <jani.simomaa@gmail.com>
Tue, 18 Dec 2018 13:26:44 +0000 (15:26 +0200)
committerTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Tue, 18 Dec 2018 14:04:07 +0000 (14:04 +0000)
gitlab #240

Change-Id: Ia982e16172698e855eac2845be0304363c9987f7

bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/variable/AbstractVariable.java
bundles/org.simantics.structural2/src/org/simantics/structural2/Functions.java
bundles/org.simantics.structural2/src/org/simantics/structural2/utils/StructuralUtils.java

index 00b01cca6031f967c315f37ed2d2c147f3a20c62..110ed2f777471aeef7e15128baa2381f3964d824 100644 (file)
@@ -966,7 +966,7 @@ public abstract class AbstractVariable implements Variable {
             Resource typeFromFunction = getTypeFromPossibleTypeFunction(graph, Layer0.getInstance(graph).Entity, getPossibleTypeFunction(graph));
             if (typeFromFunction != null)
                 return typeFromFunction;
-        } catch (Throwable t) {
+        } catch (DatabaseException t) {
             return null;
         }
 
@@ -1002,7 +1002,7 @@ public abstract class AbstractVariable implements Variable {
             Resource typeFromFunction = getTypeFromPossibleTypeFunction(graph, baseType, getPossibleTypeFunction(graph));
             if (typeFromFunction != null)
                 return typeFromFunction;
-        } catch (Throwable t) {
+        } catch (DatabaseException t) {
             return null;
         }
 
index f3f9344aa2893cce478123b95d079c53a7b35fe1..9b5fc4586a9281d16751d002c6103e3047e78bd9 100644 (file)
@@ -23,12 +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.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.request.PossibleObjectWithType;
 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;
@@ -54,6 +56,7 @@ 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;
@@ -499,31 +502,48 @@ public class Functions {
                }
 
        }
-       
+
+    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);
                }
-
-               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 {
@@ -532,45 +552,75 @@ public class Functions {
                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);
+                               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 {
-                       return walk(graph, variable, represents, graph.getPossibleType(represents, Layer0.getInstance(graph).Entity));
+                   Resource possibleType = graph.getPossibleType(represents, Layer0.getInstance(graph).Entity);
+                   if (graph.syncRequest(new IsInstanceOf(possibleType, StructuralResource2.getInstance(graph).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() {
-       
+
                @Override
                public Variable getVariable(ReadGraph graph, Variable context, String name) throws DatabaseException {
                        
                        Resource type = context.getPossibleType(graph);
                        if(type == null) return null;
+                   
+                       Tuple2 result = graph.syncRequest(new StructureTypeAndChildMapRequest(type), TransientCacheListener.instance());
+                       StructuralComponentClass clazz = (StructuralComponentClass) result.c0;
                        
-                       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));
+                           Map<String, Resource> children = (Map<String, Resource>) result.c1;
                 Resource child = children.get(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));
+                Map<String, Resource> children = graph.syncRequest(new UnescapedChildMapOfResource(represents), TransientCacheListener.instance());
                 Resource child = children.get(name);
                 return StandardChildDomainChildren.getStandardChildDomainChildVariable(graph, context, child, name);
                        }
@@ -599,7 +649,7 @@ public class Functions {
                        } 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));
+                Map<String, Resource> children = graph.syncRequest(new UnescapedChildMapOfResource(def), TransientCacheListener.instance());
                 return StandardChildDomainChildren.getStandardChildDomainChildVariables(graph, context, children, map);
                        } else {
                                Resource represents = context.getPossibleRepresents(graph);
@@ -812,7 +862,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;
 
                }
index c5b0087e237def41d9a962f5dac5702bbc501cdf..e27d92f29219a7f54963208286b5bd1936c0f485 100644 (file)
@@ -325,7 +325,9 @@ public class StructuralUtils {
     
     public static Resource structuralTypeResource(ReadGraph graph, Variable component, Resource baseType) throws DatabaseException {
         StructuralOverrideData od = StructuralOverrideData.compute(graph, component);
-        return od.type();
+        if (od != null)
+            return od.type();
+        return null;
     }
     
     public static Resource getComponentType(ReadGraph graph, Variable configuration, Resource component) throws DatabaseException {