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

📄 frmmonitor.java

📁 SRI international 发布的OAA框架软件
💻 JAVA
📖 第 1 页 / 共 4 页
字号:

/**
 * Removes a specific agent from monitor window
 */
  void removeAgent(agtInfo agt)
  {
     if (agt != null) {
        // if removing selected agent, unselect
        if (agt == selectedAgent)
           unselectAgent();

        setStatus("Agent removed: "+agt.name + " (" + agt.shortId + ")", false);
        agtList.removeElement(agt);
        if (agt.agtLabel != null)
           pnlMonitor.remove(agt.agtLabel);
        pnlMonitor.remove(agt);
        // If no more agents, gray out appropriate toolbar buttons
        // Actually, there can never be no agents, since the monitor agent is
        // connected itself.  This can only happen if we disconnect.
        if (pnlMonitor.getComponentCount() == 1)
           enableAgentFunctions(false);
        pnlMonitor_componentResized(null);
        pack();
        repaint();

        if (chartWindow.isVisible())
           chartWindow.refreshBarInfo();
     }
  }

/**
 * Given an agent's ID (or InfoList)
 * removes the agent from our agent list and display
 */
  void RemoveAgentById(IclTerm id_or_info) {
     IclTerm id;
     if (id_or_info.isList()) {
        // extract data from infoList
        id = icl_address_to_id(id_or_info.getTerm(0));
     }
     else id = id_or_info;
     // Remove agent
     if (id != null) {
       String idName;
       if(!id.isStruct()) {
         idName = id.toString();
       }
       else {
         idName = ((IclStruct)id).getFunctor();
       }
        removeAgent(findAgentByIdName(id.toString(), null));
     }
  }

/**
 * Searches for an agent by its id (first), name, or both
 * If agent is not found, return null
 */
  agtInfo findAgentByIdName(String id, String name) {
     int i = 0;
     boolean found = false;
     agtInfo agt = null;

     while ((i < agtList.size()) && (!found)) {
        agt = (agtInfo)agtList.elementAt(i);
        // If id given try that first no matter what the name
        if (id != null)
           found = (agt.shortId.equals(id));
        else
           found = ((name != null) && agt.name.equals(name));
        i = i + 1;
     }
     if (found)
        return(agt);
     else return(null);
  }

/**
 * calls agt.resetAgentInfo on each agent in list
 * This resets event-specific information such as
 * number of bytes sent, etc.
 */
  void resetAllAgentInfo() {
     int i = 0;
     agtInfo agt = null;

     for (i = 0; i < agtList.size(); i++) {
        agt = (agtInfo)agtList.elementAt(i);
        agt.resetAgentInfo();
     }
     totalBytesSent = 0;
     totalBytesReceived = 0;
     maxBytesSent = 0;
     maxBytesReceived = 0;
     lblAgentSend.setText("Sent: ");
     lblAgentReceive.setText("Received: ");
     lblTotalMsgs.setText("Total # msgs: ");
     lblTotalBytes.setText("Total bytes: " + (totalBytesSent + totalBytesReceived));
  }

/**
 * Removes all agents and cleans up when monitor disconnects from facilitator.
 */
  void cleanupAfterDisconnect() {
     int i = agtList.size()-1;
     agtInfo agt = null;

     // remove all agents
     while (i >= 0) {
        agt = (agtInfo)agtList.elementAt(i);
        removeAgent(agt);
        i = i - 1;
     }
     // resize display accordingly
     pnlMonitor_componentResized(null);
     pack();
     // Clear log info
     evBrowser.btnClear_actionPerformed(null);
  }


  /**
   * When a user clicks on the Facilitator button, find
   * agent with ID '0' from agentlist, and call it's clicked
   * event.
   */
  public void btnFacilitatorClicked() {
      agtInfo fac = findAgentByIdName("0", null);
      
      if (fac != null) { 
         ActionEvent ev = new ActionEvent(fac, 0, "Clicked");
         agentClicked(ev);
      }
  }
  
/**
 * When a user clicks on an agent, we display
 * information about the agent in the information panel
 */
  public void agentClicked(ActionEvent e) {
     agtInfo agt = (agtInfo)e.getSource();
     String t[] = new String[2];

     evBrowser.chkOneAgent.setEnabled(true);

     selectedAgent = agt;
     setStatus(" ", false);
     lblAgentName.setText(" Name: " + agt.name + " (" + agt.shortId + ")");
     if (cboSolvables.getModel().getSize() > 0)
        cboSolvables.removeAllItems();
     for (int i = 0; i < agt.solvables.size(); i++) {
        cboSolvables.addItem(agt.solvables.getTerm(i).toString());
     }
     cboSolvables.setEnabled(true);

     lblAgentLanguage.setText("Language: " + agt.language);
     lblAgentVersion.setText("OAA Version: " + agt.version);
     lblAgentHost.setText("Host: " + agt.host);

     lblAgentSend.setText("Agent sent: " + agt.bytesSent + " bytes");
     lblAgentReceive.setText("Agent received: " + agt.bytesReceived + " bytes");

     if (evBrowser.chkOneAgent.isSelected())
        evBrowser.chkOneAgent_actionPerformed(null);

  }

