Main Page | Modules | Data Structures | File List | Data Fields | Globals | Related Pages

AddClient.c

Go to the documentation of this file.
00001 // $Id: AddClient.c,v 1.3 2002/09/25 21:43:00 mjohnson Exp $
00002 
00003 #include <stdio.h>
00004 #include <string.h>
00005 #include <libicl.h>
00006 #include <liboaa.h>
00007 
00008 #define AGENT_NAME "AddClient_C"
00009 
00010 int setup_oaa_connection(int argc, char *argv[]);
00011 
00012 int main(int argc, char *argv[]) {
00013     int solve_success = 0;
00014     ICLTerm *Addend1 = NULL;
00015     ICLTerm *Addend2 = NULL;
00016     ICLTerm *Sum = NULL;
00017     ICLTerm *goal = NULL;
00018     ICLTerm *in_params = NULL;
00019     ICLTerm *out_params = NULL;
00020     ICLTerm *solutions = NULL;
00021 
00022     // This simple agent takes two numbers from the
00023     // command line and then requests that the
00024     // Facilitator find another agent that can
00025     // add them up.
00026     if (argc != 3) {
00027       printf("Please supply exactly two integer arguments to add on the command line.\n");
00028       return EXIT_FAILURE;
00029     }
00030 
00031     // Initialize the OAA library.
00032     oaa_Init(argc, argv);
00033 
00034     // Setup the connection of this agent to the Facilitator.
00035     // See setup_oaa_connection() definition below.
00036     if (!setup_oaa_connection(argc, argv)) {
00037         printf("Could not setup OAA connections...exiting.\n");
00038         return EXIT_FAILURE;
00039     }
00040 
00041     // Create an ICLTerm for the first addend using the first
00042     // integer from the command line.
00043     Addend1 = icl_NewTermFromData(argv[1], strlen(argv[1]));
00044 
00045     // Do likewise for the second addend.
00046     Addend2 = icl_NewTermFromData(argv[2], strlen(argv[2]));
00047 
00048     // We need to use an ICL variable as a placeholder
00049     // for the sum that will be computed and returned to us.
00050     Sum = icl_NewVar("Sum");
00051 
00052     // Build the goal "add(Addend1, Addend2, Sum)"
00053     // where Addend1 and Addend2 represent the
00054     // constant integer values provided on
00055     // the command line.
00056     goal = icl_NewStruct("add", 3, Addend1, Addend2, Sum);
00057 
00058     // Here we supply no special parameters, we just use an empty list.
00059     in_params = icl_NewList(NULL);
00060 
00061     // Request that the goal be solved. Upon a successful completion,
00062     // one or more solutions will be contained in "solutions".
00063     // Each solution will be in the form "add(Addend1, Addend2, Sum)"
00064     // where Sum is replaced with the computed sum.
00065     // The parmeter "out_params" will contain information
00066     // about the solution process, but that information
00067     // is not needed by this simple client.
00068     solve_success = oaa_Solve(goal, in_params, &out_params, &solutions);
00069 
00070     // If oaa_Solve() succeeded, print out the sum reported
00071     // by the first (and perhaps only) solution.
00072     if (solve_success) {
00073         int addend1 = icl_Int(Addend1);
00074         int addend2 = icl_Int(Addend2);
00075 
00076         // The sum is the third ICLTerm of the first solution.
00077         ICLTerm *solution = icl_NthTerm(solutions, 1);
00078         ICLTerm *SolvedSum = icl_NthTerm(solution, 3);
00079         int sum = icl_Int(SolvedSum);
00080 
00081         printf("\nThe sum of %d and %d is %d.\n\n", addend1, addend2, sum);
00082       }
00083     else {
00084         printf("\nThere was an OAA failure; no solution found.\n\n");
00085     }
00086 
00087     // Clean up.
00088     icl_Free(goal);
00089     icl_Free(in_params);
00090     icl_Free(out_params);
00091     icl_Free(solutions);
00092 
00093     // Disconnect and exit.
00094     oaa_Disconnect(NULL, NULL);
00095     return EXIT_SUCCESS;
00096 }
00097 
00105 int setup_oaa_connection(int argc, char *argv[]) {
00106     ICLTerm* mySolvablesAsTerm = NULL;
00107 
00108     // Use this OAA convenience function to setup communications.
00109     if (!oaa_SetupCommunication(AGENT_NAME)) {
00110         printf("Could not connect\n");
00111         return FALSE;
00112     }
00113 
00114     // Register solvables with the Facilitator.
00115     // This is a "pure client" agent and therefore
00116     // it has no solvables.
00117     // The string "parent" represents the Facilitator.
00118     mySolvablesAsTerm = icl_NewList(NULL);
00119     if (!oaa_Register("parent", AGENT_NAME, mySolvablesAsTerm)) {
00120         printf("Could not register\n");
00121         return FALSE;
00122     }
00123 
00124     // Clean up.
00125     icl_Free(mySolvablesAsTerm);
00126 
00127     // Declare agent "ready".
00128     oaa_Ready(TRUE);
00129 
00130     return TRUE;
00131 }
00132 
00133 

Generated on Thu Nov 3 22:22:19 2005 using doxygen 1.3.5