Skip to content

Commit 0266954

Browse files
committed
unit testing
1 parent 3261d60 commit 0266954

6 files changed

Lines changed: 145 additions & 119 deletions

File tree

R/combine_ttl_files.R

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,30 @@
77
#' @returns A text file with a combined TTL file.
88
#' @export
99

10-
combine_ttl_files <- function(qids,
11-
output_file,
10+
combine_ttl_files <- function(qids,
11+
output_file,
1212
base_url = "https://reprexbase.eu/fu/Special:EntityData/") {
1313
# Initialize an empty vector to store TTL content
1414
ttl_combined <- character()
1515
seen_prefixes <- FALSE
16-
16+
1717
for (qid in qids) {
1818
url <- paste0(base_url, qid, ".ttl")
1919
message("Downloading: ", url)
20-
20+
2121
# Try to download TTL content
22-
ttl_content <- tryCatch({
23-
readLines(url, warn = FALSE)
24-
}, error = function(e) {
25-
warning(paste("Failed to download", qid, ":", e$message))
26-
return(NULL)
27-
})
28-
22+
ttl_content <- tryCatch(
23+
{
24+
readLines(url, warn = FALSE)
25+
},
26+
error = function(e) {
27+
warning(paste("Failed to download", qid, ":", e$message))
28+
return(NULL)
29+
}
30+
)
31+
2932
if (is.null(ttl_content)) next
30-
33+
3134
# Separate prefixes and main body
3235
if (!seen_prefixes) {
3336
ttl_combined <- c(ttl_combined, ttl_content)
@@ -38,7 +41,7 @@ combine_ttl_files <- function(qids,
3841
ttl_combined <- c(ttl_combined, body_only)
3942
}
4043
}
41-
44+
4245
# Write combined TTL to file
4346
writeLines(ttl_combined, output_file)
4447
message("Combined TTL written to: ", output_file)

R/ttl_dataset_write.R

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,19 @@
88
#' @param overwrite If the file exists, overwrite it? Defaults to \code{TRUE}.
99
#' @return A text file with the prefix and the observation serialisations.
1010
#' @examples
11-
#' testtdf <- data.frame (s = c("eg:o1", "eg:01", "eg:02"),
12-
#' p = c("a", "eg-var:", "eg-var"),
13-
#' o = c("qb:Observation",
14-
#' "\"1\"^^<xs:decimal>",
15-
#' "\"2\"^^<xs:decimal>"))
11+
#' testtdf <- data.frame(
12+
#' s = c("eg:o1", "eg:01", "eg:02"),
13+
#' p = c("a", "eg-var:", "eg-var"),
14+
#' o = c(
15+
#' "qb:Observation",
16+
#' "\"1\"^^<xs:decimal>",
17+
#' "\"2\"^^<xs:decimal>"
18+
#' )
19+
#' )
1620
#'
1721
#' examplefile <- file.path(tempdir(), "ttl_dataset_write.ttl")
1822
#'
19-
#' dataset_ttl_write(tdf=testtdf, file_path = examplefile)
23+
#' dataset_ttl_write(tdf = testtdf, file_path = examplefile)
2024
#'
2125
#' readLines(examplefile)
2226
#' @export
@@ -25,12 +29,14 @@ dataset_ttl_write <- function(tdf,
2529
ttl_namespace = NULL,
2630
file_path = NULL,
2731
overwrite = TRUE) {
28-
2932
## load dataset_namespace
3033
default_namespace <- getdata("dataset_namespace", "dataset")
3134
default_namespace <- default_namespace[
32-
which(default_namespace$prefix %in% c("rdf:", "rdfs:", "owl:",
33-
"qb:", "dcat:", "xsd:")),]
35+
which(default_namespace$prefix %in% c(
36+
"rdf:", "rdfs:", "owl:",
37+
"qb:", "dcat:", "xsd:"
38+
)),
39+
]
3440

3541
## validate dataset
3642
validate_tdf(tdf)
@@ -44,10 +50,11 @@ dataset_ttl_write <- function(tdf,
4450
ttl_namespace <- default_namespace
4551
}
4652

47-
ttl_prefix_write(ttl_namespace = ttl_namespace,
48-
file_path=file_path,
49-
overwrite=overwrite)
53+
ttl_prefix_write(
54+
ttl_namespace = ttl_namespace,
55+
file_path = file_path,
56+
overwrite = overwrite
57+
)
5058

5159
invisible(ttl_observations_write(tdf, file_path))
5260
}
53-

R/ttl_prefix_write.R

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,37 @@
22
#' @description Write the namespace prefixes into the header of a Turtle serialisation
33
#' @param ttl_namespace A namespace definition as a two-column dataset.
44
#' @param file_path A file_path to write the TTL prefix namespace definitions.
5-
#' @param overwrite Boolean, if the file should be overwritten (if it exists at all.)
5+
#' @param overwrite Boolean, if the file should be overwritten (if it exists at all.)
66
#' Defaults to \code{TRUE}.
77
#' @export
88
#' @examples
99
#' ttl_namespace <- data.frame(
10-
#' prefix = c("dbo:", "dcterms:"),
11-
#' uri = c("<http://dbpedia.org/ontology/>",
12-
#' "<http://purl.org/dc/terms/>")
10+
#' prefix = c("dbo:", "dcterms:"),
11+
#' uri = c(
12+
#' "<http://dbpedia.org/ontology/>",
13+
#' "<http://purl.org/dc/terms/>"
14+
#' )
1315
#' )
1416
#' file_path <- tempfile()
1517
#' ttl_prefix_write(ttl_namespace, file_path)
1618
#' readLines(file_path)
17-
#'
19+
#'
20+
ttl_prefix_write <- function(ttl_namespace, file_path, overwrite = TRUE) {
21+
if (file.exists(file_path) & overwrite) file.remove(file_path)
1822

19-
ttl_prefix_write <- function(ttl_namespace, file_path, overwrite=TRUE) {
20-
if ( file.exists(file_path) & overwrite ) file.remove(file_path)
21-
2223
create_string <- paste(
23-
apply ( ttl_namespace, 1, function(x) {
24+
apply(ttl_namespace, 1, function(x) {
2425
prefix <- x[[1]]
25-
n <- max(12-nchar(x[[1]]), 1)
26-
separator <- paste(rep (" ", n), collapse="")
26+
n <- max(12 - nchar(x[[1]]), 1)
27+
separator <- paste(rep(" ", n), collapse = "")
2728
paste0("@prefix ", prefix, separator, x[[2]], " .")
28-
}), sep = "", collapse = "\n"
29+
}),
30+
sep = "", collapse = "\n"
2931
)
30-
32+
3133
if (overwrite) {
32-
write(create_string, file=file_path, append = F )
34+
write(create_string, file = file_path, append = F)
3335
} else {
34-
write(create_string, file=file_path, append = T )
36+
write(create_string, file = file_path, append = T)
3537
}
3638
}

R/ttl_statements_write.R

Lines changed: 51 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,46 @@
33
#' @param file_path The path to the file that should be written or appended.
44
#' @param overwrite If the file exists, overwrite it? Defaults to \code{TRUE}.
55
#' @examples
6-
#' tdf <- data.frame (s = c("eg:01","eg:02", "eg:01", "eg:02", "eg:01" ),
7-
#' p = c("a", "a", "eg-var:", "eg-var:", "rdfs:label"),
8-
#' o = c("qb:Observation",
9-
#' "qb:Observation",
10-
#' "\"1\"^^<xs:decimal>",
11-
#' "\"2\"^^<xs:decimal>",
12-
#' '"Example observation"'))
6+
#' tdf <- data.frame(
7+
#' s = c("eg:01", "eg:02", "eg:01", "eg:02", "eg:01"),
8+
#' p = c("a", "a", "eg-var:", "eg-var:", "rdfs:label"),
9+
#' o = c(
10+
#' "qb:Observation",
11+
#' "qb:Observation",
12+
#' "\"1\"^^<xs:decimal>",
13+
#' "\"2\"^^<xs:decimal>",
14+
#' '"Example observation"'
15+
#' )
16+
#' )
1317
#'
1418
#' examplefile <- file.path(tempdir(), "ttl_dataset_write.ttl")
1519
#'
16-
#' dataset_ttl_write(tdf=tdf, file_path = examplefile)
20+
#' dataset_ttl_write(tdf = tdf, file_path = examplefile)
1721
#'
1822
#' readLines(examplefile)
1923
#' @export
2024

21-
ttl_statements_write <- function(tdf, file_path, overwrite=TRUE) {
25+
ttl_statements_write <- function(tdf, file_path, overwrite = TRUE) {
2226
## tdf must be s, o, p
2327
validate_tdf(tdf)
24-
28+
2529
if (overwrite) {
26-
reset_text_file(file_path=file_path)
30+
reset_text_file(file_path = file_path)
2731
} else {
28-
write("\n", file=file_path, append = TRUE)
32+
write("\n", file = file_path, append = TRUE)
2933
}
3034

3135
unique_subjects <- unique(tdf$s)
32-
33-
lapply ( unique_subjects, function(x) write_statements(x, tdf, file_path))
34-
36+
37+
lapply(unique_subjects, function(x) write_statements(x, tdf, file_path))
38+
3539
invisible(TRUE)
3640
}
3741

3842
#' @keywords internal
3943
get_class_definition <- function(instance) {
4044
return_value <- which(instance$p == "a")[1]
41-
if (length(return_value)==0) stop(instance$s, " has no class definition.")
45+
if (length(return_value) == 0) stop(instance$s, " has no class definition.")
4246
return_value
4347
}
4448

@@ -48,39 +52,42 @@ write_statements <- function(x, tdf, file_path) {
4852
instance_definition <- get_class_definition(instance)
4953
instance_rows <- 1:nrow(instance)
5054
instance_rows <- instance_rows[!instance_rows %in% instance_definition]
51-
52-
definition_statement <- paste0(instance$s[instance_definition],
53-
" a ",
54-
instance$o[instance_definition], ";")
55-
56-
if (length(instance_rows)>0) {
57-
write(x = paste0(definition_statement),
58-
file = file_path, append = T
55+
56+
definition_statement <- paste0(
57+
instance$s[instance_definition],
58+
" a ",
59+
instance$o[instance_definition], ";"
60+
)
61+
62+
if (length(instance_rows) > 0) {
63+
write(
64+
x = paste0(definition_statement),
65+
file = file_path, append = T
5966
)
6067
statements <- c(
61-
#further statements
68+
# further statements
6269
unique(paste0(" ", instance$p[instance_rows], " ", instance$o[instance_rows], " ;"))
6370
)
6471

6572
# last statement must end with a .
6673
last_statement <- statements[length(statements)]
67-
statements[length(statements)] <- paste0(substr(last_statement,1, nchar(last_statement)-1), ".")
68-
write( statements,
69-
file = file_path, append = T
70-
)
71-
write( "\n",
72-
file = file_path, append = T
73-
)
74-
75-
} else {
76-
## there is only a class definition
77-
write(x = paste0(definition_statement, " ."),
78-
file = file_path, append = T
79-
)
80-
81-
write( "\n",
82-
file = file_path, append = T
83-
)
84-
## what if no class definition?
85-
}
74+
statements[length(statements)] <- paste0(substr(last_statement, 1, nchar(last_statement) - 1), ".")
75+
write(statements,
76+
file = file_path, append = T
77+
)
78+
write("\n",
79+
file = file_path, append = T
80+
)
81+
} else {
82+
## there is only a class definition
83+
write(
84+
x = paste0(definition_statement, " ."),
85+
file = file_path, append = T
86+
)
87+
88+
write("\n",
89+
file = file_path, append = T
90+
)
91+
## what if no class definition?
92+
}
8693
}

0 commit comments

Comments
 (0)