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