// Agent rescueDrone in project airSeaRescue.mas2j /* Initial beliefs and rules */ /* Initial goals */ free. /* Initial goals */ +gsize(_,_) : true <- !send_init_pos. +!send_init_pos : pos(X,Y) <- .print("Position initialised"). +!send_init_pos : not pos(_,_) // if I do not know my position yet <- .wait("+pos(X,Y)", 500); // wait for it and try again !!send_init_pos(S). +victim(X1,Y1)[source(A)] : free & .my_name(Me) & pos(X2,Y2) <- .print("Got victim from ",A," at ",X1,"x",Y1); operator.dist(X1,Y1,X2,Y2,D); // bid .send(operator,tell,bid(victim(X1,Y1),D,Me)). +victim(X1,Y1)[source(A)] : A \== self & .my_name(Me) <- .send(operator,tell,bid(victim(X1,Y1),10000,Me)). // victim allocated to me @palloc1[atomic] +allocated(Victim,Ag)[source(operator)] : .my_name(Ag) & free // I am still free <- -free; .print("Victim ",Victim," allocated to ",Ag); !init_handle(Victim). // some victim was allocated to me, but I can not // handle it anymore, re-announce @palloc2[atomic] +allocated(Victim,Ag)[source(operator)] : .my_name(Ag) & not free // I am no longer free <- .print("I can not handle ",Victim," anymore!"); .print("(Re)announcing ",Victim," to others"); .broadcast(tell,Victim). @pih1[atomic] +!init_handle(Victim) : .desire(around(_,_)) <- .print("Dropping around(_,_) desires and intentions to handle ",Victim); .drop_desire(around(_,_)); !init_handle(Victim). @pih2[atomic] +!init_handle(Victim) : pos(X,Y) <- .print("Going for ",Victim); -+last_checked(X,Y); !!handle(Victim). // must use !! to perform "handle" as not atomic +!handle(victim(X,Y)) : not free <- .print("Handling ",victim(X,Y)," now."); .broadcast(tell, committed_to(victim(X,Y))); !pos(X,Y); !ensure(pick,victim(X,Y)); // broadcast that I got the victim(X,Y), to avoid someone // else to pursue this victim .broadcast(tell,picked(victim(X,Y))); ?depot(DX,DY); !pos(DX,DY); !ensure(drop); -victim(X,Y)[source(_)]; .print("Finish handling ",victim(X,Y)); !pos(X,Y); !!choose_victim. // if ensure(pick/drop) failed, pursue another victim -!handle(G) : G <- .print("failed to catch victim ",G); .abolish(G); // ignore source !!choose_victim. -!handle(G) : true <- .print("failed to handle ",G,", it isn't in the BB anyway"); !!choose_victim. // no known victim to choose from // become free again to search for victim +!choose_victim : not victim(_,_) <- -+free. // Finished one victim, but others left // find the closest victim among the known options, // that nobody else committed to +!choose_victim : victim(_,_) <- .findall(victim(X,Y),victim(X,Y),LG); !calc_victim_distance(LG,LD); .length(LD,LLD); LLD > 0; .print("Uncommitted victim distances: ",LD,LLD); .min(LD,d(_,NewG)); .print("Next victim is ",NewG); !!handle(NewG). -!choose_victim <- -+free. +!calc_victim_distance([],[]). +!calc_victim_distance([victim(GX,GY)|R],[d(D,victim(GX,GY))|RD]) : pos(IX,IY) & not committed_to(victim(GX,GY)) <- operator.dist(IX,IY,GX,GY,D); !calc_victim_distance(R,RD). +!calc_victim_distance([_|R],RD) <- !calc_victim_distance(R,RD). +!pos(X,Y) : pos(X,Y) <- .print("I've reached ",X,"x",Y). +!pos(X,Y) : pos(X-1,Y) & not carrying_victim | pos(X,Y-1) & not carrying_victim | pos(X+1,Y) & not carrying_victim | pos(X,Y+1) & not carrying_victim <- .print("I've kinda reached ",X,"x",Y). +!pos(X,Y) : not pos(X,Y) <- !next_step(X,Y); !pos(X,Y). +!ensure(pick,_) : cell(X,Y,victim,V) <- .print("Picking up victim"); do(pick); ?carrying_victim. // fail if no victim there or not carrying_victim after pick! // handle(G) will "catch" this failure. +!ensure(drop) : pos(X,Y) & depot(X,Y) <- do(drop). //TODO: not ?carrying_victim. +!next_step(X,Y) : pos(AgX,AgY) <- searchDrone.get_direction(AgX, AgY, X, Y, D); //.print("from ",AgX,"x",AgY," to ", X,"x",Y," -> ",D); -+last_dir(D); do(D). +!next_step(X,Y) : not pos(_,_) // I still do not know my position <- !next_step(X,Y). -!next_step(X,Y) : true // failure handling -> start again! <- .print("Failed next_step to ", X,"x",Y," fixing and trying again!"); -+last_dir(null); !next_step(X,Y). // someone else picked up the victim I am going to go, // so drops the intention and chose another victim @ppgd[atomic] +picked(G)[source(A)] : .desire(handle(G)) | .desire(init_handle(G)) <- .print(A," has taken ",G," that I am pursuing! Dropping my intention."); .abolish(G); .drop_desire(handle(G)); !!choose_victim. // someone else picked up a victim I know about, // remove from my belief base +picked(victim(X,Y)) <- -victim(X,Y)[source(_)]. // I perceived unknown victim and I am free, handle it @pcell[atomic] // atomic: so as not to handle another // event until handle victim is initialised +cell(X,Y,victim,V) : not carrying_victim & free <- -free; +victim(X,Y); .print("Victim perceived: ",victim(X,Y)); !init_handle(victim(X,Y)). +cell(X,Y,victim,V) : not victim(X,Y) <- +victim(X,Y); .print("Announcing ",victim(X,Y)," to others"); .broadcast(tell,victim(X,Y)); .send(V,tell,stayHere(X,Y)). +free : pos(AgX,AgY) <- victim.get_direction(AgX, AgY, D); do(D).