Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BaseListeningAgent #793

Merged
merged 10 commits into from
Nov 20, 2024
36 changes: 35 additions & 1 deletion python/hyperon/exts/agents/agent_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,40 @@ def __next__(self):
raise StopIteration
return self._result.get()

class AgentObject:

class BaseListeningAgent:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't look like BaseListeningAgent will be used by itself. Why not create a ListeningAgent which inherits from AgentObject instead of inheriting AgentObject from BaseListeningAgent? I'm not sure if the functionality from BaseListeningAgent is always needed and will not disturb some uses of AgentObject. Inheriting ListeningAgent from AgentObject looks more flexible to me. Or am I missing something?

def __init__(self):
self.messages = Queue()
self.running = True

def start(self, *args):
if not args:
args = ()
st = StreamMethod(self.messages_processor, args)
st.start()

def message_processor(self, message, *args):
pass

def messages_processor(self, *args):
while self.running:
if not self.messages.empty():
m = self.messages.get()
self.output = []
res = self.message_processor(m, *args)
if isinstance(res, list):
self.output.extend(res)
else:
self.output.append(res)
return []

def stop(self):
self.running = False

def input(self, msg):
self.messages.put(msg)

class AgentObject(BaseListeningAgent):

'''
The base agent object class, which purpose is twofold.
Expand Down Expand Up @@ -92,6 +125,7 @@ def _try_unwrap(self, val):
return repr(val)

def __init__(self, path=None, atoms={}, include_paths=None, code=None):
super().__init__()
if path is None and code is None:
# purely Python agent
return
Expand Down
Loading