Index: libabac/abac_pl_yap.c
===================================================================
--- libabac/abac_pl_yap.c	(revision 4f40c3e6cf53f7c95a2a544455682c85f601e846)
+++ libabac/abac_pl_yap.c	(revision 2e9455f11fea0205b8b4b1306b5d0804cda72e10)
@@ -12,16 +12,15 @@
 #include <Yap/YapInterface.h>
 
-#include "abac_internal.h"
-
-#include "abac_pl_yap.h"
+#include "abac_defines.h"
 #include "abac_util.h"
+#include "abac_stack.h"
+#include "abac_context.h"
+#include "abac_list.h"
+#include "abac_pl_gen.h"
+#include "abac_pl_yy.h"
+#include "abac_aspect.h"
+#include "abac_m64.h"
 
 #include "uthash.h"
-
-extern abac_credential_t *abac_credential_dup(abac_credential_t *cred);
-extern abac_list_t *abac_credential_clauses(abac_credential_t *);
-extern char *abac_id_clause(abac_id_credential_t *);
-extern abac_aspect_t *abac_yy_get_rule_tail_aspect();
-extern abac_aspect_t *abac_yy_get_rule_head_aspect();
 
 static int debug=0;
@@ -63,7 +62,8 @@
 static int _queryname_cnt=6;
 
-
-// pl -- place holder for now 
+/* this is the prolog query structure */
+
 struct _abac_pl_t {
+    abac_context_t *ctxt;
    /* a_term <- prin */
     char *whom;
@@ -85,4 +85,7 @@
 };
 
+/* the arg term for extracting ruleset is the 4th */
+#define RULES_ARG_OF_TERM  4
+
 /***********************************************************/
 /* default to Yap2 */
@@ -90,5 +93,5 @@
 {
 #ifdef  YAP_VERSION 
-    if(YAP_VERSION == "6.3.2") 
+    if(strncmp(YAP_VERSION,"6.3.",4)==0)
         return 1;
 #endif
@@ -108,4 +111,22 @@
 
 /***********************************************************/
+/* retract( (clause) ) */
+static int _remove_clause(char *str)
+{
+    int rc=0;
+    char *tmp=NULL;
+    YAP_Term eterm;
+    asprintf(&tmp,"retract( (%s) ).", str);
+    YAP_Term goalArgs=YAP_ReadBuffer(tmp, &eterm);
+    int res=YAP_RunGoal(goalArgs);
+    if (!res) { 
+        printf("error: result of removing clause (%s)\n", tmp);
+        rc=1;
+        } else {
+            DEBUG_PRINTF("okay: result of removing clause (%s)\n", tmp);
+    }
+    free(tmp);
+    return rc;
+}
 
 static int _insert_clause(char *str)
@@ -122,8 +143,28 @@
 }
 
