diff --git a/AlgebraicDataflowArchitectureModel/src/application/SimulationLayout.java b/AlgebraicDataflowArchitectureModel/src/application/SimulationLayout.java new file mode 100644 index 0000000..0e0cdec --- /dev/null +++ b/AlgebraicDataflowArchitectureModel/src/application/SimulationLayout.java @@ -0,0 +1,157 @@ +package application; + +import com.mxgraph.model.mxGraphModel; +import com.mxgraph.view.mxGraph; +import models.dataConstraintModel.ResourcePath; +import simulator.Resource; +import simulator.ResourceIdentifier; +import simulator.Simulator; +import simulator.SystemState; + +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; + +public class SimulationLayout { + // Hold the division level for each resource + HashMap divisionLevelMap = new HashMap<>(); + // Hold the scaling factor for each resource + HashMap resourceScaleMap = new HashMap<>(); + int maxDivisionLevel = 1; + public final double BASE_WIDTH = 720; + public final double BASE_HEIGHT = BASE_WIDTH/2; + public final double MARGIN_SCALE = 0.92; + SystemState systemState; + public SimulationLayout(SystemState systemState){ + this.systemState = systemState; + // Calculate the division level for each resource + for (Resource res:systemState.getRootResources()) { + setDivisionLevel(divisionLevelMap, res.getResourceIdentifier(), 1); + } + // Calculate the scaling factor for each resource + for (ResourceIdentifier res:divisionLevelMap.keySet()) { + double scale = (double) maxDivisionLevel / divisionLevelMap.get(res); + resourceScaleMap.put(res, scale); + } + } + public double getScale(ResourceIdentifier resource){ + return resourceScaleMap.get(resource); + } + public double getDivision(ResourceIdentifier resource){ + return divisionLevelMap.get(resource); + } + + public int getMaxDivisionLevel() { + return maxDivisionLevel; + } + + public boolean isExistResource(ResourceIdentifier resourceIdentifier){ + return divisionLevelMap.containsKey(resourceIdentifier); + } + private void setDivisionLevel(HashMap resourceSet, ResourceIdentifier resource, int childCount){ + int divisionLevel; + if(resourceSet.get(resource.getParent()) == null){ + divisionLevel = 1; + }else{ + divisionLevel = resourceSet.get(resource.getParent()) * childCount; + } + if (divisionLevel > maxDivisionLevel)maxDivisionLevel = divisionLevel; + resourceSet.put(resource,divisionLevel); + + Collection identifiers = systemState.getResource(resource).getChildren(); + if (identifiers != null) { + for (Resource child:identifiers) { + setDivisionLevel(resourceSet, child.getResourceIdentifier(), identifiers.size()); + } + } + } + /** + * + * Draw an object corresponding to the target resource on the mxGraph. + * @param graph The mxGraph for drawing. + * @param parent The parent object of the target resource. + * @param resourceIdentifier The ID of the target resource. + * @param index The index. + * @param length The number of sibling resources of the target resource. + * @return The drawing object of the target resource. + */ + + private Object setLayout(mxGraph graph,Object parent, ResourceIdentifier resourceIdentifier,double index, double length){ + double width, height, x, y; + double parentWidth = graph.getCellGeometry(parent).getWidth(); + double parentHeight = graph.getCellGeometry(parent).getHeight(); + + width = parentWidth / length * MARGIN_SCALE; + height = width/2; + x = parentWidth * (index - 1)/length + (parentWidth/length)*(1-MARGIN_SCALE)/2; + + // Process to avoid hiding the parent's resource name + if ((int)length == 1) { + y = 20; + height -=20; + } else { + y = parentHeight/2 - height/2; + } + + Object result = graph.insertVertex(parent, null, + resourceIdentifier.toString(), x, y, width, height, + "shape=ellipse;perimeter=ellipsePerimeter;verticalAlign=top;"); + System.out.println(result); + return result; + } + + public mxGraph constructSimulateGraph(mxGraph graph, Simulator simulator){ + ((mxGraphModel) graph.getModel()).clear(); + Object parent = graph.getDefaultParent(); + graph.getModel().beginUpdate(); + + try { + Map resources = new HashMap<>(); + int i = 0; + int childCount = simulator.getCurState().getRootResources().size(); + // create resource vertices + for (Resource resNode: simulator.getCurState().getRootResources()) { + double scale = this.getScale(resNode.getResourceIdentifier()); + double maxDiv = this.getMaxDivisionLevel(); + double w = this.BASE_WIDTH * scale / maxDiv / childCount; + double h = this.BASE_HEIGHT * scale / maxDiv / childCount; + double x = w * i + 50; + double y = 20 ; + ResourcePath res = resNode.getResourceIdentifier(); + Object resource = graph.insertVertex(parent, null, + res.toString(), x, y, w, h, + "shape=ellipse;perimeter=ellipsePerimeter;verticalAlign=top;"); // insert a resource as a vertex + resources.put(resNode, resource); + i++; + createNextChildSimulateResourceVerticies(graph, resource, resNode, resources); + + } + + } finally { + graph.getModel().endUpdate(); + } + + return graph; + } + private void createNextChildSimulateResourceVerticies(mxGraph graph, Object parent, Resource resNode, Map resources) { //sample + Collection children = resNode.getChildren(); + if (children != null) { + //List children = resNode.getChildren(); + double i = 1; + double childCount = children.size(); + for (Resource childNode: children) { + Object childResource = setLayout(graph, parent, childNode.getResourceIdentifier(), i, childCount); + resources.put(childNode, childResource); + i++; + createNextChildSimulateResourceVerticies(graph, childResource, childNode, resources); + + } + } + if (children == null || children.size() == 0) { + Object state = graph.insertVertex(parent, null, + resNode.getState().getValue().toString(), 0.5, 0.5, 0.25, 0.25, "opacity=0;verticalAlign=down;", true); // insert a state label as a vertex + } + } +} diff --git a/AlgebraicDataflowArchitectureModel/src/application/SimulatorMenuBar.java b/AlgebraicDataflowArchitectureModel/src/application/SimulatorMenuBar.java new file mode 100644 index 0000000..25db4a3 --- /dev/null +++ b/AlgebraicDataflowArchitectureModel/src/application/SimulatorMenuBar.java @@ -0,0 +1,27 @@ +package application; + +import javax.swing.JMenu; +import javax.swing.JMenuBar; + +import application.actions.ShowUISimulatorAction; +import application.actions.ZoomInAction; +import application.actions.ZoomOutAction; + +public class SimulatorMenuBar extends JMenuBar { + private static final long serialVersionUID = 7769410930379012970L; + + private SimulatorWindow simulatorWindow = null; + + public SimulatorMenuBar(SimulatorWindow simulatorWindow) { + this.simulatorWindow = simulatorWindow; + + JMenu menu = null; + menu = add(new JMenu("View")); + menu.add(new ZoomInAction(simulatorWindow.getGraphComponent())); + menu.add(new ZoomOutAction(simulatorWindow.getGraphComponent())); + + menu = add(new JMenu("Show")); + menu.add(new ShowUISimulatorAction(simulatorWindow.getSimulator())); + } + +} diff --git a/AlgebraicDataflowArchitectureModel/src/application/SimulatorWindow.java b/AlgebraicDataflowArchitectureModel/src/application/SimulatorWindow.java index a8b4281..668090f 100644 --- a/AlgebraicDataflowArchitectureModel/src/application/SimulatorWindow.java +++ b/AlgebraicDataflowArchitectureModel/src/application/SimulatorWindow.java @@ -12,7 +12,6 @@ import javax.swing.JFrame; -import application.editor.SimulationLayout; import com.mxgraph.model.mxCell; import com.mxgraph.model.mxGeometry; import com.mxgraph.model.mxGraphModel; @@ -71,17 +70,17 @@ private Editor editor = null; private mxGraph graph = null; private mxGraphComponent graphComponent = null; - private UIWindow uiWindow; private Simulator simulator = null; private boolean bReflectingArchitectureModel = false; private double x = 20; private double y = 20; + private SimulatorMenuBar menuBar; public SimulatorWindow(Editor editor) { setTitle(title); - setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); this.graph = new mxGraph() { public boolean isPort(Object cell) { @@ -134,7 +133,8 @@ layout.constructSimulateGraph(graph, simulator); graphComponent.setCellEditor(new InputEventCellEditor(graphComponent, simulator, this.editor, graph)); - uiWindow = new UIWindow(simulator); + menuBar = new SimulatorMenuBar(this); + setJMenuBar(menuBar); } public mxGraph constructSimulateGraph(Set simulateRes, DataTransferModel model, DataFlowGraph dataFlowGraph) { @@ -198,4 +198,8 @@ this.editor = editor; } + public Simulator getSimulator() { + return simulator; + } + } diff --git a/AlgebraicDataflowArchitectureModel/src/application/UISimulatorWindow.java b/AlgebraicDataflowArchitectureModel/src/application/UISimulatorWindow.java new file mode 100644 index 0000000..b0ffa3c --- /dev/null +++ b/AlgebraicDataflowArchitectureModel/src/application/UISimulatorWindow.java @@ -0,0 +1,26 @@ +package application; + +import javax.swing.JFrame; +import javax.swing.JPanel; + +import simulator.Simulator; +import simulator.interfaces.swing.SwingPresenter; + +public class UISimulatorWindow extends JFrame { + + private static final long serialVersionUID = 1770206525826167136L; + private SwingPresenter presenter; + private Simulator simulator; + private JPanel mainPanel; + + public UISimulatorWindow(Simulator simulator) { + setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); + this.simulator = simulator; + mainPanel = new JPanel(); + presenter = new SwingPresenter(mainPanel, simulator); + this.add(mainPanel); + + setSize(870,640); + setVisible(true); + } +} diff --git a/AlgebraicDataflowArchitectureModel/src/application/UIWindow.java b/AlgebraicDataflowArchitectureModel/src/application/UIWindow.java deleted file mode 100644 index 97ca1e3..0000000 --- a/AlgebraicDataflowArchitectureModel/src/application/UIWindow.java +++ /dev/null @@ -1,26 +0,0 @@ -package application; - -import javax.swing.JFrame; -import javax.swing.JPanel; - -import simulator.Simulator; -import simulator.interfaces.swing.SwingPresenter; - -public class UIWindow extends JFrame { - - private static final long serialVersionUID = 1770206525826167136L; - private SwingPresenter presenter; - private Simulator simulator; - private JPanel mainPanel; - - public UIWindow(Simulator simulator) { - setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - this.simulator = simulator; - mainPanel = new JPanel(); - presenter = new SwingPresenter(mainPanel, simulator); - this.add(mainPanel); - - setSize(870,640); - setVisible(true); - } -} diff --git a/AlgebraicDataflowArchitectureModel/src/application/actions/ShowUISimulatorAction.java b/AlgebraicDataflowArchitectureModel/src/application/actions/ShowUISimulatorAction.java new file mode 100644 index 0000000..00f25fa --- /dev/null +++ b/AlgebraicDataflowArchitectureModel/src/application/actions/ShowUISimulatorAction.java @@ -0,0 +1,23 @@ +package application.actions; + +import java.awt.event.ActionEvent; + +import javax.swing.AbstractAction; + +import application.UISimulatorWindow; +import simulator.Simulator; + +public class ShowUISimulatorAction extends AbstractAction { + private Simulator simulator; + + public ShowUISimulatorAction(Simulator simulator) { + super("Show UI Simulator"); + this.simulator = simulator; + } + + @Override + public void actionPerformed(ActionEvent e) { + new UISimulatorWindow(simulator); + } + +} diff --git a/AlgebraicDataflowArchitectureModel/src/application/editor/InputEventCellEditor.java b/AlgebraicDataflowArchitectureModel/src/application/editor/InputEventCellEditor.java index ea39086..7caa806 100644 --- a/AlgebraicDataflowArchitectureModel/src/application/editor/InputEventCellEditor.java +++ b/AlgebraicDataflowArchitectureModel/src/application/editor/InputEventCellEditor.java @@ -31,6 +31,7 @@ import com.mxgraph.view.mxGraph; import com.mxgraph.view.mxGraphView; +import application.SimulationLayout; import application.SimulatorWindow; import application.layouts.DAGLayout; import models.algebra.Expression; diff --git a/AlgebraicDataflowArchitectureModel/src/application/editor/SimulationLayout.java b/AlgebraicDataflowArchitectureModel/src/application/editor/SimulationLayout.java deleted file mode 100644 index 99bbdff..0000000 --- a/AlgebraicDataflowArchitectureModel/src/application/editor/SimulationLayout.java +++ /dev/null @@ -1,157 +0,0 @@ -package application.editor; - -import com.mxgraph.model.mxGraphModel; -import com.mxgraph.view.mxGraph; -import models.dataConstraintModel.ResourcePath; -import simulator.Resource; -import simulator.ResourceIdentifier; -import simulator.Simulator; -import simulator.SystemState; - -import java.util.Collection; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Set; - -public class SimulationLayout { - // Hold the division level for each resource - HashMap divisionLevelMap = new HashMap<>(); - // Hold the scaling factor for each resource - HashMap resourceScaleMap = new HashMap<>(); - int maxDivisionLevel = 1; - public final double BASE_WIDTH = 400; - public final double BASE_HEIGHT = BASE_WIDTH/2; - public final double MARGIN_SCALE = 0.92; - SystemState systemState; - public SimulationLayout(SystemState systemState){ - this.systemState = systemState; - // Calculate the division level for each resource - for (Resource res:systemState.getRootResources()) { - setDivisionLevel(divisionLevelMap, res.getResourceIdentifier(), 1); - } - // Calculate the scaling factor for each resource - for (ResourceIdentifier res:divisionLevelMap.keySet()) { - double scale = (double) maxDivisionLevel / divisionLevelMap.get(res); - resourceScaleMap.put(res, scale); - } - } - public double getScale(ResourceIdentifier resource){ - return resourceScaleMap.get(resource); - } - public double getDivision(ResourceIdentifier resource){ - return divisionLevelMap.get(resource); - } - - public int getMaxDivisionLevel() { - return maxDivisionLevel; - } - - public boolean isExistResource(ResourceIdentifier resourceIdentifier){ - return divisionLevelMap.containsKey(resourceIdentifier); - } - private void setDivisionLevel(HashMap resourceSet, ResourceIdentifier resource, int childCount){ - int divisionLevel; - if(resourceSet.get(resource.getParent()) == null){ - divisionLevel = 1; - }else{ - divisionLevel = resourceSet.get(resource.getParent()) * childCount; - } - if (divisionLevel > maxDivisionLevel)maxDivisionLevel = divisionLevel; - resourceSet.put(resource,divisionLevel); - - Collection identifiers = systemState.getResource(resource).getChildren(); - if (identifiers != null) { - for (Resource child:identifiers) { - setDivisionLevel(resourceSet, child.getResourceIdentifier(), identifiers.size()); - } - } - } - /** - * - * Draw an object corresponding to the target resource on the mxGraph. - * @param graph The mxGraph for drawing. - * @param parent The parent object of the target resource. - * @param resourceIdentifier The ID of the target resource. - * @param index The index. - * @param length The number of sibling resources of the target resource. - * @return The drawing object of the target resource. - */ - - private Object setLayout(mxGraph graph,Object parent, ResourceIdentifier resourceIdentifier,double index, double length){ - double width, height, x, y; - double parentWidth = graph.getCellGeometry(parent).getWidth(); - double parentHeight = graph.getCellGeometry(parent).getHeight(); - - width = parentWidth / length * MARGIN_SCALE; - height = width/2; - x = parentWidth * (index - 1)/length + (parentWidth/length)*(1-MARGIN_SCALE)/2; - - // Process to avoid hiding the parent's resource name - if ((int)length == 1) { - y = 20; - height -=20; - } else { - y = parentHeight/2 - height/2; - } - - Object result = graph.insertVertex(parent, null, - resourceIdentifier.toString(), x, y, width, height, - "shape=ellipse;perimeter=ellipsePerimeter;verticalAlign=top;"); - System.out.println(result); - return result; - } - - public mxGraph constructSimulateGraph(mxGraph graph, Simulator simulator){ - ((mxGraphModel) graph.getModel()).clear(); - Object parent = graph.getDefaultParent(); - graph.getModel().beginUpdate(); - - try { - Map resources = new HashMap<>(); - int i = 0; - // create resource vertices - for (Resource resNode: simulator.getCurState().getRootResources()) { - double scale = this.getScale(resNode.getResourceIdentifier()); - double maxDiv = this.getMaxDivisionLevel(); - double w = this.BASE_WIDTH * scale /maxDiv; - double h = this.BASE_HEIGHT * scale /maxDiv; - int childCount = simulator.getCurState().getRootResources().size(); - double x = w * (1 + i - childCount / 2.0); - double y = 20 ; - ResourcePath res = resNode.getResourceIdentifier(); - Object resource = graph.insertVertex(parent, null, - res.toString(), x, y, w, h, - "shape=ellipse;perimeter=ellipsePerimeter;verticalAlign=top;"); // insert a resource as a vertex - resources.put(resNode, resource); - i++; - createNextChildSimulateResourceVerticies(graph, resource, resNode, resources); - - } - - } finally { - graph.getModel().endUpdate(); - } - - return graph; - } - private void createNextChildSimulateResourceVerticies(mxGraph graph, Object parent, Resource resNode, Map resources) { //sample - Collection children = resNode.getChildren(); - if (children != null) { - //List children = resNode.getChildren(); - double i = 1; - double childCount = children.size(); - for (Resource childNode: children) { - Object childResource = setLayout(graph, parent, childNode.getResourceIdentifier(), i, childCount); - resources.put(childNode, childResource); - i++; - createNextChildSimulateResourceVerticies(graph, childResource, childNode, resources); - - } - } - if (children == null || children.size() == 0) { - Object state = graph.insertVertex(parent, null, - resNode.getState().getValue().toString(), 0.5, 0.5, 0.25, 0.25, "opacity=0;verticalAlign=down;", true); // insert a state label as a vertex - } - } -}