Skip to content

Commit

Permalink
WIP: Update TU and code
Browse files Browse the repository at this point in the history
  • Loading branch information
Lecrapouille committed Dec 16, 2023
1 parent 242f85b commit f8d87ca
Show file tree
Hide file tree
Showing 28 changed files with 553 additions and 257 deletions.
14 changes: 12 additions & 2 deletions include/TimedPetriNetEditor/PetriNet.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,16 @@ class Place : public Node
return std::string("P" + std::to_string(id_));
}

//--------------------------------------------------------------------------
//! \brief Increment the number of token constrained by the type of net.
//--------------------------------------------------------------------------
size_t increment(size_t const count = 1u);

//--------------------------------------------------------------------------
//! \brief Decrement the number of token constrained by the type of net.
//--------------------------------------------------------------------------
size_t decrement(size_t const count = 1u);

//--------------------------------------------------------------------------
//! \brief For debug purpose only.
//--------------------------------------------------------------------------
Expand Down Expand Up @@ -501,7 +511,7 @@ class Net
//--------------------------------------------------------------------------
//! \brief Copy constructor. Needed to remove compilation warnings.
//--------------------------------------------------------------------------
Net(Net const& other) { *this = other; }
Net(Net const& other);

//--------------------------------------------------------------------------
//! \brief Copy operator. Needed to remove compilation warnings.
Expand Down Expand Up @@ -534,7 +544,7 @@ class Net
//! timed graph even, etc.
//! \return false if the net cannot be changed (i.e. to graph event).
//--------------------------------------------------------------------------
bool convertTo(TypeOfNet const type, std::vector<Arc*>& erroneous_arcs);
bool convertTo(TypeOfNet const type, std::string& error, std::vector<Arc*>& erroneous_arcs);

//--------------------------------------------------------------------------
//! \brief Load the Petri net from a JSON file. The current net is cleared
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#ifndef SPARSE_MATRIX_HPP
# define SPARSE_MATRIX_HPP

# include "Net/TropicalAlgebra.hpp"
# include "TimedPetriNetEditor/TropicalAlgebra.hpp"

