libtrigger.c
来自「SRI international 发布的OAA框架软件」· C语言 代码 · 共 652 行 · 第 1/2 页
C
652 行
/****************************************************************************
* File : libtrigger.c
* Author : Adam Cheyer, David Martin
* Purpose : Contains C version of trigger functions for the Open Agent
* Architecture
* Updated : 5/21/97
*
* -------------------------------------------------------------------------
* Unpublished-rights reserved under the copyright laws of the United States.
*
* This data and information is proprietary to, and a valuable trade
* secret of, SRI International. It is given in confidence by SRI
* International. Its use, duplication, or disclosure is subject to the
* restrictions set forth in the License Agreement under which it has
* been distributed.
*
* Unpublished Copyright (c) 1993-97, SRI International.
* "Open Agent Architecture" and "OAA" are Trademarks of SRI International.
* -------------------------------------------------------------------------
*
*
****************************************************************************
* Note: internal functions use the naming convention oaa_function_name(),
* while public predicates use oaa_PublicPredicate().
*****************************************************************************/
/****************************************************************************
* RCS Header
****************************************************************************/
#ifndef lint
/*static char libtrigger_c_rcsid[] = "$Header: /home/zuma1/OAA/CVSRepository/oaa2/src/oaalib/c/src/libtrigger.c,v 1.12 2004/10/12 17:58:45 agno Exp $";*/
#endif
/****************************************************************************
*Include files
****************************************************************************/
#ifndef _WINDOWS
#include <sys/param.h> /* used by getlocalhostname */
#include <sys/time.h> /* used by oaa_Ping */
#endif
#include <string.h> /* string functions */
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include "libicl_private.h"
#include "libicl.h" /* ICL term parsing/generating library */
#include "libdb.h" /* Database library */
#include "libcom_tcp.h" /* coms library */
#include "liboaa.h" /* OAA library headers */
#include "dictionary.h" /* utility collection data type */
/*****************************************************************************
* Preclarations to avoid warnings
*****************************************************************************/
int oaa_remove_trigger_local(char *type, ICLTerm *condition,
ICLTerm *action, ICLTerm *params);
int oaa_add_trigger_local(char *type, ICLTerm *condition,
ICLTerm *action, ICLTerm *params);
EXTERN int oaa_solve_local(ICLTerm *full_goal, ICLTerm *params, ICLTerm **solutions);
EXTERN ICLTerm *remove_element(ICLTerm *elt, ICLTerm *list, ICLTerm **rest);
EXTERN int oaa_remove_data_local(ICLTerm *clause1, ICLTerm *params);
EXTERN int oaa_add_data_local(ICLTerm *clause1, ICLTerm *params);
EXTERN int oaa_Inform(ICLTerm *type_info, char *format_string, ICLTerm *args);
EXTERN int icl_param_arg(char *param, ICLTerm *value, ICLTerm *paramlist,
ICLTerm **result);
EXTERN int icl_standardize_params(ICLTerm *Params,int KeepDefaults,ICLTerm **Standardized);
EXTERN int replace_element(ICLTerm *elt, ICLTerm *old_list, ICLTerm *new,
ICLTerm **new_list);
EXTERN ICLTerm* valid_oaa_solvables();
EXTERN int oaa_data_matches_solvables(ICLTerm *Clause, ICLTerm *Solvables, ICLTerm *Perm,
ICLTerm **RealClause, ICLTerm **RealMatched);
EXTERN int oaa_class(char *class);
EXTERN char* new_goal_id();
EXTERN int oaa_poll_until_event(ICLTerm *event, ICLTerm **solution);
EXTERN int icl_replace_param_value(ICLTerm *param, ICLTerm *param_list);
int oaa_remove_local_trigger_by_id(ICLTerm *trigger_id);
int oaa_fire_trigger(ICLTerm *term);
/**
* Given a trigger type, a mask and an Op (e.g. [send, receive],
* [add, remove], etc), see if any triggers fire.
*/
int oaa_CheckTriggers(char *type, ICLTerm *condition_var, char *op) {
/*****************************************************************************
* comments: looks to me like condition is an input parameter, but it could
* be a variable. If passed in as a variable, there is no provision
* for returning a value (sws, 1/4/99)
*****************************************************************************/
ICLTerm *action_var = icl_NewVar("Action");
ICLTerm *params_var = icl_NewVar("Params");
ICLTerm *trigger_id_var = icl_NewVar("TriggerId");
ICLTerm *op_as_term = icl_NewTermFromString(op);
ICLTerm *trigger_clause =
icl_NewStruct("oaa_trigger", 5, trigger_id_var,
icl_NewStr(type), icl_CopyTerm(condition_var), action_var, params_var);
ICLTerm *dummyvar = icl_NewVar("_");
ICLTerm *results=NULL;
ICLTerm *t1;
ICLTerm *temp_list=NULL;
int i;
/* For dereferencing the Action variables */
struct dyn_array local_da;
/* for each matching trigger */
/* Note : the memory for results if allocated in oaa_solve_local */
temp_list=icl_NewList(NULL);
if(oaa_solve_local(trigger_clause, temp_list, &results)) {
if(icl_IsList(results)) {
ICLListType *reslist = icl_List(results);
ICLTerm *recurrence_term = icl_NewStruct("recurrence", 1, icl_CopyTerm(dummyvar));
while(icl_list_has_more_elements(reslist)) {
ICLTerm *trigger = icl_list_element(reslist);
ICLTerm *op_specified = NULL, *op_mask = NULL, *test = NULL;
ICLTerm *result = NULL, *r_as_term = NULL, *trigger_id = NULL, *action = NULL, *params = NULL, *condition = NULL;
ICLTerm *arglist = NULL;
ICLTerm *new_params = NULL;
trigger_id = icl_NthTerm(trigger, 1);
condition = icl_NthTerm(trigger, 3);
action = icl_NthTerm(trigger, 4);
params = icl_NthTerm(trigger, 5);
/* Do unification of Condition again to set variable bindings into bArray
The unification should always succeed because we already selected
this trigger because of the Condition.
We may need to pick out variables for use with action later
*/
icl_init_dyn_array(&local_da);
icl_match_terms(condition, condition_var, &local_da);
if(STREQ(type, "task") && !icl_IsVar(condition)) {
t1 = icl_NewTermFromData("from(self)", 10);
oaa_Interpret(condition, t1, NULL);
icl_Free(t1);
}
/* see if on(Op) has been specified */
if(icl_param_arg("on", dummyvar, params, &op_specified)) {
op_mask = op_specified;
}
else {
op_mask = dummyvar;
}
/* see if Op is OK and keep var bindings */
/* NOT IMPLEMENTED
if(icl_IsGround(op_mask) || icl_IsList(op_mask))
try to unify op against op_mask
icl_GetParamValue(op_as_term, op_mask, &unified_op);
else
unified_op = op_mask;
*/
/* NOT IMPLEMENTED
// test additional conditions
if(icl_param_arg("test", dummyvar, params, &test)) {
oaa_Interpret(test,
(t1 = icl_NewTermFromString("from(self)")), NULL);
icl_Free(t1);
}
else
test = icl_NewStr("true");
*/
/* check recurrence: remove trigger? */
result = remove_element(recurrence_term, params, &new_params);
if(result && icl_IsStruct(result)) {
char *whenTrigger;
r_as_term = icl_NthTerm(result, 1);
/* don't remove trigger if 'whenever' */
whenTrigger = icl_NewStringFromTerm(r_as_term);
if(!STREQ(whenTrigger, "whenever")) {
int r2;
if(icl_IsInt(r_as_term)) {
ICLTerm *mod_params, *newrecurterm, *newtrigger;
ICLTerm *emptyList = icl_NewList(NULL);
/* decrement recurrence count */
r2 = icl_Int(r_as_term) - 1;
oaa_remove_data_local(trigger, emptyList);
if (r2 > 0) {
newrecurterm = icl_NewStruct("recurrence", 1,
icl_NewInt(r2));
mod_params =
icl_NewList(icl_NewCons(newrecurterm, icl_List(new_params)));
newtrigger =
icl_NewStruct("oaa_trigger", 5, trigger_id, icl_NewStr(type),
condition, action, mod_params);
oaa_add_data_local(newtrigger, emptyList);
}
icl_Free(emptyList);
}
else {
oaa_remove_local_trigger_by_id(trigger_id);
}
}
icl_stFree(whenTrigger);
}
else {
/*r_as_term = icl_NewStr("when");*/
oaa_remove_local_trigger_by_id(trigger_id);
}
arglist =
icl_NewList(
icl_NewCons(
icl_NewStr(type),
icl_NewCons(op_as_term,
icl_NewCons(condition,
icl_NewCons(test,
icl_NewCons(action, NULL))))));
oaa_TraceMsg("\n %s trigger fired %s\n Action: %s\n",
icl_NewStringFromTerm(action),
icl_NewStringFromTerm(condition),NULL);
if(!STREQ(type, "comm")) {
arglist = icl_NewList(icl_NewCons(
icl_NewStr(type), icl_NewCons(
condition, icl_NewCons(
action, icl_NewCons(
params, NULL)))));
oaa_Inform(trigger, "trigger_fired(%s, %s, %s, %s)\n", arglist);
}
/* FIRE!!!! */
action = icl_copy_term_nonrec(action, &local_da);
/* Free the local_da */
{
ICLTerm* toFree;
for (i = 0; i < local_da.count; i = i + 2) {
free(local_da.item[i]);
toFree = (ICLTerm*)(local_da.item[i + 1]);
icl_Free(toFree);
}
}
free(local_da.item);
oaa_fire_trigger(action);
/* loop back for more triggers */
reslist = icl_list_next_element(reslist);
/* Since action is now a copy of a term, we should free it */
icl_Free(action);
icl_Free(result);
icl_Free(new_params);
} /* End While */
icl_Free(recurrence_term);
}
}
icl_Free(trigger_clause);
icl_Free(dummyvar);
icl_Free(op_as_term);
icl_Free(results);
icl_Free(temp_list);
return TRUE;
}
int oaa_fire_trigger(ICLTerm *term) {
ICLTerm *new_params = NULL, *me, *from_me_list;
int res;
if(icl_IsStruct(term)) {
char* functor = icl_Functor(term);
if(STREQ(functor, "oaa_Solve")) {
if(icl_Arity(term) == 2) {
ICLTerm *goal = icl_NthTerm(term, 1);
ICLTerm *params = icl_NthTerm(term, 2);
if(icl_ParamValue("block", NULL, params, NULL)) {
new_params = icl_CopyTerm(params);
}
else {
ICLTerm *t1 = icl_NewTermFromData("[block(false)]", 14);
icl_append_to_list(t1, params, &new_params);
icl_Free(t1);
}
res = oaa_Solve(goal, new_params, NULL, NULL);
icl_Free(new_params);
return res;
}
else if(icl_Arity(term) == 1) {
ICLTerm *goal = icl_NthTerm(term, 1);
new_params = icl_NewTermFromData("[block(false)]", 14);
res = oaa_Solve(goal, new_params, NULL, NULL);
icl_Free(new_params);
return res;
}
}
else if(STREQ(functor, "oaa_Interpret")) {
if(icl_Arity(term) == 2) {
ICLTerm *goal = icl_NthTerm(term, 1);
ICLTerm *params = icl_NthTerm(term, 2);
if(icl_ParamValue("from", NULL, params, NULL)) {
/* new_params should be NULL */
new_params = icl_CopyTerm(params);
}
else {
oaa_Id(&me);
from_me_list = icl_NewList(icl_NewCons(icl_NewStruct("from", 1,
me),
NULL));
icl_append_to_list(from_me_list, params, &new_params);
}
res = oaa_Interpret(goal, new_params, NULL);
icl_Free(new_params);
return res;
}
else if(icl_Arity(term) == 1) {
oaa_Id(&me);
from_me_list =
icl_NewList(
icl_NewCons(icl_NewStruct("from", 1,
icl_CopyTerm(me)),
NULL));
res = oaa_Interpret(term, from_me_list, NULL);
icl_Free(from_me_list);
icl_Free(me);
return res;
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?