74 lines
2.0 KiB
Python
74 lines
2.0 KiB
Python
from abc import ABC, abstractmethod
|
|
import xml.etree.ElementTree as ET
|
|
from typing import Callable, List
|
|
|
|
class Entry(ABC):
|
|
"""
|
|
Abstract base class for all entry types in the working memory.
|
|
Provides observable pattern functionality.
|
|
"""
|
|
|
|
def __init__(self, id: str):
|
|
"""
|
|
Initialize a new entry with provided id and timestamp.
|
|
|
|
Args:
|
|
id: Unique identifier for this entry
|
|
"""
|
|
self._id = id
|
|
self._change_handlers: List[Callable[['Entry'], None]] = []
|
|
|
|
@property
|
|
def id(self) -> str:
|
|
"""Get entry's unique identifier."""
|
|
return self._id
|
|
|
|
def add_change_handler(self, handler: Callable[['Entry'], None]) -> None:
|
|
"""Add a callback for entry changes."""
|
|
if handler not in self._change_handlers:
|
|
self._change_handlers.append(handler)
|
|
|
|
def notify_change(self) -> None:
|
|
"""Notify all handlers of entry state change."""
|
|
for handler in self._change_handlers:
|
|
handler(self)
|
|
|
|
@abstractmethod
|
|
def update(self) -> None:
|
|
"""
|
|
Update the entry's state.
|
|
Must be implemented by concrete classes.
|
|
"""
|
|
pass
|
|
|
|
@abstractmethod
|
|
def generate_context(self) -> ET.Element:
|
|
"""
|
|
Generate an XML Element representing this entry's context.
|
|
Must be implemented by concrete classes.
|
|
|
|
Returns:
|
|
ET.Element: XML element containing the entry's data
|
|
"""
|
|
pass
|
|
|
|
def serialize(self) -> dict[str, str]:
|
|
"""
|
|
Collect all public attributes of the entry into a dictionary.
|
|
"""
|
|
pass
|
|
|
|
def cleanup(self) -> None:
|
|
"""
|
|
Clean up any resources used by this entry.
|
|
Should be overridden by classes that need cleanup.
|
|
Default implementation does nothing.
|
|
"""
|
|
pass
|
|
|
|
def reset(self) -> None:
|
|
"""
|
|
Reset the entry as if update was never called.
|
|
Default implementation does nothing.
|
|
"""
|
|
pass |