From c4094d4a356756e8c5f76c27c70615285cc82a03 Mon Sep 17 00:00:00 2001 From: Andy Maloney Date: Sun, 10 Dec 2023 10:58:29 -0500 Subject: [PATCH] Clean up Python support files --- framework/ccm_pyactr/ccm_print.py | 16 +++++++- .../ccm_pyactr/gactar_ccm_activate_trace.py | 4 ++ framework/pyactr/pyactr_print.py | 39 ++++++++++++++++--- 3 files changed, 52 insertions(+), 7 deletions(-) diff --git a/framework/ccm_pyactr/ccm_print.py b/framework/ccm_pyactr/ccm_print.py index 9f39adb..7adb5ec 100644 --- a/framework/ccm_pyactr/ccm_print.py +++ b/framework/ccm_pyactr/ccm_print.py @@ -1,5 +1,14 @@ """ ccm_print adds some extra print capabilities to ccm productions. + +To use it, create an instance and regsiter each chunk with it: + + printer = CCMPrint() + printer.register_chunk("add", ["num1", "num2", "count", "sum"]) + printer.register_chunk("count", ["number", "next"]) + +Then in the productions: + printer.print_chunk_slot(goal, "goal", "sum") """ from typing import Dict, List @@ -8,6 +17,11 @@ class CCMPrint: + """ + CCMPrint provides methods to print the contents of a buffer or the contents + of one slot of a buffer. + """ + def __init__(self): self.chunk_map: Dict[str, List[str]] = {} @@ -42,6 +56,6 @@ def print_chunk_slot(self, buffer: Buffer, buffer_name: str, slot: str): slot_index = slot_list.index(slot) # +1 because the first item is the chunk type - item = buffer.__getitem__(slot_index + 1) + item = buffer[slot_index + 1] print(f"{buffer_name}.{slot}: {item}") diff --git a/framework/ccm_pyactr/gactar_ccm_activate_trace.py b/framework/ccm_pyactr/gactar_ccm_activate_trace.py index 91b234f..962f8bb 100644 --- a/framework/ccm_pyactr/gactar_ccm_activate_trace.py +++ b/framework/ccm_pyactr/gactar_ccm_activate_trace.py @@ -18,6 +18,10 @@ class foo(ACTR): class ActivateTrace(MemorySubModule): + """ + ActivateTrace provides a method to print a chunk when it is activated. + """ + def __init__(self, memory: Memory): MemorySubModule.__init__(self, memory) diff --git a/framework/pyactr/pyactr_print.py b/framework/pyactr/pyactr_print.py index e5a600a..a4d4ccb 100644 --- a/framework/pyactr/pyactr_print.py +++ b/framework/pyactr/pyactr_print.py @@ -43,9 +43,13 @@ class PrintBuffer(actr.buffers.Buffer): + """ + PrintBuffer provides methods to print data from productions. + """ + def __init__(self, model: actr.ACTRModel): actr.buffers.Buffer.__init__(self, None, None) - self.ACTR_MODEL = model + self.actr_model = model model._ACTRModel__buffers["print"] = self def text(self, *args): @@ -60,6 +64,7 @@ def text(self, *args): text "'a string'" text "42" """ + text = "".join(args[1:]).strip('"') output = "" # build up our output in this buffer @@ -91,6 +96,7 @@ def buffer(self, *args): buffer retrieval buffer retrieval.word """ + name = "".join(args) contents = self.get_buffer_data(name) print(f"{name}: {contents}") @@ -100,6 +106,7 @@ def get_buffer_data(self, item: str) -> str: Given an "item" which is either a or a ., return the contents. """ + ids = item.split(".") match len(ids): case 1: @@ -119,20 +126,23 @@ def get_buffer_contents(self, buffer_name: str) -> str: """ Gets all the contents of a buffer. """ + buffer = self.get_buffer(buffer_name) - data = buffer._data + data = buffer._data # pylint: disable=protected-access if data: return str(data.copy().pop()) - else: - return "" + + return "" def get_slot_contents(self, buffer_name: str, slot_name: str) -> str: """ Gets the contents of a specific buffer slot. """ + buffer = self.get_buffer(buffer_name) + # pylint: disable=protected-access if buffer._data: chunk = buffer._data.copy().pop() else: @@ -154,31 +164,48 @@ def get_buffer(self, buffer_name: str) -> Buffer: """ Gets a buffer by name and returns it. """ - if buffer_name in self.ACTR_MODEL._ACTRModel__buffers: - return self.ACTR_MODEL._ACTRModel__buffers[buffer_name] + + # pylint: disable=protected-access + if buffer_name in self.actr_model._ACTRModel__buffers: + return self.actr_model._ACTRModel__buffers[buffer_name] print("ERROR: Buffer '" + buffer_name + "' not found") raise KeyError def add(self, *args): + """ + (Required method for actr.buffers.Buffer class - throws an exception.) + """ raise AttributeError( "Attempt to add an element to the print buffer. This is not allowed." ) def clear(self, *args): + """ + (Required method for actr.buffers.Buffer class - throws an exception.) + """ raise AttributeError("Attempt to clear the print buffer. This is not allowed.") def create(self, *args): + """ + (Required method for actr.buffers.Buffer class - throws an exception.) + """ raise AttributeError( "Attempt to add an element to the print buffer. This is not allowed." ) def retrieve(self, *args): + """ + (Required method for actr.buffers.Buffer class - throws an exception.) + """ raise AttributeError( "Attempt to retrieve from the print buffer. This is not allowed." ) def test(self, *args): + """ + (Required method for actr.buffers.Buffer class - throws an exception.) + """ raise AttributeError( "Attempt to test the print buffer state. This is not allowed." )