📄 testagent.java
字号:
package com.sri.oaa2.test.agent;
import java.util.Hashtable;
import java.util.Map;
import com.sri.oaa2.test.agent.OaaTest_T;
import com.sri.oaa2.icl.IclTerm;
import com.sri.oaa2.icl.IclList;
import com.sri.oaa2.icl.IclDataQ;
import com.sri.oaa2.icl.IclStruct;
import com.sri.oaa2.icl.IclVar;
import com.sri.oaa2.icl.IclInt;
import com.sri.oaa2.icl.IclStr;
import com.sri.oaa2.icl.ToInt;
/**
* Test agent. This agent has the following solvables:
* <pre><code>
* add(Num1,Num2,Sum) - Adds Num1 to Num2 and puts the result in Sum.
* doNothing(Nothing) - Does nothing. Nothing is an unused var.
* makeString(Len,Ret) - Len is the length of the string and the agent sets
* a string for Ret with the input length.
* copyData(A,B) - Copies IclDataQ from A to B. Return result has IclDataQ
* for A AND B (ie, twice as much data).
* copyString(A,B) - Same as copyData but with IclStr.
* setData(Data,RetLength) - Data is input IclDataQ and RetLength is set by
* the agent as the number of bytes which was
* received.
* </code></pre>
*/
public class TestAgent extends OaaTest_T {
String delayId = null;
String addName = "add";
Map files = new Hashtable();
String onekstring, fivekstring, tenkstring, twentykstring, fiftykstring, sixtykstring;
public TestAgent() {
onekstring = makeString(1024);
fivekstring = makeString(5*1024);
tenkstring = makeString(10*1024);
twentykstring = makeString(20*1024);
fiftykstring = makeString(50*1024);
sixtykstring = makeString(60*1024);
}
public TestAgent(String addName) {
setAddName(addName);
}
public String getAgentName() {
return "TestAgent";
}
public String getAgentCapabilities() {
return "[" + addName + "(Num1,Num2,Sum), doNothing(Nothing), copyData(A,B), " +
"copyString(A,B), setData(Data,RetLength), makeString(Len,Ret)]";
}
String makeString(int size) {
if (size == 1024 && onekstring != null) {
return onekstring;
} else if (size == 5*1024 && fivekstring != null) {
return fivekstring;
} else if (size == 10*1024 && tenkstring != null) {
return tenkstring;
} else if (size == 20*1024 && twentykstring != null) {
return twentykstring;
} else if (size == 50*1024 && fiftykstring != null) {
return fiftykstring;
} else if (size == 60*1024 && sixtykstring != null) {
return sixtykstring;
}
debug("Creating string of size " + size);
StringBuffer s = new StringBuffer();
for (int indx = 0; indx < size; indx++) {
char c = (char)('a' + Math.round(Math.random()*('z' - 'a')));
s.append(c);
}
return s.toString();
}
public void setAddName(String addName) {
this.addName = addName;
}
public boolean oaaDoEventCallback(IclTerm goal,
IclList params,
final IclList answers) {
/* If the event coming in is "add", grab args and return response */
if (goal.toIdentifyingString().equals("setData")) {
int length = 0;
IclTerm inTerm = goal.getTerm(0);
if (inTerm instanceof IclDataQ) {
IclDataQ dataTerm = (IclDataQ)inTerm;
byte[] data = dataTerm.getData();
length = data.length;
}
// Don't actually send the data back...
answers.add(new IclStruct(goal.toIdentifyingString(), new IclVar("Data"), new IclInt(length)));
} else if (goal.toIdentifyingString().equals("makeString")) {
int size = ((IclInt)goal.getTerm(0)).toInt();
String s = makeString(size);
answers.add(new IclStruct(goal.toIdentifyingString(), new IclInt(size),
new IclStr(s)));
return true;
} else if (goal.toIdentifyingString().equals("copyString") ||
goal.toIdentifyingString().equals("copyData")) {
IclTerm inTerm = goal.getTerm(0);
if (inTerm instanceof IclDataQ) {
IclDataQ dataTerm = (IclDataQ)inTerm;
dataTerm.getData();
}
IclTerm outClone = (IclTerm)inTerm.clone();
IclTerm outClone2 = (IclTerm)inTerm.clone();
answers.add(new IclStruct(goal.toIdentifyingString(), outClone,
outClone2));
return true;
} else if (goal.toIdentifyingString().equals("doNothing")) {
answers.add(new IclStruct(goal.toIdentifyingString(), new IclVar("Nothing")));
return true;
/* If the event coming in is "add", grab args and return response */
} else if (goal.toIdentifyingString().equals(addName)) {
int Num1 = ToInt.getInstance().from(goal.getTerm(0));
int Num2 = ToInt.getInstance().from(goal.getTerm(1));
int Sum = Num1 + Num2;
/* The answers returned must "match" the goal, but with
variables filled in. So since requests will arrive
as "add(1,2,X)", we should return "add(1,2,3)".
OAA is currently not set up to handle functions,
ie. "add(1,2)" returns "3", because some languages
(e.g. Prolog) do not work well in this mode.
Note: OAA can handle if there are multiple answers
to a question, which is why answers is a list.
Example: "sqrt(4, X)" should return two answers,
"sqrt(4, 2)" and "sqrt(4, -2)"
*/
answers.add(
new IclStruct(addName, new IclInt(Num1), new IclInt(Num2),
new IclInt(Sum)));
if (delayId != null) {
debug("Delaying solution with id " + delayId);
getLibOaa().oaaDelaySolution(delayId);
new Thread() {
public void run() {
try { Thread.sleep(2000L); } catch (Exception ex) {}
debug("Returning delayed solution");
getLibOaa().oaaReturnDelayedSolutions(delayId, answers);
}
}.start();
}
return true;
}
return super.oaaDoEventCallback(goal, params, answers);
}
public void setDelay(String delayId) {
this.delayId = delayId;
}
public static void main(String[] args) {
TestAgent agent = new TestAgent();
try {
agent.facilitatorConnect(args);
agent.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -