Skip to content

Commit

Permalink
#298 consolidate generic approach + provenance i18n
Browse files Browse the repository at this point in the history
  • Loading branch information
eblondel committed Jan 3, 2023
1 parent 24d984e commit 8555731
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 51 deletions.
76 changes: 37 additions & 39 deletions R/geoflow_provenance.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,45 +38,43 @@ geoflow_provenance <- R6Class("geoflow_provenance",
#'@param str character string to initialize a provenance using key-based syntax
initialize = function(str = NULL){
if(!is.null(str)){
data_props <- extract_cell_components(sanitize_str(str))
state_prop <- data_props[[1]]
if(!startsWith(state_prop, "statement")){
stop("The data 'statement' is mandatory")
}
state_prop <- unlist(strsplit(state_prop,"statement:"))[2]
self$setStatement(state_prop)
if(length(data_props)>1){
data_props <- data_props[2:length(data_props)]
#processes
processes <- data_props[sapply(data_props, function(x){startsWith(x, "process:")})]
processes <- lapply(processes, function(process){
return(extract_kvp(process))
})
#processors
#processors <- data_props[sapply(data_props, function(x){startsWith(x,"processor:")})]
#processors_splits <- unlist(strsplit(processors, ":"))
#processors <- unlist(strsplit(processors_splits[2],","))
#control processors vs. processes
#if(length(processors)!=length(processes)){
# stop(sprintf("Number of processors [%s] doesn't match the number of processes [%s]",
# length(processors), length(processes)))
#}
#if(length(processes)>0 & length(processors)>0 & length(processes)==length(processors)){
if(length(processes)>0){
for(i in 1:length(processes)){
process <- processes[[i]]$values[[1]]
process_obj <- geoflow_process$new()
process_des <- attr(process, "description")
process_obj$setDescription(process_des)
attr(process, "description") <- NULL
process_obj$setRationale(process)
#processor_obj <- geoflow_contact$new()
#processor_obj$setIdentifier(key = "id", processors[i])
#processor_obj$setRole("processor")
#process_obj$setProcessor(processor_obj)
self$addProcess(process_obj)
}
}
strs <- extract_cell_components(sanitize_str(str))
kvps <- extract_kvps(strs)

#statement
statement <- kvps[sapply(kvps, function(kvp){kvp$key == "statement"})]
if(length(statement)==0) stop("No provenance statement, statement is mandatory")
if(length(statement)>1) warning("More than one provenance statement declared, only the first will be considered!")
statement = statement [[1]]
self$setStatement(statement$values)

#processes
processes <- kvps[sapply(kvps, function(kvp){kvp$key == "process"})]
if(length(processes)>0){
for(process in processes){ #in case processes defined on various lines
print(process)
process_attrs <- attributes(process$values)
for(i in 1:length(process$values)){ #in case processes defined on same line (eg for i18n)
value = if(is.list(process$values)) process$values[[i]] else process$values[i]
process_obj <- geoflow_process$new()
rationale = value
description = process_attrs$description
attributes(rationale) <- NULL
if(is.null(description)) description = attr(value, "description")
if(length(process_attrs)>0){
process_attrs <- process_attrs[names(process_attrs)!="description"]
for(attr_name in names(process_attrs)){
rationale_i18n = process_attrs[[attr_name]][[i]]
attr(description, attr_name) <- attr(rationale_i18n, "description")
attributes(rationale_i18n) = NULL
attr(rationale, attr_name) <- rationale_i18n
}
}
process_obj$setRationale(rationale)
process_obj$setDescription(description)
self$addProcess(process_obj)
}
}
}
}
},
Expand Down
16 changes: 9 additions & 7 deletions R/geoflow_utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ extract_kvp <- function(str){
attributes(key) <- key_attrs
locale <- key_parts[2]
}

return(geoflow_kvp$new(key = key, values = values, locale = locale))
}

Expand All @@ -154,12 +153,16 @@ extract_kvps <- function(strs, collapse = NULL){
kvps <- lapply(strs, function(str){
kvp <- extract_kvp(str)
if(!is.null(collapse)) kvp$values <- list(paste0(kvp$values, collapse = collapse))
if(length(kvp$values)==1) kvp$values <- kvp$values[[1]]
return(kvp)
})
keys <- unique(sapply(kvps, "[[", "key"))
kvps <- lapply(keys, function(key){
kvps <- do.call("c", lapply(keys, function(key){
kvps_for_key <- kvps[sapply(kvps, function(kvp){kvp$key == key})]
with_null_locale <- any(sapply(kvps_for_key, function(x){is.null(x$locale)}))
with_locales <- any(sapply(kvps_for_key, function(x){!is.null(x$locale)}))
if(!with_locales){
return(kvps_for_key)
}
kvp_with_default_locale <- kvps_for_key[sapply(kvps_for_key, function(x){is.null(x$locale)})]
kvp_with_locale <- kvps_for_key[sapply(kvps_for_key, function(x){!is.null(x$locale)})]
if(length(kvp_with_default_locale)>0){
Expand All @@ -168,7 +171,6 @@ extract_kvps <- function(strs, collapse = NULL){
#TODO support default language in geoflow
}
#localization
key <- kvp_with_default_locale$key
#locale key descriptions
for(kvp in kvp_with_locale){
if(!is.null(attr(kvp$key, "uri"))) attr(key, paste0("uri#", kvp$locale)) <- attr(kvp$key, "uri")
Expand All @@ -177,14 +179,14 @@ extract_kvps <- function(strs, collapse = NULL){
#locale key uris
#locale values
locale_values <- kvp_with_default_locale$values
if(length(locale_values)==1) locale_values <- locale_values[[1]]
#if(length(locale_values)==1) locale_values <- locale_values[[1]]
for(item in kvp_with_locale){
attr(locale_values, paste0("locale#", toupper(item$locale))) <- item$values
}

kvp_with_locales <- geoflow_kvp$new(key = key, values = locale_values)
kvp_with_locales <- list(geoflow_kvp$new(key = key, values = locale_values))
return(kvp_with_locales)
})
}))

return(kvps)
}
Expand Down
4 changes: 2 additions & 2 deletions doc/Summary_metadata.csv
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
"Rights","accessConstraint",,,"x",,,,,,"x",,
"Rights","otherConstraint","x","x","x",,,,,,"x",,
"Rights","license","x","x","x",,,,,"x","x",,"x"
"Provenance","statement","x",,"x",,,,,,,,
"Provenance","process","x",,"x",,,,,,,,
"Provenance","statement","x","x","x",,,,,,,,
"Provenance","process","x","x","x",,,,,,,,
"Format","ressource",,,"x",,,,,,,,
"Format","distribution",,,"x",,,,,,,,
"Data",,,,,,,,,,,,
6 changes: 3 additions & 3 deletions inst/actions/geometa_create_iso_19115.R
Original file line number Diff line number Diff line change
Expand Up @@ -826,13 +826,13 @@ function(action, entity, config){
dq_lineage_scope$setLevel(dctype_iso)
dq_lineage$setScope(dq_lineage_scope)
lineage <- ISOLineage$new()
lineage$setStatement(entity$provenance$statement)
lineage$setStatement(entity$provenance$statement, locales = geoflow::get_locales_from(entity$provenance$statement))
processes <- entity$provenance$processes
if(length(processes)>0){
for(process in processes){
processStep <- ISOProcessStep$new()
processStep$setRationale(process$rationale)
processStep$setDescription(process$description)
processStep$setRationale(process$rationale, locales = geoflow::get_locales_from(process$rationale))
processStep$setDescription(process$description, locales = geoflow::get_locales_from(process$description))

#processor as responsability party
for(processor in process$processors){
Expand Down

0 comments on commit 8555731

Please sign in to comment.