/**
 * If an agent being removed is the selected agent, unselect the agent
 */
  void unselectAgent() {
     selectedAgent = null;
     setStatus(" ", false);
     lblAgentName.setText(" Name: ");
     if (cboSolvables.getModel().getSize() > 0)
        cboSolvables.removeAllItems();
     cboSolvables.setEnabled(false);

     lblAgentLanguage.setText("Language: ");
     lblAgentVersion.setText("OAA Version: ");
     lblAgentHost.setText("Host: ");

     lblAgentSend.setText("Agent sent: ");
     lblAgentReceive.setText("Agent received: ");

     if (evBrowser.chkOneAgent.isSelected()) {
        evBrowser.chkOneAgent.setSelected(false);
        evBrowser.chkOneAgent_actionPerformed(null);
     }
  }

/**
 * loads all agents from the facilitator's meta data
 */
  void loadAgents() {
     btnFacilitator.setEnabled(true);
     // The facilitator stores info about an agent in "agent_data([Id,Type,Status,Sv,Name,Info])"

     // read asynchronously
     oaa.oaaSolve(IclTerm.fromString(true, "agent_data(Id,Type,Status,Sv,Name,Info)"),new IclList(IclTerm.fromString(true, "block(false)")),
                   null);     
  }

  // Called after the results of reading agent_data
  
  void loadAgentsContinued(IclTerm solutionList) {
    
     // System.out.println("***** loadAgentsContinued ******");
     // System.out.println(solutionList);
     // System.out.println("***********");
    
     IclTerm me;

     // Read data on all currently connected agents
     // Loop over them, store info, and add icons
     for (int i = 0; i < solutionList.size(); i++) {
        // extract: [Id, Status, Solvables, Name]
        AddAgentInfo(solutionList.getTerm(i), true);
     }

     // look up my own ID
     me = oaa.oaaPrimaryAddress();

     // get all information about agents from Facilitator
     oaa.oaaSolve(IclTerm.fromString(true, "agent_host(Id,Name,Host)"),
                  new IclList(IclTerm.fromString(true, "block(false)")),
                   null);
     oaa.oaaSolve(IclTerm.fromString(true, "agent_version(Id,Lang,Vers)"),
                  new IclList(IclTerm.fromString(true, "block(false)")),
                  null);
     
     // Add trigger to trace any agents coming or going.  Since their data is
     // stored in agent_data(), updates to this predicate indicate change.
     oaa.oaaAddTrigger(
      new IclStr("data"), 
      IclTerm.fromString(true, "agent_data(Id,Type,ready,Sv,Name,Info)"),
      IclTerm.fromString(true, "oaa_Solve(m_data(Op,[Id,Type,ready,Sv,Name,Info]),[reply(none),address(" + me.toString() + ")])"),
      new IclList(new IclStruct("recurrence", new IclStr("whenever")),
                  //new IclStruct("address", new IclStr("parent")),
                  new IclStruct("on", new IclVar("Op"))));

     setStatus("Agents loaded.  Click on an agent for agent-specific information.", true);
     logAllEvents(me);

  }

  void logAllEvents(IclTerm me) {

      // DEBUG
      //System.out.println("---> Log all events");

      // Add trigger to trace any events from or to agents.
      // Don't trace events coming from me, or from any other monitor
      // agents!
      // Do this in two steps, one for receive and once for send, because
      // constraint check is slightly different.
      
      IclTerm recTest = IclTerm.fromString(true, "(memberchk(from(Id),P),agent_data(Id,Type,Status,Sv,Name,Info),not(memberchk(solvable(m_ev(_,_,_),_,_),Sv)))");
      IclList recParams = (IclList)IclTerm.fromString(true, "[recurrence(whenever),address(parent),on(receive)]"); 
      recParams.add(new IclStruct("test", recTest));
      oaa.oaaAddTrigger(
       new IclStr("comm"), 
       IclTerm.fromString(true, "event(E,P)"),
       IclTerm.fromString(true, "oaa_Solve(m_ev(receive,Id,E),[reply(none),address(" + me.toString() + ")])"),
       recParams);
         
      IclTerm sendTest = IclTerm.fromString(true, "(memberchk(address(Id),P),agent_data(Id,Type,Status,Sv,Name,Info),not(memberchk(solvable(m_ev(_,_,_),_,_),Sv)))");
      IclList sendParams = (IclList)IclTerm.fromString(true, "[recurrence(whenever),address(parent),on(send)]");
      sendParams.add(new IclStruct("test", sendTest));
      oaa.oaaAddTrigger(
       new IclStr("comm"), 
       IclTerm.fromString(true, "event(E,P)"),
       IclTerm.fromString(true, "oaa_Solve(m_ev(send,Id,E),[reply(none),address(" + me.toString() + ")])"),
       sendParams);
  }

  void btnShowLog_actionPerformed(ActionEvent e) {
     setStatus(" ", false);
     evBrowser.rbLevel_actionPerformed(null);
     evBrowser.setVisible(true);
     evBrowser.toFront();
     evBrowser.pack();
  }

  // Increase size of circle used to position agent buttons
  void btnBiggerRadius_actionPerformed(ActionEvent e) {
     radiusRatio = radiusRatio + 0.1;
     // resize display accordingly
     pnlMonitor_componentResized(null);
     pack();
  }

  // Decrease size of circle used to position agent buttons
  void btnSmallerRadius_actionPerformed(ActionEvent e) {
     radiusRatio = radiusRatio - 0.1;
     // resize display accordingly
     pnlMonitor_componentResized(null);
     pack();
  }


  void mnuSetFilter_actionPerformed(ActionEvent e) {
     evFilter.setVisible(true);
     evFilter.toFront();
  }

  void btnNextEvent_actionPerformed(ActionEvent e) {
     evBrowser.nextEvent();
  }
  void btnPrevEvent_actionPerformed(ActionEvent e) {
     evBrowser.prevEvent();
  }
  void btnFirstEvent_actionPerformed(ActionEvent e) {
     if (e.getSource() == btnFirstEvent)
        evBrowser.firstEvent();
     else
     if (e.getSource() == btnLastEvent)
        evBrowser.lastEvent();
     else
        evBrowser.firstEvent();
  }

