Skip to content

Commit

Permalink
Merge pull request #8 from aMarcireau/event
Browse files Browse the repository at this point in the history
Event
  • Loading branch information
XavierJp committed Oct 13, 2014
2 parents b1c00cf + 7d87b81 commit 814ad27
Show file tree
Hide file tree
Showing 11 changed files with 228 additions and 76 deletions.
47 changes: 47 additions & 0 deletions alobe/actor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include "actor.h"

/**
* Constructor
*/
Actor::Actor():
events(vector<shared_ptr<Event> >())
{
}

/**
* Attach an event
*/
void Actor::attach(shared_ptr<Event> event)
{
events.push_back(event);
}

/**
* Detach an event
*/
void Actor::detach(Event & event)
{
for (
vector<shared_ptr<Event> >::iterator eventIterator = events.begin();
eventIterator != events.end();
) {
if ((*eventIterator).get() == &event) {
events.erase(eventIterator);
} else {
++eventIterator;
}
}
}

/**
* Notification called by a Stepper object
*/
void Actor::notify(unsigned int step)
{
for (
vector<shared_ptr<Event> >::iterator eventIterator = events.begin();
eventIterator != events.end();
) {
(*eventIterator)->action(*this, step);
}
}
27 changes: 27 additions & 0 deletions alobe/actor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#ifndef __ALOBE__ACTOR__
#define __ALOBE__ACTOR__

#include <vector>

#include "event.h"

using namespace std;

/**
* <<Abstract>> Actor
*
* Actor in the simulation
*/
class Actor
{
public:
Actor();
void attach(shared_ptr<Event> event); // Attach an event
void detach(Event & event); // Detach an event
void notify(unsigned int step); // Notification called by a Stepper object

private:
vector<shared_ptr<Event> > events;
};

#endif // __ALOBE__ACTOR__
20 changes: 5 additions & 15 deletions alobe/event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,16 @@
/**
* Constructor
*/
Event::Event(unsigned int step):
triggerStep(step)
Event::Event(unsigned int step)
{
}

/**
* Notification called by the Stepper
* Trigger event action on an actor with a given step
*/
void Event::notify(unsigned int step)
void Event::action(Actor & actor, unsigned int step)
{
if (step == this->getTriggerStep())
{
this->action();
if (step == triggerStep) {
this->filteredAction(actor);
}
}

/**
* Getter for triggerStep
*/
unsigned int Event::getTriggerStep()
{
return triggerStep;
}
28 changes: 16 additions & 12 deletions alobe/event.h
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
#ifndef __alobe__event__
#define __alobe__event__
#ifndef __ALOBE__EVENT__
#define __ALOBE__EVENT__

class Actor;

//using namespace std;

/**
* <<Abstract>> Event
*
* An event is trigger by the Stepper it is attached to, through its notify method.
* The action() method will be called if the passed step value is equal to triggerStep.
* An event is trigger by an actor, which passes itself and the current step to the event
*/
class Event
{
public:
Event(unsigned int step = 0); // Constructor
void notify(unsigned int step); // Notification called by the Stepper
unsigned int getTriggerStep(); // Getter for triggerStep
Event(unsigned int step = 0);
void action(Actor & actor, unsigned int step); // Trigger event action on an actor with a given step

protected:
virtual void action() = 0; // Action called by notify()

private:
unsigned int triggerStep; // Store the trigger step value
virtual void filteredAction(Actor & actor) = 0; // Filtered action is called only if step == triggerStep
// This method is protected so that it can be called
// from abstract inherited classes
protected:
unsigned int triggerStep; // This property is protected so that it can be called
// from abstract inherited classes
};

#endif /* defined(__alobe__event__) */
#endif // __ALOBE__EVENT__
9 changes: 9 additions & 0 deletions alobe/main.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
#include "simulation.h"
#include "stepper.h"
#include "actor.h"

using namespace std;

int main() {
unique_ptr<Simulation> simulation = make_unique<Simulation>(make_unique<Stepper>());

simulation->toStep(10);

return 0;
}
17 changes: 4 additions & 13 deletions alobe/periodicEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,11 @@ PeriodicEvent::PeriodicEvent(unsigned int step, unsigned int period):
}

/**
* Notification called by the Stepper
* Change event filter rules (add periodicity)
*/
void PeriodicEvent::notify(unsigned int step)
void PeriodicEvent::action(Actor & actor, unsigned int step)
{
if (step >= this->getTriggerStep() and (step - this->getTriggerStep()) % this->getTriggerPeriod() == 0)
{
this->action();
if (step >= triggerStep & (step - triggerStep) % triggerPeriod == 0) {
this->filteredAction(actor);
}
}

/**
* Getter for triggerPeriod
*/
unsigned int PeriodicEvent::getTriggerPeriod()
{
return triggerPeriod;
}
17 changes: 9 additions & 8 deletions alobe/periodicEvent.h
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
#ifndef __alobe__periodicEvent__
#define __alobe__periodicEvent__
#ifndef __ALOBE__PERIODIC_EVENT__
#define __ALOBE__PERIODIC_EVENT__

#include "event.h"

//using namespace std;

/**
* <<Abstract>> PeriodicEvent
* Inherits Event
*
* Starting at triggerStep, PeriodicEvent objects call their action method every triggerPeriod
* Starting at triggerStep, PeriodicEvent objects call their filteredAction method every triggerPeriod
*/
class PeriodicEvent: public Event
{
public:
PeriodicEvent(unsigned int step = 0, unsigned int period = 1); // Constructor
void notify(unsigned int step); // Notification called by the Stepper
unsigned int getTriggerPeriod(); // Getter for triggerPeriod
PeriodicEvent(unsigned int step = 0, unsigned int period = 1);
void action(Actor & actor, unsigned int step); // Change event filter rules (add periodicity)

private:
unsigned int triggerPeriod; // Store the trigger step value
unsigned int triggerPeriod;
};

#endif /* defined(__alobe__periodicEvent__) */
#endif // __ALOBE__PERIODIC_EVENT__
45 changes: 45 additions & 0 deletions alobe/simulation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include "simulation.h"

/**
* Constructor
*/
Simulation::Simulation(unique_ptr<Stepper> stepper):
_stepper(move(stepper)),
_actors(vector<shared_ptr<Actor> >())
{
}

/**
* Add an actor
*/
void Simulation::add(shared_ptr<Actor> actor)
{
_actors.push_back(actor);
}

/**
* Calculate simulation steps until reaching 'step'
*/
void Simulation::toStep(unsigned int step)
{
while (_stepper->getStep() < step) {
nextStep();
}
}

/**
* Calculate the simulation next step
*/
void Simulation::nextStep()
{
_stepper->increment();
nextStepCallback();
}

/**
* Called at the end of the nextStep method
*/
void Simulation::nextStepCallback()
{
std::cout << "Step: "<< _stepper->getStep() << std::endl;
}
32 changes: 32 additions & 0 deletions alobe/simulation.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#ifndef __ALOBE__SIMULATION__
#define __ALOBE__SIMULATION__

#include <iostream>

#include "stepper.h"
#include "actor.h"

using namespace std;

/**
* Simulation
*
* Controller for the simulation
*/
class Simulation
{
public:
Simulation(unique_ptr<Stepper> stepper);
void add(shared_ptr<Actor> actor); // Add an actor
void toStep(unsigned int step); // Calculate simulation steps until reaching 'step'
void nextStep(); // Calculate the simulation next step

private:
void nextStepCallback(); // Called at the end of the nextStep method

private:
unique_ptr<Stepper> _stepper;
vector<shared_ptr<Actor> > _actors;
};

#endif // __ALOBE__SIMULATION__
33 changes: 20 additions & 13 deletions alobe/stepper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
* Constructor
*/
Stepper::Stepper():
step(0),
eventsByWeight(multimap<int, Event *>())
_step(0),
_actors(vector<Actor *>())
{
}

Expand All @@ -14,15 +14,14 @@ Stepper::Stepper():
*/
void Stepper::increment()
{
step += 1;
_step += 1;

for (
multimap<int, Event *>::iterator weightAndEventIterator = eventsByWeight.begin();
weightAndEventIterator != eventsByWeight.end();
++weightAndEventIterator
)
{
weightAndEventIterator->second->notify(this->getStep());
vector<Actor *>::iterator actorIterator = _actors.begin();
actorIterator != _actors.end();
++actorIterator
) {
(*actorIterator)->notify(_step);
}
}

Expand All @@ -31,21 +30,29 @@ void Stepper::increment()
*/
void Stepper::reset()
{
step = 0;
_step = 0;
}

/**
* Get the current step
*/
unsigned int Stepper::getStep()
{
return step;
return _step;
}

/**
* Attach an event
*/
void Stepper::attach(Event & event, int weight)
void Stepper::attach(Actor & actor)
{
eventsByWeight.insert(pair<int, Event *>(weight, &event));
_actors.push_back(&actor);
}

/**
* Detach an event
*/
void Stepper::detach(Actor & actor)
{
_actors.erase(remove(_actors.begin(), _actors.end(), &actor), _actors.end());
}
Loading

0 comments on commit 814ad27

Please sign in to comment.