Skip to content

Commit

Permalink
temporary
Browse files Browse the repository at this point in the history
  • Loading branch information
amadolid committed Nov 29, 2024
1 parent 0cc0339 commit 38f4752
Show file tree
Hide file tree
Showing 13 changed files with 163 additions and 123 deletions.
2 changes: 1 addition & 1 deletion jac/jaclang/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ def get_object(
data = {}
obj = Jac.get_object(id)
if obj:
data = obj.__jac__.__getstate__()
data = obj.__jac__
else:
print(f"Object with id {id} not found.", file=sys.stderr)

Expand Down
66 changes: 34 additions & 32 deletions jac/jaclang/plugin/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from functools import wraps
from logging import getLogger
from typing import Any, Callable, Mapping, Optional, Sequence, Type, Union, cast
from uuid import UUID

from jaclang.compiler.constant import colors
from jaclang.compiler.semtable import SemInfo, SemRegistry, SemScope
Expand All @@ -38,6 +37,7 @@
)
from jaclang.runtimelib.constructs import (
GenericEdge,
JacLangJID,
JacTestCheck,
)
from jaclang.runtimelib.importer import ImportPathSpec, JacImporter, PythonImporter
Expand All @@ -58,13 +58,14 @@ class JacCallableImplementation:
@staticmethod
def get_object(id: str | JID) -> Architype | None:
"""Get object by id."""
jctx = Jac.get_context()
if id == "root":
return Jac.get_context().root.architype
return jctx.root.architype

if isinstance(id, str):
id = JID(id)
id = JacLangJID(id)

if obj := Jac.get_context().mem.find_by_id(id):
if obj := id.anchor:
return obj.architype

return None
Expand All @@ -83,26 +84,25 @@ def elevate_root() -> None:
@staticmethod
@hookimpl
def allow_root(
architype: Architype, root_id: UUID, level: AccessLevel | int | str
architype: Architype, root_id: JID, level: AccessLevel | int | str
) -> None:
"""Allow all access from target root graph to current Architype."""
level = AccessLevel.cast(level)
access = architype.__jac__.access.roots

_root_id = str(root_id)
if level != access.anchors.get(_root_id, AccessLevel.NO_ACCESS):
access.anchors[_root_id] = level
if level != access.anchors.get(root_id, AccessLevel.NO_ACCESS):
access.anchors[root_id] = level

@staticmethod
@hookimpl
def disallow_root(
architype: Architype, root_id: UUID, level: AccessLevel | int | str
architype: Architype, root_id: JID, level: AccessLevel | int | str
) -> None:
"""Disallow all access from target root graph to current Architype."""
level = AccessLevel.cast(level)
access = architype.__jac__.access.roots

access.anchors.pop(str(root_id), None)
access.anchors.pop(root_id, None)

@staticmethod
@hookimpl
Expand Down Expand Up @@ -165,7 +165,7 @@ def check_access_level(to: Anchor) -> AccessLevel:
# 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 @@ -176,17 +176,17 @@ 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 (to_root := to.root.anchor):
if to_root.access.all > access_level:
access_level = to_root.access.all

level = to_root.access.roots.check(str(jroot.id))
level = to_root.access.roots.check(jroot.jid)
if level > AccessLevel.NO_ACCESS and access_level == AccessLevel.NO_ACCESS:
access_level = level

# if target anchor have set allowed roots
# if current root is allowed to target anchor
level = to_access.roots.check(str(jroot.id))
level = to_access.roots.check(jroot.jid)
if level > AccessLevel.NO_ACCESS and access_level == AccessLevel.NO_ACCESS:
access_level = level

