Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AST datastructure with Simple Parser for Text File Lines and JSON import/export #63

Merged
merged 94 commits into from
Mar 24, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
94 commits
Select commit Hold shift + click to select a range
0136287
created library de.variantsync.core
tcerny Dec 9, 2020
ebd6b2e
refractor package name
jeremiaheinle Dec 9, 2020
40b4110
deleted openjdk10 because its not working
wurstbroteater Dec 10, 2020
fd5ea93
minor changes after creating core library project
tcerny Dec 10, 2020
2070412
first draft of a Simple Parser for Text File Lines #52
tcerny Dec 14, 2020
7a95103
implemented #51
jeremiaheinle Dec 14, 2020
2f4ad8a
added UUID as type
wurstbroteater Dec 15, 2020
b9edc59
AST: added add method WIP toString
wurstbroteater Dec 16, 2020
fba3b05
added toString
wurstbroteater Dec 16, 2020
7cc7dce
Fixed and Optimised toString()
wurstbroteater Dec 16, 2020
63b21fc
added forgotten space in toString()
wurstbroteater Dec 16, 2020
421883f
changed gitignore
wurstbroteater Dec 16, 2020
b3d8ebb
edited #51
jeremiaheinle Dec 16, 2020
a003240
removed wrong java version
wurstbroteater Dec 17, 2020
8be0f6e
del apptest, enum
jeremiaheinle Dec 17, 2020
d5417dd
changed add Method of AST and its usage. Updated POM
wurstbroteater Dec 17, 2020
b132ef9
MAVEN: set JAVA version to 11 for usage of Path
wurstbroteater Dec 17, 2020
225174a
changed toString according to pauls ideas #50
wurstbroteater Dec 17, 2020
9259bcc
prettty json and test AST.toString()
jeremiaheinle Dec 17, 2020
829e4db
Merge branch 'issue-50' into issue-51
wurstbroteater Dec 17, 2020
a3a96b7
Merge remote-tracking branch 'origin/issue-51' into issue-51
wurstbroteater Dec 17, 2020
355cec9
merge dropped LineGrammar
wurstbroteater Dec 17, 2020
06dbe23
JsonParserTest fixed
wurstbroteater Dec 17, 2020
dd854f8
exclude fiels in AST from JsonParser
jeremiaheinle Dec 17, 2020
d80b38b
added seriazable
jeremiaheinle Dec 17, 2020
94425ed
Testcase for parseDirectory
tcerny Dec 21, 2020
15c2c40
fixed bug causing that the first dir is displayed twice
wurstbroteater Jan 6, 2021
bf259dc
first try to make attributed LineGrammar Enum
wurstbroteater Jan 6, 2021
5d82215
Styleguide
wurstbroteater Jan 6, 2021
1435db5
refactoring AST.java
wurstbroteater Jan 6, 2021
84b2e15
removed main from AST.java and added toString test
wurstbroteater Jan 6, 2021
34a6d4e
added size method and WIP size testing
wurstbroteater Jan 6, 2021
4071731
removed DEBUG output
wurstbroteater Jan 6, 2021
d136154
updated travis
wurstbroteater Jan 6, 2021
6fd51b2
Update .travis.yml
wurstbroteater Jan 6, 2021
f92002d
Travis decides if this is a fix or not
wurstbroteater Jan 6, 2021
87b22c8
Refactored LineGrammar according to Pauls idea, reduced size test
wurstbroteater Jan 8, 2021
a6249c0
addressing reviewer suggestions of #53
wurstbroteater Jan 16, 2021
0e8b183
added relativePath to child pom
wurstbroteater Jan 25, 2021
fa3a8a8
added maven compiler to fix IntelliJ-only-resolving-bugs
wurstbroteater Jan 25, 2021
8fcc5cd
changes in Simple Parser for Text File Lines
tcerny Jan 26, 2021
870f484
Merge branch 'seproj_ulm_2020' into issue-52
tcerny Jan 27, 2021
4414909
applied code style to newest library
tcerny Jan 27, 2021
f06f071
implemented grammar generalisation
tcerny Jan 27, 2021
b64019b
created ast package
tcerny Jan 27, 2021
28d4135
style guide in tests
tcerny Jan 27, 2021
4bc0314
Merge branch 'seproj_ulm_2020' into issue-52
wurstbroteater Feb 2, 2021
74fed02
Issue-58 resolved
jeremiaheinle Feb 11, 2021
caaadff
processed issue 56 WIP
tcerny Feb 16, 2021
70ca60d
removed commented lines and formatted files prior to pull request
tcerny Mar 2, 2021
58ebef0
finished issue-56 and deleted previously added folder de.variantsync.…
tcerny Mar 2, 2021
84d5696
Merge branch 'issue-56' into issue-52
tcerny Mar 2, 2021
1b44903
added accidentally deleted file de.variantsync.core/.project
tcerny Mar 2, 2021
1cbeff4
adressed reviewers suggestion
tcerny Mar 3, 2021
7a57030
addressed reviewers suggestions
tcerny Mar 10, 2021
21338d5
addressed reviewers suggestion
tcerny Mar 10, 2021
0e40810
changes in LineBasedParser and LineBasedParserTest
tcerny Mar 10, 2021
22f9e9e
review fixes
jeremiaheinle Mar 10, 2021
0a6b2dd
introduced local variables to increase readability
wurstbroteater Mar 10, 2021
989f811
addressing reviewer: move toString to class bottom
wurstbroteater Mar 10, 2021
dd8fbba
removed pointer magic
wurstbroteater Mar 10, 2021
33312e1
WIP: Refactoring toString
wurstbroteater Mar 10, 2021
c571d5e
corrected mistakes made during the work on issue 56
tcerny Mar 11, 2021
fb783d2
added FormatHelper class for refactoring of code duplication
tcerny Mar 11, 2021
c902a59
removed attributes-list in LineGrammar
tcerny Mar 11, 2021
43de43f
regeneralized JsonParser String/B
jeremiaheinle Mar 12, 2021
4812732
WIP refactored separator variables as discussed
wurstbroteater Mar 20, 2021
abc220d
AST added getter, setter, getMaxDepth, docu
wurstbroteater Mar 20, 2021
13cbd01
AST added getter, setter, getMaxDepth, docu
wurstbroteater Mar 20, 2021
09823af
refactored separator vars
wurstbroteater Mar 22, 2021
769ed4f
refactored AST test and toString test
wurstbroteater Mar 22, 2021
6e466ce
added comments and java docs for readability
wurstbroteater Mar 22, 2021
0ec7870
fixed toStringTest out and restructured size test
wurstbroteater Mar 22, 2021
b79c895
naming
wurstbroteater Mar 22, 2021
2e985a6
removed unused/dangerous AST methods
wurstbroteater Mar 22, 2021
1dc99c6
added test for maxDepth of AST
wurstbroteater Mar 22, 2021
f420d69
Deactivated failing test due to new AST format
wurstbroteater Mar 22, 2021
00d542b
Code formatted and clean up
wurstbroteater Mar 22, 2021
3838c0b
Changed name of AST attribut to plural form
wurstbroteater Mar 22, 2021
c6807fe
fixed test for initial size
wurstbroteater Mar 22, 2021
e8cdf23
addressing reviewer suggestion
wurstbroteater Mar 22, 2021
70ebe21
addressing reviewer suggestion
wurstbroteater Mar 22, 2021
d8524f2
changed getSubtrees and code restructure
wurstbroteater Mar 22, 2021
a46fca3
added tests
wurstbroteater Mar 22, 2021
9fabadd
Code format and clean up
wurstbroteater Mar 22, 2021
08ab049
addressing reviewer suggestion
wurstbroteater Mar 22, 2021
063ef68
minor improvments on add methods, added tests, code format and cleanup
wurstbroteater Mar 22, 2021
0669169
renamed Test so that maven will auto exec it
wurstbroteater Mar 23, 2021
94bb0ec
refactored toString, added printTree, fixed tests for AST
wurstbroteater Mar 23, 2021
32bac5e
Code format and cleanup
wurstbroteater Mar 23, 2021
72d897c
impl. reviewer suggestions as far as possible till meeting
wurstbroteater Mar 23, 2021
d297a64
changes after meeting
wurstbroteater Mar 24, 2021
2a6957b
final after meeting changes, code format and cleanup
wurstbroteater Mar 24, 2021
954c6c6
implemented final reviewer suggestions
tcerny Mar 24, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 41 additions & 26 deletions de.variantsync.core/src/main/java/de/variantsync/core/ast/AST.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,34 @@
import de.variantsync.core.interfaces.Grammar;

