-package org.simantics.modeling.ui.componentTypeEditor;\r
-\r
-import gnu.trove.map.hash.THashMap;\r
-\r
-import java.util.List;\r
-\r
-import org.eclipse.swt.SWT;\r
-import org.eclipse.swt.widgets.Composite;\r
-import org.simantics.Simantics;\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.layer0.variable.Variable;\r
-import org.simantics.db.layer0.variable.Variables;\r
-import org.simantics.db.procedure.Listener;\r
-import org.simantics.db.request.Read;\r
-import org.simantics.graphviz.Edge;\r
-import org.simantics.graphviz.Graph;\r
-import org.simantics.graphviz.Node;\r
-import org.simantics.graphviz.Record;\r
-import org.simantics.graphviz.ui.GraphvizComponent;\r
-import org.simantics.layer0.Layer0;\r
-import org.simantics.modeling.ModelingResources;\r
-import org.simantics.structural2.Functions;\r
-import org.simantics.structural2.procedural.Component;\r
-import org.simantics.structural2.procedural.Connection;\r
-import org.simantics.structural2.procedural.ConnectionPoint;\r
-import org.simantics.structural2.procedural.Interface;\r
-import org.simantics.structural2.procedural.Property;\r
-import org.simantics.structural2.procedural.SubstructureElement;\r
-import org.simantics.structural2.procedural.Terminal;\r
-import org.simantics.ui.workbench.ResourceEditorPart;\r
-import org.simantics.utils.datastructures.Pair;\r
-\r
-public class ProceduralComponentInstanceViewer extends ResourceEditorPart {\r
- \r
- GraphvizComponent graphviz;\r
- \r
- @Override\r
- public void createPartControl(Composite parent) {\r
- graphviz = new GraphvizComponent(parent, SWT.NONE);\r
- Simantics.getSession().asyncRequest(new Read<Graph>() {\r
-\r
- @Override\r
- public Graph perform(ReadGraph graph) throws DatabaseException {\r
- Resource inputResource = getInputResource();\r
- Variable context = Variables.getPossibleVariable(graph, inputResource);\r
- if(context == null)\r
- return createErrorGraph("Couldn't create variable for the resource.");\r
- try {\r
- List<SubstructureElement> proceduralDesc = \r
- Functions.getProceduralDesc(graph, context);\r
- if(proceduralDesc == null)\r
- return createErrorGraph("Component does not have a procedural substructure.");\r
- return createGraph(graph, proceduralDesc);\r
- } catch(DatabaseException e) {\r
- e.printStackTrace();\r
- return createErrorGraph(e.getMessage());\r
- }\r
- }\r
- \r
- }, new Listener<Graph>() {\r
-\r
- @Override\r
- public void execute(final Graph graph) {\r
- if(!graphviz.isDisposed())\r
- graphviz.getDisplay().asyncExec(new Runnable() {\r
-\r
- @Override\r
- public void run() {\r
- if(graphviz.isDisposed())\r
- return;\r
- if(graph != null)\r
- graphviz.setGraph(graph);\r
- }\r
- \r
- });\r
- }\r
-\r
- @Override\r
- public void exception(Throwable t) {\r
- t.printStackTrace();\r
- }\r
-\r
- @Override\r
- public boolean isDisposed() {\r
- return graphviz.isDisposed();\r
- }\r
- \r
- });\r
- }\r
-\r
- @Override\r
- public void setFocus() {\r
- graphviz.setFocus();\r
- }\r
- \r
- private static Graph createErrorGraph(String description) {\r
- Graph graph = new Graph();\r
- new Node(graph, description).setShape("rectangle");\r
- return graph;\r
- }\r
- \r
- private static String nameOf(ReadGraph g, Resource r) throws DatabaseException {\r
- return g.getRelatedValue(r, Layer0.getInstance(g).HasName);\r
- }\r
- \r
- private static Pair<Node, String> getCp(ReadGraph g, Graph graph, THashMap<String, Node> components, THashMap<Resource, Node> connectionPoints, ConnectionPoint cp) throws DatabaseException {\r
- if(cp instanceof Terminal) {\r
- Terminal terminal = (Terminal)cp;\r
- return Pair.make(components.get(terminal.component), nameOf(g, terminal.relation));\r
- }\r
- else {\r
- Interface interface_ = (Interface)cp;\r
- Node node = connectionPoints.get(interface_.relation);\r
- if(node == null) {\r
- node = new Node(graph);\r
- node.setShape("diamond");\r
- node.setLabel(nameOf(g, interface_.relation));\r
- connectionPoints.put(interface_.relation, node);\r
- }\r
- return Pair.make(node, null);\r
- }\r
- }\r
-\r
- private static Graph createGraph(ReadGraph g, List<SubstructureElement> proceduralDesc) throws DatabaseException {\r
- Graph graph = new Graph();\r
- graph.setRankdir("LR");\r
- THashMap<String, Node> components = new THashMap<String, Node>();\r
- for(SubstructureElement element : proceduralDesc)\r
- if(element instanceof Component) {\r
- Component component = (Component)element;\r
- Record record = new Record();\r
- record.add(component.name + " : " + nameOf(g, component.type));\r
- StringBuilder b = new StringBuilder();\r
- boolean first = true;\r
- for(Property property : component.properties) {\r
- if(first)\r
- first = false;\r
- else\r
- b.append("\\n");\r
- b.append(nameOf(g, property.relation) + " = " + property.value);\r
- }\r
- record.add(b.toString());\r
- components.put(component.name, record.toNode(graph));\r
- }\r
- THashMap<Resource, Node> connectionPoints = new THashMap<Resource, Node>();\r
- for(SubstructureElement element : proceduralDesc)\r
- if(element instanceof Connection) {\r
- Connection connection = (Connection)element;\r
- List<ConnectionPoint> cps = connection.connectionPoints;\r
- if(cps.size() == 2) {\r
- Pair<Node, String> cp1 = getCp(g, graph, components, connectionPoints, cps.get(0));\r
- Pair<Node, String> cp2 = getCp(g, graph, components, connectionPoints, cps.get(1));\r
- Edge edge = new Edge(cp1.first, cp2.first);\r
- if(cp1.second != null) {\r
- if(cp2.second != null)\r
- edge.setLabel(cp1.second + "-" + cp2.second);\r
- else\r
- edge.setLabel(cp1.second);\r
- }\r
- else {\r
- if(cp2.second != null)\r
- edge.setLabel(cp2.second);\r
- }\r
- }\r
- else {\r
- Node p = new Node(graph);\r
- p.setShape("point");\r
- boolean first = true;\r
- for(ConnectionPoint cp : cps) {\r
- Pair<Node, String> cp1 = getCp(g, graph, components, connectionPoints, cp);\r
- if(first) {\r
- Edge edge = new Edge(cp1.first, p);\r
- edge.setLabel(cp1.second);\r
- first = false;\r
- }\r
- else {\r
- Edge edge = new Edge(p, cp1.first);\r
- edge.setLabel(cp1.second);\r
- }\r
- }\r
- }\r
- }\r
- return graph;\r
- }\r
-\r
-}\r
+package org.simantics.modeling.ui.componentTypeEditor;
+
+import gnu.trove.map.hash.THashMap;
+
+import java.util.List;
+
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.widgets.Composite;
+import org.simantics.Simantics;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.variable.Variable;
+import org.simantics.db.layer0.variable.Variables;
+import org.simantics.db.procedure.Listener;
+import org.simantics.db.request.Read;
+import org.simantics.graphviz.Edge;
+import org.simantics.graphviz.Graph;
+import org.simantics.graphviz.Node;
+import org.simantics.graphviz.Record;
+import org.simantics.graphviz.ui.GraphvizComponent;
+import org.simantics.layer0.Layer0;
+import org.simantics.modeling.ModelingResources;
+import org.simantics.structural2.Functions;
+import org.simantics.structural2.procedural.Component;
+import org.simantics.structural2.procedural.Connection;
+import org.simantics.structural2.procedural.ConnectionPoint;
+import org.simantics.structural2.procedural.Interface;
+import org.simantics.structural2.procedural.Property;
+import org.simantics.structural2.procedural.SubstructureElement;
+import org.simantics.structural2.procedural.Terminal;
+import org.simantics.ui.workbench.ResourceEditorPart;
+import org.simantics.utils.datastructures.Pair;
+
+public class ProceduralComponentInstanceViewer extends ResourceEditorPart {
+
+ GraphvizComponent graphviz;
+
+ @Override
+ public void createPartControl(Composite parent) {
+ graphviz = new GraphvizComponent(parent, SWT.NONE);
+ Simantics.getSession().asyncRequest(new Read<Graph>() {
+
+ @Override
+ public Graph perform(ReadGraph graph) throws DatabaseException {
+ Resource inputResource = getInputResource();
+ Variable context = Variables.getPossibleVariable(graph, inputResource);
+ if(context == null)
+ return createErrorGraph("Couldn't create variable for the resource.");
+ try {
+ List<SubstructureElement> proceduralDesc =
+ Functions.getProceduralDesc(graph, context);
+ if(proceduralDesc == null)
+ return createErrorGraph("Component does not have a procedural substructure.");
+ return createGraph(graph, proceduralDesc);
+ } catch(DatabaseException e) {
+ e.printStackTrace();
+ return createErrorGraph(e.getMessage());
+ }
+ }
+
+ }, new Listener<Graph>() {
+
+ @Override
+ public void execute(final Graph graph) {
+ if(!graphviz.isDisposed())
+ graphviz.getDisplay().asyncExec(new Runnable() {
+
+ @Override
+ public void run() {
+ if(graphviz.isDisposed())
+ return;
+ if(graph != null)
+ graphviz.setGraph(graph);
+ }
+
+ });
+ }
+
+ @Override
+ public void exception(Throwable t) {
+ t.printStackTrace();
+ }
+
+ @Override
+ public boolean isDisposed() {
+ return graphviz.isDisposed();
+ }
+
+ });
+ }
+
+ @Override
+ public void setFocus() {
+ graphviz.setFocus();
+ }
+
+ private static Graph createErrorGraph(String description) {
+ Graph graph = new Graph();
+ new Node(graph, description).setShape("rectangle");
+ return graph;
+ }
+
+ private static String nameOf(ReadGraph g, Resource r) throws DatabaseException {
+ return g.getRelatedValue(r, Layer0.getInstance(g).HasName);
+ }
+
+ private static Pair<Node, String> getCp(ReadGraph g, Graph graph, THashMap<String, Node> components, THashMap<Resource, Node> connectionPoints, ConnectionPoint cp) throws DatabaseException {
+ if(cp instanceof Terminal) {
+ Terminal terminal = (Terminal)cp;
+ return Pair.make(components.get(terminal.component), nameOf(g, terminal.relation));
+ }
+ else {
+ Interface interface_ = (Interface)cp;
+ Node node = connectionPoints.get(interface_.relation);
+ if(node == null) {
+ node = new Node(graph);
+ node.setShape("diamond");
+ node.setLabel(nameOf(g, interface_.relation));
+ connectionPoints.put(interface_.relation, node);
+ }
+ return Pair.make(node, null);
+ }
+ }
+
+ private static Graph createGraph(ReadGraph g, List<SubstructureElement> proceduralDesc) throws DatabaseException {
+ Graph graph = new Graph();
+ graph.setRankdir("LR");
+ THashMap<String, Node> components = new THashMap<String, Node>();
+ for(SubstructureElement element : proceduralDesc)
+ if(element instanceof Component) {
+ Component component = (Component)element;
+ Record record = new Record();
+ record.add(component.name + " : " + nameOf(g, component.type));
+ StringBuilder b = new StringBuilder();
+ boolean first = true;
+ for(Property property : component.properties) {
+ if(first)
+ first = false;
+ else
+ b.append("\\n");
+ b.append(nameOf(g, property.relation) + " = " + property.value);
+ }
+ record.add(b.toString());
+ components.put(component.name, record.toNode(graph));
+ }
+ THashMap<Resource, Node> connectionPoints = new THashMap<Resource, Node>();
+ for(SubstructureElement element : proceduralDesc)
+ if(element instanceof Connection) {
+ Connection connection = (Connection)element;
+ List<ConnectionPoint> cps = connection.connectionPoints;
+ if(cps.size() == 2) {
+ Pair<Node, String> cp1 = getCp(g, graph, components, connectionPoints, cps.get(0));
+ Pair<Node, String> cp2 = getCp(g, graph, components, connectionPoints, cps.get(1));
+ Edge edge = new Edge(cp1.first, cp2.first);
+ if(cp1.second != null) {
+ if(cp2.second != null)
+ edge.setLabel(cp1.second + "-" + cp2.second);
+ else
+ edge.setLabel(cp1.second);
+ }
+ else {
+ if(cp2.second != null)
+ edge.setLabel(cp2.second);
+ }
+ }
+ else {
+ Node p = new Node(graph);
+ p.setShape("point");
+ boolean first = true;
+ for(ConnectionPoint cp : cps) {
+ Pair<Node, String> cp1 = getCp(g, graph, components, connectionPoints, cp);
+ if(first) {
+ Edge edge = new Edge(cp1.first, p);
+ edge.setLabel(cp1.second);
+ first = false;
+ }
+ else {
+ Edge edge = new Edge(p, cp1.first);
+ edge.setLabel(cp1.second);
+ }
+ }
+ }
+ }
+ return graph;
+ }
+
+}