Expand Down Expand Up @@ -231,9 +231,10 @@ def get_edges(
) -> list[EdgeArchitype]:
"""Get edges connected to this node."""
ret_edges: list[EdgeArchitype] = []
for anchor in Jac.get_context().mem.find(node.edges):
for jid in node.edges:
if (
(source := anchor.source.anchor)
(anchor := jid.anchor)
and (source := anchor.source.anchor)
and (target := anchor.target.anchor)
and (not filter_func or filter_func([anchor.architype]))
):
Expand Down Expand Up @@ -263,9 +264,10 @@ def edges_to_nodes(
) -> list[NodeArchitype]:
"""Get set of nodes connected to this node."""
ret_edges: list[NodeArchitype] = []
for anchor in Jac.get_context().mem.find(node.edges):
for jid in node.edges:
if (
(source := anchor.source.anchor)
(anchor := jid.anchor)
and (source := anchor.source.anchor)
and (target := anchor.target.anchor)
and (not filter_func or filter_func([anchor.architype]))
):
Expand All @@ -290,7 +292,7 @@ def edges_to_nodes(
def remove_edge(node: NodeAnchor, edge: EdgeAnchor) -> None:
"""Remove reference without checking sync status."""
for idx, ed in enumerate(node.edges):
if ed == edge.id:
if ed == edge.jid:
node.edges.pop(idx)
break

Expand Down Expand Up @@ -609,10 +611,10 @@ def reset_graph(root: Optional[Root] = None) -> int:
if isinstance(anchors := mem.__shelf__, Shelf)
else mem.__mem__.values()
):
if anchor == ranchor or anchor.root != ranchor.id:
if anchor == ranchor or anchor.root != ranchor.jid:
continue

if loaded_anchor := mem.find_by_id(anchor.id):
if loaded_anchor := mem.find_by_id(anchor.jid):
deleted_count += 1
Jac.destroy(loaded_anchor)

Expand All @@ -628,7 +630,7 @@ def get_object_func() -> Callable[[str | JID], Architype | None]:
@hookimpl
def object_ref(obj: Architype) -> JID:
"""Get object's id."""
return obj.__jac__.id
return obj.__jac__.jid

@staticmethod
@hookimpl
Expand Down Expand Up @@ -995,9 +997,10 @@ def disconnect(

for i in left:
node = i.__jac__
for anchor in Jac.get_context().mem.find(node.edges):
for jid in set(node.edges):
if (
(source := anchor.source.anchor)
(anchor := jid.anchor)
and (source := anchor.source.anchor)
and (target := anchor.target.anchor)
and (not filter_func or filter_func([anchor.architype]))
):
Expand All @@ -1017,7 +1020,6 @@ def disconnect(
):
Jac.destroy(anchor) if anchor.persistent else Jac.detach(anchor)
disconnect_occurred = True

return disconnect_occurred

@staticmethod
Expand Down Expand Up @@ -1059,12 +1061,12 @@ def builder(source: NodeAnchor, target: NodeAnchor) -> EdgeArchitype:

eanch = edge.__jac__ = EdgeAnchor(
architype=edge,
source=source.id,
target=target.id,
source=source.jid,
target=target.jid,
is_undirected=is_undirected,
)
source.edges.append(eanch.id)
target.edges.append(eanch.id)
source.edges.append(eanch.jid)
target.edges.append(eanch.jid)

if conn_assign:
for fld, val in zip(conn_assign[0], conn_assign[1]):
Expand All @@ -1089,9 +1091,9 @@ def save(obj: Architype | Anchor) -> None:
jctx = Jac.get_context()

anchor.persistent = True
anchor.root = jctx.root.id
anchor.root = jctx.root.jid

jctx.mem.set(anchor.id, anchor)
jctx.mem.set(anchor.jid, anchor)

@staticmethod
@hookimpl
Expand All @@ -1110,7 +1112,7 @@ def destroy(obj: Architype | Anchor) -> None:
case _:
pass

Jac.get_context().mem.remove(anchor.id)
Jac.get_context().mem.remove(anchor.jid)

@staticmethod
@hookimpl
Expand Down
8 changes: 4 additions & 4 deletions jac/jaclang/plugin/feature.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
TypeAlias,
Union,
)
from uuid import UUID

from jaclang.plugin.spec import (
AccessLevel,
Expand All @@ -37,6 +36,7 @@
ast,
plugin_manager,
)
from jaclang.runtimelib.constructs import ObjectArchitype


class JacAccessValidation:
Expand All @@ -50,7 +50,7 @@ def elevate_root() -> None:
@staticmethod
def allow_root(
architype: Architype,
root_id: UUID,
root_id: JID,
level: AccessLevel | int | str = AccessLevel.READ,
) -> None:
"""Allow all access from target root graph to current Architype."""
Expand All @@ -61,7 +61,7 @@ def allow_root(
@staticmethod
def disallow_root(
architype: Architype,
root_id: UUID,
root_id: JID,
level: AccessLevel | int | str = AccessLevel.READ,
) -> None:
"""Disallow all access from target root graph to current Architype."""
Expand Down Expand Up @@ -197,7 +197,7 @@ class JacClassReferences:
EdgeDir: ClassVar[TypeAlias] = EdgeDir
DSFunc: ClassVar[TypeAlias] = DSFunc
RootType: ClassVar[TypeAlias] = Root
Obj: ClassVar[TypeAlias] = Architype
Obj: ClassVar[TypeAlias] = ObjectArchitype
Node: ClassVar[TypeAlias] = NodeArchitype
Edge: ClassVar[TypeAlias] = EdgeArchitype
Walker: ClassVar[TypeAlias] = WalkerArchitype
Expand Down
5 changes: 2 additions & 3 deletions jac/jaclang/plugin/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
TypeVar,
Union,
)
from uuid import UUID

from jaclang.compiler import absyntree as ast
from jaclang.compiler.constant import EdgeDir
Expand Down Expand Up @@ -56,15 +55,15 @@ def elevate_root() -> None:
@staticmethod
@hookspec(firstresult=True)
def allow_root(
architype: Architype, root_id: UUID, level: AccessLevel | int | str
architype: Architype, root_id: JID, level: AccessLevel | int | str
) -> None:
"""Allow all access from target root graph to current Architype."""
raise NotImplementedError

@staticmethod
@hookspec(firstresult=True)
def disallow_root(
architype: Architype, root_id: UUID, level: AccessLevel | int | str
architype: Architype, root_id: JID, level: AccessLevel | int | str
) -> None:
"""Disallow all access from target root graph to current Architype."""
raise NotImplementedError
Expand Down
17 changes: 8 additions & 9 deletions jac/jaclang/plugin/tests/fixtures/other_root_access.jac
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import:py from jaclang.runtimelib.architype {Anchor}
import:py from uuid {UUID}
import:py from jaclang.runtimelib.architype {Anchor, JacLangJID}

node A {
has val: int;
Expand Down Expand Up @@ -48,15 +47,15 @@ walker create_node {
can enter with `root entry {
a = A(val=self.val);
here ++> a;
print(a.__jac__.id);
print(jid(a));
}
}

walker create_other_root {
can enter with `root entry {
other_root = `root().__jac__;
other_root = `root();
Jac.save(other_root);
print(other_root.id);
print(jid(other_root));
}
}

Expand All @@ -67,15 +66,15 @@ walker allow_other_root_access {
if self.via_all {
Jac.unrestrict(here, self.level);
} else {
Jac.allow_root(here, UUID(self.root_id), self.level);
Jac.allow_root(here, JacLangJID(self.root_id), self.level);
}
}

can enter_nested with A entry {
if self.via_all {
Jac.unrestrict(here, self.level);
} else {
Jac.allow_root(here, UUID(self.root_id), self.level);
Jac.allow_root(here, JacLangJID(self.root_id), self.level);
}
}
}
Expand All @@ -87,15 +86,15 @@ walker disallow_other_root_access {
if self.via_all {
Jac.restrict(here);
} else {
Jac.disallow_root(here, UUID(self.root_id));
Jac.disallow_root(here, JacLangJID(self.root_id));
}
}

can enter_nested with A entry {
if self.via_all {
Jac.restrict(here);
} else {
Jac.disallow_root(here, UUID(self.root_id));
Jac.disallow_root(here, JacLangJID(self.root_id));
}
}
}
Loading

0 comments on commit 38f4752

Please sign in to comment.