Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/dev' into v_1_1_x
Browse files Browse the repository at this point in the history
  • Loading branch information
Eduardo committed Sep 12, 2017
2 parents 6c40c07 + 32f9416 commit 10d2435
Show file tree
Hide file tree
Showing 7 changed files with 194 additions and 26 deletions.
3 changes: 2 additions & 1 deletion Doc/Specification/Specification.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# ***PTN Engine* Specification**

This document specifies the *PTN Engine* library version 1.0.0 .
This document specifies the *PTN Engine* library version 1.1.0 .

**Responsibility for the usage or integration of the *PTN Engine* belongs to its
users alone.**
Expand Down Expand Up @@ -78,6 +78,7 @@ C++11 specific:
- range-based for loops
- using (replacing typedef)
- brace-init
- tuple

The *PTN Engine* relies on Google Test (https://github.com/google/googletest)
for unit testing.
Expand Down
38 changes: 31 additions & 7 deletions PTN_Engine/PTN_Engine/PTN_Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

namespace ptne
{
using namespace std;

PTN_Engine::~PTN_Engine()
{}

Expand All @@ -31,19 +33,41 @@ namespace ptne
}

void PTN_Engine::createTransition(
const std::vector<std::string>& activationPlaces,
const std::vector<std::string>& destinationPlaces,
const std::vector<ConditionFunctorPtr>& additionalConditions)
const vector<string>& activationPlaces,
const vector<size_t>& activationWeights,
const vector<string>& destinationPlaces,
const vector<size_t>& destinationWeights,
const vector<ConditionFunctorPtr>& additionalConditions,
const vector<string>& inhibitorPlaces)
{
m_implementation->createTransition(
activationPlaces,
activationWeights,
destinationPlaces,
destinationWeights,
additionalConditions,
inhibitorPlaces);
}

void PTN_Engine::createTransition(
const vector<string>& activationPlaces,
const vector<string>& destinationPlaces,
const vector<ConditionFunctorPtr>& additionalConditions,
const vector<string>& inhibitorPlaces)
{
m_implementation->createTransition(activationPlaces, destinationPlaces, additionalConditions);
m_implementation->createTransition(
activationPlaces,
destinationPlaces,
additionalConditions,
inhibitorPlaces);
}

void PTN_Engine::execute()
{
m_implementation->execute();
}

void PTN_Engine::addPlace(const std::string& name,
void PTN_Engine::addPlace(const string& name,
const size_t initialNumberOfTokens,
ActionFunctorPtr onEnterAction,
ActionFunctorPtr onExitAction,
Expand All @@ -56,12 +80,12 @@ namespace ptne
input);
}

size_t PTN_Engine::getNumberOfTokens(const std::string& place) const
size_t PTN_Engine::getNumberOfTokens(const string& place) const
{
return m_implementation->getNumberOfTokens(place);
}

void PTN_Engine::incrementInputPlace(const std::string& place)
void PTN_Engine::incrementInputPlace(const string& place)
{
m_implementation->incrementInputPlace(place);
}
Expand Down
60 changes: 57 additions & 3 deletions PTN_Engine/PTN_Engine/PTN_EngineImp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,35 @@ namespace ptne

void PTN_EngineImp::createTransition(
const vector<string>& activationPlaces,
const vector<size_t>& activationWeights,
const vector<string>& destinationPlaces,
const vector<ConditionFunctorPtr>& additionalConditions)
const vector<size_t>& destinationWeights,
const vector<ConditionFunctorPtr>& additionalConditions,
const vector<string>& inhibitorPlaces)
{
vector<WeakPtrPlace> activationPlacesVector =
getPlacesFromNames(activationPlaces);

vector<WeakPtrPlace> destinationPlacesVector =
getPlacesFromNames(destinationPlaces);

vector<WeakPtrPlace> inhibitorPlacesVector =
getPlacesFromNames(inhibitorPlaces);

m_transitions.push_back(
Transition(activationPlacesVector,
activationWeights,
destinationPlacesVector,
destinationWeights,
additionalConditions,
inhibitorPlacesVector));
}

void PTN_EngineImp::createTransition(
const vector<string>& activationPlaces,
const vector<string>& destinationPlaces,
const vector<ConditionFunctorPtr>& additionalConditions,
const vector<std::string>& inhibitorPlaces)
{
vector<WeakPtrPlace> activationPlacesVector;
for(const auto& activationPlace : activationPlaces)
Expand All @@ -58,8 +85,21 @@ namespace ptne
destinationPlacesVector.push_back(m_places.at(destinationPlace));
}

vector<WeakPtrPlace> inhibitorPlacesVector;
for(const auto& inhibitorPlace : inhibitorPlaces)
{
if(m_places.find(inhibitorPlace) == m_places.end())
{
throw PTN_Exception("Invalid name: " + inhibitorPlace);
}
inhibitorPlacesVector.push_back(m_places.at(inhibitorPlace));
}

