frmmain.java
来自「SRI international 发布的OAA框架软件」· Java 代码 · 共 822 行 · 第 1/2 页
JAVA
822 行
/*
** Title: frmMain
** Version:
** Copyright: Copyright (c)1999 SRI International
** Author: Adam Cheyer
** Description: The main form for debugAgent
*/
/**
* The contents of this file are subject to the OAA Community Research
* License Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License
* at http://www.ai.sri.com/~oaa/. Software distributed under the License
* is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
* express or implied. See the License for the specific language governing
* rights and limitations under the License. Portions of the software are
* Copyright (c) SRI International, 1999. All rights reserved.
* "OAA" is a registered trademark, and "Open Agent Architecture" is a
* trademark, of SRI International, a California nonprofit public benefit
* corporation.
*/
package com.sri.oaa2.agt.debugAgent;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.BevelBorder;
import com.sri.oaa2.com.*;
import com.sri.oaa2.lib.*;
import com.sri.oaa2.icl.*;
import com.sri.oaa2.guiutils.*;
/**
* frmMain is the main window for application debugAgent.
* <p>
* This class shows a simple example of OAA usage with the Java Agent Library.
* It contains a LibOaa object which is used to interact with OAA. The Debug Agent
* provides means for requesting services (solve requests), posting events,
* adding triggers or data to the OAA system.
* <p>
* Some methods which perform OAA specific tasks are:
* <li> oaaSolve(IclTerm goal, IclTerm params) <br>
* <li> oaaPostEvent(IclTerm event, IclTerm params) <br>
* <li> oaaAddTrigger(IclTerm type, IclTerm condition, IclTerm action, IclTerm params) <br>
* <li> oaaAddData(IclTerm clause, IclList params)<br>
*
* @author Adam Cheyer
* @version %I%, %G%
*
*/
public class frmMain extends JFrame
{
// Visual elements
JMenuBar menuBar1 = new JMenuBar();
JMenu menuFile = new JMenu();
JMenuItem mnuConnect = new JMenuItem();
JMenuItem mnuExit = new JMenuItem();
JMenu menuOptions = new JMenu();
JCheckBoxMenuItem mnuWindowsLook = new JCheckBoxMenuItem();
JCheckBoxMenuItem mnuMotifLook = new JCheckBoxMenuItem();
JCheckBoxMenuItem mnuMetalLook = new JCheckBoxMenuItem();
JCheckBoxMenuItem mnuNoDisplay = new JCheckBoxMenuItem();
JMenu menuHelp = new JMenu();
JMenuItem menuHelpAbout = new JMenuItem();
JMenuItem mnuHelp = new JMenuItem();
BorderLayout borderLayout1 = new BorderLayout();
JToolBar jtoolbar1 = new JToolBar();
JButton btnClearWindow = new JButton();
JButton btnSolve = new JButton();
JButton btnTraceAll = new JButton();
JButton btnTraceFac = new JButton();
JButton btnDebugOne = new JButton();
JButton btnAgentInfo = new JButton();
JScrollPane jScrollPane1 = new JScrollPane();
JList lstEvents = new JList();
JLabel lblEnglish = new JLabel();
JLabel lblAction = new JLabel();
JLabel lblStatus = new JLabel();
BevelBorder bevBorder = new BevelBorder(BevelBorder.RAISED);
JPanel jPanel1 = new JPanel();
JComboBox txtEnglish = new JComboBox();
JComboBox txtAction = new JComboBox();
JButton btnSendEng = new JButton();
JButton btnSendAct = new JButton();
//string constants for Tooltips
static final String TIP_SOLVE_GOAL = "oaa_Solve(yourGoal, [])";
static final String TIP_TRACE_ALL = "oaa_PostEvent(ev_post_event(all, ev_trace_on), [])";
static final String TIP_TCP_TRACE_FACILITATOR = "oaa_PostEvent(ev_com_trace_on, [])";
static final String TIP_AGENT_HOST = "oaa_Solve(agent_host(Addr,Name,Host), [])";
static final String TIP_AGENT_INFO = "oaa_Solve(agent_data(Agt,Type,Status,Solvables,Name,Info), [])";
// Nonvisual elements
Vector evtLog = new Vector(); // Stores text for event window
/*******************************************************************************
* The LibOaa object is the Java Agent Library object which is used to interact
* with the OAA system.
*
* To create an agent, create a new instance of the OAA library, passing some
* communication protocol object to use as the transport layer:
*
* <pre>
* LibOaa myOaa = new LibOaa(new LibCom(new LibComTcpProtocol()), cmdArgs);
* </pre> * * It takes a communication protocol object which specifies the transport layer * for communication. The current transport protocol is TCP/IP.
*******************************************************************************/
LibOaa myOaa;
// Command line parameters
String[] mArgs;
/****************************************************************************************
* Calls methods which initialize the OAA library.
* @see #jbInit
*****************************************************************************************/
public frmMain(String[] inArgs){
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
mArgs=inArgs;
try
{
jbInit();
}
catch (Exception e)
{
e.printStackTrace();
}
connectOaaProcess(true);
}
/***************************************************************************************
* Initializes all user interface components and registers all OAA callbacks.
*
* OAA callbacks are registered using the oaaRegisterCallback() method of the OAA
* library.
*
* @see com.sri.oaa2.lib.LibOaa#oaaRegisterCallback
****************************************************************************************/
private void jbInit() throws Exception {
// Main form
this.setSize(new Dimension(655, 400));
this.setTitle("OAA Debug Interface");
this.getContentPane().setLayout(borderLayout1);
initMenus();
initToolbars();
// Msg window
lstEvents.setFont(new Font("Courier", 0, 12));
jScrollPane1.getViewport().add(lstEvents, null);
// Command Panel
initCommandPanel();
initMyOaa();
}
private void initMyOaa() {
myOaa = new LibOaa(new LibCom(new LibComTcpProtocol(), mArgs));
// Register all OAA callbacks
myOaa.oaaRegisterCallback("app_do_event",
new OAAEventListener() {
public boolean doOAAEvent(IclTerm goal, IclList params, IclList answers)
{
return oaaDoEventCallback(goal, params, answers);
}
});
myOaa.oaaRegisterCallback("app_init",
new OAAEventListener() {
public boolean doOAAEvent(IclTerm goal, IclList params, IclList answers)
{
return oaaAppInitCallback(goal, params, answers);
}
});
}
/**************************************************************************
* oaaDoEventCallback()
*
* OAA Event Callback for "app_do_event"
*************************************************************************/
/* Asynchronous events :
goal = ev_solved(g_63, // Goal Id
[addr(tcp('130.107.64.126',4201),5)], // Requestees
[addr(tcp('130.107.64.126',4201),5)], // Solvers
d(_10909), // Goal
[block(false)], // Goal's params
[d(2),d(1)]) // Solution
*/
public boolean oaaDoEventCallback(IclTerm goal, IclList params, IclList answers)
{
// Debug:
// lblStatus.setText("oaa_AppDoEvent " + goal);
//TODO: take action as a result of doEvent call
int arity = goal.size();
String pred;
if(goal.isStruct()) {
pred = ((IclStruct)goal).getFunctor();
}
else {
pred = goal.toString();
}
if (pred.equals("ev_solved")) {
// Gets all goal components
IclTerm goalId = goal.getTerm(0);
IclTerm requestees = goal.getTerm(1);
IclTerm solvers = goal.getTerm(2);
IclTerm initialGoal = goal.getTerm(3);
IclTerm goalParams = goal.getTerm(4);
IclList solutions = (IclList)goal.getTerm(5);
if (solutions.size()>0) {
// Performs the actual solution information
addLog("Received " + solutions.size() +
" solution(s) for : " + initialGoal +
" from " + solvers);
for (int i = 0; i < solutions.size(); i++)
addLog(" " + i + " : " + solutions.getTerm(i).toString());
}else
addLog("No solutions found for : " + initialGoal);
} else
if (pred.equals("inform_ui") && (arity==2)) {
addLog("inform_ui : " + goal.getTerm(1));
answers.add(goal);
}
return true;
}
/**************************************************************************
* oaaAppInitCallback()
*
* OAA Event Callback for "app_init"
*************************************************************************/
public boolean oaaAppInitCallback(IclTerm goal, IclList params, IclList answers)
{
lblStatus.setText("oaa_AppInit " + goal);
return true;
}
/***********************************************************************************
* UI Event Callbacks
*
* btnShortCut_actionPerformed() - buttons set text for txtAction input field
* btnSendEng_actionperformed() - English has been typed. Request NL translation.
* btnSendAct_actionperformed() - ICL has been typed. Send directly to OAA.
* mnuOaaConnect_actionPerformed() - Connection/disconnect to OAA requested.
***********************************************************************************/
public void btnShortCut_actionPerformed(ActionEvent e)
{
JButton btn = (JButton)e.getSource();
txtAction.setSelectedItem(btn.getToolTipText());
txtAction.requestFocus(); // Why doesn't this work?
}
boolean btnSendEng_actionPerformed(ActionEvent e)
{
lblStatus.setText("");
if(myOaa == null || myOaa.oaaIsConnected("parent") == false){
lblStatus.setText("Not connected to facilitator");
return false;
}
String sEnglishQuery = (String)txtEnglish.getSelectedItem();
if(sEnglishQuery == null) {
lblStatus.setText("Invalid request");
return false;
}
txtEnglish.insertItemAt(sEnglishQuery, 0);
txtEnglish.setSelectedItem("");
IclTerm goal = new IclStruct("nl_to_icl",
new IclStr(sEnglishQuery),
new IclList(),
new IclVar("X"));
IclList params = new IclList();
IclList localAnswers = new IclList();
if(myOaa.oaaSolve(goal, params, localAnswers)) {
IclTerm newGoal = localAnswers.getTerm(0).getTerm(2);
String sNLAnswer = newGoal.toString();
return callOaa(sNLAnswer);
}else{
lblStatus.setText("Parse failed");
return false;
}
}
boolean btnSendAct_actionPerformed(ActionEvent e)
{
lblStatus.setText("");
if(myOaa == null || myOaa.oaaIsConnected("parent") == false)
{
lblStatus.setText("Not connected to facilitator");
return false;
}
String eventString = (String) txtAction.getSelectedItem();
if(eventString == null)
{
lblStatus.setText("Empty goals/params not accepted");
return false;
}
txtAction.setSelectedItem("");
return callOaa(eventString);
}
void mnuOaaConnect_actionPerformed() {
connectOaaProcess(false);
}
void connectOaaProcess(boolean onlyIfAutoconnect)
{
if(myOaa == null){
initMyOaa();
}
// If connected, disconnect
if (myOaa.oaaIsConnected("parent"))
{
myOaa.oaaDisconnect("parent", new IclList());
myOaa = null;
mnuConnect.setText("OAA Connect");
lblStatus.setText("Disconnected from Facilitator");
}
else
{
// Since we provide a dialog box for the machine to connect
// to, we are looking for the address where the facilitator
// lives.
IclTerm remote_host = null;
IclTerm remote_port = null;
IclTerm remote_address = LibComUtils.oaaResolveVariable("oaa_connect", mArgs);
if (remote_address != null) {
remote_host = remote_address.getTerm(0);
remote_port = remote_address.getTerm(1);
}else {
// Looks in the setup.pl file
remote_address = LibComUtils.oaaResolveVariable("default_facilitator", mArgs);
if (remote_address != null) {
if (remote_host==null)
remote_host = remote_address.getTerm(0);
if (remote_port==null)
remote_port = remote_address.getTerm(1);
}
}
// Checks for autoconnection
IclTerm localAutoConnect = LibComUtils.oaaResolveVariable("oaa", mArgs);
OaaConnectDialog connectDialog = new OaaConnectDialog(this, "OAA Connect", true);
if ((remote_host != null)&&(remote_port != null)) {
String host = remote_host.toString();
int port = ToInt.getInstance().from(remote_port, -1);
connectDialog.setHostName(host);
connectDialog.setPortNumber(port);
if ((localAutoConnect != null)&&(onlyIfAutoconnect)) // Connect immediately
if (connectOaa(host, port))
return;
} else { // Couldn't find setup file, use blank values
connectDialog.setHostName("unknown");
connectDialog.setPortNumber(0);
}
if (!onlyIfAutoconnect) {
// Centers and shows the dialog box
Dimension dlgSize = connectDialog.getPreferredSize();
Dimension frmSize = getSize();
Point loc = getLocation();
connectDialog.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y);
connectDialog.setVisible(true);
if (connectDialog.mOk)
connectOaa(connectDialog.getHostName(), connectDialog.getPortNumber());
}
}
}
//**************************************************************
// OAA Connection / Deconnection managment
//**************************************************************
private boolean connectOaa(String inHostName, int inPortNumber) {
// First, connects to the facilitator
if (myOaa.getComLib().comConnected("parent")) {
lblStatus.setText("Already connected");
return false;
}
System.out.println("Trying to connect to: " + inHostName + "," + inPortNumber);
// First, connects to the facilitator
if (!myOaa.getComLib().comConnect("parent",
new IclStruct("tcp", new IclStr(inHostName),
new IclInt(inPortNumber)),
new IclList())) {
System.out.println("Couldn't connect to " + inHostName + "," + inPortNumber);
return false;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?