Skip to content

Commit

Permalink
Merge branch '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 10d2435 + f72a6b1 commit 0e0fe2f
Show file tree
Hide file tree
Showing 7 changed files with 209 additions and 6 deletions.
33 changes: 33 additions & 0 deletions Tests/BlackBoxTests/Fixtures/FixturePetriNet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,36 @@ void FixturePetriNet::testWeightedState(const size_t expectedTokens[s_numberOfWe
}

}


void FixturePetriNet::testInhibitedState(const size_t expectedTokens[s_numberOfInhibitedNetPlaces])
{
if(!m_dispatcher)
{
throw runtime_error("No dispatcher available");
}

size_t tokens[s_numberOfInhibitedNetPlaces];

//TODO
//Dangerous(ugly) cast necessary only for testing. This does not need to exist within a normal use case.
//Nonetheless it would be nice to fix it.
Dispatcher::IDispatcherPetriNet *dispatcherPetriNet = m_dispatcher->m_pPetriNet.get();

if(Dispatcher::InhibitedPetriNet* weightedPetriNet = dynamic_cast<Dispatcher::InhibitedPetriNet*>(dispatcherPetriNet))
{
tokens[0] = weightedPetriNet->getNumberOfTokens("InputWaitPackage");
tokens[1] = weightedPetriNet->getNumberOfTokens("P1");
tokens[2] = weightedPetriNet->getNumberOfTokens("P2");
tokens[3] = weightedPetriNet->getNumberOfTokens("P3");
tokens[4] = weightedPetriNet->getNumberOfTokens("P4");
tokens[5] = weightedPetriNet->getNumberOfTokens("P5");
}

for(size_t i = 0; i < s_numberOfInhibitedNetPlaces; ++i )
{
size_t a = expectedTokens[i];
EXPECT_EQ(a, tokens[i]);
}

}
9 changes: 9 additions & 0 deletions Tests/BlackBoxTests/Fixtures/FixturePetriNet.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ class FixturePetriNet: public ::testing::Test
//! Total number of places in the net.
const static size_t s_numberOfWeightedPlaces = 4;

//! Total number of places in the net.
const static size_t s_numberOfInhibitedNetPlaces = 6;

//! Constructor.
FixturePetriNet();

Expand All @@ -60,6 +63,12 @@ class FixturePetriNet: public ::testing::Test
*/
void testWeightedState(const size_t expectedTokens[s_numberOfWeightedPlaces]);

/*!
* Tests the number of tokens in all places of the net.
* \param expectedTokens Expected number of tokens in each place.
*/
void testInhibitedState(const size_t expectedTokens[s_numberOfInhibitedNetPlaces]);

//! Controller containing the PTN Engine net.
std::shared_ptr<Dispatcher> m_dispatcher;

Expand Down
18 changes: 13 additions & 5 deletions Tests/BlackBoxTests/Mocks/Dispatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,20 +120,28 @@ void Dispatcher::setRoundRobinMode()
{
m_pPetriNet = move(
PtrRoundRobinPetriNet(new RoundRobinPetriNet(shared_from_this()))
);
);
}

void Dispatcher::setFreeChoiceMode()
{
m_pPetriNet = move(
PtrFreeChoicePetriNet(new FreeChoicePetriNet(shared_from_this()))
);
PtrFreeChoicePetriNet(new FreeChoicePetriNet(shared_from_this()))
);
}

void Dispatcher::setWeightedPN()
{
m_pPetriNet = move(
PtrWeightedPetriNet(new WeightedPetriNet(shared_from_this()))
);
PtrWeightedPetriNet(new WeightedPetriNet(shared_from_this()))
);
}

void Dispatcher::setInhibitedPN()
{
m_pPetriNet = move(
PtrInhibitedPetriNet(new InhibitedPetriNet(shared_from_this()))
);
}


9 changes: 8 additions & 1 deletion Tests/BlackBoxTests/Mocks/Dispatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ class Dispatcher: public std::enable_shared_from_this<Dispatcher>
//! Petri net using weighted arcs.
class WeightedPetriNet;

//! Petri net using inhibitor arcs.
class InhibitedPetriNet;

//! For testing purposes only
friend class FixturePetriNet;

