diff --git a/AlgebraicDataflowArchitectureModel/src/application/editor/Editor.java b/AlgebraicDataflowArchitectureModel/src/application/editor/Editor.java index 286471b..8480522 100644 --- a/AlgebraicDataflowArchitectureModel/src/application/editor/Editor.java +++ b/AlgebraicDataflowArchitectureModel/src/application/editor/Editor.java @@ -19,6 +19,7 @@ import models.dataFlowModel.DataFlowGraph; import models.dataFlowModel.DataTransferChannel; import models.dataFlowModel.DataTransferModel; +import models.dataFlowModel.ResourceNode; import models.visualModel.FormulaChannel; import parser.Parser; import parser.ParserDTRAM; @@ -28,6 +29,7 @@ import java.io.*; import java.util.ArrayList; import java.util.List; +import java.util.Set; public class Editor { @@ -270,47 +272,78 @@ * @return formatted "dtram" info texts. */ private String toOutputString() { - StringBuilder fileString = new StringBuilder(); + StringBuilder output = new StringBuilder(); - fileString.append("model {\n"); - fileString.append(model.getSourceText()); - fileString.append("}\n"); + // Model + output.append("model {\n"); + output.append(model.getSourceText()); + output.append("}\n"); - fileString.append("geometry {\n"); + // Geometry + output.append("geometry {\n"); + mxCell root = (mxCell) graph.getDefaultParent(); + buildGeometryOutput(output, root); + output.append("}\n"); - Object root = graph.getDefaultParent(); - for (int i = 0; i < graph.getModel().getChildCount(root); i++) { - Object cell = graph.getModel().getChildAt(root, i); - if (graph.getModel().isVertex(cell)) { - mxGraphView view = graph.getView(); - mxCellState state = view.getState(cell); - int x = (int) state.getX(); - int y = (int) state.getY(); - int w = (int) state.getWidth(); - int h = (int) state.getHeight(); + return output.toString(); + } - for (Channel ch : model.getChannels()) { - if (ch instanceof FormulaChannel && state.getLabel().equals(ch.getChannelName())) { - fileString.append("\tnode fc ").append(state.getLabel()).append(":").append(x).append(",").append(y).append(",").append(w).append(",").append(h).append("\n"); - } else if (ch != null && state.getLabel().equals(ch.getChannelName())) { - fileString.append("\tnode c ").append(state.getLabel()).append(":").append(x).append(",").append(y).append(",").append(w).append(",").append(h).append("\n"); - } - } + private void buildGeometryOutput(StringBuilder output, final mxCell cell) { + for (int i = 0; i < cell.getChildCount(); i++) { + mxCell child = (mxCell) cell.getChildAt(i); + if (!graph.getModel().isVertex(child)) { + continue; + } + mxGraphView view = graph.getView(); + mxCellState state = view.getState(child); + String identifier = state.getLabel(); + int x = (int) state.getX(); + int y = (int) state.getY(); + int w = (int) state.getWidth(); + int h = (int) state.getHeight(); - for (ResourcePath res : model.getResourcePaths()) { - if (res != null && state.getLabel().equals(res.getLeafResourceName())) - fileString.append("\tnode r ").append(state.getLabel()).append(":").append(x).append(",").append(y).append(",").append(w).append(",").append(h).append("\n"); - } + // Skip if current cell is not graph node + if (identifier == null || identifier.isEmpty()) { + continue; + } - for (Channel ioC : model.getInputChannels()) { - if (ioC != null && state.getLabel().equals(ioC.getChannelName())) { - fileString.append("\tnode ioc ").append(state.getLabel()).append(":").append(x).append(",").append(y).append(",").append(w).append(",").append(h).append("\n"); - } + // Search resource path matches to the identifier. If it exists, then build output string. + ResourcePath path = searchForResourcePath(identifier, model.getDataFlowGraph().getRootResourceNodes()); + if (path != null) { + output.append("\tnode r ").append(identifier).append(":").append(x).append(",").append(y).append(",").append(w).append(",").append(h).append("\n"); + } + + // Search channel matches to the identifier. If it exists, then build output string + for (Channel channel : model.getChannels()) { + if (channel instanceof FormulaChannel && identifier.equals(channel.getChannelName())) { + output.append("\tnode fc ").append(state.getLabel()).append(":").append(x).append(",").append(y).append(",").append(w).append(",").append(h).append("\n"); + } else if (channel != null && state.getLabel().equals(channel.getChannelName())) { + output.append("\tnode c ").append(state.getLabel()).append(":").append(x).append(",").append(y).append(",").append(w).append(",").append(h).append("\n"); } } + + // Search IO channel matches to the identifier. If it exists, then build output string + for (Channel ioChannel : model.getInputChannels()) { + if (ioChannel != null && identifier.equals(ioChannel.getChannelName())) { + String channelName = state.getLabel(); + output.append("\tnode ioc ").append(channelName).append(":").append(x).append(",").append(y).append(",").append(w).append(",").append(h).append("\n"); + } + } + + // Recursive + buildGeometryOutput(output, child); } - fileString.append("}\n"); - return fileString.toString(); + } + + private ResourcePath searchForResourcePath(final String identifier, final Set nodes) { + for (ResourceNode node : nodes) { + ResourcePath path = node.getPrimaryResourcePath(); + if (path != null && identifier.equals(path.getName())) { + return path; + } + return searchForResourcePath(identifier, node.getChildren()); + } + return null; } public void delete() {