📄 addclient.c
字号:
// $Id: AddClient.c,v 1.3 2002/09/25 21:43:00 mjohnson Exp $
#include <stdio.h>
#include <string.h>
#include <libicl.h>
#include <liboaa.h>
#define AGENT_NAME "AddClient_C"
int setup_oaa_connection(int argc, char *argv[]);
int main(int argc, char *argv[]) {
int solve_success = 0;
ICLTerm *Addend1 = NULL;
ICLTerm *Addend2 = NULL;
ICLTerm *Sum = NULL;
ICLTerm *goal = NULL;
ICLTerm *in_params = NULL;
ICLTerm *out_params = NULL;
ICLTerm *solutions = NULL;
// This simple agent takes two numbers from the
// command line and then requests that the
// Facilitator find another agent that can
// add them up.
if (argc != 3) {
printf("Please supply exactly two integer arguments to add on the command line.\n");
return EXIT_FAILURE;
}
// Initialize the OAA library.
oaa_Init(argc, argv);
// Setup the connection of this agent to the Facilitator.
// See setup_oaa_connection() definition below.
if (!setup_oaa_connection(argc, argv)) {
printf("Could not setup OAA connections...exiting.\n");
return EXIT_FAILURE;
}
// Create an ICLTerm for the first addend using the first
// integer from the command line.
Addend1 = icl_NewTermFromData(argv[1], strlen(argv[1]));
// Do likewise for the second addend.
Addend2 = icl_NewTermFromData(argv[2], strlen(argv[2]));
// We need to use an ICL variable as a placeholder
// for the sum that will be computed and returned to us.
Sum = icl_NewVar("Sum");
// Build the goal "add(Addend1, Addend2, Sum)"
// where Addend1 and Addend2 represent the
// constant integer values provided on
// the command line.
goal = icl_NewStruct("add", 3, Addend1, Addend2, Sum);
// Here we supply no special parameters, we just use an empty list.
in_params = icl_NewList(NULL);
// Request that the goal be solved. Upon a successful completion,
// one or more solutions will be contained in "solutions".
// Each solution will be in the form "add(Addend1, Addend2, Sum)"
// where Sum is replaced with the computed sum.
// The parmeter "out_params" will contain information
// about the solution process, but that information
// is not needed by this simple client.
solve_success = oaa_Solve(goal, in_params, &out_params, &solutions);
// If oaa_Solve() succeeded, print out the sum reported
// by the first (and perhaps only) solution.
if (solve_success) {
int addend1 = icl_Int(Addend1);
int addend2 = icl_Int(Addend2);
// The sum is the third ICLTerm of the first solution.
ICLTerm *solution = icl_NthTerm(solutions, 1);
ICLTerm *SolvedSum = icl_NthTerm(solution, 3);
int sum = icl_Int(SolvedSum);
printf("\nThe sum of %d and %d is %d.\n\n", addend1, addend2, sum);
}
else {
printf("\nThere was an OAA failure; no solution found.\n\n");
}
// Clean up.
icl_Free(goal);
icl_Free(in_params);
icl_Free(out_params);
icl_Free(solutions);
// Disconnect and exit.
oaa_Disconnect(NULL, NULL);
return EXIT_SUCCESS;
}
/**
* Setups up the the connection between this agent
* and the facilitator.
* @param argc passed in but not used in this implementation
* @param argv passed in but not used in this implementation
* @return TRUE if successful
*/
int setup_oaa_connection(int argc, char *argv[]) {
ICLTerm* mySolvablesAsTerm = NULL;
// Use this OAA convenience function to setup communications.
if (!oaa_SetupCommunication(AGENT_NAME)) {
printf("Could not connect\n");
return FALSE;
}
// Register solvables with the Facilitator.
// This is a "pure client" agent and therefore
// it has no solvables.
// The string "parent" represents the Facilitator.
mySolvablesAsTerm = icl_NewList(NULL);
if (!oaa_Register("parent", AGENT_NAME, mySolvablesAsTerm)) {
printf("Could not register\n");
return FALSE;
}
// Clean up.
icl_Free(mySolvablesAsTerm);
// Declare agent "ready".
oaa_Ready(TRUE);
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -