Erlang Programming/Autonomous Agents

Explanation of the code in jungle.erl
Here we have a simple chat-bot agent called person/4. We create two instances of it called Tarzan and Jane. They talk to each other. Each has a timeout. The timeout is the length of time one will wait before they initiate conversation. The initial timeout of Jane is set to 10 seconds. The initial timeout of Tarzan is set to 8 seconds. Because of the initial values, Tarzan will speak first and Jane will respond. Both timeouts start over but keep the same values. Again, Tarzan speaks first and Jane responds. Now things get interesting. The agent can tell if the conversation is repeating. If the conversation repeats then special messages are sent to cause a swap in the relative levels of timeout. Now Tarzan waits longer than Jane and Jane has a chance to speak first. Now, Jane speaks first twice. Then they swap initiative again. Since the processes are autonomous, we need to stop them with a quit program called jungle:quit. Note: the change in timeout length is either double or half. The timeout change is similar to the exponential binary backoff for ethernet collisions. External link: Exponential_backoff.

Example program listing: jungle.erl
-module( jungle ). -compile(export_all). %% This program shows how chat-bot agents can exchange initiative(lead) while in conversation. %%  Start with start. %%  End with quit. start -> register( tarzan, spawn( jungle, person, [ tarzan, 8000, "", jane ]   ) ), register( jane, spawn( jungle, person,  [ jane,   10000, "", tarzan ] ) ), "Dialog will start in 5ish seconds, stop program with jungle:quit.". quit -> jane ! exit, tarzan ! exit. %% Args for person/4 %%  Name:  name of agent being created/called %%  T:     timeout to continue conversation %%  Last:  Last thing said %%  Other: name of other agent in conversation person( Name, T, Last, Other ) -> receive "hi" -> respond( Name, Other, "hi there \n " ), person( Name, T, "", Other ); "slower" -> show( Name, "i was told to wait more " ++ integer_to_list(round(T*2/1000))), person( Name, T*2, "", Other ); "faster" -> NT = round( T/2 ), show( Name, "I was told to wait less " ++ integer_to_list(round(NT/1000))), person( Name, NT, "", Other ); exit -> exit(normal); _AnyWord -> otherwise_empty_the_queue, person( Name, T, Last, Other ) after T -> respond( Name, Other, "hi"), case Last of           "hi" -> self ! "slower", sleep( 2000),                 % give the other time to print Other ! "faster", person( Name, T, "", Other ); _AnyWord -> person( Name, T, "hi", Other ) end end. % respond( Name, Other, String ) -> show( Name, String ), Other ! String. % show( Name, String ) -> sleep(1000), io:format( " ~s -- ~s \n ", [ Name, String ] ). % sleep(T) -> receive after T -> done end. % ===========================================================>%

Sample output from: jungle.erl
Sample output: 18> c(jungle). {ok,jungle} 19> jungle:start. jane_and_tarzan_will_start_in_5_seconds tarzan—hi jane—hi there tarzan—hi jane—hi there jane—I was told to wait less: 5 tarzan—I was told to wait more: 16 jane—hi tarzan—hi there jane—hi tarzan—hi there tarzan—I was told to wait less: 8 jane—I was told to wait more: 10 tarzan—hi jane—hi there tarzan—hi jane—hi there jane—I was told to wait less: 5 tarzan—I was told to wait more: 16 jane—hi tarzan—hi there 20> jungle:quit. exit