// FIXME Add template and manage (max,+) types
//# include <cstdint>
Expand Down
File renamed without changes.
25 changes: 15 additions & 10 deletions src/Editor/DearImGui/Drawable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ void drawArc(ImDrawList* draw_list, Arc const& arc, TypeOfNet const type, ImVec2
float x = origin.x + arc.from.x + (arc.to.x - arc.from.x) / 2.0f;
float y = origin.y + arc.from.y + (arc.to.y - arc.from.y) / 2.0f - 15.0f;
std::stringstream stream;
stream << std::fixed << std::setprecision(2) << arc.duration;
stream << std::fixed << std::setprecision(1) << arc.duration;
draw_list->AddText(ImVec2(x, y), DURATION_COLOR, stream.str().c_str());
}
}
Expand Down Expand Up @@ -193,6 +193,7 @@ void drawPlace(ImDrawList* draw_list, Place const& place, TypeOfNet const type,
}
else
{
drawToken(draw_list, p.x, p.y);
std::string tokens = std::to_string(place.tokens);
draw_list->AddText(ImVec2(p.x, p.y), CAPTION_COLOR, tokens.c_str());
}
Expand All @@ -207,18 +208,22 @@ void drawTransition(ImDrawList* draw_list, Transition const& transition, TypeOfN
// Color of the transition: green if validated else yellow if enabled
// else color is fadding value.
ImU32 color;
//if (type == TypeOfNet::PetriNet)
//{
if (transition.canFire())
{
color = FIREABLE_COLOR;
}
else if (transition.isValidated() || transition.isEnabled())
if (transition.key == "T0")
{
color = IM_COL32(255, 165, 10, 255);
}

if (transition.canFire())
{
color = FIREABLE_COLOR;
}
else if (type == TypeOfNet::GRAFCET)
{
if (transition.isValidated() || transition.isEnabled())
{
color = IM_COL32(255, 165, 0, 255);
}
//}
//else if
}
else
{
color = FILL_COLOR(alpha);
Expand Down
14 changes: 8 additions & 6 deletions src/Editor/DearImGui/KeyBindings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@
# define KEY_BINDINGS_HPP

// -----------------------------------------------------------------------------
# define KEY_BINDING_QUIT_APPLICATION ImGuiKey_Escape
# define KEY_BINDING_RUN_SIMULATION ImGuiKey_Space
# define KEY_BINDING_RUN_SIMULATION_ALT ImGuiKey_R
# define KEY_BINDING_ROTATE_CW ImGuiKey_PageUp
# define KEY_BINDING_ROTATE_CCW ImGuiKey_PageDown
# define KEY_BINDING_MOVE_PETRI_NODE ImGuiKey_Semicolon
# define KEY_QUIT_APPLICATION ImGuiKey_Escape
# define KEY_RUN_SIMULATION ImGuiKey_Space
# define KEY_RUN_SIMULATION_ALT ImGuiKey_R
# define KEY_ROTATE_CW ImGuiKey_PageUp
# define KEY_ROTATE_CCW ImGuiKey_PageDown
# define KEY_MOVE_PETRI_NODE ImGuiKey_Semicolon
# define KEY_INCREMENT_TOKENS ImGuiKey_KeypadAdd
# define KEY_DECREMENT_TOKENS ImGuiKey_KeypadSubtract

#endif
34 changes: 28 additions & 6 deletions src/Editor/PetriEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@
// along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
//=============================================================================

#include "TimedPetriNetEditor/PetriNet.hpp"
#include "TimedPetriNetEditor/Algorithms.hpp"
#include "TimedPetriNetEditor/SparseMatrix.hpp"
#include "Editor/PetriEditor.hpp"
#include "Editor/DearImGui/Drawable.hpp"
#include "Editor/DearImGui/DearUtils.hpp"
#include "Editor/DearImGui/KeyBindings.hpp"
#include "Net/SparseMatrix.hpp"
#include "Utils/Utils.hpp"
#include "TimedPetriNetEditor/Algorithms.hpp"

namespace tpne {

Expand Down Expand Up @@ -763,7 +764,8 @@ bool Editor::switchOfNet(TypeOfNet const type)
return false;

std::vector<Arc*> arcs;
if (m_net.convertTo(type, arcs))
std::string error;
if (m_net.convertTo(type, error, arcs))
return true;

m_messages.setError(m_net.error());
Expand Down Expand Up @@ -1332,16 +1334,36 @@ void Editor::PetriView::onHandleInput()

if (ImGui::IsItemHovered())
{
if (ImGui::IsKeyPressed(KEY_BINDING_MOVE_PETRI_NODE, false))
if (ImGui::IsKeyPressed(KEY_MOVE_PETRI_NODE, false))
{
handleMoveNode();
}
// Run the animation of the Petri net
else if (ImGui::IsKeyPressed(KEY_BINDING_RUN_SIMULATION) ||
ImGui::IsKeyPressed(KEY_BINDING_RUN_SIMULATION_ALT))
else if (ImGui::IsKeyPressed(KEY_RUN_SIMULATION) ||
ImGui::IsKeyPressed(KEY_RUN_SIMULATION_ALT))
{
m_editor.toogleStartSimulation();
}
// Increment the number of tokens in the place.
else if (ImGui::IsKeyPressed(KEY_INCREMENT_TOKENS))
{
Node* node = m_editor.getNode(m_mouse.position);
if ((node != nullptr) && (node->type == Node::Type::Place))
{
reinterpret_cast<Place*>(node)->increment(1u);
m_editor.m_net.modified = true;
}
}
// Decrement the number of tokens in the place.
else if (ImGui::IsKeyPressed(KEY_DECREMENT_TOKENS))
{
Node* node = m_editor.getNode(m_mouse.position);
if ((node != nullptr) && (node->type == Node::Type::Place))
{
reinterpret_cast<Place*>(node)->decrement(1u);
m_editor.m_net.modified = true;
}
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/Net/Algorithms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

#include "TimedPetriNetEditor/PetriNet.hpp"
#include "TimedPetriNetEditor/Algorithms.hpp"
#include "Net/SparseMatrix.hpp"
#include "TimedPetriNetEditor/SparseMatrix.hpp"
#include "Net/Howard.h"

namespace tpne {
Expand All @@ -35,6 +35,7 @@ template<> bool SparseMatrix<double>::display_as_dense = false;
bool isEventGraph(Net const& net, std::string& error, std::vector<Arc*>& erroneous_arcs)
{
erroneous_arcs.clear();
error.clear();
if (net.isEmpty())
{
error = "Empty Petri net is not an event graph";
Expand Down
4 changes: 2 additions & 2 deletions src/Net/Exports/ExportJulia.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
// along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
//=============================================================================

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

Expand Down
44 changes: 33 additions & 11 deletions src/Net/Imports/ImportJSON.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,44 @@ std::string importFromJSON(Net& net, std::string const& filename)
return error.str();
}

std::string type = std::string(json["type"]);
if (type == "GRAFCET") {
net.clear(TypeOfNet::GRAFCET);
} else if (type == "Petri net") {
net.clear(TypeOfNet::PetriNet);
} else if (type == "Timed Petri net") {
net.clear(TypeOfNet::TimedPetriNet);
} else if (type == "Timed event graph") {
net.clear(TypeOfNet::TimedEventGraph);
} else {
if (json.contains("type"))
{
std::string type = std::string(json["type"]);
if (type == "GRAFCET") {
net.clear(TypeOfNet::GRAFCET);
} else if (type == "Petri net") {
net.clear(TypeOfNet::PetriNet);
} else if (type == "Timed Petri net") {
net.clear(TypeOfNet::TimedPetriNet);
} else if (type == "Timed event graph") {
net.clear(TypeOfNet::TimedEventGraph);
} else {
error << "Failed parsing '" << filename << "'. Reason was '"
<< "Unknown type of net: " << type << "'" << std::endl;
return error.str();
}
}
else
{
error << "Failed parsing '" << filename << "'. Reason was '"
<< "Unknown type of net: " << type << "'" << std::endl;
<< "Missing type of Net'" << std::endl;
return error.str();
}

if (!json.contains("nets"))
{
error << "Failed parsing '" << filename << "'. Reason was '"
<< "Missing JSON nets field'" << std::endl;
return error.str();
}
nlohmann::json const& jnet = json["nets"][0];

if (!jnet.contains("name"))
{
error << "Failed parsing '" << filename << "'. Reason was '"
<< "Missing JSON net name'" << std::endl;
return error.str();
}
net.name = std::string(jnet["name"]);

// Places
Expand Down
Loading

0 comments on commit f8d87ca

Please sign in to comment.