package cipriano.util;

import cipriano.Principal;
import cipriano.model.Transicao;
import cipriano.util.Enums.EstadoEnum;
import guru.nidi.graphviz.attribute.Shape;
import guru.nidi.graphviz.engine.Graphviz;
import guru.nidi.graphviz.model.Graph;
import guru.nidi.graphviz.model.Node;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import static guru.nidi.graphviz.model.Factory.graph;
import static guru.nidi.graphviz.model.Factory.node;
import static guru.nidi.graphviz.model.Factory.to;

/**
 * Created by henrique on 26/08/16.
 */
public class GraphvizUtil {
    public static void gerarGrafo(Set<Transicao> transicaoSet) {
        Map<String, Transicao> transicaoMap = new HashMap<>();
        transicaoSet.forEach(transicao -> transicaoMap.put(transicao.toString(), transicao));

        Graph digraph = graph("G");
        boolean inicioUsed = false;
        for (Transicao transicao : transicaoSet) {
            Node inicio = null;
            if(transicao.getAtual().getEstado().equals(EstadoEnum.INICIAL) && !inicioUsed){
                inicio = node("_start_").attr(Shape.NONE);
                digraph = digraph.node(inicio);
                inicioUsed = true;
            }
            Node de = node(transicao.getAtual().getNome());
            Node para = node(transicao.getProximoEstadoNome());
            digraph = digraph.node(de.link(to(para)));

            if(transicao.getAtual().getEstado().equals(EstadoEnum.FINAL) || transicao.getAtual().getEstado().equals(EstadoEnum.AMBOS)){
                de = de.attr(Shape.DOUBLE_CIRCLE);
            }
            if(inicio != null){
                digraph = digraph.node(inicio.link(to(de)));
            }
        }
        Graphviz.fromGraph(digraph).renderToFile(new File(Principal.class.getResource("/images").getFile().concat("/grafo.png")));
    }
}

@@ -237,24 +237,62 @@
 odel.Graph;%0A
+import guru.nidi.graphviz.model.Link;%0A
 import guru.
@@ -317,16 +317,16 @@
 l.Node;%0A
-
 %0Aimport 
@@ -898,16 +898,76 @@
 aph(%22G%22)
+.directed();%0A        digraph.general().attr(%22rankdir%22, %22LR%22)
 ;%0A      
@@ -1442,32 +1442,107 @@
 moEstadoNome());
+%0A%0A            Link link = to(para).attr(%22label%22, transicao.getCaractere());
 %0A            dig
@@ -1569,24 +1569,20 @@
 de.link(
-to(para)
+link
 ));%0A%0A   

Ajuste no dígrafo gerado