Newer
Older
AlgebraicDataflowArchitectureModel / AlgebraicDataflowArchitectureModel / src / application / SimulatorWindow.java
package application;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.swing.JFrame;

import com.mxgraph.model.mxCell;
import com.mxgraph.model.mxGeometry;
import com.mxgraph.model.mxGraphModel;
import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.swing.handler.mxRubberband;
import com.mxgraph.util.mxEvent;
import com.mxgraph.util.mxEventObject;
import com.mxgraph.util.mxPoint;
import com.mxgraph.util.mxEventSource.mxIEventListener;
import com.mxgraph.view.mxGraph;

import application.actions.SimulateAction;
import application.editor.DataTransferModelingCellEditor;
import application.editor.Editor;
import application.editor.InputEventCellEditor;
import application.layouts.DAGLayout;
import generators.JavaCodeGenerator;
import models.Edge;
import models.EdgeAttribute;
import models.algebra.Expression;
import models.algebra.InvalidMessage;
import models.algebra.ParameterizedIdentifierIsFutureWork;
import models.algebra.UnificationFailed;
import models.algebra.ValueUndefined;
import models.dataConstraintModel.Channel;
import models.dataConstraintModel.ChannelMember;
import models.dataConstraintModel.ResourcePath;
import models.dataConstraintModel.Selector;
import models.dataFlowModel.ChannelNode;
import models.dataFlowModel.DataFlowEdge;
import models.dataFlowModel.DataFlowGraph;
import models.dataFlowModel.DataTransferChannel;
import models.dataFlowModel.DataTransferModel;
import models.dataFlowModel.ResolvingMultipleDefinitionIsFutureWork;
import models.dataFlowModel.ResourceNode;
import parser.Parser;
import parser.ParserDTRAM;
import parser.exceptions.ExpectedColon;
import parser.exceptions.ExpectedRightBracket;
import parser.exceptions.WrongJsonExpression;
import parser.Parser.TokenStream;
import simulator.Event;
import simulator.Resource;
import simulator.ResourceIdentifier;
import simulator.Simulator;

public class SimulatorWindow extends JFrame{

	/**
	 * 
	 */
	private static final long serialVersionUID = -2425820512017088254L;
	public static final String title = "Simulation Tool";

	final int PORT_DIAMETER = 8;
	final int PORT_RADIUS = PORT_DIAMETER / 2;
	
	private Editor editor = null;
	private mxGraph graph = null;
	private mxGraphComponent graphComponent = null;
	private Simulator simulator = null;
	private boolean bReflectingArchitectureModel = false;
	private double x = 20;
	private double y = 20;
	
	public SimulatorWindow(Editor editor) {
		setTitle(title);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		this.graph = new mxGraph() {
			public boolean isPort(Object cell) {
				mxGeometry geo = getCellGeometry(cell);
				
				return (geo != null) ? geo.isRelative() : false;
			}
			
			public boolean isCellFoldable(Object cell, boolean collapse) {
				return false;
			}
		};
		
		this.graphComponent = new mxGraphComponent(graph);
		this.editor = editor;
		
		graph.getModel().addListener(mxEvent.CHANGE, new mxIEventListener() {
			public void invoke(Object sender, mxEventObject evt) {
				List<mxCell> terminals = new ArrayList<>();
				mxCell cell = null;
				for (Object change: ((List) evt.getProperties().get("changes"))) {
					if (change instanceof mxGraphModel.mxTerminalChange) {
						mxGraphModel.mxTerminalChange terminalChange = (mxGraphModel.mxTerminalChange) change;
						cell = (mxCell) terminalChange.getCell();
						mxCell terminal = (mxCell) terminalChange.getTerminal();
						terminals.add(terminal);
					}
				}
				if (terminals.size() == 2) {
					if (!editor.connectEdge(cell, terminals.get(0), terminals.get(1))) {
						graph.removeCells(new mxCell[] {cell});
					}
				}
			}
		});
		getContentPane().add(graphComponent);
		new mxRubberband(graphComponent);
		graph.setAllowDanglingEdges(false);
		graph.setCellsDisconnectable(true);
		
		setTitle(title);
	    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	    setSize(870,640);
	    setVisible(true);
	    
		simulator = new Simulator(this.editor.getModel());
	    constructSimulateGraph(simulator.getCurState().getRootResources(), this.editor.getModel(),this.editor.getDataFlowGraph());
	    graphComponent.setCellEditor(new InputEventCellEditor(graphComponent, simulator, this.editor, graph));
	}
	
	
	public  mxGraph constructSimulateGraph(Set<Resource> simulateRes, DataTransferModel model, DataFlowGraph dataFlowGraph) { 
		bReflectingArchitectureModel = true;
		((mxGraphModel) graph.getModel()).clear();
		Object parent = graph.getDefaultParent();
		graph.getModel().beginUpdate();
		
		try {
			Map<Resource, Object> resources = new HashMap<>();
			
			// create resource vertices
			for (Resource resNode: simulateRes) {
				int w = 80;
				int h = 30;
				ResourcePath res = resNode.getResourceIdentifier();
				Object resource = graph.insertVertex(parent, null,
						res.getResourceName(), x, y, w, h,
						"shape=ellipse;perimeter=ellipsePerimeter"); // insert a resource as a vertex
				resources.put(resNode, resource);
				createChildSimulateResourceVerticies(resource, resNode, resources, w, h);
				x+=80;
				}
			
		} finally {
			graph.getModel().endUpdate();
		}
		
		bReflectingArchitectureModel = false;
		return graph;
	}
	
	
	public void createChildSimulateResourceVerticies(Object resource, Resource resNode, Map<Resource, Object> resources, int w, int h) { //sample
		
		if(resNode.getChildren() != null) {
			for (Resource childNode: resNode.getChildren()) {
				ResourcePath childRes = childNode.getResourceIdentifier();
				Object childResource = graph.insertVertex(resource, null,
						childRes.getName(), 0, 0, w/2, h/2,
						"shape=ellipse;perimeter=ellipsePerimeter"); // insert a resource as a vertex
				resources.put(childNode, childResource);
				createChildSimulateResourceVerticies(childResource, childNode, resources, w, h);
			}
		}
	}
	
	public mxGraphComponent getGraphComponent() {
		return graphComponent;
	}

	public Editor getEditor() {
		return editor;
	}

	public void setEditor(Editor editor) {
		this.editor = editor;
	}

}