Михаил Капелько 6 months ago
parent
commit
0116fed427
5 changed files with 196 additions and 2 deletions
  1. +52
    -1
      v4/ctx.h
  2. +3
    -1
      v4/ctx.py
  3. +79
    -0
      v4/ctx_test.py
  4. +53
    -0
      v4/ctx_test_Python.py
  5. +9
    -0
      v4/main.py

+ 52
- 1
v4/ctx.h View File

@@ -1,15 +1,66 @@
#include <list>
#include <string>
#include <vector>

#ifndef ctx_HEADER
#define ctx_HEADER

template <class T> class ctx_Controller {
T context;
std::list<void *(T)> callbacks;
std::list<T *(T)> functions;
bool isProcessingQueue = false;
std::list<T> 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 <typename U> 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

+ 3
- 1
v4/ctx.py View File

@@ -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):


+ 79
- 0
v4/ctx_test.py View File

@@ -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
#}

+ 53
- 0
v4/ctx_test_Python.py View File

@@ -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
#}
#}

+ 9
- 0
v4/main.py View File

@@ -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())


Loading…
Cancel
Save