From 0116fed427e68a082fd5acd102fa9630f408fdae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB=20=D0=9A=D0=B0=D0=BF?= =?UTF-8?q?=D0=B5=D0=BB=D1=8C=D0=BA=D0=BE?= Date: Thu, 30 May 2024 22:47:43 +0300 Subject: [PATCH] d --- v4/ctx.h | 53 ++++++++++++++++++++++++++++- v4/ctx.py | 4 ++- v4/ctx_test.py | 79 +++++++++++++++++++++++++++++++++++++++++++ v4/ctx_test_Python.py | 53 +++++++++++++++++++++++++++++ v4/main.py | 9 +++++ 5 files changed, 196 insertions(+), 2 deletions(-) create mode 100644 v4/ctx_test.py create mode 100644 v4/ctx_test_Python.py diff --git a/v4/ctx.h b/v4/ctx.h index 907225f..019dbe7 100644 --- a/v4/ctx.h +++ b/v4/ctx.h @@ -1,15 +1,66 @@ +#include #include -#include #ifndef ctx_HEADER #define ctx_HEADER template class ctx_Controller { T context; + std::list callbacks; + std::list functions; + bool isProcessingQueue = false; + std::list queue; ctx_Controller(const T &c) { context = c; } + + void executeFunctions() { + T c = queue.front(); + queue.pop_front(); + for (int i = 0; i < functions.size(); ++i) { + T ctx = functions[i](c); + if (ctx.recentField != "none") { + queue.push_back(ctx); + } + } + context = c; + reportContext(); + } + + void processQueue() { + // Decline recursion. + if (isProcessingQueue) { + return; + } + isProcessingQueue = true; + while (!queue.empty()) { + executeFunctions(); + } + isProcessingQueue = false; + } + + void registerCallback(void (*cb)(T)) { + callbacks.push_back(cb); + } + + void registerFunction(T (*f)(T)) { + functions.push_back(f); + } + + void reportContext() { + for (int i = 0; i < callbacks.size(); ++i) { + callbacks[i](context); + } + } + + template void set(const std::string &fieldName, const U &value) { + T c = context; + c.setField(fieldName, value); + c.recentField = fieldName; + queue.push_back(c); + processQueue(); + } }; #endif // ctx_HEADER diff --git a/v4/ctx.py b/v4/ctx.py index 76ad2cb..5ca624d 100644 --- a/v4/ctx.py +++ b/v4/ctx.py @@ -14,7 +14,9 @@ class ctx_Controller: ctx = f(c) if ctx.recentField != "none": self.queue.append(ctx) - self.context = c + + self.context.recentField = c.recentField + self.context.setField(c.recentField, c.field(c.recentField)) self.reportContext() def processQueue(self): diff --git a/v4/ctx_test.py b/v4/ctx_test.py new file mode 100644 index 0000000..2179714 --- /dev/null +++ b/v4/ctx_test.py @@ -0,0 +1,79 @@ +from ctx import * +from memory import * +from memory_Context import * + +def ctx_test_Controller_executeFunctions_set( +) -> str: + c = memory_createContext() + ctrl = ctx_Controller(c) + # Disable automatic invocation of executeFunctions. + ctrl.isProcessingQueue = True + ctrl.set("input", "123") + ctrl.registerFunction(__processInput) + # Apply 'input'. + ctrl.executeFunctions() + # Apply 'outputHelp'. + ctrl.executeFunctions() + if ( + c.input == "123" and + c.outputHelp == "Checked" + ): + return "OK: ctx_Controller_executeFunctions_set" + #} + return "ERR: ctx_Controller_executeFunctions_set" +#} + +def ctx_test_Controller_processQueue( +) -> str: + c = memory_createContext() + ctrl = ctx_Controller(c) + ctrl.registerFunction(__processInput) + ctrl.set("input", "abc"); + if ( + c.input == "abc" and + c.outputHelp == "Checked" + ): + return "OK: ctx_Controller_processQueue" + #} + return "ERR: ctx_Controller_processQueue" +#} + +def ctx_test_memoryContext_field( +) -> str: + c = memory_createContext() + c.input = "abc" + if ( + c.field("input") == "abc" + ): + return "OK: ctx_memoryContext_field" + #} + return "ERR: ctx_memoryContext_field" +#} + +def ctx_test_memoryContext_setField( +) -> str: + c = memory_createContext() + c.input = "abc" + c.setField("input", "123") + if ( + c.field("input") == "123" + ): + return "OK: ctx_memoryContext_setField" + #} + return "ERR: ctx_memoryContext_setField" +#} + +# Auxiliary functions. + +@llm_by_value +def __processInput(c): + if ( + c.recentField == "input" + ): + c.outputHelp = "Checked" + c.recentField = "outputHelp" + return c + #} + c.recentField = "none" + return c +#} diff --git a/v4/ctx_test_Python.py b/v4/ctx_test_Python.py new file mode 100644 index 0000000..ac0ec6f --- /dev/null +++ b/v4/ctx_test_Python.py @@ -0,0 +1,53 @@ +from ctx import * +from memory import * +from memory_Context import * + +def ctx_test_Python_Controller_registerFieldCallback_match( +) -> str: + c = memory_createContext() + ctrl = ctx_Controller(c) + c.input = "123" + c.recentField = "input" + globals()["__callbackInput"] = "" + ctrl.registerFieldCallback("input", __setCallbackInput) + ctrl.reportContext() + val = globals()["__callbackInput"] + if ( + c.input == globals()["__callbackInput"] + ): + return "OK: ctx_Python_Controller_registerFieldCallback_match" + #} + return f"ERR: ctx_Python_Controller_registerFieldCallback_match" +#} + +def ctx_test_Python_Controller_registerFieldCallback_mismatch( +) -> str: + c = memory_createContext() + ctrl = ctx_Controller(c) + c.input = "123" + c.outputHelp = "you" + c.recentField = "outputHelp" + globals()["__callbackInput"] = "" + ctrl.registerFieldCallback("input", __setCallbackInput) + ctrl.reportContext() + val = globals()["__callbackInput"] + if ( + globals()["__callbackInput"] == "" + ): + return "OK: ctx_Python_Controller_registerFieldCallback_mismatch" + #} + return f"ERR: ctx_Python_Controller_registerFieldCallback_mismatch" +#} + +# Auxiliary. + +__callbackInput = "" + +@llm_by_value +def __setCallbackInput(c): + if ( + c.recentField == "input" + ): + globals()["__callbackInput"] = c.input + #} +#} diff --git a/v4/main.py b/v4/main.py index c97f108..d6fbc4f 100644 --- a/v4/main.py +++ b/v4/main.py @@ -1,11 +1,20 @@ from cli import * from cli_test import * from ctx import * +from ctx_test import * +from ctx_test_Python import * from llm_test import * from llm_test_Python import * from memory_test import * import sys +print(ctx_test_Controller_executeFunctions_set()) +print(ctx_test_Controller_processQueue()) +print(ctx_test_Python_Controller_registerFieldCallback_match()) +print(ctx_test_Python_Controller_registerFieldCallback_mismatch()) +print(ctx_test_memoryContext_field()) +print(ctx_test_memoryContext_setField()) + print(llm_test_Python_copyByValue()) print(llm_test_isDigit_digit()) print(llm_test_isDigit_notDigit())