core: proc_std, create action: basically working
This commit is contained in:
parent
965543252f
commit
717ce25372
4 changed files with 29 additions and 13 deletions
|
|
@ -95,9 +95,17 @@ class Scope:
|
||||||
def create_scope(env: Mailbox, cat: Categ = (k.csys, k.c00), **kw) -> Scope:
|
def create_scope(env: Mailbox, cat: Categ = (k.csys, k.c00), **kw) -> Scope:
|
||||||
return Scope(environ=env, categ=cat, **kw)
|
return Scope(environ=env, categ=cat, **kw)
|
||||||
|
|
||||||
def create_zero(env: Mailbox) -> Mailbox:
|
def clone_scope(scope: Scope, **kw) -> Scope:
|
||||||
scope = create_scope(env)
|
value = kw.get('value', 0)
|
||||||
|
categ = kw.get('categ', scope.categ)
|
||||||
|
stage = kw.get('stage', k.initial)
|
||||||
|
return Scope(environ=scope.environ, value=value, categ=categ, stage=stage,
|
||||||
|
program=scope.program, syns=[], **kw)
|
||||||
|
|
||||||
|
async def create_zero(env: Mailbox) -> Mailbox:
|
||||||
|
scope = create_scope(env, program=gen_program({}, default=std_proc()))
|
||||||
zero = neuron(scope)
|
zero = neuron(scope)
|
||||||
|
await notify_created(Message(k.csys), zero, scope)
|
||||||
return zero
|
return zero
|
||||||
|
|
||||||
# predefined action handlers and Action sets
|
# predefined action handlers and Action sets
|
||||||
|
|
@ -115,6 +123,8 @@ def value_add() -> Handler:
|
||||||
|
|
||||||
def create() -> Handler:
|
def create() -> Handler:
|
||||||
async def handle(msg, scope) -> None:
|
async def handle(msg, scope) -> None:
|
||||||
|
new = neuron(clone_scope(scope, **msg.data))
|
||||||
|
await notify_created(msg, new, scope)
|
||||||
return None
|
return None
|
||||||
return handle
|
return handle
|
||||||
|
|
||||||
|
|
@ -127,8 +137,15 @@ def basic_actions() -> Actions:
|
||||||
|
|
||||||
# predefined specialized procs
|
# predefined specialized procs
|
||||||
|
|
||||||
def std_proc(actions: Actions | None = None, finalize=forward(), **more) -> Proc:
|
def std_proc(actions: Actions | None = None, finalize: Handler = forward(),
|
||||||
|
more: Actions = {}) -> Proc:
|
||||||
if actions is None:
|
if actions is None:
|
||||||
actions = basic_actions() | util.keyword_dict(more)
|
actions = basic_actions() | more
|
||||||
return gen_proc(actions, finalize=finalize)
|
return gen_proc(actions, finalize=finalize)
|
||||||
|
|
||||||
|
# helpers
|
||||||
|
|
||||||
|
async def notify_created(msg: Message, new: Mailbox, scope: Scope) -> None:
|
||||||
|
msg = Message(msg.head['domain'], k.created, data=msg.data | {k.new: new})
|
||||||
|
await scope.environ.put(msg)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -25,9 +25,6 @@ class Symbols:
|
||||||
|
|
||||||
keywords = Symbols()
|
keywords = Symbols()
|
||||||
|
|
||||||
def keyword_dict(d):
|
|
||||||
return dict((keywords(key), val) for key, val in d.items())
|
|
||||||
|
|
||||||
# list / iterable utilities
|
# list / iterable utilities
|
||||||
|
|
||||||
def flatten(*it, ignore_none=False):
|
def flatten(*it, ignore_none=False):
|
||||||
|
|
|
||||||
|
|
@ -67,12 +67,15 @@ async def meta_env(collector):
|
||||||
@pytest.mark.anyio
|
@pytest.mark.anyio
|
||||||
async def test_core(meta_env, collector):
|
async def test_core(meta_env, collector):
|
||||||
env = environ.create(meta_env)
|
env = environ.create(meta_env)
|
||||||
zero = core.create_zero(env)
|
zero = await core.create_zero(env)
|
||||||
await env.put(Message(k.env, k.hello))
|
await env.put(Message(k.env, k.hello))
|
||||||
await zero.put(Message(k.csys, k.hello))
|
await zero.put(Message(k.csys, k.hello))
|
||||||
|
await zero.put(Message(k.csys, k.create))
|
||||||
await asyncio.sleep(0.01)
|
await asyncio.sleep(0.01)
|
||||||
assert len(collector) == 2
|
assert len(collector) == 4
|
||||||
assert collector[0].domain is k.env
|
assert collector[0].action is k.created
|
||||||
assert collector[1].domain is k.csys
|
assert collector[1].domain is k.env
|
||||||
|
assert collector[2].domain is k.csys
|
||||||
|
assert collector[3].action is k.created
|
||||||
#print(collector)
|
#print(collector)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from scopes.util import keywords as k, keyword_dict, flatten
|
from scopes.util import keywords as k, flatten
|
||||||
|
|
||||||
def test_keywords():
|
def test_keywords():
|
||||||
assert k.dummy is k.dummy
|
assert k.dummy is k.dummy
|
||||||
|
|
@ -12,7 +12,6 @@ def test_keywords():
|
||||||
assert getattr(k, "abcd"[-2:]) is k.cd
|
assert getattr(k, "abcd"[-2:]) is k.cd
|
||||||
assert k('dummy') is k.dummy
|
assert k('dummy') is k.dummy
|
||||||
assert k('new') is k('new')
|
assert k('new') is k('new')
|
||||||
assert keyword_dict(dict(one=1, two=2)) == {k.one: 1, k.two: 2}
|
|
||||||
|
|
||||||
def test_flatten():
|
def test_flatten():
|
||||||
lst = (1, (None, 2), (3, (4, 5)))
|
lst = (1, (None, 2), (3, (4, 5)))
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue