Skip to main content

Take

Jaseci introduces the handy command called "take" to instruct walker to navigate through nodes. You may notice by default, a walker traverse with take command using the breadth first search approach (refer to the example here). But the take command is flexible hence you can indicate whether the take command should use a depth first or a breadth first traversal to navigate. Look at the following example;

node plain: has name;

## defining the graph
graph example {
has anchor head;
spawn {
n=[];
for i=0 to i<7 by i+=1 {
n.l::append(spawn node::plain(name=i+1));
}
n[0] ++> n[1] ++> n[2];
n[1] ++> n[3];
n[0] ++> n[4] ++> n[5];
n[4] ++> n[6];
head=n[0];
}
}

## walker for breadth first search
walker walk_with_breadth {
has anchor node_order = [];
node_order.l::append(here.name);
take:bfs -->; #can be replaced with take:b -->
}

walker walk_with_depth {
has anchor node_order = [];
node_order.l::append(here.name);
take:dfs -->; #can be replaced with take:d -->
}

walker init {
start = spawn here ++> graph::example;
b_order = spawn start walker::walk_with_breadth;
d_order = spawn start walker::walk_with_depth;
std.out("Walk with Breadth:",b_order,"\nWalk with Depth:",d_order);
}

Expected Output:

Walk with Breadth: [1, 2, 5, 3, 4, 6, 7]
Walk with Depth: [1, 2, 3, 4, 5, 6, 7]

You may see in the above example take:bfs--> and take:dfs -- commands instruct walker to traverse breadth first search or depth first search accordingly. Additionally, to define breadth first or depth first traversals, can use the short hand of take:b --> or take:d —>.