1 /*******************************************************************************
2 * Copyright (c) 2007, 2017 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.modeling.requests;
14 import java.util.ArrayDeque;
15 import java.util.Deque;
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;
42 * If the specified progress monitor indicates cancellation, the request will
43 * return <code>null</code>.
45 * @author Tuukka Lehtonen
47 public class CollectionRequest implements Read<CollectionResult> {
49 private static final boolean DEBUG = false;
51 IProgressMonitor monitor;
52 PageDesc defaultPageDesc;
56 StructuralResource2 sr;
59 SimulationResource SIMU;
61 public CollectionRequest(IProgressMonitor monitor, PageDesc defaultPageDesc, Resource... input) {
62 this.monitor = monitor != null ? monitor : new NullProgressMonitor();
63 this.defaultPageDesc = defaultPageDesc;
67 String safeGetName(Resource r) throws DatabaseException {
68 return g.syncRequest(new GetName(r));
72 public CollectionResult perform(ReadGraph g) throws DatabaseException {
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);
81 final CollectionResult result = new CollectionResult();
82 final Deque<Node> roots = new ArrayDeque<>();
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);
89 result.roots.add(roots.peekLast());
90 } else if (g.isInstanceOf(r, sr.Composite)) {
91 // The contents of components
93 Resource model = g.getPossibleObject(r, SIMU.IsConfigurationOf);
95 name = safeGetName(model);
97 System.out.println("Configuration root: " + name);
99 name = safeGetName(r);
101 System.out.println("Composite root: " + name);
104 Resource diagram = g.getPossibleObject(r, mr.CompositeToDiagram);
105 diagram = (diagram != null && g.isInstanceOf(diagram, dr.Composite)) ? diagram : null;
108 Node node = new Node(null, name, diagram, r);
110 result.roots.add(roots.peekLast());
112 } else if (g.isInheritedFrom(r, dr.DefinedElement)) {
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);
121 System.out.println("Symbol root: " + name);
124 Node node = new Node(null, name, composite, r);
126 result.roots.add(roots.peekLast());
132 final SubMonitor mon = SubMonitor.convert(monitor);
134 g.syncRequest(new ReadRequest() {
136 public void run(ReadGraph graph) throws DatabaseException {
137 for (Node node : roots) {
138 loadComposites(graph, node);
142 private void loadComposites(ReadGraph graph, final Node node) throws DatabaseException {
143 Resource diagram = node.getDiagramResource();
145 System.out.println("loadComposites(" + diagram + ", " + node + ")");
147 result.addDiagram(diagram, node);
148 mon.setWorkRemaining(1000);
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);
156 System.out.println("Found composite: " + n);
157 loadComposites(graph, n);
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);
163 System.out.println("Found library: " + n);
164 loadComposites(graph, n);
166 } else if (graph.isInheritedFrom(r, sr.Component)) {
167 Resource definedBy = graph.getPossibleObject(r, sr.IsDefinedBy);
168 if (definedBy == null)
170 String name = safeGetName(r);
171 Node n = new Node(node, name, null, r);
173 System.out.println("Found component: " + n);
174 loadComposites(graph, n);
181 ILog log = Platform.getLog(Platform.getBundle(Plugin.PLUGIN_ID));
183 // Assign a page size description for each node.
184 for (Node node : result.diagramList) {
185 if (monitor.isCanceled())
187 mon.setWorkRemaining(10000);
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));
202 if (monitor.isCanceled())
208 static class GetName extends ResourceRead<String> {
209 public GetName(Resource resource) {
214 public String perform(ReadGraph graph) throws DatabaseException {
216 return NameLabelUtil.modalName(graph, resource);
217 } catch (DatabaseException e) {
218 return NameUtils.getSafeName(graph, resource);