+/* assert( (clause) ) */
+static int _assert_clause(char *str)
+{
+    int rc=0;
+    YAP_Term eterm;
+    char *tmp=NULL;
+    asprintf(&tmp,"assert( (%s) )", str);
+    YAP_Term goalArgs=YAP_ReadBuffer(tmp, &eterm);
+    int res=YAP_RunGoal(goalArgs);
+    if (!res) { /* something is wrong */
+        printf("error: result of assert clause (%d)(%s)\n", res, tmp);
+        rc=1;
+        } else {
+            DEBUG_PRINTF("okay: result of assert clause (%d)(%s)\n", res, tmp);
+    }
+    free(tmp);
+    return rc;
+}
+
+
 static int _insert_cred_clause(char *cstr)
 {
     int ret=ABAC_CERT_SUCCESS;
-    int rc=_insert_clause(cstr);
+    int rc=_assert_clause(cstr);
     if (rc)
         return ABAC_CERT_BAD_YAP;
@@ -133,5 +174,5 @@
 void show_yap_db(const char *msg)
 {
-    char lstr[]="listing";
+    char lstr[]="listing.";
     YAP_Term eterm; /* error term */
 
@@ -150,5 +191,6 @@
  * Include some utility routines
  */
-abac_pl_t *abac_pl_utility(void) {
+abac_pl_t *abac_pl_utility(void)
+{
 /*
 append([],L,L).
@@ -169,9 +211,6 @@
 }
 
-/**
- * Create a new yap structure.
- */
-abac_pl_t *abac_pl_new(void) {
-
+abac_pl_t *abac_pl_init()
+{
     YAP_Term eterm;
 
@@ -181,14 +220,25 @@
     int rc=YAP_RunGoalOnce(YAP_ReadBuffer("source",&eterm));
     if(rc) {
-        if(debug) fprintf(stderr, "calling source..\n");
+        DEBUG_PRINTF( "calling source..\n");
         } else {
-            if(debug) fprintf(stderr,"calling source failed..\n");
+            DEBUG_PRINTF("PANIC: calling source failed..\n");
             YAP_Exit(1);
     }
-
     abac_pl_utility();
+}
+
+/* nothing in deinit right now */
+void abac_pl_deinit()
+{
+}
   
+/**
+ * Create a new yap query structure.
+ */
+abac_pl_t *abac_pl_new(abac_context_t *ctxt)
+{
     abac_pl_t *pl = abac_xmalloc(sizeof(abac_pl_t));
 
+    pl->ctxt=ctxt;
     pl->who=NULL;
     pl->whom = NULL;
@@ -328,8 +378,7 @@
  * don't try to free it after this call.
  */
-int abac_pl_add_credential(abac_pl_t *pl, abac_credential_t *cred)
+int abac_pl_add_credential(abac_list_t *clauses)
 {
     int rc=0;
-    abac_list_t *clauses=abac_credential_clauses(cred);
     if (clauses != NULL) {
         char *cur;
@@ -344,7 +393,26 @@
 }
 
-int abac_pl_add_type_credential(abac_pl_t *pl, abac_id_credential_t *id_cert)
-{
-    char *clause=abac_id_clause(id_cert);
+/**
+ * Delete a list of credentials from db,
+ */
+int abac_pl_remove_credential(abac_list_t *clauses)
+{
+    int rc=0;
+    if (clauses != NULL) {
+        char *cur;
+        abac_list_foreach(clauses, cur,
+            if(cur) {
+                if(debug) fprintf(stderr,"deleting =>%s\n",cur);
+                rc=_remove_clause(cur);
+            }
+        );
+    }
+    return rc;
+}
+
+
+
+int abac_pl_add_type_credential(char *clause)
+{
     if (clause != NULL) {
         int rc=_insert_cred_clause(clause);
@@ -368,61 +436,91 @@
 }
 
+/* chop a 'string' or bare-string out of istring */
+static char* _chop(char **istring)
+{
+   char *ret=NULL;
+   char *cptr=*istring;
+   char *nptr=cptr; /* next */
+   int yesQ=0;
+
+   if(strlen(*istring)==0) return ret;
+   if(cptr[0]==']') return ret;
+   if(cptr[0]=='\'') { /* take a step */
+      yesQ=1;
+      cptr++; 
+      nptr=cptr;
+   }
+   while(1) {
+       if(yesQ) { /* look for ',' or ', or '] */
+           nptr=strstr(cptr,"',");
+           if(nptr!=NULL) { 
+              *istring=nptr+2;
+              break;
+           }
+           nptr=strstr(cptr,"']");
+           if(nptr!=NULL) { 
+              *istring=nptr+2;
+              break;
+           }
+           } else { /* yesQ=no */
+              nptr=strstr(cptr,",");
+              if(nptr!=NULL) { 
+                  *istring=nptr+1;
+                  break;
+              }
+              nptr=strstr(cptr,"]");
+              if(nptr!=NULL) { 
+                  *istring=nptr+1;
+                  break;
+              }
+       }
+   }
+   if(nptr==NULL) {
+       fprintf(stderr,"BAD,  format not as expected!!\n");
+       } else {
+          int len=(nptr-cptr);
+          ret=strndup(cptr,len);
+   }
+   return ret;
+}
+
 /* cases, for YAP 6.2.x 
      ['str']
      ['str1','str2']
+     [str1,'str2']
+     ['str1',str2]
      ([] is not possible, and don't care)
 */
-static void _credentials_from_string_2(abac_list_t *credentials,char *slist) 
+static void _credentials_from_string_2(abac_context_t *ctxt,abac_list_t *credentials,char *slist) 
 {
     char *cptr=slist; /* current ptr */
-    char *sptr; /* string ptr */
-    char *ptr;
-    int len=0;
     char *string;
     abac_credential_t *cred=NULL;
     int cnt=0;
-
-    /* find first [' */  
-    ptr=strstr(cptr,"['");
-    if(ptr == NULL) 
-         return;
-    cptr=ptr+2;
-    sptr=cptr;
-    while (1) {
-        /* find next ',' or '] */
-        if(debug) fprintf(stderr,"START with %d\n", strlen(cptr));
-        ptr=strstr(cptr,"','");
-        if(ptr!=NULL) {
-             cptr=ptr+3;
-             len=(ptr-sptr);
-             string=strndup(sptr,len);
-             cred=abac_credential_lookup(string);
-             free(string); 
-             if(cred) {
-                 int i=abac_list_unique_add(credentials, cred);
-                 if(i) cnt++;
-                 } else {
-                 printf("BAD BAD\n");
-             }
-             sptr=cptr;
+    char *ptr=NULL;
+
+/* it looks like the first one could be [abc,'def'] */
+    /* find first [' or [ */  
+    ptr=strstr(cptr,"[");
+    if(ptr==NULL) { 
+        return;
+    }
+
+/* it looks like the last one could be  'abc',efg] in addition to 'abac','efg'] */
+/* it looks like the first one could be [abc,'def'] */
+    cptr++;
+    string=_chop(&cptr); /* first one */
+    while (string!=NULL) {
+        cred=abac_context_credential_lookup(ctxt,string);
+        if(cred) {
+             int i=abac_list_unique_add(credentials, cred);
+             if(i) cnt++;
              } else {
-             ptr=strstr(cptr,"']");
-             if(ptr!=NULL) {
-                 len=(ptr-sptr);
-                 string=strndup(sptr,len);
-                 cred=abac_credential_lookup(string);
-                 free(string);
-                 if(cred) {
-                     int i=abac_list_unique_add(credentials, cred);
-                     if(i) cnt++;
-                     } else {
-                     printf("BAD BAD BAD\n");
-                 }
-                 break;
-             }
+                 printf("BAD -did not find(%s)(%s)\n", abac_decode_string(string),string); 
         }
-    }
-    if(debug)
-        fprintf(stderr,"DEBUG:total %d credentials\n", cnt);
+        free(string); 
+        string=_chop(&cptr);
+    }
+    DEBUG_PRINTF("DEBUG:total %d credentials\n", cnt);
 }
 
@@ -432,5 +530,5 @@
      ([] is not possible, and don't care)
 */
-static void _credentials_from_string_3(abac_list_t *credentials,char *slist)
+static void _credentials_from_string_3(abac_context_t *ctxt,abac_list_t *credentials,char *slist)
 {
     char *cptr=slist; /* current ptr */
@@ -450,5 +548,5 @@
     while (1) {
         /* find next "," or "]" */
-        if(debug) fprintf(stderr,"START with %d\n", strlen(cptr));
+        DEBUG_PRINTF("START with %d\n", strlen(cptr));
         ptr=strstr(cptr,",");
         if(ptr!=NULL) {
@@ -456,5 +554,5 @@
              len=(ptr-sptr);
              string=strndup(sptr,len);
-             cred=abac_credential_lookup(string);
+             cred=abac_context_credential_lookup(ctxt,string);
              free(string); 
              if(cred) {
@@ -462,5 +560,5 @@
                  if(i) cnt++;
                  } else {
-                 printf("BAD BAD\n");
+                   printf("BAD 2 -did not find(%s)\n", abac_decode_string(string)); 
              }
              sptr=cptr;
@@ -470,5 +568,5 @@
                  len=(ptr-sptr);
                  string=strndup(sptr,len);
-                 cred=abac_credential_lookup(string);
+                 cred=abac_context_credential_lookup(ctxt,string);
                  free(string);
                  if(cred) {
@@ -476,5 +574,5 @@
                      if(i) cnt++;
                      } else {
-                     printf("BAD BAD BAD\n");
+                         printf("BAD BAD 2 -did not find(%s)\n", abac_decode_string(string)); 
                  }
                  break;
@@ -482,13 +580,13 @@
         }
     }
-    if(debug)
-        fprintf(stderr,"DEBUG:total %d credentials\n", cnt);
-}
-
-static void _credentials_from_string(abac_list_t *credentials,char *slist) {
-    if(isYAP3())
-         _credentials_from_string_3(credentials,slist);
-    else
-         _credentials_from_string_2(credentials,slist);
+    DEBUG_PRINTF("DEBUG:total %d credentials\n", cnt);
+}
+
+static void _credentials_from_string(abac_context_t *ctxt,abac_list_t *credentials,char *slist) {
+    if(isYAP3()) {
+         _credentials_from_string_3(ctxt,credentials,slist);
+    } else {
+         _credentials_from_string_2(ctxt,credentials,slist);
+    }
 }
 
@@ -504,5 +602,5 @@
        size = (sizeof(char) * (blk+1));
        } else {
-           if(debug) fprintf(stderr,"calling try_again needing extra space!!\n");
+           DEBUG_PRINTF("calling try_again needing extra space!!\n");
            if (sz>=max) {
                size=0;
@@ -525,4 +623,5 @@
 static int _check_buffer(char *tmp)
 {
+    if(tmp==NULL) return 0;
     int len=strlen(tmp); /* just for reference :-( */
     if (len < 3) return 0;
@@ -540,6 +639,5 @@
         }
     }
-    if(debug)
-        fprintf(stderr," _check_buffer, did not find the closing square bracket ..\n");
+    DEBUG_PRINTF(" _check_buffer, did not find the closing square bracket ..\n");
     return 0;
 }
@@ -549,9 +647,9 @@
     abac_list_t *cred_list=abac_list_new();
     YAP_Atom f = YAP_LookupAtom("isMember");
-    YAP_Functor func = YAP_MkFunctor(f, 3);
-    YAP_Term goal=YAP_MkApplTerm(func, 3, arg);
+    YAP_Functor func = YAP_MkFunctor(f, 4);
+    YAP_Term goal=YAP_MkApplTerm(func, 4, arg);
 
     pl->safe_t = YAP_InitSlot(goal);
-if(debug) fprintf(stderr," run_goal, safe_t is %ld\n", pl->safe_t);
+DEBUG_PRINTF(" run_goal, safe_t is %ld\n", pl->safe_t);
     pl->query_rc =YAP_RunGoal(goal);
     goal=YAP_GetFromSlot(pl->safe_t);
@@ -566,13 +664,10 @@
         if(!isYAP3())
             tmp_sz=try_again(0, &tmp);
-        YAP_Term argterm=YAP_ArgOfTerm(3,goal);
+        YAP_Term argterm=YAP_ArgOfTerm(RULES_ARG_OF_TERM,goal);
         while(1) {
-            int rc=1; /* XXX rc does not seems to be right... */
-/* for 6.3 series .. 
-     rc=YAP_WriteBuffer(argterm, tmp, tmp_sz,YAP_WRITE_HANDLE_VARS);
-*/
-            YAP_WriteBuffer(argterm, tmp, tmp_sz,YAP_WRITE_HANDLE_VARS); 
-
-/** it seems that 6.3 gives me some garbage at the back.. */
+            int rc=YAP_WriteBuffer(argterm, tmp, tmp_sz,YAP_WRITE_HANDLE_VARS);
+            if(YAP_VERSION == "6.2.2" || YAP_VERSION == "6.2.3") 
+               rc=1; /* XXX rc does not seems to be right for 6.2.x */
+/** it seems that 6.3 might give me some garbage at the back.. */
             if(rc==1 && _check_buffer(tmp)) {
                 break;
@@ -586,8 +681,8 @@
         }
         /* this is returned as ['string1','string2'] */
-        if(debug) fprintf(stderr,"    query answer : %s(%d)\n", tmp, strlen(tmp));
-        _credentials_from_string(cred_list,tmp); 
+        DEBUG_PRINTF("    query answer : %s(%d)\n", tmp, strlen(tmp));
+        _credentials_from_string(pl->ctxt,cred_list,tmp); 
         if(abac_list_size(cred_list)==0) {
-            fprintf(stderr,"CAN NOT retrieve result properly from YAP!!!\n");
+            fprintf(stderr,"CAN NOT retrieve result properly from YAP(1)!!!\n");
             YAP_Exit(1);
         }
@@ -614,5 +709,5 @@
 }
 
-// isMember(who,aspect(whom,what,whatwith)) 
+// isMember(namespace, who,aspect(whom,what,whatwith)) 
 /* aspect(whom, what, whatwith) */
 YAP_Term _aspect_base(char *whom,char *aspect, char *what, char *whatwith)
@@ -625,5 +720,5 @@
        asprintf(&estring, "%s(%s,%s,%s)",aspect,whom,what,whatwith);
        else asprintf(&estring, "%s(%s,%s)",aspect,whom,what);
-    if(debug) fprintf(stderr,"_aspect_base, doing %s\n",estring);
+    DEBUG_PRINTF("_aspect_base, doing %s\n",estring);
     ret=YAP_ReadBuffer(estring,&eterm);
     free(estring);
@@ -679,55 +774,60 @@
 
 // Whom.What <- ladybug
-// isMember(ladybug, role(W1, W2), R)
-static void _arg_for_what_whom(YAP_Term *arg,char *who, char *whom,
+// isMember(namespace,ladybug, role(W1, W2), R)
+static void _arg_for_what_whom(YAP_Term *arg,char *namespace, char *who, char *whom,
 char *aspect, char*what, char *whatwith, int pcnt)
 {
-    arg[0] = _make_term(who);
+    arg[0] = _make_term(namespace);
+    arg[1] = _make_term(who);
     char *tmp=_make_whatwith_var(whatwith,pcnt); 
-    arg[1] = _aspect_what_whom(who,aspect,what,tmp);
+    arg[2] = _aspect_what_whom(who,aspect,what,tmp);
     if(tmp) free(tmp);
-    arg[2] = YAP_MkVarTerm();
+    arg[3] = YAP_MkVarTerm();
 }
 
 // pAcme.What <- ladybug
-// isMember(ladybug, role(pAcme, W), R)
-static void _arg_for_what(YAP_Term *arg,char *who, char* whom, 
+// isMember(namespace, ladybug, role(pAcme, W), R)
+static void _arg_for_what(YAP_Term *arg,char *namespace,char *who, char* whom, 
 char *aspect, char*what, char*whatwith, int pcnt)
 {
-    arg[0] = _make_term(who);
+    arg[0] = _make_term(namespace);
+    arg[1] = _make_term(who);
     char *tmp=_make_whatwith_var(whatwith,pcnt); 
-    arg[1] = _aspect_what(whom,aspect,what,tmp);
+    arg[2] = _aspect_what(whom,aspect,what,tmp);
     if(tmp) free(tmp);
-    arg[2] = YAP_MkVarTerm();
+    arg[3] = YAP_MkVarTerm();
 }
 
 // W.rocket <- ladybug
-// isMember(ladybug, role(W, buy_rockets), R)
-static void _arg_for_whom(YAP_Term *arg, char *who, char*whom,
+// isMember(namespace, ladybug, role(W, buy_rockets), R)
+static void _arg_for_whom(YAP_Term *arg, char *namespace,char *who, char*whom,
 char *aspect, char *what, char*whatwith)
 {
-    arg[0] = _make_term(who);
-    arg[1] = _aspect_whom(whom,aspect,what,whatwith);
-    arg[2] = YAP_MkVarTerm();
+    arg[0] = _make_term(namespace);
+    arg[1] = _make_term(who);
+    arg[2] = _aspect_whom(whom,aspect,what,whatwith);
+    arg[3] = YAP_MkVarTerm();
 }
 
 // Acme.rocket <- V
-// isMember(V, role(pAcme, buy_rockets), R)
-static int _arg_for_who(YAP_Term *arg, char *who,char *whom, 
+// isMember(namespace, V, role(pAcme, buy_rockets), R)
+static int _arg_for_who(YAP_Term *arg, char* namespace, char *who,char *whom, 
 char *aspect, char* what, char*whatwith)
 {
-    arg[0] = YAP_MkVarTerm();
-    arg[1] = _aspect_fact(whom, aspect, what, whatwith);
-    arg[2] = YAP_MkVarTerm();
+    arg[0] = _make_term(namespace);
+    arg[1] = YAP_MkVarTerm();
+    arg[2] = _aspect_fact(whom, aspect, what, whatwith);
+    arg[3] = YAP_MkVarTerm();
 }
 
 // Acme.rocket <- x 
-// isMember(x, role(pAcme, buy_rockets), R)
-static void _arg_for_fact(YAP_Term *arg,char *who,char *whom, 
+// isMember(namespace,x, role(pAcme, buy_rockets), R)
+static void _arg_for_fact(YAP_Term *arg, char* namespace, char *who,char *whom, 
 char* aspect, char *what, char*whatwith)
 {
-    arg[0] = _make_term(who);
-    arg[1] = _aspect_fact(whom, aspect, what, whatwith);
-    arg[2] = YAP_MkVarTerm();
+    arg[0] = _make_term(namespace);
+    arg[1] = _make_term(who);
+    arg[2] = _aspect_fact(whom, aspect, what, whatwith);
+    arg[3] = YAP_MkVarTerm();
 }
 
@@ -737,4 +837,5 @@
     abac_list_t *ret=abac_list_new();
     pl->is_partial=1;
+    char *namespace=abac_context_namespace(pl->ctxt);
     char *who=pl->who;
     char *whom=pl->whom;
@@ -743,13 +844,13 @@
     char *whatwith=pl->whatwith;
     int pcnt=pl->pcnt;
-    if(debug) fprintf(stderr,"making make_partial_query..\n");
+    DEBUG_PRINTF("making make_partial_query..\n");
 
     switch (pl->query_type) { 
         case e_PL_FACT: /* try what */
             {
-            YAP_Term arg[3];
-            if(debug) fprintf(stderr,"... 1st partial attempt\n");
+            YAP_Term arg[4];
+            DEBUG_PRINTF("... 1st partial attempt\n");
             pl->query_type = e_PL_WHAT;
-            _arg_for_what(arg,who,whom, aspect, what, whatwith, pcnt);
+            _arg_for_what(arg,namespace,who,whom, aspect, what, whatwith, pcnt);
             ret=run_goal(pl,arg);
             if(pcnt==0) pl->query_type= e_PL_WHAT_DFLT;
@@ -759,8 +860,8 @@
         case e_PL_WHAT: /* try what with no params if need to */
             {
-            YAP_Term arg[3];
-            if(debug) fprintf(stderr,"... 2nd partial attempt\n");
+            YAP_Term arg[4];
+            DEBUG_PRINTF("... 2nd partial attempt\n");
             pl->query_type = e_PL_WHAT_DFLT;
-            _arg_for_what(arg,who,whom, aspect, what, whatwith, 0);
+            _arg_for_what(arg,namespace,who,whom, aspect, what, whatwith, 0);
             ret=run_goal(pl,arg);
             if(pl->query_rc) return ret;
@@ -769,8 +870,8 @@
         case e_PL_WHAT_DFLT: /* try who */
             {
-            YAP_Term arg[3];
-            if(debug) fprintf(stderr,"... 2nd half partial attempt\n");
+            YAP_Term arg[4];
+            DEBUG_PRINTF("... 2nd half partial attempt\n");
             pl->query_type = e_PL_WHO;
-            _arg_for_who(arg,who,whom, aspect, what, whatwith);
+            _arg_for_who(arg,namespace,who,whom, aspect, what, whatwith);
             ret=run_goal(pl,arg);
             if(pl->query_rc) return ret;
@@ -779,8 +880,8 @@
         case e_PL_WHO: /* try whom */
             {
-            YAP_Term arg[3];
-            if(debug) fprintf(stderr,"... 3rd partial attempt\n");
+            YAP_Term arg[4];
+            DEBUG_PRINTF("... 3rd partial attempt\n");
             pl->query_type = e_PL_WHOM;
-            _arg_for_whom(arg,who,whom, aspect, what, whatwith);
+            _arg_for_whom(arg,namespace,who,whom, aspect, what, whatwith);
             ret=run_goal(pl,arg);
             if(pl->query_rc) return ret;
@@ -789,8 +890,8 @@
         case e_PL_WHOM: /* try what-whom */
             {
-            YAP_Term arg[3];
-            if(debug) fprintf(stderr,"... 4th partial attempt\n");
+            YAP_Term arg[4];
+            DEBUG_PRINTF("... 4th partial attempt\n");
             pl->query_type = e_PL_WHAT_WHOM;
-            _arg_for_what_whom(arg,who,whom, aspect, what, whatwith,pcnt);
+            _arg_for_what_whom(arg,namespace,who,whom, aspect, what, whatwith,pcnt);
             ret=run_goal(pl,arg);
             if(pl->query_rc) return ret;
@@ -799,5 +900,5 @@
         default :
             {
-            if(debug) fprintf(stderr,"... no more partial query to try\n");
+            DEBUG_PRINTF("... no more partial query to try\n");
             pl->query_type = e_PL_NOMORE;
             }
@@ -817,18 +918,12 @@
 char *whom, char *aspect, char *what, char *whatwith, int pcnt)
 {
-    if(debug) { 
-       fprintf(stderr,"_make_yap_query: the who part(%s)\n", who);
-       fprintf(stderr,"_make_yap_query: whom part(%s)\n", whom);
-       fprintf(stderr,"_make_yap_query: the role/oset (%s)\n", aspect);
-       fprintf(stderr,"_make_yap_query: aspect what (%s)\n", what);
-       fprintf(stderr,"_make_yap_query: pcnt (%d)\n", pcnt);
-       if(whatwith)
-           fprintf(stderr,"_make_yap_query: aspect what part(%s)\n", whatwith);
-    } 
-    if(debug) fprintf(stderr,"making a yap query..\n");
+    DEBUG_PRINTF("_make_yap_query: \n who(%s)\n whom(%s)\n role/oset(%s)\n aspect what(%s)\n pcnt(%d)\n",
+                 who, whom, aspect, what, pcnt);
+    if(whatwith) { DEBUG_PRINTF(" aspect what(%s)\n", whatwith); }
 
 /* run query on fact goal */
-    YAP_Term arg[3];
-    _arg_for_fact(arg, who,whom, aspect, what, whatwith);
+    YAP_Term arg[4];
+    char* namespace=abac_context_namespace(pl->ctxt);
+    _arg_for_fact(arg, namespace,who,whom, aspect, what, whatwith);
     abac_list_t *ret=run_goal(pl,arg);
 
@@ -849,5 +944,5 @@
     abac_list_t *cred_list=abac_list_new();
 
-    if(debug) fprintf(stderr,"making query_again call..\n");
+    DEBUG_PRINTF("making query_again call..\n");
     pl->next_rc=YAP_RestartGoal();
     pl->query_made++;
@@ -856,12 +951,14 @@
     char *tmp=NULL;
     int tmp_sz=try_again(0, &tmp);
-    YAP_Term argterm=YAP_ArgOfTerm(3,goal);
+    YAP_Term argterm=YAP_ArgOfTerm(RULES_ARG_OF_TERM,goal);
     if(pl->next_rc) {
-        if(debug) fprintf(stderr,"query_again: another success\n");
+        DEBUG_PRINTF("query_again: another success\n");
         if(pl->is_partial)
             printf("A partial proof(type:%s)\n",_queryname[pl->query_type]);
         while(1) {
-            YAP_WriteBuffer(argterm, tmp, tmp_sz,YAP_WRITE_HANDLE_VARS);
-            if(strlen(tmp) > 5 && tmp[0]=='\[')
+            int rc=YAP_WriteBuffer(argterm, tmp, tmp_sz,YAP_WRITE_HANDLE_VARS);
+            if(YAP_VERSION == "6.2.2" || YAP_VERSION == "6.2.3") 
+               rc=1; /* XXX rc does not seems to be right for 6.2.x */
+            if(rc==1 && _check_buffer(tmp)) 
                 break;
             tmp_sz=try_again(tmp_sz,&tmp);
@@ -872,8 +969,8 @@
         }
         /* this is returned as ['string1','string2'] */
-        if(debug) fprintf(stderr,"    query answer : %s(%d)\n", tmp, strlen(tmp));
-        _credentials_from_string(cred_list,tmp); 
+        DEBUG_PRINTF("    query answer : %s(%d)\n", tmp, strlen(tmp));
+        _credentials_from_string(pl->ctxt,cred_list,tmp); 
         if(abac_list_size(cred_list)==0) {
-            fprintf(stderr,"CAN NOT retrieve result properly from YAP!!!\n");
+            fprintf(stderr,"CAN NOT retrieve result properly from YAP(2)!!!\n");
             YAP_Exit(1);
         }
@@ -881,5 +978,5 @@
         YAP_FreeSpaceFromYap(tmp);
         } else {
-            if(debug) fprintf(stderr,"query_again: fail finding next\n");
+            DEBUG_PRINTF("query_again: fail finding next\n");
             if(pl->is_partial) { /* if there is more partial query to make */
                 while(pl->query_type != e_PL_NOMORE) {
@@ -896,9 +993,9 @@
 void _print_cred_list(abac_list_t *p)
 {
-    if(debug) fprintf(stderr,"_print_cred_list(%d)items:\n",abac_list_size(p));
+    DEBUG_PRINTF("_print_cred_list(%d)items:\n",abac_list_size(p));
     if(abac_list_size(p) != 0) {
         abac_credential_t *cur=NULL;
         abac_list_foreach(p, cur,
-            if(debug) fprintf(stderr,"printing (%d),\n",(int)cur);
+            DEBUG_PRINTF("printing (%d),\n",(int)cur);
             abac_print_cred_info(cur, NULL);
         );
@@ -929,5 +1026,5 @@
 /* 2 types 
       acme.buys_rocket <- coyote (coyote=prin, acme.buys_rocket=role)
-        ==> isMember(coyote,role(acme,buys_rocket), L)
+        ==> isMember(namespace, coyote,role(acme,buys_rocket), L)
       acme.buys_rocket <- acme.preferred_customer  -- NOT valid 
 */
@@ -961,5 +1058,5 @@
     }
 
-    if(debug) fprintf(stderr,"printing up the yap query ..\n");
+    DEBUG_PRINTF("printing up the yap query ..\n");
 
     char *pstring;
@@ -986,25 +1083,13 @@
 
 /**
- * Get all the credentials (attribute/issuer cert pairs) from prolog
- * (which returns in string form)
- */
-abac_stack_t *abac_pl_credentials(abac_pl_t *pl)
-{
-    abac_stack_t *ret=abac_verifier_dump_creds();
-    return ret;
-}
-
-abac_stack_t *abac_pl_principals(abac_pl_t *pl)
-{
-    abac_stack_t *ret=abac_verifier_dump_principals();
-    return ret;
-}
-
-/**
  * Make a query into prolog db
 --role acme.preferred_customer --principal coyote
---role acme.prefer_customer.buy_rockets --principlal coyote
---oset acme.rockets -- object mrx-21 
+         acme.preferred_customer <-?- coyote
+--role acme.preferred_customer.buy_rockets --principlal coyote
+         acme.preferred_customer.buy_rockets <-?- coyote
+--oset acme.rockets -- object mrx_21 
+         acme.rockets <-?- mrx_21
 --oset acme.villans -- principal coyote
+         acme.villans <-?- coyote
  */
 abac_stack_t *abac_pl_query(abac_pl_t *pl, char *roleoset, char *prinobj)
@@ -1013,21 +1098,18 @@
     int len=strlen(roleoset)+strlen(prinobj)+5;
     char* attr_string=(char *) abac_xmalloc(sizeof(char)*len);
+    /* compose the query statement string */
     sprintf(attr_string,"%s<-%s", roleoset, prinobj);
 
-    if(debug)
-        fprintf(stderr,"abac_pl_query, query string is (%s)\n",attr_string);
+    DEBUG_PRINTF("abac_pl_query, query string is (%s)\n",attr_string);
 
     /* call into yacc parser */
-    abac_reset_yyfptr(attr_string);
-    abac_yy_init();
-    int rc=yyparse();
-    if (rc) {
-        free(attr_string);
+    abac_aspect_t *head_aspect = NULL;
+    abac_aspect_t *tail_aspect = NULL;
+    int using_this=0;
+    int rc=abac_yy_parse(pl->ctxt, attr_string, &head_aspect, &tail_aspect, &using_this);
+    if(rc) { /* ABAC_CERT_INVALID; */
         return NULL;
     }
 
-    abac_aspect_t *head_aspect = abac_yy_get_rule_head_aspect();
-    abac_aspect_t *tail_aspect = abac_yy_get_rule_tail_aspect();
-
     ret=_query_with_aspect(pl,head_aspect,tail_aspect);
 
@@ -1035,5 +1117,6 @@
 }
 
-abac_stack_t *abac_pl_query_with_structure(abac_pl_t *pl, abac_aspect_t *head_aspect, abac_aspect_t *tail_aspect)
+abac_stack_t *abac_pl_query_with_structure(abac_pl_t *pl,
+abac_aspect_t *head_aspect, abac_aspect_t *tail_aspect)
 {
     abac_stack_t *ret=NULL;
