MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
RewriteOperator.cpp
Go to the documentation of this file.
1#include "RewriteOperator.hpp"
2
4
6
7RewriteOperator::Symbol::Symbol(SymbolId symbol_id, std::initializer_list<double> values)
8 : id(symbol_id)
9{
10 const auto count = std::min(values.size(), max_symbol_parameters);
11 std::ranges::copy_n(values.begin(), count, parameters.begin());
12
13 const auto stored_arity = values.size() > max_symbol_parameters
15 : count;
16
17 arity = static_cast<uint8_t>(stored_arity);
18}
19
20std::optional<double> RewriteOperator::Symbol::parameter(size_t index) const noexcept
21{
22 return arity <= max_symbol_parameters && index < arity
23 ? std::optional<double> { parameters[index] }
24 : std::nullopt;
25}
26
28 : m_max_word_length(config.max_word_length)
29 , m_max_generations(config.max_generations)
30 , m_valid(true)
31 , m_ignore(std::move(config.ignore))
32{
33 if (config.axiom.size() > m_max_word_length) {
34 MF_ERROR(Journal::Component::Nodes, Journal::Context::Configuration,
35 "RewriteOperator axiom has {} symbols, exceeding max_word_length {}",
36 config.axiom.size(), m_max_word_length);
37 m_valid = false;
38 }
39
40 if (std::ranges::any_of(config.axiom, [](const Symbol& symbol) {
41 return symbol.arity > max_symbol_parameters;
42 })) {
43 MF_ERROR(Journal::Component::Nodes, Journal::Context::Configuration,
44 "RewriteOperator axiom contains a symbol with more than {} parameters",
45 max_symbol_parameters);
46 m_valid = false;
47 }
48
49 m_axiom = std::make_shared<const Word>(Word {
50 .symbols = m_valid ? std::move(config.axiom) : std::vector<Symbol> {},
51 .generation = 0 });
52 m_draft = m_axiom;
53 m_active_word = m_axiom;
54 m_submitted = Submission { .word = m_axiom, .id = 0 };
55 m_active = Submission { .word = m_axiom, .id = 0 };
56}
57
58std::vector<RewriteOperator::Symbol> RewriteOperator::symbols_from_text(std::string_view text)
59{
60 std::vector<Symbol> symbols;
61 symbols.reserve(text.size());
62 for (const char character : text) {
63 symbols.emplace_back(static_cast<SymbolId>(static_cast<unsigned char>(character)));
64 }
65 return symbols;
66}
67
69{
70 if (!m_valid) {
71 return false;
72 }
73 if (!replacement.successor) {
75 "RewriteOperator ignored replacement without a successor");
76 return false;
77 }
78
79 m_replacements.push_back(std::move(replacement));
80 return true;
81}
82
83bool RewriteOperator::replace(SymbolId predecessor, Successor successor, Predicate predicate)
84{
85 return replace(Replacement {
86 .predecessor = predecessor,
87 .predicate = std::move(predicate),
88 .successor = std::move(successor) });
89}
90
92{
93 return [symbols = symbols_from_text(text)](const Symbol&, Environment, const EmitSymbol& emit) {
94 for (const auto& symbol : symbols) {
95 if (!emit(symbol)) {
96 return;
97 }
98 }
99 };
100}
101
102bool RewriteOperator::replace(SymbolId predecessor, std::string_view successor, double weight)
103{
104 return replace(Replacement {
105 .predecessor = predecessor,
106 .weight = weight,
107 .successor = text_successor(successor) });
108}
109
110bool RewriteOperator::matches_context(const Replacement& replacement, const Environment& environment) const
111{
112 const auto ignored = [this](const Symbol& symbol) {
113 return std::ranges::find(m_ignore, symbol.id) != m_ignore.end();
114 };
115
116 const auto word = environment.word;
117 size_t cursor = environment.position;
118 for (const auto expected : replacement.left | std::views::reverse) {
119 do {
120 if (cursor == 0) {
121 return false;
122 }
123 --cursor;
124 } while (ignored(word[cursor]));
125
126 if (word[cursor].id != expected) {
127 return false;
128 }
129 }
130
131 cursor = environment.position;
132 for (const auto expected : replacement.right) {
133 do {
134 ++cursor;
135 if (cursor >= word.size()) {
136 return false;
137 }
138 } while (ignored(word[cursor]));
139
140 if (word[cursor].id != expected) {
141 return false;
142 }
143 }
144
145 return true;
146}
147
149{
150 m_candidates.clear();
151 m_weights.clear();
152
153 for (const auto& replacement : m_replacements) {
154 if (replacement.predecessor != symbol.id
155 || !matches_context(replacement, environment)
156 || (replacement.predicate && !replacement.predicate(symbol, environment))) {
157 continue;
158 }
159
160 if (!(replacement.weight > 0.0)) {
161 if (m_candidates.empty()) {
162 return &replacement;
163 }
164 continue;
165 }
166
167 m_candidates.push_back(&replacement);
168 m_weights.push_back(replacement.weight);
169 }
170
171 if (m_candidates.empty()) {
172 return nullptr;
173 }
174 if (m_candidates.size() == 1) {
175 return m_candidates.front();
176 }
177
179 return m_candidates[std::min(pick, m_candidates.size() - 1)];
180}
181
182void RewriteOperator::set_parameter(std::string_view name, double value)
183{
185 if (!set_named_control(name, value)) {
187 "RewriteOperator ignored unknown parameter '{}'", name);
188 }
189}
190
191std::optional<double> RewriteOperator::query_state(std::string_view name) const
192{
193 return m_read ? m_read(name) : std::nullopt;
194}
195
197{
198 if (m_full.load(std::memory_order_acquire)) {
199 return false;
200 }
201
202 m_submission = std::move(submission);
203 m_full.store(true, std::memory_order_release);
204 return true;
205}
206
208{
209 if (!m_full.load(std::memory_order_acquire)) {
210 return {};
211 }
212
213 auto submission = std::move(m_submission);
214 m_full.store(false, std::memory_order_release);
215 return submission;
216}
217
219{
220 if (auto retired = m_retired.pop(); retired.word) {
222 }
223
225 && m_active_submission_id.load(std::memory_order_acquire) == m_submitted.id) {
227 m_pending = false;
228 m_retirement_received = false;
229 }
230}
231
232RewriteOperator::RewriteResult RewriteOperator::rewrite(uint64_t steps, std::span<const double> inputs)
233{
234 if (!m_valid) {
236 }
237 if (steps > m_max_generations - m_draft->generation) {
239 }
240 if (steps == 0) {
242 }
243
244 try {
246 const std::shared_ptr<const void> held = m_hold ? m_hold() : nullptr;
247 auto current = m_draft;
248
249 for (uint64_t step = 0; step < steps; ++step) {
250 const auto& [symbols, generation] = *current;
251 std::vector<Symbol> next_symbols;
252 next_symbols.reserve(std::min(m_max_word_length, symbols.size()));
253 bool overflow {};
254
255 const EmitSymbol emit = [this, &next_symbols, &overflow](Symbol next) {
256 if (next.arity > max_symbol_parameters || next_symbols.size() >= m_max_word_length) {
257 overflow = true;
258 return false;
259 }
260 next_symbols.push_back(next);
261 return true;
262 };
263
264 for (size_t position = 0; position < symbols.size(); ++position) {
265 const auto& symbol = symbols[position];
266 const Environment environment {
267 .inputs = inputs,
268 .word = symbols,
269 .position = position,
270 .held = held.get(),
271 .held_tag = m_held_tag
272 };
273
274 if (const auto* replacement = select_replacement(symbol, environment)) {
275 replacement->successor(symbol, environment, emit);
276 } else {
277 emit(symbol);
278 }
279
280 if (overflow) {
282 "RewriteOperator generation exceeds max_word_length or symbol parameter limit");
284 }
285 }
286
287 current = std::make_shared<const Word>(Word {
288 .symbols = std::move(next_symbols),
289 .generation = generation + 1 });
290 }
291
292 m_draft = std::move(current);
294
295 } catch (const std::exception& exception) {
296
298 "RewriteOperator replacement failed: {}", exception.what());
299 } catch (...) {
300
302 "RewriteOperator replacement failed with an unknown exception");
303 }
304
306}
307
309{
310 if (!m_valid) {
312 }
313
315 if (m_pending) {
316 return SubmitResult::BUSY;
317 }
318 if (m_next_submission_id == std::numeric_limits<uint64_t>::max()) {
320 "RewriteOperator submission ID exhausted");
322 }
323
324 Submission submission { .word = m_draft, .id = m_next_submission_id + 1 };
325 if (!m_ready.push(submission)) {
326 return SubmitResult::BUSY;
327 }
328
331 m_pending = true;
333}
334
335RewriteOperator::GrowResult RewriteOperator::grow(uint64_t steps, std::span<const double> inputs)
336{
337 if (!m_valid) {
339 }
340
342 if (m_pending) {
343 return GrowResult::BUSY;
344 }
345
346 switch (rewrite(steps, inputs)) {
348 break;
357 }
358
359 switch (submit()) {
361 return GrowResult::GROWN;
363 return GrowResult::BUSY;
367 break;
368 }
370}
371
372bool RewriteOperator::published() const noexcept
373{
374 return m_draft == m_submitted.word
375 && m_active_submission_id.load(std::memory_order_acquire) == m_submitted.id;
376}
377
379{
380 if (!m_valid) {
381 return false;
382 }
383
386 return true;
387}
388
389std::shared_ptr<const RewriteOperator::Word> RewriteOperator::active_word() const
390{
391 const auto id = m_active_submission_id.load(std::memory_order_acquire);
392 if (m_submitted.word && m_submitted.id == id) {
393 return m_submitted.word;
394 }
395 return m_active_word;
396}
397
399{
400 auto next = std::move(m_deferred);
401 if (!next.word) {
402 next = m_ready.pop();
403 if (!next.word) {
404 return;
405 }
406 }
407
408 if (!m_retired.push(m_active)) {
409 m_deferred = std::move(next);
410 return;
411 }
412
413 m_active = std::move(next);
414 m_active_generation.store(m_active.word->generation, std::memory_order_release);
415 m_active_submission_id.store(m_active.id, std::memory_order_release);
416}
417
418} // namespace MayaFlux::Nodes::Network
#define MF_ERROR(comp, ctx,...)
#define MF_WARN(comp, ctx,...)
static size_t weighted_pick(std::span< const double > weights, Stochastic &generator)
Weighted random draw over a row of nonnegative weights.
void refresh_controls()
Read node sources into their controls.
bool set_named_control(std::string_view name, double value)
Sets the control called name, the way set() would.
bool settle_named_control(std::string_view name)
Drops the pending typed value of the control called name.
bool restore_axiom()
Restore the axiom as the draft word on the producer lane, without submitting it.
GrowResult grow(uint64_t steps=1, std::span< const double > inputs={})
Rewrite the draft word and submit it, on the producer lane.
SubmitResult submit()
Submit the draft word; it becomes the active word at the next RelationNetwork block.
static Successor text_successor(std::string_view text)
A successor that emits the text's characters as parameterless symbols.
std::function< bool(Symbol)> EmitSymbol
Append a symbol; false on a bound violation.
std::shared_ptr< const Word > active_word() const
Return the latest active word on the producer lane.
std::function< std::shared_ptr< const void >()> m_hold
std::shared_ptr< const Word > m_active_word
RewriteResult rewrite(uint64_t steps=1, std::span< const double > inputs={})
Rewrite the draft word on the producer lane, without submitting it.
static std::vector< Symbol > symbols_from_text(std::string_view text)
One parameterless symbol per character, each character being its own id.
bool published() const noexcept
True once the word last submitted is the draft and is the active word.
std::function< std::optional< double >(std::string_view)> m_read
void set_parameter(std::string_view name, double value) override
Set the control called name; unknown names are logged and ignored.
std::vector< const Replacement * > m_candidates
bool matches_context(const Replacement &replacement, const Environment &environment) const
std::function< void(const Symbol &, Environment, const EmitSymbol &)> Successor
Kinesis::Stochastic::Stochastic m_generator
std::optional< double > query_state(std::string_view name) const override
The number held by the control called name, or nullopt for an unknown name.
const Replacement * select_replacement(const Symbol &symbol, const Environment &environment)
GrowResult
What grow() did; the first failure of its rewrite or its submit.
bool replace(Replacement replacement)
Append a rule; false when the operator is invalid or the successor is empty.
void process(float frames) override
Process for one batch cycle.
std::function< bool(const Symbol &, Environment)> Predicate
@ Configuration
Configuration and parameter updates.
@ NodeProcessing
Node graph processing (Nodes::NodeGraphManager).
@ Nodes
DSP Generator and Filter Nodes, graph pipeline, node management.
What a replacement sees: the caller's inputs, the word with the symbol's position in it,...
One rewrite rule: left < predecessor > right, if predicate, with weight.
std::array< double, max_symbol_parameters > parameters
std::optional< double > parameter(size_t index) const noexcept
Symbol(SymbolId id, std::initializer_list< double > values={})