m_transitions.push_back(
Transition(activationPlacesVector, destinationPlacesVector, additionalConditions));
Transition(activationPlacesVector,
destinationPlacesVector,
additionalConditions,
inhibitorPlacesVector));
}

void PTN_EngineImp::execute()
Expand Down Expand Up @@ -128,7 +168,7 @@ namespace ptne

void PTN_EngineImp::clearInputPlaces()
{
for( WeakPtrPlace& place : m_inputPlaces)
for( const WeakPtrPlace& place : m_inputPlaces)
{
if(SharedPtrPlace spPlace = place.lock())
{
Expand Down Expand Up @@ -163,4 +203,18 @@ namespace ptne
m_places.at(place)->increaseNumberOfTokens(1);
}

vector<WeakPtrPlace> PTN_EngineImp::getPlacesFromNames(const vector<string>& placesNames) const
{
vector<WeakPtrPlace> placesVector;
for(const auto& placeName : placesNames)
{
if(m_places.find(placeName) == m_places.end())
{
throw PTN_Exception("Invalid name: " + placeName);
}
placesVector.push_back(m_places.at(placeName));
}
return placesVector;
}

}
31 changes: 28 additions & 3 deletions PTN_Engine/PTN_Engine/PTN_EngineImp.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,32 @@ namespace ptne
/*!
* Create a new transition
* \param activationPlaces A vector with the names of the activation places.
* \param activationWeights A vector with the weights of each activation place.
* \param destinationPlaces A vector with the names of the destination places.
* \param destinationWeights A vector with the weights of each destination place.
* \param additionalConditions A vector with functors that return bool.
* \param inhibitorPlaces Places that cannot have tokens to fire the transition.
*/
void createTransition(
const std::vector<std::string>& activationPlaces,
const std::vector<std::string>& destinationPlaces,
const std::vector<ConditionFunctorPtr>& additionalConditions);
const std::vector<std::string>& activationPlaces,
const std::vector<size_t>& activationWeights,
const std::vector<std::string>& destinationPlaces,
const std::vector<size_t>& destinationWeights,
const std::vector<ConditionFunctorPtr>& additionalConditions,
const std::vector<std::string>& inhibitorPlaces);

/*!
* Create a new transition
* \param activationPlaces A vector with the names of the activation places.
* \param destinationPlaces A vector with the names of the destination places.
* \param additionalConditions A vector with functors that return bool.
* \param inhibitorPlaces Places that cannot have tokens to fire the transition.
*/
void createTransition(
const std::vector<std::string>& activationPlaces,
const std::vector<std::string>& destinationPlaces,
const std::vector<ConditionFunctorPtr>& additionalConditions,
const std::vector<std::string>& inhibitorPlaces);

//! Run until it no more transitions can be fired or stop is flagged.
void execute();
Expand Down Expand Up @@ -124,6 +143,12 @@ namespace ptne
*/
std::map<std::string, SharedPtrPlace> m_places;

//! Translates a vector of names of places to a vector of weak pointers to those places.
/*!
*
*/
std::vector<WeakPtrPlace> getPlacesFromNames(const std::vector<std::string>&) const;

