CallGraphReader.java
Home
/
src /
main /
java /
br /
ufrgs /
inf /
prosoft /
approachescomparison /
adapter /
CallGraphReader.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package br.ufrgs.inf.prosoft.approachescomparison.adapter;
import br.ufrgs.inf.prosoft.memoizeit.graph.Graph;
import br.ufrgs.inf.prosoft.memoizeit.graph.Node;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author romulo
*/
public class CallGraphReader {
public static Graph<String> parseFile(String path) {
try {
Map<String, Node<String>> callHasNode = new HashMap<>();
List<String> lines = Files.readAllLines(Paths.get(path));
for (String line : lines) {
if (line.charAt(0) != 'M') {
continue;
}
int indexOfSpace = line.indexOf(" ");
String callerString = line.substring(2, indexOfSpace);
String calleeString = line.substring(indexOfSpace + 4, line.length());
callerString = reshapeMethodName(callerString);
calleeString = reshapeMethodName(calleeString);
Node<String> callerNode = callHasNode.get(callerString);
if (callerNode == null) {
callerNode = new Node<>(callerString);
callHasNode.put(callerString, callerNode);
}
Node<String> calleeNode = callHasNode.get(calleeString);
if (calleeNode == null) {
calleeNode = new Node<>(calleeString);
callHasNode.put(calleeString, calleeNode);
}
callerNode.addLink(calleeNode);
}
return new Graph<>(callHasNode);
} catch (IOException ex) {
Logger.getLogger(CallGraphReader.class.getName()).log(Level.SEVERE, null, ex);
}
return new Graph<>();
}
public static Map<String, Set<String>> parseFileToMap(String path) {
Map<String, Set<String>> nodeHasLinks = new HashMap<>();
try {
List<String> lines = Files.readAllLines(Paths.get(path));
for (String line : lines) {
if (line.charAt(0) != 'M') {
continue;
}
int indexOfSpace = line.indexOf(" ");
String callerString = line.substring(2, indexOfSpace);
String calleeString = line.substring(indexOfSpace + 4, line.length());
callerString = reshapeMethodName(callerString);
calleeString = reshapeMethodName(calleeString);
Set<String> links = nodeHasLinks.get(callerString);
if (links == null) {
links = new HashSet<>();
nodeHasLinks.put(callerString, links);
}
links.add(calleeString);
}
} catch (IOException ex) {
Logger.getLogger(CallGraphReader.class.getName()).log(Level.SEVERE, null, ex);
}
return nodeHasLinks;
}
private static String reshapeMethodName(String methodName) {
return methodName.substring(0, methodName.indexOf("("))
.replace(":", ".")
.replace("$", ".");
}
private static Collection<Node<String>> getRoots(Graph<String> graph) {
Collection<Node<String>> roots = new ArrayList<>(graph.getNodes());
graph.getEdges().forEach(edge -> roots.remove(edge.getTarget()));
return roots;
}
}