static YAP_Bool point_list_to_sequence (YAP_Term term, unsigned int size, sequence_t *sequence) { YAP_Float x, y; unsigned int n; YAP_Term head; *sequence = GEOSCoordSeq_create (size, 2); if (*sequence == NULL) return (FALSE); for (n = 0; YAP_IsPairTerm (term) != FALSE; n ++) { assert (n < size); head = YAP_HeadOfTerm (term); if ((Yap_IsNumberTerm (YAP_ArgOfTerm (1, head), &x) == FALSE) || (Yap_IsNumberTerm (YAP_ArgOfTerm (2, head), &y) == FALSE) || (GEOSCoordSeq_setX (*sequence, n, x) == 0) || (GEOSCoordSeq_setY (*sequence, n, y) == 0)) { GEOSCoordSeq_destroy (*sequence); return (FALSE); } term = YAP_TailOfTerm (term); } assert (n == size); assert (YAP_IsAtomTerm (term) != FALSE); assert (strcmp (YAP_AtomName (YAP_AtomOfTerm (term)), "[]") == 0); return (TRUE); }
static int p_create_cell_matrix_and_copy1(void) { int rows, cols; mxArray *mat; YAP_Term tl = YAP_ARG3; rows = YAP_IntOfTerm(YAP_ARG1); cols = YAP_IntOfTerm(YAP_ARG2); if (!(mat = mxCreateCellMatrix(rows, cols))) return FALSE; while (YAP_IsPairTerm(tl)) { YAP_Term th = YAP_HeadOfTerm(tl); int off = MAT_ACCESS(YAP_IntOfTerm(YAP_ArgOfTerm(1,th))-1, YAP_IntOfTerm(YAP_ArgOfTerm(2,th))-1, rows,cols); mxArray *mat2 = get_array(YAP_ArgOfTerm(3,th)); mxSetCell(mat,off, mat2); tl = YAP_TailOfTerm(tl); } if (YAP_IsAtomTerm(YAP_ARG4)) { return !engPutVariable(Meng, YAP_AtomName(YAP_AtomOfTerm(YAP_ARG4)), mat); } return YAP_Unify(YAP_ARG4,address2term(mat)); }
static int progress( void *instance, const lbfgsfloatval_t *local_x, const lbfgsfloatval_t *local_g, const lbfgsfloatval_t fx, const lbfgsfloatval_t xnorm, const lbfgsfloatval_t gnorm, const lbfgsfloatval_t step, int n, int k, int ls ) { YAP_Term call; YAP_Bool result; YAP_Int s1; YAP_Term t[8]; t[0] = YAP_MkFloatTerm(fx); t[1] = YAP_MkFloatTerm(xnorm); t[2] = YAP_MkFloatTerm(gnorm); t[3] = YAP_MkFloatTerm(step); t[4] = YAP_MkIntTerm(n); t[5] = YAP_MkIntTerm(k); t[6] = YAP_MkIntTerm(ls); t[7] = YAP_MkVarTerm(); call = YAP_MkApplTerm( fprogress8, 8, t); s1 = YAP_InitSlot(call); optimizer_status=OPTIMIZER_STATUS_CB_PROGRESS; result=YAP_CallProlog(call); optimizer_status=OPTIMIZER_STATUS_RUNNING; call = YAP_GetFromSlot( s1 ); if (result==FALSE) { printf("ERROR: Calling the progress call back function in YAP.\n"); // Goal did not succeed return FALSE; } if (YAP_IsIntTerm(YAP_ArgOfTerm(8,call))) { return YAP_IntOfTerm(YAP_ArgOfTerm(8,call)); } YAP_ShutdownGoal( TRUE ); fprintf(stderr, "ERROR: The progress call back function did not return an integer as last argument\n"); return 1; }
static int cuda_load_fact( void ) { int i = currentFact; #if defined(DATALOG) || defined(TUFFY) YAP_Term th = YAP_ARG1; int ncols = currentPred->num_columns; int j; int *mat = currentPred->address_host_table; for (j = 0; j < ncols; j++) { YAP_Term ta = YAP_ArgOfTerm(j+1, th); if (YAP_IsAtomTerm(ta)) { mat[i*ncols+j] = YAP_AtomToInt(YAP_AtomOfTerm(ta)); } else { mat[i*ncols+j] = YAP_IntOfTerm(ta); } } #endif i++; if (i == currentPred->num_rows) { Cuda_NewFacts(currentPred); currentPred = NULL; currentFact = 0; } else { currentFact = i; } return TRUE; }
static mxArray* get_array(YAP_Term ti) { if (YAP_IsIntTerm(ti)) { return mxCreateDoubleScalar(YAP_IntOfTerm(ti)); } else if (YAP_IsFloatTerm(ti)) { return mxCreateDoubleScalar(YAP_FloatOfTerm(ti)); } else if (YAP_IsAtomTerm(ti)) { return matlab_getvar(ti); } else if (YAP_IsPairTerm(ti)) { YAP_Term tv = YAP_HeadOfTerm(ti); YAP_Term tf = YAP_TailOfTerm(ti); const mxArray *mout; if (!YAP_IsAtomTerm(tv)) { char s[BUFSIZE]; if (!YAP_StringToBuffer(ti, s, BUFSIZE)) return FALSE; return mxCreateString(s); } mout = matlab_getvar(tv); if (!mout) return FALSE; if (YAP_IsIntTerm(tf)) { return mxGetFieldByNumber(mout, 0, YAP_IntOfTerm(tf)); } else if (YAP_IsAtomTerm(tf)) { const char *s=YAP_AtomName(YAP_AtomOfTerm(tf)); return mxGetField(mout, 0, s); } else { return NULL; } } else { return (mxArray *)YAP_IntOfTerm(YAP_ArgOfTerm(1,ti)); } }
static YAP_Bool line_to_geometry (YAP_Term term, geometry_type_t type, geometry_t *geometry) { sequence_t sequence; unsigned int size; assert ((type == GEOS_LINESTRING) || (type == GEOS_LINEARRING)); assert (geometry != NULL); term = YAP_ArgOfTerm (1, term); if ((is_list_get_size (term, &size) == FALSE) || ((type == GEOS_LINESTRING) && (size < 2)) || ((type == GEOS_LINEARRING) && (size < 4))) return (FALSE); if (point_list_to_sequence (term, size, &sequence) == FALSE) return (FALSE); if (type == GEOS_LINESTRING) *geometry = GEOSGeom_createLineString (sequence); else *geometry = GEOSGeom_createLinearRing (sequence); if (*geometry == NULL) return (FALSE); return (TRUE); }
static YAP_Bool make_point_to_geometry (YAP_Term term, geometry_t *geometry) { YAP_Functor functor; const char * functor_name; unsigned int arity; YAP_Term p[2]; if (YAP_IsApplTerm (term) == FALSE) return (FALSE); functor = YAP_FunctorOfTerm (term); functor_name = YAP_AtomName (YAP_NameOfFunctor (functor)); arity = YAP_ArityOfFunctor (functor); if ((strcmp (functor_name, ",") != 0) || (arity != 2)) return (FALSE); p[0] = YAP_ArgOfTerm (1, term); p[1] = YAP_ArgOfTerm (2, term); functor = YAP_MkFunctor (YAP_LookupAtom (NAME_POINT), 2); term = YAP_MkApplTerm (functor, 2, p); return (point_to_geometry (term, geometry)); }
static YAP_Bool list_to_geometry (YAP_Term term, unsigned int minimum, procedure_to_geometry_t procedure, geometry_type_t geometry_type, geometry_t *geometry) { geometry_t *p; unsigned int size; unsigned int v, n; YAP_Term head; assert (geometry != NULL); term = YAP_ArgOfTerm (1, term); if ((is_list_get_size (term, &size) == FALSE) || (size < minimum)) return (FALSE); p = (geometry_t *) malloc (sizeof (geometry_t) * size); if (p == NULL) { warning ("%s: list_to_geometry: not enough memory", __FILE__); return (FALSE); } memset (p, 0, sizeof (geometry_t) * size); for (n = 0; YAP_IsPairTerm (term) != FALSE; n ++) { assert (n < size); head = YAP_HeadOfTerm (term); if (procedure (head, &p[n]) == FALSE) { for (v = 0; v < n; v ++) GEOSGeom_destroy (p[v]); free (p); return (FALSE); } term = YAP_TailOfTerm (term); } assert (n == size); assert (YAP_IsAtomTerm (term) != FALSE); assert (strcmp (YAP_AtomName (YAP_AtomOfTerm (term)), "[]") == 0); if (geometry_type == GEOS_POLYGON) *geometry = GEOSGeom_createPolygon (p[0], p + 1, size - 1); else *geometry = GEOSGeom_createCollection (geometry_type, p, size); memset (p, 0, sizeof (geometry_t) * size); free (p); if (*geometry == NULL) return (FALSE); return (TRUE); }
YAP_Bool point_to_geometry (YAP_Term term, geometry_t *geometry) { sequence_t sequence; YAP_Float x, y; YAP_Functor functor; const char * functor_name; unsigned int arity; assert (geometry != NULL); if (YAP_IsApplTerm (term) == FALSE) return (FALSE); functor = YAP_FunctorOfTerm (term); functor_name = YAP_AtomName (YAP_NameOfFunctor (functor)); arity = YAP_ArityOfFunctor (functor); if ((strcmp (functor_name, NAME_POINT) != 0) || (arity != 2)) return (FALSE); if ((Yap_IsNumberTerm (YAP_ArgOfTerm (1, term), &x) == FALSE) || (Yap_IsNumberTerm (YAP_ArgOfTerm (2, term), &y) == FALSE)) return (FALSE); sequence = GEOSCoordSeq_create (1, 2); if (sequence == NULL) return (FALSE); if ((GEOSCoordSeq_setX (sequence, 0, x) == 0) || (GEOSCoordSeq_setY (sequence, 0, y) == 0)) { GEOSCoordSeq_destroy (sequence); return (FALSE); } *geometry = GEOSGeom_createPoint (sequence); if (*geometry == NULL) return (FALSE); return (TRUE); }
static lbfgsfloatval_t evaluate( void *instance, const lbfgsfloatval_t *x, lbfgsfloatval_t *g_tmp, const int n, const lbfgsfloatval_t step ) { YAP_Term call; YAP_Term a1; YAP_Bool result; YAP_Int s1; YAP_Term t[3]; t[0] = YAP_MkVarTerm(); t[1] = YAP_MkIntTerm(n); t[2] = YAP_MkFloatTerm(step); call = YAP_MkApplTerm(fcall3, 3, t); g=g_tmp; s1 = YAP_InitSlot(call); optimizer_status=OPTIMIZER_STATUS_CB_EVAL; result=YAP_CallProlog(call); optimizer_status=OPTIMIZER_STATUS_RUNNING; if (result==FALSE) { printf("ERROR: Calling the evaluate call back function in YAP.\n"); // Goal did not succeed return FALSE; } call = YAP_GetFromSlot( s1 ); a1 = YAP_ArgOfTerm(1,call); if (YAP_IsFloatTerm(a1)) { YAP_ShutdownGoal( TRUE ); return (lbfgsfloatval_t) YAP_FloatOfTerm(a1); } else if (YAP_IsIntTerm(a1)) { YAP_ShutdownGoal( TRUE ); return (lbfgsfloatval_t) YAP_IntOfTerm(a1); } YAP_ShutdownGoal( TRUE ); fprintf(stderr, "ERROR: The evaluate call back function did not return a number as first argument.\n"); return 0; }
static int load_facts( void ) { int32_t nrows = YAP_IntOfTerm(YAP_ARG1); int32_t ncols = YAP_IntOfTerm(YAP_ARG2), i = 0; YAP_Term t3 = YAP_ARG3; int32_t *mat = (int32_t *)malloc(sizeof(int32_t)*nrows*ncols); int32_t pname = YAP_AtomToInt(YAP_NameOfFunctor(YAP_FunctorOfTerm(YAP_HeadOfTerm(t3)))); predicate *pred; while(YAP_IsPairTerm(t3)) { int32_t j = 0; YAP_Term th = YAP_HeadOfTerm(t3); for (j = 0; j < ncols; j++) { YAP_Term ta = YAP_ArgOfTerm(j+1, th); if (YAP_IsAtomTerm(ta)) { mat[i*ncols+j] = YAP_AtomToInt(YAP_AtomOfTerm(ta)); } else { mat[i*ncols+j] = YAP_IntOfTerm(ta); } } t3 = YAP_TailOfTerm( t3 ); i++; } if (YAP_IsVarTerm( YAP_ARG4)) { // new pred = (predicate *)malloc(sizeof(predicate)); } else { pred = (predicate *)YAP_IntOfTerm(YAP_ARG4); if (pred->address_host_table) free( pred->address_host_table ); } pred->name = pname; pred->num_rows = nrows; pred->num_columns = ncols; pred->is_fact = TRUE; pred->address_host_table = mat; Cuda_NewFacts(pred); if (YAP_IsVarTerm( YAP_ARG4)) { return YAP_Unify(YAP_ARG4, YAP_MkIntTerm((YAP_Int)pred)); } else { return TRUE; } }
/*ARGS ARE AVAILABLE*/ void *RtreeUdiSearch (control_t *control) { rect_t r; int i; struct ClauseList clauselist; struct CallbackM cm; callback_m_t c; YAP_Term Constraints; /*RTreePrint ((*control)[0].tree);*/ for (i = 0; i < NARGS && (*control)[i].arg != 0 ; i++) if (YAP_IsAttVar(YAP_A((*control)[i].arg))) { /*get the constraits rect*/ Constraints = YAP_AttsOfVar(YAP_A((*control)[i].arg)); /* Yap_DebugPlWrite(Constraints); */ r = RectOfTerm(YAP_ArgOfTerm(2,Constraints)); c = &cm; c->cl = Yap_ClauseListInit(&clauselist); c->pred = (*control)[i].pred; if (!c->cl) return NULL; /*? or fail*/ RTreeSearch((*control)[i].tree, r, callback, c); Yap_ClauseListClose(c->cl); if (Yap_ClauseListCount(c->cl) == 0) { Yap_ClauseListDestroy(c->cl); return Yap_FAILCODE(); } if (Yap_ClauseListCount(c->cl) == 1) { return Yap_ClauseListToClause(c->cl); } return Yap_ClauseListCode(c->cl); } return NULL; /*YAP FALLBACK*/ }
control_t *RtreeUdiInsert (Term term,control_t *control,void *clausule) { int i; rect_t r; assert(control); for (i = 0; i < NARGS && (*control)[i].arg != 0 ; i++) { r = RectOfTerm(YAP_ArgOfTerm((*control)[i].arg,term)); if (!(*control)[i].tree) (*control)[i].tree = RTreeNew(); RTreeInsert(&(*control)[i].tree,r,clausule); } /* printf("insert %p\n", clausule); */ return (control); }
control_t *RtreeUdiInit (Term spec, void * pred, int arity){ control_t *control; YAP_Term arg; int i, c; /* YAP_Term mod; */ /* spec = Yap_StripModule(spec, &mod); */ if (! YAP_IsApplTerm(spec)) return (NULL); control = (control_t *) malloc (sizeof(*control)); assert(control); memset((void *) control,0, sizeof(*control)); c = 0; for (i = 1; i <= arity; i ++) { arg = YAP_ArgOfTerm(i,spec); if (YAP_IsAtomTerm(arg) && strcmp("+",YAP_AtomName(YAP_AtomOfTerm(arg))) == 0) { (*control)[c].pred = pred; (*control)[c++].arg = i; } } /* for (i = 0; i < NARGS; i++) printf("%d,%p\t",(*control)[i].arg,(*control)[i].tree); printf("\n"); */ return control; }
static int load_rule( void ) { // maximum of 2K symbols per rule, should be enough for ILP int32_t vec[2048], *ptr = vec, *nvec, neg[2048]; // qK different variables; YAP_Term vars[1024]; int32_t nvars = 0, x; int32_t ngoals = YAP_IntOfTerm(YAP_ARG1); /* gives the number of goals */ int32_t ncols = YAP_IntOfTerm(YAP_ARG2); YAP_Term t3 = YAP_ARG3; YAP_Atom name = YAP_NameOfFunctor(YAP_FunctorOfTerm(YAP_HeadOfTerm(t3))); int32_t pname = YAP_AtomToInt(name); const char *strname = YAP_AtomName(name); predicate *pred; int32_t cont = 0; memset(neg, 0x0, 2048 * sizeof(int32_t)); while(YAP_IsPairTerm(t3)) { int32_t j = 0, m; YAP_Term th = YAP_HeadOfTerm(t3); YAP_Functor f = YAP_FunctorOfTerm( th ); int32_t n = YAP_ArityOfFunctor( f ); YAP_Atom at = YAP_NameOfFunctor( f ); if (at == AtomEq) *ptr++ = SBG_EQ; else if (at == AtomGt) *ptr++ = SBG_GT; else if (at == AtomLt) *ptr++ = SBG_LT; else if (at == AtomGe) *ptr++ = SBG_GE; else if (at == AtomLe) *ptr++ = SBG_LE; else if (at == AtomDf) *ptr++ = SBG_DF; else if (at == AtomNt) { neg[cont] = 1; cont++; } else { *ptr++ = YAP_AtomToInt( at ); cont++; } for (j = 0; j < n; j++) { YAP_Term ta = YAP_ArgOfTerm(j+1, th); if (YAP_IsVarTerm(ta)) { int32_t k; for (k = 0; k< nvars; k++) { if (vars[k] == ta) { *ptr++ = k+1; break; } } if (k == nvars) { vars[k] = ta; *ptr++ = k+1; nvars++; } } else if (YAP_IsAtomTerm(ta)) { *ptr++ = -YAP_AtomToInt(YAP_AtomOfTerm(ta)); } else if (YAP_IsApplTerm(ta)) { f = YAP_FunctorOfTerm( ta ); at = YAP_NameOfFunctor( f ); m = YAP_ArityOfFunctor( f ); *ptr++ = YAP_AtomToInt( at ); for (x = 0; x < m; x++) { YAP_Term ta2 = YAP_ArgOfTerm(x+1, ta); if (YAP_IsVarTerm(ta2)) { int32_t k; for (k = 0; k < nvars; k++) { if (vars[k] == ta2) { *ptr++ = k+1; break; } } if (k == nvars) { vars[k] = ta2; *ptr++ = k+1; nvars++; } } else if (YAP_IsAtomTerm(ta2)) { *ptr++ = -YAP_AtomToInt(YAP_AtomOfTerm(ta)); } else { *ptr++ = -YAP_IntOfTerm(ta); } } } else { *ptr++ = -YAP_IntOfTerm(ta); } } *ptr++ = 0; t3 = YAP_TailOfTerm( t3 ); } if (YAP_IsVarTerm( YAP_ARG4)) { // new pred = (predicate *)malloc(sizeof(predicate)); } else { pred = (predicate *)YAP_IntOfTerm(YAP_ARG4); if (pred->address_host_table) free( pred->address_host_table ); } pred->name = pname; pred->num_rows = ngoals; pred->num_columns = ncols; pred->is_fact = FALSE; x = (strlen(strname) + 1) * sizeof(char); pred->predname = (char *)malloc(x); memcpy(pred->predname, strname, x); nvec = (int32_t *)malloc(sizeof(int32_t)*(ptr-vec)); memcpy(nvec, vec, sizeof(int32_t)*(ptr-vec)); pred->address_host_table = nvec; pred->negatives = (int32_t *)malloc(sizeof(int32_t) * cont); memcpy(pred->negatives, neg, sizeof(int32_t) * cont); Cuda_NewRule( pred ); return YAP_Unify(YAP_ARG4, YAP_MkIntTerm((YAP_Int)pred)); }