-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Import from / Export to timed event graph format
- Loading branch information
1 parent
30583a2
commit 99ea173
Showing
13 changed files
with
259 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
TimedEventGraph 3 5 | ||
|
||
1 0: 0 1 | ||
0 1: 1 0 | ||
0 2: 0 0 | ||
1 2: 1 0 | ||
2 2: 2 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
TimedEventGraph 8 12 | ||
|
||
1 0: 61 2 | ||
3 1: 81 1 | ||
0 2: 58 1 | ||
5 2: 0 0 | ||
2 3: 86 2 | ||
4 3: 69 2 | ||
3 4: 69 1 | ||
6 4: 36 1 | ||
4 5: 35 1 | ||
1 6: 0 0 | ||
7 6: 58 1 | ||
5 7: 61 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
//============================================================================= | ||
// TimedPetriNetEditor: A timed Petri net editor. | ||
// Copyright 2021 -- 2023 Quentin Quadrat <lecrapouille@gmail.com> | ||
// | ||
// This file is part of TimedPetriNetEditor. | ||
// | ||
// TimedPetriNetEditor is free software: you can redistribute it and/or modify it | ||
// under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, but | ||
// WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
// General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// 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 <fstream> | ||
#include <cstring> | ||
|
||
namespace tpne { | ||
|
||
//------------------------------------------------------------------------------ | ||
std::string exportToTimedEventGraph(Net const& net, std::string const& filename) | ||
{ | ||
std::stringstream error; | ||
|
||
if (!isEventGraph(net)) | ||
{ | ||
error << "Failed to export the Petri net to '" << filename | ||
<< "'. Reason was 'the net is not an event graph'" | ||
<< std::endl; | ||
return error.str(); | ||
} | ||
|
||
std::ofstream file(filename); | ||
if (!file) | ||
{ | ||
error << "Failed to export the Petri net to '" << filename | ||
<< "'. Reason was " << strerror(errno) << std::endl; | ||
return error.str(); | ||
} | ||
|
||
file << "TimedEventGraph " << net.transitions().size() << " " | ||
<< net.places().size() << std::endl << std::endl; | ||
|
||
for (auto const& p: net.places()) | ||
{ | ||
file << p.arcsIn[0]->from.id << " " << p.arcsOut[0]->to.id | ||
<< ": " << p.arcsIn[0]->duration << " " << p.tokens | ||
<< std::endl; | ||
} | ||
|
||
return {}; | ||
} | ||
|
||
} // namespace tpne |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
//============================================================================= | ||
// TimedPetriNetEditor: A timed Petri net editor. | ||
// Copyright 2021 -- 2023 Quentin Quadrat <lecrapouille@gmail.com> | ||
// | ||
// This file is part of TimedPetriNetEditor. | ||
// | ||
// TimedPetriNetEditor is free software: you can redistribute it and/or modify it | ||
// under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, but | ||
// WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
// General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. | ||
//============================================================================= | ||
|
||
#include "Imports.hpp" | ||
#include "TimedPetriNetEditor/PetriNet.hpp" | ||
#include "Utils/Utils.hpp" | ||
|
||
#include <fstream> | ||
#include <cstring> | ||
|
||
namespace tpne { | ||
|
||
//------------------------------------------------------------------------------ | ||
std::string importFromTimedEventGraph(Net& net, std::string const& filename) | ||
{ | ||
std::stringstream error; | ||
size_t initial_transition, final_transition, tokens; | ||
float duration; | ||
// windows screen. | ||
// FIXME: get the exact dimension Editor::viewSize() | ||
// FIXME: initial frame iteration: the screen size is not at its final size | ||
const size_t w = 700u; const size_t h = 700u; | ||
const size_t margin = 50u; const size_t nodes_by_line = 4u; | ||
|
||
net.reset(TypeOfNet::TimedPetriNet);//TimedEventGraph); | ||
|
||
// Check if file exists | ||
std::ifstream file(filename); | ||
if (!file) | ||
{ | ||
error << "Failed opening '" << filename << "'. Reason was '" | ||
<< strerror(errno) << "'" << std::endl; | ||
return error.str(); | ||
} | ||
|
||
// Extract number of transitions and number of lines | ||
size_t transitions, lines, rows; | ||
char separator; | ||
std::string type; | ||
|
||
if (!(file >> type >> transitions >> lines)) | ||
{ | ||
error << "Malformed header. Needed 'TimedEventGraph number_transitions number_lines'" | ||
<< std::endl; | ||
return error.str(); | ||
} | ||
if (type != "TimedEventGraph") | ||
{ | ||
error << "Malformed token. Expected to extract token 'TimedEventGraph'" | ||
<< std::endl; | ||
return error.str(); | ||
} | ||
|
||
// Since the file does not give position, we place them as square | ||
size_t dx = (w - 2u * margin) / (nodes_by_line - 1u); | ||
size_t dy = (h - 2u * margin) / (nodes_by_line - 1u); | ||
size_t x = margin, y = margin; | ||
for (size_t id = 0u; id < transitions; ++id) | ||
{ | ||
net.addTransition(id, Transition::to_str(id), x, y, 0); | ||
x += dx; | ||
if (x > w - margin) { x = margin; y += dy; } | ||
} | ||
|
||
// Create arcs between created transitions. Places are automatically created | ||
// when the arc is created. | ||
for (size_t i = 0u; i < lines; ++i) | ||
{ | ||
if (!(file >> final_transition >> initial_transition >> separator >> duration >> tokens)) | ||
{ | ||
error << "Malformed line. Expected 4 values: 'initial_transition" | ||
<< " final_transition: duration tokens'" << std::endl; | ||
return error.str(); | ||
} | ||
if ((initial_transition >= transitions) || (final_transition >= transitions)) | ||
{ | ||
error << "Malformed line. Invalid transition ID" << std::endl; | ||
return error.str(); | ||
} | ||
if (separator != ':') | ||
{ | ||
error << "Malformed line. Missing ':' separator" << std::endl; | ||
return error.str(); | ||
} | ||
|
||
Transition* t0 = net.findTransition(initial_transition); | ||
Transition* t1 = net.findTransition(final_transition); | ||
assert((t0 != nullptr) && (t1 != nullptr)); | ||
net.addArc(*t0, *t1, tokens, duration); | ||
} | ||
|
||
return {}; | ||
} | ||
|
||
} // namespace tpne |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.