Set indent to tabs and applied format to project

This commit is contained in:
Carlos Galindo 2019-03-26 20:29:29 +01:00
parent 782a4361c2
commit 0e5667582b
Signed by untrusted user who does not match committer: kauron
GPG key ID: 83E68706DEE119A3
21 changed files with 586 additions and 634 deletions

View file

@ -0,0 +1,14 @@
<component name="ProjectCodeStyleConfiguration">
<code_scheme name="Project" version="173">
<codeStyleSettings language="JAVA">
<indentOptions>
<option name="USE_TAB_CHARACTER" value="true" />
</indentOptions>
</codeStyleSettings>
<codeStyleSettings language="XML">
<indentOptions>
<option name="USE_TAB_CHARACTER" value="true" />
</indentOptions>
</codeStyleSettings>
</code_scheme>
</component>

View file

@ -0,0 +1,5 @@
<component name="ProjectCodeStyleConfiguration">
<state>
<option name="USE_PER_PROJECT_SETTINGS" value="true" />
</state>
</component>

74
pom.xml
View file

@ -1,44 +1,44 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <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"> 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> <modelVersion>4.0.0</modelVersion>
<groupId>com.upv.ltd.graphs</groupId> <groupId>com.upv.ltd.graphs</groupId>
<artifactId>graphs</artifactId> <artifactId>graphs</artifactId>
<version>0.0.1-SNAPSHOT</version> <version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>graphs</name> <name>graphs</name>
<url>http://maven.apache.org</url> <url>http://maven.apache.org</url>
<properties> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> </properties>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
<artifactId>junit</artifactId> <artifactId>junit</artifactId>
<version>3.8.1</version> <version>3.8.1</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.google.code.gson</groupId> <groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId> <artifactId>gson</artifactId>
<version>2.3.1</version> <version>2.3.1</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.github.javaparser</groupId> <groupId>com.github.javaparser</groupId>
<artifactId>javaparser-symbol-solver-core</artifactId> <artifactId>javaparser-symbol-solver-core</artifactId>
<version>3.5.17</version> <version>3.5.17</version>
</dependency> </dependency>
</dependencies> </dependencies>
<developers> <developers>
<developer> <developer>
<name>Josep Silva</name> <name>Josep Silva</name>
<email>jsilva@dsic.upv.es</email> <email>jsilva@dsic.upv.es</email>
<url>http://www.dsic.upv.es/~jsilva</url> <url>http://www.dsic.upv.es/~jsilva</url>
<id>jsilva</id> <id>jsilva</id>
</developer> </developer>
</developers> </developers>
</project> </project>

View file

@ -4,363 +4,353 @@ package grafos;
/*$Id$*/ /*$Id$*/
/* /*
****************************************************************************** ******************************************************************************
* * * *
* (c) Copyright Laszlo Szathmary * * (c) Copyright Laszlo Szathmary *
* * * *
* This program is free software; you can redistribute it and/or modify it * * 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 * * 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 * * the Free Software Foundation; either version 2.1 of the License, or *
* (at your option) any later version. * * (at your option) any later version. *
* * * *
* This program is distributed in the hope that it will be useful, but * * This program is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public *
* License for more details. * * License for more details. *
* * * *
* You should have received a copy of the GNU Lesser General Public License * * 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, * * along with this program; if not, write to the Free Software Foundation, *
* Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * * Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
* * * *
****************************************************************************** ******************************************************************************
*/ */
import java.io.BufferedReader; import java.io.*;
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; import java.util.Properties;
/** /**
* <dl> * <dl>
* <dt>Purpose: GraphViz Java API * <dt>Purpose: GraphViz Java API
* <dd> * <dd>
* *
* <dt>Description: * <dt>Description:
* <dd> With this Java class you can simply call dot * <dd> With this Java class you can simply call dot
* from your Java programs. * from your Java programs.
* <dt>Example usage: * <dt>Example usage:
* <dd> * <dd>
* <pre> * <pre>
* GraphViz gv = new GraphViz(); * GraphViz gv = new GraphViz();
* gv.addln(gv.start_graph()); * gv.addln(gv.start_graph());
* gv.addln("A -> B;"); * gv.addln("A -> B;");
* gv.addln("A -> C;"); * gv.addln("A -> C;");
* gv.addln(gv.end_graph()); * gv.addln(gv.end_graph());
* System.out.println(gv.getDotSource()); * System.out.println(gv.getDotSource());
* *
* String type = "gif"; * String type = "gif";
* File out = new File("out." + type); // out.gif in this example * File out = new File("out." + type); // out.gif in this example
* gv.writeGraphToFile( gv.getGraph( gv.getDotSource(), type ), out ); * gv.writeGraphToFile( gv.getGraph( gv.getDotSource(), type ), out );
* </pre> * </pre>
* </dd> * </dd>
* *
* </dl> * </dl>
* *
* @version v0.5.1, 2013/03/18 (March) -- Patch of Juan Hoyos (Mac support) * @author Laszlo Szathmary (<a href="jabba.laci@gmail.com">jabba.laci@gmail.com</a>)
* @version v0.5, 2012/04/24 (April) -- Patch of Abdur Rahman (OS detection + start subgraph + * @version v0.1, 2003/12/04 (December) -- first release
* read config file) */
* @version v0.4, 2011/02/05 (February) -- Patch of Keheliya Gallaba is added. Now you public class GraphViz {
* 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 * Detects the client's operating system.
* @version v0.2, 2010/07/22 (July) -- bug fix */
* @version v0.1, 2003/12/04 (December) -- first release private final static String osName = System.getProperty("os.name").replaceAll("\\s", "");
* @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. * Load the config.properties file.
*/ */
private final static String cfgProp = "./src/main/res/config.properties"; private final static String cfgProp = "./src/main/res/config.properties";
private final static Properties configFile = new Properties() { private final static Properties configFile = new Properties() {
private final static long serialVersionUID = 1L; { private final static long serialVersionUID = 1L;
try {
load(new FileInputStream(cfgProp));
} catch (Exception ignored) {}
}
};
/** {
* The dir. where temporary files will be created. try {
*/ load(new FileInputStream(cfgProp));
private static String TEMP_DIR = "/tmp/"; } catch (Exception ignored) {
}
}
};
/** /**
* Where is your dot program located? It will be called externally. * The dir. where temporary files will be created.
*/ */
private static String DOT = configFile.getProperty("dotFor" + osName); private static String TEMP_DIR = "/tmp/";
/** /**
* The image size in dpi. 96 dpi is normal size. Higher values are 10% higher each. * Where is your dot program located? It will be called externally.
* Lower values 10% lower each. */
* private static String DOT = configFile.getProperty("dotFor" + osName);
* 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. * The image size in dpi. 96 dpi is normal size. Higher values are 10% higher each.
*/ * Lower values 10% lower each.
private int currentDpiPos = 7; * <p>
* 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};
/** /**
* Increase the image size (dpi). * Define the index in the image size array.
*/ */
public void increaseDpi() { private int currentDpiPos = 7;
if ( this.currentDpiPos < (this.dpiSizes.length - 1) ) {
++this.currentDpiPos;
}
}
/** /**
* Decrease the image size (dpi). * Increase the image size (dpi).
*/ */
public void decreaseDpi() { public void increaseDpi() {
if (this.currentDpiPos > 0) { if (this.currentDpiPos < (this.dpiSizes.length - 1)) {
--this.currentDpiPos; ++this.currentDpiPos;
} }
} }
public int getImageDpi() { /**
return this.dpiSizes[this.currentDpiPos]; * Decrease the image size (dpi).
} */
public void decreaseDpi() {
if (this.currentDpiPos > 0) {
--this.currentDpiPos;
}
}
/** public int getImageDpi() {
* The source of the graph written in dot language. return this.dpiSizes[this.currentDpiPos];
*/ }
private StringBuilder graph = new StringBuilder();
/** /**
* Constructor: creates a new GraphViz object that will contain * The source of the graph written in dot language.
* a graph. */
*/ private StringBuilder graph = new StringBuilder();
public GraphViz() {
}
/** /**
* Returns the graph's source description in dot language. * Constructor: creates a new GraphViz object that will contain
* @return Source of the graph in dot language. * a graph.
*/ */
public String getDotSource() { public GraphViz() {
return this.graph.toString(); }
}
/** /**
* Adds a string to the graph's source (without newline). * Returns the graph's source description in dot language.
*/ *
public void add(String line) { * @return Source of the graph in dot language.
this.graph.append(line); */
} public String getDotSource() {
return this.graph.toString();
}
/** /**
* Adds a string to the graph's source (with newline). * Adds a string to the graph's source (without newline).
*/ */
public void addln(String line) { public void add(String line) {
this.graph.append(line).append("\n"); this.graph.append(line);
} }
/** /**
* Adds a newline to the graph's source. * Adds a string to the graph's source (with newline).
*/ */
public void addln() { public void addln(String line) {
this.graph.append('\n'); this.graph.append(line).append("\n");
} }
public void clearGraph(){ /**
this.graph = new StringBuilder(); * Adds a newline to the graph's source.
} */
public void addln() {
this.graph.append('\n');
}
/** public void clearGraph() {
* Returns the graph as an image in binary format. this.graph = new StringBuilder();
* @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;
try { /**
dot = writeDotSourceToFile(dot_source); * Returns the graph as an image in binary format.
if (dot != null) *
{ * @param dot_source Source of the graph to be drawn.
img_stream = get_img_stream(dot, type); * @param type Type of the output image to be produced, e.g.: gif, dot, fig, pdf, ps, svg, png.
if (!dot.delete()) * @return A byte array containing the image of the graph.
System.err.println("Warning: " + dot.getAbsolutePath() + " could not be deleted!"); */
return img_stream; public byte[] getGraph(String dot_source, String type) {
} File dot;
return null; byte[] img_stream;
} catch (java.io.IOException ioe) { return null; }
}
/** try {
* Writes the graph's image in a file. dot = writeDotSourceToFile(dot_source);
* @param img A byte array containing the image of the graph. if (dot != null) {
* @param file Name of the file to where we want to write. img_stream = get_img_stream(dot, type);
* @return Success: 1, Failure: -1 if (!dot.delete())
*/ System.err.println("Warning: " + dot.getAbsolutePath() + " could not be deleted!");
public int writeGraphToFile(byte[] img, String file) return img_stream;
{ }
File to = new File(file); return null;
return writeGraphToFile(img, to); } catch (java.io.IOException ioe) {
} return null;
}
}
/** /**
* Writes the graph's image in a file. * 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. * @param img A byte array containing the image of the graph.
* @return Success: 1, Failure: -1 * @param file Name of the file to where we want to write.
*/ * @return Success: 1, Failure: -1
public int writeGraphToFile(byte[] img, File to) */
{ public int writeGraphToFile(byte[] img, String file) {
try { File to = new File(file);
FileOutputStream fos = new FileOutputStream(to); return writeGraphToFile(img, 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 * Writes the graph's image in a file.
* binary format. *
* @param dot Source of the graph (in dot language). * @param img A byte array containing the image of the graph.
* @param type Type of the output image to be produced, e.g.: gif, dot, fig, pdf, ps, svg, png. * @param to A File object to where we want to write.
* @return The image of the graph in .gif format. * @return Success: 1, Failure: -1
*/ */
private byte[] get_img_stream(File dot, String type) public int writeGraphToFile(byte[] img, File to) {
{ try {
File img; FileOutputStream fos = new FileOutputStream(to);
byte[] img_stream = null; fos.write(img);
fos.close();
} catch (java.io.IOException ioe) {
return -1;
}
return 1;
}
try { /**
img = File.createTempFile("graph_", "."+type, new File(GraphViz.TEMP_DIR)); * It will call the external dot program, and return the image in
Runtime rt = Runtime.getRuntime(); * 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;
// patch by Mike Chenault try {
String[] args = {DOT, "-T"+type, "-Gdpi="+dpiSizes[this.currentDpiPos], dot.getAbsolutePath(), "-o", img.getAbsolutePath()}; img = File.createTempFile("graph_", "." + type, new File(GraphViz.TEMP_DIR));
Process p = rt.exec(args); Runtime rt = Runtime.getRuntime();
p.waitFor(); // patch by Mike Chenault
String[] args = {DOT, "-T" + type, "-Gdpi=" + dpiSizes[this.currentDpiPos], dot.getAbsolutePath(), "-o", img.getAbsolutePath()};
Process p = rt.exec(args);
FileInputStream in = new FileInputStream(img.getAbsolutePath()); p.waitFor();
img_stream = new byte[in.available()];
in.read(img_stream);
// Close it if we need to
in.close();
if (!img.delete()) FileInputStream in = new FileInputStream(img.getAbsolutePath());
System.err.println("Warning: " + img.getAbsolutePath() + " could not be deleted!"); img_stream = new byte[in.available()];
} in.read(img_stream);
catch (java.io.IOException ioe) { // Close it if we need to
System.err.println("Error: in I/O processing of tempfile in dir " + GraphViz.TEMP_DIR+"\n"); in.close();
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; if (!img.delete())
} 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. * Writes the source of the graph in a file, and returns the written file
* @return A string to open a graph. * as a File object.
*/ *
public String start_graph() { * @param str Source of the graph (in dot language).
return "digraph G {"; * @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 end a graph. * Returns a string that is used to start a graph.
* @return A string to close a graph. *
*/ * @return A string to open a graph.
public String end_graph() { */
return "}"; public String start_graph() {
} return "digraph G {";
}
/** /**
* Takes the cluster or subgraph id as input parameter and returns a string * Returns a string that is used to end a graph.
* that is used to start a subgraph. *
* @return A string to open a subgraph. * @return A string to close a graph.
*/ */
public String start_subgraph(int clusterid) { public String end_graph() {
return "subgraph cluster_" + clusterid + " {"; return "}";
} }
/** /**
* Returns a string that is used to end a graph. * Takes the cluster or subgraph id as input parameter and returns a string
* @return A string to close a graph. * that is used to start a subgraph.
*/ *
public String end_subgraph() { * @return A string to open a subgraph.
return "}"; */
} public String start_subgraph(int clusterid) {
return "subgraph cluster_" + clusterid + " {";
}
/** /**
* Read a DOT graph from a text file. * Returns a string that is used to end a graph.
* *
* @param input Input text file containing the DOT graph * @return A string to close a graph.
* source. */
*/ public String end_subgraph() {
public void readSource(String input) return "}";
{ }
StringBuilder sb = new StringBuilder();
try /**
{ * Read a DOT graph from a text file.
FileInputStream fis = new FileInputStream(input); *
DataInputStream dis = new DataInputStream(fis); * @param input Input text file containing the DOT graph
BufferedReader br = new BufferedReader(new InputStreamReader(dis)); * source.
String line; */
while ((line = br.readLine()) != null) { public void readSource(String input) {
sb.append(line); StringBuilder sb = new StringBuilder();
}
dis.close();
}
catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
this.graph = sb; 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 } // end of class GraphViz

View file

@ -1,15 +1,15 @@
package grafos; package grafos;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import com.github.javaparser.JavaParser; import com.github.javaparser.JavaParser;
import com.github.javaparser.ast.CompilationUnit; import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.Node; import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.comments.Comment; import com.github.javaparser.ast.comments.Comment;
import com.github.javaparser.ast.visitor.VoidVisitor; import com.github.javaparser.ast.visitor.VoidVisitor;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class Transformador { public class Transformador {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
@ -27,35 +27,34 @@ public class Transformador {
// Recorremos el AST // Recorremos el AST
List<String> arcos = new ArrayList<String>(); List<String> arcos = new ArrayList<String>();
VoidVisitor<List<String>> visitador = new Visitador(); VoidVisitor<List<String>> visitador = new Visitador();
visitador.visit(cu,arcos); visitador.visit(cu, arcos);
// Imprimimos el CFG del programa // Imprimimos el CFG del programa
String dotInfo = imprimirGrafo(arcos); String dotInfo = imprimirGrafo(arcos);
// Generamos un PDF con el CFG del programa // Generamos un PDF con el CFG del programa
System.out.print("\nGenerando PDF..."); System.out.print("\nGenerando PDF...");
GraphViz gv=new GraphViz(); GraphViz gv = new GraphViz();
gv.addln(gv.start_graph()); gv.addln(gv.start_graph());
gv.add(dotInfo); gv.add(dotInfo);
gv.addln(gv.end_graph()); gv.addln(gv.end_graph());
String type = "pdf"; // String type = "gif"; String type = "pdf"; // String type = "gif";
// gv.increaseDpi(); // gv.increaseDpi();
gv.decreaseDpi(); gv.decreaseDpi();
gv.decreaseDpi(); gv.decreaseDpi();
gv.decreaseDpi(); gv.decreaseDpi();
gv.decreaseDpi(); gv.decreaseDpi();
File destino_CFG = new File(ruta + "_CFG."+ type); File destino_CFG = new File(ruta + "_CFG." + type);
gv.writeGraphToFile( gv.getGraph( gv.getDotSource(), type ), destino_CFG); gv.writeGraphToFile(gv.getGraph(gv.getDotSource(), type), destino_CFG);
System.out.println(" PDF generado!"); System.out.println(" PDF generado!");
} }
// Imprime el grafo en la pantalla // Imprime el grafo en la pantalla
private static String imprimirGrafo(List<String> arcos) private static String imprimirGrafo(List<String> arcos) {
{ StringBuilder dotInfo = new StringBuilder();
StringBuilder dotInfo= new StringBuilder(); for (String arco : arcos) {
for(String arco:arcos) {
dotInfo.append(arco); dotInfo.append(arco);
System.out.println("ARCO: "+arco); System.out.println("ARCO: " + arco);
} }
System.out.println("\nCFG:"); System.out.println("\nCFG:");
System.out.println(dotInfo); System.out.println(dotInfo);
@ -64,16 +63,15 @@ public class Transformador {
} }
// Elimina todos los comentarios de un nodo y sus hijos // Elimina todos los comentarios de un nodo y sus hijos
private static void quitarComentarios(Node node){ private static void quitarComentarios(Node node) {
node.removeComment(); node.removeComment();
for (Comment comment : node.getOrphanComments()) for (Comment comment : node.getOrphanComments()) {
{
node.removeOrphanComment(comment); node.removeOrphanComment(comment);
} }
// Do something with the node // Do something with the node
for (Node child : node.getChildNodes()){ for (Node child : node.getChildNodes()) {
quitarComentarios(child); quitarComentarios(child);
} }
} }
} }

View file

@ -1,7 +1,5 @@
package grafos; package grafos;
import java.util.List;
import com.github.javaparser.ast.NodeList; import com.github.javaparser.ast.NodeList;
import com.github.javaparser.ast.body.MethodDeclaration; import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.stmt.BlockStmt; import com.github.javaparser.ast.stmt.BlockStmt;
@ -9,15 +7,16 @@ import com.github.javaparser.ast.stmt.ExpressionStmt;
import com.github.javaparser.ast.stmt.Statement; import com.github.javaparser.ast.stmt.Statement;
import com.github.javaparser.ast.visitor.VoidVisitorAdapter; import com.github.javaparser.ast.visitor.VoidVisitorAdapter;
import java.util.List;
public class Visitador extends VoidVisitorAdapter<List<String>>
{ public class Visitador extends VoidVisitorAdapter<List<String>> {
//********************************************************/ //********************************************************/
//********************** Atributos ***********************/ //********************** Atributos ***********************/
//********************************************************/ //********************************************************/
// Usamos un contador para numerar las instrucciones // Usamos un contador para numerar las instrucciones
private int contador=1; private int contador = 1;
private String nodoAnterior = "Start"; private String nodoAnterior = "Start";
private String nodoActual = ""; private String nodoActual = "";
@ -25,23 +24,21 @@ public class Visitador extends VoidVisitorAdapter<List<String>>
//*********************** Metodos ************************/ //*********************** Metodos ************************/
//********************************************************/ //********************************************************/
// Visitador de métodos // Visitador de métodos
// Este visitador añade el nodo final al CFG // Este visitador añade el nodo final al CFG
@Override @Override
public void visit(MethodDeclaration methodDeclaration, List<String>collector) public void visit(MethodDeclaration methodDeclaration, List<String> collector) {
{ // Visitamos el método
// Visitamos el método
super.visit(methodDeclaration, collector); super.visit(methodDeclaration, collector);
// Añadimos el nodo final al CFG // Añadimos el nodo final al CFG
collector.add(nodoAnterior+"-> Stop;"); collector.add(nodoAnterior + "-> Stop;");
} }
// Visitador de expresiones // Visitador de expresiones
// Cada expresión encontrada genera un nodo en el CFG // Cada expresión encontrada genera un nodo en el CFG
@Override @Override
public void visit(ExpressionStmt es, List<String>collector) public void visit(ExpressionStmt es, List<String> collector) {
{
// Creamos el nodo actual // Creamos el nodo actual
nodoActual = crearNodo(es); nodoActual = crearNodo(es);
@ -54,8 +51,7 @@ public class Visitador extends VoidVisitorAdapter<List<String>>
} }
// Añade un arco desde el último nodo hasta el nodo actual (se le pasa como parametro) // Añade un arco desde el último nodo hasta el nodo actual (se le pasa como parametro)
private void añadirArcoSecuencialCFG(List<String>collector) private void añadirArcoSecuencialCFG(List<String> collector) {
{
System.out.println("NODO: " + nodoActual); System.out.println("NODO: " + nodoActual);
String arco = nodoAnterior + "->" + nodoActual + ";"; String arco = nodoAnterior + "->" + nodoActual + ";";
@ -63,29 +59,25 @@ public class Visitador extends VoidVisitorAdapter<List<String>>
} }
// Crear arcos // Crear arcos
private void crearArcos(List<String>collector) private void crearArcos(List<String> collector) {
{ añadirArcoSecuencialCFG(collector);
añadirArcoSecuencialCFG(collector);
} }
// Crear nodo // Crear nodo
// Añade un arco desde el nodo actual hasta el último control // Añade un arco desde el nodo actual hasta el último control
private String crearNodo(Object objeto) private String crearNodo(Object objeto) {
{ return "\"(" + contador++ + ") " + quitarComillas(objeto.toString()) + "\"";
return "\"("+ contador++ +") "+quitarComillas(objeto.toString())+"\"";
} }
// Sustituye " por \" en un string: Sirve para eliminar comillas. // Sustituye " por \" en un string: Sirve para eliminar comillas.
private static String quitarComillas(String texto) private static String quitarComillas(String texto) {
{ return texto.replace("\"", "\\\"");
return texto.replace("\"", "\\\"");
} }
// Dada una sentencia, // Dada una sentencia,
// Si es una <EFBFBD>nica instrucci<EFBFBD>n, devuelve un bloque equivalente // Si es una <EFBFBD>nica instrucci<EFBFBD>n, devuelve un bloque equivalente
// Si es un bloque, lo devuelve // Si es un bloque, lo devuelve
private BlockStmt convertirEnBloque(Statement statement) private BlockStmt convertirEnBloque(Statement statement) {
{
if (statement instanceof BlockStmt) if (statement instanceof BlockStmt)
return (BlockStmt) statement; return (BlockStmt) statement;

View file

@ -2,12 +2,10 @@ package ejemplos;
public class Bucles_1 { public class Bucles_1 {
public static void main(String[] args) public static void main(String[] args) {
{
// BUCLE WHILE (sin anidamiento) // BUCLE WHILE (sin anidamiento)
int x=1; int x = 1;
while (x<=10) while (x <= 10) {
{
System.out.print(x); System.out.print(x);
x++; x++;
} }

View file

@ -2,19 +2,16 @@ package ejemplos;
public class Bucles_2 { public class Bucles_2 {
public static void main(String[] args) public static void main(String[] args) {
{
// BUCLE WHILE anidado a otro WHILE // BUCLE WHILE anidado a otro WHILE
System.out.println("Empieza bucle WHILE anidado a otro WHILE:"); System.out.println("Empieza bucle WHILE anidado a otro WHILE:");
int x=1; int x = 1;
char y='a'; char y = 'a';
while (x<=10) while (x <= 10) {
{ System.out.print(" " + x);
System.out.print(" "+x); y = 'a';
y='a'; while (y <= 'c') {
while (y<='c') System.out.print(" " + y);
{
System.out.print(" "+y);
y++; y++;
} }
x++; x++;

View file

@ -7,30 +7,28 @@ public class Bucles_3 {
// BUCLE FOR (sin anidamiento) // BUCLE FOR (sin anidamiento)
System.out.println("Empieza bucle FOR:"); System.out.println("Empieza bucle FOR:");
for (x=1;x<=10;x++) for (x = 1; x <= 10; x++) {
{ System.out.print(" " + x);
System.out.print(" "+x);
} }
System.out.println(); System.out.println();
// BUCLE WHILE (sin anidamiento) // BUCLE WHILE (sin anidamiento)
System.out.println("Empieza bucle WHILE:"); System.out.println("Empieza bucle WHILE:");
x=1; x = 1;
while (x<=10) while (x <= 10) {
{ System.out.print(" " + x);
System.out.print(" "+x);
x++; x++;
} }
System.out.println(); System.out.println();
// BUCLE DO WHILE (sin anidamiento) // BUCLE DO WHILE (sin anidamiento)
System.out.println("Empieza bucle DO WHILE:"); System.out.println("Empieza bucle DO WHILE:");
x=1; x = 1;
do{ do {
System.out.print(" "+x); System.out.print(" " + x);
x++; x++;
} }
while (x<=10); while (x <= 10);
System.out.println(); System.out.println();
} }

View file

@ -2,22 +2,19 @@ package ejemplos;
public class Bucles_4 { public class Bucles_4 {
public static void main(String[] args) public static void main(String[] args) {
{ int x = 1;
int x=1;
//Bucle 1: Contador //Bucle 1: Contador
while (x<10) while (x < 10) {
{
System.out.println(x); System.out.println(x);
x++; x++;
} }
//Bucle 2: Sumatorio //Bucle 2: Sumatorio
int suma=0; int suma = 0;
int y=1; int y = 1;
while (y<10) while (y < 10) {
{
suma += y; suma += y;
y++; y++;
} }
@ -27,8 +24,7 @@ public class Bucles_4 {
int sumatorio = 0; int sumatorio = 0;
int min = 10; int min = 10;
int max = 100; int max = 100;
for (int num = min; num <= max; num++) for (int num = min; num <= max; num++) {
{
sumatorio += num; sumatorio += num;
} }
System.out.println(sumatorio); System.out.println(sumatorio);

View file

@ -8,26 +8,22 @@ public class Bucles_5 {
// BUCLE FOR anidado a otro FOR // BUCLE FOR anidado a otro FOR
System.out.println("Empieza bucle FOR anidado a otro FOR:"); System.out.println("Empieza bucle FOR anidado a otro FOR:");
for (x=1;x<=10;x++) for (x = 1; x <= 10; x++) {
{ System.out.print(" " + x);
System.out.print(" "+x); for (y = 'a'; y <= 'c'; y++) {
for (y='a';y<='c';y++) System.out.print(" " + y);
{
System.out.print(" "+y);
} }
} }
System.out.println(); System.out.println();
// BUCLE WHILE anidado a otro WHILE // BUCLE WHILE anidado a otro WHILE
System.out.println("Empieza bucle WHILE anidado a otro WHILE:"); System.out.println("Empieza bucle WHILE anidado a otro WHILE:");
x=1; x = 1;
while (x<=10) while (x <= 10) {
{ System.out.print(" " + x);
System.out.print(" "+x); y = 'a';
y='a'; while (y <= 'c') {
while (y<='c') System.out.print(" " + y);
{
System.out.print(" "+y);
y++; y++;
} }
x++; x++;
@ -36,16 +32,15 @@ public class Bucles_5 {
// BUCLE FOR anidado a bucle DO WHILE // BUCLE FOR anidado a bucle DO WHILE
System.out.println("Empieza bucle FOR anidado a bucle DO WHILE:"); System.out.println("Empieza bucle FOR anidado a bucle DO WHILE:");
x=1; x = 1;
do{ do {
System.out.print(" "+x); System.out.print(" " + x);
for (y='a';y<='c';y++) for (y = 'a'; y <= 'c'; y++) {
{ System.out.print(" " + y);
System.out.print(" "+y);
} }
x++; x++;
} }
while (x<=10); while (x <= 10);
System.out.println(); System.out.println();
} }

View file

@ -2,24 +2,20 @@ package ejemplos;
public class Bucles_6 { public class Bucles_6 {
public static void main(String[] args) public static void main(String[] args) {
{
// BUCLE WHILE (sin anidamiento) // BUCLE WHILE (sin anidamiento)
System.out.println("Empieza bucle WHILE:"); System.out.println("Empieza bucle WHILE:");
int x=1; int x = 1;
while (x<=10) while (x <= 10) {
{ System.out.print(" " + x);
System.out.print(" "+x);
x++; x++;
while (x<=10) while (x <= 10) {
{ System.out.print(" " + x);
System.out.print(" "+x);
x++; x++;
} }
} }
while (x<=10) while (x <= 10) {
{ System.out.print(" " + x);
System.out.print(" "+x);
x++; x++;
} }
System.out.println(); System.out.println();

View file

@ -2,12 +2,11 @@ package ejemplos;
public class Test_1 { public class Test_1 {
public static void main(String[] args) public static void main(String[] args) {
{
System.out.println("HOLA mundo"); System.out.println("HOLA mundo");
int x=1; int x = 1;
x=2; x = 2;
x=3; x = 3;
x=4; x = 4;
} }
} }

View file

@ -2,13 +2,12 @@ package ejemplos;
public class Test_2 { public class Test_2 {
public static void main(String[] args) public static void main(String[] args) {
{ int x = 1;
int x=1;
x++; x++;
++x; ++x;
int y=0; int y = 0;
x=x+y; x = x + y;
System.out.println(x); System.out.println(x);
} }
} }

View file

@ -2,13 +2,12 @@ package ejemplos;
public class Test_3 { public class Test_3 {
public static void main(String[] args) public static void main(String[] args) {
{ int x = 1;
int x=1;
if (x==1) if (x == 1)
x=2; x = 2;
x=3; x = 3;
x=4; x = 4;
} }
} }

View file

@ -2,20 +2,17 @@ package ejemplos;
public class Test_4 { public class Test_4 {
public static void main(String[] args) public static void main(String[] args) {
{ int x = 1;
int x=1;
if (x==1) if (x == 1) {
{ x = 2;
x=2; if (x >= 1) {
if (x>=1) x = 3;
{ x = 4;
x=3;
x=4;
} }
} }
x=5; x = 5;
x=6; x = 6;
} }
} }

View file

@ -2,21 +2,18 @@ package ejemplos;
public class Test_5 { public class Test_5 {
public static void main(String[] args) public static void main(String[] args) {
{ int x = 1;
int x=1;
if (x==1) if (x == 1) {
{ x = 2;
x=2; if (x >= 1) {
if (x>=1) x = 3;
{ x = 4;
x=3;
x=4;
} }
x=5; x = 5;
} }
x=6; x = 6;
x=7; x = 7;
} }
} }

View file

@ -2,20 +2,16 @@ package ejemplos;
public class Test_6 { public class Test_6 {
public static void main(String[] args) public static void main(String[] args) {
{ int x = 1;
int x=1;
if (x==1) if (x == 1) {
{ x = 2;
x=2; x = 3;
x=3; } else {
x = 4;
x = 5;
} }
else x = 6;
{
x=4;
x=5;
}
x=6;
} }
} }

View file

@ -3,21 +3,16 @@ package ejemplos;
public class Test_7 { public class Test_7 {
public static void main(String[] args) public static void main(String[] args) {
{ int x = 1;
int x=1;
if (x==1) if (x == 1) {
{ x = 2;
x=2; } else x = 3;
} x = 4;
else x=3; if (x == 2) {
x=4; x = 5;
if (x==2) } else if (x == 3) x = 6;
{ x = 7;
x=5;
}
else if (x==3) x=6;
x=7;
} }
} }

View file

@ -2,21 +2,18 @@ package ejemplos;
public class Test_8 { public class Test_8 {
public static void main(String[] args) public static void main(String[] args) {
{ int x = 1;
int x=1;
if (x==1) if (x == 1) {
{ x = 2;
x=2;
} }
x=5; x = 5;
x=6; x = 6;
if (x==2) if (x == 2) {
{ x = 7;
x=7;
} }
if (x==3) x=8; if (x == 3) x = 8;
x=9; x = 9;
} }
} }

View file

@ -3,28 +3,22 @@ package ejemplos;
public class Test_9 { public class Test_9 {
public static void main(String[] args) public static void main(String[] args) {
{
// ANIDAMIENTO de IF y WHILE // ANIDAMIENTO de IF y WHILE
// ANIDAMIENTO de IF y WHILE 2 // ANIDAMIENTO de IF y WHILE 2
int x=0; int x = 0;
if (x>1) if (x > 1) {
{ x = 1;
x=1; while (x > 2) {
while (x>2) x = 2;
{ while (x > 3) {
x=2; x = 3;
while (x>3) if (x > 4) {
{ x = 4;
x=3; if (x > 5) {
if (x>4) x = 5;
{
x=4;
if (x>5)
{
x=5;
} }
x--; x--;
} }