⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 trafficvolume.java

📁 人工智能中Agent开发包。多 Agent 系统是处理自治 Agent 之间知识层的协作问题
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
* The contents of this file are subject to the BT "ZEUS" Open Source 
* Licence (L77741), Version 1.0 (the "Licence"); you may not use this file 
* except in compliance with the Licence. You may obtain a copy of the Licence
* from $ZEUS_INSTALL/licence.html or alternatively from
* http://www.labs.bt.com/projects/agents/zeus/licence.htm
* 
* Except as stated in Clause 7 of the Licence, software distributed under the
* Licence is distributed WITHOUT WARRANTY OF ANY KIND, either express or 
* implied. See the Licence for the specific language governing rights and 
* limitations under the Licence.
* 
* The Original Code is within the package zeus.*.
* The Initial Developer of the Original Code is British Telecommunications
* public limited company, whose registered office is at 81 Newgate Street, 
* London, EC1A 7AJ, England. Portions created by British Telecommunications 
* public limited company are Copyright 1996-9. All Rights Reserved.
* 
* THIS NOTICE MUST BE INCLUDED ON ANY COPY OF THIS FILE
*/

/*
    this is a test of the Sourceforge CVS.
    Please ignore this comment!
*/

package zeus.visualiser.statistics;

import java.util.*;
import zeus.util.*;
import zeus.concepts.*;
import zeus.actors.rtn.Engine;

public class TrafficVolume {
   protected Hashtable  goalTraffic = new Hashtable();
   protected Hashtable  negotiationTraffic = new Hashtable();
   protected Hashtable  allTraffic = new Hashtable();
   protected Hashtable  referenceTable = new Hashtable();
   protected boolean    updatingGoalTraffic =  true;
   protected OntologyDb ontology;


   public TrafficVolume(OntologyDb ontology) {
      this.ontology = ontology;
   }

   public synchronized void clear() {
      allTraffic.clear();
      clearGoalTraffic();
   }

   public synchronized void clearGoalTraffic() {
      goalTraffic.clear();
      negotiationTraffic.clear();
      referenceTable.clear();
   }

   public boolean isUpdatingGoalTraffic() {
      return updatingGoalTraffic;
   }

   public synchronized void setUpdatingGoalTraffic(boolean set) {
      updatingGoalTraffic = set;
   }

   public synchronized void update(Performative msg) {
      updateTraffic(msg);
      updateGoalTraffic(msg);
   }

   protected void updateTraffic(Performative msg) {
/**
      allTraffic structure:

      sender --> Hashtable
                    |
                 receiver --> int[Performative.MESSAGE_TYPES]
*/
      String sender = msg.getSender();
      String receiver = msg.getReceiver();
      String type = msg.getType();

      Hashtable inner = (Hashtable)allTraffic.get(sender);
      if ( inner == null ) {
         inner = new Hashtable();
         allTraffic.put(sender,inner);
      }
      int[] data = (int[])inner.get(receiver);
      if ( data == null ) {
         data = new int[Performative.MESSAGE_TYPES.length];
         for(int i = 0; i < data.length; i++ )
            data[i] = 0;
         inner.put(receiver,data);
      }
      int j = Misc.whichPosition(type,Performative.MESSAGE_TYPES);
      Core.ERROR(j != -1,1,this);
      data[j] += 1;
   }

   protected void updateGoalTraffic(Performative msg) {
      if ( !updatingGoalTraffic ) return;

      MsgContentHandler hd;
      Goal g;
      String rootId;

      String sender = msg.getSender();
      String receiver = msg.getReceiver();
      String type = msg.getType();
      String reply_with = msg.getReplyWith();
      String in_reply_to = msg.getInReplyTo();
      String content = msg.getContent();
      String key = (reply_with != null) ? reply_with : in_reply_to;

      if ( type.equals("cfp") || type.equals("propose") ||
           type.equals("accept-proposal") || type.equals("reject-proposal") ) {
         g = ZeusParser.goal(ontology,content);
         rootId = g.getRootId();
         updateGoalTraffic(sender,receiver,type,rootId,key);
         updateNegotiationTraffic(sender,receiver,type,key,g);
      }
      else if ( referenceTable.containsKey(key) ) {
         if ( type.equals("cancel") || type.equals("failure") ) {
            g = ZeusParser.goal(ontology,content);
            rootId = g.getRootId();
            updateGoalTraffic(sender,receiver,type,rootId,key);
         }
         else if ( type.equals("inform") ) {
            hd = new MsgContentHandler(content);
            updateGoalTraffic(sender,receiver,hd.tag(),null,key);
         }
      }
   }

