static foreign_t pl_order_table_mapping(term_t handle, term_t from, term_t to, control_t ctrl) { OrdTable t; int f; if ( !get_order_table(handle, &t) ) return FALSE; if ( PL_get_integer(from, &f) && f >= 0 && f <= 255 ) return unify_mapped_code(to, ORD(t, f)); if ( PL_is_variable(from) ) { switch(PL_foreign_control(ctrl)) { case PL_FIRST_CALL: f = 0; break; case PL_REDO: f = (int)PL_foreign_context(ctrl); break; case PL_PRUNED: return TRUE; } while( f <= 255 && !unify_mapped_code(to, ORD(t, f)) ) f++; if ( f <= 255 ) { if ( !PL_unify_integer(from, f) ) return FALSE; PL_retry(f+1); } return FALSE; } return FALSE; }
foreign_t pl_dictionary_unify(term_t QueryCharList,control_t handle) { trieQuery* ctxt; char* queryString; // assuming that no word is longer than 100 chars const char* queryResult; switch(PL_foreign_control(handle)) { case PL_FIRST_CALL: queryString = (char*)malloc(sizeof(char)*1000); getQueryString(QueryCharList,queryString); init_new_dictionary_query(ctxt,queryString); free(queryString); if(!ctxt || !ctxt->next_match(queryResult)) { if(ctxt) clean_up_dictionary_query(ctxt); PL_fail; } else { PL_unify_list_chars(QueryCharList,queryResult); PL_retry_address(ctxt); } case PL_REDO: ctxt=(trieQuery*)PL_foreign_context_address(handle); if(!ctxt->next_match(queryResult)) { clean_up_dictionary_query(ctxt); PL_fail; } else { PL_unify_list_chars(QueryCharList,queryResult); PL_retry_address(ctxt); } case PL_CUTTED: ctxt==(trieQuery*)PL_foreign_context_address(handle); clean_up_dictionary_query(ctxt); PL_succeed; } }
foreign_t pl_dictionary_unify(term_t QueryCharList,control_t handle) { trieQuery* ctxt; char queryString[100]; // assuming that no word is longer than 100 chars char* queryResult; switch(PL_foreign_control(handle)) { case PL_FIRST_CALL: getQueryString(QueryCharList,queryString); init_new_dictionary_query(ctxt,queryString); if(!ctxt->next(queryResult)) { clean_up_dictionary_query(ctxt); PL_fail; } else { PL_unify_list_chars(QueryCharList,queryResult); PL_retry_address(ctxt); } case PL_REDO: ctxt=PL_foreign_context_address(handle); if(!ctxt->next(queryResult)) { clean_up_dictionary_query(ctxt); PL_fail; } else { PL_unify_list_chars(QueryCharList,queryResult); PL_retry_address(ctxt); } case PL_CUTTED: ctxt=PL_foreign_context_address(handle); clean_up_dictionary_query(ctxt); PL_succeed; } }
foreign_t fcgi_param(term_t name, term_t value, control_t h) { fcgi_context *ctxt; char **env, **cgi_environ; char *s, *v, *sep; ctxt = pthread_getspecific(key); if ( FCGX_IsCGI() ) { cgi_environ = environ; } else { cgi_environ = ctxt->env; } if ( !PL_is_variable(name) ) { if ( !PL_get_atom_chars(name, &s) ) { return PL_type_error("atom", name); } v = FCGX_GetParam(s, cgi_environ); if ( !v ) { return FALSE; } return PL_unify_chars(value, PL_ATOM|REP_UTF8, -1, v); } switch ( PL_foreign_control(h) ) { case PL_FIRST_CALL: { env = cgi_environ; break; } case PL_REDO: { env = PL_foreign_context_address(h); break; } case PL_PRUNED: default: { return TRUE; } } for ( ; *env; env++ ) { s = strdup(*env); sep = index(s, '='); sep[0] = '\0'; if ( !PL_unify_chars(name, PL_ATOM|REP_UTF8, -1, s) ) { free(s); return FALSE; } if ( !PL_unify_chars(value, PL_ATOM|REP_UTF8, -1, sep+1) ) { free(s); return FALSE; } free(s); PL_retry_address(env+1); } return FALSE; }
/******************** * pl_fact_exists ********************/ static foreign_t pl_fact_exists(term_t pl_name, term_t pl_fields, term_t pl_list, control_t handle) { context_t *ctx; char *name, factname[64]; fid_t frame; term_t pl_values; OhmFact *f; switch (PL_foreign_control(handle)) { case PL_FIRST_CALL: if (!PL_is_list(pl_fields) || /*!PL_is_list(pl_list) ||*/ !PL_get_chars(pl_name, &name, CVT_ALL)) PL_fail; strncpy(factname, name, sizeof(factname)); factname[sizeof(factname)-1] = '\0'; if ((ctx = malloc(sizeof(*ctx))) == NULL) PL_fail; memset(ctx, 0, sizeof(*ctx)); if (get_field_names(ctx, pl_fields) != 0) { free(ctx); PL_fail; } ctx->store = ohm_fact_store_get_fact_store(); ctx->facts = ohm_fact_store_get_facts_by_name(ctx->store, factname); break; case PL_REDO: ctx = PL_foreign_context_address(handle); break; case PL_CUTTED: ctx = PL_foreign_context_address(handle); goto nomore; default: PL_fail; } /* XXX TODO: shouldn't we discard the frame here instead of closing them */ frame = PL_open_foreign_frame(); while (ctx->facts != NULL) { f = (OhmFact *)ctx->facts->data; ctx->facts = g_slist_next(ctx->facts); if (!fact_values(ctx, f, &pl_values) && PL_unify(pl_list, pl_values)) { PL_close_foreign_frame(frame); /* PL_discard_foreign_frame ??? */ PL_retry_address(ctx); } PL_rewind_foreign_frame(frame); } PL_close_foreign_frame(frame); /* PL_discard_foreign_frame ??? */ nomore: if (ctx->fields) free(ctx->fields); free(ctx); PL_fail; }
static foreign_t pl_clingo_solve(term_t ccontrol, term_t assumptions, term_t Show, term_t Model, control_t h) { int rc = TRUE; solve_state *state = NULL; clingo_symbolic_literal_t *assump_vec = NULL; int control = PL_foreign_control(h); if (control == PL_FIRST_CALL) { size_t alen = 0; if (!(state = malloc(sizeof(*state)))) { rc = PL_resource_error("memory"); goto out; } memset(state, 0, sizeof(*state)); if (!(rc = get_clingo(ccontrol, &state->ctl))) { goto out; } if (PL_skip_list(assumptions, 0, &alen) != PL_LIST) { rc = PL_type_error("list", assumptions); goto out; } term_t tail = PL_copy_term_ref(assumptions); term_t head = PL_new_term_ref(); if (!(assump_vec = malloc(sizeof(*assump_vec) * alen))) { rc = PL_resource_error("memory"); goto out; } memset(assump_vec, 0, sizeof(*assump_vec) * alen); for (size_t i = 0; PL_get_list(tail, head, tail); i++) { if (!(rc = clingo_status(get_assumption(head, &assump_vec[i])))) { goto out; } } if (!(rc = clingo_status(clingo_control_solve_iteratively( state->ctl->control, assump_vec, alen, &state->it)))) { goto out; } } else { state = PL_foreign_context_address(h); } while (control != PL_PRUNED) { clingo_model_t *model; if (!(rc = clingo_status( clingo_solve_iteratively_next(state->it, &model)))) { goto out; } if (model) { int show; if (!(rc = get_show_map(Show, &show))) { goto out; } if (!(rc = unify_model(Model, show, model))) { if (PL_exception(0)) { goto out; } } else { PL_retry_address(state); state = NULL; break; } } else { rc = FALSE; break; } } out: if (assump_vec) { free(assump_vec); } if (state) { if (state->it) { clingo_solve_iteratively_close(state->it); } free(state); } return rc; }