Index: libabac/abac_pl_yap.c
===================================================================
--- libabac/abac_pl_yap.c	(revision 202a7f97d0dc5cb5eb2b43332f0711eafa4d7170)
+++ libabac/abac_pl_yap.c	(revision da5afdf4d61c978dbd17b9d6f20e5a45dcab870a)
@@ -21,8 +21,11 @@
 
 extern char *abac_cn_with_role(abac_role_t *);
-extern abac_list_t *abac_credential_clauses(abac_credential_t *cred);
+extern char *abac_cn_with_oset(abac_oset_t *);
+extern abac_list_t *abac_credential_clauses(abac_credential_t *);
+extern char *abac_id_clause(abac_id_cert_t *);
+
 static int debug=0;
 
-// pl
+// pl -- place holder for now 
 struct _abac_pl_t {
     FILE *fptr;
@@ -56,5 +59,5 @@
 {
     char lstr[]="listing";
-    YAP_Term *eterm;
+    YAP_Term *eterm; /* error term */
 
     printf("\n\n========= yap db (%s)\n",msg);
@@ -164,5 +167,5 @@
 }
 
-/* cases,
+/* cases, 
      ['str']
      ['str1','str2']
@@ -197,5 +200,5 @@
              free(string); 
              if(cred) {
-                 abac_stack_push(credentials, cred);
+                 abac_stack_unique_push(credentials, cred);
                  cnt++;
                  } else {
@@ -211,5 +214,5 @@
                  free(string);
                  if(cred) {
-                     abac_stack_push(credentials, cred);
+                     abac_stack_unique_push(credentials, cred);
                      cnt++;
                      } else {
@@ -224,54 +227,166 @@
 }
 
-/* 2 types 
-      acme.buys_rocket <- coyote (coyote=prin, acme.buys_rocket=role)
-      acme.buys_rocket <- acme.preferred_customer  -- NOT DONE YET */
-static abac_stack_t *_query(abac_pl_t *pl, abac_role_t* role, abac_role_t* role_prin )
+
+static abac_stack_t *_make_yap_query(char *cn_prin, char *cn_rule, char* estring)
 {
     YAP_Term *eterm;
     YAP_Term arg[3];
-    char tmp[2000];
+    char tmp[5000];
     abac_stack_t *cred_list = abac_stack_new();
 
-    if(debug)
-        _show_yap_db("DEBUG:calling within _query");
-
-    char *cn_prin=abac_cn_with_role(role_prin);
-    char *cn_role=abac_cn_with_role(role);
-
-    if(abac_role_role_name(role_prin)!=NULL) {
-        printf("fail, a.r <- b.r query is not implemented yet !!!\n");
-        YAP_Exit(1);
-    }
-
-    if (cn_prin == NULL || cn_role == NULL) {
-        printf("fail, query's call got bad roles.. \n");
-        YAP_Exit(1);
-    }
-    sprintf(tmp,"role(%s,%s)", cn_role, abac_role_role_name(role));
+    if(debug) printf(" the role part(%s)\n", estring);
     arg[0]=YAP_MkAtomTerm(YAP_LookupAtom(cn_prin));
-    arg[1]=YAP_ReadBuffer(tmp,eterm);
+    if(debug) printf(" the principal part(%s)\n", cn_prin);
+    arg[1]=YAP_ReadBuffer(estring,eterm);
     arg[2]=YAP_MkVarTerm();
     YAP_Atom f = YAP_LookupAtom("isMember");
     YAP_Functor func = YAP_MkFunctor(f, 3);
     YAP_Term goal=YAP_MkApplTerm(func, 3, arg);
+
     int rc =YAP_RunGoal( goal );
     if (rc) {
-        printf("success, query call ok.. \n");
-        YAP_WriteBuffer(arg[2], tmp, 2000,YAP_WRITE_HANDLE_VARS);
-        /* printf("    answer : %s(%d)\n", tmp, strlen(tmp)); */
+        printf("YAP query succeed\n");
+        YAP_WriteBuffer(arg[2], tmp, 5000,YAP_WRITE_HANDLE_VARS);
+        if(debug) printf("    query answer : %s\n", tmp);
+        /* this is returned as ['string1','string2'] */
+        _credentials_from_string(cred_list,tmp); 
+/**** XXX somehow this got stuck in yap ???
+        while (YAP_RestartGoal()) {
+            if(debug) printf("another success\n");
+            YAP_WriteBuffer(arg[2], tmp, 5000,YAP_WRITE_HANDLE_VARS);
+            if(debug) printf("    query answer : %s\n", tmp); 
+            _credentials_from_string(cred_list,tmp); 
+        }
+***/
+    } else {
+        printf("YAP query failed\n");
+        /* YAP_Exit(1); */
+    }
+    return cred_list;
+}
+
+/* 2 types 
+      acme.buys_rocket <- coyote (coyote=prin, acme.buys_rocket=role)
+        ==> isMember(coyote,role(acme,buys_rocket), L)
+
+      acme.buys_rocket <- acme.preferred_customer  -- NOT DONE YET */
+static abac_stack_t *_query_with_role(abac_pl_t *pl, abac_role_t* role, abac_role_t* role_prin)
+{
+    char tmp[5000];
+    abac_stack_t *ret=NULL;
+
+    if(1)
+        _show_yap_db("DEBUG:calling within _query_with_role");
+
+    char *cn_prin=abac_cn_with_role(role_prin);
+    char *cn_role=abac_cn_with_role(role);
+
+    if(abac_role_role_name(role_prin)!=NULL) {
+        printf("fail, a.r <- b.r query is not implemented yet !!!\n");
+        YAP_Exit(1);
+    }
+
+    if (cn_prin == NULL || cn_role == NULL) {
+        printf("fail, query's call got bad roles.. \n");
+        YAP_Exit(1);
+    }
+
+    if(debug) printf("printing up the yap query ..\n");
+    char *pstring=abac_role_role_param_string(role);
+    if(pstring) {
+        sprintf(tmp,"role(%s,%s,%s)", cn_role, abac_role_role_name(role),pstring);
+        free(pstring);
+        } else {
+            sprintf(tmp,"role(%s,%s)", cn_role, abac_role_role_name(role));
+    }
+    ret=_make_yap_query(cn_prin,cn_role,tmp);
+    return ret;
+}
+
+static abac_stack_t *_query_with_oset(abac_pl_t *pl, abac_oset_t* oset, abac_oset_t* oset_obj)
+{
+    char tmp[5000];
+    abac_stack_t *ret=NULL;
+
+    if(debug)
+        _show_yap_db("DEBUG:calling within _query_with_oset");
+
+    char *cn_oset=abac_cn_with_oset(oset);
+    /* could be obj or principal */
+    char *cn_prin=NULL;
+    if(abac_oset_is_object(oset_obj)) {
+        cn_prin=abac_oset_object_cn(oset_obj);
+        } else {
+            cn_prin=abac_oset_principal_cn(oset_obj);
+    }
+
+    if(abac_oset_oset_name(oset_obj)!=NULL) {
+        printf("fail, a.o <- b.o query is not implemented yet !!!\n");
+        YAP_Exit(1);
+    }
+
+    if (cn_prin == NULL || cn_oset == NULL) {
+        printf("fail, query's call got bad oset.. \n");
+        YAP_Exit(1);
+    }
+
+    if(debug) printf("printing up the yap query ..\n");
+    char *pstring=abac_oset_oset_param_string(oset);
+    if(pstring) {
+        sprintf(tmp,"oset(%s,%s,%s)", cn_oset, abac_oset_oset_name(oset),pstring);
+        free(pstring);
+        } else {
+            sprintf(tmp,"oset(%s,%s)", cn_oset, abac_oset_oset_name(oset));
+    }
+
+    ret=_make_yap_query(cn_prin,cn_oset,tmp);
+    return ret;
+}
+
+static abac_stack_t *_dump(abac_pl_t *pl)
+{
+    YAP_Term *eterm;
+    YAP_Term arg[3];
+    char tmp[5000];
+    abac_stack_t *cred_list = abac_stack_new();
+
+    if(debug)
+        _show_yap_db("DEBUG:calling within _dump");
+
+    arg[0]=YAP_MkVarTerm();
+    arg[1]=YAP_MkVarTerm();
+    arg[2]=YAP_MkVarTerm();
+    YAP_Atom f = YAP_LookupAtom("isMember");
+    YAP_Functor func = YAP_MkFunctor(f, 3);
+    YAP_Term goal=YAP_MkApplTerm(func, 3, arg);
+
+    int rc =YAP_RunGoal( goal );
+    if (rc) {
+        printf("YAP dump succeed\n");
+        YAP_WriteBuffer(arg[2], tmp, 5000,YAP_WRITE_HANDLE_VARS);
+        if(debug) printf("    dump answer : %s\n", tmp);
         /* this is returned as ['string1','string2'] */
         _credentials_from_string(cred_list,tmp); 
         while (YAP_RestartGoal()) {
-            printf("another success\n");
-            YAP_WriteBuffer(arg[2], tmp, 2000,YAP_WRITE_HANDLE_VARS);
-            /* printf("    answer : %s\n", tmp); */
+            if(debug) printf("another dump success\n");
+            YAP_WriteBuffer(arg[2], tmp, 5000,YAP_WRITE_HANDLE_VARS);
+            if(debug) printf("    dump restart answer : %s\n", tmp);
             _credentials_from_string(cred_list,tmp); 
         }
     } else {
-        printf("fail, query's call is bad.. \n");
-        YAP_Exit(1);
+        printf("YAP _dump query failed\n");
+        /* YAP_Exit(1); */
     }
     return cred_list;
+}
+
+/**
+ * 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=_dump(pl);
+    return ret;
 }
 
@@ -280,29 +395,36 @@
 --role acme.preferred_customer --principal coyote
 --role acme.prefer_customer.buy_rockets --principlal coyote
+--oset acme.rockets -- object mrx-21 
+--oset acme.villans -- principal coyote
  */
-abac_stack_t *abac_pl_query(abac_pl_t *pl, char *role_name, char *principal)
-{
-    abac_role_t *role = abac_role_from_string(role_name);
-    abac_role_t *prin_role = abac_role_from_string(principal);
+abac_stack_t *abac_pl_query(abac_pl_t *pl, char *roleoset, char *prinobj)
+{
     abac_stack_t *ret=NULL;
-
-    // give up on bogus roles
-    if (role == NULL || prin_role == NULL) {
+    int len=strlen(roleoset)+strlen(prinobj)+5;
+    char* attr_string=(char *) abac_xmalloc(sizeof(char)*len);
+    sprintf(attr_string,"%s<-%s", roleoset, prinobj);
+
+    if(debug)
+        printf("abac_pl_query, query string is (%s)\n",attr_string);
+
+    /* call into yacc parser */
+    abac_reset_yyfptr(attr_string);
+    abac_yyinit();
+    int rc=yyparse();
+    if (rc) {
+        free(attr_string);
         return NULL;
     }
 
-    ret=_query(pl,role,prin_role);
-
+    if(abac_get_yap_is_oset()) {
+        abac_oset_t *head_oset = abac_get_yap_head_oset();
+        abac_oset_t *tail_oset = abac_get_yap_tail_oset();
+        ret=_query_with_oset(pl,head_oset,tail_oset);
+        } else {
+            abac_role_t *head_role = abac_get_yap_head_role();
+            abac_role_t *tail_role = abac_get_yap_tail_role();
+            ret=_query_with_role(pl,head_role,tail_role);
+    }
     return ret;
-}
-
-/**
- * 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 *credentials = abac_stack_new();
-    /* fix up a stream to stream the credentials from prolog engine */
-    return credentials;
 }
 