   protected void updateGoalTraffic(String sender, String receiver, String type,
                                    String rootId, String reply_tag) {
/**
      goalTraffic structure:

      rootId --> Hashtable
                    |
                  sender --> Hashtable
                                |
                             receiver --> int[Engine.COORDINATION_MESSAGE_TYPES]
*/

      if ( rootId == null ) {
         if ( (rootId = (String)referenceTable.get(reply_tag)) == null )
            rootId = "--UnknownId--";
      }

      referenceTable.put(reply_tag,rootId);

      Hashtable outer = (Hashtable)goalTraffic.get(rootId);
      if ( outer == null ) {
         outer = new Hashtable();
         goalTraffic.put(rootId,outer);
      }
      Hashtable  inner = (Hashtable)outer.get(sender);
      if ( inner == null ) {
         inner = new Hashtable();
         outer.put(sender,inner);
      }
      int[] data = (int[])inner.get(receiver);
      if ( data == null ) {
         data = new int[Engine.COORDINATION_MESSAGE_TYPES.length];
         for(int i = 0; i < data.length; i++ ) data[i] = 0;
         inner.put(receiver,data);
      }
      int j = Misc.whichPosition(type,Engine.COORDINATION_MESSAGE_TYPES);
      Core.ERROR(j != -1,2,this);
      data[j] += 1;
   }
   protected void updateNegotiationTraffic(String sender, String receiver,
      String msg_type, String reply_tag, Goal g) {
/**
      negotiationTraffic structure:

      rootId --> Hashtable
                    |
               goalId[Fact] --> Hashtable
                                 |
                           sender receiver --> Hashtable
                                                   |
                                                 sender --> Vector
*/
      String rootId = g.getRootId();
      String goalId = g.getId() + "[" + g.getFactType() + "]";
      double cost = g.getCost();

      // REM temp hack to avoid zero-value cost in graphs
      if ( Math.abs(cost) < 1.0E-12 ) return;

      if ( rootId == null ) {
         if ( (rootId = (String)referenceTable.get(reply_tag)) == null )
            rootId = "--UnknownId--";
      }

      referenceTable.put(reply_tag,rootId);

      Hashtable outer = (Hashtable)negotiationTraffic.get(rootId);
      if ( outer == null ) {
         outer = new Hashtable();
         negotiationTraffic.put(rootId,outer);
      }
      Hashtable inner = (Hashtable)outer.get(goalId);
      if ( inner == null ) {
         inner = new Hashtable();
         outer.put(goalId,inner);
      }
      String Id = sender.compareTo(receiver) > 0 ?
                  sender + " " + receiver : receiver + " " + sender;

      Hashtable innermost = (Hashtable)inner.get(Id);
      if ( innermost == null ) {
         innermost = new Hashtable();
         inner.put(Id,innermost);
      }

      Vector List = (Vector)innermost.get(sender);
      if ( List == null ) {
         List = new Vector();
         innermost.put(sender,List);
      }
      List.addElement(new Double(cost));
   }

   public String[] getDistributionByTypeLabels() {
      return Performative.MESSAGE_TYPES;
   }

   public synchronized double[] getDistributionByTypeData() {
      double result[] = new double[Performative.MESSAGE_TYPES.length];
      for(int i = 0; i < result.length; i++ ) result[i] = 0.0;
      Enumeration enum = allTraffic.elements();
      Hashtable inner;
      while( enum.hasMoreElements() ) {
         inner = (Hashtable)enum.nextElement();
         Enumeration elements = inner.elements();
         while( elements.hasMoreElements() ) {
            int[] data = (int[])elements.nextElement();
            for(int i = 0; i < result.length; i++ )
               result[i] += data[i];
         }
      }
      return result;
   }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -