Skip to content

Commit

Permalink
A untested prototype of understand_parse_dependencies
Browse files Browse the repository at this point in the history
Modeled after parse_dependencies with a similar output, this is for review to see if it is in-line with specification
  • Loading branch information
RavenMarQ committed Sep 26, 2024
1 parent 12ed2d7 commit 334bac3
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions R/src.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,57 @@
############## Parsers ##############


#' Parse dependencies from Scitool's Understand
#'
#' @param understand_path path to the und folder
#' @param project_path path to the project folder to analyze
#' @param language the language of the .git repo (accepts cpp, java, ruby, python, pom)
#' @param output_dir path to output directory (formatted output_path/)
#' @export
#' @family parsers
understand_parse_dependencies <- function(project_path,language,output_dir="/tmp/"){
# Use Understand to parse the code folder.
# Create the variables used in command lines
db_dir <- paste0(output_dir, "/Understand.und")
xml_dir <- paste0(db_dir, "/Dependencies.xml")

# Generate the XML file
system2("und", "create", "-db", db_dir, "languages", language)
system2("und", "-db", db_dir, "add", project_path)
system2("und", "analyze", db_dir)
system2("und", "export", "-dependencies", "file", "cytoscape", xml_dir, db_dir)

# Parse the XML file
xml_data <- xmlParse(xml_dir)

# Extract nodes
nodes <- xpathSApply(xml_data, "//node", xmlToList)

# Create the data table with id and label
node_list <- lapply(nodes, function(node) {
id <- xmlGetAttr(node, "id")
label <- xpathSApply(node, ".//att[@name='node.label']", xmlGetAttr, "value")
data.table(id = id, label = label)
})

# Extract edges
edges <- xpathSApply(xml_data, "//edge", xmlToList)

# Create the data table with id_from, id_to, and dependency_kind
edge_list <- lapply(nodes, function(edge) {
id_from <- xmlGetAttr(edge, "source")
id_to <- xmlGetAttr(edge, "target")
dependency_kind <- xpathSApply(edge, ".//att[@name='node.label']", xmlGetAttr, "value")
data.table(id_from = id_from, id_to = id_to, dependency_kind = dependency_kind)
})

# Combine the lists into a single data frame
edge_list <- rbindlist(edge_list)
node_list <- rbindlist(node_list)
graph <- list(edge_list = edge_list, node_list = node_list)
return(graph)
}

#' Parse dependencies from Depends
#'
#' @param depends_jar_path path to depends jar
Expand Down

0 comments on commit 334bac3

Please sign in to comment.