All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.vavr.render.dot.DotFile Maven / Gradle / Ivy

The newest version!
/*                        __    __  __  __    __  ___
 *                       \  \  /  /    \  \  /  /  __/
 *                        \  \/  /  /\  \  \/  /  /
 *                         \____/__/  \__\____/__/.ɪᴏ
 * ᶜᵒᵖʸʳᶦᵍʰᵗ ᵇʸ ᵛᵃᵛʳ ⁻ ˡᶦᶜᵉⁿˢᵉᵈ ᵘⁿᵈᵉʳ ᵗʰᵉ ᵃᵖᵃᶜʰᵉ ˡᶦᶜᵉⁿˢᵉ ᵛᵉʳˢᶦᵒⁿ ᵗʷᵒ ᵈᵒᵗ ᶻᵉʳᵒ
 */
package io.vavr.render.dot;

import io.vavr.collection.Tree;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Creates .dot representation of Vavr Tree.
 * 

* See Graphviz */ public class DotFile { public static DotFile create(Tree tree) { return new DotFile(tree); } public static void write(Tree tree, File file) throws IOException { FileWriter writer = new FileWriter(file); writer.write(create(tree).toString()); writer.close(); } public static void write(Tree tree, String fileName) throws IOException { write(tree, new File(fileName)); } private final Map labels = new HashMap<>(); private final List edges = new ArrayList<>(); private String name = "Tree"; private DotFile(Tree tree) { processTree(tree, null, 0); } public DotFile setName(String name) { this.name = name; return this; } @Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append("digraph ") .append(name) .append(" {\n"); for (Map.Entry label : labels.entrySet()) { sb.append(" ") .append(label.getKey()) .append(" [label=\"") .append(label.getValue().replace("\"", "\\\"")) .append("\"];\n"); } for (String edge : edges) { sb.append(" ").append(edge).append(";\n"); } sb.append("}\n"); return sb.toString(); } private int processTree(Tree tree, String parentLabel, int index) { Object value = tree.getValue(); String label = "lbl" + index; labels.put(label, value.toString()); if(parentLabel != null) { edges.add(parentLabel + " -> " + label); } for (Tree.Node child : tree.getChildren()) { index = processTree(child, label, index + 1); } return index; } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy