testclient.c
来自「SRI international 发布的OAA框架软件」· C语言 代码 · 共 393 行
C
393 行
// $Id: TestClient.c,v 1.1 2003/08/15 01:32:31 giuli Exp $
#include <stdio.h>
#include <string.h>
#include <libicl.h>
#include <liboaa.h>
#include <libdb.h>
#ifndef _WINDOWS
#include <sys/time.h>
#else
#include <sys/types.h>
#include <sys/timeb.h>
#endif
#define REPEATS 1
#define LOOPS 1
static long solveCounter = 0;
static int getAgentAddress(const char *agentName, ICLTerm **agentAddress);
static int runDoNothingTests(const char *agentToTest);
static int runMakeStringTests(const char *agentToTest);
static int runTest(const char *label,
const char *goalString,
const char *paramString,
const int repeats);
static int runTests(const char *agentToTest,
const char *goalString,
const char* goalTemplateString);
static int setup_oaa_connection(int argc, char *argv[]);
static int timed_Solve(ICLTerm *goal,
ICLTerm *in_params,
ICLTerm **out_params,
ICLTerm **solutions,
int *millisecondsToSolve);
int main(int argc, char *argv[]) {
int i = 0;
int passed = FALSE;
int answer = 0;
oaa_Init(argc, argv);
setup_oaa_connection(argc, argv);
for (i = 1; i <= LOOPS; i++) {
printf("\nStart Loop %d\n\n", i);fflush(stdout);
passed = runDoNothingTests("TestAgent");
passed = runMakeStringTests("TestAgent")
&& passed;
printf("\nEnd Loop %d\n\n", i);fflush(stdout);
printf("\nTotal Solve Requests: %d\n\n", solveCounter);fflush(stdout);
}
if (passed) {
printf("All tests passed\n\n");
answer = 0;
}
else {
printf("At least one test failed\n\n");
answer = 1;
}
oaa_Disconnect(NULL, NULL);
return answer;
}
static int getAgentAddress(const char *agentName, ICLTerm **agentAddress) {
ICLTerm *goal = NULL;
ICLTerm *in_params = icl_NewList(NULL);
ICLTerm *out_params = NULL;
ICLTerm *solutions = NULL;
int answer = FALSE;
char buf[256];
memset(&buf,0,sizeof(buf));
sprintf(buf,"agent_host(_,%s,_)",agentName);
goal = icl_NewTermFromString(buf);
answer = oaa_Solve(goal, in_params, &out_params, &solutions);
// get the first address
*agentAddress = icl_CopyTerm(icl_NthTerm(icl_NthTerm(solutions, 1), 1));
icl_Free(goal);
icl_Free(in_params);
icl_Free(out_params);
icl_Free(solutions);
return answer;
}
static int runDoNothingTests(const char *agentToTest) {
return runTests("TestAgent", "doNothing(Something)", "doNothing(_)");
}
static int runMakeStringTests(const char *agentToTest) {
int answer = TRUE;
/// int sizes[] = {0, 1, 2, 5, 10, 20, 50};
int sizes[] = {0, 1, 2, 5, 10};
int numberOfMakeStringTests = sizeof(sizes) / sizeof(int);
int i;
char *makeString_templateString = "makeString(_,_)";
char makeString_goalString[256];
memset(makeString_goalString, 0, 256);
for (i = 0; i < numberOfMakeStringTests; i++) {
sprintf(makeString_goalString, "makeString(%d, AnswerString)", sizes[i]);
answer = runTests(agentToTest, makeString_goalString, makeString_templateString)
&& answer;
}
return answer;
}
static int runTest(const char *label,
const char *goalString,
const char *paramString,
const int repeats) {
int answer = FALSE;
char *tempString = NULL;
ICLTerm *out_params = NULL;
ICLTerm *solutions = NULL;
double totalTime = 0;
double averageTime = 0;
int i;
ICLTerm *goal = NULL;
ICLTerm *in_params = NULL;
printf("*****************************\n");
printf("%s\n", label);
printf(" Goal: %s\n", goalString);
printf(" In_Params: %s\n", paramString);
fflush(stdout);
// Call oaa_Solve() once to initialize OAA...
// ...do not count in timing results
goal = icl_NewTermFromString((char*)goalString);
in_params = icl_NewTermFromString((char*)paramString);
answer = oaa_Solve(goal, in_params, &out_params, &solutions);
solveCounter++;
icl_Free(goal);
icl_Free(in_params);
icl_Free(out_params);
icl_Free(solutions);
for (i = 1; i <= repeats; i++) {
int time = 0;
goal = icl_NewTermFromString((char*)goalString);
in_params = icl_NewTermFromString((char*)paramString);
answer = timed_Solve(goal, in_params, &out_params, &solutions, &time);
totalTime += time;
if(i == repeats) { // write report
tempString = icl_NewStringFromTerm(out_params);
printf(" Out_Params: %s\n", tempString);
icl_stFree(tempString);
// This next operation can take some time
// for long, complex solutions...
tempString = icl_NewStringFromTerm(solutions);
if (strlen(tempString) > 128) {
printf(" Solutions: [too big to print...]\n");
}
else {
printf(" Solutions: %s\n", tempString);
}
fflush(stdout);
icl_stFree(tempString);
printf(" Solve Success: %s\n", answer ? "true" : "FALSE!!!");
averageTime = (repeats == 0 ? 0 : totalTime/repeats);
printf(" Total Time (ms): %d\n", (int)totalTime);
printf(" Average Time (ms): %d\n", (int)averageTime);
printf(" Solve Requests: %d\n", repeats);
printf("*****************************\n");
fflush(stdout);
}
icl_Free(goal);
icl_Free(in_params);
icl_Free(out_params);
icl_Free(solutions);
}
return answer;
}
static int runTests(const char *agentToTest,
const char *goalString,
const char* goalTemplateString) {
int answer = TRUE;
static ICLTerm *agentAddress = NULL;
static char *agentAddressString = NULL;
static char *testLabel = NULL;
char paramString[256];
memset(paramString, 0, 256);
if (agentAddress == NULL || agentAddressString == NULL) {
if (!getAgentAddress(agentToTest, &agentAddress)) {
printf("The agent name \"%s\" does not appear to be valid.\nQuitting.");
exit(1);
}
agentAddressString = icl_NewStringFromTerm(agentAddress);
}
printf("*****************************\n\n");
printf(" Agent: %s\n", agentToTest);fflush(stdout);
printf(" Address: %s\n", agentAddressString);fflush(stdout);
printf(" Goal: %s\n", goalString);fflush(stdout);
printf(" Goal Template: %s\n", goalTemplateString);fflush(stdout);
printf("\n*****************************\n");
//-------------------------------------------------------------------------
testLabel = "Classic oaa_Solve()";
sprintf(paramString,
"[]");
answer = runTest(testLabel, goalString, paramString, REPEATS)
&& answer;
//-------------------------------------------------------------------------
testLabel = "Classic oaa_Solve() Plus Named Address";
sprintf(paramString,
"[get_direct_connect_used(_),"
"address(name('%s'))]",
agentToTest);
answer = runTest(testLabel, goalString, paramString, REPEATS)
&& answer;
//-------------------------------------------------------------------------
testLabel = "Classic oaa_Solve() Plus Numeric Address";
sprintf(paramString,
"[get_direct_connect_used(_),"
"address(%s)]",
agentAddressString);
answer = runTest(testLabel, goalString, paramString, REPEATS)
&& answer;
//-------------------------------------------------------------------------
testLabel = "Direct Connect";
sprintf(paramString,
"[direct_connect(true),"
"get_direct_connect_used(_)]");
answer = runTest(testLabel, goalString, paramString, REPEATS)
&& answer;
//-------------------------------------------------------------------------
testLabel = "Direct Connect + full_goal()";
sprintf(paramString,
"[direct_connect(true),get_direct_connect_used(_),"
"full_goal(%s)]",
goalString,
agentAddressString);
answer = runTest(testLabel, goalTemplateString, paramString, REPEATS)
&& answer;
//-------------------------------------------------------------------------
testLabel = "Direct Connect Plus Named Address";
sprintf(paramString,
"[direct_connect(true),"
"get_direct_connect_used(_),"
"address(name('%s'))]",
agentToTest);
answer = runTest(testLabel, goalString, paramString, REPEATS)
&& answer;
//-------------------------------------------------------------------------
testLabel = "Direct Connect Plus Numeric Address";
sprintf(paramString,
"[direct_connect(true),"
"get_direct_connect_used(_),"
"address(%s)]",
agentAddressString);
answer = runTest(testLabel, goalString, paramString, REPEATS)
&& answer;
//-------------------------------------------------------------------------
testLabel = "Direct Connect Plus Named Address + full_goal()";
sprintf(paramString,
"[direct_connect(true),"
"get_direct_connect_used(_),"
"full_goal(%s),"
"address(name('%s'))]",
goalString,
agentToTest);
answer = runTest(testLabel, goalTemplateString, paramString, REPEATS)
&& answer;
//-------------------------------------------------------------------------
testLabel = "Direct Connect Plus Numeric Address + full_goal()";
sprintf(paramString,
"[direct_connect(true),"
"get_direct_connect_used(_),"
"full_goal(%s),"
"address(%s)]",
goalString,
agentAddressString);
answer = runTest(testLabel, goalTemplateString, paramString, REPEATS)
&& answer;
//-------------------------------------------------------------------------
icl_Free(agentAddress);
icl_stFree(agentAddressString);
return answer;
}
static int setup_oaa_connection(int argc, char *argv[]) {
ICLTerm* mySolvablesAsTerm = icl_NewList(NULL);
if (!oaa_SetupCommunication("TestClient")) {
printf("Could not connect\n");
fflush(stdout);
return FALSE;
}
if (!oaa_Register("parent", "TestClient", mySolvablesAsTerm)) {
printf("Could not register\n");
return FALSE;
}
icl_Free(mySolvablesAsTerm);
oaa_Ready(TRUE);
return TRUE;
}
static int timed_Solve(ICLTerm *goal,
ICLTerm *in_params,
ICLTerm **out_params,
ICLTerm **solutions,
int *millisecondsToSolve) {
int answer = FALSE;
double t0 = 0;
double t1 = 0;
#ifndef _WINDOWS
struct timeval tv0, tv1;
#else
struct _timeb tv0, tv1;
#endif
#ifndef _WINDOWS
gettimeofday(&tv0, NULL);
#else
_ftime(&tv0);
#endif
answer = oaa_Solve(goal, in_params, out_params, solutions);
#ifndef _WINDOWS
gettimeofday(&tv1, (void *)0);
t0 = tv0.tv_sec*1000000.0 + tv0.tv_usec;
t1 = tv1.tv_sec*1000000.0 + tv1.tv_usec;
if (millisecondsToSolve != NULL) {
*millisecondsToSolve = (int)((t1 - t0)*0.001);
}
#else
_ftime(&tv1);
if (millisecondsToSolve != NULL) {
*millisecondsToSolve = (int)(1000*(tv1.time - tv0.time) + tv1.millitm - tv0.millitm);
}
#endif
solveCounter++;
// Solaris printf() doesn't like these to be NULL...
if(*out_params == NULL) {
*out_params = icl_NewList(NULL);
}
if(*solutions == NULL) {
*solutions = icl_NewList(NULL);
}
return answer;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?