addagent.c

来自「SRI international 发布的OAA框架软件」· C语言 代码 · 共 137 行

C
137
字号
// $Id: AddAgent.c,v 1.3 2002/09/25 21:42:59 mjohnson Exp $

#include <stdio.h>
#include <libicl.h>
#include <liboaa.h>

#define AGENT_NAME "AddAgent_C"

int add_callback(ICLTerm* goal, ICLTerm* params, ICLTerm* solutions);
int setup_oaa_connection(int argc, char *argv[]);

int main(int argc, char *argv[]) {

    // 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;
    }

    // Start the OAA main (infinite) loop
    // and wait for solve requests to arrive.
    oaa_MainLoop(TRUE);

    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* mySolvablesAsList = NULL;

    if (!oaa_SetupCommunication(AGENT_NAME)) {
        printf("Could not connect\n");
        return FALSE;
    }

    // Prepare a list of solvables that this agent is capable of
    // handling. This agent only has one solvable ("add")
    // and it is associated with the callback identified
    // by the name "add". See also the definition
    // of add_callback() below.
    mySolvablesAsList
        = icl_NewTermFromData(
            "[solvable(add(Addend1, Addend2, Sum), [callback(add)])]",
            55); // there are 55 characters in this string

    // Register solvables with the Facilitator.
    // The string "parent" represents the Facilitator.
    if (!oaa_Register("parent", AGENT_NAME, mySolvablesAsList)) {
        printf("Could not register\n");
        return FALSE;
    }

    // Register this agent's single callback.
    if (!oaa_RegisterCallback("add", add_callback)) {
        printf("Could not register add callback\n");
        return FALSE;
    }

    // Clean up.
    icl_Free(mySolvablesAsList);

    return TRUE;
}

/**
 * Satisfies <code>add(Addend1, Addendd2, Sum)</code> goals.
 * @param goal the goal
 * @param params parameters
 * @param solutions the solutions list; this callback
 * will add the satisfied goal to this list if the callback is
 * successful.
 * @return TRUE if successful
 */
int add_callback(ICLTerm* goal,
                        ICLTerm* params,
                        ICLTerm* solutions) {

    // The first addend is the first term
    // in "goal". The "goal" struct
    // is in the form "add(Addend1, Addend2, Sum)".
    ICLTerm *Addend1 = icl_CopyTerm(icl_NthTerm(goal, 1));

    // Repeat for the second addend.
    ICLTerm *Addend2 = icl_CopyTerm(icl_NthTerm(goal, 2));

    // Compute the sum.
    int addend1 = icl_Int(Addend1);
    int addend2 = icl_Int(Addend2);
    int sum = addend1 + addend2;

    // Prepare an ICLTerm containing the integer sum.
    ICLTerm *Sum = icl_NewInt(sum);

    // Prepare a struct that contains the satisfied goal.
    ICLTerm *solution = icl_NewStruct("add", 3, Addend1, Addend2, Sum);

    // Add this solution to the list of solutions.
    // This will return TRUE if the add is successful.
    return icl_AddToList(solutions, solution, TRUE);
}

// doxygen content

/**
 * @defgroup Example Example: AddAgent and AddClient
 *
 * A simple pair of agents that demonstrate the
 * basics for building OAA agents using the C
 * language.
 *
 * @{
 */

/**
 * @file AddAgent.c
 * An agent that adds two integers together.
 * Its single solvable is <code>add(Addend1, Addend2, Sum)</code>.
 */

/**
 * @file AddClient.c
 * An agent that utilizes the "add" solvable
 * registered by the @link AddAgent.c AddAgent @endlink.
 */

/** @} */

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?