]> 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 3181dd7c45d7ea2434eed586393a2d2c7127c5b2..97038c63548f6833ef115c30cbd6781a11156d55 100644 (file)
@@ -13,6 +13,7 @@ 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;
@@ -23,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;
@@ -39,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);
@@ -168,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);
@@ -290,5 +311,35 @@ public class StructuralUtils {
     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);
+       
+    }
+    
+
 
 }