Skip to content

Commit 8921c10

Browse files
committed
add max_length and type checking to entity objects
- enforce a length on entity objects to prevent a list when a single input is required - add type checking for entity objects. initialised value must be of type specied by 'type' slot
1 parent f9ed38b commit 8921c10

File tree

8 files changed

+93
-6
lines changed

8 files changed

+93
-6
lines changed

NAMESPACE

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export("dataset.data<-")
44
export("dataset.sample_meta<-")
55
export("dataset.variable_meta<-")
66
export("description<-")
7+
export("max_length<-")
78
export("method.steps<-")
89
export("models<-")
910
export("name<-")
@@ -39,6 +40,7 @@ export(iris_dataset)
3940
export(is.output)
4041
export(is.param)
4142
export(iterator)
43+
export(max_length)
4244
export(method)
4345
export(method.apply)
4446
export(method.seq)
@@ -91,6 +93,7 @@ exportMethods("dataset.data<-")
9193
exportMethods("dataset.sample_meta<-")
9294
exportMethods("dataset.variable_meta<-")
9395
exportMethods("description<-")
96+
exportMethods("max_length<-")
9497
exportMethods("method.steps<-")
9598
exportMethods("models<-")
9699
exportMethods("name<-")
@@ -114,6 +117,7 @@ exportMethods(evaluate)
114117
exportMethods(is.output)
115118
exportMethods(is.param)
116119
exportMethods(length)
120+
exportMethods(max_length)
117121
exportMethods(method.apply)
118122
exportMethods(method.steps)
119123
exportMethods(model.predict)

R/entity_class.R

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#' @export entity
1313
#' @param obj An entity object
1414
#' @param value Value of the entity
15+
#' @param max_length maximum length of value vector (default 1)
1516
#' @include generics.R struct_class.R
1617
#' @return and entity object
1718
#' @examples
@@ -29,10 +30,27 @@
2930
#'
3031
entity<-setClass(
3132
"entity",
32-
slots=c('value'),
33+
slots=c(value='ANY'),
3334
contains='struct_class',
3435
prototype=list(name='name not provided',
35-
description='no description provided')
36+
description='no description provided',
37+
value='',
38+
type='character',
39+
max_length=1
40+
),
41+
validity = function(object) {
42+
check_length=length(value(object)) <= max_length(object)
43+
check_type=class(value(object))[1] %in% type(object)
44+
45+
msg=TRUE
46+
if (!check_length) {
47+
msg=paste0(name(object),': number of values must be less than "max_length"')
48+
}
49+
if (!check_type) {
50+
msg=paste0(name(object),': class of value must match "type"')
51+
}
52+
return(msg)
53+
}
3654
)
3755

3856
#' @describeIn entity get the value for an entity
@@ -52,8 +70,29 @@ setMethod(f="value<-",
5270
definition=function(obj,value)
5371
{
5472
obj@value=value
73+
validObject(obj)
5574
return(obj)
5675
}
5776
)
5877

78+
#' @describeIn entity get the maximum length of value vector for an entity
79+
#' @export
80+
setMethod(f="max_length",
81+
signature=c("entity"),
82+
definition=function(obj)
83+
{
84+
return(obj@max_length)
85+
}
86+
)
5987

88+
#' @describeIn entity set the maximum length of value vector for an entity
89+
#' @export
90+
setMethod(f="max_length<-",
91+
signature=c("entity"),
92+
definition=function(obj,value)
93+
{
94+
obj@max_length=value
95+
validObject(obj)
96+
return(obj)
97+
}
98+
)

R/example_objects.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ example_model=setClass('example_model',
5757
stato.id='STATO:0000047'),
5858
params.value_2=20,
5959
outputs.result_1=entity(name='Result 1',type='dataset',
60-
description='An example entity object'),
60+
description='An example entity object',value=dataset()),
6161
outputs.result_2=dataset(),
6262
predicted='result_1'
6363
)
@@ -167,7 +167,7 @@ example_method=setClass('example_method',
167167
stato.id='STATO:0000047'),
168168
params.value_2=20,
169169
outputs.result_1=entity(name='Result 1',type='dataset',
170-
description='An example entity object'),
170+
description='An example entity object',value=dataset()),
171171
outputs.result_2=dataset()
172172
)
173173
)

R/generics.R

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,19 @@ setGeneric("value",function(obj)standardGeneric("value"))
693693
#' @rdname metric
694694
setGeneric("value<-",function(obj,value)standardGeneric("value<-"))
695695

696+
#' get the max value vector length for an entity
697+
#'
698+
#' @return max value vector length for an entity
699+
#' @rdname entity
700+
#' @export
701+
setGeneric("max_length",function(obj)standardGeneric("max_length"))
702+
703+
#' set the value for a metric
704+
#'
705+
#' @export
706+
#' @rdname metric
707+
setGeneric("max_length<-",function(obj,value)standardGeneric("max_length<-"))
708+
696709
####################################
697710
###### stato class generics #####
698711
####################################

man/entity-class.Rd

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/entity.Rd

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/metric.Rd

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-base.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ test_that('metric object',{
2626

2727
# test entity
2828
test_that('entity object',{
29-
E=entity()
29+
E=entity(type='numeric',value=0)
3030
value(E)=1
3131
expect_equal(value(E),1)
3232
})
3333

3434
# test enum
3535
test_that('enum object',{
36-
E=enum(list=c('hello','world'),value='hello')
36+
E=enum(list=c('hello','world'),value='hello',type='character')
3737
# check object creation
3838
expect_equal(value(E),'hello')
3939
# check use first value if value = NULL

0 commit comments

Comments
 (0)