//! Flag to stop the execution of the net.
/*!
* For future use in multi-threaded operation.
Expand Down
49 changes: 41 additions & 8 deletions PTN_Engine/PTN_Engine/Transition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,18 @@
#include "PTN_Engine/PTN_Engine/Transition.h"
#include "PTN_Engine/Place.h"
#include "PTN_Engine/IConditionFunctor.h"
#include "PTN_Engine/PTN_Exception.h"

namespace ptne
{
using namespace std;

Transition::Transition(const vector<WeakPtrPlace>& activationPlaces,
const vector<WeakPtrPlace>& destinationPlaces,
const vector<ConditionFunctorPtr>& additionalActivationConditions):
m_additionalActivationConditions{additionalActivationConditions}
const vector<ConditionFunctorPtr>& additionalActivationConditions,
const vector<WeakPtrPlace>& inhibitorPlaces):
m_additionalActivationConditions{additionalActivationConditions},
m_inhibitorPlaces(inhibitorPlaces)
{
for(size_t i = 0; i < activationPlaces.size(); ++i)
{
Expand All @@ -45,24 +48,26 @@ namespace ptne
const vector<size_t>& activationWeights,
const vector<WeakPtrPlace>& destinationPlaces,
const vector<size_t>& destinationWeights,
const vector<ConditionFunctorPtr>& additionalActivationConditions):
m_additionalActivationConditions{additionalActivationConditions}
const vector<ConditionFunctorPtr>& additionalActivationConditions,
const vector<WeakPtrPlace>& inhibitorPlaces):
m_additionalActivationConditions{additionalActivationConditions},
m_inhibitorPlaces(inhibitorPlaces)
{
if(activationPlaces.size() != activationWeights.size())
{
throw runtime_error("The number of activation weights must be the same as the number of activation places.");
throw PTN_Exception("The number of activation weights must be the same as the number of activation places.");
}

if(destinationPlaces.size() != destinationWeights.size())
{
throw runtime_error("The number of destination weights must be the same as the number of destination places.");
throw PTN_Exception("The number of destination weights must be the same as the number of destination places.");
}

for(size_t i = 0; i < activationPlaces.size(); ++i)
{
if(activationWeights[i] == 0)
{
throw runtime_error("Weights cannot be 0.");
throw PTN_Exception("Weights cannot be 0.");
}
m_activationPlaces.push_back(tuple<WeakPtrPlace, size_t>(activationPlaces[i], activationWeights[i]));
}
Expand All @@ -71,7 +76,7 @@ namespace ptne
{
if(destinationWeights[i] == 0)
{
throw runtime_error("Weights cannot be 0.");
throw PTN_Exception("Weights cannot be 0.");
}
m_destinationPlaces.push_back(tuple<WeakPtrPlace, size_t>(destinationPlaces[i], destinationWeights[i]));
}
Expand All @@ -91,6 +96,11 @@ namespace ptne

bool Transition::isActive() const
{
if(!checkInhibitorPlaces())
{
return false;
}

if(!checkActivationPlaces())
{
return false;
Expand All @@ -104,6 +114,25 @@ namespace ptne
return true;
}

bool Transition::checkInhibitorPlaces() const
{
for(const WeakPtrPlace& inhibitorPlace: m_inhibitorPlaces)
{
if(SharedPtrPlace spInhibitorPlace = inhibitorPlace.lock())
{
if(spInhibitorPlace->getNumberOfTokens() > 0)
{
return false;
}
}
else
{
throw PTN_Exception("Could not obtain pointer to place.");
}
}
return true;
}

bool Transition::checkActivationPlaces() const
{
for( const tuple<WeakPtrPlace, size_t>& tupleActivationPlace : m_activationPlaces)
Expand All @@ -118,6 +147,10 @@ namespace ptne
return false;
}
}
else
{
throw PTN_Exception("Could not obtain pointer to place.");
}
}
return true;
}
Expand Down
18 changes: 15 additions & 3 deletions PTN_Engine/PTN_Engine/Transition.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,13 @@ namespace ptne
* \param activationPlaces Collection of incoming places.
* \param destinationPlaces Collection of outgoing places.
* \param additionalActivationConditions Boolean function from the controller that can block firing.
* \param inhibitorPlaces Places that must be empty for the transition to fire.
*/
Transition(
const std::vector<WeakPtrPlace>& activationPlaces,
const std::vector<WeakPtrPlace>& destinationPlaces,
const std::vector<ConditionFunctorPtr>& additionalActivationConditions);
const std::vector<ConditionFunctorPtr>& additionalActivationConditions,
const std::vector<WeakPtrPlace>& inhibitorPlaces = {});

//! Constructor.
/*!
Expand All @@ -64,13 +66,15 @@ namespace ptne
* \param destinationPlaces Collection of outgoing places.
* \param destinationWeights Weights of each destination place.
* \param additionalActivationConditions Boolean function from the controller that can block firing.
* \param inhibitorPlaces Places that must be empty for the transition to fire.
*/
Transition(
const std::vector<WeakPtrPlace>& activationPlaces,
const std::vector<size_t>& activationWeights,
const std::vector<WeakPtrPlace>& destinationPlaces,
const std::vector<size_t>& destinationWeights,
const std::vector<ConditionFunctorPtr>& additionalActivationConditions);
const std::vector<ConditionFunctorPtr>& additionalActivationConditions,
const std::vector<WeakPtrPlace>& inhibitorPlaces = {});

/*!
* Evaluate the activation places and transit the tokens if possible.
Expand All @@ -86,6 +90,12 @@ namespace ptne

private:

/*!
* Checks if all inhibitor places are empty.
* \return True if yes, false if not.
*/
bool checkInhibitorPlaces() const;

/*!
* Checks if all activation places have enough tokens.
* \return True if yes, false if not.
Expand All @@ -107,7 +117,6 @@ namespace ptne
//! Inserts tokens in the destination places.
void enterDestinationPlaces();


//! Pointers to the activations places from the net.
std::vector<std::tuple<WeakPtrPlace,size_t>> m_activationPlaces;

Expand All @@ -117,6 +126,9 @@ namespace ptne
//! Pointers to the controller's functions that evaluate if the transition can be fired.
std::vector<ConditionFunctorPtr> m_additionalActivationConditions;

//! Inhibitor arcs
std::vector<WeakPtrPlace> m_inhibitorPlaces;

};
}

Expand Down
Loading

0 comments on commit 10d2435

Please sign in to comment.