10
10
# ' can be extracted using \code{param_obj} and \code{output_obj}.
11
11
# '
12
12
# ' @export
13
+ # ' @import rlang
13
14
# ' @inheritParams struct_class
14
15
# ' @param obj An entity object
15
16
# ' @param max_length Maximum length of value vector (default 1)
16
17
# ' @param value The value of the parameter/outputs
17
18
# ' @param ... additional inputs to the struct_class object
18
- # ' @include generics.R struct_class.R parameter_class.R constraint_class.R
19
+ # ' @include generics.R struct_class.R parameter_class.R
19
20
# ' @return An entity object
20
21
# ' @examples
21
22
# ' # Create a new entity object
31
32
# ' value(E) = 10
32
33
# ' @rdname entity
33
34
entity = function (
34
- name ,
35
- description = character (0 ),
36
- type = ' character' ,
37
- value = NULL ,
38
- max_length = Inf ,
39
- constraints = list (
40
- constraint.type(), # check type by default
41
- constraint.max_len() # check max length by default
42
- ),
43
- ... ) {
35
+ name ,
36
+ description = character (0 ),
37
+ type = ' character' ,
38
+ value = NULL ,
39
+ max_length = Inf ,
40
+ constraints = list (
41
+ type_equals ,
42
+ maximum_length
43
+ ),
44
+ ... ) {
44
45
45
46
value = check_init_val(value ,type )
46
47
@@ -67,27 +68,25 @@ entity = function(
67
68
value = character (0 ),
68
69
type = ' character' ,
69
70
max_length = Inf ,
70
- ontology = character (),
71
- constraints = list (
72
- constraint.type(), # check type by default
73
- constraint.max_len() # check max length by default
74
- )
75
- ),
71
+ ontology = character ()),
76
72
validity = function (object ) {
77
73
78
- # loop over all constraints
79
- msg = character (0 )
80
- for (k in object @ constraints ) {
81
- # test constraint
82
- check = k @ test(object )
74
+ msg = character (0 )
75
+ for (k in object @ constraints ){
76
+ check = eval(k $ ex ,env = list2env(slots(object )))
83
77
if (! check ) {
84
- msg = c(msg ,k @ message(object ))
78
+ msg = c(
79
+ msg ,
80
+ paste0(' Entity "' ,object $ name ,
81
+ ' " is not valid because ' ,
82
+ eval(k $ msg ,env = list2env(slots(object )))
83
+ )
84
+ )
85
85
}
86
86
}
87
- # if no messages the all checks passed
88
87
if (length(msg )== 0 ) {
89
88
return (TRUE )
90
- } else { # otherwise return message
89
+ } else {
91
90
return (msg )
92
91
}
93
92
}
@@ -112,62 +111,130 @@ check_init_val=function(value,type) {
112
111
# ' @rdname entity
113
112
# ' @export
114
113
setMethod (f = "value ",
115
- signature = c(" entity" ),
116
- definition = function (obj ) {
117
- return (obj @ value )
118
- }
114
+ signature = c(" entity" ),
115
+ definition = function (obj ) {
116
+ return (obj @ value )
117
+ }
119
118
)
120
119
121
120
# ' @rdname entity
122
121
# ' @export
123
122
setMethod(f = " value<-" ,
124
- signature = c(" entity" ),
125
- definition = function (obj ,value ) {
126
- obj @ value = value
127
- validObject(obj )
128
- return (obj )
129
- }
123
+ signature = c(" entity" ),
124
+ definition = function (obj ,value ) {
125
+ obj @ value = value
126
+ validObject(obj )
127
+ return (obj )
128
+ }
130
129
)
131
130
132
131
# ' @rdname entity
133
132
# ' @export
134
133
setMethod (f = "max_length ",
135
- signature = c(" entity" ),
136
- definition = function (obj ) {
137
- return (obj @ max_length )
138
- }
134
+ signature = c(" entity" ),
135
+ definition = function (obj ) {
136
+ return (obj @ max_length )
137
+ }
139
138
)
140
139
141
140
# ' @rdname entity
142
141
# ' @export
143
142
setMethod(f = " max_length<-" ,
144
- signature = c(" entity" ),
145
- definition = function (obj ,value ) {
146
- obj @ max_length = value
147
- validObject(obj )
148
- return (obj )
149
- }
143
+ signature = c(" entity" ),
144
+ definition = function (obj ,value ) {
145
+ obj @ max_length = value
146
+ validObject(obj )
147
+ return (obj )
148
+ }
150
149
)
151
150
152
151
153
152
setMethod (f = 'show ',
154
- signature = c(' entity' ),
155
- definition = function (object ) {
156
- callNextMethod() # force the default output
157
-
158
- V = value(object )
159
- if (is(V ,' DatasetExperiment' ) | is(V ,' SummarizedExperiment' ) | is(V ,' matrix' )) {
160
- V = paste0(nrow(V ), ' rows x ' , ncol(V ), ' columns (' ,class(V ),' )' )
161
- } else if (is.atomic(V )) {
162
- V = V
163
- } else {
164
- V = class(V )
165
- }
153
+ signature = c(' entity' ),
154
+ definition = function (object ) {
155
+ callNextMethod() # force the default output
166
156
167
- # add extra info
168
- cat(' value: ' , V , ' \n ' ,sep = ' ' )
169
- cat(' type: ' , paste0(object $ type ,collapse = ' , ' ), ' \n ' ,sep = ' ' )
170
- cat(' max length: ' , max_length(object ),sep = ' ' )
171
- cat(' \n ' )
172
- }
157
+ V = value(object )
158
+ if (is(V ,' DatasetExperiment' ) | is(V ,' SummarizedExperiment' ) | is(V ,' matrix' )) {
159
+ V = paste0(nrow(V ), ' rows x ' , ncol(V ), ' columns (' ,class(V ),' )' )
160
+ } else if (is.atomic(V )) {
161
+ V = V
162
+ } else {
163
+ V = class(V )
164
+ }
165
+
166
+ # add extra info
167
+ cat(' value: ' , V , ' \n ' ,sep = ' ' )
168
+ cat(' type: ' , paste0(object $ type ,collapse = ' , ' ), ' \n ' ,sep = ' ' )
169
+ cat(' max length: ' , max_length(object ),sep = ' ' )
170
+ cat(' \n ' )
171
+ }
172
+ )
173
+
174
+
175
+ # ' Entity constraints
176
+ # '
177
+ # ' This helper function creates a list of quosures that can be used to apply
178
+ # ' constraints to an entity object.
179
+ # '
180
+ # ' @export
181
+ # ' @param ... any number of constraints e.g. value<6
182
+ # ' @returns A list of quosures that will be evaluated to check validity of the entity
183
+ # ' @examples
184
+ # ' # ensure the value of an entity is exactly 6
185
+ # ' constraint(value==6)
186
+ # '
187
+ # ' # check the length
188
+ # ' constraint(length(value)==1)
189
+ # '
190
+ # '
191
+ entity_constraint = function (ex ,msg ){
192
+ return (
193
+ list (
194
+ ex = enexpr(ex ),
195
+ msg = enexpr(msg )
196
+ )
197
+ )
198
+ }
199
+
200
+
201
+
202
+ # function to return all slots as a list
203
+ slots = function (object ){
204
+ n = slotNames(object )
205
+ S = lapply(n ,function (x ){
206
+ return (slot(object ,x ))
207
+ })
208
+ names(S )= n
209
+ return (S )
210
+ }
211
+
212
+ # function to expand inherits to vectors/lists
213
+ elements_inherit = function (x , class_name ) {
214
+ sapply(x , function (el ) inherits(el , class_name ))
215
+ }
216
+
217
+ # check that all elements of a vector inherit one of the classes in 'type' INCLUDES LISTS
218
+ vector_type_equals = entity_constraint(
219
+ ex = all(elements_inherit(value ,type )) | (' ANY' %in% type ),
220
+ msg = paste0(' all elements in "value" must be ' , paste0(' "' ,type ,' "' ,collapse = ' or ' ),' .' ))
221
+
222
+ # check that the value is of the correct type
223
+ type_equals = entity_constraint(
224
+ ex = inherits(value ,type ) | (' ANY' %in% type ),
225
+ msg = paste0(' "value" must be ' , paste0(' "' ,type ,' "' ,collapse = ' or ' ),' .' )
173
226
)
227
+
228
+ # check length does not exceed value
229
+ maximum_length = entity_constraint(
230
+ ex = length(value ) < = max_length ,
231
+ msg = paste0(' the length of "value" must be less than or equal to ' , max_length )
232
+ )
233
+
234
+ # check minimum value
235
+ minimum_value = function (x ) {
236
+ entity_constraint(
237
+ ex = all(value > = !! x ),
238
+ msg = paste0(' "value" must be greater than ' , !! x )
239
+ )
240
+ }
0 commit comments