1010# ' can be extracted using \code{param_obj} and \code{output_obj}.
1111# '
1212# ' @export
13+ # ' @import rlang
1314# ' @inheritParams struct_class
1415# ' @param obj An entity object
1516# ' @param max_length Maximum length of value vector (default 1)
1617# ' @param value The value of the parameter/outputs
1718# ' @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
1920# ' @return An entity object
2021# ' @examples
2122# ' # Create a new entity object
3132# ' value(E) = 10
3233# ' @rdname entity
3334entity = 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+ ... ) {
4445
4546 value = check_init_val(value ,type )
4647
@@ -67,27 +68,25 @@ entity = function(
6768 value = character (0 ),
6869 type = ' character' ,
6970 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 ()),
7672 validity = function (object ) {
7773
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 )))
8377 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+ )
8585 }
8686 }
87- # if no messages the all checks passed
8887 if (length(msg )== 0 ) {
8988 return (TRUE )
90- } else { # otherwise return message
89+ } else {
9190 return (msg )
9291 }
9392 }
@@ -112,62 +111,130 @@ check_init_val=function(value,type) {
112111# ' @rdname entity
113112# ' @export
114113setMethod (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+ }
119118)
120119
121120# ' @rdname entity
122121# ' @export
123122setMethod(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+ }
130129)
131130
132131# ' @rdname entity
133132# ' @export
134133setMethod (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+ }
139138)
140139
141140# ' @rdname entity
142141# ' @export
143142setMethod(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+ }
150149)
151150
152151
153152setMethod (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
166156
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 ' ),' .' )
173226)
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