]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/requests/CollectionRequest.java
Default property editing restores assertions
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / requests / CollectionRequest.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 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.AdaptionException;
31 import org.simantics.db.exception.DatabaseException;
32 import org.simantics.db.request.Read;
33 import org.simantics.diagram.query.DiagramRequests;
34 import org.simantics.diagram.stubs.DiagramResource;
35 import org.simantics.layer0.Layer0;
36 import org.simantics.modeling.ModelingResources;
37 import org.simantics.modeling.internal.Plugin;
38 import org.simantics.simulation.ontology.SimulationResource;
39 import org.simantics.structural.stubs.StructuralResource2;
40 import org.simantics.utils.page.PageDesc;
41
42 /**
43  * If the specified progress monitor indicates cancellation, the request will
44  * return <code>null</code>.
45  * 
46  * @author Tuukka Lehtonen
47  */
48 public class CollectionRequest implements Read<CollectionResult> {
49
50     private static final boolean DEBUG = false;
51
52     IProgressMonitor monitor;
53     PageDesc defaultPageDesc;
54     Resource[] input;
55     ReadGraph g;
56     Layer0 l0;
57     StructuralResource2 sr;
58     DiagramResource dr;
59     ModelingResources mr;
60     SimulationResource SIMU;
61
62     public CollectionRequest(IProgressMonitor monitor, PageDesc defaultPageDesc, Resource... input) {
63         this.monitor = monitor != null ? monitor : new NullProgressMonitor();
64         this.defaultPageDesc = defaultPageDesc;
65         this.input = input;
66     }
67
68     String safeGetName(Resource r) throws DatabaseException {
69         return g.syncRequest(new GetName(r));
70     }
71
72 //    Collection<String> getPartOfGroups(Resource diagram) throws DatabaseException {
73 //        Resource r = diagram;
74 //        Deque<String> result = new ArrayDeque<String>();
75 //        loop:
76 //            while (true) {
77 //                for (Resource partOf : g.getObjects(r, b.PartOf)) {
78 //                    if (g.isInstanceOf(partOf, dr.DiagramLibrary)) {
79 //                        result.addFirst(safeGetName(partOf));
80 //                        r = partOf;
81 //                        continue loop;
82 //                    }
83 //                }
84 //                return result;
85 //            }
86 //    }
87
88     @Override
89     public CollectionResult perform(ReadGraph g) throws DatabaseException {
90         this.g = g;
91
92         l0 = Layer0.getInstance(g);
93         dr = DiagramResource.getInstance(g);
94         sr = StructuralResource2.getInstance(g);
95         mr = ModelingResources.getInstance(g);
96         SIMU = SimulationResource.getInstance(g);
97
98         final CollectionResult result = new CollectionResult();
99         final Deque<Node> roots = new ArrayDeque<Node>();
100
101         // 1. Based on input, look for the proper nodes to start browsing for diagrams.
102         for (Resource r : input) {
103             /*if (g.isInstanceOf(r, SIMU.Model)) {
104                 // Complete models
105                 Resource composite = g.getPossibleObject(r, SIMU.HasConfiguration);
106                 Resource diagram = composite != null ? g.getPossibleObject(composite, mr.CompositeToDiagram) : null;
107
108                 if (DEBUG)
109                     System.out.println("Model root");
110
111                 if (composite != null) {                        
112                         Node node = new Node(null, safeGetName(r), diagram, composite, r);
113                     roots.add(node);
114                     result.roots.add(roots.peekLast());
115                 }
116             } else*/ if (g.isInstanceOf(r, l0.IndexRoot)) {
117 //              for(Resource type : ModelingUtils.searchByTypeShallow(g, r, sr.ComponentType)) {
118 //                      Resource composite = g.getPossibleObject(type, sr.IsDefinedBy);
119 //                    Resource diagram = composite != null ? g.getPossibleObject(composite, mr.CompositeToDiagram) : null;
120 //                      if(composite != null) {
121 //                      Node node = new Node(null, safeGetName(r), diagram, composite, r);
122 //                        roots.add(node);
123 //                        result.roots.add(roots.peekLast());
124 //                      }
125 //              }
126                 
127                 Node node = new Node(null, safeGetName(r), null, r);
128                 roots.add(node);
129                 result.roots.add(roots.peekLast());
130                 
131             } else if (g.isInstanceOf(r, sr.Composite)) {
132                 // The contents of components
133                 String name = null;
134                 Resource model = g.getPossibleObject(r, SIMU.IsConfigurationOf);
135                 //Resource componentType = g.getPossibleObject(r, sr.Defines);
136                 if (model != null) {
137                     name = safeGetName(model);
138
139                     if (DEBUG)
140                         System.out.println("Configuration root: " + name);
141
142 //                } else if (componentType != null) {
143 //                    Resource singleInstance = componentType != null ? g.getPossibleObject(componentType, b.HasSingleInstance) : null;
144 //                    name = singleInstance != null ? safeGetName(singleInstance) : safeGetName(componentType);
145 //                    System.out.println("Composite of component type root: " + name);
146                 } else {
147                     name = safeGetName(r);
148                     if (DEBUG)
149                         System.out.println("Composite root: " + name);
150                 }
151
152                 Resource diagram = g.getPossibleObject(r, mr.CompositeToDiagram);
153                 diagram = (diagram != null && g.isInstanceOf(diagram, dr.Composite)) ? diagram : null;
154
155                 {
156                         Node node = new Node(null, name, diagram, r);
157                         roots.add(node);
158                         result.roots.add(roots.peekLast());
159                 }
160 //            } else if (g.isInstanceOf(r, sr.Component)) {
161 //                // Complete components themselves
162 //                Resource componentType = g.getSingleType(r, sr.Component);
163 //                Resource composite = g.getPossibleObject(componentType, sr.IsDefinedBy);
164 //                Resource diagram = (composite != null && g.isInstanceOf(composite, sr.Composite)) ? g.getPossibleObject(composite, mr.CompositeToDiagram) : null;
165 //                String name = safeGetName(r);
166 //                System.out.println("Component root: " + name);
167 //                roots.add(new Node(null, name, diagram, composite, r));
168 //                result.roots.add(roots.peekLast());
169             } else if (g.isInheritedFrom(r, dr.DefinedElement)) {
170                 // Symbols
171                 Resource composite = g.getPossibleObject(r, sr.IsDefinedBy);
172                 composite = (composite != null && g.isInstanceOf(composite, dr.Composite)) ? composite : null;
173                 if (composite != null) {
174                     Resource componentType = g.getPossibleObject(r, mr.SymbolToComponentType);
175                     String name = safeGetName(componentType);
176                     name += " Symbol";
177                     if (DEBUG)
178                         System.out.println("Symbol root: " + name);
179                     
180                     {                 
181                         Node node = new Node(null, name, composite, r);
182                             roots.add(node);
183                             result.roots.add(roots.peekLast());
184                     }
185                 }
186 //            } else if (g.isInheritedFrom(r, sr.Component)) {
187 //                // Reusable component types
188 //                Resource composite = g.getPossibleObject(r, sr.IsDefinedBy);
189 //                Resource diagram = (composite != null && g.isInstanceOf(composite, sr.Composite)) ? g.getPossibleObject(composite, mr.CompositeToDiagram) : null;
190 //                String name = safeGetName(r);
191 //                System.out.println("Component type root: " + name);
192 //                roots.add(new Node(null, name, diagram, r, composite));
193 //                result.roots.add(roots.peekLast());
194             }
195         }
196
197         final SubMonitor mon = SubMonitor.convert(monitor);
198
199         g.syncRequest(new ReadRequest() {
200             @Override
201             public void run(ReadGraph graph) throws DatabaseException {
202                 for (Node node : roots) {
203                     loadComposites(graph, node);
204                 }
205             }
206
207             private void loadComposites(ReadGraph graph, final Node node) throws DatabaseException {
208                 Resource diagram = node.getDiagramResource();
209                 //System.out.println("loadComposites(" + diagram + ", " + node + ")");
210                 if (diagram != null) {
211                     result.addDiagram(diagram, node);
212 //                    node.setPartOfGroups(getPartOfGroups(diagram));
213                 }
214                 mon.setWorkRemaining(1000);
215
216                 for(Resource r : graph.getObjects(node.getDefiningResources().resources[0], l0.ConsistsOf)) {
217                         if(graph.isInstanceOf(r, sr.Composite)) {
218                                 String compositeName = graph.syncRequest(new GetName(r));
219                                 Resource definingDiagram = graph.getPossibleObject(r, mr.CompositeToDiagram);
220                         Node n = new Node(node, compositeName, definingDiagram, r);
221                         loadComposites(graph, n);
222                         mon.worked(1);
223                         } else if (graph.isInstanceOf(r, l0.Library)) {
224                                 String compositeName = graph.syncRequest(new GetName(r));
225                         Node n = new Node(node, compositeName, null, r);
226                         loadComposites(graph, n);
227                         mon.worked(1);
228                         } else if (graph.isInheritedFrom(r, sr.Component)) {
229                                 String name = safeGetName(r);
230                                 Node n = new Node(node, name, null, r);
231                                 loadComposites(graph, n);
232                                 mon.worked(1);
233                   }
234                 }
235                 
236             }
237
238         });
239
240         ILog log = Platform.getLog(Platform.getBundle(Plugin.PLUGIN_ID));
241
242         // Assign a page size description for each node.
243         for (Node node : result.diagramList) {
244             if (monitor.isCanceled())
245                 return null;
246             mon.setWorkRemaining(10000);
247
248             try {
249                 PageDesc realPageDesc = g.syncRequest(DiagramRequests.getPageDesc(node.getDiagramResource(), defaultPageDesc));
250                 node.setPageDesc(realPageDesc);
251             } catch (DatabaseException e) {
252                 log.log(new Status(IStatus.WARNING, Plugin.PLUGIN_ID,
253                         "Broken page description in diagram "
254                         + NameUtils.getURIOrSafeNameInternal(g, node.getDiagramResource())
255                         + ". Reopen the diagram to fix it.", e));
256             }
257
258             mon.worked(1);
259         }
260
261         if (monitor.isCanceled())
262             return null;
263
264         return result;
265     }
266     
267     static class GetName extends ResourceRead<String> {
268
269         public GetName(Resource resource) {
270             super(resource);
271         }
272
273         @Override
274         public String perform(ReadGraph graph) throws DatabaseException {
275             try {
276                 return NameLabelUtil.modalName(graph, resource);
277             } catch (AdaptionException e) {
278                 return NameUtils.getSafeName(graph, resource);
279             }
280         }
281
282     }
283
284 }