Expand Down Expand Up @@ -89,7 +92,7 @@ class Dispatcher: public std::enable_shared_from_this<Dispatcher>
using PtrRoundRobinPetriNet = std::unique_ptr<RoundRobinPetriNet>;
using PtrFreeChoicePetriNet = std::unique_ptr<FreeChoicePetriNet>;
using PtrWeightedPetriNet = std::unique_ptr<WeightedPetriNet>;

using PtrInhibitedPetriNet = std::unique_ptr<InhibitedPetriNet>;

//! Petri net that defines and controls the dispatcher business logic.
std::unique_ptr<IDispatcherPetriNet> m_pPetriNet;
Expand Down Expand Up @@ -143,6 +146,9 @@ class Dispatcher: public std::enable_shared_from_this<Dispatcher>
//! Select weighted Petri net.
void setWeightedPN();

//! Select inhibited Petri net.
void setInhibitedPN();

};

template class ptne::Action<Dispatcher>;
Expand All @@ -154,3 +160,4 @@ using DispatcherFireCondition = ptne::ActivationCondition<Dispatcher>;
#include "Mocks/RoundRobinPetriNet.h"
#include "Mocks/FreeChoicePetriNet.h"
#include "Mocks/WeightedPetriNet.h"
#include "Mocks/InhibitedPetriNet.h"
77 changes: 77 additions & 0 deletions Tests/BlackBoxTests/Mocks/InhibitedPetriNet.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* This file is part of PTN Engine
*
* Copyright (c) 2017 Eduardo Valgôde
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "Mocks/InhibitedPetriNet.h"
#include "PTN_Engine/Place.h"


using namespace ptne;
using namespace std;

Dispatcher::InhibitedPetriNet::InhibitedPetriNet(shared_ptr<Dispatcher> ptrDispatcher):
PTN_Engine{}
{

//Places
addPlace("InputWaitPackage", 0, nullptr, nullptr, true);

addPlace("P1",1, nullptr, nullptr);
addPlace("P2",1, nullptr, nullptr);
addPlace("P3",1, nullptr, nullptr);
addPlace("P4",0, nullptr, nullptr);
addPlace("P5",0, nullptr, nullptr);

//Transitions


createTransition(
{"InputWaitPackage", "P1", "P3"}, //activation
{"P4"}, //destination
{} //additional conditions
);


createTransition(
{"P2"}, //activation
{"P5"}, //destination
{}, //additional conditions
{"P3"} //inhibitor arc
);


createTransition(
{"InputWaitPackage", "P4"}, //activation
{"P1", "P3"}, //destination
{} //additional conditions
);


createTransition(
{"P5"}, //activation
{"P2"}, //destination
{}, //additional conditions
{"P4"} //inhibitor arc
);

}

void Dispatcher::InhibitedPetriNet::dispatch()
{
incrementInputPlace("InputWaitPackage");
execute();
}
47 changes: 47 additions & 0 deletions Tests/BlackBoxTests/Mocks/InhibitedPetriNet.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* This file is part of PTN Engine
*
* Copyright (c) 2017 Eduardo Valgôde
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#include "Mocks/Dispatcher.h"
#include "Mocks/IDispatcherPetriNet.h"
#include "PTN_Engine/PTN_Engine.h"

//! Implements PTN Engine net with a Petri net that uses the inhibitor arc.
/*!
* The behaviour is defined in the constructor.
*/
class Dispatcher::InhibitedPetriNet:
public IDispatcherPetriNet,
private ptne::PTN_Engine
{
//For testing purposes.
friend class FixturePetriNet;

public:

/*!
* Constructor.
* \param ptrDispatcher Shared pointer to the dispatcher.
*/
InhibitedPetriNet(std::shared_ptr<Dispatcher> ptrDispatcher);

//! Trigger the dispatch process.
void dispatch() override;

};
22 changes: 22 additions & 0 deletions Tests/BlackBoxTests/Tests/TestPetriNet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,25 @@ TEST_F(FixturePetriNet, Weights_1)

}

TEST_F(FixturePetriNet, Inhibited_1)
{
if(!m_dispatcher)
{
throw std::runtime_error("No dispatcher available");
}

m_dispatcher->setResetCounter(true);
m_dispatcher->setInhibitedPN();

m_dispatcher->dispatch();

size_t expectedState[6] = {0,0,0,0,1,1};
testInhibitedState(expectedState);

m_dispatcher->dispatch();

size_t expectedState_[6] = {0,1,1,1,0,0};
testInhibitedState(expectedState_);

}

0 comments on commit 0e0fe2f

Please sign in to comment.