/**
 * Updates the status bar, and sets its border to yellow
 * for important events, to catch the user's eye
 */
  void setStatus(String statusText, boolean highlight) {
     if (highlight)
        lblStatus.setBackground(Color.yellow);
     else lblStatus.setBackground(Color.lightGray);
     lblStatus.setText(statusText);
  }


  boolean fromToOk(String f, String KS, String Op) {
     int p = f.indexOf("("+KS+")");
     if (p >= 0) {
        if (f.indexOf("FROM/TO:") >= 0)
            return true;
        else
        if ((f.indexOf("FROM:") >= 0) && (f.indexOf("FROM:") < 5))
           return Op.equals("on_receive");
        else
        if ((f.indexOf("TO:") >= 0) && (f.indexOf("TO:") < 3))
           return Op.equals("on_send");
     }
     return false;
  }

/**
 * Returns true if an event should be added to the event
 * log.  Events can be filtered using the evtFilter window.
 */
  boolean shouldLogEvent(String op, String KS, String event) {
     int p;

     if ((evFilter == null) || (evFilter.vecFilter.size() == 0))
        return true;

     for (int i = 0; i < evFilter.vecFilter.size(); i++) {
        String f = (String) evFilter.vecFilter.elementAt(i);
        if (fromToOk(f, KS, op))
           return true;
        else
        if (Unifier.getInstance().unify(IclTerm.fromString(true ,f), IclTerm.fromString(true, event)) != null)
           return true;
     }
     return false;
  }

  void setNewLookAndFeel(LookAndFeel lf) {
      try  {
        UIManager.setLookAndFeel(lf);
        SwingUtilities.updateComponentTreeUI(this.getRootPane());
        this.validate();
      }
      catch (Exception ex) {
        ex.printStackTrace();
      }
   }

   /** 
    * Given a full address return only the Id
    */
   private IclTerm icl_address_to_id(IclTerm inFullAddress) {   
     IclTerm tempTerm = IclTerm.fromString(true, "addr(FacAddr, Id)");
     if((Unifier.getInstance().unify(inFullAddress, tempTerm)) != null) {
       return inFullAddress.getTerm(1);
     }
     return new IclStr("0");
   }

   static public URL getAgentIconURL(String name) {
     URL url;
     ImageIcon icon = null;
     
     url = frmMonitor.class.getResource("images/" + name + ".gif");
     if (url == null) {
       url = frmMonitor.class.getResource("images/agent.gif");
     }
     return url;
   }

}    // END OF FRMMONITOR CLASS

⌨️ 快捷键说明

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