3
$\begingroup$

I'm trying to implement a tree sort for 8 numbers. I've created 15 tree nodes agents and one manager agent. What I'm trying to achieve is to synchronize leaves in the tree and send their generated numbers to manager node. My problem is that in my for cycle, the leaves are not synchronized so manager is waiting for next leaf but that leaf may have sent it's number already. Is there a way to synchronize these leaves, so that manager waits for each leaf and then prints out received number?

Here are the agents:

treeSort.mas2j

MAS treeSort {
    infrastructure: Centralised
    agents:
        manager;
        agent#15;
}

manager.asl:

!start.
+!start : true <-
    for ( .range(I,8,15) ) {
        .concat("agent",I,TempAgent);
        .print("waiting from ",TempAgent);
        .term2string(YT,TempAgent);
        .wait(recievedNum(YT,X));
        .print("from ",YT," recvd ",X);    
    };
    .println(" done").

agent.asl

!start.

@p1 +!start : .my_name(agent8) | .my_name(agent9)| .my_name(agent10)|.my_name(agent11)
        | .my_name(agent12) | .my_name(agent13) | .my_name(agent14) |.my_name(agent15) <- +myNum(math.round(math.random(100)));
    ?myNum(X);
    +iam(list);
    .my_name(Y);
    .send(manager, tell, recievedNum(Y,X)).

@p2 +!start : .my_name(agent7) | .my_name(agent6)|  .my_name(agent5) |.my_name(agent4)
        | .my_name(agent3) | .my_name(agent2) <- +iam(node).

@p3 +!start : .my_name(agent1) <- +iam(root).

This is the output of program: https://imgur.com/a/ewOAuxv . As you can see, the manager is waiting for number from agent9 bu because he already got that number from him waiting for agent8, he will now wait forever. There is managers database: https://imgur.com/a/ZZ35pD9 .

$\endgroup$
  • 1
    $\begingroup$ In your explanation, agent 9 replies before agent 8. Isn't it a problem in the first place for the sorting algorithm? Please consider asking on StackOverflow, as this really is a programming question (including the process synchronization primitives in Jason). $\endgroup$ – Eric Platon May 27 '18 at 13:40
  • 1
    $\begingroup$ I already did: stackoverflow.com/questions/49896288/… $\endgroup$ – nocturne May 28 '18 at 14:29