1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2018 Leo Tenenbaum
// This file is part of GraphColoring.
//
// GraphColoring 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.
//
// GraphColoring 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 GraphColoring. If not, see <https://www.gnu.org/licenses/>.
////////////////////////////////////////////////////////////////////////////////
#include "graphloader.hpp"
#include <sstream>
#include "utils/errors.hpp"
#include "../level.hpp"
#include "../graphcoloring.hpp"
namespace graphcoloring {
GraphLoader::GraphLoader(const ColorLoader& color_loader_,
const GlobalLoader& global_loader_)
: color_loader(color_loader_), global_loader(global_loader_)
{}
void GraphLoader::LoadDocument(const pugi::xml_document& document, Graph& graph)
{
pugi::xml_node graph_node = document.child("graph");
for (pugi::xml_node vertex_node : graph_node.children("vertex"))
ReadVertex(vertex_node, graph);
for (pugi::xml_node edge_node : graph_node.children("edge"))
ReadEdge(edge_node, graph);
}
bool GraphLoader::IsVertexProtected(std::string protections, char protection)
const
{
bool is_protected = protections.find(protection) != std::string::npos;
if (global_loader.IsVertexProtected(protection))
is_protected = true;
return is_protected;
}
bool GraphLoader::IsEdgeProtected(std::string protections, char protection)
const
{
bool is_protected = protections.find(protection) != std::string::npos;
if (global_loader.IsEdgeProtected(protection))
is_protected = true;
return is_protected;
}
void GraphLoader::ReadVertex(pugi::xml_node vertex_node, Graph& graph)
{
int x = vertex_node.attribute("x").as_int();
int y = vertex_node.attribute("y").as_int();
int v_id = graph.AddVertex(x, y);
Vertex& v = graph.GetVertexByID(v_id);
if (!vertex_node.attribute("origin").empty())
{
v.is_delete_protected = true;
graph.AddOrigin(v_id);
}
if (!vertex_node.attribute("id").empty())
{
vertex_ids[vertex_node.attribute("id").value()] = v.id;
}
gui::Color color
= color_loader.GetColorFromAttribute(vertex_node.attribute("color"));
v.ChangeColor(color);
// NOTE: This next line does handle the case where there is no protect
// specified; pugixml will default to "".
std::string protections = vertex_node.attribute("protect").value();
v.is_color_protected = IsVertexProtected(protections, PROTECT_COLOR);
v.is_delete_protected = IsVertexProtected(protections, PROTECT_DELETE);
v.is_edge_protected = IsVertexProtected(protections, PROTECT_EDGE);
}
void GraphLoader::ReadEdge(pugi::xml_node edge_node, Graph& graph)
{
std::string v1_name = edge_node.attribute("v1").value();
std::string v2_name = edge_node.attribute("v2").value();
int v1 = GetVertexByName(v1_name);
int v2 = GetVertexByName(v2_name);
if (v1 == -1)
utils::errors::Die("There is no vertex named " + v1_name);
if (v2 == -1)
utils::errors::Die("There is no vertex named " + v2_name);
int e_id = graph.AddEdge(v1, v2);
Edge& e = graph.GetEdgeByID(e_id);
if (!edge_node.attribute("id").empty())
{
edge_ids[edge_node.attribute("id").value()] = e.id;
}
gui::Color color
= color_loader.GetColorFromAttribute(edge_node.attribute("color"));
e.ChangeColor(color);
std::string protections = edge_node.attribute("protect").value();
e.is_color_protected = IsEdgeProtected(protections, PROTECT_COLOR);
e.is_delete_protected = IsEdgeProtected(protections, PROTECT_DELETE);
}
void GraphLoader::WriteVertex(const Vertex& vertex, pugi::xml_node& node) const
{
node.set_name("vertex");
node.append_attribute("x") = vertex.x;
node.append_attribute("y") = vertex.y;
node.append_attribute("id") = vertex.id;
node.append_attribute("color") = color_loader.GetColorName(vertex.Color())
.c_str();
std::stringstream protections;
if (vertex.is_color_protected) protections << PROTECT_COLOR;
if (vertex.is_delete_protected) protections << PROTECT_DELETE;
if (vertex.is_edge_protected) protections << PROTECT_EDGE;
node.append_attribute("protect") = protections.str().c_str();
}
void GraphLoader::WriteEdge(const Edge& edge, pugi::xml_node& node) const
{
node.set_name("edge");
node.append_attribute("v1") = edge.from.id;
node.append_attribute("v2") = edge.to.id;
node.append_attribute("id") = edge.id;
node.append_attribute("color") = color_loader.GetColorName(edge.Color())
.c_str();
std::stringstream protections;
if (edge.is_color_protected) protections << PROTECT_COLOR;
if (edge.is_delete_protected) protections << PROTECT_DELETE;
node.append_attribute("protect") = protections.str().c_str();
}
void GraphLoader::WriteGraph(const Graph& graph, pugi::xml_node& node) const
{
for (const Vertex* v : graph.vertices)
{
pugi::xml_node vertex_node = node.append_child("vertex");
WriteVertex(*v, vertex_node);
}
for (const Edge* e : graph.edges)
{
pugi::xml_node edge_node = node.append_child("edge");
WriteEdge(*e, edge_node);
}
}
int GraphLoader::GetVertexByName(const std::string& name) const
{
if (vertex_ids.count(name))
return vertex_ids.at(name);
else
return -1;
}
int GraphLoader::GetEdgeByName(const std::string& name) const
{
if (edge_ids.count(name))
return edge_ids.at(name);
else
return -1;
}
} // namespace graphcoloring
|