core, work in progress: scope creation, environment setup
This commit is contained in:
parent
48a5105eea
commit
c1bff58ad3
3 changed files with 41 additions and 7 deletions
|
|
@ -1,6 +1,6 @@
|
||||||
# scopes.core.core
|
# scopes.core.core
|
||||||
|
|
||||||
"""The scopes.core package"""
|
"""A concurrent cybernetic communication system"""
|
||||||
|
|
||||||
from __future__ import annotations # no check of type annotations at runtime
|
from __future__ import annotations # no check of type annotations at runtime
|
||||||
import asyncio
|
import asyncio
|
||||||
|
|
@ -11,6 +11,8 @@ from typing import Any
|
||||||
from scopes.core.message import Message
|
from scopes.core.message import Message
|
||||||
from scopes.util import keywords as k, Symbol
|
from scopes.util import keywords as k, Symbol
|
||||||
|
|
||||||
|
type Mailbox = asyncio.Queue
|
||||||
|
type Categ = tuple[Symbol, Symbol]
|
||||||
type Proc = Callable[[Scope, Message], AsyncGenerator[Scope]]
|
type Proc = Callable[[Scope, Message], AsyncGenerator[Scope]]
|
||||||
type Program = Callable[[Scope], Proc]
|
type Program = Callable[[Scope], Proc]
|
||||||
type Syn = Callable[[Message], Message]
|
type Syn = Callable[[Message], Message]
|
||||||
|
|
@ -22,13 +24,18 @@ async def proc_dummy(scope: Scope, msg: Message) -> AsyncGenerator[Scope]:
|
||||||
class Scope:
|
class Scope:
|
||||||
|
|
||||||
value: Any = 0
|
value: Any = 0
|
||||||
categ: tuple[Symbol, Symbol] = (k.csys, k.c00)
|
categ: Categ = (k.csys, k.c00)
|
||||||
stage: Symbol = k.initial
|
stage: Symbol = k.initial
|
||||||
proc: Proc = proc_dummy
|
proc: Proc = proc_dummy
|
||||||
program: Program = lambda x: proc_dummy
|
program: Program = lambda x: proc_dummy
|
||||||
syns: list[Syn] = field(default_factory=list)
|
syns: list[Syn] = field(default_factory=list)
|
||||||
environ: asyncio.Queue = field(default_factory=asyncio.Queue)
|
environ: Mailbox = field(default_factory=asyncio.Queue)
|
||||||
|
|
||||||
def set_proc(self) -> Scope:
|
def set_proc(self) -> Scope:
|
||||||
self.proc = self.program(self)
|
self.proc = self.program(self)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
|
def create_scope(env: Mailbox, cat=(k.csys, k.c00), **kw):
|
||||||
|
scope = Scope(environ=env, categ=cat, **kw)
|
||||||
|
return scope.set_proc()
|
||||||
|
|
||||||
|
|
|
||||||
14
scopes/core/environ.py
Normal file
14
scopes/core/environ.py
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
# scopes.core.environ
|
||||||
|
|
||||||
|
"""Defining an environment for the communication system"""
|
||||||
|
|
||||||
|
from scopes import core
|
||||||
|
from scopes.core import actor
|
||||||
|
from scopes.util import keywords as k
|
||||||
|
|
||||||
|
async def proc_env(scope, msg):
|
||||||
|
await scope.environ.put(msg)
|
||||||
|
|
||||||
|
def create(meta_env):
|
||||||
|
return core.create_scope(meta_env, (k.env, k.c00), program=lambda x: proc_env)
|
||||||
|
|
||||||
|
|
@ -6,7 +6,7 @@ import asyncio
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from scopes import core
|
from scopes import core
|
||||||
from scopes.core import actor, message
|
from scopes.core import actor, environ, message
|
||||||
from scopes.util import keywords as k
|
from scopes.util import keywords as k
|
||||||
|
|
||||||
collector = []
|
collector = []
|
||||||
|
|
@ -26,6 +26,7 @@ async def handle_alt(msg):
|
||||||
case k.show: show('switched')
|
case k.show: show('switched')
|
||||||
case _: await asyncio.sleep(0.01); show(msg)
|
case _: await asyncio.sleep(0.01); show(msg)
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.anyio
|
@pytest.mark.anyio
|
||||||
async def test_actor():
|
async def test_actor():
|
||||||
async with asyncio.TaskGroup() as group:
|
async with asyncio.TaskGroup() as group:
|
||||||
|
|
@ -53,7 +54,19 @@ def test_message():
|
||||||
assert msg.categ() == (k.test, k.person)
|
assert msg.categ() == (k.test, k.person)
|
||||||
|
|
||||||
|
|
||||||
def test_core():
|
@pytest.fixture
|
||||||
sc = core.Scope()
|
def coll():
|
||||||
assert sc.value == 0
|
return []
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
async def meta_env(coll):
|
||||||
|
async def collect(msg):
|
||||||
|
coll.append(msg)
|
||||||
|
return actor.create(collect)
|
||||||
|
|
||||||
|
@pytest.mark.anyio
|
||||||
|
async def test_core(meta_env, coll):
|
||||||
|
env_scope = environ.create(meta_env)
|
||||||
|
assert env_scope.value == 0
|
||||||
|
await asyncio.sleep(0.01)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue