package parser; import java.io.BufferedReader; import java.io.IOException; import java.util.ArrayList; import java.util.Collection; import com.mxgraph.model.mxCell; import com.mxgraph.model.mxGeometry; import com.mxgraph.model.mxIGraphModel; import com.mxgraph.view.mxCellState; import com.mxgraph.view.mxGraph; import com.mxgraph.view.mxGraphView; import models.dataFlowModel.DataFlowModel; import parser.exceptions.ExpectedAssignment; import parser.exceptions.ExpectedChannel; import parser.exceptions.ExpectedChannelName; import parser.exceptions.ExpectedEquals; import parser.exceptions.ExpectedFormulaChannel; import parser.exceptions.ExpectedGeometry; import parser.exceptions.ExpectedInOrOutOrRefKeyword; import parser.exceptions.ExpectedIoChannel; import parser.exceptions.ExpectedLeftCurlyBracket; import parser.exceptions.ExpectedModel; import parser.exceptions.ExpectedNode; import parser.exceptions.ExpectedRHSExpression; import parser.exceptions.ExpectedResource; import parser.exceptions.ExpectedRightBracket; import parser.exceptions.ExpectedStateTransition; import parser.exceptions.WrongLHSExpression; import parser.exceptions.WrongRHSExpression; public class ParserDTRAM extends Parser { protected TokenStream stream = null; private static final String MODEL = "model"; private static final String GEOMETRY = "geometry"; private static final String NODE = "node"; private static final String RESOURCE = "r"; private static final String CHANNEL = "c"; private static final String FORMULA_CHANNEL = "fc"; private static final String IO_CHANNEL = "ioc"; /**-------------------------------------------------------------------------------- * * @param reader */ public DataFlowModel doParseModel(final BufferedReader reader) throws ExpectedRightBracket, ExpectedChannel, ExpectedChannelName, ExpectedLeftCurlyBracket, ExpectedInOrOutOrRefKeyword, ExpectedStateTransition, ExpectedEquals, ExpectedRHSExpression, WrongLHSExpression, WrongRHSExpression, ExpectedAssignment, ExpectedModel, ExpectedGeometry { stream = new TokenStream(); try { String line; while ((line = reader.readLine()) != null) { stream.addLine(line); } reader.close(); } catch (IOException e) { e.printStackTrace(); return null; } DataFlowModel model = getParsedModel(stream); return model; } /**-------------------------------------------------------------------------------- * * @param graph */ public void doParseGeometry(final mxGraph graph) throws ExpectedRightBracket, ExpectedChannel, ExpectedChannelName, ExpectedLeftCurlyBracket, ExpectedInOrOutOrRefKeyword, ExpectedStateTransition, ExpectedEquals, ExpectedRHSExpression, WrongLHSExpression, WrongRHSExpression, ExpectedAssignment, ExpectedModel, ExpectedGeometry, ExpectedNode, ExpectedResource, ExpectedFormulaChannel, ExpectedIoChannel{ parseGeometry(stream, graph); } /**-------------------------------------------------------------------------------- * private /**-------------------------------------------------------------------------------- * * @param stream */ private DataFlowModel getParsedModel(final TokenStream stream) throws ExpectedRightBracket, ExpectedChannel, ExpectedChannelName, ExpectedLeftCurlyBracket, ExpectedInOrOutOrRefKeyword, ExpectedStateTransition, ExpectedEquals, ExpectedRHSExpression, WrongLHSExpression, WrongRHSExpression, ExpectedAssignment, ExpectedModel, ExpectedGeometry { if (!stream.hasNext()) { return null; } String modelKeyword = stream.next(); if (!modelKeyword.equals(MODEL)) { throw new ExpectedModel(stream.getLine()); } if (!stream.hasNext()) { throw new ExpectedModel(stream.getLine()); } String leftBracket = stream.next(); if (!leftBracket.equals(LEFT_CURLY_BRACKET)) { throw new ExpectedLeftCurlyBracket(stream.getLine()); } DataFlowModel model = parseDataFlowModel(stream); String rightBracket = stream.next(); if(!rightBracket.equals(RIGHT_CURLY_BRACKET)) { throw new ExpectedRightBracket(stream.getLine()); } return model; } /**-------------------------------------------------------------------------------- * change graph's geometries from "DTRAM" file. * @param stream * @param graph */ private void parseGeometry(final TokenStream stream, final mxGraph graph) throws ExpectedRightBracket, ExpectedChannel, ExpectedChannelName, ExpectedLeftCurlyBracket, ExpectedInOrOutOrRefKeyword, ExpectedStateTransition, ExpectedEquals, ExpectedRHSExpression, WrongLHSExpression, WrongRHSExpression, ExpectedAssignment,ExpectedModel, ExpectedGeometry, ExpectedNode, ExpectedResource, ExpectedFormulaChannel, ExpectedIoChannel { String geometry = stream.next(); if (!geometry.equals(GEOMETRY)) { throw new ExpectedGeometry(stream.getLine()); } String leftBracket = stream.next(); if(!leftBracket.equals(LEFT_CURLY_BRACKET)) { throw new ExpectedLeftCurlyBracket(stream.getLine()); } String node = stream.next(); while (node.equals(NODE)) { String rOrFcOrIocOrC = stream.next(); if (!rOrFcOrIocOrC.equals(RESOURCE) &&!rOrFcOrIocOrC.equals(FORMULA_CHANNEL) &&!rOrFcOrIocOrC.equals(IO_CHANNEL) &&!rOrFcOrIocOrC.equals(CHANNEL)) { throw new ExpectedNode(stream.getLine()); } String name = stream.next(); String colon = stream.next(); String x = stream.next(); int xC = Integer.parseInt(x); // C = Coordinate(���W) String comma1 = stream.next(); String y = stream.next(); int yC = Integer.parseInt(y); String comma2 = stream.next(); String w = stream.next(); int wC = Integer.parseInt(w); String comma3 = stream.next(); String h = stream.next(); int hC = Integer.parseInt(h); Object root = graph.getDefaultParent(); mxIGraphModel graphModel = graph.getModel(); for (int i = 0; i < graph.getModel().getChildCount(root); i++) { Object cell = graph.getModel().getChildAt(root, i); if (graph.getModel().isVertex(cell)) { mxGeometry geom = (mxGeometry) ((mxCell) cell).getGeometry().clone(); mxGraphView view = graph.getView(); mxCellState state = view.getState(cell); if (name.equals(state.getLabel())){ geom.setX(xC); geom.setY(yC); graphModel.setGeometry(cell, geom); } } } node = stream.next(); } if (!node.equals(RIGHT_CURLY_BRACKET)) { throw new ExpectedRightBracket(stream.getLine()); } } }