]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.structural2/src/org/simantics/structural2/utils/StructuralUtils.java
StructuralUtils.isImmutable now takes L0.readOnly of input into account
[simantics/platform.git] / bundles / org.simantics.structural2 / src / org / simantics / structural2 / utils / StructuralUtils.java
index 9b9dc6cb67ec862501598cc4e10252e7d2c3469f..aac19a7f176b663c9297e9e3b820e86e44392992 100644 (file)
@@ -1,3 +1,14 @@
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
 package org.simantics.structural2.utils;
 
 import java.util.ArrayList;
@@ -7,7 +18,6 @@ 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;
@@ -24,6 +34,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 +51,25 @@ 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);
+            Set<Resource> types = graph.getTypes(componentType);
+            if(types.contains(STR.ProceduralComponentType))
+                return StructuralComponentClass.PROCEDURAL;
+            else if(graph.hasStatement(componentType, STR.IsDefinedBy))
+                return StructuralComponentClass.DEFINED;
+            else if(types.contains(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 +172,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 +199,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);
@@ -278,11 +308,13 @@ public class StructuralUtils {
     }
 
     public static boolean isImmutable(ReadGraph graph, Resource r) throws DatabaseException {
+        // Marking a resource L0.readOnly also makes it immutable
+        if (graph.isImmutable(r) || Layer0Utils.isMarkedReadOnly(graph, r))
+            return true;
         StructuralResource2 STR = StructuralResource2.getInstance(graph);
         Resource uc = graph.syncRequest(new PossibleTypedParent(r, STR.ComponentType));
-        return graph.isImmutable(r)
-                // Anything under a published or locked user component is published as well
-                || (uc != null && (Layer0Utils.isPublished(graph, uc)
+        return  // Anything under a published or locked user component is published as well
+                (uc != null && (Layer0Utils.isPublished(graph, uc)
                          || graph.hasStatement(uc, STR.ComponentType_Locked)))
                 // Anything under a published container (shared library) is published as well
                 || Layer0Utils.isContainerPublished(graph, r)
@@ -290,7 +322,27 @@ public class StructuralUtils {
     }
     
     public static List<Variable> structuralConnectionConnectionPoints(ReadGraph graph, Connection conn, Resource relationType) throws DatabaseException {
-       return new ArrayList<Variable>(conn.getConnectionPoints(graph, relationType));
+        return new ArrayList<Variable>(conn.getConnectionPoints(graph, relationType));
+    }
+    
+    public static Resource structuralTypeResource(ReadGraph graph, Variable component, Resource baseType) throws DatabaseException {
+        StructuralOverrideData od = StructuralOverrideData.compute(graph, component);
+        if (od != null)
+            return od.type();
+        return null;
+    }
+    
+    public static Resource getComponentType(ReadGraph graph, Variable configuration, Resource component) throws DatabaseException {
+        Variable componentVariable = configuration.browse(graph, component);
+        return componentVariable.getType(graph);
     }
 
+    public static Resource getPossibleComponentType(ReadGraph graph, Variable configuration, Resource component) throws DatabaseException {
+        Variable componentVariable = configuration.browsePossible(graph, component);
+        if(componentVariable == null) return null;
+        StructuralResource2 STR = StructuralResource2.getInstance(graph);
+        return componentVariable.getPossibleType(graph, STR.Component);
+    }
+
+
 }