Skip to content

Commit

Permalink
BROKEN: manage receptivities in simulation
Browse files Browse the repository at this point in the history
  • Loading branch information
Lecrapouille committed Jan 5, 2024
1 parent 5a785a9 commit 5351e6c
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 58 deletions.
39 changes: 22 additions & 17 deletions src/Editor/PetriEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,6 @@ void Editor::inspector()

// Transition captions and GRAFCET transitivities
{
// FIXME parse and clear sensors if and only if we modified entrytext
ImGui::Begin("Transitions");
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0));
ImGui::Checkbox(m_states.show_transition_captions
Expand All @@ -738,29 +737,35 @@ void Editor::inspector()
&m_states.show_transition_captions);
ImGui::PopStyleVar();
ImGui::Separator();
//if (!editor.m_simulation.running)
// editor.m_net.m_sensors.clear();
ImGui::Text("%s", "Captions:");
for (auto& transition: m_net.transitions())
for (auto& t: m_net.transitions())
{
ImGui::InputText(transition.key.c_str(), &transition.caption, readonly);
if (!m_simulation.running)
ImGui::InputText(t.key.c_str(), &t.caption, readonly);
if ((m_net.type() == TypeOfNet::GRAFCET) && (!m_simulation.running))
{
//std::string err = editor.m_net.parse(transition, true);
//if (!err.empty())
//{
// ImGui::TextColored(ImVec4(1.0f, 0.0f, 0.0f, 1.0f), "%s", err.c_str());
//}
std::vector<Receptivity> const& receptivities = m_simulation.receptivities();
Receptivity const& recp = receptivities[t.id];
// FIXME parse and clear sensors if and only if we modified entrytext
if (!recp.isValid()) // && recp.compiled()
{
ImGui::TextColored(ImVec4(1.0f, 0.0f, 0.0f, 1.0f), "%s", recp.error().c_str());
}
}
}
ImGui::End();

//ImGui::Begin("Sensors");
//for (auto const& it: editor.m_net.m_sensors.database())
//{
// ImGui::SliderInt(it.first.c_str(), &it.second, 0, 1);
//}
//ImGui::End();
if (m_net.type() == TypeOfNet::GRAFCET)
{
//if (m_simulation.running)
{
ImGui::Begin("Sensors");
for (auto& it: Sensors::instance().database())
{
ImGui::SliderInt(it.first.c_str(), &it.second, 0, 1);
}
ImGui::End();
}
}
}

// Arc durations
Expand Down
19 changes: 8 additions & 11 deletions src/Net/Exports/ExportGrafcetCpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
// along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
//=============================================================================

#include "Net/Exports/Exports.hpp"
#include "TimedPetriNetEditor/PetriNet.hpp"
#include "Net/Exports/Exports.hpp"
#include "Net/Receptivities.hpp"
#include <fstream>
#include <cstring>

Expand Down Expand Up @@ -233,9 +234,7 @@ class Grafcet: public MQTT
file << " //-------------------------------------------------------------------------" << std::endl;
if (net.type() == TypeOfNet::GRAFCET)
{
// FIXME
file << "TODO Receptivity" << std::endl;
//file << " bool T" << t.id << "() { return " << Receptivity::Parser::translate(t.caption, "C") << "; } const";
file << " bool T" << t.id << "() { return " << Receptivity::Parser::translate(t.caption, "C") << "; } const";
}
else
{
Expand All @@ -261,13 +260,11 @@ class Grafcet: public MQTT
file << " bool T[MAX_TRANSITIONS];" << std::endl;
file << " //! \\brief MQTT topic to communicate with the Petri net editor" << std::endl;
file << " std::string m_topic = \"pneditor/" << name_space << "\";" << std::endl;
//FIXME
file << "TODO sensors" << std::endl;
//for (auto const& s: m_sensors.database())
//{
// file << " //! \\brief" << std::endl;
// file << " bool " << s.first << " = " << s.second << ";" << std::endl;
//}
for (auto const& s: Sensors::instance().database())
{
file << " //! \\brief" << std::endl;
file << " bool " << s.first << " = " << s.second << ";" << std::endl;
}
file << "};" << std::endl;
file << "" << std::endl;
file << "} // namespace " << name_space << std::endl;
Expand Down
22 changes: 3 additions & 19 deletions src/Net/PetriNet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -635,34 +635,18 @@ bool Net::resetReceptivies()
{
bool res = true;

// Receptivities for GRAFCET are defined by sensors.
if (m_type == TypeOfNet::GRAFCET)
{
#if 0 // FIXME
m_sensors.clear();
for (auto& transition: m_transitions)
{
std::string err = parse(transition, true);
if (!err.empty())
{
m_error.str("");
m_error << err;
res = false;
}
}
#endif
}
// For PetriNet set receptivities to false since we want the
// user to click on desired transitions.
else if (m_type == TypeOfNet::PetriNet)
if (m_type == TypeOfNet::PetriNet)
{
for (auto& transition: m_transitions)
{
transition.receptivity = false;
}
}
// For other type of nets (timed Petri net, graph events) the
// receptiviites are always true.
// receptiviites are always true. For GRAFCET transition.receptivity
// is set by the simulation depending on the boolean expression.
else
{
for (auto& transition: m_transitions)
Expand Down
2 changes: 1 addition & 1 deletion src/Net/Receptivities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ bool Receptivity::StepExp::evaluate() const
bool Receptivity::VariableExp::evaluate() const
{
try {
return Sensors::get(m_name);
return Sensors::instance().get(m_name);
}
catch (...) {
assert("Unkown variable");
Expand Down
21 changes: 15 additions & 6 deletions src/Net/Receptivities.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,25 @@ class Net;
class Sensors
{
public:

static Sensors& instance() { static Sensors sensor; return sensor; }

//! \brief Get the value of the given sensor. Throw if the sensor is unkown.
static bool get(std::string const& sensor) { return m_values.at(sensor); modified = true; }
static void set(std::string const& sensor, int const value) { m_values[sensor] = value; }
static std::map<const std::string, int> const& database() { return m_values; }
static void clear() { m_values.clear(); modified = false; }
bool get(std::string const& sensor) { return m_values.at(sensor); modified = true; }
void set(std::string const& sensor, int const value) { m_values[sensor] = value; }
std::map<const std::string, int>& database() { return m_values; }
void clear() { m_values.clear(); modified = false; }

private:

Sensors() = default;

private:

static std::map<const std::string, int> m_values;
std::map<const std::string, int> m_values;

public:
static bool modified;// = false;
static bool modified;
};

// *****************************************************************************
Expand All @@ -65,6 +72,8 @@ class Receptivity
public:

std::string compile(std::string const& code, Net& net);
inline bool isValid() const { return m_valid; }
inline std::string const& error() const { return m_error; }
bool evaluate();

// *************************************************************************
Expand Down
6 changes: 2 additions & 4 deletions src/Net/Simulation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,8 @@ class Simulation

Simulation(Net& net, Messages& m_messages);
void step(float const dt);
std::vector<TimedToken> const& timedTokens() const
{
return m_timed_tokens;
}
inline std::vector<TimedToken> const& timedTokens() const { return m_timed_tokens; }
inline std::vector<Receptivity> const& receptivities() const { return m_receptivities; }

private:

Expand Down

0 comments on commit 5351e6c

Please sign in to comment.