static int defOperator(Module m, atom_t name, int type, int priority, int force) { GET_LD Symbol s; operator *op; int t = (type & OP_MASK); /* OP_PREFIX, ... */ DEBUG(7, Sdprintf(":- op(%d, %s, %s) in module %s\n", priority, PL_atom_chars(operatorTypeToAtom(type)), PL_atom_chars(name), PL_atom_chars(m->name))); assert(t>=OP_PREFIX && t<=OP_POSTFIX); if ( !force && !SYSTEM_MODE ) { if ( name == ATOM_comma || (name == ATOM_bar && ((t&OP_MASK) != OP_INFIX || (priority < 1001 && priority != 0))) ) { GET_LD atom_t action = (name == ATOM_comma ? ATOM_modify : ATOM_create); term_t t = PL_new_term_ref(); PL_put_atom(t, name); return PL_error(NULL, 0, NULL, ERR_PERMISSION, action, ATOM_operator, t); } } LOCK(); if ( !m->operators ) m->operators = newOperatorTable(8); if ( (s = lookupHTable(m->operators, (void *)name)) ) { op = s->value; } else if ( priority < 0 ) { UNLOCK(); /* already inherited: do not change */ return TRUE; } else { op = allocHeapOrHalt(sizeof(*op)); op->priority[OP_PREFIX] = -1; op->priority[OP_INFIX] = -1; op->priority[OP_POSTFIX] = -1; op->type[OP_PREFIX] = OP_INHERIT; op->type[OP_INFIX] = OP_INHERIT; op->type[OP_POSTFIX] = OP_INHERIT; } op->priority[t] = priority; op->type[t] = (priority >= 0 ? type : OP_INHERIT); if ( !s ) { PL_register_atom(name); addHTable(m->operators, (void *)name, op); } UNLOCK(); return TRUE; }
int currentOperator(Module m, atom_t name, int kind, int *type, int *priority) { operator *op; assert(kind >= OP_PREFIX && kind <= OP_POSTFIX); if ( !m ) m = MODULE_user; if ( (op = visibleOperator(m, name, kind)) ) { if ( op->priority[kind] > 0 ) { *type = op->type[kind]; *priority = op->priority[kind]; DEBUG(MSG_OPERATOR, Sdprintf("currentOperator(%s) --> %s %d\n", PL_atom_chars(name), PL_atom_chars(operatorTypeToAtom(*type)), *priority)); succeed; } } fail; }
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 foreign_t python_field(term_t parent, term_t att, term_t tobj) { PyObject *pF; atom_t name; char *s; int arity; if (!PL_get_name_arity(att, &name, &arity)) { { return false; } } else { PyObject *p; // got Scope.Exp // get Scope ... p = term_to_python(parent, true); // Exp if (!PL_get_name_arity(att, &name, &arity)) { { return false; } } s = PL_atom_chars(name); if (arity == 1 && !strcmp(s, "()")) { if (!PL_get_arg(1, att, att)) { return false; } if (!PL_get_name_arity(att, &name, &arity)) { { return false; } } s = PL_atom_chars(name); } if (!s || !p) { { return false; } } else if ((pF = PyObject_GetAttrString(p, s)) == NULL) { PyErr_Clear(); { return false; } } } { foreign_t rc; rc = address_to_term(pF, tobj); return rc; } }
static int process_console_options(rlc_console_attr *attr, term_t options) { term_t tail = PL_copy_term_ref(options); term_t opt = PL_new_term_ref(); while(PL_get_list(tail, opt, tail)) { atom_t name; const char *s; int arity; if ( !PL_get_name_arity(opt, &name, &arity) ) return type_error(opt, "compound"); s = PL_atom_chars(name); if ( streq(s, "registry_key") && arity == 1 ) { TCHAR *key; if ( !get_chars_arg_ex(1, opt, &key) ) return FALSE; attr->key = key; } else return domain_error(opt, "window_option"); } if ( !PL_get_nil(tail) ) return type_error(tail, "list"); return TRUE; }
static int get_params(term_t t, clingo_part_t *pv) { int rc; atom_t name; term_t arg; clingo_symbol_t *values = NULL; if (!(rc = get_name_arity(t, &name, &pv->size))) { rc = PL_type_error("callable", t); goto out; } arg = PL_new_term_ref(); if (!(values = malloc(sizeof(*pv->params) * pv->size))) { rc = PL_resource_error("memory"); goto out; } for (size_t i = 0; i < pv->size; i++) { _PL_get_arg(i + 1, t, arg); if (!(rc = clingo_status(get_value(arg, &values[i], FALSE)))) { goto out; } } pv->params = values; pv->name = PL_atom_chars(name); values = NULL; out: if (values) { free(values); } return rc; }
static SourceFile lookupSourceFile_unlocked(atom_t name, int create) { GET_LD SourceFile file; if ( !GD->files.table ) { GD->files.table = newHTable(32); GD->files.table->free_symbol = freeSymbolSourceFile; GD->files.no_hole_before = 1; } if ( (file=lookupHTable(GD->files.table, (void*)name)) ) { ; } else if ( create ) { file = allocHeapOrHalt(sizeof(*file)); memset(file, 0, sizeof(*file)); file->name = name; file->system = GD->bootsession; #ifdef O_PLMT file->mutex = allocSimpleMutex(PL_atom_chars(name)); #endif PL_register_atom(file->name); file->magic = SF_MAGIC; registerSourceFile(file); addNewHTable(GD->files.table, (void*)name, file); } else { file = NULL; } return file; }
int destroySourceFile(SourceFile sf) { DEBUG(MSG_SRCFILE, Sdprintf("Destroying source file %s\n", PL_atom_chars(sf->name))); clearSourceAdmin(sf); LOCK(); if ( sf->magic == SF_MAGIC ) { SourceFile f; sf->magic = SF_MAGIC_DESTROYING; f = deleteHTable(GD->files.table, (void*)sf->name); assert(f); PL_unregister_atom(sf->name); putSourceFileArray(sf->index, NULL); if ( GD->files.no_hole_before > sf->index ) GD->files.no_hole_before = sf->index; } UNLOCK(); unallocSourceFile(sf); return TRUE; }
static PyObject *find_obj(PyObject *ob, term_t lhs) { char *s; PyObject *out, *pName; int arity = 0; if (!PL_get_atom_chars(lhs, &s)) { atom_t name; if (!PL_get_name_arity(lhs, &name, &arity)) return NULL; s = PL_atom_chars(name); } if (ob) { out = PyObject_GetAttrString(ob, s); return out; } if (!ob && !arity) { #if PY_MAJOR_VERSION < 3 pName = PyString_FromString(s); #else pName = PyUnicode_FromString(s); #endif if (pName == NULL) { return NULL; } if ((out = PyImport_Import(pName))) { Py_IncRef(out); // Py_DecRef(pName); ?? return out; } } if (!ob && py_Main && (out = PyObject_GetAttrString(py_Main, s))) return out; return NULL; }
gint write_plgi_blob(IOSTREAM *s, atom_t a, gint flags) { PLGIBlob *blob = PL_blob_data(a, NULL, NULL); Sfprintf(s, "<%s>(%p)", PL_atom_chars(blob->name), blob->data); return TRUE; }
static int format_error(const char *pred, size_t pos, Field f) { char buf[1024]; sprintf(buf, "%s: bad record, field %d (%s), char-index %ld", pred, f->index, PL_atom_chars(f->name), (long)pos); return PL_warning(buf); }
// parse a list of Prolog terms and add arguments to an OSC message static int add_msg_args(lo_message msg, term_t list) { term_t head=PL_new_term_ref(); // copy term ref so as not to modify original list=PL_copy_term_ref(list); while (PL_get_list(list,head,list)) { atom_t name; int arity; const char *type; if (!PL_get_name_arity(head,&name,&arity)) return type_error(head,"term"); type=PL_atom_chars(name); switch (arity) { case 1: { term_t a1=PL_new_term_ref(); PL_get_arg(1,head,a1); if (!strcmp(type,"int")) { int x; if (!PL_get_integer(a1,&x)) return type_error(a1,"integer"); lo_message_add_int32(msg,x); } else if (!strcmp(type,"double")) { double x; if (!PL_get_float(a1,&x)) return type_error(a1,"float"); lo_message_add_double(msg,x); } else if (!strcmp(type,"string")) { char *x; if (!PL_get_chars(a1,&x,CVT_ATOM|CVT_STRING)) return type_error(a1,"string"); lo_message_add_string(msg,x); } else if (!strcmp(type,"symbol")) { char *x; if (!PL_get_chars(a1,&x,CVT_ATOM)) return type_error(a1,"atom"); lo_message_add_symbol(msg,x); } else if (!strcmp(type,"float")) { double x; if (!PL_get_float(a1,&x)) return type_error(a1,"float"); lo_message_add_float(msg,(float)x); } break; } case 0: { if (!strcmp(type,"true")) lo_message_add_true(msg); else if (!strcmp(type,"false")) lo_message_add_false(msg); else if (!strcmp(type,"nil")) lo_message_add_nil(msg); else if (!strcmp(type,"inf")) lo_message_add_infinitum(msg); break; } } } if (!PL_get_nil(list)) return type_error(list,"nil"); return TRUE; }
/* * get the name and arity of an SWI Event. */ eventType* getEventType(term_t event_term) { atom_t name; int arity; char* eventName = (char*)malloc(256*sizeof(char)); eventType* ev = (eventType*)malloc(sizeof(eventType)); PL_get_name_arity(event_term,&name,&arity); eventName = PL_atom_chars(name); strcpy(ev->name,eventName); ev->arity=arity; return ev; }
static foreign_t pl_clingo_add(term_t ccontrol, term_t params, term_t program) { char *prog; clingo_env *ctl; atom_t name; size_t arity; char *param_buf[FAST_PARAMS]; char **prog_params = param_buf; term_t arg = PL_new_term_ref(); int rc; if (!(rc = get_clingo(ccontrol, &ctl))) { goto out; } if (!get_name_arity(params, &name, &arity)) { rc = PL_type_error("callable", params); goto out; } if (arity + 1 > FAST_PARAMS && !(prog_params = malloc(sizeof(char *) * arity))) { rc = PL_resource_error("memory"); goto out; } for (size_t i = 0; i < arity; i++) { _PL_get_arg(i + 1, params, arg); if (!(rc = get_null_terminated_string(arg, &prog_params[i], CVT_ATOM))) { goto out; } } if (!(rc = get_null_terminated_string(program, &prog, CVT_ATOM | CVT_STRING | CVT_LIST | BUF_DISCARDABLE))) { goto out; } if (!(rc = clingo_status( clingo_control_add(ctl->control, PL_atom_chars(name), (const char **)prog_params, arity, prog)))) { goto out; } out: if (prog_params != param_buf) { free(prog_params); } return rc; }
/* * Trigger an event without explicitely defining timestamps */ foreign_t triggerEvent_u(term_t args) { atom_t name; int arity=0; PL_get_name_arity(args,&name,&arity); /* find from _event_hash the EventModel to follow */ eventHash tempEventHash; eventHash* foundEventNode = NULL; memset(&tempEventHash,0,sizeof(eventHash)); tempEventHash.ev.arity=arity; strcpy(tempEventHash.ev.name,PL_atom_chars(name)); HASH_FIND(hh,_event_hash,&tempEventHash.ev,sizeof(eventType),foundEventNode); /* if EventModel is in the Hash */ if(foundEventNode != NULL) { EtalisEvent *myEvent=(EtalisEvent*)malloc(sizeof(EtalisEvent)); myEvent->RootModel=foundEventNode->myNode; if (arity != 0) parse_validate_args(args,myEvent); newTimeStamp(&myEvent->timestamps[0]); myEvent->timestamps[1] = myEvent->timestamps[0]; /* debg models */ #ifdef DEBUG EtalisEventNode *myModel = myEvent->RootModel; EtalisExecNode *myExec = myModel->parentNode; #endif /* f dbg */ debug("--- Triggering event ... %s/%d @ [%ld.%06ld,%ld.%06ld]\n",myEvent->RootModel->event.name,myEvent->RootModel->event.arity,myEvent->timestamps[0].time,myEvent->timestamps[0].parts,myEvent->timestamps[1].time,myEvent->timestamps[1].parts); foundEventNode->myNode->trigger(myEvent); } PL_succeed; }
void default_op_parser(EtalisExecNode* operatorNode,term_t t) { assert(operatorNode != NULL); PL_open_foreign_frame(); operatorNode->leftChild=(EtalisEventNode*)malloc(sizeof(EtalisEventNode)); operatorNode->rightChild=NULL; operatorNode->condition=NULL; operatorNode->window_size=0; term_t _left_event=t; atom_t _left_event_name; PL_get_name_arity(_left_event,&_left_event_name,&((operatorNode->leftChild)->event.arity)); strcpy(operatorNode->leftChild->event.name,PL_atom_chars(_left_event_name)); }
int printMessage(atom_t severity, ...) { GET_LD wakeup_state wstate; term_t av; predicate_t pred = PROCEDURE_print_message2; va_list args; int rc; if ( ++LD->in_print_message >= OK_RECURSIVE*3 ) fatalError("printMessage(): recursive call\n"); if ( !saveWakeup(&wstate, TRUE PASS_LD) ) { LD->in_print_message--; return FALSE; } av = PL_new_term_refs(2); va_start(args, severity); PL_put_atom(av+0, severity); rc = PL_unify_termv(av+1, args); va_end(args); if ( rc ) { if ( isDefinedProcedure(pred) && LD->in_print_message <= OK_RECURSIVE ) { rc = PL_call_predicate(NULL, PL_Q_NODEBUG|PL_Q_CATCH_EXCEPTION, pred, av); } else if ( LD->in_print_message <= OK_RECURSIVE*2 ) { Sfprintf(Serror, "Message: "); if ( ReadingSource ) Sfprintf(Serror, "%s:%d ", PL_atom_chars(source_file_name), (int)source_line_no); rc = PL_write_term(Serror, av+1, 1200, 0); Sfprintf(Serror, "\n"); } else /* in_print_message == 2 */ { Sfprintf(Serror, "printMessage(): recursive call\n"); } } restoreWakeup(&wstate PASS_LD); LD->in_print_message--; return rc; }
// parse a list of type terms and encode as a NULL terminated // string where each character encodes the type of one argument. static int get_types_list(term_t list, char *typespec, int len) { term_t head=PL_new_term_ref(); int count=0; // copy term ref so as not to modify original list=PL_copy_term_ref(list); while (PL_get_list(list,head,list) && count<len) { atom_t name; int arity; const char *type; if (!PL_get_name_arity(head,&name,&arity)) return type_error(head,"term"); type=PL_atom_chars(name); switch (arity) { case 1: { if (!strcmp(type,"int")) { typespec[count++]='i'; } else if (!strcmp(type,"double")) { typespec[count++]='d'; } else if (!strcmp(type,"string")) { typespec[count++]='s'; } else if (!strcmp(type,"symbol")) { typespec[count++]='S'; } else if (!strcmp(type,"float")) { typespec[count++]='f'; } break; } case 0: { if (!strcmp(type,"true")) typespec[count++]='T'; else if (!strcmp(type,"false")) typespec[count++]='F'; else if (!strcmp(type,"nil")) typespec[count++]='N'; else if (!strcmp(type,"inf")) typespec[count++]='I'; break; } } } typespec[count]=0; if (!PL_get_nil(list)) return type_error(list,"nil"); return TRUE; }
static int get_lang_stemmer(term_t t, struct sb_stemmer **stemmer) { stem_cache *cache = get_cache(); atom_t lang; int i; if ( !PL_get_atom(t, &lang) ) return type_error("atom", t); for(i=0; i<CACHE_SIZE; i++) { if ( cache->stemmers[i].language == lang ) { *stemmer = cache->stemmers[i].stemmer; return TRUE; } } for(i=0; i<CACHE_SIZE; i++) { if ( !cache->stemmers[i].stemmer ) { struct sb_stemmer *st; if ( !(st= sb_stemmer_new(PL_atom_chars(lang), NULL)) ) { if ( errno == ENOMEM ) return resource_error("memory"); else return domain_error("snowball_algorithm", t); } cache->stemmers[i].language = lang; cache->stemmers[i].stemmer = st; PL_register_atom(cache->stemmers[i].language); *stemmer = cache->stemmers[i].stemmer; return TRUE; } } assert(0); /* TBD: clean cache */ return FALSE; }
int destroySourceFile(SourceFile sf) { Symbol s; DEBUG(MSG_SRCFILE, Sdprintf("Destroying source file %s\n", PL_atom_chars(sf->name))); clearSourceAdmin(sf); LOCK(); s = lookupHTable(GD->files.table, (void*)sf->name); assert(s); deleteSymbolHTable(GD->files.table, s); PL_unregister_atom(sf->name); putSourceFileArray(sf->index, NULL); if ( GD->files.no_hole_before > sf->index ) GD->files.no_hole_before = sf->index; UNLOCK(); unallocSourceFile(sf); return TRUE; }
gint release_plgi_blob(atom_t a) { PLGIBlob *blob = PL_blob_data(a, NULL, NULL); PLGI_debug("[GC] releasing: <%s>%p", PL_atom_chars(blob->name), blob->data); if ( blob->magic != PLGI_BLOB_MAGIC) { return TRUE; } blob->magic = 0x0; switch ( blob->blob_type ) { case PLGI_BLOB_GOBJECT: { g_object_unref(blob->data); break; } case PLGI_BLOB_GPARAMSPEC: { g_param_spec_unref(blob->data); break; } case PLGI_BLOB_GVARIANT: { g_variant_unref(blob->data); break; } case PLGI_BLOB_SIMPLE: { if ( blob->is_owned ) { g_free(blob->data); } break; } case PLGI_BLOB_BOXED: { if ( blob->is_owned ) { g_boxed_free(blob->gtype, blob->data); } break; } case PLGI_BLOB_FOREIGN: { chokeme(PLGI_BLOB_FOREIGN); break; } case PLGI_BLOB_OPAQUE: { break; } case PLGI_BLOB_UNTYPED: { break; } default: { g_assert_not_reached(); } } return TRUE; }
static foreign_t python_access(term_t obj, term_t f, term_t out) { PyObject *o = term_to_python(obj, true), *pValue, *pArgs, *pF; atom_t name; char *s; int i, arity; term_t targ = PL_new_term_ref(); if (o == NULL) { return false; } if (PL_is_atom(f)) { if (!PL_get_atom_chars(f, &s)) { return false; } if ((pValue = PyObject_GetAttrString(o, s)) == NULL) { PyErr_Print(); { return false; } } { return python_to_term(pValue, out); } } if (!PL_get_name_arity(f, &name, &arity)) { { return false; } } /* follow chains of the form a.b.c.d.e() */ while (name == ATOM_dot && arity == 2) { term_t tleft = PL_new_term_ref(); PyObject *lhs; if (!PL_get_arg(1, f, tleft)) { return false; } lhs = term_to_python(tleft, true); if ((o = PyObject_GetAttr(o, lhs)) == NULL) { PyErr_Print(); { return false; } } if (!PL_get_arg(2, f, f)) { return false; } if (!PL_get_name_arity(f, &name, &arity)) { { return false; } } } s = PL_atom_chars(name); if (!s) { return false; } if ((pF = PyObject_GetAttrString(o, s)) == NULL) { PyErr_Print(); { return false; } } pArgs = PyTuple_New(arity); for (i = 0; i < arity; i++) { PyObject *pArg; if (!PL_get_arg(i + 1, f, targ)) { return false; } /* ignore (_) */ if (i == 0 && PL_is_variable(targ)) { pArgs = Py_None; } pArg = term_to_python(targ, true); if (pArg == NULL) { return false; } /* pArg reference stolen here: */ PyTuple_SetItem(pArgs, i, pArg); } pValue = PyObject_CallObject(pF, pArgs); Py_DECREF(pArgs); Py_DECREF(pF); if (pValue == NULL) { { return false; } } { return python_to_term(pValue, out); } }
static bool get_value(term_t t, clingo_symbol_t *val, int minus) { switch (PL_term_type(t)) { case PL_INTEGER: { int i; if (PL_get_integer(t, &i)) { clingo_symbol_create_number(i, val); return true; } return false; } case PL_ATOM: { char *s; size_t len; if (PL_get_nchars(t, &len, &s, CVT_ATOM | REP_UTF8 | CVT_EXCEPTION)) { return clingo_symbol_create_id(s, !minus, val); /* no sign */ } return false; } case PL_STRING: { char *s; size_t len; if (PL_get_nchars(t, &len, &s, CVT_STRING | REP_UTF8 | CVT_EXCEPTION)) { return clingo_symbol_create_string(s, val); } return false; } case PL_TERM: { bool rc; term_t arg; atom_t name; size_t arity; /* TBD: -atom, #const */ clingo_symbol_t *values = NULL; if (!(rc = get_name_arity(t, &name, &arity))) { clingo_set_error(clingo_error_runtime, "prolog error"); goto out_term; } arg = PL_new_term_ref(); if (name == ATOM_minus && arity == 1) { if (!(rc = get_value(arg, val, TRUE))) { goto out_term; } } else if (name == ATOM_hash && arity == 1) { atom_t a; _PL_get_arg(1, t, arg); if (!(rc = PL_get_atom_ex(arg, &a))) { clingo_set_error(clingo_error_runtime, "prolog error"); goto out_term; } if (a == ATOM_inf) { clingo_symbol_create_infimum(val); } else if (a == ATOM_sup) { clingo_symbol_create_supremum(val); } else { rc = false; clingo_set_error(clingo_error_runtime, "bad value"); goto out_term; } } else { const char *id = PL_atom_chars(name); /* TBD: errors */ size_t i; if (!(values = malloc(sizeof(*values) * arity))) { rc = false; clingo_set_error(clingo_error_bad_alloc, "memory"); goto out_term; } for (i = 0; i < arity; i++) { _PL_get_arg(i + 1, t, arg); if (!(rc = get_value(arg, &values[i], FALSE))) { goto out_term; } } PL_reset_term_refs(arg); if (!(rc = clingo_symbol_create_function(id, values, arity, !minus, val))) { goto out_term; } } out_term: if (values) { free(values); } return rc; } default: clingo_set_error(clingo_error_runtime, "bad value"); return false; } }
/* 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; }
/* General structure of a rule : [Rule_Label] ComplexEvent <- CEP_Clause [WHERE_Clause] [WITHIN_Clause] */ void construct_rule(EtalisBatch* batch,term_t term) { EtalisExecNode* NodeRule = batch->nodes; assert(NodeRule != NULL); atom_t cep_name; int temp_arity; int i=0; int LUT_Size=2; #ifdef DEBUG printf("--- Constructing rule: \n"); #endif /* WITHIN Clause */ parse_within_op_(NodeRule,term); /*TODO Add a check that a within is explicitely stated in the rule */ /* WHERE Clause */ parse_where_op_(NodeRule,term); /* CEP Clause */ term_t cep_term = PL_new_term_refs(2); if(NodeRule->has_condition == ETALIS_TRUE) { term_t first_level_term = PL_new_term_refs(2); PL_get_arg(1,term,first_level_term); PL_get_arg(1,first_level_term,cep_term); } else PL_get_arg(1,term,cep_term); PL_get_name_arity(cep_term, &cep_name, &temp_arity); char* aaa = PL_atom_chars(cep_name); /* find the right CEP operator */ while(strcmp(CEP_LUT_[i].CEP_name,PL_atom_chars(cep_name)) != 0 && i<LUT_Size) i++; NodeRule->left_exec.exec_1=CEP_LUT_[i].exec1.exec_1; NodeRule->right_exec.exec_1=CEP_LUT_[i].exec2.exec_1; if(i != LUT_Size) /*The operator is found in the CEP_LUT_*/ { switch (CEP_LUT_[i].CEP_arity) { case 1: NodeRule->op_type=unary; strcpy(NodeRule->name,CEP_LUT_[i].CEP_name); NodeRule->left_exec.exec_1=CEP_LUT_[i].exec1.exec_1; break; case 2: NodeRule->op_type=binary; strcpy(NodeRule->name,CEP_LUT_[i].CEP_name); NodeRule->left_exec.exec_1=CEP_LUT_[i].exec1.exec_1; NodeRule->right_exec.exec_1=CEP_LUT_[i].exec2.exec_1; CEP_LUT_[i].parser_func(NodeRule,cep_term); break; default: printf("error compiling the rules\n"); } } else /*no operator is found : identity operator is assumed*/ { NodeRule->op_type=unary; strcpy(NodeRule->name,"identity"); NodeRule->left_exec.exec_1=_cep_identity; default_op_parser(NodeRule,cep_term); } /* triggering an event should execute the correct function*/ /* deprecated */ /* NodeRule->leftChild->trigger=NodeRule->left_exec.exec_1; if(NodeRule->op_type == binary) NodeRule->rightChild->trigger=NodeRule->right_exec.exec_1; */ /* setting the tree connections between the events and the CEP operator */ NodeRule->leftChild->parentNode=NodeRule; if(NodeRule->op_type == binary) NodeRule->rightChild->parentNode=NodeRule; /** Propagate the WHERE Clauses */ where_binarization(NodeRule); }
static foreign_t python_builtin_eval(term_t caller, term_t dict, term_t out) { PyObject *pI, *pArgs, *pOut; PyObject *env; atom_t name; char *s; int i, arity; term_t targ = PL_new_term_ref(); if ((env = py_Builtin) == NULL) { // no point in even trying { return false; } } if (PL_get_name_arity(caller, &name, &arity)) { if (!(s = PL_atom_chars(name))) { return false; } if ((pI = PyObject_GetAttrString(env, s)) == NULL) { PyErr_Print(); { return false; } } } else { // Prolog should make sure this never happens. { return false; } } pArgs = PyTuple_New(arity); for (i = 0; i < arity; i++) { PyObject *pArg; if (!PL_get_arg(i + 1, caller, targ)) { return false; } /* ignore (_) */ if (i == 0 && PL_is_variable(targ)) { pArg = Py_None; } else { pArg = term_to_python(targ, true); if (pArg == NULL) { return false; } } /* pArg reference stolen here: */ if (PyTuple_SetItem(pArgs, i, pArg)) { PyErr_Print(); { return false; } } } pOut = PyObject_CallObject(pI, pArgs); Py_DECREF(pArgs); Py_DECREF(pI); if (pOut == NULL) { PyErr_Print(); { return false; } } { foreign_t rc = python_to_ptr(pOut, out); ; return rc; } }
void parse_seq_op_ (EtalisExecNode* operatorNode,term_t t) { assert(operatorNode != NULL); fid_t fid = PL_open_foreign_frame(); /*get components of the operator*/ term_t _level_1 = PL_new_term_refs(2); term_t _left_event=_level_1; term_t _right_event=_level_1+1; atom_t _left_event_name,_right_event_name; int temp_arity,i=0; /* TODO check for embedded operators, if operator -> parse right function ; else get atomic events */ PL_get_arg(1,t,_left_event); PL_get_name_arity(_left_event,&_left_event_name,&temp_arity); char * tt = PL_atom_chars(_left_event_name); i=0; while(strcmp(CEP_LUT_[i].CEP_name,PL_atom_chars(_left_event_name)) != 0 && i<LUT_Size) i++; /* #CONT */ if(i != LUT_Size) /*The operator is found in the CEP_LUT_*/ /* create a temp event */ { EtalisEventNode* TempEvent = (EtalisEventNode*)calloc(1,sizeof(EtalisEventNode)); /* this temp event is the complex event of the embedded operation */ TempEvent->event.arity = 0; strcpy(TempEvent->event.name,"temp_"); /* TODO Temp Events */ /* Temp Events have no arguments */ TempEvent->parentNode = operatorNode; strcpy(TempEvent->parentNode->name,tt); TempEvent->is_temp = ETALIS_TRUE; operatorNode->leftChild = TempEvent; EtalisExecNode* NewNodeRule = (EtalisExecNode*)calloc(1,sizeof(EtalisExecNode)); /* binarization and allocation of the embedded operator */ TempEvent->childNode = NewNodeRule; NewNodeRule->parentEvent = TempEvent; TempEvent->trigger = _seq_win_cep_l; switch (CEP_LUT_[i].CEP_arity) { case 1: break; case 2: /* found embedded */ CEP_LUT_[i].parser_func(NewNodeRule,_left_event); break; default: printf("error compiling the rules\n"); } } else /* an atomic event has been found */ { operatorNode->leftChild = (EtalisEventNode*) calloc(1,sizeof(EtalisEventNode)); PL_get_name_arity(_left_event,&_left_event_name,(int *)&((operatorNode->leftChild)->event.arity)); strcpy(operatorNode->leftChild->event.name,PL_atom_chars(_left_event_name)); operatorNode->leftChild->childNode = NULL; operatorNode->leftChild->parentNode=operatorNode; operatorNode->leftChild->is_temp = ETALIS_FALSE; if (operatorNode->parentEvent->is_temp) strcat(operatorNode->parentEvent->event.name,operatorNode->leftChild->event.name); operatorNode->leftChild->trigger = _seq_win_cep_l; } PL_get_arg(2,t,_right_event); operatorNode->rightChild = (EtalisEventNode*) calloc(1,sizeof(EtalisEventNode)); PL_get_name_arity(_right_event,&_right_event_name,(int *)&(operatorNode->rightChild->event.arity)); strcpy(operatorNode->rightChild->event.name,PL_atom_chars(_right_event_name)); operatorNode->rightChild->childNode = NULL; operatorNode->rightChild->parentNode = operatorNode; operatorNode->rightChild->is_temp = ETALIS_FALSE; PL_discard_foreign_frame(fid); if (operatorNode->parentEvent->is_temp) { strcat(operatorNode->parentEvent->event.name,operatorNode->rightChild->event.name); operatorNode->rightChild->trigger = _seq_win_cep_l; } else /* event is latest event in the call list */ { operatorNode->rightChild->trigger = _seq_batch_r; } ; }
/* * parse a where clause and add the information into the ExecNode. * TODO #hafsi#5# * TODO implement a non binary, rule wide WHERE clause parser and interpreter. * */ void parse_where_op_(EtalisExecNode* operatorNode, term_t t) { /* find out whether a where clause is used */ term_t constraints = PL_new_term_refs(3); atom_t wheref; int arr; ETALIS_BOOL where_available=0; PL_get_arg(1,t,constraints); PL_get_name_arity(constraints,&wheref,&arr); char* gg = (char*)malloc(sizeof(char)*256); memset(gg,0,256); gg = PL_atom_chars(wheref); if(!strcmp(gg,"wheref")) where_available = ETALIS_TRUE; if(where_available) { /* process where clause */ /* A = eventClause(unlabeled, e2(_G321, _G322, _G323, _G324), withinop(wheref(seqf(a(_G321, _G322), d(_G323, _G324)), conditions), 2.0)) */ operatorNode->whereNode =(EtalisWhereNode*)malloc(sizeof(EtalisWhereNode)); memset(operatorNode->whereNode,0,sizeof(EtalisWhereNode)); term_t _where_level_1 = PL_new_term_refs(2); term_t rule_gut_term = _where_level_1+1; term_t constraints_term = _where_level_1+2; PL_get_arg(1,t,_where_level_1); PL_get_arg(1,_where_level_1,rule_gut_term); PL_get_arg(2,_where_level_1,constraints_term); #ifdef DEBUG char* testing = (char*)malloc(sizeof(char)*256); char* args = (char*)malloc(sizeof(char)*256); *args="\0"; memset(testing,0,256); int size_contraints,idx=0; PL_get_name_arity(constraints_term,NULL,&size_contraints); termToStringVerbatim(_where_level_1,testing,args); /* wheref(seqf(seqf(a(_G1776),b(_G1778)),c(_G1780)),,(>(_G1776,1),<(_G1778,2))) */ int j=strlen(testing); int num_=0; for (j=strlen(testing)-2;j>0;j--) { if (testing[j] == ')') num_++; if (testing[j] == '(') num_--; /*printf("%c : %d : %d\n",testing[j],j,num_);*/ if(num_ == 0 ) break; } char* real_constr = testing + j ; printf("--- WHERE Block detected | Constraints: %s \n",real_constr); #endif operatorNode->has_condition=ETALIS_TRUE; } else /* no constraints are detected */ { /*get atomic events of the operator*/ /* A = eventClause(unlabeled, e2(_G321, _G322, _G323, _G324), withinop(seqf(a(_G321, _G322), d(_G323, _G324)), 2.0)). */ operatorNode->has_condition=ETALIS_FALSE; } }