ExamplesSome simple (and not so simple) examples of agents and Jacomma use with the JPython interpreter are presented below. The source code for the examples (and more) is located at the src/jacomma/examples directory of the Jacomma distribution. Hello World with AgentsWell, in order to have a hello world example with agents, you need a couple of them. So, first things first, let's start icm, and do the hello world thing in a JPython interpreter: $ icm -Dm -L /tmp/icm.log $ cd src/jacomma/examples $ jpython JPython 1.1 on java1.3.0 (JIT: jitc) Copyright (C) 1997-1999 Corporation for National Research Initiatives >>> import java; import jacomma; import echo >>> from jacomma.platform.core import AgentManager, AgentObserverAdapter >>> ag = AgentManager.instance.createAgent( "test", AgentObserverAdapter() ) >>> p = AgentManager.instance.createAgent( "print", AgentObserverAdapter() ) >>> echo.register_print_handler( p ) >>> ag.send( p.getHandle() , "Hello World!" ) >>> (print@aphex.media.mit.edu,test@aphex.media.mit.edu,{},Hello World!) So, what happened here? Well, we created a couple of agents (in the same, local Jacomma engine), and we registered a print handler for one of them. We then send a message to "print", who received it and printed it asynchronously in a different thread. How does print work? Well, this is show in the echo JPython module that resides in the jacomma.examples package. Show me a GUI!And say that you are a click-addict that wants to see GUI objects passed around. Well, this is easy to do, using the frame_handler defined again in the echo module. To make the example more interesting, let the two agents reside in different Jacomma engines or even in different network hosts - there is no difference with this respect, since communication is location and connectivity transparent. So, let us use the "test" agent of the previous example, in order to create and send a frame to an arbitrary agent, named "display" (for simplicity at the localhost): >>> import javax >>> f = javax.swing.JFrame( "Agent says..." ) >>> f.getContentPane().add( javax.swing.JLabel( "Hello World!" ) ) >>> f.pack() >>> l = java.util.ArrayList() >>> l.add( jacomma.icm.type.Symbol( "show" ) ) >>> l.add( f ) >>> ag.send( jacomma.icm.type.Handle.createHandle( "display" ), l ) At this point, a nice message frame has been sent to an arbitrary remote agent. So let us start a new Jacomma engine, create an agent with that name and with the capability to accept and display frames embeeded in ('show, [frame]) messages: $ jpython JPython 1.1 on java1.3.0 (JIT: jitc) Copyright (C) 1997-1999 Corporation for National Research Initiatives >>> import java; import jacomma; import echo >>> from jacomma.platform.core import AgentManager, AgentObserverAdapter >>> ag = AgentManager.instance.createAgent( "display", AgentObserverAdapter() ) >>> echo.register_frame_handler( ag ) and voilla! The frame magically appears! Let's Chat!Finally, as an illustration of a relatively complex application, we present a chatting system. The chatting system is centered around an april agent, which broadcasts all messages that it receives to all agents that it knows about. The chat clients are instances of simple JPython agent, named messenger. And here is a nice screenshot of a three-way chatting: |
|
|