nk 1.0.0
C++ Numerical Computing Runtime
Loading...
Searching...
No Matches
reactive_session.hpp
1
32
33#pragma once
34
35#include <nk/core/ast.hpp>
36#include <nk/core/dependency_graph.hpp>
37#include <nk/core/types.hpp>
39
40#include <cstdint>
41#include <memory>
42#include <set>
43#include <string>
44#include <vector>
45
46namespace nk {
47
48class Engine;
49
51{
52public:
55 explicit ReactiveSession(Engine &engine);
56
61 bool load(const std::string &source);
62
66 void setParam(const std::string &var, Value value);
67
71 void setParamElement(const std::string &var, size_t r, size_t c,
72 Value value);
73
77 size_t evalDirty();
78
80 bool hasDirty() const { return !dirty_.empty(); }
81
84 std::vector<std::vector<size_t>> detectCycles() const;
85
86 const DependencyGraph &graph() const { return *graph_; }
87 size_t statementCount() const { return statements_.size(); }
88 const std::string &lastError() const { return lastError_; }
89
92 uint64_t generation() const { return generation_; }
93
94private:
95 void markDirty(const std::vector<size_t> &indices);
96 Value runStatement(size_t index);
97
98 Engine &engine_;
99 std::shared_ptr<const std::string> src_;
100 std::vector<ASTNodePtr> statements_; // program children (owned)
101 std::unique_ptr<DependencyGraph> graph_;
102 // Figure-state rollback points, one per statement (captured after
103 // that statement ran). evalDirty restores the checkpoint of the
104 // last CLEAN statement before the dirty set, so re-run graphics
105 // statements reproduce the full-re-run figure (no `hold on`
106 // line accumulation across ticks).
107 std::vector<FigureManager::FigureCheckpoint> checkpoints_;
108 // Figure state captured in load() BEFORE the first statement runs
109 // (may hold a PREVIOUS script's figures — sessions share the
110 // engine). The rollback target when a dirty set starts at 0.
112 std::set<size_t> dirty_;
113 std::set<std::string> params_; // externally-set variables
114 std::string lastError_;
115 uint64_t generation_ = 0;
116};
117
118} // namespace nk
Dependency DAG over statement indices.
size_t evalDirty()
Execute the dirty statements in source order through the per-statement seam.
std::vector< std::vector< size_t > > detectCycles() const
Tarjan SCCs over the dependency edges (invariant guard — expected empty while edges stay forward-only...
bool load(const std::string &source)
Parse source, compute facts + DAG, run the initial FULL pass (whole-source eval — script/function sem...
ReactiveSession(Engine &engine)
The engine is NOT owned; the session must outlive uses of it with that engine (workspace state is sha...
uint64_t generation() const
Bumped on every committed write (param/element/evalDirty) — the buffer-generation source for zero-cop...
void setParamElement(const std::string &var, size_t r, size_t c, Value value)
Single-cell write (the reactive table editor).
void setParam(const std::string &var, Value value)
Whole-variable parameter write (Play sliders).
bool hasDirty() const
True when something is pending for evalDirty().
Core MATLAB-compatible multidimensional array and scalar value type.
Definition value.hpp:61