-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtask.R
165 lines (156 loc) · 5.49 KB
/
task.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#' @title Task
#' @description R6 class for Task settings
#' @field active logical indicating if `Task` is performed in a workflow.
#' @field outputFolder List of output files/folders to save the task output
#' @field workflowFolder folder where workflow is run and saved
#' @field settings list of settings for task such as plot configurations
#' @field message message or title of the task
#' @export
#' @import ospsuite.utils
#' @family workflow tasks
Task <- R6::R6Class(
"Task",
public = list(
active = NULL,
outputFolder = NULL,
workflowFolder = NULL,
settings = NULL,
message = NULL,
#' @description
#' Create a `Task` object
#' @param outputFolder task output folder to save results
#' @param inputFolder task input folder where input files are stored
#' @param inputs expected input files required by task
#' @param outputs expected output files generated by task
#' @param workflowFolder folder where workflow is run and saved
#' @param settings specific settings for task (e.g. plot configurations)
#' @param active logical indicating if `Task` is performed in a workflow.
#' Default value is `FALSE`
#' @param message message of the `Task`.
#' @return A new `Task` object
initialize = function(outputFolder = NULL,
inputFolder = NULL,
inputs = NULL,
outputs = NULL,
workflowFolder = getwd(),
settings = NULL,
active = FALSE,
message = NULL) {
validateIsOfType(active, "logical")
validateIsString(c(outputFolder, inputFolder, inputs, outputs), nullAllowed = TRUE)
self$active <- active
self$outputFolder <- outputFolder
self$workflowFolder <- workflowFolder
self$settings <- settings
self$message <- message
private$.inputFolder <- inputFolder
private$.inputs <- inputs
private$.outputs <- outputs
},
#' @description
#' Activate `Task`
activate = function() {
self$active <- TRUE
},
#' @description
#' Inactivate `Task`
inactivate = function() {
self$active <- FALSE
},
#' @description
#' Check if `Task` inputs exist
#' @return logical indicating if input is valid
validateInput = function() {
isValid <- TRUE
for (inputToCheck in private$.inputs) {
if (!file.exists(inputToCheck)) {
isValid <- FALSE
logErrorThenStop(messages$errorTaskInputDoesNotExist(inputToCheck))
}
}
return(isValid)
},
#' @description
#' Check if a task input from a specific `SimulationStructure` is valid
#' @param structureSet `SimulationStructure` object
#' @return logical indicating if input is valid
validateStructureSetInput = function(structureSet) {
validateIsOfType(structureSet, "SimulationStructure")
# Get only the input from the structure set
structureSetInputs <- c(
structureSet$simulationResultFileNames,
structureSet$pkAnalysisResultsFileNames,
structureSet$sensitivityAnalysisResultsFileNames
)
inputsToCheck <- intersect(structureSetInputs, private$.inputs)
# If no required input
if (isEmpty(inputsToCheck)) {
return(TRUE)
}
tryCatch(
{
validateFileExists(inputsToCheck, nullAllowed = TRUE)
},
error = function(e) {
missingInputs <- inputsToCheck[!file.exists(inputsToCheck)]
logErrorThenStop(message = messages$errorTaskInputDoesNotExist(missingInputs), self$workflowFolder)
}
)
# If no error is caught, return isValid = TRUE
return(TRUE)
},
#' @description
#' Print `Task` features
#' @return Text of task features
print = function() {
taskActiveMessage <- " NOT"
if (self$active) {
taskActiveMessage <- ""
}
info <- c(
sprintf("Task: %s", self$message %||% ""),
sprintf("Task is%s active", taskActiveMessage),
sprintf("Workflow folder: %s", self$workflowFolder %||% ""),
sprintf("Expected input folder: %s", private$.inputFolder %||% "none"),
sprintf("Expected input files: %s", ifNotNull(
private$.inputs,
paste0(private$.inputs, collapse = ", "),
"none"
)),
sprintf("Expected output folder: %s", self$outputFolder %||% "none"),
sprintf("Expected output files: %s", ifNotNull(
private$.outputs,
paste0(private$.outputs, collapse = ", "),
"none"
))
)
invisible(self)
return(info)
},
#' @description
#' Get the relative path to a file to be output by this task
#' @param fileName name (with extension) of the file to be output
#' @return Output file's relative path
getRelativePath = function(fileName) {
return(file.path(self$outputFolder, fileName))
},
#' @description
#' Get the absolute path to a file to be output by this task
#' @param fileName name (with extension) of the file to be output
#' @return Output file's absolute path
getAbsolutePath = function(fileName) {
return(file.path(self$workflowFolder, self$outputFolder, fileName))
},
#' @description
#' Get `Task` required input files
#' @return Required file names
getInputs = function() {
return(private$.inputs)
}
),
private = list(
.inputFolder = NULL,
.inputs = NULL,
.outputs = NULL
)
)