Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
290 lines (252 loc) · 10.1 KB
/
Copy pathmain.cpp
File metadata and controls
290 lines (252 loc) · 10.1 KB
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
// Generic, domain-unrelated example: a small text-processing pipeline that
// demonstrates the core building blocks of filterGraph:
// - MessageFilter: the base stage interface
// - FilterGraph: compile-time chaining of stages
// - FilterRegistry + DslFilterGraph: runtime graphs described in the text DSL,
// with fan-out (a "tap"), fan-in (merges, with typed slots) and several
// named outputs (named slots and inputs: see apps/namedPipeline)
// - validateDslGraph: every problem in a broken graph, located by line:column
// - JsonFilterGraph: the JSON format, which remains supported
#include <filterGraph/core/filterGraph/DslFilterGraph.hpp>
#include <filterGraph/core/filterGraph/FanoutFilter.hpp>
#include <filterGraph/core/filterGraph/FilterGraph.hpp>
#include <filterGraph/core/filterGraph/JsonFilterGraph.hpp>
#include <filterGraph/core/filterGraph/MergeFilter.hpp>
#include <filterGraph/core/filterGraph/MessageFilter.hpp>
#include <nlohmann/json.hpp>
#include <algorithm>
#include <any>
#include <cctype>
#include <cstddef>
#include <format>
#include <iostream>
#include <memory>
#include <optional>
#include <string>
using filterGraph::DslFilterGraph;
using filterGraph::FilterGraph;
using filterGraph::FilterRegistrar;
using filterGraph::JsonFilterGraph;
using filterGraph::MergeInputs;
using filterGraph::MessageFilter;
using filterGraph::TypedMergeFilter;
using filterGraph::registerFanoutFilter;
using filterGraph::registerMergeFilter;
using filterGraph::validateDslGraph;
namespace dsl = filterGraph::dsl;
// --- Stages -----------------------------------------------------------
class UppercaseFilter : public MessageFilter<std::string>
{
public:
std::optional<std::string> filter(std::string&& text) override
{
std::ranges::transform(text, text.begin(), [](unsigned char c) { return static_cast<char>(std::toupper(c)); });
return text;
}
};
class ReverseFilter : public MessageFilter<std::string>
{
public:
std::optional<std::string> filter(std::string&& text) override
{
std::ranges::reverse(text);
return text;
}
};
// Changes the message type: string in, length out.
class LengthFilter : public MessageFilter<std::string, std::size_t>
{
public:
std::optional<std::size_t> filter(std::string&& text) override
{
return text.size();
}
};
// A configurable stage: drops (short-circuits) any message shorter than a
// configurable minimum length, demonstrating both configuration and early
// termination via std::nullopt.
class MinLengthFilter : public MessageFilter<std::string>
{
public:
explicit MinLengthFilter(std::size_t minLength)
: mMinLength(minLength)
{
}
std::optional<std::string> filter(std::string&& text) override
{
if (text.size() < mMinLength)
{
return std::nullopt;
}
return text;
}
private:
std::size_t mMinLength;
};
class PrintFilter : public MessageFilter<std::string, int>
{
public:
explicit PrintFilter(std::string prefix = {})
: mPrefix(std::move(prefix))
{
}
std::optional<int> filter(std::string&& text) override
{
std::cout << mPrefix << text << std::endl;
return 0;
}
private:
std::string mPrefix;
};
// --- Registration: the names the DSL (and JSON) refer to ---------------
static FilterRegistrar<UppercaseFilter> registerUppercase("Uppercase");
static FilterRegistrar<ReverseFilter> registerReverse("Reverse");
static FilterRegistrar<LengthFilter> registerLength("Length");
// Stage arguments, e.g. `MinLength(minLength=3)`, arrive as a JSON object.
static FilterRegistrar<MinLengthFilter> registerMinLength(
"MinLength",
[](const nlohmann::json& config) {
return std::make_shared<MinLengthFilter>(config.at("minLength").get<std::size_t>());
});
static FilterRegistrar<PrintFilter> registerPrint(
"Print",
[](const nlohmann::json& config) {
return std::make_shared<PrintFilter>(config.value("prefix", std::string{}));
});
// A merge stage for fan-in `(a, b, c) -> Concat`. The combiner receives one
// slot per edge; empty slots are "holes" left by paths that dropped the
// message. It concatenates the present values with " | " and counts the holes.
static const bool sRegisterConcat = [] {
registerMergeFilter<std::string>(
"Concat",
[](MergeInputs&& inputs) -> std::optional<std::string> {
std::string joined;
std::size_t holes = 0;
for (auto& input : inputs)
{
if (!input.has_value())
{
++holes;
continue;
}
if (!joined.empty())
{
joined += " | ";
}
joined += std::any_cast<std::string>(input);
}
return joined + std::format(" ({} hole{})", holes, holes == 1 ? "" : "s");
});
return true;
}();
// A typed merge stage for `(upper, length) -> Report`: it declares the type of
// each slot, so the DSL checks the group's edges when the graph is built, and
// the slots arrive as std::optionals (empty = a hole) instead of std::any.
class ReportFilter : public TypedMergeFilter<std::string, std::string, std::size_t>
{
public:
std::optional<std::string> merge(std::optional<std::string>&& upper,
std::optional<std::size_t>&& length) override
{
return std::format("{} ({} chars)", upper.value_or("-"), length.value_or(0));
}
};
static FilterRegistrar<ReportFilter> registerReport("Report");
// Only needed by the JSON example: in the DSL, fan-out is built in.
static const bool sRegisterFanout = [] {
registerFanoutFilter<std::string>("Fanout");
return true;
}();
int main()
{
// 1) Compile-time pipeline: FilterGraph<Uppercase, Reverse, Print>
{
using Pipeline = FilterGraph<UppercaseFilter, ReverseFilter, PrintFilter>;
Pipeline pipeline(
std::make_shared<UppercaseFilter>(),
std::make_shared<ReverseFilter>(),
std::make_shared<PrintFilter>("[compile-time] "));
pipeline.filter(std::string{"Hello, filterGraph!"});
}
// 2) Runtime graph in the text DSL, with a tap. Both statements read `in`,
// so each gets its own copy: the first uppercases and prints its copy
// (then discards it at `end`); the second reverses the message, drops it
// if it is too short, and prints it as the graph's output.
{
DslFilterGraph<std::string, int> pipeline(R"dsl(
in -> Uppercase -> shouted -> Print(prefix="[tap] ") -> end
in -> Reverse -> reversed -> MinLength(minLength=3) -> long -> Print(prefix="[main] ") -> out
)dsl");
pipeline.filter(std::string{"Hello, filterGraph!"});
pipeline.filter(std::string{"ab"}); // tapped, then dropped by MinLength on the main path
}
// 3) Fan-in: three paths read the same input and a merge combines them.
// MinLength=100 drops its copy, which leaves a hole in the merge.
{
DslFilterGraph<std::string, int> pipeline(R"dsl(
in -> Uppercase -> upper
in -> Reverse -> reversed
in -> MinLength(minLength=100) -> long
(upper, reversed, long) -> Concat -> joined -> Print(prefix="[merge] ") -> out
)dsl");
pipeline.filter(std::string{"Hello, filterGraph!"});
}
// 3b) The same fan-in with a typed merge: Report declares its slot types,
// so a group in the wrong order is rejected when the graph is built
// instead of throwing std::bad_any_cast on the first message.
{
DslFilterGraph<std::string, int> pipeline(R"dsl(
in -> Uppercase -> upper
in -> Length -> length
(upper, length) -> Report -> reported -> Print(prefix="[typed] ") -> out
)dsl");
pipeline.filter(std::string{"Hello, filterGraph!"});
for (const auto& diagnostic : validateDslGraph<std::string, int>(
"in -> Uppercase -> upper\n"
"in -> Length -> length\n"
"(length, upper) -> Report -> reported -> Print -> out\n"))
{
std::cout << "[typed check] " << dsl::formatDiagnostic(diagnostic) << '\n';
}
}
// 4) Several named outputs of different types, returned as GraphOutputs.
{
DslFilterGraph<std::string> analysis(R"dsl(
in -> Uppercase -> out.upper
in -> Length -> out.length
in -> MinLength(minLength=100) -> out.long
)dsl");
auto outputs = analysis.filter(std::string{"Hello, filterGraph!"});
std::cout << std::format("[outputs] upper={} length={} long={}\n",
*outputs->get<std::string>("upper"),
*outputs->get<std::size_t>("length"),
outputs->has("long") ? "present" : "dropped");
}
// 5) Problems are reported before anything runs: all of them at once, each
// located by line:column. Constructing a DslFilterGraph from this text
// would throw a GraphError carrying the same diagnostics.
{
const auto diagnostics = validateDslGraph<std::string, int>(
"in -> Uppercas -> shouted -> Print -> out\n"
"in -> MinLength -> long -> Print -> end\n");
for (const auto& diagnostic : diagnostics)
{
std::cout << "[check] " << dsl::formatDiagnostic(diagnostic) << '\n';
}
}
// 6) The JSON format is still supported and uses the same registry. This is
// the tap pipeline from (2), written with a Fanout stage.
{
auto pipelineConfig = nlohmann::json::parse(R"json([
{ "type": "Fanout", "config": { "branches": [
[ { "type": "Uppercase" }, { "type": "Print", "config": { "prefix": "[json tap] " } } ]
] } },
{ "type": "Reverse" },
{ "type": "MinLength", "config": { "minLength": 3 } },
{ "type": "Print", "config": { "prefix": "[json main] " } }
])json");
JsonFilterGraph<std::string, int> pipeline(pipelineConfig);
pipeline.filter(std::string{"Hello, filterGraph!"});
}
return 0;
}