]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.modeling/src/org/simantics/modeling/ComponentUtils.java
Add utility class org.simantics.modeling.help.HelpContexts
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / ComponentUtils.java
index c34efd3ab675d43773c631d80dd9cd255d8aff2e..01a3d3da32b53c957b6988198f5757607c77edc4 100644 (file)
-/*******************************************************************************\r
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- *     VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.modeling;\r
-\r
-import java.util.ArrayList;\r
-import java.util.Collection;\r
-import java.util.Collections;\r
-import java.util.LinkedList;\r
-import java.util.List;\r
-\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.common.ResourceArray;\r
-import org.simantics.db.common.utils.NameUtils;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.layer0.Layer0;\r
-import org.simantics.simulation.ontology.SimulationResource;\r
-import org.simantics.structural.stubs.StructuralResource2;\r
-\r
-/**\r
- * @author Tuukka Lehtonen\r
- */\r
-public final class ComponentUtils {\r
-\r
-    public static boolean componentHasDiagram(ReadGraph g, Resource component) throws DatabaseException {\r
-        return !getComponentDiagrams(g, component).isEmpty();\r
-    }\r
-\r
-    public static boolean compositeHasDiagram(ReadGraph g, Resource composite) throws DatabaseException {\r
-        return !getCompositeDiagrams(g, composite).isEmpty();\r
-    }\r
-\r
-    public static Collection<Resource> getComponentDiagrams(ReadGraph g, Resource component) throws DatabaseException {\r
-        StructuralResource2 sr = StructuralResource2.getInstance(g);\r
-        Resource componentType = g.getPossibleType(component, sr.Component);\r
-        Resource composite = componentType != null ? g.getPossibleObject(componentType, sr.IsDefinedBy) : null;\r
-        if (composite != null)\r
-            return getCompositeDiagrams(g, composite);\r
-        return Collections.emptyList();\r
-    }\r
-\r
-    public static Collection<Resource> getCompositeDiagrams(ReadGraph g, Resource composite) throws DatabaseException {\r
-        return g.getObjects(composite, ModelingResources.getInstance(g).CompositeToDiagram);\r
-    }\r
-\r
-    public static Resource getCompositeDiagram(ReadGraph g, Resource composite) throws DatabaseException {\r
-        return g.getSingleObject(composite, ModelingResources.getInstance(g).CompositeToDiagram);\r
-    }\r
-\r
-    public static Resource getPossibleCompositeDiagram(ReadGraph g, Resource composite) throws DatabaseException {\r
-        return g.getPossibleObject(composite, ModelingResources.getInstance(g).CompositeToDiagram);\r
-    }\r
-\r
-    public static Resource getPossibleDiagramComposite(ReadGraph g, Resource diagram) throws DatabaseException {\r
-        return g.getPossibleObject(diagram, ModelingResources.getInstance(g).DiagramToComposite);\r
-    }\r
-\r
-    public static Collection<ResourceArray> formInputs(ReadGraph g, Resource compositeOrComponent) throws DatabaseException {\r
-        ResourceArray path = getStructuralPath(g, compositeOrComponent);\r
-        Collection<ResourceArray> inputs = new ArrayList<ResourceArray>();\r
-\r
-        if (compositeHasDiagram(g, compositeOrComponent)) {\r
-            for (Resource dia : getCompositeDiagrams(g, compositeOrComponent)) {\r
-                //System.out.println(": " + NameUtils.getSafeName(g, compositeOrComponent));\r
-                //System.out.println(": " + NameUtils.getSafeName(g, dia));\r
-                inputs.add(path.prepended(dia));\r
-            }\r
-        } else if (componentHasDiagram(g, compositeOrComponent)) {\r
-            for (Resource dia : getComponentDiagrams(g, compositeOrComponent)) {\r
-                //System.out.println("* " + NameUtils.getSafeName(g, compositeOrComponent));\r
-                //System.out.println("* " + NameUtils.getSafeName(g, dia));\r
-                inputs.add(path.prepended(dia));\r
-            }\r
-        }\r
-\r
-        return inputs;\r
-    }\r
-\r
-//    public static ResourceArray getStructuralPath(ReadGraph g, Resource compositeOrComponent) throws DatabaseException {\r
-//        Builtins b = g.getBuiltins();\r
-//        StructuralResource2 sr = StructuralResource2.getInstance(g);\r
-//        Resource component = null;\r
-//\r
-//        List<Resource> result = new ArrayList<Resource>();\r
-//\r
-//        // Find component where to start the path browsing\r
-//        if (g.isInstanceOf(compositeOrComponent, sr.Composite)) {\r
-////            System.out.println("got composite: " + NameUtils.getSafeName(g, compositeOrComponent));\r
-//            Resource defines = g.getPossibleObject(compositeOrComponent, sr.Defines);\r
-//            if (defines == null)\r
-//                return ResourceArray.EMPTY;\r
-//            component = g.getPossibleObject(defines, b.HasSingleInstance);\r
-//            if (component == null)\r
-//                return ResourceArray.EMPTY;\r
-////            System.out.println("start path browsing from: " + NameUtils.getSafeName(g, component));\r
-//        } else if (g.isInstanceOf(compositeOrComponent, sr.Component)) {\r
-////            System.out.println("got component: " + NameUtils.getSafeName(g, compositeOrComponent));\r
-//            component = compositeOrComponent;\r
-//        } else {\r
-//            return ResourceArray.EMPTY;\r
-//        }\r
-//\r
-//        while (component != null) {\r
-////            System.out.println("ADDING COMPONENT: " + NameUtils.getSafeName(g, component));\r
-//            result.add(component);\r
-//\r
-//            Resource composite = g.getPossibleObject(component, b.PartOf);\r
-//            if (composite == null)\r
-//                break;\r
-////            System.out.println("  PART OF: " + NameUtils.getSafeName(g, composite));\r
-//\r
-//            Resource defines = g.getPossibleObject(composite, sr.Defines);\r
-//            if (defines == null)\r
-//                break;\r
-////            System.out.println("  WHICH DEFINES: " + NameUtils.getSafeName(g, defines));\r
-//\r
-//            component = g.getPossibleObject(defines, b.HasSingleInstance);\r
-//        }\r
-////        System.out.println("FOUND PATH:");\r
-////        for (Resource r : result)\r
-////            System.out.println("    " + NameUtils.getSafeName(g, r));\r
-//        return new ResourceArray(result);\r
-//    }\r
-\r
-    public static ResourceArray getStructuralPath(ReadGraph g, Resource compositeOrComponent) throws DatabaseException {\r
-        Layer0 b = Layer0.getInstance(g);\r
-        StructuralResource2 sr = StructuralResource2.getInstance(g);\r
-        Resource component = null;\r
-\r
-        List<Resource> result = new ArrayList<Resource>();\r
-\r
-//        System.out.println("checking composite or component: " + NameUtils.getSafeName(g, compositeOrComponent));\r
-\r
-        // Find component where to start the path browsing\r
-        if (g.isInstanceOf(compositeOrComponent, sr.Composite)) {\r
-//            System.out.println("got composite: " + NameUtils.getSafeName(g, compositeOrComponent));\r
-            component = compositeOrComponent;\r
-        } else if (g.isInstanceOf(compositeOrComponent, sr.Component)) {\r
-//            System.out.println("got component: " + NameUtils.getSafeName(g, compositeOrComponent));\r
-            component = compositeOrComponent;\r
-        } else {\r
-            return ResourceArray.EMPTY;\r
-        }\r
-//        System.out.println("start path browsing from: " + NameUtils.getSafeName(g, component));\r
-\r
-        while (component != null) {\r
-//            System.out.println("ADDING COMPONENT: " + NameUtils.getSafeName(g, component));\r
-            result.add(component);\r
-\r
-            Resource composite = g.getPossibleObject(component, b.PartOf);\r
-            if (composite == null)\r
-                break;\r
-            if (!g.isInstanceOf(composite, sr.Composite)) {\r
-//                System.out.println("  PART OF NON-COMPOSITE: " + NameUtils.getSafeName(g, composite));\r
-                break;\r
-            }\r
-//            System.out.println("  PART OF COMPOSITE: " + NameUtils.getSafeName(g, composite));\r
-            component = composite;\r
-        }\r
-//        System.out.println("FOUND PATH:");\r
-//        for (Resource r : result)\r
-//            System.out.println("    " + NameUtils.getSafeName(g, r));\r
-        return new ResourceArray(result);\r
-    }\r
-\r
-    public static Resource getCompositeConfigurationRoot(ReadGraph g, Resource composite) throws DatabaseException {\r
-        StructuralResource2 sr = StructuralResource2.getInstance(g);\r
-\r
-        if (!g.isInstanceOf(composite, sr.Composite))\r
-            throw new IllegalArgumentException("argument " + NameUtils.getSafeName(g, composite) + " is not a composite component");\r
-\r
-        ResourceArray path = getStructuralPath(g, composite);\r
-        if (path.isEmpty()) {\r
-            return composite;\r
-        }\r
-\r
-        Resource topmostComponent = path.resources[path.resources.length - 1];\r
-        //Resource parent = g.getPossibleObject(topmostComponent, g.getBuiltins().PartOf);\r
-        //assert parent != null;\r
-        //return parent;\r
-        return topmostComponent;\r
-    }\r
-\r
-    public static Resource getComponentConfigurationRoot(ReadGraph g, Resource component) throws DatabaseException {\r
-        StructuralResource2 sr = StructuralResource2.getInstance(g);\r
-\r
-        if (g.isInstanceOf(component, sr.Composite))\r
-            throw new IllegalArgumentException("argument " + NameUtils.getSafeName(g, component) + " is a composite component");\r
-        if (!g.isInstanceOf(component, sr.Component))\r
-            throw new IllegalArgumentException("argument " + NameUtils.getSafeName(g, component) + " is not a component");\r
-\r
-        ResourceArray path = getStructuralPath(g, component);\r
-        Resource topmostComponent = path.isEmpty() ? component : path.resources[path.resources.length - 1];\r
-//        Resource parent = g.getPossibleObject(topmostComponent, g.getBuiltins().PartOf);\r
-//        assert parent != null;\r
-//        return parent;\r
-        return topmostComponent;\r
-    }\r
-\r
-    public static Resource tryGetComponentContainer(ReadGraph g, Resource component) throws DatabaseException {\r
-        Layer0 l0 = Layer0.getInstance(g);\r
-        StructuralResource2 sr = StructuralResource2.getInstance(g);\r
-\r
-        if (g.isInstanceOf(component, sr.Composite))\r
-            throw new IllegalArgumentException("argument " + NameUtils.getSafeName(g, component) + " is a composite component");\r
-        if (!g.isInstanceOf(component, sr.Component))\r
-            throw new IllegalArgumentException("argument " + NameUtils.getSafeName(g, component) + " is not a component");\r
-\r
-        Resource container = g.getPossibleObject(component, l0.PartOf);\r
-        if (container == null)\r
-            return null;\r
-        return container;\r
-    }\r
-\r
-    public static Resource tryGetComponentConfigurationRoot(ReadGraph g, Resource component) throws DatabaseException {\r
-        StructuralResource2 sr = StructuralResource2.getInstance(g);\r
-\r
-        if (g.isInstanceOf(component, sr.Composite))\r
-            return null;\r
-        if (!g.isInstanceOf(component, sr.Component))\r
-            return null;\r
-\r
-        ResourceArray path = getStructuralPath(g, component);\r
-        Resource topmostComponent = path.isEmpty() ? component : path.resources[path.resources.length - 1];\r
-\r
-//        Resource parent = g.getPossibleObject(topmostComponent, g.getBuiltins().PartOf);\r
-//        return parent;\r
-        return topmostComponent;\r
-    }\r
-\r
-    public static List<Resource> getCompositePathToConfiguration(ReadGraph graph, Resource composite) throws DatabaseException {\r
-        Layer0 L0 = Layer0.getInstance(graph);\r
-        SimulationResource SIMU = SimulationResource.getInstance(graph);\r
-        LinkedList<Resource> compositePath = new LinkedList<Resource>();\r
-        while (!graph.hasStatement(composite, SIMU.IsConfigurationOf)) {\r
-            compositePath.addFirst(composite);\r
-            composite = graph.getPossibleObject(composite, L0.PartOf);\r
-            if (composite == null)\r
-                return null;\r
-        }\r
-        return compositePath;\r
-    }\r
-\r
-}\r
+/*******************************************************************************
+ * 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.modeling;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.LinkedList;
+import java.util.List;
+
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.common.ResourceArray;
+import org.simantics.db.common.utils.NameUtils;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.layer0.Layer0;
+import org.simantics.simulation.ontology.SimulationResource;
+import org.simantics.structural.stubs.StructuralResource2;
+
+/**
+ * @author Tuukka Lehtonen
+ */
+public final class ComponentUtils {
+
+    public static boolean componentHasDiagram(ReadGraph g, Resource component) throws DatabaseException {
+        return !getComponentDiagrams(g, component).isEmpty();
+    }
+
+    public static boolean compositeHasDiagram(ReadGraph g, Resource composite) throws DatabaseException {
+        return !getCompositeDiagrams(g, composite).isEmpty();
+    }
+
+    public static Collection<Resource> getComponentDiagrams(ReadGraph g, Resource component) throws DatabaseException {
+        StructuralResource2 sr = StructuralResource2.getInstance(g);
+        Resource componentType = g.getPossibleType(component, sr.Component);
+        Resource composite = componentType != null ? g.getPossibleObject(componentType, sr.IsDefinedBy) : null;
+        if (composite != null)
+            return getCompositeDiagrams(g, composite);
+        return Collections.emptyList();
+    }
+
+    public static Collection<Resource> getCompositeDiagrams(ReadGraph g, Resource composite) throws DatabaseException {
+        return g.getObjects(composite, ModelingResources.getInstance(g).CompositeToDiagram);
+    }
+
+    public static Resource getCompositeDiagram(ReadGraph g, Resource composite) throws DatabaseException {
+        return g.getSingleObject(composite, ModelingResources.getInstance(g).CompositeToDiagram);
+    }
+
+    public static Resource getPossibleCompositeDiagram(ReadGraph g, Resource composite) throws DatabaseException {
+        return g.getPossibleObject(composite, ModelingResources.getInstance(g).CompositeToDiagram);
+    }
+
+    public static Resource getPossibleDiagramComposite(ReadGraph g, Resource diagram) throws DatabaseException {
+        return g.getPossibleObject(diagram, ModelingResources.getInstance(g).DiagramToComposite);
+    }
+
+    public static Collection<ResourceArray> formInputs(ReadGraph g, Resource compositeOrComponent) throws DatabaseException {
+        ResourceArray path = getStructuralPath(g, compositeOrComponent);
+        Collection<ResourceArray> inputs = new ArrayList<ResourceArray>();
+
+        if (compositeHasDiagram(g, compositeOrComponent)) {
+            for (Resource dia : getCompositeDiagrams(g, compositeOrComponent)) {
+                //System.out.println(": " + NameUtils.getSafeName(g, compositeOrComponent));
+                //System.out.println(": " + NameUtils.getSafeName(g, dia));
+                inputs.add(path.prepended(dia));
+            }
+        } else if (componentHasDiagram(g, compositeOrComponent)) {
+            for (Resource dia : getComponentDiagrams(g, compositeOrComponent)) {
+                //System.out.println("* " + NameUtils.getSafeName(g, compositeOrComponent));
+                //System.out.println("* " + NameUtils.getSafeName(g, dia));
+                inputs.add(path.prepended(dia));
+            }
+        }
+
+        return inputs;
+    }
+
+//    public static ResourceArray getStructuralPath(ReadGraph g, Resource compositeOrComponent) throws DatabaseException {
+//        Builtins b = g.getBuiltins();
+//        StructuralResource2 sr = StructuralResource2.getInstance(g);
+//        Resource component = null;
+//
+//        List<Resource> result = new ArrayList<Resource>();
+//
+//        // Find component where to start the path browsing
+//        if (g.isInstanceOf(compositeOrComponent, sr.Composite)) {
+////            System.out.println("got composite: " + NameUtils.getSafeName(g, compositeOrComponent));
+//            Resource defines = g.getPossibleObject(compositeOrComponent, sr.Defines);
+//            if (defines == null)
+//                return ResourceArray.EMPTY;
+//            component = g.getPossibleObject(defines, b.HasSingleInstance);
+//            if (component == null)
+//                return ResourceArray.EMPTY;
+////            System.out.println("start path browsing from: " + NameUtils.getSafeName(g, component));
+//        } else if (g.isInstanceOf(compositeOrComponent, sr.Component)) {
+////            System.out.println("got component: " + NameUtils.getSafeName(g, compositeOrComponent));
+//            component = compositeOrComponent;
+//        } else {
+//            return ResourceArray.EMPTY;
+//        }
+//
+//        while (component != null) {
+////            System.out.println("ADDING COMPONENT: " + NameUtils.getSafeName(g, component));
+//            result.add(component);
+//
+//            Resource composite = g.getPossibleObject(component, b.PartOf);
+//            if (composite == null)
+//                break;
+////            System.out.println("  PART OF: " + NameUtils.getSafeName(g, composite));
+//
+//            Resource defines = g.getPossibleObject(composite, sr.Defines);
+//            if (defines == null)
+//                break;
+////            System.out.println("  WHICH DEFINES: " + NameUtils.getSafeName(g, defines));
+//
+//            component = g.getPossibleObject(defines, b.HasSingleInstance);
+//        }
+////        System.out.println("FOUND PATH:");
+////        for (Resource r : result)
+////            System.out.println("    " + NameUtils.getSafeName(g, r));
+//        return new ResourceArray(result);
+//    }
+
+    public static ResourceArray getStructuralPath(ReadGraph g, Resource compositeOrComponent) throws DatabaseException {
+        Layer0 b = Layer0.getInstance(g);
+        StructuralResource2 sr = StructuralResource2.getInstance(g);
+        Resource component = null;
+
+        List<Resource> result = new ArrayList<Resource>();
+
+//        System.out.println("checking composite or component: " + NameUtils.getSafeName(g, compositeOrComponent));
+
+        // Find component where to start the path browsing
+        if (g.isInstanceOf(compositeOrComponent, sr.Composite)) {
+//            System.out.println("got composite: " + NameUtils.getSafeName(g, compositeOrComponent));
+            component = compositeOrComponent;
+        } else if (g.isInstanceOf(compositeOrComponent, sr.Component)) {
+//            System.out.println("got component: " + NameUtils.getSafeName(g, compositeOrComponent));
+            component = compositeOrComponent;
+        } else {
+            return ResourceArray.EMPTY;
+        }
+//        System.out.println("start path browsing from: " + NameUtils.getSafeName(g, component));
+
+        while (component != null) {
+//            System.out.println("ADDING COMPONENT: " + NameUtils.getSafeName(g, component));
+            result.add(component);
+
+            Resource composite = g.getPossibleObject(component, b.PartOf);
+            if (composite == null)
+                break;
+            if (!g.isInstanceOf(composite, sr.Composite)) {
+//                System.out.println("  PART OF NON-COMPOSITE: " + NameUtils.getSafeName(g, composite));
+                break;
+            }
+//            System.out.println("  PART OF COMPOSITE: " + NameUtils.getSafeName(g, composite));
+            component = composite;
+        }
+//        System.out.println("FOUND PATH:");
+//        for (Resource r : result)
+//            System.out.println("    " + NameUtils.getSafeName(g, r));
+        return new ResourceArray(result);
+    }
+
+    public static Resource getCompositeConfigurationRoot(ReadGraph g, Resource composite) throws DatabaseException {
+        StructuralResource2 sr = StructuralResource2.getInstance(g);
+
+        if (!g.isInstanceOf(composite, sr.Composite))
+            throw new IllegalArgumentException("argument " + NameUtils.getSafeName(g, composite) + " is not a composite component");
+
+        ResourceArray path = getStructuralPath(g, composite);
+        if (path.isEmpty()) {
+            return composite;
+        }
+
+        Resource topmostComponent = path.resources[path.resources.length - 1];
+        //Resource parent = g.getPossibleObject(topmostComponent, g.getBuiltins().PartOf);
+        //assert parent != null;
+        //return parent;
+        return topmostComponent;
+    }
+
+    public static Resource getComponentConfigurationRoot(ReadGraph g, Resource component) throws DatabaseException {
+        StructuralResource2 sr = StructuralResource2.getInstance(g);
+
+        if (g.isInstanceOf(component, sr.Composite))
+            throw new IllegalArgumentException("argument " + NameUtils.getSafeName(g, component) + " is a composite component");
+        if (!g.isInstanceOf(component, sr.Component))
+            throw new IllegalArgumentException("argument " + NameUtils.getSafeName(g, component) + " is not a component");
+
+        ResourceArray path = getStructuralPath(g, component);
+        Resource topmostComponent = path.isEmpty() ? component : path.resources[path.resources.length - 1];
+//        Resource parent = g.getPossibleObject(topmostComponent, g.getBuiltins().PartOf);
+//        assert parent != null;
+//        return parent;
+        return topmostComponent;
+    }
+
+    public static Resource tryGetComponentContainer(ReadGraph g, Resource component) throws DatabaseException {
+        Layer0 l0 = Layer0.getInstance(g);
+        StructuralResource2 sr = StructuralResource2.getInstance(g);
+
+        if (g.isInstanceOf(component, sr.Composite))
+            throw new IllegalArgumentException("argument " + NameUtils.getSafeName(g, component) + " is a composite component");
+        if (!g.isInstanceOf(component, sr.Component))
+            throw new IllegalArgumentException("argument " + NameUtils.getSafeName(g, component) + " is not a component");
+
+        Resource container = g.getPossibleObject(component, l0.PartOf);
+        if (container == null)
+            return null;
+        return container;
+    }
+
+    public static Resource tryGetComponentConfigurationRoot(ReadGraph g, Resource component) throws DatabaseException {
+        StructuralResource2 sr = StructuralResource2.getInstance(g);
+
+        if (g.isInstanceOf(component, sr.Composite))
+            return null;
+        if (!g.isInstanceOf(component, sr.Component))
+            return null;
+
+        ResourceArray path = getStructuralPath(g, component);
+        Resource topmostComponent = path.isEmpty() ? component : path.resources[path.resources.length - 1];
+
+//        Resource parent = g.getPossibleObject(topmostComponent, g.getBuiltins().PartOf);
+//        return parent;
+        return topmostComponent;
+    }
+
+    public static List<Resource> getCompositePathToConfiguration(ReadGraph graph, Resource composite) throws DatabaseException {
+        Layer0 L0 = Layer0.getInstance(graph);
+        SimulationResource SIMU = SimulationResource.getInstance(graph);
+        LinkedList<Resource> compositePath = new LinkedList<Resource>();
+        while (!graph.hasStatement(composite, SIMU.IsConfigurationOf)) {
+            compositePath.addFirst(composite);
+            composite = graph.getPossibleObject(composite, L0.PartOf);
+            if (composite == null)
+                return null;
+        }
+        return compositePath;
+    }
+
+}