<commit_before>package ru.spbstu.kspt.librarymigration

import com.github.systemdir.gml.YedGmlWriter
import com.github.systemdir.gml.model.EdgeGraphicDefinition
import com.github.systemdir.gml.model.GraphicDefinition
import com.github.systemdir.gml.model.NodeGraphicDefinition
import com.github.systemdir.gml.model.YedGmlGraphicsProvider
import com.samskivert.mustache.Mustache
import org.jgrapht.EdgeFactory
import org.jgrapht.ext.VertexNameProvider
import org.jgrapht.graph.DirectedPseudograph
import java.awt.Color
import java.io.File
import java.io.FileReader
import java.io.FileWriter
import java.nio.file.Files
import java.nio.file.Paths


/**
 * Created by artyom on 16.06.16.
 */

fun toDOT(library: Library, visited: List<Edge> = listOf()): String {
    val edges = mutableListOf<Map<String, Any>>()
    val virtuals = library.stateMachines.map { it -> it to mutableListOf<Int>() }.toMap()

    var counter = 0
    for (machine in library.stateMachines) {
        for (edge in machine.getDisplayedEdges()) {
            val baseMap = mapOf<String, Any>(
                    "src" to edge.src.id(),
                    "dst" to edge.dst.id(),
                    "edge" to edge.label(),
                    "visited" to visited.contains(edge),
                    "style" to edge.getStyle())
            val values = if (edge is ExpressionEdge && edge.linkedEdge != null) {

                virtuals.get(machine)!!.add(counter)
                baseMap + mapOf(
                        "machine" to edge.linkedEdge!!.dst.id(),
                        "linkedEdge" to edge.linkedEdge!!.label(),
                        "counter" to counter++,
                        "linkedVisited" to visited.contains(edge.linkedEdge!!))
            } else baseMap
            edges += values
        }
    }

    val machines = library.stateMachines.mapIndexed {
        num, machine ->
        mapOf("num" to num,
                "name" to machine.label(),
                "vertices" to machine.states,
                "virtuals" to virtuals.get(machine)!!.map {
                    count ->
                    mapOf("counter" to count)
                })
    }

    val compiler = Mustache.compiler()
    val template = compiler.compile(FileReader("graphviz.mustache"))
    return template.execute(mapOf("machines" to machines, "edges" to edges))
}

fun graphvizRender(graph: String, prefix: String) {
    val folder = "pictures/"
    val folderPath = Paths.get(folder)
    if (Files.isDirectory(folderPath) == false) Files.createDirectory(folderPath)
    val dotPath = Paths.get(folder + prefix + ".dot")
    Files.write(dotPath, graph.toByteArray());
    DOTtoPDF(folder + prefix)
}

fun DOTtoPDF(prefix: String) {
    val dotPath = prefix + ".dot"
    val pdfPath = prefix + ".pdf"
    val rt = Runtime.getRuntime();
    val command = "dot -Tpdf %s -o %s".format(dotPath, pdfPath)
    rt.exec(command)
}

fun toJGrapht(library: Library): DirectedPseudograph<State, Edge> {
    System.out.println("Start")
    val graph = DirectedPseudograph<State, Edge>(
            EdgeFactory { source, target -> source.machine.edges.first { edge -> edge.src == source && edge.dst == target } }
    )

    val provider = ExampleGraphicsProvider()<commit_msg>Don't stop execution if Graphviz is not available.<commit_after>package ru.spbstu.kspt.librarymigration

import com.github.systemdir.gml.YedGmlWriter
import com.github.systemdir.gml.model.EdgeGraphicDefinition
import com.github.systemdir.gml.model.GraphicDefinition
import com.github.systemdir.gml.model.NodeGraphicDefinition
import com.github.systemdir.gml.model.YedGmlGraphicsProvider
import com.samskivert.mustache.Mustache
import org.jgrapht.EdgeFactory
import org.jgrapht.ext.VertexNameProvider
import org.jgrapht.graph.DirectedPseudograph
import java.awt.Color
import java.io.File
import java.io.FileReader
import java.io.FileWriter
import java.io.IOException
import java.nio.file.Files
import java.nio.file.Paths


/**
 * Created by artyom on 16.06.16.
 */

fun toDOT(library: Library, visited: List<Edge> = listOf()): String {
    val edges = mutableListOf<Map<String, Any>>()
    val virtuals = library.stateMachines.map { it -> it to mutableListOf<Int>() }.toMap()

    var counter = 0
    for (machine in library.stateMachines) {
        for (edge in machine.getDisplayedEdges()) {
            val baseMap = mapOf<String, Any>(
                    "src" to edge.src.id(),
                    "dst" to edge.dst.id(),
                    "edge" to edge.label(),
                    "visited" to visited.contains(edge),
                    "style" to edge.getStyle())
            val values = if (edge is ExpressionEdge && edge.linkedEdge != null) {

                virtuals.get(machine)!!.add(counter)
                baseMap + mapOf(
                        "machine" to edge.linkedEdge!!.dst.id(),
                        "linkedEdge" to edge.linkedEdge!!.label(),
                        "counter" to counter++,
                        "linkedVisited" to visited.contains(edge.linkedEdge!!))
            } else baseMap
            edges += values
        }
    }

    val machines = library.stateMachines.mapIndexed {
        num, machine ->
        mapOf("num" to num,
                "name" to machine.label(),
                "vertices" to machine.states,
                "virtuals" to virtuals.get(machine)!!.map {
                    count ->
                    mapOf("counter" to count)
                })
    }

    val compiler = Mustache.compiler()
    val template = compiler.compile(FileReader("graphviz.mustache"))
    return template.execute(mapOf("machines" to machines, "edges" to edges))
}

fun graphvizRender(graph: String, prefix: String) {
    val folder = "pictures/"
    val folderPath = Paths.get(folder)
    if (Files.isDirectory(folderPath) == false) Files.createDirectory(folderPath)
    val dotPath = Paths.get(folder + prefix + ".dot")
    Files.write(dotPath, graph.toByteArray());
    DOTtoPDF(folder + prefix)
}

fun DOTtoPDF(prefix: String) {
    val dotPath = prefix + ".dot"
    val pdfPath = prefix + ".pdf"
    val rt = Runtime.getRuntime();
    val command = "dot -Tpdf %s -o %s".format(dotPath, pdfPath)
    try {
        rt.exec(command)
    } catch (ex: IOException) {
        println("Graphviz is not available!")
    }
}

fun toJGrapht(library: Library): DirectedPseudograph<State, Edge> {
    System.out.println("Start")
    val graph = DirectedPseudograph<State, Edge>(
            EdgeFactory { source, target -> source.machine.edges.first { edge -> edge.src == source && edge.dst == target } }
    )

    val provider = ExampleGraphicsProvider()
{'fraction_non_alphanumeric': 0.11916942521817635, 'fraction_numerical': 0.0021065302437556425, 'mean_word_length': 2.478283621140764, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7587839', 'n_tokens_mistral': 2017, 'n_tokens_neox': 1940, 'n_words': 448}