summaryrefslogtreecommitdiff
path: root/src/graphcoloring/levels/valueloader.cpp
blob: 527ce99877ec66a11722ef9227168bb179d5d14b (plain)
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
////////////////////////////////////////////////////////////////////////////////
// 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 "valueloader.hpp"

#include "../level.hpp"
#include "utils/errors.hpp"

namespace graphcoloring {

ValueLoader::ValueLoader() : objective_points(-1) {}

void ValueLoader::AddNode(pugi::xml_node node)
{
	variables[node.attribute("id").value()] = Value(node);
}

void ValueLoader::LoadGraph(const GraphLoader& graph_loader)
{
	for (const std::pair<std::string, int>& v_id : graph_loader.vertex_ids)
		variables[v_id.first] = v_id.second;
	for (const std::pair<std::string, int>& e_id : graph_loader.edge_ids)
		variables[e_id.first] = e_id.second;
}


void ValueLoader::LoadColors(const ColorLoader& color_loader)
{
	for (std::pair<std::string, gui::Color> color : color_loader.color_names)
	{
		variables[color.first] = (int)color.second;
	}
}

void ValueLoader::LoadDocument(const pugi::xml_document& document)
{
	for (pugi::xml_node node : document.child("values").children())
	{
		std::string name = node.name();
		if (name == "comment") continue;
		if (name == "include")
		{
			pugi::xml_document doc;
			std::string filename = node.attribute("file").value();
			std::string path = "assets/levels/" + filename + ".xml";
			doc.load_file(path.c_str());
			LoadDocument(doc);
		}
		else
		{
			AddNode(node);
		}
	}
}


int ValueLoader::VariableValue(const Graph& graph,
	const std::string& name) const
{
	if (variables.count(name) == 0)
		utils::errors::Die("Variable not found: " + name);
	std::function<Value(std::string)> lookup_variable
		= [this] (std::string name)->Value {
			if (variables.count(name) == 0)
				utils::errors::Die("Variable not found: " + name);
			return variables.at(name);
		};
	return variables.at(name).Eval(graph, lookup_variable);
}

int ValueLoader::Points(const Graph& graph) const
{
	return VariableValue(graph, "points");
}

int ValueLoader::ObjectivePoints(const Graph& graph) const
{
	return VariableValue(graph, "objective");
}

} // namespace graphcoloring