From 4a8e20d5c3b48cac1b9cd8123fe27e8ab5029640 Mon Sep 17 00:00:00 2001 From: Dao McGill <77309217+daomcgill@users.noreply.github.com> Date: Tue, 5 Nov 2024 17:06:51 -1000 Subject: [PATCH] i #314 Added exec/annotate.R and exec/query.R - annotate.R generates the XML file - query.R calls the query functions, depending on options Note: may have to revisit output format after getting into the fasttext notebook Signed-off-by: Dao McGill --- exec/annotate.R | 4 +-- exec/query.R | 76 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 3 deletions(-) create mode 100644 exec/query.R diff --git a/exec/annotate.R b/exec/annotate.R index 15d55b4e..2e9c8e19 100644 --- a/exec/annotate.R +++ b/exec/annotate.R @@ -35,15 +35,13 @@ OPTIONS: --version Show version. " - - arguments <- docopt::docopt(doc, version = 'Kaiaulu 0.0.0.9600') if(arguments[["annotate"]] & arguments[["help"]]){ cli_alert_info("Annotates source code using srcML.") }else if(arguments[["annotate"]]){ tools_path <- arguments[[""]] - conf_path <- arguments[[""]] + conf_path <- arguments[[""]] srcml_filepath <- arguments[[""]] tool <- yaml::read_yaml(tools_path) diff --git a/exec/query.R b/exec/query.R new file mode 100644 index 00000000..2514e243 --- /dev/null +++ b/exec/query.R @@ -0,0 +1,76 @@ +#!/usr/local/bin/Rscript + +require(yaml, quietly = TRUE) +require(cli, quietly = TRUE) +require(docopt, quietly = TRUE) +require(kaiaulu, quietly = TRUE) +require(data.table, quietly = TRUE) +require(stringi, quietly = TRUE) +require(XML, quietly = TRUE) + +doc <- " +USAGE: + query.R query [--namespace | --file-docs | --class-docs | --variables | --packages | --functions | --imports] + +DESCRIPTION: + Runs a specified query on the annotated XML file and saves the output to the appropriate path. + +OPTIONS: + --namespace Run the namespace query. + --file-docs Run the file-level documentation query. + --class-docs Run the class-level documentation query. + --variables Run the variables query. + --packages Run the packages query. + --functions Run the functions query. + --imports Run the imports query. +" + +arguments <- docopt::docopt(doc, version = 'Kaiaulu 0.0.0.9600') + +tools_path <- arguments[[""]] +conf_path <- arguments[[""]] +srcml_filepath <- arguments[[""]] + +tool <- yaml::read_yaml(tools_path) +conf <- yaml::read_yaml(conf_path) +srcml_path <- path.expand(tool[["srcml"]]) + +# Set output folder path +output_folder <- "../../analysis/maven/queries" +if (!dir.exists(output_folder)) { + dir.create(output_folder, recursive = TRUE) +} + +# Determine which query to run and save output +if (arguments[["--namespace"]]) { + output_file <- file.path(output_folder, "namespace_output.csv") + query_result <- query_src_text_namespace(srcml_path, srcml_filepath) +} else if (arguments[["--file-docs"]]) { + output_file <- file.path(output_folder, "file_docs_output.csv") + query_result <- query_src_text_file_docs(srcml_path, srcml_filepath) +} else if (arguments[["--class-docs"]]) { + output_file <- file.path(output_folder, "class_docs_output.csv") + query_result <- query_src_text_class_docs(srcml_path, srcml_filepath) +} else if (arguments[["--variables"]]) { + output_file <- file.path(output_folder, "variables_output.csv") + query_result <- query_src_text_variables(srcml_path, srcml_filepath) +} else if (arguments[["--packages"]]) { + output_file <- file.path(output_folder, "packages_output.csv") + query_result <- query_src_text_packages(srcml_path, srcml_filepath) +} else if (arguments[["--functions"]]) { + output_file <- file.path(output_folder, "functions_output.csv") + query_result <- query_src_text_functions(srcml_path, srcml_filepath) +} else if (arguments[["--imports"]]) { + output_file <- file.path(output_folder, "imports_output.csv") + query_result <- query_src_text_imports(srcml_path, srcml_filepath) +} else { + stop("No valid query option provided.") +} + +# Transform each row into a single line of text +plain_text_output <- apply(query_result, 1, function(row) paste(row, collapse = " ")) + +# Save to file +writeLines(plain_text_output, output_file) +cli_alert_success(paste0("Query results saved for fastText at: ", output_file)) +