Skip to content

Commit

Permalink
close #107: improve arg parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Enchufa2 committed Jul 13, 2017
1 parent df5218c commit c0984bd
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 124 deletions.
3 changes: 1 addition & 2 deletions R/simulator-methods.R
Expand Up @@ -71,8 +71,7 @@ run <- function(.env, until=1000, progress=NULL, steps=10) UseMethod("run")

#' @export
run.simmer <- function(.env, until=1000, progress=NULL, steps=10) {
progress <- evaluate_value(progress)
steps <- evaluate_value(steps)
check_args(until, progress, steps, types=c("number", "function or NULL", "number"))
if (is.function(progress)) {
progress(0)
for (i in seq(until/steps, until, until/steps)) {
Expand Down
66 changes: 20 additions & 46 deletions R/simulator.R
Expand Up @@ -3,8 +3,9 @@ Simmer <- R6Class("simmer",
name = NA,

initialize = function(name="anonymous", verbose=FALSE) {
self$name <- evaluate_value(name)
private$sim_obj <- Simulator__new(name, evaluate_value(verbose))
check_args(name, verbose, types=c("string", "flag"), n=3)
self$name <- name
private$sim_obj <- Simulator__new(name, verbose)
self
},

Expand Down Expand Up @@ -38,9 +39,7 @@ Simmer <- R6Class("simmer",
now = function() { now_(private$sim_obj) },

peek = function(steps=1, verbose=FALSE) {
steps <- evaluate_value(steps)
verbose <- evaluate_value(verbose)
steps <- replace(steps, steps == Inf, -1)
check_args(steps, verbose, types=c("number", "flag"))
ret <- peek_(private$sim_obj, steps)
if (!verbose) ret$time
else ret # nocov
Expand All @@ -52,37 +51,25 @@ Simmer <- R6Class("simmer",
},

run = function(until=1000) {
until <- evaluate_value(until)
until <- replace(until, until == Inf, -1)
run_(private$sim_obj, until)
self
},

add_resource = function(name, capacity=1, queue_size=Inf, mon=TRUE, preemptive=FALSE,
preempt_order=c("fifo", "lifo"), queue_size_strict=FALSE) {
check_args(name, capacity, queue_size, mon, preemptive, queue_size_strict,
types=c("string", rep("number or schedule", 2), rep("flag", 3)))
preempt_order <- match.arg(preempt_order)
name <- evaluate_value(name)
capacity <- evaluate_value(capacity)
capacity_schedule <- NA
queue_size <- evaluate_value(queue_size)
queue_size_schedule <- NA
mon <- evaluate_value(mon)
preemptive <- evaluate_value(preemptive)
queue_size_strict <- evaluate_value(queue_size_strict)

if (is.numeric(capacity) && is.infinite(capacity))
capacity <- -1
else if (inherits(capacity, "schedule")) {

if (inherits(capacity, "schedule")) {
capacity_schedule <- capacity
capacity <- capacity_schedule$get_schedule()$init
}
} else capacity_schedule <- NA

if (is.numeric(queue_size) && is.infinite(queue_size))
queue_size <- -1
else if (inherits(queue_size, "schedule")) {
if (inherits(queue_size, "schedule")) {
queue_size_schedule <- queue_size
queue_size <- queue_size_schedule$get_schedule()$init
}
} else queue_size_schedule <- NA

ret <- add_resource_(private$sim_obj, name, capacity, queue_size, mon,
preemptive, preempt_order, queue_size_strict)
Expand All @@ -103,22 +90,15 @@ Simmer <- R6Class("simmer",

add_generator = function(name_prefix, trajectory, distribution, mon=1,
priority=0, preemptible=priority, restart=FALSE) {
stopifnot(inherits(trajectory, "trajectory"))
name_prefix <- evaluate_value(name_prefix)
mon <- evaluate_value(mon)
priority <- evaluate_value(priority)
preemptible <- evaluate_value(preemptible)
restart <- evaluate_value(restart)
distribution <- make_resetable(distribution)
ret <- add_generator_(private$sim_obj, name_prefix, trajectory[], distribution, mon,
priority, preemptible, restart)
check_args(name_prefix, trajectory, distribution, mon, priority, preemptible, restart,
types=c("string", "trajectory", "function", "flag", rep("number", 2), "flag"))
ret <- add_generator_(private$sim_obj, name_prefix, trajectory[],
make_resetable(distribution), mon, priority, preemptible, restart)
if (ret) private$gen[[name_prefix]] <- mon
self
},

get_mon_arrivals = function(per_resource=FALSE, ongoing=FALSE) {
per_resource <- evaluate_value(per_resource)
ongoing <- evaluate_value(ongoing)
get_mon_arrivals_(private$sim_obj, per_resource, ongoing)
},

Expand Down Expand Up @@ -150,29 +130,23 @@ Simmer <- R6Class("simmer",
monitor_data
},

get_n_generated = function(name) {
get_n_generated_(private$sim_obj, evaluate_value(name))
},
get_n_generated = function(name) get_n_generated_(private$sim_obj, name),

get_capacity = function(name) {
ret <- get_capacity_(private$sim_obj, evaluate_value(name))
ret <- get_capacity_(private$sim_obj, name)
if (ret < 0) ret <- Inf
ret
},

get_queue_size = function(name) {
ret <- get_queue_size_(private$sim_obj, evaluate_value(name))
ret <- get_queue_size_(private$sim_obj, name)
if (ret < 0) ret <- Inf
ret
},

get_server_count = function(name) {
get_server_count_(private$sim_obj, evaluate_value(name))
},
get_server_count = function(name) get_server_count_(private$sim_obj, name),

get_queue_count = function(name) {
get_queue_count_(private$sim_obj, evaluate_value(name))
},
get_queue_count = function(name) get_queue_count_(private$sim_obj, name),

# not exposed, internal use
get_generators = function() { private$gen },
Expand Down
4 changes: 2 additions & 2 deletions R/trajectory-activities.R
Expand Up @@ -245,10 +245,10 @@ branch.trajectory <- function(.trj, option, continue, ...) .trj$branch(option, c
#'
#' @return Returns the trajectory object.
#' @export
rollback <- function(.trj, amount, times=Inf, check) UseMethod("rollback")
rollback <- function(.trj, amount, times=Inf, check=NULL) UseMethod("rollback")

#' @export
rollback.trajectory <- function(.trj, amount, times=Inf, check) .trj$rollback(amount, times, check)
rollback.trajectory <- function(.trj, amount, times=Inf, check=NULL) .trj$rollback(amount, times, check)

#' Add a leave activity
#'
Expand Down

0 comments on commit c0984bd

Please sign in to comment.