/**
* This class represents the Abstract Syntax Tree data structure.
* This class represents the Abstract Syntax Tree data structure. This also tests the LineGrammar indirectly.
wurstbroteater marked this conversation as resolved.
Show resolved Hide resolved
*
* @param <G> a generic which needs to extend the Grammar class, defining the type of the AST
* @param <Value> a generic which defines the value of the actual AST
* @param <V> a generic which defines the value of the actual AST
* @author eric
*/
public class AST<G extends Grammar, Value> {
public class AST<G extends Grammar, V> {

private UUID id;
private Value value;
private G type;
private List<AST<G, Value>> subtrees;
private V value;
private List<AST<G, V>> subtrees;

// all attributes which should not be visible to the GSON parser need to be at least transient
public static transient final String INDENT_STRING = " ";
public static transient final String NEXT_SEPARATOR = "\u2502 ";
public static transient final String NEXT_ACT_SEPARATOR = "\u251C\u2500 ";
public static transient final String LAST_SEPARATOR = "\u2514\u2500 ";

public AST(G type, Value value, UUID id) {
public AST(UUID id, G type, V value) {
this.id = id;
this.type = type;
this.value = value;
this.subtrees = new ArrayList<>();
}

public AST(G type, Value value) {
this(type, value, UUID.randomUUID());
public AST(G type, V value) {
this(UUID.randomUUID(), type, value);
}

/**
Expand All @@ -50,7 +50,7 @@ public UUID getId() {
return id;
}

public Value getValue() {
public V getValue() {
return value;
}

Expand All @@ -61,7 +61,7 @@ public G getType() {
/**
* @return the subtrees as an unmodifiable List
*/
public List<AST<G, Value>> getSubtrees() {
public List<AST<G, V>> getSubtrees() {
return Collections.unmodifiableList(subtrees);
}

Expand All @@ -71,54 +71,69 @@ public int getMaxDepth() {
}

int maxDepth = 0;
for (final AST<G, Value> node : subtrees) {
for (final AST<G, V> node : subtrees) {
maxDepth = Math.max(node.getMaxDepth(), maxDepth);
}
return ++maxDepth;
wurstbroteater marked this conversation as resolved.
Show resolved Hide resolved
}
wurstbroteater marked this conversation as resolved.
Show resolved Hide resolved

/**
* At the moment it is not allowed to add null as an AST.
* This method calls the addChild method for each element of the given list. ASTs which would be rejected by the addChild method are skipped.
*
* @param toAdd List of AST which should be added as subtrees
* @return true if all items where successfully added
*/
public boolean addChildren(List<AST<G, Value>> toAdd) {
public boolean addChildren(List<AST<G, V>> toAdd) {
boolean out = true;
if (toAdd != null) {
return subtrees.addAll(toAdd);
for (final AST<G, V> elem : toAdd) {
if (!addChild(elem)) {
out = false;
}
}
}
return false;
return out;
wurstbroteater marked this conversation as resolved.
Show resolved Hide resolved
}
wurstbroteater marked this conversation as resolved.
Show resolved Hide resolved

/**
* At the moment it is not allowed to add null as an AST.
* This method adds an element to the AST by checking its validity through the the Grammar G and assuring that the element is not null.
*
* @param toAdd Single AST which should be added as subtree
* @return true if the item was successfully added
*/
public boolean addChild(AST<G, Value> toAdd) {
if (toAdd != null) {
public boolean addChild(AST<G, V> toAdd) {
if ((toAdd != null) && type.isValidChild(toAdd.type)) {
return subtrees.add(toAdd);
}
return false;
}

public int size() {
int tmpSize = 1;
for (final AST<G, Value> act : subtrees) {
for (final AST<G, V> act : subtrees) {
tmpSize += act.size();
}
return tmpSize;
}

/**
* This recursive method prints for each tree element the Grammar type, the Value and (for the sake of readability) only the most significant bits of the
* UUID. It returns the AST as human readable tree.
* This method returns only the most significant bits of the UUID, the type, value and subtree size as a String of the actual AST.
*
* @return AST as readable String
* @return UUID, Type, Value, subtree size as String
*/
@Override
public String toString() {
return String.format("[ Id: %d, Type: %s, Value: %s, Subtree-size: %d ]", id.getMostSignificantBits(), type.toString(), value.toString(),
subtrees.size());
}

/**
* This recursive method prints for each tree element the Grammar type, the Value and (for the sake of readability) only the most significant bits of the
* UUID. It returns the whole AST as human readable tree.
*
* @return AST as readable String
*/
public String printTree() {
final StringBuilder result = new StringBuilder();
if (value == null) {
return result.toString();
wurstbroteater marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -127,12 +142,12 @@ public String toString() {

final HashSet<Integer> levelFinished = new HashSet<>(); // determines if all subtrees of the actual tree on this depth have been drawn or not
final boolean isActualElementLastElement = false;
toString(result, this, depth, levelFinished, isActualElementLastElement);
printTree(result, this, depth, levelFinished, isActualElementLastElement);
}
return result.toString();
}

private void toString(StringBuilder result, AST<G, Value> parent, int depth, HashSet<Integer> levelFinished, boolean isLast) {
private void printTree(StringBuilder result, AST<G, V> parent, int depth, HashSet<Integer> levelFinished, boolean isLast) {
// print enough INDENT_STRINGS and choose separator according to whether or not there are subtrees left
for (int i = 0; i < depth; i++) {
StringBuilder line = new StringBuilder(INDENT_STRING).append(NEXT_SEPARATOR);
Expand All @@ -152,7 +167,7 @@ private void toString(StringBuilder result, AST<G, Value> parent, int depth, Has
}
result.append(String.format("%s %s uuid: %d%n", parent.type, parent.value, parent.getId().getMostSignificantBits()));
depth++;
for (final AST<G, Value> child : parent.subtrees) {
for (final AST<G, V> child : parent.subtrees) {
isLast = false;
if (parent.subtrees.indexOf(child) == (parent.subtrees.size() - 1)) {
// reached last child of parent
Expand All @@ -162,7 +177,7 @@ private void toString(StringBuilder result, AST<G, Value> parent, int depth, Has
// first child of new sub tree with unfinished depth so it needs NEXT_SEPARATOR later
levelFinished.remove(depth - 1);
}
toString(result, child, depth, levelFinished, isLast);
printTree(result, child, depth, levelFinished, isLast);
}
wurstbroteater marked this conversation as resolved.
Show resolved Hide resolved
}
}
59 changes: 54 additions & 5 deletions de.variantsync.core/src/test/java/de/variantsync/core/ASTTest.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package de.variantsync.core;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.util.Arrays;
import java.util.List;

import org.junit.Before;
import org.junit.Test;
Expand All @@ -24,6 +26,7 @@ public class ASTTest {
AST<LineGrammar, String> mainJava;
int lineIndex = 0; // only for toString testing
final int INITIAL_AST_SIZE = 11;
final int INITIAL_TOSTRING_ROWS = 10;

@Before
public void setup() {
Expand All @@ -45,13 +48,59 @@ public void setup() {
new AST<>(LineGrammar.Line, "}")));
}

@Test
public void toStringOnInitialTest() {
final String[] lines = root.toString().split(" ");
assertEquals(INITIAL_TOSTRING_ROWS, lines.length);
// this loop skips the names of the variable and only checks their value
for (int i = 2; i < lines.length; i = i + 2) {
if (lines[i].charAt(lines[i].length() - 1) == ',') {
lines[i] = lines[i].substring(0, lines[i].length() - 1);
}
switch (i) {
case 2:
assertEquals(root.getId().getMostSignificantBits(), Long.parseLong(lines[i]));
break;
case 4:
assertEquals(root.getType().toString(), lines[i]);
break;
case 6:
assertEquals(root.getValue().toString(), lines[i]);
break;
case 8:
assertEquals(root.getSubtrees().size(), Integer.parseInt(lines[i]));
break;
default:
throw new IllegalArgumentException(String.format("toStringOnInitialTest has incorrect lines size: %d", i));
}
}
}
wurstbroteater marked this conversation as resolved.
Show resolved Hide resolved

@Test
public void addOnInitialTest() {
wurstbroteater marked this conversation as resolved.
Show resolved Hide resolved
final AST<LineGrammar, String> newTree = new AST<>(LineGrammar.Directory, "newROOT");
final int oldSize = root.getSubtrees().size();
root.addChild(newTree);
AST<LineGrammar, String> newTree = new AST<>(LineGrammar.Directory, "newROOT");
int oldSize = root.getSubtrees().size();
assertTrue(root.addChild(newTree));
assertEquals(oldSize + 1, root.getSubtrees().size());

newTree = new AST<>(LineGrammar.Line, "!LineYouAreLookingFor");
oldSize = root.getSubtrees().size();
assertFalse(root.addChild(newTree));
assertEquals(oldSize, root.getSubtrees().size());
}

@Test
public void addAllOnInitialTest() {
wurstbroteater marked this conversation as resolved.
Show resolved Hide resolved
List<AST<LineGrammar, String>> newTrees = Arrays.asList(new AST<>(LineGrammar.Line, "public class Main {"), new AST<>(LineGrammar.BinaryFile, "101010"),
new AST<>(LineGrammar.TextFile, "fancyFile.txt"), new AST<>(LineGrammar.Directory, "fancyFolder"));
int oldSize = root.getSubtrees().size();
assertFalse(root.addChildren(newTrees));
assertEquals(oldSize + 3, root.getSubtrees().size());

newTrees = Arrays.asList(new AST<>(LineGrammar.Directory, "public class Main {"), new AST<>(LineGrammar.BinaryFile, "101010"));
oldSize = root.getSubtrees().size();
assertTrue(root.addChildren(newTrees));
assertEquals(oldSize + 2, root.getSubtrees().size());
}

@Test(expected = UnsupportedOperationException.class)
Expand Down Expand Up @@ -82,8 +131,8 @@ public void getMaxDepthOnEmptyASTTest() {
}

@Test
public void toStringOnInitialTest() {
final String[] lines = root.toString().split(String.format("%n"));
public void printTreeOnInitialTest() {
final String[] lines = root.printTree().split(String.format("%n"));
assertEquals(INITIAL_AST_SIZE, lines.length);
// test root values
final String[] rootAttributes = lines[0].split(" ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

//TODO. Add compare method to AST and then compare ASTs with this method.

public class JsonParserTestASTWithLineGrammar {
public class JsonParserASTWithLineGrammarTest {

AST<LineGrammar, String> exampleAst;
Path examplePath;
Expand Down