core: creation of simple zero neuron
This commit is contained in:
parent
ed48076e67
commit
4b3af802cd
3 changed files with 45 additions and 28 deletions
|
|
@ -43,12 +43,19 @@ class Scope:
|
|||
return self
|
||||
|
||||
def create_scope(env: Mailbox, cat=(k.csys, k.c00), **kw) -> Scope:
|
||||
scope = Scope(environ=env, categ=cat, **kw)
|
||||
return scope.set_proc()
|
||||
return Scope(environ=env, categ=cat, **kw)
|
||||
|
||||
def neuron(scope: Scope) -> Mailbox:
|
||||
async def process(msg: Message) -> actor.Behavior | None:
|
||||
nonlocal scope
|
||||
scope = await scope.proc(scope, msg)
|
||||
return None if scope is None else process
|
||||
new_scope = await scope.proc(scope, msg)
|
||||
if new_scope is not None:
|
||||
scope = new_scope
|
||||
return process
|
||||
return actor.create(process)
|
||||
|
||||
def create_zero(env: Mailbox) -> Mailbox:
|
||||
scope = create_scope(env)
|
||||
zero = neuron(scope)
|
||||
return zero
|
||||
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ from scopes.util import keywords as k
|
|||
|
||||
async def proc_env(scope, msg):
|
||||
await scope.environ.put(msg)
|
||||
#return scope
|
||||
|
||||
def create(meta_env):
|
||||
scope = core.create_scope(meta_env, (k.env, k.c00), program=lambda x: proc_env)
|
||||
|
|
|
|||
|
|
@ -10,29 +10,35 @@ from scopes.core import actor, environ
|
|||
from scopes.core.message import Message
|
||||
from scopes.util import keywords as k
|
||||
|
||||
collector = []
|
||||
|
||||
def show(msg):
|
||||
collector.append(msg)
|
||||
@pytest.fixture
|
||||
def collector():
|
||||
return []
|
||||
|
||||
|
||||
async def handle_basic(msg):
|
||||
match msg:
|
||||
case k.show: show(msg)
|
||||
case k.switch: return handle_alt
|
||||
case _: show(msg)
|
||||
def show(msg, coll):
|
||||
coll.append(msg)
|
||||
|
||||
async def handle_alt(msg):
|
||||
match msg:
|
||||
case k.show: show('switched')
|
||||
case _: await asyncio.sleep(0.01); show(msg)
|
||||
def handle_basic(coll):
|
||||
async def handle(msg):
|
||||
match msg:
|
||||
case k.show: show(msg, coll)
|
||||
case k.switch: return handle_alt(coll)
|
||||
case _: show(msg, coll)
|
||||
return handle
|
||||
|
||||
def handle_alt(coll):
|
||||
async def handle(msg):
|
||||
match msg:
|
||||
case k.show: show('switched', coll)
|
||||
case _: await asyncio.sleep(0.01); show(msg, coll)
|
||||
return handle
|
||||
|
||||
|
||||
@pytest.mark.anyio
|
||||
async def test_actor():
|
||||
async def test_actor(collector):
|
||||
async with asyncio.TaskGroup() as group:
|
||||
q1 = actor.create(handle_basic, group)
|
||||
q2 = actor.create(handle_basic, group)
|
||||
q1 = actor.create(handle_basic(collector), group)
|
||||
q2 = actor.create(handle_basic(collector), group)
|
||||
msgs = [Message(k.test, k.hello), k.show, k.switch, k.show, "actor 1: Goodbye"]
|
||||
await asyncio.gather(*[q1.put(m) for m in msgs])
|
||||
await q2.put("actor 2")
|
||||
|
|
@ -53,19 +59,22 @@ def test_message():
|
|||
|
||||
|
||||
@pytest.fixture
|
||||
def coll():
|
||||
return []
|
||||
|
||||
@pytest.fixture
|
||||
async def meta_env(coll):
|
||||
async def meta_env(collector):
|
||||
async def collect(msg):
|
||||
coll.append(msg)
|
||||
collector.append(msg)
|
||||
return actor.create(collect)
|
||||
|
||||
@pytest.mark.anyio
|
||||
async def test_core(meta_env, coll):
|
||||
async def test_core(meta_env, collector):
|
||||
env = environ.create(meta_env)
|
||||
await env.put(Message(k.env, k.hello))
|
||||
await asyncio.sleep(0.01)
|
||||
print(coll)
|
||||
assert len(collector) == 1
|
||||
assert collector[0].domain is k.env
|
||||
zero = core.create_zero(env)
|
||||
await zero.put(Message(k.csys, k.hello))
|
||||
await asyncio.sleep(0.01)
|
||||
assert len(collector) == 2
|
||||
assert collector[1].domain is k.csys
|
||||
#print(collector)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue