Home
agentMom User`s Manual
Contents
1. public Object getContent return content public String getPerformative return performative public String getReceiver return receiver public String getSender return sender public void setContent Object cont content cont public void setContent String cont content cont public void setPerformative String perf performative perf public void setReceiver String name receiver name public void setSender String name sender name A 4 agentMom Appendix A MessageHandler package afit mom agentMOM message handler import java net import java io import java lang public class MessageHandler extends Thread public int portNo public ServerSocket server public Agent parent public String endToken END public MessageHandler int port Agent p super MessageHandler parent Agent p if port lt 1024 throw new IllegalArgumentException Bad port number given to Listener constructor for agent p name Try making a ServerSocket to the given port try A server new ServerSocket port catch IOException e System out println Agent p name Failed to start ServerSocket on port port System exit 1 Made the connection so set the local port number portNo port System out println Agent p name messageHandler Started on port port This method was created in V
2. new Thread NUMCLIENTS try host InetAddress getLocalHost getHostName System out println Starting Server Agent on host host server new Thread new ServerAgent Server sPort Server server start for int i 0 i lt clients length i System out println Starting Client Agent i 1 clients i new Thread new ClientAgent Client i 1 cPort i host sPort Client i 1 clients i start while true catch UnknownHostException e System out println Unknown Host Exception when starting ClientAgent B 12 agentMom Appendix B WindowDestroyer package afit momtest import java awt import java awt event k k p This type was created in VisualAge id class WindowDestroyer extends WindowAdapter kk ie This method was created in VisualAge a void windowClosing WindowEvent e System exit 0 B 13
3. from m getSender set up conversation try while notDone switch state case 0 state 1 break case 1 parent remove_service m sender String m content m host m port parent printRegistrants state 2 break case 2 m setPerformative reply m setContent OK sendMessage m output notDone false break input close output close connection close catch UnknownHostException e parent write Unknown Host exception catch EOFException oef parent write Server terminated connection catch IOException e parent write IO Exception e printStackTrace B 10 agentMom Appendix B Registration package afit momtest This type was created in VisualAge 2 public class Registration String agent String host int port String service Registration constructor comment public Registration String a String h int p String ser agent a host h port p service ser B 11 agentMom Appendix B Tester package afit momtest This type was created in VisualAge import java net public class Tester Tester constructor comment Ay public Tester super Starts the application ie public static void main java lang String args int NUMCLIENTS 3 int cPort 3000 starting port address int sPort 5000 String host Thread server Thread clients
4. hostName portNum parent a service theService public void run Message m new Message int state 0 boolean notDone true parent write Starting Client Register conversation set up conversation try connection new Socket connectionHost connectionPort output new ObjectOutputStream connection getOutputStream output flush input new ObjectInputStream connection getInputStream while notDone switch state case 0 m performative register m content service sendMessage m output state 1 break Case 1 m readMessage input if m performative equals reply notDone false else parent write ERROR did not get reply back break input close output close connection close catch UnknownHostException e parent write Unknown Host exception in Client Register catch EOFException oef parent write Server terminated connection in Client Register catch IOException e parent write IO Exception in Client Register B 7 agentMom Appendix B Client_Unregister package afit momtest This type was created in VisualAge import java net import java io import afit mom public class Client_Unregister extends Conversation ClientAgent parent override parent String service public Client_Unregister ClientAgent a String hostName int portNum S
5. notDone switch state case 0 m performative register m content service sendMessage m output state 1 break case 1 m readMessage input if m performative equals reply notDone false else parent write ERROR did not get reply back break In the code above the state variable starts at state zero In state 0 the message performative is set to register and the message content is set to a string sent to the conversation by the ClientAgent when it was initialized Actually the content of a message can take any Java object type After sending the message the state variable is set to 1 and the break statement takes us out of the switch statement Since notDone is still true we stay in the loop this time entering the case 1 option of the switch statement At this point we wait at the readMessage call until a message comes in from the other agent Then if it is what we expect a reply performative we process it otherwise we print an error message In this case we do nothing with the reply and simply set the notDone variable to false so that we will exit the while loop After exiting the conversation we close the connection with the other agent using the sequence of close statements shown below input close output close connection close j Message Class The message class defines the field used in the message passed back and forth between agents It is fairly straight
6. July 2000 agentMom User s Manual Scott A DeLoach GRADUATE SCHOOL OF ENGINEERING AND MANAGEMENT AIR FORCE INSTITUTE OF TECHNOLOGY WRIGHT PATTERSON AIR FORCE BASE OHIO Approved for public release distribution unlimited agentMom Introduction What iS AGENtMOM c cece cece eee eee eee e netted 3 HOW to USE agent MON ii A at at daha A ded at daha at vais ded et valet a a alee ied dal 4 Agent EAS A eae died 4 Conversation Cl Sui A A a A At ba 4 Message Class iii a a ee ee ee 6 Message Handler ii A cnet eine ea cent wet aaa eee ee 7 Sorry Clas ui e eee Se ade ee eg ee et ee ee ee do ded 8 Step By Step Construction occoccccocnccncnncnnnnnnnncnnnnnnnnnnnnnnnrnnnnnnnnnrrnnnrnrnnrnannrnnnrnrnernaness 8 AGENtMOM Source COdC ce nn rn nn rene r nen n rn nr nrnnrnrnnrnrnnnninnan 1 AO iru A Doan a gdadta a aa a N 1 Conversation eiae ti 2 Message erana e E A tated aa AA A 4 MessageHandler gt cviocoioa ada 5 SOY irte aa a abia 6 APPENDIX B 1 agentMom Example Source Code cccce eee n eens 1 ClientAgent iu c s EAA tad er Aids cal tliat col bine a ld adds 1 ServerAgent s2 33 30 io EE a la Nel asa kta see iio 4 Client Register cuca pis sida aladdin 7 Client Un Feglster oli ini Ab 8 Server o Register iii ta 9 Server UNregiSte Fil ia ets 10 Registration it cod all idad es 11 TOS iii ATA A a 12 Window Destroyer voii A a AA AA A 13 agentMom Using agentMom Introduction What is agentMom age
7. ameters in a single message as shown in the registration class below public class ComplexObject implements Serializable String agent String host int port String service public ComplexObject String a String h int p String ser agent a host h port p service ser This class encapsulates four parameters three strings and an integer that can be assigned to message content field Note that in order to pass an object across a socket connection it must implement the interface Serializable Message Handler The message handler class MessageHandler is very simple to use When an agent is created it simply creates a new message handler thread as shown below MessageHandler h new MessageHandler this port this h start The two required parameters are simply the port number and a pointer to the parent agent object When started the message handler starts a socket server on the indicated port and waits for a connection from another agent When a connection is received the message handler simply calls the parent agent s receiveMessage method with the connection and the input and output streams As described above the agent does the rest The code for the main message handler run method is shown below public void run Socket connection int counter 1 ObjectOutputStream output ObjectInputStream input System out println Server Running on portNo while true try connectio
8. bjectInputStream i ObjectOutputStream o ServerAgent a Message m1 super s i Oo a ml parent a public void run int state 0 boolean notDone true parent write Got gt gt m getPerformative m getContent from m getSender set up conversation try while notDone switch state case 0 parent add_service m sender String m content m host m port parent printRegistrants state 1 break case 1 m setPerformative reply m setContent OK sendMessage m output notDone false input close output close connection close catch UnknownHostException e parent write Unknown Host exception catch EOFException oef parent write Server terminated connection catch IOException e parent write IO Exception Server Register e printStackTrace B 9 agentMom Appendix B Server_Unregister package afit momtest This type was created in VisualAge import java net import java io import afit mom public class Server_Unregister extends Conversation ServerAgent parent override parent public Server Unregister Socket s ObjectInputStream i ObjectOutputStream o ServerAgent a Message ml super s i Oo a ml parent a public void run int state 0 boolean notDone true parent write Got gt gt m getPerformative m getContent
9. bjectOutputStream output int i Message m Server Register sS Thread t define all possible conversations receivable here try m Message input readObject System out println _ Invalid Attempt to start new conversation with performative m performative from m sender B 2 agentMom Appendix B t new Thread new Sorry server input output this m t start start new thread catch ClassNotFoundException cnfex System out println ClassNotFoundException catch IOException cnfex System out println IOException on port ServerAgent receiveMessage This method was created in VisualAge public void run Thread init theText append Starting Client In setVisible true init new Thread new Client_Register this serverHost serverPort get_service start conversation init start int j for int i 0 i lt 10000000 i j i 1 init new Thread new Client_Unregister this serverHost serverPort get_service start conversation init start This method was created in VisualAge protected void write String s this theText append s n B 3 agentMom Appendix B ServerAgent package afit momtest import java util import java awt import java awt event import java net import java io import afit mom This type was created in VisualAge public
10. ce String agent String service String host int port this registrants addElement new Registration agent host port service This method was created in VisualAge public static void main String args ServerAgent s new ServerAgent Server 5000 s run This method was created in VisualAge 5 protected void printRegistrants java util Enumeration enum this registrants elements Registration v enum this registrants elements while enum hasMoreElements v Registration enum nextElement this theText append lt v agent v service v host v port gt In receiveMessage method comment x7 public void receiveMessage Socket server ObjectInputStream input ObjectOutputStream output int i Message m Server Register sS Thread t define all possible conversations receivable here try m Message input readObject this theText append Received message m performative from 4 m sender n if m performative equals register t new Thread new Server Register server input output this m t start start new thread else if m performative equals unregister t new Thread new Server Unregister server input output this m t start start new thread B 5 agentMom Appendix B else System out println _ Invalid Attempt to start new conversation w
11. class ServerAgent extends Agent implements ActionListener private TextArea theText private Panel textPanel private TextField Tname private Vector registrants new Vector 25 ServerAgent constructor comment public ServerAgent String n int p super n p MessageHandler handler server initialization handler new MessageHandler port this handler start window initialization setSize 500 300 set default size height of new window addWindowListener new WindowDestroyer create listener setTitle Server name the window setBackground Color blue setLayout new BorderLayout text panel textPanel new Panel theText new TextArea 12 60 theText setBackground Color white theText set Foreground Color black textPanel add theText add textPanel Center top label Panel namePanel new Panel namePanel setForeground Color white Label nameLabel new Label Enter text of your choice namePanel add nameLabel Center add namePanel North setVisible true B 4 agentMom Appendix B This method was created in VisualAge public void actionPerformed ActionEvent e if e getActionCommand equals Exit System exit 0 else theText setText Error in memo interface textPanel repaint This method was created in VisualAge protected void add_servi
12. d message gt Thread subobject creation Figure 1 agentMom While conversations handle the message passing back and forth between agents they still must have a way to communicate with their parent agents This is accomplish using method calls from the conversation back to their parents agentMom How to use agentMom The afit mom package which makes up the basics of agentMom is shown in Figure 2 It consists of two abstract classes Agent and Conversation and three concrete classes MessageHandler Message and Sorry The actual source code associated with these classes are in Appendix A Figure 2 agentMom Object Model Agent Class The Agent class is an abstract class that defines the minimum requirements for an agent to use agentMom It must be runnable as a separate thread which requires a run method It also has two required parameters that must be set for each agent the name of the agent and the port number on which it s message handler will listen for incoming messages Also the agent must implement the receiveMessage method This is the method that will be called by the message handler when the agent has received a connection starting a new conversation Each agent must implement this method by reading the message from the connection and determining if it is a valid conversation Two agents that implement these methods ClientAgent and ServerAgent are shown in Appendix B The receiveMessage method for the ServerAgent
13. ext new TextArea 12 50 theText setBackground Color white theText set Foreground Color black textPanel add theText add textPanel Center top label Panel namePanel new Panel namePanel setForeground Color white B 1 agentMom Appendix B Label nameLabel new Label Enter text of your choice namePanel add nameLabel Center add namePanel North This method was created in VisualAge public void actionPerformed ActionEvent e if e getActionCommand equals Exit System exit 0 else theText setText Error in memo interface textPanel repaint This method was created in VisualAge public String get _service if this name equals Client 1 return Laundry if this name equals Client 2 return Dry Cleaning if this name equals Client 3 return Drill Press Operator else return Unknown This method was created in VisualAge public static void main String args String serverHost int serverPort 5000 try serverHost InetAddress getLocalHost getHostName ClientAgent s new ClientAgent Client 3000 serverHost serverPort s run catch UnknownHostException e System out println Host Exception when starting ClientAgent receiveMessage method comment public void receiveMessage Socket server ObjectInputStream input O
14. forward and consists of the following attributes public String host null public int port 0 public String sender null public String receiver null public String performative null public String force null public String inreplyto null public String language null public String ontology null public String replywith null public Object content null agentMom Not all of these fields need to be used They were derived from the fields in a KQML message and only some of them are necessary When a conversation calls the sendMessage method it automatically fill the sender host and port fields using the parent agent s name and port attributes and automatically gets the host name from the system The other fields of interest in an agentMom message are the performative and content fields As seen above the performative field describe the action that the message intends and is used in the agent and conversation classes to 1 determine the type of conversation being requested and 2 to control the execution of a conversation in the run method agentMom does have any specific performative types The user define any performative they feel are necessary The content of an agentMom message is also very general Basically the message passes any valid Java object type This can be as simple as a string or a more complex object that encapsulates a number of attribute types These complex objects can be used to pass multiple par
15. io import java awt public abstract class Agent implements Runnable public String name public int port This method was created in VisualAge y protected Agent String n int p this name n this port p public abstract void receiveMessage Socket server ObjectInputStream input ObjectOutputStream output This method was created in VisualAge Ed public abstract void run This method was created in VisualAge private void write String s System out println s A 1 agentMom Appendix A Conversation package afit mom This type was created in VisualAge import java net import java io public abstract class Conversation extends Thread protected Agent parent protected Socket connection protected ObjectInputStream input protected ObjectOutputStream output protected Message m protected String connectionHost protected int connectionPort Conversation constructor comment 7 public Conversation Agent a super parent a Server Register constructor comment public Conversation Agent a String hostName int portNum super a parent a connectionHost hostName connectionPort portNum public Conversation Socket s ObjectInputStream i ObjectOutputStream o Agent a Message m1 super a parent a connection sS input i output O m ml public Message readMessage ObjectInputS
16. is shown in Figure 3 In this case after the receiveMessage method reads the message using the m Message input readObject method call the performative of the message is checked to see if it is either register or unregister In the case of the ServerAgent these are the only two performatives the agent can recognize that start conversations in which it can participate If it is either of these performatives its creates a new conversation object as a new thread sends it the initial message and starts it running using the conversation s run method If the message received does not start with a recognizable performative the agent starts the default Sorry conversation which simply sends a sorry message in reply to the performative Therefore all good conversations should be able to respond to a sorry performative when it tries to start a conversation Conversation Class The Conversation class is an abstract class that actually carries out the message passing between agents There are two methods in the Conversation class readMessage and sendMessage that actually pass the messages back and forth over the socket connection agentMom There are really two types of conversation classes that can be derived from the Conversation class one for the conversation initiator and one for the conversation respondent The basic difference lies in which constructor is used and the details in the abstract run method which must be implemented in
17. isualAge public void run Socket connection int counter 1 ObjectOutputStream output ObjectInputStream input System out println Server Running on portNo while true try connection server accept System out println Connection counter received from connection getInetAddress getHostName output new ObjectOutputStream connection getOutputStream output flush input new ObjectInputStream connection getInputStream parent receiveMessage connection input output catch IOException cnfex System out println IOException on port portNo A 5 Sorry package afit mom It defines a general purpose conversation to reply Sorry to any unknown type of conversation import java net import java io public class Sorry extends Conversation protected Agent parent override parent Server Register constructor comment public Sorry Socket s ObjectInputStream i ObjectOutputStream o Agent a Message m1 super a parent a connection s input i output O m ml This method was created in VisualAge public void run int state 0 boolean notDone true set up conversation try while notDone switch state case 0 m setPerformative sorry m setContent unknown conversation request sendMessage m output notDone false break input close o
18. ith performative m performative from m sender t new Thread new Sorry server input output this m t start start new thread catch ClassNotFoundException cnfex System out println ClassNotFoundException catch IOException cnfex System out println IOException on port ServerAgent receiveMessage This method was created in VisualAge protected void remove service String agent String service String host int port java util Enumeration enum this registrants elements Registration v while enum hasMoreElements v Registration enum nextElement if v agent equals agent amp amp v service equals service amp amp v host equals host amp amp v port port this registrants removeElement v This method was created in VisualAge 7 public void run theText append Starting Server n setVisible true px This method was created in VisualAge y protected void write String s this theText append s n B 6 agentMom Appendix B Client_Register package afit momtest This type was created in VisualAge import java net import java io import afit mom public class Client Register extends Conversation ClientAgent parent override parent String service public Client Register ClientAgent a String hostName int portNum String theService super a
19. n server accept System out println Connection counter received from connection getInetAddress getHostName agentMom output new ObjectOutputStream connection getOutputStream output flush input new ObjectInputStream connection getInputStream parent receiveMessage connection input output catch IOException cnfex System out println IOException on port portNo Sorry Class The Sorry class is simply a default conversation class that sends a sorry message when a performative is received that is not expected by either the receiveMessage method of the parent agent or within the flow of a conversation Step By Step Construction To build an application using the agentMom framework you need to perform the following 1 Get a copy of agentMom classes as described in Appendix A 2 Define your agent classes and conversations according the MaSE Multiagent Systems Engineering methodology An environment agentTool is available to help you with this For each agent class in your system extend the agentMom Agent class a b c d Define the receiveMessage method to handle all conversations for which the agent is a respondent may be automatically generated by agentTool For each action defined in the set of conversations in which this agent may participate define a method in the agent class This will be your interface to the conversation Implement
20. ntMom is a framework upon which distributed multiagent systems can be developed It is implemented in Java and provides the basic building blocks for building agents conversations between agents and the message that are passed in the conversations An overview of how agentMom works is shown in Figure 1 An agent allows itself to speak with other agents by starting a message handler that monitors a local port for messages All agent communication is performed via conversations which define valid sequences of messages that agents can use to communicate When one agent wants to communicate with another agent it starts one of its conversations as a separate Java thread The conversation then establishes a socket connection with the other agent s message handler and sends the initial message in the conversation When the message handler receives a message it passes the message to the agent s receiveMessage method that compares the message against its known list of allowable message types to see if it is the start of a valid conversation If the conversation is valid the agent starts its side of the appropriate conversation also as a separate Java thread From that point on all communication is controlled by the two conversation threads Conversations send messages to each other using built in readMessage and sendMessage methods i receive receive l message message 1 make connection amp i send initial message h send rea
21. the concrete class derived from the Conversation class gee receiveMessage method comment public void receiveMessage Socket server ObjectInputStream input ObjectOutputStream output int i Message m Server_Register s Thread t try m Message input readObject this theText append Received message m performative from m sender n if m performative equals register t new Thread new Server_Register server input output this m t start start new thread else if m performative equals unregister t new Thread new Server_Unregister server input output this m t start start new thread else System out printin Invalid Attempt to start new conversation with performative m performative from m sender t new Thread new Sorry server input output this m t start start new thread catch ClassNotFoundException cnfex System out printin ClassNotFoundException catch IOException cnfex System out printin IOException on port ServerAgent receiveMessage Figure 3 receiveMessage Example An example of an initiator conversation class is the Client_Register class in Appendix B To initiate this conversation the ClientAgent creates a new Client _Register object as a separate thread using the Client_Register constructor This constructor does not need to send a socket input stream or output stream see second Conversa
22. the run method as the main procedure of the method If your agent initiates any conversations this is where they will originate If you want to run your agent as a stand alone application create a main method to initialize the agent running For each conversation in your system design create two conversation classes in initiator and a respondent class a b For each initiator class define a constructor that includes as parameters any parameters in the first message sent For each respondent class define a constructor that includes as a parameter the message read by the parent receiveMessage method before the conversation thread was started Implement the run method may be automatically generated by agentTool i Define a state variable initialized to state 0 ii If it is an initiator conversation create a connection with the agent of interest agentMom iii Create a switch statement within a while loop where each case in the switch statement corresponds to a state or a transition Ensure at least one of the states exits the while loop iv Close the connection 5 Create any supporting classes for things such as a Objects that combine multiple parameters into a single object b System setup testing routines c Components of intelligent agents agentMom Appendix A Appendix A agentMom Source Code Agent package afit mom This type was created in VisualAge import java net import java
23. tion constructor in Appendix A since as an initiator the conversation creates a new socket and opens an input and output stream with a second agent s message handler When the ClientAgent starts the Client_Register conversation class the Client_Register run method is started This method controls the conversation It creates a new connection using the following commands connection new Socket connectionHost connectionPort output new ObjectOutputStream connection getOutputStream output flush input new ObjectInputStream connection getInputStream After the connection is made the method enters a while loop that iterates until the conversation is completed Inside the while loop is a simple switch statement that has a case for each possible state of the conversation Actually the state in the run method may or may not correspond one to one with the states of the conversation as defined in a MaSE agentMom conversation diagram Actually it is possible to have one state for each state in the diagram plus a state for each transition out of a state In a simple conversation such as the Client_Register conversation this could be modeled as a simple sequence of statements however in the general case conversations may have loops and many branches out of a single state thus the switch within a loop provides the most general mechanism for modeling conversation states The loop and switch statement are shown below while
24. tream input Message m null try m Message input readObject catch ClassNotFoundException cnfex System out println ClassNotFoundException catch IOException cnfex System out println IOException on port return m A 2 agentMom Appendix A receiveMessage method comment xy public abstract void receiveMessage Message m This method was created in VisualAge public abstract void run This method was created in VisualAge 5 public void sendMessage Message m ObjectOutputStream output try m sender parent name m host connection getlInetAddress getLocalHost getHostName m port parent port output writeObject Object m catch IOException e System out println Exception e A 3 agentMom Appendix A Message package afit mom import java lang public class Message implements java io Serializable public String host null public int port 0 public String sender null public String receiver null public String performative null public String force null public String inreplyto null th xpected label in a reply public String language null name of language used in the content public String ontology null name of the ontoloty used in the content public String replywith null label user expects for reply public Object content null public Message super
25. tring theService super a hostName portNum parent a service theService public void run Message m new Message int state 0 boolean notDone true parent write Starting Client Unregister conversation set up conversation try connection new Socket connectionHost connectionPort output new ObjectOutputStream connection getOutputStream output flush input new ObjectInputStream connection getInputStream while notDone switch state case 0 m setPerformative unregister m content service sendMessage m output state 1 break case 1 m readMessage input if m performative equals reply notDone false else parent write ERROR did not get reply back n break input close output close connection close catch UnknownHostException e parent write Unknown Host exception in Client Unregister catch EOFException oef parent write Server terminated connection in Client Unregister catch IOException e parent write IO Exception in Client Unregister e printStackTrace B 8 agentMom Appendix B Server Register package afit momtest This type was created in VisualAge import java net import java io import afit mom public class Server Register extends Conversation ServerAgent parent override parent public Server Register Socket s O
26. utput close connection close catch UnknownHostException e System out println Unknown Host exception catch EOFException oef System out println Server terminated connection catch IOException e System out println IO Exception e printStackTrace A 6 agentMom Appendix A agentMom Appendix B Appendix B agentMom Example Source Code ClientAgent package afit momtest import java awt import java awt event import java io import java net import afit mom This type was created in VisualAge public class ClientAgent extends Agent implements private TextArea theText private Panel textPanel private TextField Tname public String serverHost public int serverPort ClientAgent constructor comment public ClientAgent String agentName int agentPort String sHost int sPort super agentName agentPort this serverHost ct server initialization MessageHandler h new MessageHandler this port h start window initialization ActionListener sHost the Host to connect to his serverPort sPort the Port to connect to this setSize 400 300 set default size height of new window addWindowListener new WindowDestroyer setTitle this name name the window setBackground Color red setLayout new BorderLayout text panel textPanel new Panel theT
Download Pdf Manuals
Related Search
Related Contents
módulo de anexação inteligente hp l1619a Kingston Technology ValueRAM KVR1333D3LS4R9S/2G memory module 4-3 溶接・溶断における健康と安全 神山 宣彦 【みんなの感想】 AlarmView Manual de Usuario Cooler Master NotePal U-LITE 37-0909 A & I ELECTRIC AIR COMP ENGLISH.indd Hughes & Kettner Tubeman MKII Series Linc Screen 鈴鹿市清掃センター 改修対策事業 要求水準書 (管理運営編) Copyright © All rights reserved.
Failed to retrieve file