Skip to content

Commit 55d9427

Browse files
committed
use tidy constraints
1 parent 3525473 commit 55d9427

File tree

7 files changed

+162
-143
lines changed

7 files changed

+162
-143
lines changed

DESCRIPTION

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ Collate:
3434
'chart_class.R'
3535
'output_class.R'
3636
'DatasetExperiment_class.R'
37-
'constraint_class.R'
3837
'entity_class.R'
3938
'stato_class.R'
4039
'entity_stato_class.R'
@@ -65,5 +64,5 @@ Suggests:
6564
VignetteBuilder: knitr
6665
Imports: methods,ontologyIndex,
6766
datasets, graphics, stats, utils, knitr,
68-
SummarizedExperiment, S4Vectors, rols
67+
SummarizedExperiment, S4Vectors, rols,rlang
6968
biocViews: WorkflowStep

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export(chart_names)
2626
export(chart_plot)
2727
export(citations)
2828
export(entity)
29+
export(entity_constraint)
2930
export(entity_stato)
3031
export(enum)
3132
export(enum_stato)
@@ -147,6 +148,7 @@ import(S4Vectors)
147148
import(SummarizedExperiment)
148149
import(datasets)
149150
import(methods)
151+
import(rlang)
150152
importFrom(graphics,plot)
151153
importFrom(knitr,purl)
152154
importFrom(ontologyIndex,get_ontology)

R/constraint_class.R

Lines changed: 0 additions & 76 deletions
This file was deleted.

R/entity_class.R

Lines changed: 130 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@
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
@@ -31,16 +32,16 @@
3132
#' value(E) = 10
3233
#' @rdname entity
3334
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+
...) {
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
114113
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+
}
119118
)
120119

121120
#' @rdname entity
122121
#' @export
123122
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+
}
130129
)
131130

132131
#' @rdname entity
133132
#' @export
134133
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+
}
139138
)
140139

141140
#' @rdname entity
142141
#' @export
143142
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+
}
150149
)
151150

152151

153152
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
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+
}

R/example_objects.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ iris_DatasetExperiment = function () {
4545
#' @param value_0 a numeric value
4646
#' @param value_1 a numeric value
4747
#' @param value_2 a numeric value
48-
#' @param ... additional slots and values to pass to struct_class
48+
#' @param ... additional slots and values to pass to struct_class
4949
#' @rdname example_model
5050
#' @include model_class.R
5151
#' @examples

man/entity.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)