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

Examples : refactroing #184

Merged
merged 6 commits into from
Jan 31, 2024
Merged
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
77 changes: 13 additions & 64 deletions examples/reference/abilities.jac
Original file line number Diff line number Diff line change
@@ -1,76 +1,25 @@
#Ability with decorators, access_modifiers
"""Calculator object with static method"""
obj Calculator{
#static ability(function)

static can :priv multiply(a :float, b :float) -> float{
return a*b;
}
}
with entry{
print(Calculator.multiply(9,-2));
}
# Animal Archetype with an abstract ability
obj Animal {
"""Abstract ability for making a sound."""
can make_sound -> str abs;
}

# Concrete obj representing a Dog
obj Dog:Animal: {
"""Overridden method to make a dog sound."""
can make_sound -> str {
return "Woof! Woof!";
can substract -> float abs;
can add(number:float,*a :tuple) -> str;
}
}

# Concrete obj representing a Cat
obj Cat:Animal: {
"""Overridden method to make a cat sound."""
can make_sound -> str {
return "Meow!";
obj substractor:Calculator:{
can substract(x:float,y:float)->float{
return(x-y);
}
}

# Ability to simulate interactions with animals
can interact_with_animal(animal: Animal) -> None {
sound = animal.make_sound();
print(f"The animal says: {sound}");
}
:obj:Calculator:can:add(number:float,*a :tuple) -> str {
return(number*sum(a));
}

# Usage
with entry{
# Creating instances of concrete architypes
Milo = Dog();
Leo = Cat();

# Interacting with animals
interact_with_animal(Milo);
interact_with_animal(Leo);
}
# Declaration
can greet(name: str) -> str;

# Definition after greet has been declared
:can:greet
(name: str) -> str {
print(f"Hey, {name} Welcome to Jaseci!");

}
with entry{
#ability calling
greet('Coder');

}
#Simple Ability
"""Ability(Function) to calculate the numbers"""
can add(*a :tuple) -> int {
return sum(a);
}

with entry{
#ability calling
print(add(9,-3,4));

cal = Calculator();
sub = substractor();
print(Calculator.multiply(9,-2));
print(cal.add(5,20,34,56));
print(sub.substract(9,-2));
}

72 changes: 17 additions & 55 deletions examples/reference/abilities.py
Original file line number Diff line number Diff line change
@@ -1,65 +1,27 @@
# Function with decorators, access_modifiers
class Calculator:
"""Calculator object with static method"""
from abc import ABC, abstractmethod


# static function
class Calculator:
@staticmethod
def multiply(a, b):
def multiply(a: float, b: float) -> float:
return a * b

@abstractmethod
def substract(self, x: float, y: float) -> float:
pass

print(Calculator.multiply(9, -2))


# Animal Archetype with an abstract ability
class Animal:
"""Abstract class for making a sound."""

def make_sound(self):
raise NotImplementedError("Abstract method 'make_sound' must be overridden")


# Concrete class representing a Dog
class Dog(Animal):
def make_sound(self):
return "Woof! Woof!"

def add(self, number: float, *a: tuple) -> str:
return str(number * sum(a))

# Concrete class representing a Cat
class Cat(Animal):
def make_sound(self):
return "Meow!"

class Substractor(Calculator):
def substract(self, x: float, y: float) -> float:
return x - y

# Ability to simulate interactions with animals
def interact_with_animal(animal):
sound = animal.make_sound()
print(f"The animal says: {sound}")

cal = Calculator()
sub = Substractor()

# Creating instances of concrete archetypes
Milo = Dog()
Leo = Cat()

# Interacting with animals
interact_with_animal(Milo)
interact_with_animal(Leo)


# Declaration & Definition in same block
def greet(name):
print(f"Hey, {name} Welcome to Jaseci!")


# fun calling
greet("Coder")


# Simple Function
def add(*a):
"""Ability(Function) to calculate the numbers"""
return sum(a)


# function calling
print(add(9, -3, 4))
print(Calculator.multiply(9, -2))
print(cal.add(5, 20, 34, 56))
print(sub.substract(9, -2))
1 change: 0 additions & 1 deletion examples/reference/assert_statements.jac
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
can foo(value : int){
assert value > 0, "Value must be positive";

}

with entry{
Expand Down
2 changes: 1 addition & 1 deletion examples/reference/atomic_pipe_back_expressions.jac
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ with entry {
print <: "Hello world!" ;
a = [2,4,5,7,8];
b = [4,8,9,13,20];
c = len<:a + len<:b ;
c = len<:a + b:>len;
print(c);

}
55 changes: 55 additions & 0 deletions examples/reference/connect_expressions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from jaclang.plugin.feature import JacFeature as jac


@jac.make_architype("node", on_entry=[], on_exit=[])
class node_a:
value: int


@jac.make_architype(
"walker",
on_entry=[
jac.DSFunc("create", jac.RootType),
jac.DSFunc("travel", jac.RootType | node_a),
],
on_exit=[],
)
class Creator:
def create(self, creator_here: jac.RootType) -> None:
end = creator_here
i = 0
while i < 7:
if i % 2 == 0:
jac.connect(
end,
(end := node_a(value=i)),
jac.build_edge(jac.EdgeDir.OUT, None, None),
)
else:
jac.connect(
end,
(end := node_a(value=i + 10)),
jac.build_edge(jac.EdgeDir.OUT, MyEdge, (("val",), (i,))),
)
i += 1

def travel(self, creator_here: jac.RootType | node_a) -> None:
for i in jac.edge_ref(
creator_here,
jac.EdgeDir.OUT,
MyEdge,
lambda x: [i for i in x if i.val <= 6],
):
print(i.value)
if jac.visit_node(
self, jac.edge_ref(creator_here, jac.EdgeDir.OUT, None, None)
):
pass


@jac.make_architype("edge", on_entry=[], on_exit=[])
class MyEdge:
val: int = jac.has_instance_default(gen_func=lambda: 5)


jac.spawn_call(jac.get_root(), Creator())
11 changes: 5 additions & 6 deletions examples/reference/data_spatial_calls.jac
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
walker walker_1 {
walker Creator {
can func2 with `<root> entry;
}

node node_1 {
has val: int;

can func_1 with walker_1 entry;
can func_1 with Creator entry;
}

:node:node_1:can:func_1 {
print("visiting ", <s>);
visit -->;
}

:walker:walker_1:can:func2 {
:walker:Creator:can:func2 {
end = <here>;
for i=0 to i<5 by i+=1 {
end ++> (end := node_1(val=i + 1));
Expand All @@ -22,6 +21,6 @@ node node_1 {
}

with entry {
<r> spawn |> walker_1;
<r> spawn :> walker_1;
<r> spawn :>Creator;
<r> spawn |>Creator;
}
20 changes: 11 additions & 9 deletions examples/reference/data_spatial_calls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@


@jac.make_architype("walker", on_entry=[jac.DSFunc("func2", jac.RootType)], on_exit=[])
class walker_1:
def func2(self, walker_here: jac.RootType) -> None:
end = walker_here
class Creator:
def func2(self, creator_here: jac.RootType) -> None:
end = creator_here
i = 0
while i < 5:
jac.connect(
Expand All @@ -14,19 +14,21 @@ def func2(self, walker_here: jac.RootType) -> None:
jac.build_edge(jac.EdgeDir.OUT, None, None),
)
i += 1
if jac.visit_node(self, jac.edge_ref(walker_here, jac.EdgeDir.OUT, None)):
if jac.visit_node(
self, jac.edge_ref(creator_here, jac.EdgeDir.OUT, None, None)
):
pass


@jac.make_architype("node", on_entry=[jac.DSFunc("func_1", walker_1)], on_exit=[])
@jac.make_architype("node", on_entry=[jac.DSFunc("func_1", Creator)], on_exit=[])
class node_1:
val: int

def func_1(self, node_here: walker_1) -> None:
def func_1(self, node_here: Creator) -> None:
print("visiting ", self)
if jac.visit_node(node_here, jac.edge_ref(self, jac.EdgeDir.OUT, None)):
if jac.visit_node(node_here, jac.edge_ref(self, jac.EdgeDir.OUT, None, None)):
pass


jac.spawn_call(jac.get_root(), walker_1())
jac.spawn_call(jac.get_root(), walker_1())
jac.spawn_call(jac.get_root(), Creator())
jac.spawn_call(jac.get_root(), Creator())
16 changes: 5 additions & 11 deletions examples/reference/data_spatial_spawn_expressions.jac
Original file line number Diff line number Diff line change
@@ -1,27 +1,21 @@
walker Adder{
can do with `<root> entry;
}

node node_a{
has x:int = 0;
has y:int = 0;
has x:int = 0,
y:int = 0;
can add with Adder entry;
}

:walker:Adder:can:do{
<here> ++> node_a();

visit -->;

}

:node:node_a:can:add{
<self>.x= 550 ;
<self>.y= 450 ;
print(int(<self>.x) + int(<self>.y));

print(int(<s>.x) + int(<s>.y));
}

with entry{
Adder() spawn <root>; # spawn will iniiate the walker Adder from root node
# spawn will iniiate the walker Adder from root node
Adder() spawn <root>;
}
10 changes: 5 additions & 5 deletions examples/reference/data_spatial_spawn_expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@

@jac.make_architype("walker", on_entry=[jac.DSFunc("do", jac.RootType)], on_exit=[])
class Adder:
def do(self, walker_here: jac.RootType) -> None:
jac.connect(walker_here, node_a(), jac.build_edge(jac.EdgeDir.OUT, None, None))
if jac.visit_node(self, jac.edge_ref(walker_here, jac.EdgeDir.OUT, None)):
def do(self, Adder_here: jac.RootType) -> None:
jac.connect(Adder_here, node_a(), jac.build_edge(jac.EdgeDir.OUT, None, None))
if jac.visit_node(self, jac.edge_ref(Adder_here, jac.EdgeDir.OUT, None, None)):
pass


@jac.make_architype("node", on_entry=[jac.DSFunc("add", Adder)], on_exit=[])
class node_a:
x: int = 0
y: int = 0
x: int = jac.has_instance_default(gen_func=lambda: 0)
y: int = jac.has_instance_default(gen_func=lambda: 0)

def add(self, node_here: Adder) -> None:
self.x = 550
Expand Down
Loading
Loading