Skip to content

Examples

Node Creation and Connections

Nodes can be connected in various ways, including from a single node to a list of nodes, a list of nodes to a single node, a list of nodes to another list of nodes, or even a single node to a single node

node MyNode{}

with entry{
    first_tier =[MyNode() for i in range(2)];
    second_tier =[MyNode() for i in range(2)];

    root ++> first_tier;
    first_tier ++> second_tier;

    end_tier = MyNode();
    second_tier ++> end_tier;


}
Graph Image

Image

Custom Edge Creation and Operations

We can establish connections between nodes or lists of nodes using custom edges instead of the default generic edges, allowing for more control and customization in the relationships between nodes.

node MyNode{
    has val:int =0;
}

edge a{}

edge b{}

edge c{}

with entry{
    Start = MyNode(5);
    root +:a:+> Start;
    Start +:b:+> MyNode(10) +:c:+> MyNode(15);
    Start +:b:+> MyNode(20) +:a:+> MyNode(25);

    print([root-->]);
    print([root<--]);
    print([root-:a:->]);
    print([root-:a:-> -:b:->]);
    print([root-:a:-> -:b:->-:c:->]);

}

Lines 17-21 demonstrate how to retrieve visitable or reachable nodes from a given node by applying specific edge conditions, such as filtering based on edge types or chaining multiple edge traversals.

Graph Image

Image

Filtering

Filtering Based on Edge Types and Attributes

We can filter nodes based on specific edge attributes, such as filtering by edge values to retrieve a subset of connected nodes.

node A{
    has val:int =0;
}

edge a{
    has val:int = 0;
}
edge b{
    has val:int = 0;
}

with entry{

    root +:a:val=10:+> A(10);
    root +:a:val=20:+> A(20);
    root +:b:val=30:+> A(30);

    print([root--> ]);
    print([root-:a:-> ]);
    print([root-:a:val<=15:-> ]);
    print([root-:a:val>=20:-> ]);
}
Graph Image

Image

Filtering Based on Node Types and Attributes

We can filter specific types of nodes from a list of visitable nodes based on their type, and further apply conditions on node attributes to refine the results.

node A{
    has val:int =0;
}
node B{
    has val:int =0;
}
node C{
    has val:int =0;
}

edge a{}

edge b{}

edge c{}

with entry{
    Start = A(5);
    Intermediate = B(10);
    End = C(25);
    root +:a:+> Start;
    Start +:b:+> Intermediate +:c:+> C(15);
    Start +:b:+> A(20) +:a:+> End;
    Intermediate +:c:+> End;

    print([root-->-:b:->(`?A)]);
    print([root-:a:-> -:b:-> -:a:->(`?C)]);
    print([root-:a:-> -:b:-> -:c:->(`?C)]);
    print([root-:a:-> -:b:-> -:c:->(?val==25)]);
    print([root-:a:-> -:b:-> -:c:->(?val<20)]);
}
Graph Image

Image

Visiting

We can retrieve all visitable nodes from a node in specific directions.

node MyNode{
    has Name:str;
}

edge a{}


with entry{
    Start = MyNode("Start");
    End = MyNode("End");
    root <+:a:+ Start;
    root +:a:+> End;

    print([root-->]);
    print([root<--]);
    print([Start-->]);
    print([End<--]);


}
Graph Image

Image

DS Entry-Exit

node test_node {
    has value: int;
}

walker test_walker {
    has visited_nodes: list = [];
    has entry_count: int = 0;
    has exit_count: int = 0;

    can traverse with `root entry {
        visit [-->](`?test_node);
    }

    can log_entry with entry {
        print("Entering at the beginning of walker: ",here);
        self.entry_count += 1;
    }

    can log_visit with test_node exit {
        print("Visiting node : ", here);
        self.visited_nodes.append(here);
    }

    can log_exit with exit {
        print("Exiting at the end of walker: ",here);
        self.exit_count += 1;
    }
}

with entry {
    for i in range(10) {
        root ++> (next:=test_node(value=i));
    }
    wlk_obj = root spawn test_walker();
    print(wlk_obj);
}
Output
Entering at the beginning of walker:  Root()
Visiting node :  test_node(value=1)
Visiting node :  test_node(value=0)
Exiting at the end of walker:  test_node(value=0)
test_walker(visited_nodes=[test_node(value=1), test_node(value=0)], entry_count=1, exit_count=1)

can log_entry with entry

  • This ability is triggered once when the walker is spawned. It is essentially the "entry point" of the walker’s operation. In the example, it logs the entry at the beginning, increments entry_count by 1, and prints the node where the walker starts (in this case, the root node).
  • This DS function is called once at the beginning of the walker’s traversal before visiting any nodes.

can log_visit with test_node exit

  • This ability is executed each time the walker visits a node of type test_node during its traversal. In the example, whenever the walker visits a test_node, it prints the node and appends the node to visited_nodes.
  • This DS function operates during traversal and is called on each node of type test_node the walker visits.

can log_exit with exit

  • This ability is triggered once when the walker finishes its traversal, marking the end of its operation. In the example, it logs the exit point, increments exit_count by 1, and prints the final node visited.
  • This DS function is executed at the end of the walker's execution, after all nodes have been visited.

Walker Definition

node A{
    has val:int =0;
}

edge a{
}

walker W{
    can create with `root entry;
}

:walker:W:can:create{
    Start = A(5);
    here +:a:+> Start;
    Start +:a:+> A(10) +:a:+> A(15);
    Start +:a:+> A(20) +:a:+> A(25);
}



with entry{
    root spawn W();
    print([root-->-->(`?A)]);
    print([root--> --> -->(`?A)]);
}
Graph Image

Image

Node Connections

node a{
    has value:int =10;
}

edge b{
    has value:int = 20;
}

with entry{
    node_1 = a(value=5);
    node_2 = a();
    node_3 = a(value=15);
    node_4 = a(value=20);
    node_5 = a(value=25);

    root ++> node_1;
    node_1 +:edge_1:= b(value=5):+> node_2;
    node_1 +:edge_2:=b(value=10):+> node_3;
    node_1 +:edge_3:=b(value=15):+> node_4;
    node_1 +:edge_4:=b():+> node_5;

    node_1 del --> node_2;
    del node_3;

}
Graph Image

Image

Code Explanation
  • Create nodes

    1. node_1 = a(value=5); new node created with node class a, its value 5 and assigned to variable node_1.
    2. node_2 = a(); New node created with node class a, its default value and assigned to variable node_2.
  • Connect nodes

    1. root ++> node_1; node_1 is connected to root node with generic edge.
    2. node_1 +:b(value=10):+> node_3; node_3 is connected to node_1 by custom edge with edge class b and its value 5.
    3. node_1 +:b():+> node_2; node_5 is connected to node_1 by custom edge with edge class b and its default value.
  • Delete edge

    1. node_1 del --> node_2; edge between node_1 and node_2 is deleted.
  • Delete node

    1. del node_3; node_3 is deleted.