This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
yap-6.3/C/udi.c

296 lines
7.0 KiB
C
Raw Normal View History

2012-12-17 20:23:03 +00:00
#include <stdio.h>
#include "Yap.h"
2012-12-17 20:23:03 +00:00
#include "YapInterface.h"
#include "clause.h"
2012-12-18 18:26:59 +00:00
#include "clause_list.h"
#include "udi.h"
2012-12-17 20:23:03 +00:00
#include "utarray.h"
/* to keep a vector of udi indexers */
UT_icd udicb_icd = {sizeof(UdiControlBlock), NULL, NULL, NULL};
UT_array *indexing_structures;
2012-12-17 20:23:03 +00:00
/*
* New user indexed predicate:
* first argument is the decl term.
* second argument is the init call with the structure
*/
void
Yap_UdiRegister(UdiControlBlock cb){
/*TODO: check structure integrity and duplicates */
utarray_push_back(indexing_structures, &cb);
}
2009-02-20 15:52:17 +00:00
2012-12-17 20:23:03 +00:00
struct udi_p_args {
void *idxstr; //user indexing structure
UdiControlBlock control; //user indexing structure functions
};
2009-02-20 11:42:48 +00:00
2012-12-17 20:23:03 +00:00
/* a pointer utarray list
* This is a hack, becouse I do no know the real type of clauses*/
UT_icd ptr_icd = {sizeof(void *), NULL, NULL, NULL };
#define UDI_MI 10
/******
2012-12-17 20:23:03 +00:00
All the info we need to enter user indexed code:
right now, this is just a linked list....
******/
typedef struct udi_info
{
2012-12-17 20:23:03 +00:00
PredEntry *p; //predicate (need to identify asserts)
UT_array *clauselist; //clause list used on returns
struct udi_p_args args[UDI_MI]; //indexed args only the first UDI_MI
struct udi_info *next;
} *UdiInfo;
2012-12-17 20:23:03 +00:00
int Yap_udi_args_init(Term spec, int arity, UdiInfo blk);
/******
we now have one extra user indexed predicate. We assume these
are few, so we can do with a linked list.
******/
2012-12-17 20:23:03 +00:00
//static int
//add_udi_block(PredEntry *p, void *info, UdiControlBlock cmd)
//{
// UdiInfo blk = (UdiInfo) Yap_AllocCodeSpace(sizeof(struct udi_info));
// if (!blk)
// return FALSE;
// blk->next = UdiControlBlocks;
// UdiControlBlocks = blk;
// blk->p = p;
// blk->functions = cmd;
// blk->cb = info;
// return TRUE;
//}
2012-12-17 20:23:03 +00:00
/*
* New user indexed predicate:
* the first argument is the term.
*/
2012-12-20 17:13:30 +00:00
static int
p_new_udi( USES_REGS1 )
{
2012-12-17 20:23:03 +00:00
Term spec = Deref(ARG1);
PredEntry *p;
2012-12-17 20:23:03 +00:00
// UdiControlBlock cmd;
// Atom udi_t;
UdiInfo blk;
int info;
2012-12-18 18:26:59 +00:00
//fprintf(stderr,"new pred\n");
/* get the predicate from the spec, copied from cdmgr.c */
if (IsVarTerm(spec)) {
2012-12-17 20:23:03 +00:00
Yap_Error(INSTANTIATION_ERROR,spec,"new user index/1");
return FALSE;
} else if (!IsApplTerm(spec)) {
2012-12-17 20:23:03 +00:00
Yap_Error(TYPE_ERROR_COMPOUND,spec,"new user index/1");
return FALSE;
} else {
2012-12-17 20:23:03 +00:00
Functor fun = FunctorOfTerm(spec);
Term tmod = CurrentModule;
while (fun == FunctorModule) {
tmod = ArgOfTerm(1,spec);
if (IsVarTerm(tmod) ) {
Yap_Error(INSTANTIATION_ERROR, spec, "new user index/1");
return FALSE;
}
if (!IsAtomTerm(tmod) ) {
Yap_Error(TYPE_ERROR_ATOM, spec, "new user index/1");
return FALSE;
}
spec = ArgOfTerm(2, spec);
fun = FunctorOfTerm(spec);
}
p = RepPredProp(PredPropByFunc(fun, tmod));
}
2009-02-20 11:42:48 +00:00
if (!p)
2012-12-17 20:23:03 +00:00
return FALSE;
/* boring, boring, boring! */
2012-12-17 20:23:03 +00:00
if ((p->PredFlags
& (DynamicPredFlag|LogUpdatePredFlag|UserCPredFlag|CArgsPredFlag|NumberDBPredFlag|AtomDBPredFlag|TestPredFlag|AsmPredFlag|CPredFlag|BinaryPredFlag))
|| (p->ModuleOfPred == PROLOG_MODULE)) {
Yap_Error(PERMISSION_ERROR_MODIFY_STATIC_PROCEDURE, spec, "udi/2");
return FALSE;
}
if (p->PredFlags & (DynamicPredFlag|LogUpdatePredFlag|TabledPredFlag)) {
2012-12-17 20:23:03 +00:00
Yap_Error(PERMISSION_ERROR_ACCESS_PRIVATE_PROCEDURE, spec, "udi/2");
return FALSE;
}
2012-12-17 20:23:03 +00:00
/* TODO: remove AtomRTree from atom list */
/* this is the real work */
2012-12-17 20:23:03 +00:00
blk = (UdiInfo) Yap_AllocCodeSpace(sizeof(struct udi_info));
2012-12-18 18:26:59 +00:00
memset((void *) blk,0, sizeof(struct udi_info));
2012-12-17 20:23:03 +00:00
if (!blk) {
Yap_Error(OUT_OF_HEAP_ERROR, spec, "new user index/1");
return FALSE;
}
blk->next = UdiControlBlocks;
2012-12-18 18:26:59 +00:00
utarray_new(blk->clauselist, &ptr_icd);
2012-12-17 20:23:03 +00:00
blk->p = p;
2012-12-18 18:26:59 +00:00
UdiControlBlocks = blk;
//fprintf(stderr,"PRED %p\n",p);
2012-12-17 20:23:03 +00:00
info = Yap_udi_args_init(spec, p->ArityOfPE, blk);
if (!info)
return FALSE;
2012-12-17 20:23:03 +00:00
p->PredFlags |= UDIPredFlag;
return TRUE;
}
2012-12-17 20:23:03 +00:00
/*
* Here we initialize the arguments indexing
*/
int
Yap_udi_args_init(Term spec, int arity, UdiInfo blk)
{
int i;
Term arg;
Atom idxtype;
UdiControlBlock *p;
2012-12-18 18:26:59 +00:00
//fprintf(stderr,"udi init\n");
2012-12-17 20:23:03 +00:00
for (i = 1; i <= arity && i <= UDI_MI ; i++) {
arg = ArgOfTerm(i,spec);
if (IsAtomTerm(arg)) {
idxtype = AtomOfTerm(arg);
2012-12-18 18:26:59 +00:00
// fprintf(stderr,"%p-%s %p-%s\n",idxtype, YAP_AtomName(idxtype),
// AtomMinus, YAP_AtomName(AtomMinus));
2012-12-17 20:23:03 +00:00
if (idxtype == AtomMinus)
continue;
p = NULL;
while ((p = (UdiControlBlock *) utarray_next(indexing_structures, p))) {
2012-12-18 18:26:59 +00:00
//fprintf(stderr,"cb: %p %p-%s\n", *p, (*p)->decl, YAP_AtomName((*p)->decl));
2012-12-17 20:23:03 +00:00
if (idxtype == (*p)->decl){
blk->args[i-1].control = *p;
2012-12-20 17:13:30 +00:00
blk->args[i-1].idxstr = (*p)->init(spec, i, arity);
2012-12-17 20:23:03 +00:00
}
}
if (blk->args[i-1].control == NULL){ /* not "-" and not found*/
fprintf(stderr, "Invalid Spec (%s)\n", AtomName(idxtype));
return FALSE;
}
}
}
return TRUE;
}
/* just pass info to user, called from cdmgr.c */
int
Yap_new_udi_clause(PredEntry *p, yamop *cl, Term t)
{
2012-12-18 18:26:59 +00:00
int i;
2012-12-17 20:23:03 +00:00
/* find our structure*/
struct udi_info *info = UdiControlBlocks;
while (info->p != p && info)
info = info->next;
if (!info)
return FALSE;
/* do the actual insertion */
2012-12-18 18:26:59 +00:00
/*insert into clauselist*/
utarray_push_back(info->clauselist,(void *) cl);
for (i = 0; i < UDI_MI ; i++) {
if (info->args[i].control != NULL){
2012-12-20 17:13:30 +00:00
//fprintf(stderr,"call insert\n");
info->args[i].idxstr = info->args[i].control->insert(info->args[i].idxstr, t, i + 1, (void *)cl);
2012-12-18 18:26:59 +00:00
// info->cb = info->functions->insert(t, info->cb, (void *)cl);
}
}
2012-12-17 20:23:03 +00:00
return TRUE;
}
2012-12-18 18:26:59 +00:00
struct CallbackM
{
clause_list_t cl;
void * pred;
};
typedef struct CallbackM * callback_m_t;
static int callback(void *key, void *data, void *arg)
{
callback_m_t x;
x = (callback_m_t) arg;
return Yap_ClauseListExtend(x->cl,data,x->pred);
}
/* index, called from absmi.c */
yamop *
Yap_udi_search(PredEntry *p)
{
2012-12-18 18:26:59 +00:00
int i, r = -1;
struct ClauseList clauselist;
struct CallbackM cm;
callback_m_t c;
/* find our structure*/
2012-12-17 20:23:03 +00:00
struct udi_info *info = UdiControlBlocks;
while (info->p != p && info)
info = info->next;
if (!info)
return NULL;
2012-12-18 18:26:59 +00:00
/*TODO: handle intersection*/
c = &cm;
c->cl = Yap_ClauseListInit(&clauselist);
c->pred = p;
for (i = 0; i < UDI_MI ; i++) {
if (info->args[i].control != NULL){
// fprintf(stderr,"call search %d %p\n", i, (void *)c);
2012-12-20 17:13:30 +00:00
r = info->args[i].control->search(info->args[i].idxstr, i + 1, callback, c);
2012-12-18 18:26:59 +00:00
/*info->functions->search(info->cb);*/
}
}
Yap_ClauseListClose(c->cl);
if (r >= 0) {
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);
}
else
Yap_ClauseListDestroy(c->cl);
return NULL;
}
2012-06-21 16:47:19 +01:00
/* index, called from absmi.c */
void
Yap_udi_abolish(PredEntry *p)
{
2012-12-17 20:23:03 +00:00
/* tell the predicate destroy */
2012-06-21 16:47:19 +01:00
}
void
Yap_udi_init(void)
{
UdiControlBlocks = NULL;
2012-12-17 20:23:03 +00:00
/*init indexing structures array*/
utarray_new(indexing_structures, &udicb_icd);
Yap_InitCPred("$udi_init", 1, p_new_udi, 0);
/* TODO: decide if yap.udi should be loaded automaticaly in init.yap */
}
2009-02-20 15:52:17 +00:00