diff --git a/symmetri/gui/menu_bar.cpp b/symmetri/gui/menu_bar.cpp index 99b732e..6fc071f 100644 --- a/symmetri/gui/menu_bar.cpp +++ b/symmetri/gui/menu_bar.cpp @@ -32,7 +32,7 @@ void draw_menu_bar(const model::ViewModel &vm) { file_dialog.Open(); } if (ImGui::MenuItem("Save")) { - rxdispatch::push(farbart::writeToDisk(vm)); + rxdispatch::push(farbart::writeGraphToDisk(vm)); } // Exit... ImGui::EndMenu(); diff --git a/symmetri/gui/write_graph_to_disk.cpp b/symmetri/gui/write_graph_to_disk.cpp index 72415f4..b0eca96 100644 --- a/symmetri/gui/write_graph_to_disk.cpp +++ b/symmetri/gui/write_graph_to_disk.cpp @@ -7,64 +7,71 @@ #include "tinyxml2/tinyxml2.h" namespace farbart { -model::Computer writeToDisk(const model::ViewModel& vm) { - return [&net = vm.net, &p_positions = vm.p_positions, - &t_positions = vm.t_positions, p_view = vm.p_view, t_view = vm.t_view, - path = vm.active_file]() { - using namespace tinyxml2; - XMLDocument doc; - XMLElement* pnml = doc.NewElement("pnml"); - pnml->SetAttribute("xmlns", - "http://www.pnml.org/version-2009/grammar/pnml"); - doc.InsertFirstChild(pnml); - auto xmlnet = pnml->InsertFirstChild(doc.NewElement("net")); - auto root = xmlnet->InsertFirstChild(doc.NewElement("page")); - for (auto idx : p_view) { - XMLElement* child = doc.NewElement("place"); - XMLElement* s_child = doc.NewElement("graphics"); - XMLElement* s_s_child = doc.NewElement("position"); - child->SetAttribute("id", net.place[idx].c_str()); - s_s_child->SetAttribute("x", p_positions[idx].x); - s_s_child->SetAttribute("y", p_positions[idx].y); - s_child->InsertEndChild(s_s_child); - child->InsertEndChild(s_child); +auto toXml(const symmetri::Petri::PTNet& net, + const std::vector& p_positions, + const std::vector& t_positions, + const std::vector& p_view, + const std::vector& t_view) { + using namespace tinyxml2; + auto doc = std::make_unique(); + XMLElement* pnml = doc->NewElement("pnml"); + pnml->SetAttribute("xmlns", "http://www.pnml.org/version-2009/grammar/pnml"); + doc->InsertFirstChild(pnml); + auto xmlnet = pnml->InsertFirstChild(doc->NewElement("net")); + auto root = xmlnet->InsertFirstChild(doc->NewElement("page")); + + for (auto idx : p_view) { + XMLElement* child = doc->NewElement("place"); + XMLElement* s_child = doc->NewElement("graphics"); + XMLElement* s_s_child = doc->NewElement("position"); + child->SetAttribute("id", net.place[idx].c_str()); + s_s_child->SetAttribute("x", p_positions[idx].x); + s_s_child->SetAttribute("y", p_positions[idx].y); + s_child->InsertEndChild(s_s_child); + child->InsertEndChild(s_child); + root->InsertEndChild(child); + } + + for (auto idx : t_view) { + XMLElement* child = doc->NewElement("transition"); + XMLElement* s_child = doc->NewElement("graphics"); + XMLElement* s_s_child = doc->NewElement("position"); + child->SetAttribute("id", net.transition[idx].c_str()); + s_s_child->SetAttribute("x", t_positions[idx].x); + s_s_child->SetAttribute("y", t_positions[idx].y); + s_child->InsertEndChild(s_s_child); + child->InsertEndChild(s_child); + root->InsertEndChild(child); + + for (const auto& token : net.input_n[idx]) { + XMLElement* child = doc->NewElement("arc"); + child->SetAttribute("source", net.place[std::get(token)].c_str()); + child->SetAttribute("target", net.transition[idx].c_str()); + child->SetAttribute( + "color", + std::string(std::get(token).toString()).c_str()); root->InsertEndChild(child); } - - for (auto idx : t_view) { - XMLElement* child = doc.NewElement("transition"); - XMLElement* s_child = doc.NewElement("graphics"); - XMLElement* s_s_child = doc.NewElement("position"); - child->SetAttribute("id", net.transition[idx].c_str()); - s_s_child->SetAttribute("x", t_positions[idx].x); - s_s_child->SetAttribute("y", t_positions[idx].y); - s_child->InsertEndChild(s_s_child); - child->InsertEndChild(s_child); + for (const auto& token : net.output_n[idx]) { + XMLElement* child = doc->NewElement("arc"); + child->SetAttribute("source", net.transition[idx].c_str()); + child->SetAttribute("target", net.place[std::get(token)].c_str()); + child->SetAttribute( + "color", + std::string(std::get(token).toString()).c_str()); root->InsertEndChild(child); - - for (const auto& token : net.input_n[idx]) { - XMLElement* child = doc.NewElement("arc"); - child->SetAttribute("source", - net.place[std::get(token)].c_str()); - child->SetAttribute("target", net.transition[idx].c_str()); - child->SetAttribute( - "color", - std::string(std::get(token).toString()).c_str()); - root->InsertEndChild(child); - } - for (const auto& token : net.output_n[idx]) { - XMLElement* child = doc.NewElement("arc"); - child->SetAttribute("source", net.transition[idx].c_str()); - child->SetAttribute("target", - net.place[std::get(token)].c_str()); - child->SetAttribute( - "color", - std::string(std::get(token).toString()).c_str()); - root->InsertEndChild(child); - } } - doc.SaveFile(path.c_str()); + } + return doc; +} + +model::Computer writeGraphToDisk(const model::ViewModel& vm) { + return [&net = vm.net, &p_positions = vm.p_positions, + &t_positions = vm.t_positions, p_view = vm.p_view, t_view = vm.t_view, + path = vm.active_file]() { + auto doc = toXml(net, p_positions, t_positions, p_view, t_view); + doc->SaveFile(path.c_str()); return [path](model::Model&& m_ptr) { m_ptr.data->active_file = path.c_str(); return m_ptr; diff --git a/symmetri/gui/write_graph_to_disk.h b/symmetri/gui/write_graph_to_disk.h index a8aa9d2..fcd3a8c 100644 --- a/symmetri/gui/write_graph_to_disk.h +++ b/symmetri/gui/write_graph_to_disk.h @@ -1,10 +1,8 @@ #pragma once -#include - #include "model.h" namespace farbart { -model::Computer writeToDisk(const model::ViewModel &vm); +model::Computer writeGraphToDisk(const model::ViewModel &vm); } // namespace farbart