1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2012 Association for Decentralized Information Management in
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.sysdyn.ui.structure;
\r
14 import java.util.HashMap;
\r
15 import java.util.HashSet;
\r
17 import org.simantics.db.ReadGraph;
\r
18 import org.simantics.db.Resource;
\r
19 import org.simantics.db.common.request.PossibleObjectWithType;
\r
20 import org.simantics.db.common.utils.NameUtils;
\r
21 import org.simantics.db.exception.DatabaseException;
\r
22 import org.simantics.db.request.Read;
\r
23 import org.simantics.graphviz.Edge;
\r
24 import org.simantics.graphviz.Graph;
\r
25 import org.simantics.graphviz.Node;
\r
26 import org.simantics.layer0.Layer0;
\r
27 import org.simantics.sysdyn.SysdynResource;
\r
30 * Builds a graph about the modular structure of the model where a selected object is located.
\r
32 * @author Teemu Lempinen
\r
35 public class ModuleStructureGraphRequest implements Read<Graph> {
\r
37 private Resource resource;
\r
40 * Request a hierarchical graph about the model of resource
\r
43 public ModuleStructureGraphRequest(Resource resource) {
\r
44 this.resource = resource;
\r
48 public Graph perform(ReadGraph graph) throws DatabaseException {
\r
49 SysdynResource sr = SysdynResource.getInstance(graph);
\r
50 Layer0 l0 = Layer0.getInstance(graph);
\r
52 Graph g = new Graph();
\r
55 // Find model resource, the root of this
\r
56 Resource model = resource;
\r
57 while(model != null && !graph.isInstanceOf(model, sr.SysdynModel)) {
\r
58 model = graph.getPossibleObject(model, l0.PartOf);
\r
61 // Model root was not found, return empty graph
\r
66 Node rootNode = new Node(g, NameUtils.getSafeLabel(graph, model));
\r
67 rootNode.setShape("rectangle");
\r
68 HashSet<Resource> visited = new HashSet<Resource>();
\r
70 findChildModules(g, rootNode, graph, model, visited);
\r
77 * Recursive call for finding child modules
\r
78 * @param g GraphViz graph
\r
79 * @param parent Parent module or model
\r
80 * @param graph ReadGraph
\r
81 * @param resource Module type or model
\r
82 * @param visited All visited modules. Needed to check for loops in the structure. Loops are not allowed.
\r
83 * @throws DatabaseException
\r
85 private void findChildModules(Graph g, Node parent, ReadGraph graph, Resource resource, HashSet<Resource> visited) throws DatabaseException {
\r
86 SysdynResource sr = SysdynResource.getInstance(graph);
\r
87 Layer0 l0 = Layer0.getInstance(graph);
\r
89 HashMap<Resource, Integer> modules = new HashMap<Resource, Integer>();
\r
91 // Find all module children
\r
92 Resource configuration = graph.syncRequest(new PossibleObjectWithType(resource, l0.ConsistsOf, sr.Configuration));
\r
93 for(Resource m : graph.getObjects(configuration, l0.ConsistsOf)) {
\r
94 Resource type = graph.getPossibleObject(m, l0.InstanceOf);
\r
95 if(graph.isInheritedFrom(type, sr.Module)) {
\r
96 if(!modules.containsKey(type))
\r
97 modules.put(type, 0);
\r
98 modules.put(type, modules.get(type) + 1);
\r
102 // Display module children in graph
\r
103 for(Resource type : modules.keySet()) {
\r
105 Node node = new Node(g, NameUtils.getSafeName(graph, type));
\r
106 node.setShape("rectangle");
\r
107 Edge edge = new Edge(g, parent, node);
\r
108 edge.set("labeldistance", "1.5");
\r
109 edge.set("labelfontsize", "7");
\r
110 edge.setFontColor("#4f4f4f");
\r
111 if(modules.get(type) > 1)
\r
112 edge.setHeadLabel(modules.get(type).toString());
\r
114 if(visited.contains(type)) {
\r
115 // Found a loop. Stop recursive call and display error.
\r
116 edge.setFontColor("#FF0000");
\r
117 edge.setColor("#FF000");
\r
118 edge.setLabel("Error: loop");
\r
119 node.setColor("#FF0000");
\r
120 node.setFontColor("#FF0000");
\r
124 findChildModules(g, node, graph, type, visited);
\r