11 std::ranges::copy_n(values.begin(), count,
parameters.begin());
17 arity =
static_cast<uint8_t
>(stored_arity);
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);
40 if (std::ranges::any_of(config.
axiom, [](
const Symbol& symbol) {
41 return symbol.arity > max_symbol_parameters;
43 MF_ERROR(Journal::Component::Nodes, Journal::Context::Configuration,
44 "RewriteOperator axiom contains a symbol with more than {} parameters",
45 max_symbol_parameters);
50 .symbols = m_valid ? std::move(config.axiom) : std::vector<Symbol> {},
53 m_active_word = m_axiom;
54 m_submitted = Submission { .word = m_axiom, .id = 0 };
55 m_active = Submission { .word = m_axiom, .id = 0 };
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)));
75 "RewriteOperator ignored replacement without a successor");
86 .predecessor = predecessor,
87 .predicate = std::move(predicate),
88 .successor = std::move(successor) });
94 for (
const auto& symbol : symbols) {
105 .predecessor = predecessor,
112 const auto ignored = [
this](
const Symbol& symbol) {
116 const auto word = environment.
word;
117 size_t cursor = environment.
position;
118 for (
const auto expected : replacement.
left | std::views::reverse) {
124 }
while (ignored(word[cursor]));
126 if (word[cursor].
id != expected) {
132 for (
const auto expected : replacement.
right) {
135 if (cursor >= word.size()) {
138 }
while (ignored(word[cursor]));
140 if (word[cursor].
id != expected) {
154 if (replacement.predecessor != symbol.
id
156 || (replacement.predicate && !replacement.predicate(symbol, environment))) {
160 if (!(replacement.weight > 0.0)) {
187 "RewriteOperator ignored unknown parameter '{}'", name);
198 if (
m_full.load(std::memory_order_acquire)) {
203 m_full.store(
true, std::memory_order_release);
209 if (!
m_full.load(std::memory_order_acquire)) {
214 m_full.store(
false, std::memory_order_release);
220 if (
auto retired =
m_retired.pop(); retired.word) {
246 const std::shared_ptr<const void> held =
m_hold ?
m_hold() :
nullptr;
249 for (uint64_t step = 0; step < steps; ++step) {
250 const auto& [symbols, generation] = *current;
251 std::vector<Symbol> next_symbols;
260 next_symbols.push_back(next);
264 for (
size_t position = 0; position < symbols.size(); ++position) {
265 const auto& symbol = symbols[position];
269 .position = position,
275 replacement->successor(symbol, environment, emit);
282 "RewriteOperator generation exceeds max_word_length or symbol parameter limit");
287 current = std::make_shared<const Word>(
Word {
288 .symbols = std::move(next_symbols),
289 .generation = generation + 1 });
295 }
catch (
const std::exception& exception) {
298 "RewriteOperator replacement failed: {}", exception.what());
302 "RewriteOperator replacement failed with an unknown exception");
320 "RewriteOperator submission ID exhausted");
325 if (!
m_ready.push(submission)) {
346 switch (
rewrite(steps, inputs)) {
#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.
std::atomic< bool > m_full
bool push(Submission &submission) noexcept
Submission pop() noexcept
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::atomic< uint64_t > m_active_submission_id
bool m_retirement_received
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.
SubmissionMailbox m_retired
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.
SubmissionMailbox m_ready
std::atomic< uint64_t > m_active_generation
bool published() const noexcept
True once the word last submitted is the draft and is the active word.
static constexpr size_t max_symbol_parameters
uint64_t m_max_generations
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
std::vector< Replacement > m_replacements
std::vector< SymbolId > m_ignore
bool matches_context(const Replacement &replacement, const Environment &environment) const
std::vector< double > m_weights
std::function< void(const Symbol &, Environment, const EmitSymbol &)> Successor
uint64_t m_next_submission_id
RewriteOperator(Config config)
std::shared_ptr< const Word > m_draft
std::shared_ptr< const Word > m_axiom
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.
std::vector< Symbol > axiom
std::span< const Symbol > word
What a replacement sees: the caller's inputs, the word with the symbol's position in it,...
std::vector< SymbolId > right
std::vector< SymbolId > left
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={})