X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.modeling.ui%2Fsrc%2Forg%2Fsimantics%2Fmodeling%2Fui%2FcomponentTypeEditor%2FProceduralComponentInstanceViewer.java;h=d7abea5dabb4f37e64a20d9515e080000a8bcbb6;hp=74040e42801d04879a0904dad8f763546f8ed326;hb=fd2a6ff5d708baed2bdbaf94e6cd7bcc7b288069;hpb=969bd23cab98a79ca9101af33334000879fb60c5 diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/componentTypeEditor/ProceduralComponentInstanceViewer.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/componentTypeEditor/ProceduralComponentInstanceViewer.java index 74040e428..d7abea5da 100644 --- a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/componentTypeEditor/ProceduralComponentInstanceViewer.java +++ b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/componentTypeEditor/ProceduralComponentInstanceViewer.java @@ -1,188 +1,188 @@ -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() { - - @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 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() { - - @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 getCp(ReadGraph g, Graph graph, THashMap components, THashMap 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 proceduralDesc) throws DatabaseException { - Graph graph = new Graph(); - graph.setRankdir("LR"); - THashMap components = new THashMap(); - 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 connectionPoints = new THashMap(); - for(SubstructureElement element : proceduralDesc) - if(element instanceof Connection) { - Connection connection = (Connection)element; - List cps = connection.connectionPoints; - if(cps.size() == 2) { - Pair cp1 = getCp(g, graph, components, connectionPoints, cps.get(0)); - Pair 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 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; - } - -} +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() { + + @Override + public Graph perform(ReadGraph graph) throws DatabaseException { + Resource inputResource = getInputResource(); + Variable context = Variables.getPossibleVariable(graph, inputResource); + if(context == null) + return createErrorGraph(Messages.ProceduralComponentInstanceViewer_CouldnotCreateVariableForResource); + try { + List proceduralDesc = + Functions.getProceduralDesc(graph, context); + if(proceduralDesc == null) + return createErrorGraph(Messages.ProceduralComponentInstanceViewer_NoProceduralSubstructure); + return createGraph(graph, proceduralDesc); + } catch(DatabaseException e) { + e.printStackTrace(); + return createErrorGraph(e.getMessage()); + } + } + + }, new Listener() { + + @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"); //$NON-NLS-1$ + return graph; + } + + private static String nameOf(ReadGraph g, Resource r) throws DatabaseException { + return g.getRelatedValue(r, Layer0.getInstance(g).HasName); + } + + private static Pair getCp(ReadGraph g, Graph graph, THashMap components, THashMap 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"); //$NON-NLS-1$ + node.setLabel(nameOf(g, interface_.relation)); + connectionPoints.put(interface_.relation, node); + } + return Pair.make(node, null); + } + } + + private static Graph createGraph(ReadGraph g, List proceduralDesc) throws DatabaseException { + Graph graph = new Graph(); + graph.setRankdir("LR"); //$NON-NLS-1$ + THashMap components = new THashMap(); + for(SubstructureElement element : proceduralDesc) + if(element instanceof Component) { + Component component = (Component)element; + Record record = new Record(); + record.add(component.name + " : " + nameOf(g, component.type)); //$NON-NLS-1$ + StringBuilder b = new StringBuilder(); + boolean first = true; + for(Property property : component.properties) { + if(first) + first = false; + else + b.append("\\n"); //$NON-NLS-1$ + b.append(nameOf(g, property.relation) + " = " + property.value); //$NON-NLS-1$ + } + record.add(b.toString()); + components.put(component.name, record.toNode(graph)); + } + THashMap connectionPoints = new THashMap(); + for(SubstructureElement element : proceduralDesc) + if(element instanceof Connection) { + Connection connection = (Connection)element; + List cps = connection.connectionPoints; + if(cps.size() == 2) { + Pair cp1 = getCp(g, graph, components, connectionPoints, cps.get(0)); + Pair 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); //$NON-NLS-1$ + else + edge.setLabel(cp1.second); + } + else { + if(cp2.second != null) + edge.setLabel(cp2.second); + } + } + else { + Node p = new Node(graph); + p.setShape("point"); //$NON-NLS-1$ + boolean first = true; + for(ConnectionPoint cp : cps) { + Pair 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; + } + +}