]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.structural2/src/org/simantics/structural2/variables/ConnectionBrowser.java
Work in progress
[simantics/platform.git] / bundles / org.simantics.structural2 / src / org / simantics / structural2 / variables / ConnectionBrowser.java
index ac1dceceb3ddfce6739ece5c01909ebc7c618c7a..2cc08eb3c70f643feee67b27b0d0d207caf9c962 100644 (file)
@@ -1,8 +1,5 @@
 package org.simantics.structural2.variables;
 
-import gnu.trove.map.hash.THashMap;
-import gnu.trove.set.hash.THashSet;
-
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
@@ -36,9 +33,14 @@ import org.simantics.structural.stubs.StructuralResource2;
 import org.simantics.structural2.Functions;
 import org.simantics.structural2.Functions.InterfaceResolution;
 import org.simantics.structural2.queries.ConnectionSet;
+import org.simantics.structural2.utils.StructuralUtils;
+import org.simantics.structural2.utils.StructuralUtils.StructuralComponentClass;
 import org.simantics.structural2.variables.StandardProceduralChildVariable.FixedConnection;
 import org.simantics.utils.datastructures.Pair;
 
+import gnu.trove.map.hash.THashMap;
+import gnu.trove.set.hash.THashSet;
+
 public class ConnectionBrowser {
 
     /**
@@ -79,7 +81,10 @@ public class ConnectionBrowser {
                  Resource relation = graph.getInverse(stat.getPredicate());
                  //System.out.println(NameUtils.getSafeName(graph, component) + "." + NameUtils.getSafeName(graph, relation));
                  Resource boundConnection = graph.getPossibleObject(relation, STR.IsBoundBy);
-                 Resource type = graph.getPossibleObject(component, L0.InstanceOf);
+                 Resource type = StructuralUtils.getComponentType(graph, configuration, component);
+                 //
+                 //Resource type = StructuralUtils.getComponentType(graph, configuration, component);
+                 //Resource type = newContext != null ? newContext.getPossibleType(graph) : graph.getPossibleObject(component, L0.InstanceOf); 
                  Resource def = type != null ? graph.getPossibleObject(type, STR.IsDefinedBy) : null;
                  if(boundConnection != null && def != null) {
                      // The connection point is bound in component type
@@ -356,7 +361,13 @@ public class ConnectionBrowser {
                        HashSet<VariableConnectionPointDescriptor> result = new HashSet<VariableConnectionPointDescriptor>();
                        for(Resource c : conns) {
                                List<Resource> rs = graph.syncRequest(new ConnectionComponentsWithAncestor(graph, c), TransientCacheAsyncListener.<List<Resource>>instance()); 
+                       for(Resource r : rs) {
+                               System.err.println("--connections " + NameUtils.getSafeName(graph, r));
+                       }
                                result.addAll(graph.syncRequest(ConnectionVariables.forStructural(graph, curConfiguration, rs)));
+                               for(VariableConnectionPointDescriptor desc2 : result) {
+                                       reportDescriptor(graph, desc2);
+                               }
                        }
                        return result;
                        
@@ -365,6 +376,9 @@ public class ConnectionBrowser {
                Resource connection = graph.getPossibleObject(represents, res);
                if(connection != null) {
                        List<Resource> rs = graph.syncRequest(new ConnectionComponentsWithAncestor(graph, connection), TransientCacheAsyncListener.<List<Resource>>instance());
+                       for(Resource r : rs) {
+                               System.err.println("--connections " + NameUtils.getSafeName(graph, r));
+                       }
                        return graph.syncRequest(ConnectionVariables.forConfiguration(graph, curConfiguration, rs));
                }
                else {
@@ -383,6 +397,21 @@ public class ConnectionBrowser {
         
     }
     
+    public static void reportDescriptor(ReadGraph graph, VariableConnectionPointDescriptor d) throws DatabaseException {
+
+       if(d instanceof ActualConnectionDescriptor) {
+               ActualConnectionDescriptor d2 = (ActualConnectionDescriptor)d; 
+               
+               System.err.println("--ActualConnectionPointDescriptor2");
+               System.err.println("---root: " + d2.root.getURI(graph));
+               System.err.println("---component: " + graph.getPossibleURI(d2.component));
+               System.err.println("---type: " + graph.getPossibleURI(d2.componentType));
+               System.err.println("---cp: " + graph.getPossibleURI(d2.cp));
+               System.err.println("---var: " + d2.getVariable(graph).getURI(graph));
+       }
+       
+    }
+    
     public static class ConnectionVariables extends BinaryRead<Variable, List<Resource>, Collection<VariableConnectionPointDescriptor>> {
 
        private ConnectionVariables(Variable parameter1, List<Resource> parameter2) {
@@ -427,15 +456,25 @@ public class ConnectionBrowser {
                @Override
                public Collection<VariableConnectionPointDescriptor> perform(ReadGraph graph) throws DatabaseException {
                        if(parameter == null) return Collections.emptyList();
+                       System.err.println("ConnectionVariables " + parameter.getURI(graph));
                        StructuralResource2 STR = StructuralResource2.getInstance(graph);
             ArrayList<VariableConnectionPointDescriptor> result = null;
             for(int i=1;i<parameter2.size();i++) {
                Resource connRes = parameter2.get(i);
                for(Statement stm : graph.getStatements(connRes, STR.Connects)) {
+                       Layer0 L0 = Layer0.getInstance(graph);
                        Resource component = stm.getObject();
                        Resource connectionPoint = graph.getInverse(stm.getPredicate());
                        if(result == null) result = new ArrayList<VariableConnectionPointDescriptor>();
-                       result.add(new ActualConnectionDescriptor(parameter, component, connectionPoint));
+                       String componentName = graph.getRelatedValue(component, L0.HasName, Bindings.STRING);
+                       if(parameter.getURI(graph).endsWith("SelectionView/Instance") && componentName.equals("Explorer1"))
+                               System.err.println("asd");
+                       Variable possibleChild = parameter.getPossibleChild(graph, componentName);
+                       if(possibleChild != null) {
+                               result.add(new ActualConnectionDescriptor(parameter, component, possibleChild.getType(graph), connectionPoint));
+                       } else {
+                               throw new DatabaseException("No child with name " + componentName + " could be resolved for variable " + parameter.getURI(graph));
+                       }
                }
             }
             if(result == null) return Collections.emptyList();
@@ -453,12 +492,8 @@ public class ConnectionBrowser {
                @Override
                public Boolean perform(ReadGraph graph) throws DatabaseException {
                        
-                       StructuralResource2 STR = StructuralResource2.getInstance(graph);
-
-                       if(graph.isInstanceOf(resource, STR.ProceduralComponentType)) return false;
-                       if(graph.hasStatement(resource, STR.IsDefinedBy)) return false;
-                       
-                       return true;
+                       StructuralComponentClass clazz = StructuralComponentClass.get(graph, resource);
+                       return StructuralComponentClass.PRIMITIVE.equals(clazz);
 
                }
        
@@ -591,28 +626,52 @@ public class ConnectionBrowser {
 
        public static Collection<VariableConnectionPointDescriptor> doFlatten(ReadGraph graph, Variable child, Resource cp, Resource relationType) throws DatabaseException {
        
-        Collection<VariableConnectionPointDescriptor> climbed = climb(graph, child, cp, null);
-        boolean needDrill = false;
+        Set<VariableConnectionPointDescriptor> result = null;
+        Set<VariableConnectionPointDescriptor> needDrill = null;
+
+       String debug = child.getURI(graph) + " " + NameUtils.getSafeLabel(graph, cp);
+               System.err.println("doFlatten " + debug);
+       
+       if(debug.endsWith("01/TreeTable01 parent"))
+               System.err.println("asd");
+        
+               Collection<VariableConnectionPointDescriptor> climbed = climb(graph, child, cp, null);
         for(VariableConnectionPointDescriptor desc : climbed) {
                if(!desc.isLeaf(graph)) {
-                       needDrill = true;
-                       break;
+                       if(needDrill == null)
+                               needDrill = new THashSet<VariableConnectionPointDescriptor>(climbed.size());
+                       needDrill.add(desc);
+               } else {
+                       if(result == null)
+                               result = new THashSet<VariableConnectionPointDescriptor>(climbed.size());
+                       result.add(desc);
                }
         }
         
-        if(!needDrill) {
+        if(needDrill == null) {
+               /*
+                * All descriptors were already flat - just take case of filtering
+                */
             if(relationType != null) {
                 ArrayList<VariableConnectionPointDescriptor> filtered = new ArrayList<VariableConnectionPointDescriptor>(climbed.size());
                 for(VariableConnectionPointDescriptor desc : climbed)
                     if(filterByRelationType(graph, desc, relationType))
                         filtered.add(desc);
                 return filtered;
+            } else {
+                return climbed;
             }
-            return climbed;
         }
         
-        THashSet<VariableConnectionPointDescriptor> result = new THashSet<VariableConnectionPointDescriptor>(climbed.size());
-        for(VariableConnectionPointDescriptor top : climbed) {
+
+        /*
+         * There were some descriptors that require drill
+         */
+        for(VariableConnectionPointDescriptor top : needDrill) {
+               
+               if(debug.endsWith("01/TreeTable01 parent"))
+                       System.err.println("asd");
+
                Collection<VariableConnectionPointDescriptor> drilled = drill(graph, top);
             if(drilled != null) {
                for(VariableConnectionPointDescriptor drill : drilled) {
@@ -620,10 +679,22 @@ public class ConnectionBrowser {
                                if(!filterByRelationType(graph, drill, relationType))
                                        continue;
                        }
+                       if(result == null)
+                               result = new THashSet<VariableConnectionPointDescriptor>(climbed.size());
                        result.add(drill);
                }
             }
         }
+
+               System.err.println("doFlatten finished: " + debug);
+
+       if(debug.endsWith("01/TreeTable01 parent"))
+               System.err.println("asd");
+
+               for(VariableConnectionPointDescriptor desc2 : result) {
+                       reportDescriptor(graph, desc2);
+               }
+        
         return result;
         
     }