Skip to content

Commit

Permalink
Canceling a ScheduledActions instance now cancels the associated Sche…
Browse files Browse the repository at this point in the history
…duleTask immediately (previously, cancellation was deferred until the next execution pass)
  • Loading branch information
cstawarz committed Feb 25, 2015
1 parent 56a0409 commit 9620310
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 16 deletions.
58 changes: 45 additions & 13 deletions core/Core/ParadigmComponents/ScheduledActions.cpp
Expand Up @@ -81,10 +81,6 @@ unsigned int ScheduledActions::getNRepeated() const {
return nRepeated;
}

bool ScheduledActions::shouldCancel() const {
return bool(*cancel);
}

void ScheduledActions::executeActions() {
for(int i = 0; i < action_list.getNElements(); i++){
action_list[i]->announceEntry();
Expand All @@ -98,14 +94,6 @@ void ScheduledActions::executeActions() {
****************************************************************/

void *scheduled_action_runner(const shared_ptr<ScheduledActions> &sa){
if (sa->shouldCancel()) {
shared_ptr<ScheduleTask> node(sa->getNode());
if (node) {
node->cancel();
return NULL;
}
}

shared_ptr <Clock> clock = Clock::instance();
MWTime delta = clock->getCurrentTimeUS() - ((sa->getTimeScheduled() + sa->getDelay()) + sa->getNRepeated()*sa->getInterval());

Expand All @@ -120,6 +108,17 @@ void ScheduledActions::executeActions() {
}


void ScheduledActions::CancelNotification::notify(const Datum &data, MWTime time) {
boost::shared_ptr<ScheduledActions> sa = saWeak.lock();
if (sa && data.getBool()) {
shared_ptr<ScheduleTask> node(sa->getNode());
if (node) {
node->cancel();
}
}
}


shared_ptr<Component> ScheduledActionsFactory::createObject(std::map<std::string, std::string> parameters,
ComponentRegistry *reg) {
REQUIRE_ATTRIBUTES(parameters, "delay", "repeats", "duration");
Expand All @@ -140,9 +139,42 @@ shared_ptr<Component> ScheduledActionsFactory::createObject(std::map<std::string

// TODO .. needs more work, the actual actions aren't included here

shared_ptr <Component> newScheduledActions = shared_ptr<Component>(new ScheduledActions(repeats, delay, duration, cancel));
auto newScheduledActions = boost::make_shared<ScheduledActions>(repeats, delay, duration, cancel);

if (cancel) {
auto notification = boost::make_shared<ScheduledActions::CancelNotification>(newScheduledActions);
cancel->addNotification(notification);
}

return newScheduledActions;
}


END_NAMESPACE_MW



























13 changes: 12 additions & 1 deletion core/Core/ParadigmComponents/ScheduledActions.h
Expand Up @@ -44,8 +44,19 @@ BEGIN_NAMESPACE_MW
MWTime getInterval() const;
MWTime getTimeScheduled() const;
unsigned int getNRepeated() const;
bool shouldCancel() const;
void executeActions();

class CancelNotification : public VariableNotification {
public:
CancelNotification(const boost::shared_ptr<ScheduledActions> &sa) :
saWeak(sa)
{ }

void notify(const Datum &data, MWTime time) override;

private:
boost::weak_ptr<ScheduledActions> saWeak;
};
};

class ScheduledActionsFactory : public ComponentFactory{
Expand Down
9 changes: 7 additions & 2 deletions examples/Tests/Action/CancelScheduledActions.xml
@@ -1,4 +1,4 @@
<?xml version="1.0"?>
<?xml version="1.0" standalone="no"?>
<monkeyml version="1.0">
<io_devices tag="IO Devices"></io_devices>
<variables tag="Variables">
Expand All @@ -19,6 +19,10 @@
<action type="assignment" tag="Reset pulse_count" variable="pulse_count" value="0"></action>
<action type="assignment" tag="Reset cancel_pulses" variable="cancel_pulses" value="0"></action>
<action type="schedule" tag="Schedule pulses" delay="0" duration="pulse_interval" repeats="1000" cancel="cancel_pulses">
<action type="if" tag="Conditionally Execute Actions" condition="pulse_count &gt;= num_pulses">
<action type="assert" tag="Assert" condition="NO" message="Scheduled actions were not canceled successfully" stop_on_failure="NO"></action>
<action type="assignment" tag="Set cancel_pulses" variable="cancel_pulses" value="1"></action>
</action>
<action type="assignment" tag="Update pulse_count" variable="pulse_count" value="pulse_count + 1"></action>
<action type="report" tag="Report 2" message="Sent pulse $pulse_count"></action>
</action>
Expand All @@ -28,7 +32,8 @@
</task_system_state>
<task_system_state tag="Cancel pulses" interruptible="YES">
<action_marker _unmoveable="1" tag="Actions"></action_marker>
<action type="assignment" tag="Cancel pulses" variable="cancel_pulses" value="1"></action>
<action type="assignment" tag="Set cancel_pulses" variable="cancel_pulses" value="1"></action>
<action type="assignment" tag="Clear cancel_pulses" variable="cancel_pulses" value="0"></action>
<action type="start_timer" tag="Start Timer" timer="MyTimer" timebase="" duration="3" duration_units="s"></action>
<transition_marker _unmoveable="1" tag="Transitions"></transition_marker>
<transition type="timer_expired" tag="If Timer Expired, Transition to ..." target="Exit State System" timer="MyTimer"></transition>
Expand Down

0 comments on commit 9620310

Please sign in to comment.