Skip to content

Commit

Permalink
temporary
Browse files Browse the repository at this point in the history
  • Loading branch information
amadolid committed Sep 25, 2024
1 parent 5fdf10b commit 8f67d6c
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 33 deletions.
12 changes: 6 additions & 6 deletions jac/jaclang/plugin/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def check_read_access(to: Anchor) -> bool:
"""Read Access Validation."""
if not (access_level := Jac.check_access_level(to) > AccessLevel.NO_ACCESS):
logger.info(
f"Current root doesn't have read access to {to.__class__.__name__}[{to.id}]"
f"Current root doesn't have read access to {to.__class__.__name__}[{to.jid}]"
)
return access_level

Expand All @@ -116,7 +116,7 @@ def check_connect_access(to: Anchor) -> bool:
"""Write Access Validation."""
if not (access_level := Jac.check_access_level(to) > AccessLevel.READ):
logger.info(
f"Current root doesn't have connect access to {to.__class__.__name__}[{to.id}]"
f"Current root doesn't have connect access to {to.__class__.__name__}[{to.jid}]"
)
return access_level

Expand All @@ -126,7 +126,7 @@ def check_write_access(to: Anchor) -> bool:
"""Write Access Validation."""
if not (access_level := Jac.check_access_level(to) > AccessLevel.CONNECT):
logger.info(
f"Current root doesn't have write access to {to.__class__.__name__}[{to.id}]"
f"Current root doesn't have write access to {to.__class__.__name__}[{to.jid}]"
)
return access_level

Expand All @@ -138,13 +138,13 @@ def check_access_level(to: Anchor) -> AccessLevel:
return AccessLevel.WRITE

jctx = Jac.get_context()

jmem: ShelfStorage = jctx.mem
jroot = jctx.root

# if current root is system_root
# if current root id is equal to target anchor's root id
# if current root is the target anchor
if jroot == jctx.system_root or jroot.id == to.root or jroot == to:
if jroot == jctx.system_root or jroot.jid == to.root or jroot == to:
return AccessLevel.WRITE

access_level = AccessLevel.NO_ACCESS
Expand All @@ -155,7 +155,7 @@ def check_access_level(to: Anchor) -> AccessLevel:

# if target anchor's root have set allowed roots
# if current root is allowed to the whole graph of target anchor's root
if to.root and isinstance(to_root := jctx.mem.find_one(to.root), Anchor):
if to.root and isinstance(to_root := jmem.find_one(to.root), Anchor):
if to_root.access.all > access_level:
access_level = to_root.access.all

Expand Down
18 changes: 11 additions & 7 deletions jac/jaclang/runtimelib/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,36 @@
from typing import Callable, Optional, cast
from uuid import UUID

from .implementation import NodeAnchor, Root
from .implementation import JID, NodeAnchor, Root
from .interface import ExecutionContext as BaseExecutionContext
from .memory import ShelfStorage


EXECUTION_CONTEXT = ContextVar[Optional["ExecutionContext"]]("ExecutionContext")

SUPER_ROOT_UUID = UUID("00000000-0000-0000-0000-000000000000")
SUPER_ROOT_JID = JID[NodeAnchor](
id=UUID("00000000-0000-0000-0000-000000000000"), type=NodeAnchor
)
SUPER_ROOT_ARCHITYPE = object.__new__(Root)
SUPER_ROOT_ANCHOR = NodeAnchor(
id=SUPER_ROOT_UUID, architype=SUPER_ROOT_ARCHITYPE, persistent=False, edges=[]
jid=SUPER_ROOT_JID, architype=SUPER_ROOT_ARCHITYPE, persistent=False, edge_ids=set()
)
SUPER_ROOT_ARCHITYPE.__jac__ = SUPER_ROOT_ANCHOR


class ExecutionContext(BaseExecutionContext):
class ExecutionContext(BaseExecutionContext[NodeAnchor]):
"""Execution Context."""

mem: ShelfStorage

def init_anchor(
self,
anchor_id: str | None,
default: NodeAnchor,
) -> NodeAnchor:
"""Load initial anchors."""
if anchor_id:
if isinstance(anchor := self.mem.find_by_id(UUID(anchor_id)), NodeAnchor):
if isinstance(anchor := self.mem.find_by_id(JID(anchor_id)), NodeAnchor):
return anchor
raise ValueError(f"Invalid anchor id {anchor_id} !")
return default
Expand All @@ -58,10 +62,10 @@ def create(
ctx.reports = []

if not isinstance(
system_root := ctx.mem.find_by_id(SUPER_ROOT_UUID), NodeAnchor
system_root := ctx.mem.find_by_id(SUPER_ROOT_JID), NodeAnchor
):
system_root = Root().__jac__
system_root.id = SUPER_ROOT_UUID
system_root.id = SUPER_ROOT_JID
ctx.mem.set(system_root.id, system_root)

ctx.system_root = system_root
Expand Down
53 changes: 47 additions & 6 deletions jac/jaclang/runtimelib/implementation.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

from dataclasses import dataclass, field
from logging import getLogger
from typing import ClassVar, TypeAlias
from re import IGNORECASE, compile
from typing import Type, TypeAlias
from uuid import UUID, uuid4

from .interface import (
Expand All @@ -19,6 +20,13 @@
_ANCHOR,
)


JID_REGEX = compile(
r"^(n|e|w):([^:]*):([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})$",
IGNORECASE,
)


Anchor: TypeAlias = "NodeAnchor" | "EdgeAnchor" | "WalkerAnchor"
Architype: TypeAlias = "NodeArchitype" | "EdgeArchitype" | "WalkerArchitype"
logger = getLogger(__name__)
Expand All @@ -28,8 +36,41 @@
class JID(_JID[UUID, _ANCHOR]):
"""Jaclang Default JID."""

id: UUID = field(default_factory=uuid4)
name: str = ""
id: UUID
name: str

def __init__(
self: JID[Anchor],
id: str | UUID | None = None,
type: Type[Anchor] | None = None,
name: str = "",
) -> None:
"""Override JID initializer."""
match id:
case str():
if matched := JID_REGEX.search(id):
self.id = UUID(matched.group(3))
self.name = matched.group(2)
match matched.group(1).lower():
case "n":
self.type = NodeAnchor
case "e":
self.type = EdgeAnchor
case _:
self.type = WalkerAnchor
return
raise ValueError("Not a valid JID format!")
case UUID():
self.id = id
case None:
self.id = uuid4()
case _:
raise ValueError("Not a valid id for JID!")

if type is None:
raise ValueError("Type is required from non string JID!")
self.type = type
self.name = name

def __repr__(self) -> str:
"""Override string representation."""
Expand Down Expand Up @@ -111,7 +152,7 @@ def __deserialize__(cls, data: WalkerAnchor) -> WalkerAnchor:
class NodeArchitype(_NodeArchitype["NodeArchitype"]):
"""NodeArchitype Interface."""

__jac__: ClassVar[NodeAnchor]
__jac__: NodeAnchor

def __serialize__(self) -> NodeArchitype:
"""Override serialization."""
Expand All @@ -126,7 +167,7 @@ def __deserialize__(cls, data: NodeArchitype) -> NodeArchitype:
class EdgeArchitype(_EdgeArchitype["EdgeArchitype"]):
"""EdgeArchitype Interface."""

__jac__: ClassVar[EdgeAnchor]
__jac__: EdgeAnchor

def __serialize__(self) -> EdgeArchitype:
"""Override serialization."""
Expand All @@ -141,7 +182,7 @@ def __deserialize__(cls, data: EdgeArchitype) -> EdgeArchitype:
class WalkerArchitype(_WalkerArchitype["WalkerArchitype"]):
"""Walker Architype Interface."""

__jac__: ClassVar[WalkerAnchor]
__jac__: WalkerAnchor

def __serialize__(self) -> WalkerArchitype:
"""Override serialization."""
Expand Down
28 changes: 14 additions & 14 deletions jac/jaclang/runtimelib/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
from dataclasses import dataclass, field
from enum import IntEnum
from types import UnionType
from typing import Any, Callable, ClassVar, Generator, Generic, Iterable, Type, TypeVar
from typing import Any, Callable, Generator, Generic, Iterable, Type, TypeVar


_ID = TypeVar("_ID")
_ANCHOR = TypeVar("_ANCHOR", bound="Anchor")
_NODE_ANCHOR = TypeVar("_NODE_ANCHOR", bound="NodeAnchor")

_SERIALIZE = TypeVar("_SERIALIZE")
_DESERIALIZE = TypeVar("_DESERIALIZE")


#########################################################################################
# ID / ACCESS #
#########################################################################################
Expand Down Expand Up @@ -125,7 +125,7 @@ class WalkerAnchor(Anchor[_SERIALIZE]):
class Architype(Generic[_SERIALIZE], ABC):
"""Architype Interface."""

__jac__: ClassVar[Anchor]
__jac__: Anchor

@abstractmethod
def __serialize__(self) -> _SERIALIZE:
Expand All @@ -140,19 +140,19 @@ def __deserialize__(cls: Type[_DESERIALIZE], data: _SERIALIZE) -> _DESERIALIZE:
class NodeArchitype(Architype[_SERIALIZE]):
"""NodeArchitype Interface."""

__jac__: ClassVar[NodeAnchor]
__jac__: NodeAnchor


class EdgeArchitype(Architype[_SERIALIZE]):
"""EdgeArchitype Interface."""

__jac__: ClassVar[EdgeAnchor]
__jac__: EdgeAnchor


class WalkerArchitype(Architype[_SERIALIZE]):
"""Walker Architype Interface."""

__jac__: ClassVar[WalkerAnchor]
__jac__: WalkerAnchor


@dataclass(kw_only=True)
Expand Down Expand Up @@ -241,24 +241,24 @@ def remove(self, ids: _ID | Iterable[_ID]) -> None:
#########################################################################################


class ExecutionContext(ABC):
class ExecutionContext(Generic[_NODE_ANCHOR], ABC):
"""Execution Context."""

mem: Memory
reports: list[Any]
system_root: NodeAnchor
root: NodeAnchor
entry_node: NodeAnchor
system_root: _NODE_ANCHOR
root: _NODE_ANCHOR
entry_node: _NODE_ANCHOR

@abstractmethod
def init_anchor(
self,
anchor_id: str | None,
default: NodeAnchor,
) -> NodeAnchor:
anchor_jid: JID | None,
default: _NODE_ANCHOR,
) -> _NODE_ANCHOR:
"""Load initial anchors."""

def set_entry_node(self, entry_node: str | None) -> None:
def set_entry_node(self, entry_node: JID | None) -> None:
"""Override entry."""
self.entry_node = self.init_anchor(entry_node, self.root)

Expand Down

0 comments on commit 8f67d6c

Please sign in to comment.