-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tngl.cpp
239 lines (218 loc) · 8.65 KB
/
Tngl.cpp
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#include "Tngl.h"
#include <map>
#include <iostream>
#include <stdexcept>
#include <regex>
#include <iostream>
namespace tngl {
struct Tngl::Pimpl {
std::map<std::string, Node*> seedNodes;
std::multimap<std::string, std::unique_ptr<Node>> nodes;
};
Tngl::Tngl(Node& seedNode, std::string const& seedNodeName, ExceptionHandler const& errorHandler, NodeBuilders const& nodeBuilders)
: Tngl({{seedNodeName, &seedNode}}, errorHandler, nodeBuilders)
{}
Tngl::Tngl(std::map<std::string, Node*> const& seedNodes, ExceptionHandler const& errorHandler, NodeBuilders const& nodeBuilders)
: pimpl{std::make_unique<Pimpl>()}
{
auto& nodes = pimpl->nodes;
pimpl->seedNodes = seedNodes;
// hook the seed nodes together
for (auto& [name, sn] : pimpl->seedNodes) {
for (auto& link : sn->getLinks()) {
if (link->satisfied()) {
continue;
}
for (auto& [peerName, peer] : pimpl->seedNodes) {
if (link->matchesName(peerName)) {
link->setOther(peer, peerName);
if (link->satisfied()) {
break;
}
}
}
}
}
std::vector<LinkBase*> links;
auto isUnsatisfied = [](LinkBase const* link) {
return not link->satisfied() and (link->getFlags() & Flags::CreateIfNotExist) == Flags::CreateIfNotExist;
};
for (auto const& [name, seedNode] : pimpl->seedNodes) {
std::copy(begin(seedNode->getLinks()), end(seedNode->getLinks()), std::back_inserter(links));
};
std::set<std::string> brokenBuilders;
auto findCreatorForLink = [&](LinkBase const* link) {
return std::find_if(nodeBuilders.begin(), nodeBuilders.end(), [&](auto const& creator) {
return link->matchesName(creator.first) and // if the name matches
brokenBuilders.find(creator.first) == brokenBuilders.end() and // if we did not try to create it before
nodes.find(creator.first) == nodes.end() and // if it is not created yet
detail::is_type_ancestor(link->getType(), creator.second->getType()); // if it produces the right type
});
};
while (true) {
// find a link that can be set
auto linkIt = std::find_if(links.begin(), links.end(), [&](LinkBase const* link) {
return isUnsatisfied(link) and findCreatorForLink(link) != nodeBuilders.end();
});
if (linkIt == links.end()) {
break; // we've exhausted the things to create
}
auto creatorIt = findCreatorForLink(*linkIt);
if (creatorIt == nodeBuilders.end()) {
// if at one point we decided to build a node but cannot do so now something is terribly broken
throw std::runtime_error("fatal internal error");
}
// build the new node
std::unique_ptr<Node> newNode;
try {
newNode = creatorIt->second->create();
if (not newNode) {
throw std::runtime_error("cannot create node with name: \"" + creatorIt->first + "\"");
}
} catch (...) {
brokenBuilders.insert(creatorIt->first);
try {
std::throw_with_nested(NodeNotCreatableError{creatorIt->first, "cannot create: \"" + creatorIt->first + "\""});
} catch (std::exception const& error) {
if (errorHandler) {
errorHandler(error);
}
}
continue;
}
// put all new links into the set of all links
std::copy(newNode->getLinks().begin(), newNode->getLinks().end(), std::back_inserter(links));
// apply newNode to as many links as possible
for (auto link : links) {
if (not link->satisfied() and link->matchesName(creatorIt->first)) {
link->setOther(newNode.get(), creatorIt->first);
}
}
// set the links of newNode to everything we have created so far
for (auto link : newNode->getLinks()) {
for (auto& [name, node] : pimpl->seedNodes) {
if (link->matchesName(name)) {
link->setOther(node, name);
if (link->satisfied()) {
break;
}
}
}
if (link->satisfied()) {
break;
}
for (auto& node : nodes) {
if (link->matchesName(node.first)) {
link->setOther(node.second.get(), node.first);
if (link->satisfied()) {
break;
}
}
}
}
// put all new links into the set of all links
std::copy(begin(newNode->getLinks()), end(newNode->getLinks()), std::back_inserter(links));
nodes.emplace(creatorIt->first, std::move(newNode));
}
auto isUnsatisfiedButRequired = [](LinkBase const* link) {
return not link->satisfied() and (link->getFlags() & Flags::Required) == Flags::Required;
};
// drop all nodes whose required links cannot be satisfied
auto handleBadNode = [&](Node &node, std::string const& name) {
if (errorHandler) {
// find all unsatisfied links and report them to the handler
std::vector<LinkBase*>unsatisfiedLinks;
auto const& links = node.getLinks();
std::copy_if(links.begin(), links.end(), std::back_inserter(unsatisfiedLinks), isUnsatisfiedButRequired);
errorHandler(NodeLinksNotSatisfiedError{std::move(unsatisfiedLinks), &node, "cannot create a valid environment for " + name});
}
// unlink all links to it
std::for_each(nodes.begin(), nodes.end(), [&](auto& otherNode) {
auto const& links = otherNode.second->getLinks();
std::for_each(links.begin(), links.end(), [&](auto link) {link->unset(&node);});
});
};
while (true) {
auto it = std::find_if(nodes.begin(), nodes.end(), [&](auto const& node) {
auto const& links = node.second->getLinks();
return std::find_if(links.begin(), links.end(), isUnsatisfiedButRequired) != links.end();
});
if (it == nodes.end()) {
break;
}
handleBadNode(*it->second.get(), it->first);
nodes.erase(it);
}
// test if the requires of the seed note are satisfied
{
for (auto& [name, seedNode] : pimpl->seedNodes) {
auto const& seedLinks = seedNode->getLinks();
if (seedLinks.end() != std::find_if(seedLinks.begin(), seedLinks.end(), isUnsatisfiedButRequired)) {
handleBadNode(*seedNode, name);
}
}
}
}
Tngl::~Tngl() {}
void Tngl::initialize(ExceptionHandler const& errorHandler) {
std::vector<Node*> initialized_nodes;
auto initializer = [&](std::string const& name, Node* node) {
try {
std::cout << "init: " << name << "\n";
node->initializeNode();
initialized_nodes.emplace_back(node);
} catch (...) {
for (auto* n : initialized_nodes) {
n->deinitializeNode();
}
try {
std::throw_with_nested(NodeInitializeError{node, name, "\"" + name + "\" threw during initialization"});
} catch (std::exception const& error) {
if (errorHandler) {
errorHandler(error);
}
}
}
};
for (auto& [name, node] : pimpl->seedNodes) {
initializer(name, node);
}
for (auto& [name, node] : pimpl->nodes) {
initializer(name, node.get());
}
}
void Tngl::deinitialize() {
for (auto& [name, b] : pimpl->seedNodes) {
std::cout << "deinit: " << name << "\n";
b->deinitializeNode();
}
for (auto& [name, b] : pimpl->nodes) {
std::cout << "deinit: " << name << "\n";
b->deinitializeNode();
}
}
std::multimap<std::string, Node*> Tngl::getNodesImpl(std::regex const& regex) const {
std::multimap<std::string, Node*> nodes;
for (auto& [name, node] : pimpl->seedNodes) {
if (std::regex_match(name, regex)) {
nodes.emplace(name, node);
}
}
for (auto& [name, node] : pimpl->nodes) {
if (std::regex_match(name, regex)) {
nodes.emplace(name, node.get());
}
}
return nodes;
}
std::multimap<std::string, Node*> Tngl::getNodes() const {
std::multimap<std::string, Node*> nodes;
for (auto& [name, node] : pimpl->seedNodes) {
nodes.emplace(name, node);
}
for (auto& [name, node] : pimpl->nodes) {
nodes.emplace(name, node.get());
}
return nodes;
}
}