Skip to content

Commit

Permalink
initial implementation of #177
Browse files Browse the repository at this point in the history
  • Loading branch information
Enchufa2 committed Nov 29, 2018
1 parent 173ee82 commit bdca4a2
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 4 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ S3method(get_server_count_selected,simmer)
S3method(get_sources,simmer)
S3method(get_sources,wrap)
S3method(get_trajectory,simmer)
S3method(handle_unfinished,trajectory)
S3method(join,trajectory)
S3method(leave,trajectory)
S3method(length,trajectory)
Expand Down Expand Up @@ -116,6 +117,7 @@ export(get_server_count)
export(get_server_count_selected)
export(get_sources)
export(get_trajectory)
export(handle_unfinished)
export(join)
export(leave)
export(log_)
Expand Down
4 changes: 4 additions & 0 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ Leave__new <- function(prob) {
.Call(`_simmer_Leave__new`, prob)
}

HandleUnfinished__new <- function(trj) {
.Call(`_simmer_HandleUnfinished__new`, trj)
}

Leave__new_func <- function(prob) {
.Call(`_simmer_Leave__new_func`, prob)
}
Expand Down
17 changes: 17 additions & 0 deletions R/trajectory-activities.R
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,23 @@ leave <- function(.trj, prob) UseMethod("leave")
#' @export
leave.trajectory <- function(.trj, prob) .trj$leave(prob)

#' Handle Unfinished Arrivals
#'
#' Activity for setting a drop-out trajectory for unfinished arrivals, i.e.,
#' those dropped from a resource (due to preemption or resource shrinkage) or
#' those that \code{\link{leave}} a trajectory.
#'
#' @inheritParams seize
#' @param handler trajectory object to handle unfinished arrivals. A \code{NULL}
#' value will unset the drop-out trajectory.
#'
#' @return Returns the trajectory object.
#' @export
handle_unfinished <- function(.trj, handler) UseMethod("handle_unfinished")

#' @export
handle_unfinished.trajectory <- function(.trj, handler) .trj$handle_unfinished(handler)

#' Renege on some Condition
#'
#' Activities for setting or unsetting a timer or a signal after which the arrival will abandon.
Expand Down
6 changes: 6 additions & 0 deletions R/trajectory-class.R
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,12 @@ Trajectory <- R6Class("trajectory",
)
},

handle_unfinished = function(handler) {
check_args(handler=c("trajectory", "NULL"))
traj <- as.list(c(handler[]))
private$add_activity(HandleUnfinished__new(traj))
},

renege_in = function(t, out=NULL) {
check_args(t=c("number", "function"), out=c("trajectory", "NULL"))
traj <- as.list(c(out[]))
Expand Down
25 changes: 25 additions & 0 deletions inst/include/simmer/activity/leave.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,31 @@ namespace simmer {
T prob;
};

/**
* Set a path to handle unfinished arrivals (from 'leave' or resources)
*/
class HandleUnfinished : public Fork {
public:
CLONEABLE(HandleUnfinished)

HandleUnfinished(const VEC<REnv>& trj)
: Fork("HandleUnfinished", VEC<bool>(trj.size(), false), trj) {}

void print(unsigned int indent = 0, bool verbose = false, bool brief = false) {
Activity::print(indent, verbose, brief);
internal::print(brief, false);
Fork::print(indent, verbose, brief);
}

double run(Arrival* arrival) {
Activity* next = NULL;
if (heads.size())
next = heads[0];
arrival->set_dropout(next);
return 0;
}
};

} // namespace simmer

#endif
11 changes: 7 additions & 4 deletions inst/include/simmer/process/arrival.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ namespace simmer {
Arrival(Simulator* sim, const std::string& name, int mon, Order order,
Activity* first_activity, int priority = 0)
: Process(sim, name, mon, priority), order(order), paused(false),
clones(new int(0)), activity(first_activity), timer(NULL), batch(NULL)
{ init(); }
clones(new int(0)), activity(first_activity), timer(NULL),
dropout(NULL), batch(NULL) { init(); }

Arrival(const Arrival& o)
: Process(o), order(o.order), paused(o.paused), clones(o.clones),
activity(NULL), attributes(o.attributes), timer(NULL), batch(NULL)
{ init(); }
activity(NULL), attributes(o.attributes), timer(NULL),
dropout(NULL), batch(NULL) { init(); }

~Arrival() { reset(); }

Expand Down Expand Up @@ -186,6 +186,8 @@ namespace simmer {
batch = NULL;
}

void set_dropout(Activity* next) { dropout = next; }

void set_renege(double timeout, Activity* next) {
cancel_renege();
timer = new Task(sim, "Renege-Timer",
Expand Down Expand Up @@ -222,6 +224,7 @@ namespace simmer {
SelMap selected; /**< selected resource */
Task* timer; /**< timer that triggers reneging */
std::string signal; /**< signal that triggers reneging */
Activity* dropout; /**< drop-out trajectory */
Batched* batch; /**< batch that contains this arrival */
ResMSet resources; /**< resources that contain this arrival */

Expand Down
6 changes: 6 additions & 0 deletions inst/include/simmer/process/arrival_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@
namespace simmer {

inline void Arrival::terminate(bool finished) {
if (!finished && dropout) {
activity = dropout;
sim->schedule(0, this, priority);
return;
}

foreach_ (ResMSet::value_type& itr, resources) {
Rcpp::warning("'%s': leaving without releasing '%s'", name, itr->name);
itr->erase(this, true);
Expand Down
22 changes: 22 additions & 0 deletions man/handle_unfinished.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,17 @@ BEGIN_RCPP
return rcpp_result_gen;
END_RCPP
}
// HandleUnfinished__new
SEXP HandleUnfinished__new(const std::vector<Environment>& trj);
RcppExport SEXP _simmer_HandleUnfinished__new(SEXP trjSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< const std::vector<Environment>& >::type trj(trjSEXP);
rcpp_result_gen = Rcpp::wrap(HandleUnfinished__new(trj));
return rcpp_result_gen;
END_RCPP
}
// Leave__new_func
SEXP Leave__new_func(const Function& prob);
RcppExport SEXP _simmer_Leave__new_func(SEXP probSEXP) {
Expand Down Expand Up @@ -1335,6 +1346,7 @@ static const R_CallMethodDef CallEntries[] = {
{"_simmer_Rollback__new", (DL_FUNC) &_simmer_Rollback__new, 2},
{"_simmer_Rollback__new_func", (DL_FUNC) &_simmer_Rollback__new_func, 2},
{"_simmer_Leave__new", (DL_FUNC) &_simmer_Leave__new, 1},
{"_simmer_HandleUnfinished__new", (DL_FUNC) &_simmer_HandleUnfinished__new, 1},
{"_simmer_Leave__new_func", (DL_FUNC) &_simmer_Leave__new_func, 1},
{"_simmer_Clone__new", (DL_FUNC) &_simmer_Clone__new, 2},
{"_simmer_Clone__new_func", (DL_FUNC) &_simmer_Clone__new_func, 2},
Expand Down
5 changes: 5 additions & 0 deletions src/activity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,11 @@ SEXP Leave__new(double prob) {
return XPtr<Leave<double> >(new Leave<double>(prob));
}

//[[Rcpp::export]]
SEXP HandleUnfinished__new(const std::vector<Environment>& trj) {
return XPtr<HandleUnfinished>(new HandleUnfinished(trj));
}

//[[Rcpp::export]]
SEXP Leave__new_func(const Function& prob) {
return XPtr<Leave<Function> >(new Leave<Function>(prob));
Expand Down

0 comments on commit bdca4a2

Please sign in to comment.