int add_varc(int nVal,prolog_term probabilities) { variable * v; int i; double p,p0; nVars=nVars+1; vars=(variable *) realloc(vars,nVars * sizeof(variable)); v=&vars[nVars-1]; v->nVal=nVal; probs=(double *) realloc(probs,(((boolVars+v->nVal-1)* sizeof(double)))); p0=1; for (i=0; i<v->nVal-1; i++) { p=p2c_float(p2p_car(probabilities)); probs[boolVars+i]=p/p0; probabilities=p2p_cdr(probabilities); p0=p0*(1-p/p0); } v->firstBoolVar=boolVars; boolVars=boolVars+v->nVal-1; return nVars-1; }
/* print a prolog_term into a buffer. (Atoms are quoted if !toplevel and it's necessary for Prolog reading) */ void printpterm(prolog_term term, int toplevel, char *straddr, long int *ind) { int i; if (is_var(term)) { sprintf(tempstring,"_%p",term); strcpy(straddr+*ind,tempstring); *ind += strlen(tempstring); } else if (is_int(term)) { sprintf(tempstring,"%d",p2c_int(term)); strcpy(straddr+*ind,tempstring); *ind += strlen(tempstring); } else if (is_float(term)) { sprintf(tempstring,"%f",p2c_float(term)); strcpy(straddr+*ind,tempstring); *ind += strlen(tempstring); } else if (is_nil(term)) { strcpy(straddr+*ind,"[]"); *ind += 2; } else if (is_string(term)) { printpstring(p2c_string(term),toplevel,straddr,ind); } else if (is_list(term)) { strcpy(straddr+*ind,"["); *ind += 1; printpterm(p2p_car(term),FALSE,straddr,ind); term = p2p_cdr(term); while (is_list(term)) { strcpy(straddr+*ind,","); *ind += 1; printpterm(p2p_car(term),FALSE,straddr,ind); term = p2p_cdr(term); } if (!is_nil(term)) { strcpy(straddr+*ind,"|"); *ind += 1; printpterm(term,FALSE,straddr,ind); } strcpy(straddr+*ind,"]"); *ind += 1; } else if (is_functor(term)) { printpstring(p2c_functor(term),FALSE,straddr,ind); if (p2c_arity(term) > 0) { strcpy(straddr+*ind,"("); *ind += 1; printpterm(p2p_arg(term,1),FALSE,straddr,ind); for (i = 2; i <= p2c_arity(term); i++) { strcpy(straddr+*ind,","); *ind += 1; printpterm(p2p_arg(term,i),FALSE,straddr,ind); } strcpy(straddr+*ind,")"); *ind += 1; } } else fprintf(stderr,"error, unrecognized type"); }
static char* buildSQLQuery(prolog_term sqlQueryList) { prolog_term element; char* sqlQuery; char* temp; int cnt; size_t i; sqlQuery = (char *)malloc(QUERY_SIZE * sizeof(char)+1); sqlQuery[0] = '\0'; while (!is_nil(sqlQueryList)) { element = p2p_car(sqlQueryList); sqlQueryList = p2p_cdr(sqlQueryList); if (is_string(element)) { if (p2c_string(element)[0] == DB_INTERFACE_TERM_SYMBOL) { cnt = 0; temp = (char *)malloc(ELEMENT_SIZE * sizeof(char)+1); temp[cnt++] = '\''; /* protect inner quotes in Prolog terms */ for (i = 0 ; i < strlen(p2c_string(element)) ; i++) { if (p2c_string(element)[i] == '\'') { temp[cnt++] = '\\'; temp[cnt++] = p2c_string(element)[i]; } else { temp[cnt++] = p2c_string(element)[i]; } } temp[cnt++] = '\''; temp[cnt++] = '\0'; strcat(sqlQuery, temp); if (temp != NULL) { free(temp); temp = NULL; } } else { strcat(sqlQuery, p2c_string(element)); } } else if (is_int(element)) { temp = (char *)malloc(ELEMENT_SIZE * sizeof(char)); sprintf(temp, "%d", (int)p2c_int(element)); strcat(sqlQuery, temp); if (temp != NULL) { free(temp); temp = NULL; } } else if (is_float(element)) { temp = (char *)malloc(ELEMENT_SIZE * sizeof(char)); sprintf(temp, "%f", p2c_float(element)); strcat(sqlQuery, temp); if (temp != NULL) { free(temp); temp = NULL; } } else if (is_var(element)) { errorMesg = "XSB_DBI ERROR: Unbound variable in parameter list"; } } return sqlQuery; }
DllExport int call_conv executePreparedStatement(void) { struct xsb_data** (*executeStmtDriver)(struct xsb_data**, struct xsb_queryHandle*); char* (*errorMesgDriver)(); void (*freeResultDriver)(); struct xsb_queryHandle* qHandle; struct xsb_connectionHandle* cHandle; struct xsb_data** bindValues; struct xsb_data** result; prolog_term bindList, returnList, element; char *queryHandle, *chandle; int i, val; queryHandle = ptoc_string(CTXTc 1); bindList = reg_term(CTXTc 2); returnList = reg_term(CTXTc 3); qHandle = NULL; cHandle = NULL; bindValues = NULL; result = NULL; if ((qHandle = isQueryHandle(queryHandle)) == NULL) { errorMesg = "XSB_DBI ERROR: Query handle does not exist"; errorNumber = "XSB_DBI_005"; return FALSE; } if (qHandle->state == QUERY_BEGIN) { bindValues = (struct xsb_data **)malloc(qHandle->numParams * sizeof(struct xsb_data *)); for (i = 0 ; i < qHandle->numParams ; i++) bindValues[i] = NULL; for (i = 0 ; i < qHandle->numParams ; i++) { bindValues[i] = (struct xsb_data *)malloc(sizeof(struct xsb_data)); bindValues[i]->val = NULL; if (is_nil(bindList)) { errorMesg = "XSB_DBI ERROR: Not all paremeters supplied"; errorNumber = "XSB_DBI_008"; freeBindValues(bindValues,qHandle->numParams); return FALSE; } element = p2p_car(bindList); if (is_string(element)) { bindValues[i]->type = STRING_TYPE; bindValues[i]->length = strlen(p2c_string(element)); bindValues[i]->val = (union xsb_value *)malloc(sizeof(union xsb_value)); bindValues[i]->val->str_val = p2c_string(element); } else if (is_int(element)) { bindValues[i]->type = INT_TYPE; bindValues[i]->val = (union xsb_value *)malloc(sizeof(union xsb_value)); bindValues[i]->val->i_val = p2c_int(element); } else if (is_float(element)) { bindValues[i]->type = FLOAT_TYPE; bindValues[i]->val = (union xsb_value *)malloc(sizeof(union xsb_value)); bindValues[i]->val->f_val = p2c_float(element); } else if (is_functor(element)) { } else if (is_var(element)) { errorMesg = "XSB_DBI ERROR: Unbound variable in parameter list"; errorNumber = "XSB_DBI_009"; freeBindValues(bindValues,qHandle->numParams); return FALSE; } bindList = p2p_cdr(bindList); } } if (getDriverFunction(qHandle->connHandle->driver, EXEC_PREPARE) != NULL) executeStmtDriver = getDriverFunction(qHandle->connHandle->driver, EXEC_PREPARE)->executeStmtDriver; else{ freeBindValues(bindValues,qHandle->numParams); return FALSE; } result = executeStmtDriver(bindValues, qHandle); freeBindValues(bindValues,qHandle->numParams); if (result == NULL && qHandle->state == QUERY_BEGIN) { if (getDriverFunction(qHandle->connHandle->driver, ERROR_MESG) != NULL) errorMesgDriver = getDriverFunction(qHandle->connHandle->driver, ERROR_MESG)->errorMesgDriver; else return FALSE; errorMesg = errorMesgDriver(); if (errorMesg != NULL) return FALSE; } val = bindReturnList(returnList, result, qHandle); if (result == NULL) { qHandle->state = QUERY_BEGIN; } else { if (getDriverFunction(qHandle->connHandle->driver, FREE_RESULT) != NULL) freeResultDriver = getDriverFunction(qHandle->connHandle->driver, FREE_RESULT)->freeResultDriver; else return FALSE; freeResultDriver(result, qHandle->numResultCols); } if (val == TOO_MANY_RETURN_COLS || val == TOO_FEW_RETURN_COLS || val == INVALID_RETURN_LIST) return FALSE; cHandle = qHandle->connHandle; chandle = cHandle->handle; if ((cHandle = isConnectionHandle(chandle)) != NULL) { if (getDriverFunction(cHandle->driver, ERROR_MESG) != NULL) errorMesgDriver = getDriverFunction(cHandle->driver, ERROR_MESG)->errorMesgDriver; else return FALSE; errorMesg = errorMesgDriver(); errorNumber = "XSB_DBI_000"; } /* if (errorMesg == NULL && (val == RESULT_NONEMPTY_OR_NOT_REQUESTED || val == RESULT_EMPTY_BUT_REQUESTED)){ */ if (errorMesg == NULL && val == RESULT_NONEMPTY_OR_NOT_REQUESTED) { return TRUE; } else return FALSE; }
int set_python_argument(prolog_term temp, PyObject *pArgs,int i) { PyObject *pValue; if(find_prolog_term_type(temp) == INT) { prolog_term argument = temp; int argument_int = p2c_int(argument); pValue = PyInt_FromLong(argument_int); PyTuple_SetItem(pArgs, i-1, pValue); }else if(find_prolog_term_type(temp) == STRING) { prolog_term argument = temp; char *argument_char = p2c_string(argument); //printf("%s", argument_char); pValue = PyString_FromString(argument_char); PyTuple_SetItem(pArgs, i-1, pValue); }else if(find_prolog_term_type(temp) == FLOAT) { prolog_term argument = temp; float argument_float = p2c_float(argument); pValue = PyFloat_FromDouble(argument_float); PyTuple_SetItem(pArgs, i-1, pValue); } else if(find_prolog_term_type(temp) == LIST) { prolog_term argument = temp; int count = find_length_prolog_list(argument); PyObject *pList = PyList_New(count); if (! prlist2pyList(argument, pList, count)) return FALSE; PyTuple_SetItem(pArgs, i-1, pList); } else if (find_prolog_term_type(temp) == FUNCTOR) { //region begin : handle pyobject term if(strcmp(p2c_functor(temp),"pyObject") == 0) { prolog_term ref = p2p_arg(temp, 1); char *node_pointer = p2c_string(ref); pyobj_ref_node *pyobj_ref = (pyobj_ref_node *)strtol(node_pointer,NULL, 0); //pyobj_ref_node *node = (pyobj_ref_node *)(node_pointer); pValue = pyobj_ref->python_obj; //printf("set_argN: %p %zu" , pValue, pyobj_ref->ref_id); PyTuple_SetItem(pArgs, i - 1 , pValue); } //end region } // else if(find_prolog_term_type(temp) == REF) // { //when a reference is passed. // prolog_term argument = temp; // char *argument_ref = p2c_string(argument); // if(strncmp("ref_", argument_ref, 4)== 0 ) // { //gets the reference id from the string and finds it in the list // argument_ref = argument_ref + 4; // size_t ref = strtoul(argument_ref, NULL, 0); // PyObject *obj = get_pyobj_ref_list(ref); // if(obj == NULL) // return FALSE; // PyTuple_SetItem(pArgs, i-1, obj); // } // } return TRUE; }
int convert_prObj_pyObj(prolog_term prTerm, PyObject **pyObj) { if(find_prolog_term_type(prTerm) == INT) { prolog_term argument = prTerm; int argument_int = p2c_int(argument); *pyObj = PyInt_FromLong(argument_int); return TRUE; }else if(find_prolog_term_type(prTerm) == STRING) { prolog_term argument = prTerm; char *argument_char = p2c_string(argument); *pyObj = PyString_FromString(argument_char); return TRUE; }else if(find_prolog_term_type(prTerm) == FLOAT) { prolog_term argument = prTerm; float argument_float = p2c_float(argument); *pyObj = PyFloat_FromDouble(argument_float); return TRUE; } else if(find_prolog_term_type(prTerm) == LIST) { prolog_term argument = prTerm; if(find_prolog_term_type(argument) == LIST) { int count = find_length_prolog_list(argument); PyObject *pList = PyList_New(count); if(!prlist2pyList(argument, pList, count)) return FALSE; *pyObj = pList; return TRUE; } } // else if(find_prolog_term_type(prTerm) == REF) // { //when a reference is passed. // prolog_term argument = prTerm; // char *argument_ref = p2c_string(argument); // if(strncmp("ref_", argument_ref, 4)== 0 ) // { //gets the reference id from the string and finds it in the list // argument_ref = argument_ref + 4; // size_t ref = strtoul(argument_ref, NULL, 0); // PyObject *obj = get_pyobj_ref_list(ref); // if(obj == NULL) // return FALSE; // *pyObj = obj; // return TRUE; // } // } else if (find_prolog_term_type(prTerm) == FUNCTOR) { //region begin : handle pyobject term if(strcmp(p2c_functor(prTerm),"pyObject") == 0) { prolog_term ref = p2p_arg(prTerm, 1); char *node_pointer = p2c_string(ref); pyobj_ref_node *pyobj_ref = (pyobj_ref_node *)strtol(node_pointer,NULL, 0); //pyobj_ref_node *node = (pyobj_ref_node *)(node_pointer); *pyObj = pyobj_ref->python_obj; } //end region } return FALSE; }