rb_red_blk_tree* buildEventMap() { printf("--- Generating Event Rule Map ... \n"); rb_red_blk_tree* EventTree = RBTreeCreate(Compare_EventType,DestroyEventType,DestroyInfoEventKey,PrintEventKey,PrintInfoEventKey); if(!EventTree) { printf("Error Building the Event Rule Map.\n"); return NULL; } int i=0; term_t a0 = PL_new_term_refs(3); term_t b0 = PL_new_term_refs(2); static predicate_t p; static functor_t event_functor; char myEvents[256][256]; int arity; eventType* temp=NULL; if ( !event_functor ) event_functor = PL_new_functor(PL_new_atom("event"), 2); PL_cons_functor(a0+1,event_functor,b0,b0+1); if ( !p ) p = PL_predicate("trClause", 3, NULL); qid_t qid = PL_open_query(NULL, PL_Q_NORMAL, p, a0); while(PL_next_solution(qid) != FALSE) { //termToString(b0,myEvents[i]); atom_t name; PL_get_name_arity(b0, &name, &arity); sprintf(myEvents[i],"%s",PL_atom_chars(name)); temp=(eventType*)calloc(1,sizeof(eventType)); trClause* trc=(trClause*)calloc(1,sizeof(trClause)); strcpy(temp->name,PL_atom_chars(name)); temp->arity = arity; RBTreeInsert(EventTree,temp,trc); temp=NULL; trc=NULL; padding(' ',4); printf("+New Event Signature : %s/%d\n",myEvents[i],arity); i++; } PL_close_query(qid); #if DEBUG RBTreePrint(EventTree); #endif printf("--- Done!\n"); return EventTree; }
static bool call_function(clingo_location_t loc, char const *name, clingo_symbol_t const *in, size_t ilen, void *closure, clingo_symbol_callback_t *cb, void *cb_closure) { (void)loc; (void)closure; static predicate_t pred = 0; fid_t fid = 0; qid_t qid = 0; term_t av; bool rc = true; if (!pred) { pred = PL_predicate("inject_values", 3, "clingo"); } if (!(fid = PL_open_foreign_frame())) { rc = false; clingo_set_error(clingo_error_runtime, "prolog error"); goto out; } av = PL_new_term_refs(3); PL_put_atom_chars(av + 0, name); if (!(rc = unify_list_from_span(av + 1, in, ilen))) { clingo_set_error(clingo_error_runtime, "prolog error"); goto out; } if ((qid = PL_open_query(NULL, PL_Q_PASS_EXCEPTION, pred, av))) { while (PL_next_solution(qid)) { clingo_symbol_t value; if (!(rc = get_value(av + 2, &value, FALSE))) { goto out; } if (!(rc = cb(&value, 1, cb_closure))) { goto out; } } if (PL_exception(0)) { rc = false; clingo_set_error(clingo_error_runtime, "prolog error"); goto out; } } out: if (qid) { PL_close_query(qid); } if (fid) { PL_close_foreign_frame(fid); } return rc; }
int query_loop(atom_t goal, int loop) { GET_LD int rc; int clear_stacks = (LD->query == NULL); do { fid_t fid; qid_t qid = 0; term_t except = 0; predicate_t p; if ( !resetProlog(clear_stacks) ) goto error; if ( !(fid = PL_open_foreign_frame()) ) goto error; p = PL_pred(PL_new_functor(goal, 0), MODULE_system); if ( (qid = PL_open_query(MODULE_system, PL_Q_NORMAL, p, 0)) ) { rc = PL_next_solution(qid); } else { error: except = exception_term; rc = FALSE; /* Won't get any better */ break; } if ( !rc && (except = PL_exception(qid)) ) { atom_t a; tracemode(FALSE, NULL); debugmode(DBG_OFF, NULL); setPrologFlagMask(PLFLAG_LASTCALL); if ( PL_get_atom(except, &a) && a == ATOM_aborted ) { #ifdef O_DEBUGGER callEventHook(PLEV_ABORT); #endif printMessage(ATOM_informational, PL_ATOM, ATOM_aborted); } } if ( qid ) PL_close_query(qid); if ( fid ) PL_discard_foreign_frame(fid); if ( !except ) break; } while(loop); return rc; }
int query_loop(atom_t goal, int loop) { GET_LD int rc; int clear_stacks = (LD->query == NULL); do { fid_t fid; qid_t qid = 0; term_t except = 0; predicate_t p; if ( !resetProlog(clear_stacks) ) goto error; if ( !(fid = PL_open_foreign_frame()) ) goto error; p = PL_pred(PL_new_functor(goal, 0), MODULE_system); if ( (qid = PL_open_query(MODULE_system, PL_Q_NORMAL, p, 0)) ) { rc = PL_next_solution(qid); } else { error: except = exception_term; rc = -1; /* Won't get any better */ break; } if ( !rc && (except = PL_exception(qid)) ) { restore_after_exception(except); rc = -1; } if ( qid ) PL_close_query(qid); if ( fid ) PL_discard_foreign_frame(fid); if ( !except ) break; } while(loop); return rc; }
// ###################################################################### bool SWIProlog::query(const char *predicate, std::vector<std::string> &args) { bool ret=false; #ifdef HAVE_SWI_PROLOG_H term_t a0 = PL_new_term_refs(args.size()); predicate_t p = NULL; p = PL_predicate(predicate, args.size(), NULL); for(uint i=0; i<args.size(); i++) { if (args[i].size() != 0) PL_put_atom_chars(a0+i, args[i].c_str()); } qid_t query_id= PL_open_query(NULL, (PL_Q_NORMAL|PL_Q_CATCH_EXCEPTION), p, a0); ret = PL_next_solution(query_id); if (ret) { //fill in the results in the place holdes for(uint i=0; i<args.size(); i++) { if (args[i].size() == 0) { char *data; PL_get_atom_chars(a0+i, &data); args[i] = std::string(data); } } } PL_close_query(query_id); #else LINFO("SWI prolog not found"); #endif return ret; }
// ###################################################################### bool SWIProlog::consult(const char *filename) { bool ret = false; #ifdef HAVE_SWI_PROLOG_H term_t a0 = PL_new_term_refs(1); predicate_t p = NULL; p = PL_predicate("consult", 1, NULL); PL_put_atom_chars(a0, filename); qid_t query_id= PL_open_query(NULL, (PL_Q_NORMAL|PL_Q_CATCH_EXCEPTION), p, a0); ret = PL_next_solution(query_id); PL_close_query(query_id); #else LINFO("SWI prolog not found"); #endif return ret; }
static PyObject* pyswipl_run(PyObject* self_Py, PyObject* args_Py) { char* goalString; char* answer; int answerCount; PyObject* answerList_Py; PyObject* answerString_Py; PyObject* bindingList_Py; PyObject* binding_Py; term_t swipl_args; term_t swipl_goalCharList; term_t swipl_bindingList; term_t swipl_head; term_t swipl_list; predicate_t swipl_predicate; qid_t swipl_qid; fid_t swipl_fid; /**********************************************************/ /* The queryString_C should be a python string represting */ /* the query to be executed on the prolog system. */ /**********************************************************/ if(!PyArg_ParseTuple(args_Py, "s", &goalString)) return NULL; else { /**********************************************************/ /* Create a Python list to hold the lists of bindings. */ /**********************************************************/ //if ( answerList_Py != NULL ) // Py_DECREF(answerList_Py); answerList_Py=PyList_New(0); /**********************************************************/ /* Open a foreign frame and initialize the term refs. */ /**********************************************************/ swipl_fid=PL_open_foreign_frame(); swipl_head=PL_new_term_ref(); /* Used in unpacking the binding List */ swipl_args=PL_new_term_refs(2); /* The compound term for arguments to run/2 */ swipl_goalCharList=swipl_args; /* Alias for arg 1 */ swipl_bindingList=swipl_args+1; /* Alias for arg 2 */ /**********************************************************/ /* Pack the query string into the argument compund term. */ /**********************************************************/ PL_put_list_chars(swipl_goalCharList,goalString); /**********************************************************/ /* Generate a predicate to pyrun/2 */ /**********************************************************/ swipl_predicate=PL_predicate("pyrun",2,NULL); /**********************************************************/ /* Open the query, and iterate through the solutions. */ /**********************************************************/ swipl_qid=PL_open_query(NULL,PL_Q_NORMAL,swipl_predicate, swipl_args); while(PL_next_solution(swipl_qid)) { /**********************************************************/ /* Create a Python list to hold the bindings. */ /**********************************************************/ bindingList_Py=PyList_New(0); /**********************************************************/ /* Step through the bindings and add each to the list. */ /**********************************************************/ swipl_list=PL_copy_term_ref(swipl_bindingList); while(PL_get_list(swipl_list, swipl_head, swipl_list)) { PL_get_chars(swipl_head, &answer, CVT_ALL|CVT_WRITE|BUF_RING); answerString_Py = PyString_FromString(answer); PyList_Append(bindingList_Py, answerString_Py); Py_DECREF(answerString_Py); } /**********************************************************/ /* Add this binding list to the list of all solutions. */ /**********************************************************/ PyList_Append(answerList_Py, bindingList_Py); Py_DECREF(bindingList_Py); } /**********************************************************/ /* Free this foreign frame... */ /* Added by Nathan Denny, July 18, 2001. */ /* Fixes a bug with running out of global stack when */ /* asserting _lots_ of facts. */ /**********************************************************/ PL_close_query(swipl_qid); PL_discard_foreign_frame(swipl_fid); /**********************************************************/ /* Return the list of solutions. */ /**********************************************************/ return answerList_Py; } }
/************************* * libprolog_load_file *************************/ int libprolog_load_file(char *path, int extension) { char *loader = extension ? "load_foreign_library" : "consult"; predicate_t pr_loader; fid_t frame; qid_t qid; term_t pl_path; int success; /* * load the given file (native prolog or foreign library) * * Notes: * The prolog predicate consult/1 does not seem to fail or raise an * exception upon errors. It merely produces an error message and * tries to continue or gives up processing the input file. In either * case it succeeds (ie. the goal consult(path) is always proven in * the prolog sense). * * This default behaviour is not acceptable for us. As a library we * want to let our caller know whether loading was successful or not. * Otherwise it would be impossible to write even remotely reliable * applications using this library. * * To detect errors we have special prolog glue code that hooks into * SWI Prologs user:message_hook and lets us know about errors * (libprolog:mark_error) if loading is active (libprolog:loading). * Currently the glue code prints an error message but it would be * fairly easy to collect the errors here and let our caller print * them if needed. For the time being this glue code lives in policy.pl * but will eventually be separated out (to libprolog.pl ?). */ libprolog_clear_errors(); libprolog_load_start(); frame = PL_open_foreign_frame(); pr_loader = PL_predicate(loader, 1, NULL); pl_path = PL_new_term_ref(); PL_put_atom_chars(pl_path, path); qid = PL_open_query(NULL, NORMAL_QUERY_FLAGS, pr_loader, pl_path); success = PL_next_solution(qid); if (PL_exception(qid)) { #if 0 char **exception = collect_exception(qid, &exception); libprolog_dump_exception(exception); #endif success = FALSE; } PL_close_query(qid); PL_discard_foreign_frame(frame); libprolog_load_done(); if (libprolog_has_errors()) return FALSE; else return success; }
/* compile all rules */ EtalisExecTree* buildExecTree() { printf("--- Generating Rule Tree ...\n"); EtalisExecTree* tree = calloc(1,sizeof(EtalisExecTree)); tree->size=3; /* TODO (hafsi#4#): fixme */ /*if more than one rule, find out how many complex events*/ tree->exec=print_event; tree->complexEvents = (EtalisExecNode*)calloc(tree->size,sizeof(EtalisExecNode)); EtalisBatch* temp_batch = (EtalisBatch*)malloc(sizeof(EtalisBatch)); int i=0; static predicate_t p; term_t _args_binary_event_rule = PL_new_term_refs(3); atom_t name; int temp_arity; if ( !p ) p = PL_predicate("binary_event_rule", 3, NULL); qid_t qid = PL_open_query(NULL, PL_Q_NORMAL, p, _args_binary_event_rule); while(PL_next_solution(qid) != FALSE) { EtalisEventNode* temp_event = tree->complexEvents+i; /* next complex event */ EtalisExecNode* temp_operator =(EtalisExecNode*)malloc(sizeof(EtalisExecNode)); memset(temp_operator,0,sizeof(EtalisExecNode)); assert( temp_event != NULL && temp_operator != NULL); temp_event->parentNode=NULL; /*a complex event does not have a parent*/ temp_event->childNode=temp_operator; temp_event->trigger=_cep_print_event; /* by default, triggering a complx event would print it */ temp_operator->parentEvent=temp_event; temp_batch->batchSize=1; temp_batch->nodes=temp_operator; /*get label*/ PL_get_name_arity(_args_binary_event_rule, &name, &temp_arity); strcpy(temp_batch->label,PL_atom_chars(name)); /*get complex event*/ PL_get_name_arity(_args_binary_event_rule+1, &name, &temp_arity); strcpy(temp_event->event.name,PL_atom_chars(name)); temp_event->event.arity = temp_arity; /*get rule*/ construct_rule(temp_batch,_args_binary_event_rule+2); /* init a stack for each event*/ /* query the tree in the depth */ EtalisEventNode* temp_event_index = temp_operator->leftChild; for (temp_event_index = temp_operator->leftChild;temp_event_index->childNode != NULL;temp_event_index = temp_event_index->childNode->leftChild) { temp_event_index->eventStack = StackCreate(); if(temp_event_index->parentNode->op_type == binary) temp_event_index->parentNode->rightChild->eventStack = StackCreate(); } /* Create stack for leaf nodes*/ temp_event_index->eventStack = StackCreate(); if(temp_event_index->parentNode->op_type == binary) temp_event_index->parentNode->rightChild->eventStack = StackCreate(); /* build argument logical models */ /* if(temp_operator->has_condition == ETALIS_TRUE) { ; build_args_map(temp_operator,_args_binary_event_rule+1,_args_binary_event_rule+2); } else { build_args_map(temp_operator,_args_binary_event_rule+1,_args_binary_event_rule+2); } */ /*print the rule*/ /* only if debugging */ #ifdef DEBUG /*print_rule(temp_event);*/ #endif /*add to event hash*/ addToEventHash(temp_operator); i++; /*next rule*/ }; PL_close_query(qid); /*from the rules build the tree*/ printf("--- Done!\n"); return tree; }