]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.structural2/src/org/simantics/structural2/utils/StructuralUtils.java
Work in progress
[simantics/platform.git] / bundles / org.simantics.structural2 / src / org / simantics / structural2 / utils / StructuralUtils.java
index 9b9dc6cb67ec862501598cc4e10252e7d2c3469f..97038c63548f6833ef115c30cbd6781a11156d55 100644 (file)
@@ -7,13 +7,13 @@ import java.util.List;
 import java.util.Set;
 
 import org.simantics.databoard.Bindings;
-import org.simantics.datatypes.literal.GUID;
 import org.simantics.db.ReadGraph;
 import org.simantics.db.RequestProcessor;
 import org.simantics.db.Resource;
 import org.simantics.db.Statement;
 import org.simantics.db.WriteGraph;
 import org.simantics.db.common.CommentMetadata;
+import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
 import org.simantics.db.common.request.ObjectsWithType;
 import org.simantics.db.common.request.PossibleTypedParent;
 import org.simantics.db.common.utils.NameUtils;
@@ -24,6 +24,7 @@ import org.simantics.db.layer0.util.Layer0Utils;
 import org.simantics.db.layer0.variable.Variable;
 import org.simantics.layer0.Layer0;
 import org.simantics.structural.stubs.StructuralResource2;
+import org.simantics.structural2.Functions.StructuralOverrideData;
 import org.simantics.structural2.internal.queries.ConnectedTo;
 import org.simantics.structural2.internal.queries.RelatedConnections;
 import org.simantics.structural2.internal.queries.RelatedConnectionsOfConnectionJoin;
@@ -40,6 +41,24 @@ import gnu.trove.set.hash.THashSet;
  */
 public class StructuralUtils {
     
+       public static enum StructuralComponentClass {
+               
+               PRIMITIVE,REPLACEABLE,DEFINED,PROCEDURAL;
+
+               public static StructuralComponentClass get(ReadGraph graph, Resource componentType) throws DatabaseException {
+                       StructuralResource2 STR = StructuralResource2.getInstance(graph);
+                       if(graph.isInstanceOf(componentType, STR.ProceduralComponentType))
+                               return StructuralComponentClass.PROCEDURAL;
+                       else if(graph.hasStatement(componentType, STR.IsDefinedBy))
+                               return StructuralComponentClass.DEFINED;
+                       else if(graph.isInstanceOf(componentType, STR.ReplaceableDefinedComponentType))
+                               return StructuralComponentClass.REPLACEABLE;
+                       else
+                               return StructuralComponentClass.PRIMITIVE;
+               }
+
+       }
+       
     public static Collection<Resource> getConnectionRelations(ReadGraph graph, Resource componentType) throws DatabaseException {
         Layer0 L0 = Layer0.getInstance(graph);
         StructuralResource2 STR = StructuralResource2.getInstance(graph);
@@ -142,8 +161,7 @@ public class StructuralUtils {
         g.claim(component, L0.HasName, Layer0Utils.literal(g, name));
         g.claim(component, L0.HasLabel, Layer0Utils.literal(g, ""));
         g.claim(parent, L0.ConsistsOf, component);
-        // Add identifier
-        Layer0Utils.addL0Identifier(g, component);
+        Layer0Utils.claimNewIdentifier(g, component, true);
         // Add comment to change set.
         CommentMetadata cm = g.getMetadata(CommentMetadata.class);
         g.addMetadata(cm.add("Created component " + component));
@@ -170,6 +188,7 @@ public class StructuralUtils {
      * Returns the component type of the given component or null if the 
      * parameter is not a component.
      */
+    @Deprecated
     public static Resource getComponentType(ReadGraph g, Resource component) throws DatabaseException {
         StructuralResource2 STR = StructuralResource2.getInstance(g);
         return g.getPossibleType(component, STR.Component);
@@ -267,7 +286,7 @@ public class StructuralUtils {
        if(property == null) return null;
        Connection conn = property.getPossibleValue(graph);
        if(conn == null) return null;
-       Collection<Variable> cps = conn.getConnectionPoints(graph, null);
+       Collection<Variable> cps = conn.getConnectionPoints(graph, component, null);
        if(cps.size() == 2) {
                for(Variable var : cps) {
                        if(property.equals(var)) continue;
@@ -289,8 +308,38 @@ public class StructuralUtils {
                 ;
     }
     
-    public static List<Variable> structuralConnectionConnectionPoints(ReadGraph graph, Connection conn, Resource relationType) throws DatabaseException {
-       return new ArrayList<Variable>(conn.getConnectionPoints(graph, relationType));
+    public static List<Variable> structuralConnectionConnectionPoints(ReadGraph graph, Variable component, Connection conn, Resource relationType) throws DatabaseException {
+       return new ArrayList<Variable>(conn.getConnectionPoints(graph, component, relationType));
+    }
+    
+    public static Resource structuralTypeResource(ReadGraph graph, Variable component, Resource baseType) throws DatabaseException {
+
+       if(component.getURI(graph).endsWith("/Alternative/Alternative/Panel2"))
+               System.err.println("structuralTypeResource " + component.getURI(graph));
+       
+       StructuralOverrideData od = StructuralOverrideData.compute(graph, component);
+       return od.type();
+       
+//     Resource represents = component.getPossibleRepresents(graph);
+//     if(represents == null) {
+//             String uri = component.getPossiblePropertyValue(graph, "typeURI");
+//             if(uri != null) return graph.syncRequest(new org.simantics.db.common.primitiverequest.Resource(uri), TransientCacheAsyncListener.<Resource>instance());
+//             throw new DatabaseException("No type for " + component.getURI(graph));
+//     }
+//     return graph.getPossibleType(represents, baseType);
+//     
+//     Pair<Resource,Resource> representsAndType = graph.syncRequest(new PossibleRepresentsAndTypeWithOverrides(component));
+//     return representsAndType.second;
+       
+    }
+    
+    public static Resource getComponentType(ReadGraph graph, Variable configuration, Resource component) throws DatabaseException {
+
+       Variable componentVariable = configuration.browse(graph, component);
+       return componentVariable.getType(graph);
+       
     }
+    
+
 
 }