diff --git a/AlgebraicDataflowArchitectureModel/src/graphicalrefactor/editor/Editor.java b/AlgebraicDataflowArchitectureModel/src/graphicalrefactor/editor/Editor.java index 9683074..2aca6a3 100644 --- a/AlgebraicDataflowArchitectureModel/src/graphicalrefactor/editor/Editor.java +++ b/AlgebraicDataflowArchitectureModel/src/graphicalrefactor/editor/Editor.java @@ -165,7 +165,7 @@ try { File file = new File(curFilePath); FileWriter filewriter = new FileWriter(file); - filewriter.write(model.toString()); + filewriter.write(model.getSourceText()); filewriter.close(); } catch (IOException e) { e.printStackTrace(); @@ -430,6 +430,7 @@ } public void setChannelCode(DataflowChannelGenerator ch, String code) { + ch.setSourceText(code); TokenStream stream = new TokenStream(); for (String line: code.split("\n")) { stream.addLine(line); diff --git a/AlgebraicDataflowArchitectureModel/src/graphicalrefactor/views/GraphicalRefactorCellEditor.java b/AlgebraicDataflowArchitectureModel/src/graphicalrefactor/views/GraphicalRefactorCellEditor.java index 07cfc9f..37c57c9 100644 --- a/AlgebraicDataflowArchitectureModel/src/graphicalrefactor/views/GraphicalRefactorCellEditor.java +++ b/AlgebraicDataflowArchitectureModel/src/graphicalrefactor/views/GraphicalRefactorCellEditor.java @@ -67,7 +67,7 @@ } } JPanel panel = new JPanel(); - JTextArea textArea = new JTextArea(ch.toString(), 10, 20); + JTextArea textArea = new JTextArea(ch.getSourceText(), 10, 20); panel.add(textArea); // JEditorPane panel = new JEditorPane("text/plain", ch.toString()); // panel.setEditable(true); diff --git a/AlgebraicDataflowArchitectureModel/src/models/dataConstraintModel/ChannelGenerator.java b/AlgebraicDataflowArchitectureModel/src/models/dataConstraintModel/ChannelGenerator.java index ac571de..8a9bd1f 100644 --- a/AlgebraicDataflowArchitectureModel/src/models/dataConstraintModel/ChannelGenerator.java +++ b/AlgebraicDataflowArchitectureModel/src/models/dataConstraintModel/ChannelGenerator.java @@ -8,6 +8,7 @@ protected Set groupSelectors = null; protected Set channelSelectors = null; protected Set channelMembers = null; + protected String sourceText = null; public ChannelGenerator(String channelName) { this.channelName = channelName; @@ -92,4 +93,15 @@ public String toString() { return channelName; } + + public void setSourceText(String sourceText) { + this.sourceText = sourceText; + } + + public String getSourceText() { + if (sourceText == null) { + return toString(); + } + return sourceText; + } } diff --git a/AlgebraicDataflowArchitectureModel/src/models/dataConstraintModel/DataConstraintModel.java b/AlgebraicDataflowArchitectureModel/src/models/dataConstraintModel/DataConstraintModel.java index 1679f1a..d8987b3 100644 --- a/AlgebraicDataflowArchitectureModel/src/models/dataConstraintModel/DataConstraintModel.java +++ b/AlgebraicDataflowArchitectureModel/src/models/dataConstraintModel/DataConstraintModel.java @@ -386,4 +386,25 @@ } return out; } + + public String getSourceText() { + String out = ""; + String init = ""; + for (IdentifierTemplate identifierTemplate: identifierTemplates.values()) { + String initializer = identifierTemplate.getInitText(); + if (initializer != null) { + init += initializer; + } + } + if (init.length() > 0) { + out += "init {\n" + init + "}\n"; + } + for (ChannelGenerator channelGenerator: ioChannelGenerators.values()) { + out += channelGenerator.getSourceText(); + } + for (ChannelGenerator channelGenerator: channelGenerators.values()) { + out += channelGenerator.getSourceText(); + } + return out; + } } diff --git a/AlgebraicDataflowArchitectureModel/src/models/dataConstraintModel/IdentifierTemplate.java b/AlgebraicDataflowArchitectureModel/src/models/dataConstraintModel/IdentifierTemplate.java index d0e62ff..d91355c 100644 --- a/AlgebraicDataflowArchitectureModel/src/models/dataConstraintModel/IdentifierTemplate.java +++ b/AlgebraicDataflowArchitectureModel/src/models/dataConstraintModel/IdentifierTemplate.java @@ -9,6 +9,7 @@ private Type resourceStateType = null; private int numParameters = 0; private Expression initialValue = null; + protected String initText = null; public IdentifierTemplate(String resourceName, int numParameters) { this.resourceName = resourceName; @@ -50,6 +51,14 @@ this.initialValue = initialValue; } + public void setInitText(String initText) { + this.initText = initText; + } + + public String getInitText() { + return initText; + } + public boolean equals(Object another) { if (!(another instanceof IdentifierTemplate)) return false; return resourceName.equals(((IdentifierTemplate) another).resourceName); diff --git a/AlgebraicDataflowArchitectureModel/src/parser/Parser.java b/AlgebraicDataflowArchitectureModel/src/parser/Parser.java index 1e0600d..cf24fc9 100644 --- a/AlgebraicDataflowArchitectureModel/src/parser/Parser.java +++ b/AlgebraicDataflowArchitectureModel/src/parser/Parser.java @@ -85,16 +85,17 @@ public static DataflowChannelGenerator parseChannel(TokenStream stream, DataFlowModel model) throws ExpectedRightBracket, ExpectedChannel, ExpectedChannelName, ExpectedLeftCurlyBracket, ExpectedInOrOutOrRefKeyword, ExpectedStateTransition, ExpectedEquals, ExpectedRHSExpression, WrongLHSExpression, WrongRHSExpression, ExpectedAssignment { if (!stream.hasNext()) return null; - String channelOtInitKeyword = stream.next(); - if (!channelOtInitKeyword.equals(CHANNEL)) { - if (!channelOtInitKeyword.equals(INIT)) throw new ExpectedChannel(stream.getLine()); + String channelOrInitKeyword = stream.next(); + if (!channelOrInitKeyword.equals(CHANNEL)) { + if (!channelOrInitKeyword.equals(INIT)) throw new ExpectedChannel(stream.getLine()); parseInit(stream, model); - channelOtInitKeyword = stream.next(); + channelOrInitKeyword = stream.next(); } if (!stream.hasNext()) throw new ExpectedChannelName(stream.getLine()); String channelName = stream.next(); if (channelName.equals(LEFT_CURLY_BRACKET)) throw new ExpectedChannelName(stream.getLine()); + int fromLine = stream.getLine(); DataflowChannelGenerator channel = new DataflowChannelGenerator(channelName); String leftBracket = stream.next(); if (!leftBracket.equals(LEFT_CURLY_BRACKET)) throw new ExpectedLeftCurlyBracket(stream.getLine()); @@ -120,6 +121,8 @@ throw new ExpectedInOrOutOrRefKeyword(stream.getLine()); } } + int toLine = stream.getLine(); + channel.setSourceText(stream.getSourceText(fromLine, toLine)); return channel; } @@ -128,6 +131,7 @@ if (!leftBracket.equals(LEFT_CURLY_BRACKET)) throw new ExpectedLeftCurlyBracket(stream.getLine()); String resourceName = null; while (stream.hasNext() && !(resourceName = stream.next()).equals(RIGHT_CURLY_BRACKET)) { + int fromLine = stream.getLine(); IdentifierTemplate identifier = model.getIdentifierTemplate(resourceName); if (identifier == null) { identifier = new IdentifierTemplate(resourceName, 0); @@ -141,12 +145,14 @@ String equals = stream.next(); if (!equals.equals(ASSIGNMENT)) throw new ExpectedAssignment(stream.getLine()); + int toLine = stream.getLine(); Expression rightTerm = null; if (!stream.hasNext()) throw new ExpectedRHSExpression(stream.getLine()); rightTerm = parseTerm(stream, model); if (rightTerm == null) throw new WrongRHSExpression(stream.getLine()); identifier.setInitialValue(rightTerm); + identifier.setInitText(stream.getSourceText(fromLine, toLine)); } } @@ -336,6 +342,7 @@ public static class TokenStream { private ArrayList> tokens = new ArrayList<>(); + private ArrayList lines = new ArrayList<>(); private int line = 0; private int n = 0; @@ -345,6 +352,7 @@ } public void addLine(String line) { + lines.add(line); line = line.trim(); tokens.add( splitBy( @@ -440,5 +448,13 @@ public int getLine() { return line; } + + public String getSourceText(int from, int to) { + String text = ""; + for (int l = from; l <= to; l++) { + text += lines.get(l) + "\n"; + } + return text; + } } }