YAP_Bool term_to_geometry (YAP_Term term, geometry_t *geometry) { YAP_Functor functor; const char * functor_name; unsigned int n, arity, size; assert (geometry != NULL); *geometry = NULL; if (YAP_IsApplTerm (term) == FALSE) return (FALSE); functor = YAP_FunctorOfTerm (term); functor_name = YAP_AtomName (YAP_NameOfFunctor (functor)); arity = YAP_ArityOfFunctor (functor); size = sizeof (translate) / sizeof (struct type_name_arity); for (n = 0; n < size; n ++) if (strcmp (translate[n].name, functor_name) == 0) { if (translate[n].arity == arity) return (translate[n].procedure_import (term, geometry)); break; } return (FALSE); }
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; } }
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)); }
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 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)); }