jade笔记(编辑修改稿)内容摘要:

is))。 }// } 按照以前记 载,在 beans 属性中加入库,然后再运行参数中,设置,主类依然为 ,参数为 baz: bob:,运行成功后,以下是输出结果: baz: about to propose marriage to bob bob: I wonder if anybody wants to marry me? bob:baz has asked me to marry him! bob:I39。 m going to agree. baz: 吼吼 ! bob 已经同意嫁给我了 , I39。 m so excited! baz:bob has informed me of the status of my request. They said : I Do! 上例中, 应用了 SimpleAchieveREInitiator 和 SimpleAchieveREResponder 两个 基类 ,适用于两个 Agent 之间的交互。 可以看出发起者对于不同的回应有不同的执行动作。 技巧:从 AMS 中获取所有 Agent 的 AID。 引用文件: import。 import .*。 调用示例: AMSAgentDescription [] agents = null。 try { SearchConstraints c = new SearchConstraints()。 (new Long(1))。 agents = ( this, new AMSAgentDescription (), c )。 } catch (Exception e) { ( Problem searching AMS: + e )。 ()。 } ACLMessage msg = new ACLMessage()。 ( Ping )。 for (int i=0。 i。 i++){ if ( agents[i].getName().equals(getAID()) ){ //如果不是自己则加入到接收者 数组中 continue。 } ( agents[i].getName() )。 } : MessageTemplate class 利用 MessageTemplate 可以针对 ACLMessage 的每个属性设置模式,以达到过滤消息的目的。 为了可以构建更复杂的匹配规则, 多 个 模式也可以进行 and,or, not运算。 最有用的一些规则或方法包括:通信行为匹配,发送者匹配,会话 ID匹配。 比如 MatchPerformative( performative ) 是 通信行为的匹配。 这里 performative 可能是 : 还有发送者匹配 MatchSender( AID ),会话匹配 MatchConversationID( String ), 通信协议匹配 MatchProtocol( String ) ,本体匹配 MatchOntology( String)。 比如 : MessageTemplate mt = ( ( ), ( new AID( a1, )))。 相当于建立了一个模板, 表示消息规则为 INFORM 行为并 且发送者为“ a1”。 接收过程如下: ACLMessage msg = receive( mt )。 if (msg != null) { ... handle message } block()。 示例: package。 import。 import。 import .*。 import .*。 public class Template extends Agent { MessageTemplate mt1 = ( ( ), ( new AID( a1,)))。 protected void setup() { // Send messages to a1 and a2 ACLMessage msg = new ACLMessage()。 ( Ping )。 for (int i = 1。 i=2。 i++) ( new AID( a + i, ) )。 send(msg)。 // Setup Behaviour 1 addBehaviour(new CyclicBehaviour(this) { public void action( ) { (Behaviour ONE: )。 ACLMessage msg= receive( mt1 )。 if (msg!=null ( gets + () + from + ().getLocalName() + = + () )。 else ( gets NULL )。 block()。 } })。 // Setup Behaviour 2 addBehaviour(new CyclicBehaviour(this) { public void action() { (Behaviour TWO: )。 ACLMessage msg= receive()。 if (msg!=null) ( gets + () + from + ().getLocalName() + = + () )。 else ( gets NULL )。 block()。 } })。 } } package。 import。 import .*。 import .*。 public class Responder extends Agent { protected void setup() { addBehaviour(new CyclicBehaviour(this) { public void action() { ACLMessage msg = receive()。 if (msg!=null) { ACLMessage reply = ()。 ( )。 ( Gossip..... )。 send(reply)。 ( )。 ( Really sexy stuff... cheap! )。 send(reply)。 } block()。 } })。 } } 输出结果: :发送消息为 java序列化对象 在 JADE 中 ,支持 agents 之间通信的消息内容使用序列化的 java对象 ,对本地应用 ,特别是所有 agent 都用 java 实现的情况下也是很有用的 . 看实例:一个 ObjectSender 负责发送一个 Person 对象, ObjectReceiver 负责接收后打印出接收到的内容。 源文件: package。 import .*。 import。 public class Person implements Serializable { String name。 String surname。 Date birthdate。 int age。 Person(String n, String s, Date d, int a) { name = n。 surname = s。 birthdate = d。 age = a。 } public String toString() { return(name+ + surname + born on +()+ age = +age)。 } } 文件 2: package。 import。 import。 import .*。 import。 public class ObjectSender extends Agent { protected void setup() { try{ ACLMessage msg = new ACLMessage()。 ( Ping )。 AID personR = new AID( personR, )。 (personR )。 Person p = new Person(Name1, Surname1, new Date(), 1)。 (p)。 (JavaSerialization)。 send(msg)。 (getLocalName()+ sent 1st msg +msg)。 send(msg)。 } catch (IOException e ) { ()。 } doDelete()。 // kill itself because it has pleted its task. } } 文件三: package。 import .*。 import。 import。 import。 public class ObjectReceiver extends Agent { protected void setup() { addBehaviour(new CyclicBehaviour(this) { public void action() { try{ ACLMessage msg = blockingReceive()。 (getLocalName()+ rx msg+msg)。 if (JavaSerialization.equals(())) { Person p = (Person)()。 (getLocalName()+ 获取 Java对象: )。 (().getName())。 (())。 } } catch(UnreadableException e3){ (getLocalName()+ catched exception +())。 } block()。 } })。 } } 外部应用程序。
阅读剩余 0%
本站所有文章资讯、展示的图片素材等内容均为注册用户上传(部分报媒/平媒内容转载自网络合作媒体),仅供学习参考。 用户通过本站上传、发布的任何内容的知识产权归属用户或原始著作权人所有。如有侵犯您的版权,请联系我们反馈本站将在三个工作日内改正。