Skip to content

Commit

Permalink
Clean up Python support files
Browse files Browse the repository at this point in the history
  • Loading branch information
asmaloney committed Dec 10, 2023
1 parent 42b2cc3 commit c4094d4
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 7 deletions.
16 changes: 15 additions & 1 deletion framework/ccm_pyactr/ccm_print.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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]] = {}

Expand Down Expand Up @@ -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}")
4 changes: 4 additions & 0 deletions framework/ccm_pyactr/gactar_ccm_activate_trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
39 changes: 33 additions & 6 deletions framework/pyactr/pyactr_print.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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

Expand Down Expand Up @@ -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}")
Expand All @@ -100,6 +106,7 @@ def get_buffer_data(self, item: str) -> str:
Given an "item" which is either a <buffer name> or a <buffer name>.<slot name>,
return the contents.
"""

ids = item.split(".")
match len(ids):
case 1:
Expand All @@ -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 "<empty>"

return "<empty>"

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:
Expand All @@ -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."
)

0 comments on commit c4094d4

Please sign in to comment.