]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/requests/CollectionRequest.java
Merge "(refs #7307) Added features field to SCL module header"
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / requests / CollectionRequest.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2017 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.modeling.requests;
13
14 import java.util.ArrayDeque;
15 import java.util.Deque;
16
17 import org.eclipse.core.runtime.ILog;
18 import org.eclipse.core.runtime.IProgressMonitor;
19 import org.eclipse.core.runtime.IStatus;
20 import org.eclipse.core.runtime.NullProgressMonitor;
21 import org.eclipse.core.runtime.Platform;
22 import org.eclipse.core.runtime.Status;
23 import org.eclipse.core.runtime.SubMonitor;
24 import org.simantics.NameLabelUtil;
25 import org.simantics.db.ReadGraph;
26 import org.simantics.db.Resource;
27 import org.simantics.db.common.request.ReadRequest;
28 import org.simantics.db.common.request.ResourceRead;
29 import org.simantics.db.common.utils.NameUtils;
30 import org.simantics.db.exception.DatabaseException;
31 import org.simantics.db.request.Read;
32 import org.simantics.diagram.query.DiagramRequests;
33 import org.simantics.diagram.stubs.DiagramResource;
34 import org.simantics.layer0.Layer0;
35 import org.simantics.modeling.ModelingResources;
36 import org.simantics.modeling.internal.Plugin;
37 import org.simantics.simulation.ontology.SimulationResource;
38 import org.simantics.structural.stubs.StructuralResource2;
39 import org.simantics.utils.page.PageDesc;
40
41 /**
42  * If the specified progress monitor indicates cancellation, the request will
43  * return <code>null</code>.
44  * 
45  * @author Tuukka Lehtonen
46  */
47 public class CollectionRequest implements Read<CollectionResult> {
48
49     private static final boolean DEBUG = false;
50
51     IProgressMonitor monitor;
52     PageDesc defaultPageDesc;
53     Resource[] input;
54     ReadGraph g;
55     Layer0 l0;
56     StructuralResource2 sr;
57     DiagramResource dr;
58     ModelingResources mr;
59     SimulationResource SIMU;
60
61     public CollectionRequest(IProgressMonitor monitor, PageDesc defaultPageDesc, Resource... input) {
62         this.monitor = monitor != null ? monitor : new NullProgressMonitor();
63         this.defaultPageDesc = defaultPageDesc;
64         this.input = input;
65     }
66
67     String safeGetName(Resource r) throws DatabaseException {
68         return g.syncRequest(new GetName(r));
69     }
70
71     @Override
72     public CollectionResult perform(ReadGraph g) throws DatabaseException {
73         this.g = g;
74
75         l0 = Layer0.getInstance(g);
76         dr = DiagramResource.getInstance(g);
77         sr = StructuralResource2.getInstance(g);
78         mr = ModelingResources.getInstance(g);
79         SIMU = SimulationResource.getInstance(g);
80
81         final CollectionResult result = new CollectionResult();
82         final Deque<Node> roots = new ArrayDeque<>();
83
84         // 1. Based on input, look for the proper nodes to start browsing for diagrams.
85         for (Resource r : input) {
86             if (g.isInstanceOf(r, l0.IndexRoot)) {
87                 Node node = new Node(null, safeGetName(r), null, r);
88                 roots.add(node);
89                 result.roots.add(roots.peekLast());
90             } else if (g.isInstanceOf(r, sr.Composite)) {
91                 // The contents of components
92                 String name = null;
93                 Resource model = g.getPossibleObject(r, SIMU.IsConfigurationOf);
94                 if (model != null) {
95                     name = safeGetName(model);
96                     if (DEBUG)
97                         System.out.println("Configuration root: " + name);
98                 } else {
99                     name = safeGetName(r);
100                     if (DEBUG)
101                         System.out.println("Composite root: " + name);
102                 }
103
104                 Resource diagram = g.getPossibleObject(r, mr.CompositeToDiagram);
105                 diagram = (diagram != null && g.isInstanceOf(diagram, dr.Composite)) ? diagram : null;
106
107                 {
108                     Node node = new Node(null, name, diagram, r);
109                     roots.add(node);
110                     result.roots.add(roots.peekLast());
111                 }
112             } else if (g.isInheritedFrom(r, dr.DefinedElement)) {
113                 // Symbols
114                 Resource composite = g.getPossibleObject(r, sr.IsDefinedBy);
115                 composite = (composite != null && g.isInstanceOf(composite, dr.Composite)) ? composite : null;
116                 if (composite != null) {
117                     Resource componentType = g.getPossibleObject(r, mr.SymbolToComponentType);
118                     String name = safeGetName(componentType);
119                     name += " Symbol";
120                     if (DEBUG)
121                         System.out.println("Symbol root: " + name);
122
123                     {
124                         Node node = new Node(null, name, composite, r);
125                         roots.add(node);
126                         result.roots.add(roots.peekLast());
127                     }
128                 }
129             }
130         }
131
132         final SubMonitor mon = SubMonitor.convert(monitor);
133
134         g.syncRequest(new ReadRequest() {
135             @Override
136             public void run(ReadGraph graph) throws DatabaseException {
137                 for (Node node : roots) {
138                     loadComposites(graph, node);
139                 }
140             }
141
142             private void loadComposites(ReadGraph graph, final Node node) throws DatabaseException {
143                 Resource diagram = node.getDiagramResource();
144                 if (DEBUG)
145                     System.out.println("loadComposites(" + diagram + ", " + node + ")");
146                 if (diagram != null)
147                     result.addDiagram(diagram, node);
148                 mon.setWorkRemaining(1000);
149
150                 for(Resource r : graph.getObjects(node.getDefiningResources().resources[0], l0.ConsistsOf)) {
151                     if(graph.isInstanceOf(r, sr.Composite)) {
152                         String compositeName = graph.syncRequest(new GetName(r));
153                         Resource definingDiagram = graph.getPossibleObject(r, mr.CompositeToDiagram);
154                         Node n = new Node(node, compositeName, definingDiagram, r);
155                         if (DEBUG)
156                             System.out.println("Found composite: " + n);
157                         loadComposites(graph, n);
158                         mon.worked(1);
159                     } else if (graph.isInstanceOf(r, l0.Library)) {
160                         String compositeName = graph.syncRequest(new GetName(r));
161                         Node n = new Node(node, compositeName, null, r);
162                         if (DEBUG)
163                             System.out.println("Found library: " + n);
164                         loadComposites(graph, n);
165                         mon.worked(1);
166                     } else if (graph.isInheritedFrom(r, sr.Component)) {
167                         Resource definedBy = graph.getPossibleObject(r, sr.IsDefinedBy);
168                         if (definedBy == null)
169                             continue;
170                         String name = safeGetName(r);
171                         Node n = new Node(node, name, null, r);
172                         if (DEBUG)
173                             System.out.println("Found component: " + n);
174                         loadComposites(graph, n);
175                         mon.worked(1);
176                     }
177                 }
178             }
179         });
180
181         ILog log = Platform.getLog(Platform.getBundle(Plugin.PLUGIN_ID));
182
183         // Assign a page size description for each node.
184         for (Node node : result.diagramList) {
185             if (monitor.isCanceled())
186                 return null;
187             mon.setWorkRemaining(10000);
188
189             try {
190                 PageDesc realPageDesc = g.syncRequest(DiagramRequests.getPageDesc(node.getDiagramResource(), defaultPageDesc));
191                 node.setPageDesc(realPageDesc);
192             } catch (DatabaseException e) {
193                 log.log(new Status(IStatus.WARNING, Plugin.PLUGIN_ID,
194                         "Broken page description in diagram "
195                         + NameUtils.getURIOrSafeNameInternal(g, node.getDiagramResource())
196                         + ". Reopen the diagram to fix it.", e));
197             }
198
199             mon.worked(1);
200         }
201
202         if (monitor.isCanceled())
203             return null;
204
205         return result;
206     }
207
208     static class GetName extends ResourceRead<String> {
209         public GetName(Resource resource) {
210             super(resource);
211         }
212
213         @Override
214         public String perform(ReadGraph graph) throws DatabaseException {
215             try {
216                 return NameLabelUtil.modalName(graph, resource);
217             } catch (DatabaseException e) {
218                 return NameUtils.getSafeName(graph, resource);
219             }
220         }
221     }
222
223 }