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

AddAgent.c

Go to the documentation of this file.
00001 // $Id: AddAgent.c,v 1.3 2002/09/25 21:42:59 mjohnson Exp $
00002 
00003 #include <stdio.h>
00004 #include <libicl.h>
00005 #include <liboaa.h>
00006 
00007 #define AGENT_NAME "AddAgent_C"
00008 
00009 int add_callback(ICLTerm* goal, ICLTerm* params, ICLTerm* solutions);
00010 int setup_oaa_connection(int argc, char *argv[]);
00011 
00012 int main(int argc, char *argv[]) {
00013 
00014     // Initialize the OAA library.
00015     oaa_Init(argc, argv);
00016 
00017     // Setup the connection of this agent to the Facilitator.
00018     // See setup_oaa_connection() definition below.
00019     if (!setup_oaa_connection(argc, argv)) {
00020         printf("Could not setup OAA connections...exiting.\n");
00021         return EXIT_FAILURE;
00022     }
00023 
00024     // Start the OAA main (infinite) loop
00025     // and wait for solve requests to arrive.
00026     oaa_MainLoop(TRUE);
00027 
00028     return EXIT_SUCCESS;
00029 }
00030 
00038 int setup_oaa_connection(int argc, char *argv[]) {
00039     ICLTerm* mySolvablesAsList = NULL;
00040 
00041     if (!oaa_SetupCommunication(AGENT_NAME)) {
00042         printf("Could not connect\n");
00043         return FALSE;
00044     }
00045 
00046     // Prepare a list of solvables that this agent is capable of
00047     // handling. This agent only has one solvable ("add")
00048     // and it is associated with the callback identified
00049     // by the name "add". See also the definition
00050     // of add_callback() below.
00051     mySolvablesAsList
00052         = icl_NewTermFromData(
00053             "[solvable(add(Addend1, Addend2, Sum), [callback(add)])]",
00054             55); // there are 55 characters in this string
00055 
00056     // Register solvables with the Facilitator.
00057     // The string "parent" represents the Facilitator.
00058     if (!oaa_Register("parent", AGENT_NAME, mySolvablesAsList)) {
00059         printf("Could not register\n");
00060         return FALSE;
00061     }
00062 
00063     // Register this agent's single callback.
00064     if (!oaa_RegisterCallback("add", add_callback)) {
00065         printf("Could not register add callback\n");
00066         return FALSE;
00067     }
00068 
00069     // Clean up.
00070     icl_Free(mySolvablesAsList);
00071 
00072     return TRUE;
00073 }
00074 
00084 int add_callback(ICLTerm* goal,
00085                         ICLTerm* params,
00086                         ICLTerm* solutions) {
00087 
00088     // The first addend is the first term
00089     // in "goal". The "goal" struct
00090     // is in the form "add(Addend1, Addend2, Sum)".
00091     ICLTerm *Addend1 = icl_CopyTerm(icl_NthTerm(goal, 1));
00092 
00093     // Repeat for the second addend.
00094     ICLTerm *Addend2 = icl_CopyTerm(icl_NthTerm(goal, 2));
00095 
00096     // Compute the sum.
00097     int addend1 = icl_Int(Addend1);
00098     int addend2 = icl_Int(Addend2);
00099     int sum = addend1 + addend2;
00100 
00101     // Prepare an ICLTerm containing the integer sum.
00102     ICLTerm *Sum = icl_NewInt(sum);
00103 
00104     // Prepare a struct that contains the satisfied goal.
00105     ICLTerm *solution = icl_NewStruct("add", 3, Addend1, Addend2, Sum);
00106 
00107     // Add this solution to the list of solutions.
00108     // This will return TRUE if the add is successful.
00109     return icl_AddToList(solutions, solution, TRUE);
00110 }
00111 
00112 // doxygen content
00113 

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