Skip to content
This repository has been archived by the owner on Sep 12, 2024. It is now read-only.

[INPROGRESS][ABSTRACTION]: Initial #611

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions jaclang/runtimelib/implementation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
"""Jaclang Runtimelib Implementation."""

from __future__ import annotations

from dataclasses import dataclass, field
from uuid import UUID, uuid4
from typing import Type

from .interface import (
JID as _JID,
Anchor as _Anchor,
_ANCHOR,
NodeAnchor as _NodeAnchor,
EdgeAnchor as _EdgeAnchor,
WalkerAnchor as _WalkerAnchor,
Architype as _Architype,
NodeArchitype as _NodeArchitype,
EdgeArchitype as _EdgeArchitype,
WalkerArchitype as _WalkerArchitype,
)


@dataclass
class JID(_JID[UUID, _ANCHOR]):
"""Jaclang Default JID."""

id: UUID
type: Type[_ANCHOR]

@property
def anchor(self) -> _ANCHOR | None:
"""Get anchor by id and type."""
return None

def __repr__(self) -> str:
"""Override string representation."""
return f"{self.type.__class__.__name__[:1].lower()}:{self.name}:{self.id}"

def __str__(self) -> str:
"""Override string parsing."""
return f"{self.type.__class__.__name__[:1].lower()}:{self.name}:{self.id}"


@dataclass
class NodeAnchor(_NodeAnchor[JID]):
"""NodeAnchor Interface."""

edge_ids: list[JID["EdgeAnchor"]]


@dataclass
class EdgeAnchor(_EdgeAnchor[JID]):
"""NodeAnchor Interface."""

source_id: JID["NodeAnchor"]
target_id: JID["NodeAnchor"]
104 changes: 104 additions & 0 deletions jaclang/runtimelib/interface.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
"""Jaclang Runtimelib interfaces."""

from abc import ABC, abstractmethod
from dataclasses import dataclass
from typing import Generic, Iterable, Type, TypeVar

_ID = TypeVar("_ID")
_ANCHOR = TypeVar("_ANCHOR", bound="Anchor")
_ARCHITYPE = TypeVar("_ARCHITYPE", bound="Architype")

_BASE_ANCHOR = TypeVar("_BASE_ANCHOR", bound="Anchor")

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


@dataclass(eq=False, repr=False, kw_only=True)
class JID(Generic[_ID, _ANCHOR], ABC):
"""Jaclang ID Interface."""

id: _ID
type: Type[_ANCHOR]
name: str

@property
@abstractmethod
def anchor(self) -> _ANCHOR | None:
"""Get anchor by id and type."""

@abstractmethod
def __repr__(self) -> str:
"""Override string representation."""

@abstractmethod
def __str__(self) -> str:
"""Override string parsing."""

@abstractmethod
def __eq__(self, value: object) -> bool:
"""Override equal operation."""


@dataclass
class Anchor(Generic[_ID, _ARCHITYPE, _SERIALIZE], ABC):
"""Anchor Interface."""

jid: JID[_ID, "Anchor"]
architype: _ARCHITYPE

@abstractmethod
def __serialize__(self) -> _SERIALIZE:
"""Override string representation."""

@classmethod
@abstractmethod
def __deserialize__(cls: Type[_DESERIALIZE], data: _SERIALIZE) -> _DESERIALIZE:
"""Override string parsing."""


@dataclass
class NodeAnchor(Anchor[_ID, "NodeArchitype", _SERIALIZE]):
"""NodeAnchor Interface."""

edge_ids: Iterable[JID[_ID, "EdgeAnchor"]]


@dataclass
class EdgeAnchor(Anchor[_ID, "EdgeArchitype", _SERIALIZE]):
"""EdgeAnchor Interface."""

source_id: JID[_ID, "NodeAnchor"]
target_id: JID[_ID, "NodeAnchor"]


@dataclass
class WalkerAnchor(Anchor[_ID, "WalkerArchitype", _SERIALIZE]):
"""WalkerAnchor Interface."""


class Architype(Generic[_BASE_ANCHOR, _SERIALIZE], ABC):
"""Architype Interface."""

__jac__: _BASE_ANCHOR

@abstractmethod
def __serialize__(self) -> _SERIALIZE:
"""Override string representation."""

@classmethod
@abstractmethod
def __deserialize__(cls: Type[_DESERIALIZE], data: _SERIALIZE) -> _DESERIALIZE:
"""Override string parsing."""


class NodeArchitype(Architype[NodeAnchor, _SERIALIZE]):
"""NodeArchitype Interface."""


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


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