frmsample.java
来自「SRI international 发布的OAA框架软件」· Java 代码 · 共 174 行
JAVA
174 行
/* Sample OAA Agent
This tiny agent sample agent could be a shell for an agent wrapper around
a fax program. It publishes the solvable "fax(Dest, Document)" and sets
up a callback to handle these requests.
*/
package com.sri.oaa2.agt.sampleAgent;
/* Include dependencies */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.sri.oaa2.com.*;
import com.sri.oaa2.lib.*;
import com.sri.oaa2.icl.*;
import com.sri.oaa2.guiutils.*;
import antlr_oaa.RecognitionException;
import antlr_oaa.TokenStreamException;
public class FrmSample extends JFrame {
/*******************************************************************************
* 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()));
* </pre>
*
* It takes a communication protocol object which specifies the transport layer
* for communication. The current transport protocol is TCP/IP.
*******************************************************************************/
LibOaa myOaa = new LibOaa(new LibCom(new LibComTcpProtocol()));
public FrmSample() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch (Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
this.setTitle("Fax Agent");
//this.getContentPane().setLayout(null);
this.setSize(new Dimension(200, 50));
// Connect Button
JButton btnConnect = new JButton("Connect");
btnConnect.setBounds(new Rectangle(0, 20, 399, 40));
btnConnect.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
// First, connects to the facilitator
if (myOaa.getComLib().comConnected("parent")) {
System.out.println("Already connected");
return;
}
if (!myOaa.getComLib().comConnect(
"parent",
new IclStruct("tcp",
new IclVar("A"),
new IclVar("B")),
new IclList()) {
System.out.println("Couldn't connect to the facilitator");
return;
}
// Once the connection is established, performs handshaking with the facilitator
if (!myOaa.oaaRegister("parent", "fax", IclTerm.fromString(true, "[fax(Destination,Document)]"), new IclList()))
{
System.out.println("Could not register");
return;
}
// Connection succeeded!
myOaa.oaaReady(true);
}
});
this.getContentPane().add(btnConnect, null);
// Register OAA callback
myOaa.oaaRegisterCallback("app_do_event",
new OAAEventListener() {
public boolean doOAAEvent(IclTerm goal, IclList params, IclList answers)
{
return oaaDoEventCallback(goal, params, answers);
}
});
}
//Overriden so we can exit on System Close
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
// Pretend to send a document to faxNumber
boolean myFaxAPI(String faxNum, String doc) {
return true;
}
// Defining the capabilities registered with Facilitator
public boolean oaaDoEventCallback(IclTerm goal, IclList params, IclList answers) {
// fax(Person, Document)
String pred;
if(goal.isStruct()) {
pred = ((IclStruct)goal).getFunctor();
}
else {
return false;
}
if (pred.equals("fax")) {
// pull out args as Strings
String person = goal.getTerm(0).toString();
String doc = goal.getTerm(1).toString();
// see if we can find fax number for person
IclList local_answers = new IclList();
// If OK, pull out result and call our low-level fax API
try {
if (myOaa.oaaSolve(
IclTerm.fromString("fax_num(" + person + ",N)"),
new IclList(),
local_answers)) {
String faxNum = local_answers.getTerm(1).toString();
if (myFaxAPI(faxNum, doc)) {
// Build the answers list.
answers.add(0,
IclTerm.fromString("["+goal.toString()+"("+params.toString()+")]"));
return true;
}
}
}
catch(RecognitionException re) {
System.err.println("Ignoring RecognitionException");
re.printStackTrace();
return false;
}
catch(TokenStreamException tse) {
System.err.println("Ignoring TokenStreamException");
tse.printStackTrace();
return false;
}
}
// Fails
return false;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?