Skip to content

Commit

Permalink
Modification of toDot method, added showRepeatingEdges flag, to manag…
Browse files Browse the repository at this point in the history
…e possibility of displaying repeating edges, if 'true' show all adges, if 'false' don't display repeating edges
  • Loading branch information
Uacias authored and neotheprogramist committed Nov 14, 2023
1 parent 4ce9944 commit 330d58b
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,10 @@ public void commit(final NodeUpdater nodeUpdater) {
* @return The DOT representation of the Verkle Trie.
*/

public String toDotTree() {
public String toDotTree(Boolean showRepeatingEdges) {
StringBuilder result = new StringBuilder("digraph VerkleTrie {\n");
Node<V> root = getRoot();
result.append(root.toDot());
result.append(root.toDot(showRepeatingEdges));
return result.append("}\n").toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ public String print() {
* @return DOT representation of the BranchNode.
*/
@Override
public String toDot() {
public String toDot(Boolean showRepeatingEdges) {
StringBuilder result = new StringBuilder()
.append(getClass().getSimpleName()).append(getLocation().orElse(Bytes.EMPTY))
.append("\", location=\"").append(getLocation().orElse(Bytes.EMPTY))
Expand All @@ -258,10 +258,10 @@ public String toDot() {
String edgeString = getClass().getSimpleName() + getLocation().orElse(Bytes.EMPTY) + " -> " + child.getClass().getSimpleName()
+ child.getLocation().orElse(Bytes.EMPTY) + "\n";

if(!result.toString().contains(edgeString)) {
if(showRepeatingEdges || !result.toString().contains(edgeString)) {
result.append(edgeString);
}
result.append(child.toDot());
result.append(child.toDot(showRepeatingEdges));
}
return result.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public String print() {
* @return DOT representation of the InternalNode.
*/
@Override
public String toDot() {
public String toDot(Boolean showRepeatingEdges) {
StringBuilder result = new StringBuilder()
.append(getClass().getSimpleName()).append(getLocation().orElse(Bytes.EMPTY))
.append("[location=\"").append(getLocation().orElse(Bytes.EMPTY))
Expand All @@ -175,10 +175,10 @@ public String toDot() {
String edgeString = getClass().getSimpleName() + getLocation().orElse(Bytes.EMPTY) + " -> " + child.getClass().getSimpleName()
+ child.getLocation().orElse(Bytes.EMPTY) + "\n";

if(!result.toString().contains(edgeString)) {
if(showRepeatingEdges || !result.toString().contains(edgeString)) {
result.append(edgeString);
}
result.append(child.toDot());
result.append(child.toDot(showRepeatingEdges));
}
return result.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public String print() {
* @return DOT representation of the LeafNode.
*/
@Override
public String toDot() {
public String toDot(Boolean showRepeatingEdges) {
Bytes locationBytes = getLocation().orElse(Bytes.EMPTY);

return new StringBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,12 @@ default List<Node<V>> getChildren() {
String print();

/**
* Generates DOT representation for the node.
* Generates DOT representation for the Node.
*
* @return DOT representation of the node.
* @param showRepeatingEdges If true, prints all edges; if false, prints only unique edges.
*
* @return DOT representation of the Node.
*/
String toDot();
String toDot(Boolean showRepeatingEdges);

}
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public String print() {
* @return DOT representation of the NullLeafNode.
*/
@Override
public String toDot() {
public String toDot(Boolean showRepeatingEdges) {
String result = getClass().getSimpleName() + getLocation().orElse(Bytes.EMPTY) + " [location=\"" + getLocation().orElse(Bytes.EMPTY) + "\"]\n";
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public String print() {
* @return DOT representation of the NullNode.
*/
@Override
public String toDot() {
public String toDot(Boolean showRepeatingEdges) {
String result = getClass().getSimpleName() + getLocation().orElse(Bytes.EMPTY) + "[location=\"" + getLocation().orElse(Bytes.EMPTY) + "\"]\n";
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,9 @@ private Bytes extractStem(final Bytes stemValue) {
return stemValue.slice(0, 31);
}


@Override
public String toDot() {
public String toDot(Boolean showRepeatingEdges) {
StringBuilder result = new StringBuilder()
.append(getClass().getSimpleName()).append(getLocation().orElse(Bytes.EMPTY))
.append("[location=\"").append(getLocation().orElse(Bytes.EMPTY))
Expand All @@ -311,10 +312,10 @@ public String toDot() {
String edgeString = getClass().getSimpleName() + getLocation().orElse(Bytes.EMPTY) + " -> " + child.getClass().getSimpleName()
+ child.getLocation().orElse(Bytes.EMPTY) + "\n";

if(!result.toString().contains(edgeString)) {
if(showRepeatingEdges || !result.toString().contains(edgeString)) {
result.append(edgeString);
}
result.append(child.toDot());
result.append(child.toDot(showRepeatingEdges));
}
return result.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public String print() {
* @return DOT representation of the StoredNode.
*/
@Override
public String toDot() {
public String toDot(Boolean showRepeatingEdges) {
String result = getClass().getSimpleName() + getLocation().orElse(Bytes.EMPTY) + "[location=\"" + getLocation().orElse(Bytes.EMPTY) + "\"]\n";
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@ public class SimpleVerkleTrieTest {


@Test
public void toDotTreeWithOneValue() {
public void testToDotTrieOneValueNoRepeatingEdges() {
SimpleVerkleTrie<Bytes32, Bytes32> trie = new SimpleVerkleTrie<>();
Bytes32 key = Bytes32.fromHexString("0x00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff");
Bytes32 value = Bytes32.fromHexString("0x1000000000000000000000000000000000000000000000000000000000000000");
trie.put(key, value);

System.out.println(trie.toDotTree());
System.out.println(trie.toDotTree(false));
}


@Test
public void toDotTreeWithTwoValues() {
public void testToDotTrieTwoValuesNoRepeatingEdges() {
SimpleVerkleTrie<Bytes32, Bytes32> trie = new SimpleVerkleTrie<Bytes32, Bytes32>();
Bytes32 key1 = Bytes32.fromHexString("0x00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff");
Bytes32 value1 = Bytes32.fromHexString("0x1000000000000000000000000000000000000000000000000000000000000000");
Expand All @@ -48,7 +48,31 @@ public void toDotTreeWithTwoValues() {
trie.put(key1, value1);
trie.put(key2, value2);

System.out.println(trie.toDotTree());
System.out.println(trie.toDotTree(false));
}
@Test
public void testToDotTrieOneValueRepeatingEdges() {
SimpleVerkleTrie<Bytes32, Bytes32> trie = new SimpleVerkleTrie<>();
Bytes32 key = Bytes32.fromHexString("0x00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff");
Bytes32 value = Bytes32.fromHexString("0x1000000000000000000000000000000000000000000000000000000000000000");
trie.put(key, value);

System.out.println(trie.toDotTree(true));
}


@Test
public void testToDotTrieTwoValuesRepeatingEdges() {
SimpleVerkleTrie<Bytes32, Bytes32> trie = new SimpleVerkleTrie<Bytes32, Bytes32>();
Bytes32 key1 = Bytes32.fromHexString("0x00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff");
Bytes32 value1 = Bytes32.fromHexString("0x1000000000000000000000000000000000000000000000000000000000000000");
Bytes32 key2 = Bytes32.fromHexString("0x00112233445566778899aabbccddeeff00112233445566778899aabbccddee00");
Bytes32 value2 = Bytes32.fromHexString("0x0100000000000000000000000000000000000000000000000000000000000000");

trie.put(key1, value1);
trie.put(key2, value2);

System.out.println(trie.toDotTree(true));
}
@Test
public void testEmptyTrie() {
Expand Down

0 comments on commit 330d58b

Please sign in to comment.