Skip to content

Commit

Permalink
Issue #42 - Cleanup code
Browse files Browse the repository at this point in the history
  • Loading branch information
vldtecno committed Mar 24, 2024
1 parent 864bb03 commit 08ef7d7
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 126 deletions.
2 changes: 1 addition & 1 deletion Examples/Elevator/Controller/ElevatorPetriNet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void ElevatorPetriNet::destinationButton()
incrementInput("DestinationButton");
}

void ElevatorPetriNet::incrementInput(const string &inputPlace, const bool debug)
void ElevatorPetriNet::incrementInput(const string &inputPlace, const bool)
{
incrementInputPlace(inputPlace);
}
Expand Down
1 change: 0 additions & 1 deletion Examples/PhoneMenu/Controller/Controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ void Controller::setStateMachine(shared_ptr<IMenuStateMachine> stateMachine)
m_menuStateMachine = stateMachine;
}


bool Controller::justReturnTrue() const
{
return true;
Expand Down
29 changes: 10 additions & 19 deletions PTN_Engine/ImportExport/XML/src/XML/XML_FileImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,10 @@ namespace ptne
{
void XML_FileImporter::_import(const string &filePath, PTN_Engine &ptnEngine)
{
xml_parse_result result = m_document.load_file(filePath.c_str());

if (!result)
if (const auto &result = m_document.load_file(filePath.c_str()); !result)
{
throw PTN_Exception(result.description());
}

IFileImporter::_import(ptnEngine);
}

Expand All @@ -132,15 +129,13 @@ vector<PlaceProperties> XML_FileImporter::importPlaces() const
{
PlaceProperties placeProperties;
bool isInput = false;
const string isInputStr = getAttributeValue(place, "input");
if (!isInputStr.empty())
if (const string isInputStr = getAttributeValue(place, "input"); !isInputStr.empty())
{
isInput = getAttributeValue(place, "input") == "true" ? true : false;
isInput = isInputStr == "true" ? true : false;
}

size_t numberOfTokens = 0;
const string numberOfTokensStr = getAttributeValue(place, "tokens");
if (!numberOfTokensStr.empty())
size_t numberOfTokens = 0;
if (const string numberOfTokensStr = getAttributeValue(place, "tokens"); !numberOfTokensStr.empty())
{
numberOfTokens = static_cast<size_t>(atol(numberOfTokensStr.c_str()));
}
Expand All @@ -158,19 +153,17 @@ vector<PlaceProperties> XML_FileImporter::importPlaces() const

vector<TransitionProperties> XML_FileImporter::importTransitions() const
{
using enum ArcProperties::Type;
vector<TransitionProperties> transitionInfoCollection;
for (xml_node transition : m_document.child("PTN-Engine").child("Transitions"))
{
TransitionProperties transitionProperties;

const auto activationConditions = collectTransitionAttributes(transition, "ActivationConditions");
transitionProperties.name = getNodeValue<string>("Name", transition);
transitionProperties.activationArcs = collectArcAttributes(transition, "ActivationPlaces",
ArcProperties::Type::ACTIVATION);
transitionProperties.destinationArcs = collectArcAttributes(transition, "DestinationPlaces",
ArcProperties::Type::DESTINATION);
transitionProperties.inhibitorArcs = collectArcAttributes(transition, "InhibitorPlaces",
ArcProperties::Type::INHIBITOR);;
transitionProperties.activationArcs = collectArcAttributes(transition, "ActivationPlaces", ACTIVATION);
transitionProperties.destinationArcs = collectArcAttributes(transition, "DestinationPlaces", DESTINATION);
transitionProperties.inhibitorArcs = collectArcAttributes(transition, "InhibitorPlaces", INHIBITOR);
transitionProperties.additionalConditionsNames = activationConditions;
transitionProperties.requireNoActionsInExecution = getNodeValue<bool>("RequireNoActionsInExecution", transition);
transitionInfoCollection.emplace_back(transitionProperties);
Expand All @@ -190,8 +183,7 @@ vector<ArcProperties> XML_FileImporter::importArcs() const
arcProperties.weight = getNodeValue<size_t>("Weight", arc);

using enum ArcProperties::Type;
auto typeStr = getNodeValue<string>("Type", arc);
if (typeStr == "Activation")
if (auto typeStr = getNodeValue<string>("Type", arc); typeStr == "Activation")
{
arcProperties.type = ACTIVATION;
}
Expand All @@ -211,7 +203,6 @@ vector<ArcProperties> XML_FileImporter::importArcs() const
{
throw PTN_Exception("Type string not supported");
}

arcInfoCollection.emplace_back(arcProperties);
}
return arcInfoCollection;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ class DLL_PUBLIC IFileImporter
virtual void _import(const std::string &filePath, PTN_Engine &ptnEngine) = 0;

protected:

void _import(PTN_Engine &ptnEngine) const;

private:
Expand Down
26 changes: 10 additions & 16 deletions PTN_Engine/PTN_EngineImp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@
namespace ptne
{
using namespace std;
using enum PTN_Engine::ACTIONS_THREAD_OPTION;

PTN_EngineImp::PTN_EngineImp(PTN_Engine::ACTIONS_THREAD_OPTION actionsThreadOption)
: m_actionsThreadOption(actionsThreadOption)
, m_eventLoop(*this)
{
if (m_actionsThreadOption == PTN_Engine::ACTIONS_THREAD_OPTION::JOB_QUEUE)
if (m_actionsThreadOption == JOB_QUEUE)
{
m_jobQueue = make_unique<JobQueue>();
}
Expand Down Expand Up @@ -70,28 +71,21 @@ void PTN_EngineImp::createTransition(const TransitionProperties &transitionPrope
transitionProperties.requireNoActionsInExecution);
}

void PTN_EngineImp::createPlace(const PlaceProperties &placeProperties)
void PTN_EngineImp::createPlace(PlaceProperties placeProperties)
{
ActionFunction onEnterAction = placeProperties.onEnterAction;
if (!placeProperties.onEnterActionFunctionName.empty())
{
onEnterAction = m_actions.getItem(placeProperties.onEnterActionFunctionName);
placeProperties.onEnterAction = m_actions.getItem(placeProperties.onEnterActionFunctionName);
}

ActionFunction onExitAction = placeProperties.onExitAction;
if (!placeProperties.onExitActionFunctionName.empty())
{
onExitAction = m_actions.getItem(placeProperties.onExitActionFunctionName);
placeProperties.onExitAction = m_actions.getItem(placeProperties.onExitActionFunctionName);
}

auto place = make_shared<Place>(*this,
placeProperties.name,
placeProperties.initialNumberOfTokens,
placeProperties.onEnterActionFunctionName,
onEnterAction,
placeProperties.onExitActionFunctionName,
onExitAction,
placeProperties.input);
auto place = make_shared<Place>(*this, placeProperties);
m_places.insert(place);
}

Expand Down Expand Up @@ -140,11 +134,11 @@ void PTN_EngineImp::setActionsThreadOption(const PTN_Engine::ACTIONS_THREAD_OPTI
{
return;
}
if (actionsThreadOption == PTN_Engine::ACTIONS_THREAD_OPTION::JOB_QUEUE && m_jobQueue == nullptr)
if (actionsThreadOption == JOB_QUEUE && m_jobQueue == nullptr)
{
m_jobQueue = make_unique<JobQueue>();
}
else if (actionsThreadOption != PTN_Engine::ACTIONS_THREAD_OPTION::JOB_QUEUE && m_jobQueue != nullptr)
else if (actionsThreadOption != JOB_QUEUE && m_jobQueue != nullptr)
{
m_jobQueue.reset();
}
Expand Down Expand Up @@ -212,7 +206,7 @@ PTN_Engine::EventLoopSleepDuration PTN_EngineImp::getEventLoopSleepDuration() co
return m_eventLoop.getSleepDuration();
}

void PTN_EngineImp::addArc(const ArcProperties &arcProperties)
void PTN_EngineImp::addArc(const ArcProperties &arcProperties) const
{
if (isEventLoopRunning())
{
Expand Down Expand Up @@ -317,7 +311,7 @@ PTN_EngineImp::createAnonymousConditions(const vector<ConditionFunction> &condit

void PTN_EngineImp::addJob(const ActionFunction &actionFunction)
{
if (m_actionsThreadOption != PTN_Engine::ACTIONS_THREAD_OPTION::JOB_QUEUE || m_jobQueue == nullptr ||
if (m_actionsThreadOption != JOB_QUEUE || m_jobQueue == nullptr ||
!m_jobQueue->isActive())
{
throw PTN_Exception("addJob incorrectly called");
Expand Down
6 changes: 3 additions & 3 deletions PTN_Engine/PTN_EngineImp.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class PTN_EngineImp final : public IPTN_EngineEL, public IPTN_EnginePlace
public:
explicit PTN_EngineImp(PTN_Engine::ACTIONS_THREAD_OPTION actionsThreadOption);

~PTN_EngineImp();
~PTN_EngineImp() override;
PTN_EngineImp(const PTN_EngineImp &) = delete;
PTN_EngineImp(PTN_EngineImp &&) = delete;
PTN_EngineImp &operator=(const PTN_EngineImp &) = delete;
Expand All @@ -80,7 +80,7 @@ class PTN_EngineImp final : public IPTN_EngineEL, public IPTN_EnginePlace
//! \brief createPlace
//! \param placeProperties
//!
void createPlace(const PlaceProperties &placeProperties);
void createPlace(PlaceProperties placeProperties);

//!
//! \brief isEventLoopRunning
Expand Down Expand Up @@ -190,7 +190,7 @@ class PTN_EngineImp final : public IPTN_EngineEL, public IPTN_EnginePlace
//! \brief addArc
//! \param arcProperties
//!
void addArc(const ArcProperties &arcProperties);
void addArc(const ArcProperties &arcProperties) const;

//!
//! \brief addArc
Expand Down
40 changes: 9 additions & 31 deletions PTN_Engine/Place.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,38 +27,16 @@ namespace ptne
{
using namespace std;

Place::Place(IPTN_EnginePlace &parent,
const string &name,
const size_t initialNumberOfTokens,
const ActionFunction &onEnterAction,
const ActionFunction &onExitAction,
const bool input)
Place::Place(IPTN_EnginePlace &parent, const PlaceProperties &placeProperties)
: m_ptnEngine(parent)
, m_name(name)
, m_onEnterAction(onEnterAction)
, m_onExitAction(onExitAction)
, m_numberOfTokens(initialNumberOfTokens)
, m_isInputPlace(input)
{
}

Place::Place(IPTN_EnginePlace &parent,
const string &name,
const size_t initialNumberOfTokens,
const string &onEnterActionName,
const ActionFunction &onEnterAction,
const string &onExitActionName,
const ActionFunction &onExitAction,
const bool input)
: m_ptnEngine(parent)
, m_name(name)
, m_onEnterActionName(onEnterActionName)
, m_onEnterAction(onEnterAction)
, m_onExitActionName(onExitActionName)
, m_onExitAction(onExitAction)
, m_numberOfTokens(initialNumberOfTokens)
, m_isInputPlace(input)
{
, m_name(placeProperties.name)
, m_onEnterActionName(placeProperties.onEnterActionFunctionName)
, m_onEnterAction(placeProperties.onEnterAction)
, m_onExitActionName(placeProperties.onExitActionFunctionName)
, m_onExitAction(placeProperties.onExitAction)
, m_numberOfTokens(placeProperties.initialNumberOfTokens)
, m_isInputPlace(placeProperties.input)
{
}

string Place::getName() const
Expand Down
33 changes: 1 addition & 32 deletions PTN_Engine/Place.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,39 +35,8 @@ class Place final
public:
using ActionFunction = std::function<void(void)>;

//!
//! Place constructor.
//! \param initialNumberOfTokens The number of tokens the place contains originally.
//! \param onEnterAction The action to be performed when a token enters the place.
//! \param onExitAction The action to be performed when a token leaves the place.
//! \param input Flag that marks the place as an input place.
//!
Place(IPTN_EnginePlace &parent,
const std::string &name,
const size_t initialNumberOfTokens,
const ActionFunction &onEnterAction,
const ActionFunction &onExitAction,
const bool input = false);

//!
//! \brief Place
//! \param parent
//! \param name
//! \param initialNumberOfTokens
//! \param onEnterActionName
//! \param onEnterAction
//! \param onExitActionName
//! \param onExitAction
//! \param input
//!
Place(IPTN_EnginePlace &parent,
const std::string &name,
const size_t initialNumberOfTokens,
const std::string &onEnterActionName,
const ActionFunction &onEnterAction,
const std::string &onExitActionName,
const ActionFunction &onExitAction,
const bool input = false);
const PlaceProperties &placeProperties);

~Place() = default;
Place(const Place &) = delete;
Expand Down
28 changes: 15 additions & 13 deletions PTN_Engine/Transition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,10 @@ array<vector<ArcProperties>,3> Transition::getArcsProperties() const
return arcProperties;
};

arcsProperties[0] = arcPropertiesFromArcs(m_activationArcs, ArcProperties::Type::ACTIVATION);
arcsProperties[1] = arcPropertiesFromArcs(m_destinationArcs, ArcProperties::Type::DESTINATION);
arcsProperties[2] = arcPropertiesFromArcs(m_inhibitorArcs, ArcProperties::Type::INHIBITOR);
using enum ArcProperties::Type;
arcsProperties[0] = arcPropertiesFromArcs(m_activationArcs, ACTIVATION);
arcsProperties[1] = arcPropertiesFromArcs(m_destinationArcs, DESTINATION);
arcsProperties[2] = arcPropertiesFromArcs(m_inhibitorArcs, INHIBITOR);

return arcsProperties;
}
Expand All @@ -236,29 +237,30 @@ void Transition::addPlace(const shared_ptr<Place> &place, const ArcProperties::T
}
};

using enum ArcProperties::Type;
switch (type)
{
default:
{
throw PTN_Exception("Unexpected type");
}
case ArcProperties::Type::ACTIVATION:
case ACTIVATION:
{
addArcTo(m_activationArcs);
break;
}
case ArcProperties::Type::BIDIRECTIONAL:
case BIDIRECTIONAL:
{
addArcTo(m_activationArcs);
addArcTo(m_destinationArcs);
break;
}
case ArcProperties::Type::DESTINATION:
case DESTINATION:
{
addArcTo(m_destinationArcs);
break;
}
case ArcProperties::Type::INHIBITOR:
case INHIBITOR:
{
addArcTo(m_inhibitorArcs);
break;
Expand All @@ -282,29 +284,30 @@ void Transition::removePlace(const shared_ptr<Place> &place, const ArcProperties
}
};

using enum ArcProperties::Type;
switch (type)
{
default:
{
throw PTN_Exception("Unexpected type");
}
case ArcProperties::Type::ACTIVATION:
case ACTIVATION:
{
removePlaceFrom(m_activationArcs);
break;
}
case ArcProperties::Type::BIDIRECTIONAL:
case BIDIRECTIONAL:
{
removePlaceFrom(m_activationArcs);
removePlaceFrom(m_destinationArcs);
break;
}
case ArcProperties::Type::DESTINATION:
case DESTINATION:
{
removePlaceFrom(m_destinationArcs);
break;
}
case ArcProperties::Type::INHIBITOR:
case INHIBITOR:
{
removePlaceFrom(m_inhibitorArcs);
break;
Expand Down Expand Up @@ -339,7 +342,7 @@ bool Transition::checkAdditionalConditions() const
}
else
{
if (!(activationCondition)())
if (!activationCondition())
{
return false;
}
Expand All @@ -355,7 +358,6 @@ bool Transition::noActionsInExecution() const
const WeakPtrPlace &activationPlace = activationArc.place;

SharedPtrPlace spPlace = lockWeakPtr(activationPlace);

if (spPlace->isOnEnterActionInExecution())
{
return false;
Expand Down
Loading

0 comments on commit 08ef7d7

Please sign in to comment.