]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.modeling/src/org/simantics/modeling/requests/CollectionRequest.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / requests / CollectionRequest.java
diff --git a/bundles/org.simantics.modeling/src/org/simantics/modeling/requests/CollectionRequest.java b/bundles/org.simantics.modeling/src/org/simantics/modeling/requests/CollectionRequest.java
new file mode 100644 (file)
index 0000000..ee08ec5
--- /dev/null
@@ -0,0 +1,284 @@
+/*******************************************************************************\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.requests;\r
+\r
+import java.util.ArrayDeque;\r
+import java.util.Deque;\r
+\r
+import org.eclipse.core.runtime.ILog;\r
+import org.eclipse.core.runtime.IProgressMonitor;\r
+import org.eclipse.core.runtime.IStatus;\r
+import org.eclipse.core.runtime.NullProgressMonitor;\r
+import org.eclipse.core.runtime.Platform;\r
+import org.eclipse.core.runtime.Status;\r
+import org.eclipse.core.runtime.SubMonitor;\r
+import org.simantics.NameLabelUtil;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.request.ReadRequest;\r
+import org.simantics.db.common.request.ResourceRead;\r
+import org.simantics.db.common.utils.NameUtils;\r
+import org.simantics.db.exception.AdaptionException;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.request.Read;\r
+import org.simantics.diagram.query.DiagramRequests;\r
+import org.simantics.diagram.stubs.DiagramResource;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.modeling.ModelingResources;\r
+import org.simantics.modeling.internal.Plugin;\r
+import org.simantics.simulation.ontology.SimulationResource;\r
+import org.simantics.structural.stubs.StructuralResource2;\r
+import org.simantics.utils.page.PageDesc;\r
+\r
+/**\r
+ * If the specified progress monitor indicates cancellation, the request will\r
+ * return <code>null</code>.\r
+ * \r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class CollectionRequest implements Read<CollectionResult> {\r
+\r
+    private static final boolean DEBUG = false;\r
+\r
+    IProgressMonitor monitor;\r
+    PageDesc defaultPageDesc;\r
+    Resource[] input;\r
+    ReadGraph g;\r
+    Layer0 l0;\r
+    StructuralResource2 sr;\r
+    DiagramResource dr;\r
+    ModelingResources mr;\r
+    SimulationResource SIMU;\r
+\r
+    public CollectionRequest(IProgressMonitor monitor, PageDesc defaultPageDesc, Resource... input) {\r
+        this.monitor = monitor != null ? monitor : new NullProgressMonitor();\r
+        this.defaultPageDesc = defaultPageDesc;\r
+        this.input = input;\r
+    }\r
+\r
+    String safeGetName(Resource r) throws DatabaseException {\r
+        return g.syncRequest(new GetName(r));\r
+    }\r
+\r
+//    Collection<String> getPartOfGroups(Resource diagram) throws DatabaseException {\r
+//        Resource r = diagram;\r
+//        Deque<String> result = new ArrayDeque<String>();\r
+//        loop:\r
+//            while (true) {\r
+//                for (Resource partOf : g.getObjects(r, b.PartOf)) {\r
+//                    if (g.isInstanceOf(partOf, dr.DiagramLibrary)) {\r
+//                        result.addFirst(safeGetName(partOf));\r
+//                        r = partOf;\r
+//                        continue loop;\r
+//                    }\r
+//                }\r
+//                return result;\r
+//            }\r
+//    }\r
+\r
+    @Override\r
+    public CollectionResult perform(ReadGraph g) throws DatabaseException {\r
+        this.g = g;\r
+\r
+        l0 = Layer0.getInstance(g);\r
+        dr = DiagramResource.getInstance(g);\r
+        sr = StructuralResource2.getInstance(g);\r
+        mr = ModelingResources.getInstance(g);\r
+        SIMU = SimulationResource.getInstance(g);\r
+\r
+        final CollectionResult result = new CollectionResult();\r
+        final Deque<Node> roots = new ArrayDeque<Node>();\r
+\r
+        // 1. Based on input, look for the proper nodes to start browsing for diagrams.\r
+        for (Resource r : input) {\r
+            /*if (g.isInstanceOf(r, SIMU.Model)) {\r
+                // Complete models\r
+                Resource composite = g.getPossibleObject(r, SIMU.HasConfiguration);\r
+                Resource diagram = composite != null ? g.getPossibleObject(composite, mr.CompositeToDiagram) : null;\r
+\r
+                if (DEBUG)\r
+                    System.out.println("Model root");\r
+\r
+                if (composite != null) {                       \r
+                       Node node = new Node(null, safeGetName(r), diagram, composite, r);\r
+                    roots.add(node);\r
+                    result.roots.add(roots.peekLast());\r
+                }\r
+            } else*/ if (g.isInstanceOf(r, l0.IndexRoot)) {\r
+//             for(Resource type : ModelingUtils.searchByTypeShallow(g, r, sr.ComponentType)) {\r
+//                     Resource composite = g.getPossibleObject(type, sr.IsDefinedBy);\r
+//                    Resource diagram = composite != null ? g.getPossibleObject(composite, mr.CompositeToDiagram) : null;\r
+//                     if(composite != null) {\r
+//                     Node node = new Node(null, safeGetName(r), diagram, composite, r);\r
+//                        roots.add(node);\r
+//                        result.roots.add(roots.peekLast());\r
+//                     }\r
+//             }\r
+               \r
+               Node node = new Node(null, safeGetName(r), null, r);\r
+               roots.add(node);\r
+               result.roots.add(roots.peekLast());\r
+               \r
+            } else if (g.isInstanceOf(r, sr.Composite)) {\r
+                // The contents of components\r
+                String name = null;\r
+                Resource model = g.getPossibleObject(r, SIMU.IsConfigurationOf);\r
+                //Resource componentType = g.getPossibleObject(r, sr.Defines);\r
+                if (model != null) {\r
+                    name = safeGetName(model);\r
+\r
+                    if (DEBUG)\r
+                        System.out.println("Configuration root: " + name);\r
+\r
+//                } else if (componentType != null) {\r
+//                    Resource singleInstance = componentType != null ? g.getPossibleObject(componentType, b.HasSingleInstance) : null;\r
+//                    name = singleInstance != null ? safeGetName(singleInstance) : safeGetName(componentType);\r
+//                    System.out.println("Composite of component type root: " + name);\r
+                } else {\r
+                    name = safeGetName(r);\r
+                    if (DEBUG)\r
+                        System.out.println("Composite root: " + name);\r
+                }\r
+\r
+                Resource diagram = g.getPossibleObject(r, mr.CompositeToDiagram);\r
+                diagram = (diagram != null && g.isInstanceOf(diagram, dr.Composite)) ? diagram : null;\r
+\r
+                {\r
+                       Node node = new Node(null, name, diagram, r);\r
+                       roots.add(node);\r
+                       result.roots.add(roots.peekLast());\r
+                }\r
+//            } else if (g.isInstanceOf(r, sr.Component)) {\r
+//                // Complete components themselves\r
+//                Resource componentType = g.getSingleType(r, sr.Component);\r
+//                Resource composite = g.getPossibleObject(componentType, sr.IsDefinedBy);\r
+//                Resource diagram = (composite != null && g.isInstanceOf(composite, sr.Composite)) ? g.getPossibleObject(composite, mr.CompositeToDiagram) : null;\r
+//                String name = safeGetName(r);\r
+//                System.out.println("Component root: " + name);\r
+//                roots.add(new Node(null, name, diagram, composite, r));\r
+//                result.roots.add(roots.peekLast());\r
+            } else if (g.isInheritedFrom(r, dr.DefinedElement)) {\r
+                // Symbols\r
+                Resource composite = g.getPossibleObject(r, sr.IsDefinedBy);\r
+                composite = (composite != null && g.isInstanceOf(composite, dr.Composite)) ? composite : null;\r
+                if (composite != null) {\r
+                    Resource componentType = g.getPossibleObject(r, mr.SymbolToComponentType);\r
+                    String name = safeGetName(componentType);\r
+                    name += " Symbol";\r
+                    if (DEBUG)\r
+                        System.out.println("Symbol root: " + name);\r
+                    \r
+                    {                 \r
+                       Node node = new Node(null, name, composite, r);\r
+                           roots.add(node);\r
+                           result.roots.add(roots.peekLast());\r
+                    }\r
+                }\r
+//            } else if (g.isInheritedFrom(r, sr.Component)) {\r
+//                // Reusable component types\r
+//                Resource composite = g.getPossibleObject(r, sr.IsDefinedBy);\r
+//                Resource diagram = (composite != null && g.isInstanceOf(composite, sr.Composite)) ? g.getPossibleObject(composite, mr.CompositeToDiagram) : null;\r
+//                String name = safeGetName(r);\r
+//                System.out.println("Component type root: " + name);\r
+//                roots.add(new Node(null, name, diagram, r, composite));\r
+//                result.roots.add(roots.peekLast());\r
+            }\r
+        }\r
+\r
+        final SubMonitor mon = SubMonitor.convert(monitor);\r
+\r
+        g.syncRequest(new ReadRequest() {\r
+            @Override\r
+            public void run(ReadGraph graph) throws DatabaseException {\r
+                for (Node node : roots) {\r
+                    loadComposites(graph, node);\r
+                }\r
+            }\r
+\r
+            private void loadComposites(ReadGraph graph, final Node node) throws DatabaseException {\r
+                Resource diagram = node.getDiagramResource();\r
+                //System.out.println("loadComposites(" + diagram + ", " + node + ")");\r
+                if (diagram != null) {\r
+                    result.addDiagram(diagram, node);\r
+//                    node.setPartOfGroups(getPartOfGroups(diagram));\r
+                }\r
+                mon.setWorkRemaining(1000);\r
+\r
+                for(Resource r : graph.getObjects(node.getDefiningResources().resources[0], l0.ConsistsOf)) {\r
+                       if(graph.isInstanceOf(r, sr.Composite)) {\r
+                               String compositeName = graph.syncRequest(new GetName(r));\r
+                               Resource definingDiagram = graph.getPossibleObject(r, mr.CompositeToDiagram);\r
+                       Node n = new Node(node, compositeName, definingDiagram, r);\r
+                        loadComposites(graph, n);\r
+                        mon.worked(1);\r
+                       } else if (graph.isInstanceOf(r, l0.Library)) {\r
+                               String compositeName = graph.syncRequest(new GetName(r));\r
+                       Node n = new Node(node, compositeName, null, r);\r
+                        loadComposites(graph, n);\r
+                        mon.worked(1);\r
+                       } else if (graph.isInheritedFrom(r, sr.Component)) {\r
+                               String name = safeGetName(r);\r
+                               Node n = new Node(node, name, null, r);\r
+                               loadComposites(graph, n);\r
+                               mon.worked(1);\r
+                  }\r
+                }\r
+                \r
+            }\r
+\r
+        });\r
+\r
+        ILog log = Platform.getLog(Platform.getBundle(Plugin.PLUGIN_ID));\r
+\r
+        // Assign a page size description for each node.\r
+        for (Node node : result.diagramList) {\r
+            if (monitor.isCanceled())\r
+                return null;\r
+            mon.setWorkRemaining(10000);\r
+\r
+            try {\r
+                PageDesc realPageDesc = g.syncRequest(DiagramRequests.getPageDesc(node.getDiagramResource(), defaultPageDesc));\r
+                node.setPageDesc(realPageDesc);\r
+            } catch (DatabaseException e) {\r
+                log.log(new Status(IStatus.WARNING, Plugin.PLUGIN_ID,\r
+                        "Broken page description in diagram "\r
+                        + NameUtils.getURIOrSafeNameInternal(g, node.getDiagramResource())\r
+                        + ". Reopen the diagram to fix it.", e));\r
+            }\r
+\r
+            mon.worked(1);\r
+        }\r
+\r
+        if (monitor.isCanceled())\r
+            return null;\r
+\r
+        return result;\r
+    }\r
+    \r
+    static class GetName extends ResourceRead<String> {\r
+\r
+        public GetName(Resource resource) {\r
+            super(resource);\r
+        }\r
+\r
+        @Override\r
+        public String perform(ReadGraph graph) throws DatabaseException {\r
+            try {\r
+                return NameLabelUtil.modalName(graph, resource);\r
+            } catch (AdaptionException e) {\r
+                return NameUtils.getSafeName(graph, resource);\r
+            }\r
+        }\r
+\r
+    }\r
+\r
+}
\ No newline at end of file