Initial commit
This commit is contained in:
commit
a52d26a18e
24 changed files with 1091 additions and 0 deletions
40
.classpath
Normal file
40
.classpath
Normal file
|
@ -0,0 +1,40 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="src" output="target/classes" path="src/main/java">
|
||||
<attributes>
|
||||
<attribute name="optional" value="true"/>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
|
||||
<attributes>
|
||||
<attribute name="optional" value="true"/>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="src" path="src/test/res"/>
|
||||
<classpathentry kind="src" path="src/main/res"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
|
||||
<attributes>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
|
||||
<attributes>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/Maven: junit:junit:3.8.1"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/Maven: com.google.code.gson:gson:2.3.1"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/Maven: com.github.javaparser:javaparser-symbol-solver-core:3.5.17"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/Maven: com.github.javaparser:javaparser-symbol-solver-logic:3.5.17"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/Maven: org.javassist:javassist:3.22.0-GA"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/Maven: com.github.javaparser:javaparser-symbol-solver-model:3.5.17"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/Maven: com.github.javaparser:javaparser-core:3.5.17"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/Maven: com.google.guava:guava:23.4-jre"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/Maven: com.google.code.findbugs:jsr305:1.3.9"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/Maven: com.google.errorprone:error_prone_annotations:2.0.18"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/Maven: com.google.j2objc:j2objc-annotations:1.1"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/Maven: org.codehaus.mojo:animal-sniffer-annotations:1.14"/>
|
||||
<classpathentry kind="output" path="target/classes"/>
|
||||
</classpath>
|
23
.project
Normal file
23
.project
Normal file
|
@ -0,0 +1,23 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>LTD_Proyecto2_Alumnos</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.m2e.core.maven2Builder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
<nature>org.eclipse.m2e.core.maven2Nature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
25
LTD_Proyecto2_Alumnos.eml
Normal file
25
LTD_Proyecto2_Alumnos.eml
Normal file
|
@ -0,0 +1,25 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<component LANGUAGE_LEVEL="JDK_1_5" inheritJdk="true">
|
||||
<output-test url="file://$MODULE_DIR$/target/test-classes"/>
|
||||
<contentEntry url="file://$MODULE_DIR$">
|
||||
<testFolder url="file://$MODULE_DIR$/src/test/java"/>
|
||||
<testFolder url="file://$MODULE_DIR$/src/test/res"/>
|
||||
<excludeFolder url="file://$MODULE_DIR$/src/main/java/temporal"/>
|
||||
<excludeFolder url="file://$MODULE_DIR$/target"/>
|
||||
</contentEntry>
|
||||
<lib name="Maven: junit:junit:3.8.1" scope="TEST"/>
|
||||
<levels>
|
||||
<level name="Maven: junit:junit:3.8.1" value="project"/>
|
||||
<level name="Maven: com.google.code.gson:gson:2.3.1" value="project"/>
|
||||
<level name="Maven: com.github.javaparser:javaparser-symbol-solver-core:3.5.17" value="project"/>
|
||||
<level name="Maven: com.github.javaparser:javaparser-symbol-solver-logic:3.5.17" value="project"/>
|
||||
<level name="Maven: org.javassist:javassist:3.22.0-GA" value="project"/>
|
||||
<level name="Maven: com.github.javaparser:javaparser-symbol-solver-model:3.5.17" value="project"/>
|
||||
<level name="Maven: com.github.javaparser:javaparser-core:3.5.17" value="project"/>
|
||||
<level name="Maven: com.google.guava:guava:23.4-jre" value="project"/>
|
||||
<level name="Maven: com.google.code.findbugs:jsr305:1.3.9" value="project"/>
|
||||
<level name="Maven: com.google.errorprone:error_prone_annotations:2.0.18" value="project"/>
|
||||
<level name="Maven: com.google.j2objc:j2objc-annotations:1.1" value="project"/>
|
||||
<level name="Maven: org.codehaus.mojo:animal-sniffer-annotations:1.14" value="project"/>
|
||||
</levels>
|
||||
</component>
|
2
LTD_Proyecto2_Alumnos.iml
Normal file
2
LTD_Proyecto2_Alumnos.iml
Normal file
|
@ -0,0 +1,2 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module classpath="eclipse" classpath-dir="$MODULE_DIR$" org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4" />
|
44
pom.xml
Normal file
44
pom.xml
Normal file
|
@ -0,0 +1,44 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.upv.ltd.graphs</groupId>
|
||||
<artifactId>graphs</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>graphs</name>
|
||||
<url>http://maven.apache.org</url>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>3.8.1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
<artifactId>gson</artifactId>
|
||||
<version>2.3.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.javaparser</groupId>
|
||||
<artifactId>javaparser-symbol-solver-core</artifactId>
|
||||
<version>3.5.17</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<developers>
|
||||
<developer>
|
||||
<name>Josep Silva</name>
|
||||
<email>jsilva@dsic.upv.es</email>
|
||||
<url>http://www.dsic.upv.es/~jsilva</url>
|
||||
<id>jsilva</id>
|
||||
</developer>
|
||||
</developers>
|
||||
</project>
|
366
src/main/java/grafos/GraphViz.java
Normal file
366
src/main/java/grafos/GraphViz.java
Normal file
|
@ -0,0 +1,366 @@
|
|||
package grafos;
|
||||
|
||||
//GraphViz.java - a simple API to call dot from Java programs
|
||||
|
||||
/*$Id$*/
|
||||
/*
|
||||
******************************************************************************
|
||||
* *
|
||||
* (c) Copyright Laszlo Szathmary *
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or modify it *
|
||||
* under the terms of the GNU Lesser General Public License as published by *
|
||||
* the Free Software Foundation; either version 2.1 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* This program is distributed in the hope that it will be useful, but *
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public *
|
||||
* License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU Lesser General Public License *
|
||||
* along with this program; if not, write to the Free Software Foundation, *
|
||||
* Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
|
||||
* *
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.FileWriter;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* <dl>
|
||||
* <dt>Purpose: GraphViz Java API
|
||||
* <dd>
|
||||
*
|
||||
* <dt>Description:
|
||||
* <dd> With this Java class you can simply call dot
|
||||
* from your Java programs.
|
||||
* <dt>Example usage:
|
||||
* <dd>
|
||||
* <pre>
|
||||
* GraphViz gv = new GraphViz();
|
||||
* gv.addln(gv.start_graph());
|
||||
* gv.addln("A -> B;");
|
||||
* gv.addln("A -> C;");
|
||||
* gv.addln(gv.end_graph());
|
||||
* System.out.println(gv.getDotSource());
|
||||
*
|
||||
* String type = "gif";
|
||||
* File out = new File("out." + type); // out.gif in this example
|
||||
* gv.writeGraphToFile( gv.getGraph( gv.getDotSource(), type ), out );
|
||||
* </pre>
|
||||
* </dd>
|
||||
*
|
||||
* </dl>
|
||||
*
|
||||
* @version v0.5.1, 2013/03/18 (March) -- Patch of Juan Hoyos (Mac support)
|
||||
* @version v0.5, 2012/04/24 (April) -- Patch of Abdur Rahman (OS detection + start subgraph +
|
||||
* read config file)
|
||||
* @version v0.4, 2011/02/05 (February) -- Patch of Keheliya Gallaba is added. Now you
|
||||
* can specify the type of the output file: gif, dot, fig, pdf, ps, svg, png, etc.
|
||||
* @version v0.3, 2010/11/29 (November) -- Windows support + ability to read the graph from a text file
|
||||
* @version v0.2, 2010/07/22 (July) -- bug fix
|
||||
* @version v0.1, 2003/12/04 (December) -- first release
|
||||
* @author Laszlo Szathmary (<a href="jabba.laci@gmail.com">jabba.laci@gmail.com</a>)
|
||||
*/
|
||||
public class GraphViz
|
||||
{
|
||||
/**
|
||||
* Detects the client's operating system.
|
||||
*/
|
||||
private final static String osName = System.getProperty("os.name").replaceAll("\\s","");
|
||||
|
||||
/**
|
||||
* Load the config.properties file.
|
||||
*/
|
||||
private final static String cfgProp = "./src/main/res/config.properties";
|
||||
private final static Properties configFile = new Properties() {
|
||||
private final static long serialVersionUID = 1L; {
|
||||
try {
|
||||
load(new FileInputStream(cfgProp));
|
||||
} catch (Exception e) {}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* The dir. where temporary files will be created.
|
||||
*/
|
||||
private static String TEMP_DIR = "/tmp/";
|
||||
|
||||
/**
|
||||
* Where is your dot program located? It will be called externally.
|
||||
*/
|
||||
private static String DOT = configFile.getProperty("dotFor" + osName);
|
||||
|
||||
/**
|
||||
* The image size in dpi. 96 dpi is normal size. Higher values are 10% higher each.
|
||||
* Lower values 10% lower each.
|
||||
*
|
||||
* dpi patch by Peter Mueller
|
||||
*/
|
||||
private int[] dpiSizes = {46, 51, 57, 63, 70, 78, 86, 96, 106, 116, 128, 141, 155, 170, 187, 206, 226, 249};
|
||||
|
||||
/**
|
||||
* Define the index in the image size array.
|
||||
*/
|
||||
private int currentDpiPos = 7;
|
||||
|
||||
/**
|
||||
* Increase the image size (dpi).
|
||||
*/
|
||||
public void increaseDpi() {
|
||||
if ( this.currentDpiPos < (this.dpiSizes.length - 1) ) {
|
||||
++this.currentDpiPos;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Decrease the image size (dpi).
|
||||
*/
|
||||
public void decreaseDpi() {
|
||||
if (this.currentDpiPos > 0) {
|
||||
--this.currentDpiPos;
|
||||
}
|
||||
}
|
||||
|
||||
public int getImageDpi() {
|
||||
return this.dpiSizes[this.currentDpiPos];
|
||||
}
|
||||
|
||||
/**
|
||||
* The source of the graph written in dot language.
|
||||
*/
|
||||
private StringBuilder graph = new StringBuilder();
|
||||
|
||||
/**
|
||||
* Constructor: creates a new GraphViz object that will contain
|
||||
* a graph.
|
||||
*/
|
||||
public GraphViz() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the graph's source description in dot language.
|
||||
* @return Source of the graph in dot language.
|
||||
*/
|
||||
public String getDotSource() {
|
||||
return this.graph.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a string to the graph's source (without newline).
|
||||
*/
|
||||
public void add(String line) {
|
||||
this.graph.append(line);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a string to the graph's source (with newline).
|
||||
*/
|
||||
public void addln(String line) {
|
||||
this.graph.append(line + "\n");
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a newline to the graph's source.
|
||||
*/
|
||||
public void addln() {
|
||||
this.graph.append('\n');
|
||||
}
|
||||
|
||||
public void clearGraph(){
|
||||
this.graph = new StringBuilder();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the graph as an image in binary format.
|
||||
* @param dot_source Source of the graph to be drawn.
|
||||
* @param type Type of the output image to be produced, e.g.: gif, dot, fig, pdf, ps, svg, png.
|
||||
* @return A byte array containing the image of the graph.
|
||||
*/
|
||||
public byte[] getGraph(String dot_source, String type)
|
||||
{
|
||||
File dot;
|
||||
byte[] img_stream = null;
|
||||
|
||||
try {
|
||||
dot = writeDotSourceToFile(dot_source);
|
||||
if (dot != null)
|
||||
{
|
||||
img_stream = get_img_stream(dot, type);
|
||||
if (dot.delete() == false)
|
||||
System.err.println("Warning: " + dot.getAbsolutePath() + " could not be deleted!");
|
||||
return img_stream;
|
||||
}
|
||||
return null;
|
||||
} catch (java.io.IOException ioe) { return null; }
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes the graph's image in a file.
|
||||
* @param img A byte array containing the image of the graph.
|
||||
* @param file Name of the file to where we want to write.
|
||||
* @return Success: 1, Failure: -1
|
||||
*/
|
||||
public int writeGraphToFile(byte[] img, String file)
|
||||
{
|
||||
File to = new File(file);
|
||||
return writeGraphToFile(img, to);
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes the graph's image in a file.
|
||||
* @param img A byte array containing the image of the graph.
|
||||
* @param to A File object to where we want to write.
|
||||
* @return Success: 1, Failure: -1
|
||||
*/
|
||||
public int writeGraphToFile(byte[] img, File to)
|
||||
{
|
||||
try {
|
||||
FileOutputStream fos = new FileOutputStream(to);
|
||||
fos.write(img);
|
||||
fos.close();
|
||||
} catch (java.io.IOException ioe) { return -1; }
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* It will call the external dot program, and return the image in
|
||||
* binary format.
|
||||
* @param dot Source of the graph (in dot language).
|
||||
* @param type Type of the output image to be produced, e.g.: gif, dot, fig, pdf, ps, svg, png.
|
||||
* @return The image of the graph in .gif format.
|
||||
*/
|
||||
private byte[] get_img_stream(File dot, String type)
|
||||
{
|
||||
File img;
|
||||
byte[] img_stream = null;
|
||||
|
||||
try {
|
||||
img = File.createTempFile("graph_", "."+type, new File(GraphViz.TEMP_DIR));
|
||||
Runtime rt = Runtime.getRuntime();
|
||||
|
||||
// patch by Mike Chenault
|
||||
String[] args = {DOT, "-T"+type, "-Gdpi="+dpiSizes[this.currentDpiPos], dot.getAbsolutePath(), "-o", img.getAbsolutePath()};
|
||||
Process p = rt.exec(args);
|
||||
|
||||
p.waitFor();
|
||||
|
||||
FileInputStream in = new FileInputStream(img.getAbsolutePath());
|
||||
img_stream = new byte[in.available()];
|
||||
in.read(img_stream);
|
||||
// Close it if we need to
|
||||
if( in != null ) in.close();
|
||||
|
||||
if (img.delete() == false)
|
||||
System.err.println("Warning: " + img.getAbsolutePath() + " could not be deleted!");
|
||||
}
|
||||
catch (java.io.IOException ioe) {
|
||||
System.err.println("Error: in I/O processing of tempfile in dir " + GraphViz.TEMP_DIR+"\n");
|
||||
System.err.println(" or in calling external command");
|
||||
ioe.printStackTrace();
|
||||
}
|
||||
catch (java.lang.InterruptedException ie) {
|
||||
System.err.println("Error: the execution of the external program was interrupted");
|
||||
ie.printStackTrace();
|
||||
}
|
||||
|
||||
return img_stream;
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes the source of the graph in a file, and returns the written file
|
||||
* as a File object.
|
||||
* @param str Source of the graph (in dot language).
|
||||
* @return The file (as a File object) that contains the source of the graph.
|
||||
*/
|
||||
private File writeDotSourceToFile(String str) throws java.io.IOException
|
||||
{
|
||||
File temp;
|
||||
try {
|
||||
temp = File.createTempFile("dorrr",".dot", new File(GraphViz.TEMP_DIR));
|
||||
FileWriter fout = new FileWriter(temp);
|
||||
fout.write(str);
|
||||
BufferedWriter br=new BufferedWriter(new FileWriter("/tmp/dotsource.dot"));
|
||||
br.write(str);
|
||||
br.flush();
|
||||
br.close();
|
||||
fout.close();
|
||||
}
|
||||
catch (Exception e) {
|
||||
System.err.println("Error: I/O error while writing the dot source to temp file!");
|
||||
return null;
|
||||
}
|
||||
return temp;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string that is used to start a graph.
|
||||
* @return A string to open a graph.
|
||||
*/
|
||||
public String start_graph() {
|
||||
return "digraph G {";
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string that is used to end a graph.
|
||||
* @return A string to close a graph.
|
||||
*/
|
||||
public String end_graph() {
|
||||
return "}";
|
||||
}
|
||||
|
||||
/**
|
||||
* Takes the cluster or subgraph id as input parameter and returns a string
|
||||
* that is used to start a subgraph.
|
||||
* @return A string to open a subgraph.
|
||||
*/
|
||||
public String start_subgraph(int clusterid) {
|
||||
return "subgraph cluster_" + clusterid + " {";
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string that is used to end a graph.
|
||||
* @return A string to close a graph.
|
||||
*/
|
||||
public String end_subgraph() {
|
||||
return "}";
|
||||
}
|
||||
|
||||
/**
|
||||
* Read a DOT graph from a text file.
|
||||
*
|
||||
* @param input Input text file containing the DOT graph
|
||||
* source.
|
||||
*/
|
||||
public void readSource(String input)
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
try
|
||||
{
|
||||
FileInputStream fis = new FileInputStream(input);
|
||||
DataInputStream dis = new DataInputStream(fis);
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(dis));
|
||||
String line;
|
||||
while ((line = br.readLine()) != null) {
|
||||
sb.append(line);
|
||||
}
|
||||
dis.close();
|
||||
}
|
||||
catch (Exception e) {
|
||||
System.err.println("Error: " + e.getMessage());
|
||||
}
|
||||
|
||||
this.graph = sb;
|
||||
}
|
||||
|
||||
} // end of class GraphViz
|
79
src/main/java/grafos/Transformador.java
Normal file
79
src/main/java/grafos/Transformador.java
Normal file
|
@ -0,0 +1,79 @@
|
|||
package grafos;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.github.javaparser.JavaParser;
|
||||
import com.github.javaparser.ast.CompilationUnit;
|
||||
import com.github.javaparser.ast.Node;
|
||||
import com.github.javaparser.ast.comments.Comment;
|
||||
import com.github.javaparser.ast.visitor.VoidVisitor;
|
||||
|
||||
public class Transformador {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
// Ruta del fichero con el programa que vamos a transformar
|
||||
String ruta = "./src/test/res/ejemplos/Test_1";
|
||||
|
||||
// Abrimos el fichero original (un ".java")
|
||||
File original = new File(ruta + ".java");
|
||||
|
||||
// Parseamos el fichero original. Se crea una unidad de compilación (un AST).
|
||||
CompilationUnit cu = JavaParser.parse(original);
|
||||
|
||||
quitarComentarios(cu);
|
||||
|
||||
// Recorremos el AST
|
||||
List<String> arcos = new ArrayList<String>();
|
||||
VoidVisitor<List<String>> visitador = new Visitador();
|
||||
visitador.visit(cu,arcos);
|
||||
|
||||
// Imprimimos el CFG del programa
|
||||
String dotInfo = imprimirGrafo(arcos);
|
||||
|
||||
// Generamos un PDF con el CFG del programa
|
||||
System.out.print("\nGenerando PDF...");
|
||||
GraphViz gv=new GraphViz();
|
||||
gv.addln(gv.start_graph());
|
||||
gv.add(dotInfo);
|
||||
gv.addln(gv.end_graph());
|
||||
String type = "pdf"; // String type = "gif";
|
||||
// gv.increaseDpi();
|
||||
gv.decreaseDpi();
|
||||
gv.decreaseDpi();
|
||||
gv.decreaseDpi();
|
||||
gv.decreaseDpi();
|
||||
File destino_CFG = new File(ruta + "_CFG."+ type);
|
||||
gv.writeGraphToFile( gv.getGraph( gv.getDotSource(), type ), destino_CFG);
|
||||
System.out.println(" PDF generado!");
|
||||
}
|
||||
|
||||
// Imprime el grafo en la pantalla
|
||||
private static String imprimirGrafo(List<String> arcos)
|
||||
{
|
||||
String dotInfo="";
|
||||
for(String arco:arcos) {
|
||||
dotInfo += arco;
|
||||
System.out.println("ARCO: "+arco);
|
||||
}
|
||||
System.out.println("\nCFG:");
|
||||
System.out.println(dotInfo);
|
||||
|
||||
return dotInfo;
|
||||
}
|
||||
|
||||
// Elimina todos los comentarios de un nodo y sus hijos
|
||||
static void quitarComentarios(Node node){
|
||||
node.removeComment();
|
||||
for (Comment comment : node.getOrphanComments())
|
||||
{
|
||||
node.removeOrphanComment(comment);
|
||||
}
|
||||
// Do something with the node
|
||||
for (Node child : node.getChildNodes()){
|
||||
quitarComentarios(child);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
101
src/main/java/grafos/Visitador.java
Executable file
101
src/main/java/grafos/Visitador.java
Executable file
|
@ -0,0 +1,101 @@
|
|||
package grafos;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.github.javaparser.ast.NodeList;
|
||||
import com.github.javaparser.ast.body.MethodDeclaration;
|
||||
import com.github.javaparser.ast.stmt.BlockStmt;
|
||||
import com.github.javaparser.ast.stmt.ExpressionStmt;
|
||||
import com.github.javaparser.ast.stmt.Statement;
|
||||
import com.github.javaparser.ast.visitor.VoidVisitorAdapter;
|
||||
|
||||
|
||||
public class Visitador extends VoidVisitorAdapter<List<String>>
|
||||
{
|
||||
/********************************************************/
|
||||
/********************** Atributos ***********************/
|
||||
/********************************************************/
|
||||
|
||||
// Usamos un contador para numerar las instrucciones
|
||||
int contador=1;
|
||||
String nodoAnterior = "Start";
|
||||
String nodoActual = "";
|
||||
|
||||
/********************************************************/
|
||||
/*********************** Metodos ************************/
|
||||
/********************************************************/
|
||||
|
||||
// Visitador de métodos
|
||||
// Este visitador añade el nodo final al CFG
|
||||
@Override
|
||||
public void visit(MethodDeclaration methodDeclaration, List<String>collector)
|
||||
{
|
||||
// Visitamos el método
|
||||
super.visit(methodDeclaration, collector);
|
||||
|
||||
// Añadimos el nodo final al CFG
|
||||
collector.add(nodoAnterior+"-> Stop;");
|
||||
}
|
||||
|
||||
// Visitador de expresiones
|
||||
// Cada expresión encontrada genera un nodo en el CFG
|
||||
@Override
|
||||
public void visit(ExpressionStmt es, List<String>collector)
|
||||
{
|
||||
// Creamos el nodo actual
|
||||
nodoActual = crearNodo(es);
|
||||
|
||||
crearArcos(collector);
|
||||
|
||||
nodoAnterior = nodoActual;
|
||||
|
||||
// Seguimos visitando...
|
||||
super.visit(es, collector);
|
||||
}
|
||||
|
||||
// Añade un arco desde el último nodo hasta el nodo actual (se le pasa como parametro)
|
||||
private void añadirArcoSecuencialCFG(List<String>collector)
|
||||
{
|
||||
System.out.println("NODO: " + nodoActual);
|
||||
|
||||
String arco = nodoAnterior + "->" + nodoActual + ";";
|
||||
collector.add(arco);
|
||||
}
|
||||
|
||||
// Crear arcos
|
||||
private void crearArcos(List<String>collector)
|
||||
{
|
||||
añadirArcoSecuencialCFG(collector);
|
||||
}
|
||||
|
||||
// Crear nodo
|
||||
// Añade un arco desde el nodo actual hasta el último control
|
||||
private String crearNodo(Object objeto)
|
||||
{
|
||||
return "\"("+ contador++ +") "+quitarComillas(objeto.toString())+"\"";
|
||||
}
|
||||
|
||||
// Sustituye " por \" en un string: Sirve para eliminar comillas.
|
||||
private static String quitarComillas(String texto)
|
||||
{
|
||||
return texto.replace("\"", "\\\"");
|
||||
}
|
||||
|
||||
// Dada una sentencia,
|
||||
// Si es una <EFBFBD>nica instrucci<EFBFBD>n, devuelve un bloque equivalente
|
||||
// Si es un bloque, lo devuelve
|
||||
private BlockStmt convertirEnBloque(Statement statement)
|
||||
{
|
||||
if (statement instanceof BlockStmt)
|
||||
return (BlockStmt) statement;
|
||||
|
||||
BlockStmt block = new BlockStmt();
|
||||
NodeList<Statement> blockStmts = new NodeList<Statement>();
|
||||
blockStmts.add(statement);
|
||||
|
||||
block.setStatements(blockStmts);
|
||||
|
||||
return block;
|
||||
}
|
||||
|
||||
}
|
23
src/main/res/config.properties
Normal file
23
src/main/res/config.properties
Normal file
|
@ -0,0 +1,23 @@
|
|||
##############################################################
|
||||
# Linux Configurations #
|
||||
##############################################################
|
||||
# The dir. where temporary files will be created.
|
||||
tempDirForLinux = /tmp
|
||||
# Where is your dot program located? It will be called externally.
|
||||
dotForLinux = /usr/bin/dot
|
||||
|
||||
##############################################################
|
||||
# Windows Configurations #
|
||||
##############################################################
|
||||
# The dir. where temporary files will be created.
|
||||
tempDirForWindows = c:/temp
|
||||
# Where is your dot program located? It will be called externally.
|
||||
dotForWindows = "c:/Program Files (x86)/Graphviz 2.28/bin/dot.exe"
|
||||
|
||||
##############################################################
|
||||
# Mac Configurations #
|
||||
##############################################################
|
||||
# The dir. where temporary files will be created.
|
||||
tempDirForMacOSX = /tmp
|
||||
# Where is your dot program located? It will be called externally.
|
||||
dotForMacOSX = /usr/local/bin/dot
|
16
src/test/res/ejemplos/Bucles_1.java
Executable file
16
src/test/res/ejemplos/Bucles_1.java
Executable file
|
@ -0,0 +1,16 @@
|
|||
package ejemplos;
|
||||
|
||||
public class Bucles_1 {
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
// BUCLE WHILE (sin anidamiento)
|
||||
int x=1;
|
||||
while (x<=10)
|
||||
{
|
||||
System.out.print(x);
|
||||
x++;
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
}
|
24
src/test/res/ejemplos/Bucles_2.java
Executable file
24
src/test/res/ejemplos/Bucles_2.java
Executable file
|
@ -0,0 +1,24 @@
|
|||
package ejemplos;
|
||||
|
||||
public class Bucles_2 {
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
// BUCLE WHILE anidado a otro WHILE
|
||||
System.out.println("Empieza bucle WHILE anidado a otro WHILE:");
|
||||
int x=1;
|
||||
char y='a';
|
||||
while (x<=10)
|
||||
{
|
||||
System.out.print(" "+x);
|
||||
y='a';
|
||||
while (y<='c')
|
||||
{
|
||||
System.out.print(" "+y);
|
||||
y++;
|
||||
}
|
||||
x++;
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
}
|
37
src/test/res/ejemplos/Bucles_3.java
Executable file
37
src/test/res/ejemplos/Bucles_3.java
Executable file
|
@ -0,0 +1,37 @@
|
|||
package ejemplos;
|
||||
|
||||
public class Bucles_3 {
|
||||
|
||||
public static void main(String[] args) {
|
||||
int x;
|
||||
|
||||
// BUCLE FOR (sin anidamiento)
|
||||
System.out.println("Empieza bucle FOR:");
|
||||
for (x=1;x<=10;x++)
|
||||
{
|
||||
System.out.print(" "+x);
|
||||
}
|
||||
System.out.println();
|
||||
|
||||
// BUCLE WHILE (sin anidamiento)
|
||||
System.out.println("Empieza bucle WHILE:");
|
||||
x=1;
|
||||
while (x<=10)
|
||||
{
|
||||
System.out.print(" "+x);
|
||||
x++;
|
||||
}
|
||||
System.out.println();
|
||||
|
||||
// BUCLE DO WHILE (sin anidamiento)
|
||||
System.out.println("Empieza bucle DO WHILE:");
|
||||
x=1;
|
||||
do{
|
||||
System.out.print(" "+x);
|
||||
x++;
|
||||
}
|
||||
while (x<=10);
|
||||
System.out.println();
|
||||
|
||||
}
|
||||
}
|
41
src/test/res/ejemplos/Bucles_4.java
Executable file
41
src/test/res/ejemplos/Bucles_4.java
Executable file
|
@ -0,0 +1,41 @@
|
|||
package ejemplos;
|
||||
|
||||
public class Bucles_4 {
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
int x=1;
|
||||
|
||||
//Bucle 1: Contador
|
||||
while (x<10)
|
||||
{
|
||||
System.out.println(x);
|
||||
x++;
|
||||
}
|
||||
|
||||
//Bucle 2: Sumatorio
|
||||
int suma=0;
|
||||
int y=1;
|
||||
while (y<10)
|
||||
{
|
||||
suma += y;
|
||||
y++;
|
||||
}
|
||||
System.out.println(suma);
|
||||
|
||||
//Bucle 3: Sumatorio
|
||||
int sumatorio = 0;
|
||||
int min = 10;
|
||||
int max = 100;
|
||||
for (int num = min; num <= max; num++)
|
||||
{
|
||||
sumatorio += num;
|
||||
}
|
||||
System.out.println(sumatorio);
|
||||
|
||||
int count = 0;
|
||||
while (count < 10)
|
||||
count++;
|
||||
System.out.println(count);
|
||||
}
|
||||
}
|
52
src/test/res/ejemplos/Bucles_5.java
Executable file
52
src/test/res/ejemplos/Bucles_5.java
Executable file
|
@ -0,0 +1,52 @@
|
|||
package ejemplos;
|
||||
|
||||
public class Bucles_5 {
|
||||
|
||||
public static void main(String[] args) {
|
||||
int x = 0;
|
||||
char y = '0';
|
||||
|
||||
// BUCLE FOR anidado a otro FOR
|
||||
System.out.println("Empieza bucle FOR anidado a otro FOR:");
|
||||
for (x=1;x<=10;x++)
|
||||
{
|
||||
System.out.print(" "+x);
|
||||
for (y='a';y<='c';y++)
|
||||
{
|
||||
System.out.print(" "+y);
|
||||
}
|
||||
}
|
||||
System.out.println();
|
||||
|
||||
// BUCLE WHILE anidado a otro WHILE
|
||||
System.out.println("Empieza bucle WHILE anidado a otro WHILE:");
|
||||
x=1;
|
||||
while (x<=10)
|
||||
{
|
||||
System.out.print(" "+x);
|
||||
y='a';
|
||||
while (y<='c')
|
||||
{
|
||||
System.out.print(" "+y);
|
||||
y++;
|
||||
}
|
||||
x++;
|
||||
}
|
||||
System.out.println();
|
||||
|
||||
// BUCLE FOR anidado a bucle DO WHILE
|
||||
System.out.println("Empieza bucle FOR anidado a bucle DO WHILE:");
|
||||
x=1;
|
||||
do{
|
||||
System.out.print(" "+x);
|
||||
for (y='a';y<='c';y++)
|
||||
{
|
||||
System.out.print(" "+y);
|
||||
}
|
||||
x++;
|
||||
}
|
||||
while (x<=10);
|
||||
System.out.println();
|
||||
|
||||
}
|
||||
}
|
27
src/test/res/ejemplos/Bucles_6.java
Normal file
27
src/test/res/ejemplos/Bucles_6.java
Normal file
|
@ -0,0 +1,27 @@
|
|||
package ejemplos;
|
||||
|
||||
public class Bucles_6 {
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
// BUCLE WHILE (sin anidamiento)
|
||||
System.out.println("Empieza bucle WHILE:");
|
||||
int x=1;
|
||||
while (x<=10)
|
||||
{
|
||||
System.out.print(" "+x);
|
||||
x++;
|
||||
while (x<=10)
|
||||
{
|
||||
System.out.print(" "+x);
|
||||
x++;
|
||||
}
|
||||
}
|
||||
while (x<=10)
|
||||
{
|
||||
System.out.print(" "+x);
|
||||
x++;
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
}
|
13
src/test/res/ejemplos/Test_1.java
Normal file
13
src/test/res/ejemplos/Test_1.java
Normal file
|
@ -0,0 +1,13 @@
|
|||
package ejemplos;
|
||||
|
||||
public class Test_1 {
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
System.out.println("HOLA mundo");
|
||||
int x=1;
|
||||
x=2;
|
||||
x=3;
|
||||
x=4;
|
||||
}
|
||||
}
|
14
src/test/res/ejemplos/Test_2.java
Normal file
14
src/test/res/ejemplos/Test_2.java
Normal file
|
@ -0,0 +1,14 @@
|
|||
package ejemplos;
|
||||
|
||||
public class Test_2 {
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
int x=1;
|
||||
x++;
|
||||
++x;
|
||||
int y=0;
|
||||
x=x+y;
|
||||
System.out.println(x);
|
||||
}
|
||||
}
|
14
src/test/res/ejemplos/Test_3.java
Normal file
14
src/test/res/ejemplos/Test_3.java
Normal file
|
@ -0,0 +1,14 @@
|
|||
package ejemplos;
|
||||
|
||||
public class Test_3 {
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
int x=1;
|
||||
|
||||
if (x==1)
|
||||
x=2;
|
||||
x=3;
|
||||
x=4;
|
||||
}
|
||||
}
|
21
src/test/res/ejemplos/Test_4.java
Normal file
21
src/test/res/ejemplos/Test_4.java
Normal file
|
@ -0,0 +1,21 @@
|
|||
package ejemplos;
|
||||
|
||||
public class Test_4 {
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
int x=1;
|
||||
|
||||
if (x==1)
|
||||
{
|
||||
x=2;
|
||||
if (x>=1)
|
||||
{
|
||||
x=3;
|
||||
x=4;
|
||||
}
|
||||
}
|
||||
x=5;
|
||||
x=6;
|
||||
}
|
||||
}
|
22
src/test/res/ejemplos/Test_5.java
Normal file
22
src/test/res/ejemplos/Test_5.java
Normal file
|
@ -0,0 +1,22 @@
|
|||
package ejemplos;
|
||||
|
||||
public class Test_5 {
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
int x=1;
|
||||
|
||||
if (x==1)
|
||||
{
|
||||
x=2;
|
||||
if (x>=1)
|
||||
{
|
||||
x=3;
|
||||
x=4;
|
||||
}
|
||||
x=5;
|
||||
}
|
||||
x=6;
|
||||
x=7;
|
||||
}
|
||||
}
|
21
src/test/res/ejemplos/Test_6.java
Normal file
21
src/test/res/ejemplos/Test_6.java
Normal file
|
@ -0,0 +1,21 @@
|
|||
package ejemplos;
|
||||
|
||||
public class Test_6 {
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
int x=1;
|
||||
|
||||
if (x==1)
|
||||
{
|
||||
x=2;
|
||||
x=3;
|
||||
}
|
||||
else
|
||||
{
|
||||
x=4;
|
||||
x=5;
|
||||
}
|
||||
x=6;
|
||||
}
|
||||
}
|
23
src/test/res/ejemplos/Test_7.java
Normal file
23
src/test/res/ejemplos/Test_7.java
Normal file
|
@ -0,0 +1,23 @@
|
|||
package ejemplos;
|
||||
|
||||
|
||||
public class Test_7 {
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
int x=1;
|
||||
|
||||
if (x==1)
|
||||
{
|
||||
x=2;
|
||||
}
|
||||
else x=3;
|
||||
x=4;
|
||||
if (x==2)
|
||||
{
|
||||
x=5;
|
||||
}
|
||||
else if (x==3) x=6;
|
||||
x=7;
|
||||
}
|
||||
}
|
22
src/test/res/ejemplos/Test_8.java
Normal file
22
src/test/res/ejemplos/Test_8.java
Normal file
|
@ -0,0 +1,22 @@
|
|||
package ejemplos;
|
||||
|
||||
public class Test_8 {
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
int x=1;
|
||||
|
||||
if (x==1)
|
||||
{
|
||||
x=2;
|
||||
}
|
||||
x=5;
|
||||
x=6;
|
||||
if (x==2)
|
||||
{
|
||||
x=7;
|
||||
}
|
||||
if (x==3) x=8;
|
||||
x=9;
|
||||
}
|
||||
}
|
41
src/test/res/ejemplos/Test_9.java
Normal file
41
src/test/res/ejemplos/Test_9.java
Normal file
|
@ -0,0 +1,41 @@
|
|||
package ejemplos;
|
||||
|
||||
public class Test_9 {
|
||||
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
// ANIDAMIENTO de IF y WHILE
|
||||
|
||||
// ANIDAMIENTO de IF y WHILE 2
|
||||
|
||||
int x=0;
|
||||
if (x>1)
|
||||
{
|
||||
x=1;
|
||||
while (x>2)
|
||||
{
|
||||
x=2;
|
||||
while (x>3)
|
||||
{
|
||||
x=3;
|
||||
if (x>4)
|
||||
{
|
||||
x=4;
|
||||
if (x>5)
|
||||
{
|
||||
x=5;
|
||||
}
|
||||
x--;
|
||||
}
|
||||
x--;
|
||||
}
|
||||
x--;
|
||||
}
|
||||
x--;
|
||||
}
|
||||
|
||||
x--;
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue