Commit a42ee2e3 authored by Javier Costa's avatar Javier Costa
Browse files

Open graph automaticcaly

parent 64eb5e2f
Loading
Loading
Loading
Loading
+23 −4
Original line number Diff line number Diff line
@@ -15,9 +15,8 @@ import tfm.utils.Logger;
import tfm.visitors.CFGVisitor;
import tfm.visitors.PDGVisitor;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.*;
import java.util.Arrays;

import static guru.nidi.graphviz.model.Factory.graph;
import static guru.nidi.graphviz.model.Factory.node;
@@ -26,7 +25,7 @@ public class Main {

    private static long t0;

    public static void main(String[] args) throws IOException {
    public static void main(String[] args) throws IOException, InterruptedException {
        File file = new File("/home/jacosro/IdeaProjects/TFM/src/main/java/tfm/programs/Example1.java");
        CompilationUnit compilationUnit = JavaParser.parse(file);

@@ -50,6 +49,8 @@ public class Main {
        Logger.log(graph.toGraphvizRepresentation());
        Logger.log();
        Logger.format("Done in %.2f ms", (tt - t0) / 10e6);

        openGraphAsPng(graph);
    }

    public static CFGGraph cfg(File file, CompilationUnit cu) {
@@ -80,4 +81,22 @@ public class Main {

        return pdgGraph;
    }

    private static void openGraphAsPng(Graph graph) throws IOException, InterruptedException {
        PrintWriter printWriter = new PrintWriter("./out/graph.txt");
        printWriter.println(graph.toGraphvizRepresentation());
        printWriter.close();

        int exit = new ProcessBuilder(
                Arrays.asList("dot", "-Tpng", "./out/graph.txt", "-o", "./out/graph.png")
        ).start()
        .waitFor();

        if (exit != 0) {
            Logger.log("Error procesando el archivo de grafo");
            return;
        }

        new ProcessBuilder(Arrays.asList("xdg-open", "./output/graph.png")).start();
    }
}
+7 −2
Original line number Diff line number Diff line
@@ -52,8 +52,13 @@ public abstract class Arc<D extends ArcData> extends edg.graphlib.Arrow<String,

        Arc arc = (Arc) o;

        Node from = (Node) arc.getFrom();
        Node from2 = (Node) getFrom();
        Node to = (Node) getTo();
        Node to2 = (Node) arc.getTo();

        return Objects.equals(arc.getData(), getData()) &&
                Objects.equals(arc.getFrom(), getFrom()) &&
                Objects.equals(arc.getTo(), getTo());
                Objects.equals(from.getId(), from2.getId()) &&
                Objects.equals(to.getId(), to2.getId());
    }
}
+16 −5
Original line number Diff line number Diff line
@@ -7,9 +7,7 @@ import tfm.arcs.Arc;
import tfm.arcs.data.ArcData;
import tfm.nodes.Node;

import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.*;
import java.util.stream.Collectors;

/**
@@ -87,9 +85,22 @@ public abstract class Graph<NodeType extends Node> extends edg.graphlib.Graph<St

    public abstract NodeType addNode(String instruction, Statement statement);

    public String toString() {
    @SuppressWarnings("unchecked")
    public Set<NodeType> getNodes() {
        return getVerticies().stream()
                .map(edg.graphlib.Vertex::toString)
                .map(vertex -> (NodeType) vertex)
                .collect(Collectors.toSet());
    }

    public Set<Arc<ArcData>> getArcs() {
        return getArrows().stream()
                .map(arrow -> (Arc<ArcData>) arrow)
                .collect(Collectors.toSet());
    }

    public String toString() {
        return getNodes().stream()
                .map(Node::toString)
                .collect(Collectors.joining(System.lineSeparator()));
    }

+5 −4
Original line number Diff line number Diff line
@@ -72,8 +72,9 @@ public abstract class PDGGraph extends Graph<PDGNode> {
    public String toGraphvizRepresentation() {
        String lineSep = System.lineSeparator();

        String nodesDeclaration = getVerticies().stream()
                .map(vertex -> ((Node) vertex).toGraphvizRepresentation())
        String nodesDeclaration = getNodes().stream()
                .sorted(Comparator.comparingInt(Node::getId))
                .map(Node::toGraphvizRepresentation)
                .collect(Collectors.joining(lineSep));

        StringBuilder rankedNodes = new StringBuilder();
@@ -104,9 +105,9 @@ public abstract class PDGGraph extends Graph<PDGNode> {
        }

        String arrows =
                getArrows().stream()
                getArcs().stream()
                        .sorted(Comparator.comparingInt(arrow -> ((Node) arrow.getFrom()).getId()))
                        .map(arrow -> ((Arc) arrow).toGraphvizRepresentation())
                        .map(Arc::toGraphvizRepresentation)
                        .collect(Collectors.joining(lineSep));


+12 −12
Original line number Diff line number Diff line
@@ -10,22 +10,22 @@ public class Example1 {

//        if (x < y) {
            while (x < y) {
//                y = x;

                while(y < x) {
                    y += x;
                    x = y;

                    while (x > 1) {
                        y += 12;
                        x = y;
                    }
                }
                y = x;

//                while(y < x) {
//                    y += x;
//                    x = y;
//
//                    while (x > 1) {
//                        y += 12;
//                        x = y;
//                    }
//                }

                x++;
            }

            y = x + 1;
//            y = x + 1;
//        } else {
//            x = 4;
//            y *= x;