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
|
return self
|
||||||
|
|
||||||
def create_scope(env: Mailbox, cat=(k.csys, k.c00), **kw) -> Scope:
|
def create_scope(env: Mailbox, cat=(k.csys, k.c00), **kw) -> Scope:
|
||||||
scope = Scope(environ=env, categ=cat, **kw)
|
return Scope(environ=env, categ=cat, **kw)
|
||||||
return scope.set_proc()
|
|
||||||
|
|
||||||
def neuron(scope: Scope) -> Mailbox:
|
def neuron(scope: Scope) -> Mailbox:
|
||||||
async def process(msg: Message) -> actor.Behavior | None:
|
async def process(msg: Message) -> actor.Behavior | None:
|
||||||
nonlocal scope
|
nonlocal scope
|
||||||
scope = await scope.proc(scope, msg)
|
new_scope = await scope.proc(scope, msg)
|
||||||
return None if scope is None else process
|
if new_scope is not None:
|
||||||
|
scope = new_scope
|
||||||
|
return process
|
||||||
return actor.create(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):
|
async def proc_env(scope, msg):
|
||||||
await scope.environ.put(msg)
|
await scope.environ.put(msg)
|
||||||
|
#return scope
|
||||||
|
|
||||||
def create(meta_env):
|
def create(meta_env):
|
||||||
scope = core.create_scope(meta_env, (k.env, k.c00), program=lambda x: proc_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.core.message import Message
|
||||||
from scopes.util import keywords as k
|
from scopes.util import keywords as k
|
||||||
|
|
||||||
collector = []
|
@pytest.fixture
|
||||||
|
def collector():
|
||||||
def show(msg):
|
return []
|
||||||
collector.append(msg)
|
|
||||||
|
|
||||||
|
|
||||||
async def handle_basic(msg):
|
def show(msg, coll):
|
||||||
match msg:
|
coll.append(msg)
|
||||||
case k.show: show(msg)
|
|
||||||
case k.switch: return handle_alt
|
|
||||||
case _: show(msg)
|
|
||||||
|
|
||||||
async def handle_alt(msg):
|
def handle_basic(coll):
|
||||||
match msg:
|
async def handle(msg):
|
||||||
case k.show: show('switched')
|
match msg:
|
||||||
case _: await asyncio.sleep(0.01); show(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
|
@pytest.mark.anyio
|
||||||
async def test_actor():
|
async def test_actor(collector):
|
||||||
async with asyncio.TaskGroup() as group:
|
async with asyncio.TaskGroup() as group:
|
||||||
q1 = actor.create(handle_basic, group)
|
q1 = actor.create(handle_basic(collector), group)
|
||||||
q2 = actor.create(handle_basic, group)
|
q2 = actor.create(handle_basic(collector), group)
|
||||||
msgs = [Message(k.test, k.hello), k.show, k.switch, k.show, "actor 1: Goodbye"]
|
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 asyncio.gather(*[q1.put(m) for m in msgs])
|
||||||
await q2.put("actor 2")
|
await q2.put("actor 2")
|
||||||
|
|
@ -53,19 +59,22 @@ def test_message():
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def coll():
|
async def meta_env(collector):
|
||||||
return []
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
async def meta_env(coll):
|
|
||||||
async def collect(msg):
|
async def collect(msg):
|
||||||
coll.append(msg)
|
collector.append(msg)
|
||||||
return actor.create(collect)
|
return actor.create(collect)
|
||||||
|
|
||||||
@pytest.mark.anyio
|
@pytest.mark.anyio
|
||||||
async def test_core(meta_env, coll):
|
async def test_core(meta_env, collector):
|
||||||
env = environ.create(meta_env)
|
env = environ.create(meta_env)
|
||||||
await env.put(Message(k.env, k.hello))
|
await env.put(Message(k.env, k.hello))
|
||||||
await asyncio.sleep(0.01)
|
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