tst_utils.c

00001 /* File:      tst_utils.c
00002 ** Author(s): Ernie Johnson
00003 ** Contact:   xsb-contact@cs.sunysb.edu
00004 ** 
00005 ** Copyright (C) The Research Foundation of SUNY, 1986, 1993-1998
00006 ** 
00007 ** XSB is free software; you can redistribute it and/or modify it under the
00008 ** terms of the GNU Library General Public License as published by the Free
00009 ** Software Foundation; either version 2 of the License, or (at your option)
00010 ** any later version.
00011 ** 
00012 ** XSB is distributed in the hope that it will be useful, but WITHOUT ANY
00013 ** WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00014 ** FOR A PARTICULAR PURPOSE.  See the GNU Library General Public License for
00015 ** more details.
00016 ** 
00017 ** You should have received a copy of the GNU Library General Public License
00018 ** along with XSB; if not, write to the Free Software Foundation,
00019 ** Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00020 **
00021 ** $Id: tst_utils.c,v 1.29 2005/11/16 17:32:06 dwarren Exp $
00022 ** 
00023 */
00024 
00025 
00026 #include "xsb_config.h"
00027 #include "xsb_debug.h"
00028 
00029 #include <stdio.h>
00030 #include <stdlib.h>
00031 
00032 #include "auxlry.h"
00033 #include "cell_xsb.h"
00034 #include "binding.h"
00035 #include "psc_xsb.h"
00036 #include "register.h"
00037 #include "deref.h"
00038 #include "trie_internals.h"
00039 #include "tst_aux.h"
00040 #include "macro_xsb.h"
00041 #include "choice.h"
00042 #include "inst_xsb.h"
00043 #include "error_xsb.h"
00044 
00045 /* ======================================================================= */
00046 
00047 extern void printterm(FILE *, Cell, int);   /* prints to stddbg */
00048 
00049 #ifdef BITS64
00050 #define IntegerFormatString     "%ld"
00051 #else
00052 #define IntegerFormatString     "%d"
00053 #endif
00054 
00055 /* ====================================================================== */
00056 
00057 /*
00058  *            Stacks needed by TST-Subsumptive Operations
00059  *            ===========================================
00060  */
00061 
00062 
00063 #ifndef MULTI_THREAD
00064 DynamicStack  tstTermStack;
00065 DynamicStack  tstTermStackLog;
00066 DynamicStack  tstSymbolStack;
00067 DynamicStack  tstTrail;
00068 #endif
00069 
00070 
00071 void tstInitDataStructs(CTXTdecl) {
00072 
00073   extern void initCollectRelevantAnswers(CTXTdecl);
00074   extern void initSubsumptiveLookup(CTXTdecl);
00075 
00076   DynStk_Init(&tstTermStack, 0 /*TST_TERMSTACK_INITSIZE*/, Cell, "TST Term Stack");
00077   DynStk_Init(&tstTermStackLog, 0 /*TST_TERMSTACKLOG_INITSIZE*/, tstLogFrame,
00078               "TST TermStackLog");
00079   DynStk_Init(&tstSymbolStack, 0 /*TST_SYMBOLSTACK_INITSIZE*/, Cell,
00080               "Trie-Symbol Stack");
00081   DynStk_Init(&tstTrail, 0 /*TST_TRAIL_INITSIZE*/, CPtr, "TST Trail");
00082   initSubsumptiveLookup(CTXT);
00083   initCollectRelevantAnswers(CTXT);
00084 }
00085 
00086 
00087 void tstShrinkDynStacks(CTXTdecl) {
00088 
00089   dsShrink(&tstTermStack);
00090   dsShrink(&tstTermStackLog);
00091   dsShrink(&tstSymbolStack);
00092   dsShrink(&tstTrail);
00093 }
00094 
00095 /* ======================================================================= */
00096 
00097 /*
00098  *                  D E B U G G I N G   R O U T I N E S
00099  *                  ===================================
00100  */
00101 
00102 
00103 /*-------------------------------------------------------------------------*/
00104 
00105 /*
00106  *                        Printing Trie Nodes
00107  *                        -------------------
00108  */
00109 
00110 char *stringNodeStatus(byte fieldNodeStatus) {
00111 
00112   if ( fieldNodeStatus == VALID_NODE_STATUS )
00113     return "Valid";
00114   else
00115     return inst_name(fieldNodeStatus);
00116 }
00117 
00118 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
00119 
00120 char *TrieTypeStrings[] = {
00121   "Call Trie", "Basic Answer Trie", "Time-Stamped Answer Trie",
00122   "Delay Trie", "Asserted Trie", "Interned Trie", "--"
00123 };
00124 
00125 char *stringTrieType(byte fieldTrieType) {
00126 
00127   if ( fieldTrieType > 5 )
00128     return TrieTypeStrings[6];
00129   else
00130     return TrieTypeStrings[fieldTrieType];
00131 }
00132 
00133 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
00134 
00135 char *NodeTypeStrings[] = {
00136   "Interior", "Indexed Interior", "Leaf", "Indexed Leaf",
00137   "Index Header", "--", "--", "--", "Root",
00138 };
00139 
00140 char *stringNodeType(byte fieldNodeType) {
00141 
00142   if ( fieldNodeType > 8 )
00143     return NodeTypeStrings[5];
00144   else
00145     return NodeTypeStrings[fieldNodeType];
00146 }
00147 
00148 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
00149 
00150 void printTrieSymbol(FILE *fp, Cell symbol) {
00151 
00152   if ( symbol == ESCAPE_NODE_SYMBOL )
00153     fprintf(fp, "%lu [ESCAPE_NODE_SYMBOL]", ESCAPE_NODE_SYMBOL);
00154   else {
00155     switch(TrieSymbolType(symbol)) {
00156     case XSB_INT:
00157       fprintf(fp, IntegerFormatString, int_val(symbol));
00158       break;
00159     case XSB_FLOAT:
00160       fprintf(fp, "%f", float_val(symbol));
00161       break;
00162     case XSB_STRING:
00163       fprintf(fp, "%s", string_val(symbol));
00164       break;
00165     case XSB_TrieVar:
00166       fprintf(fp, "V" IntegerFormatString, DecodeTrieVar(symbol));
00167       break;
00168     case XSB_STRUCT:
00169       {
00170         Psc psc;
00171         if (isboxedfloat(symbol))
00172           {
00173               fprintf(fp, "%lf", boxedfloat_val(symbol));
00174               break;              
00175           }
00176         psc = DecodeTrieFunctor(symbol);
00177         fprintf(fp, "%s/%d", get_name(psc), get_arity(psc));
00178       }
00179       break;
00180     case XSB_LIST:
00181       fprintf(fp, "LIST");
00182       break;
00183     default:
00184       fprintf(fp, "Unknown symbol (tag = %ld)", cell_tag(symbol));
00185       break;
00186     }
00187   }
00188 }
00189 
00190 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
00191 
00192 void printTrieNode(FILE *fp, BTNptr pTN) {
00193 
00194   fprintf(fp, "Trie Node: Addr(%p)", pTN);
00195   if ( IsDeletedNode(pTN) )
00196     fprintf(fp, "  (DELETED)");
00197   fprintf(fp, "\n\tInstr(%s), Status(%s), NodeType(%s),\n"
00198           "\tTrieType(%s), Symbol(",
00199           inst_name(TN_Instr(pTN)),
00200           stringNodeStatus(TN_Status(pTN)),
00201           stringNodeType(TN_NodeType(pTN)),
00202           stringTrieType(TN_TrieType(pTN)));
00203   printTrieSymbol(fp, TN_Symbol(pTN));
00204   fprintf(fp, ")");
00205   if ( IsInTimeStampedTrie(pTN) )
00206     fprintf(fp, ", TimeStamp(%ld)", TSTN_TimeStamp((TSTNptr)pTN));
00207   fprintf(fp, "\n\tParent(%p), Child(%p), Sibling(%p)\n",
00208          TN_Parent(pTN), TN_Child(pTN), TN_Sibling(pTN));
00209 }
00210 
00211 /*-----------------------------------------------------------------------*/
00212 
00213 /*
00214  *              Trie-Path Printing Using the SymbolStack
00215  *              ----------------------------------------
00216  */
00217 
00218 static void symstkPrintNextTerm(CTXTdeclc FILE *fp, xsbBool list_recursion) {
00219 
00220   Cell symbol;
00221 
00222   if ( SymbolStack_IsEmpty ) {
00223     fprintf(fp, "<no subterm>");
00224     return;
00225   }
00226   SymbolStack_Pop(symbol);
00227   switch ( TrieSymbolType(symbol) ) {
00228   case XSB_INT:
00229     if ( list_recursion )
00230       fprintf(fp, "|" IntegerFormatString "]", int_val(symbol));
00231     else
00232       fprintf(fp, IntegerFormatString, int_val(symbol));
00233     break;
00234   case XSB_FLOAT:
00235     if ( list_recursion )
00236       fprintf(fp, "|%f]", float_val(symbol));
00237     else
00238       fprintf(fp, "%f", float_val(symbol));
00239     break;
00240   case XSB_STRING:
00241     {
00242       char *string = string_val(symbol);
00243       if ( list_recursion ) {
00244         if ( string == nil_string )
00245           fprintf(fp, "]");
00246         else
00247           fprintf(fp, "|%s]", string);
00248       }
00249       else
00250         fprintf(fp, "%s", string);
00251     }
00252     break;
00253   case XSB_TrieVar:
00254     if ( list_recursion )
00255       fprintf(fp, "|V" IntegerFormatString "]", DecodeTrieVar(symbol));
00256     else
00257       fprintf(fp, "V" IntegerFormatString, DecodeTrieVar(symbol));
00258     break;
00259   case XSB_STRUCT:
00260     {
00261       Psc psc;
00262       int i;
00263       if (isboxedfloat(symbol))
00264       {
00265         if ( list_recursion )
00266           fprintf(fp, "|%lf]", boxedfloat_val(symbol));
00267         else
00268           fprintf(fp, "%lf", boxedfloat_val(symbol));
00269         break;         
00270       }
00271 
00272       if ( list_recursion )
00273         fprintf(fp, "|");
00274       psc = DecodeTrieFunctor(symbol);
00275       fprintf(fp, "%s(", get_name(psc));
00276       for (i = 1; i < (int)get_arity(psc); i++) {
00277         symstkPrintNextTerm(CTXTc fp,FALSE);
00278         fprintf(fp, ",");
00279       }
00280       symstkPrintNextTerm(CTXTc fp,FALSE);
00281       fprintf(fp, ")");
00282       if ( list_recursion )
00283         fprintf(fp, "]");
00284     }
00285     break;
00286   case XSB_LIST:
00287     if ( list_recursion )
00288       fprintf(fp, ",");
00289     else
00290       fprintf(fp, "[");
00291     symstkPrintNextTerm(CTXTc fp,FALSE);
00292     symstkPrintNextTerm(CTXTc fp,TRUE);
00293     break;
00294   default:
00295     fprintf(fp, "<unknown symbol>");
00296     break;
00297   }
00298 }
00299 
00300 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
00301 
00302 void printTriePath(CTXTdeclc FILE *fp, BTNptr pLeaf, xsbBool printLeafAddr) {
00303 
00304   BTNptr pRoot;
00305 
00306   if ( IsNULL(pLeaf) ) {
00307     fprintf(fp, "NULL");
00308     return;
00309   }
00310 
00311   if ( ! IsLeafNode(pLeaf) ) {
00312     fprintf(fp, "printTriePath() called with non-Leaf node!\n");
00313     printTrieNode(fp, pLeaf);
00314     return;
00315   }
00316 
00317   if ( printLeafAddr )
00318     fprintf(fp, "Leaf %p: ", pLeaf);
00319 
00320   if ( IsEscapeNode(pLeaf) ) {
00321     pRoot = BTN_Parent(pLeaf);
00322     if ( IsNonNULL(pRoot) )
00323       printTrieSymbol(fp, BTN_Symbol(pRoot));
00324     else
00325       fprintf(fp, "ESCAPE node");
00326     return;
00327   }
00328 
00329   SymbolStack_ResetTOS;
00330   SymbolStack_PushPathRoot(pLeaf,pRoot);
00331   if ( IsTrieFunctor(BTN_Symbol(pRoot)) ) {
00332     SymbolStack_Push(BTN_Symbol(pRoot));
00333     symstkPrintNextTerm(CTXTc fp,FALSE);
00334   }
00335   else {
00336     printTrieSymbol(fp,BTN_Symbol(pRoot));
00337     fprintf(fp, "(");
00338     symstkPrintNextTerm(CTXTc fp,FALSE);
00339     while ( ! SymbolStack_IsEmpty ) {
00340       fprintf(fp, ",");
00341       symstkPrintNextTerm(CTXTc fp,FALSE);
00342     }
00343     fprintf(fp, ")");
00344   }
00345 }
00346 
00347 /*-----------------------------------------------------------------------*/
00348 
00349 /*
00350  *                     Printing the Answer Template
00351  *                     ----------------------------
00352  *
00353  *  The answer template is assumed to be stored in high-to-low memory
00354  *  vector fashion, with the incoming template pointer pointing to the
00355  *  first term Cell (highest memory Cell of the vector).
00356  */
00357 
00358 void printAnswerTemplate(FILE *fp, CPtr pAnsTmplt, int size) {
00359 
00360   int i;
00361 
00362   fprintf(fp, "Answer Template:\n\tret(");
00363   if (size > 0) {
00364     for (i = 1; i < size; i++) {
00365       printterm(fp, *pAnsTmplt--, 10);
00366       fprintf(fp, ",");
00367     }
00368     printterm(fp, *pAnsTmplt, 10);
00369   }
00370   fprintf(fp, ")\n");
00371 }
00372 
00373 /*-------------------------------------------------------------------------*/
00374 
00375 /*
00376  *              Printing Componenets of a Subgoal Frame
00377  *              ---------------------------------------
00378  *
00379  * Functions for subgoal frame printing can be found in debug_xsb.c.
00380  */
00381 
00382 
00383 /* Printing a SF's associated Call
00384    ------------------------------- */
00385 void sfPrintGoal(CTXTdeclc FILE *fp, VariantSF pSF, xsbBool printAddr) {
00386 
00387   if ( printAddr )
00388     fprintf(fp, "SF %p  ", pSF);
00389   printTriePath(CTXTc fp, subg_leaf_ptr(pSF), NO);
00390 }
00391 
00392 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
00393 
00394 /* Printing Subsumed Calls
00395    ----------------------- */
00396 void sfPrintConsGoals(CTXTdeclc FILE *fp, SubProdSF pProd) {
00397 
00398   SubConsSF pCons;
00399 
00400   fprintf(fp, "Producer:\n  ");
00401   sfPrintGoal(CTXTc fp, (VariantSF)pProd, YES);
00402   fprintf(fp, "\nConsumers:\n");
00403   for ( pCons = subg_consumers(pProd);  IsNonNULL(pCons);
00404         pCons = conssf_consumers(pCons) ) {
00405     fprintf(fp, "  ");
00406     sfPrintGoal(CTXTc fp, (VariantSF)pCons, YES);
00407     fprintf(fp, "\n");
00408   }
00409 }
00410 
00411 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
00412 
00413 /* Printing Answers in an Answer List
00414    ---------------------------------- */
00415 void printAnswerList(CTXTdeclc FILE *fp, ALNptr pALN) {
00416 
00417   fprintf(fp, "Answer List %p:\n", pALN);
00418   while ( IsNonNULL(pALN) ) {
00419     fprintf(fp, "  ");
00420     printTriePath(CTXTc fp, ALN_Answer(pALN), YES);
00421     fprintf(fp, "\n");
00422     pALN = ALN_Next(pALN);
00423   }
00424 }
00425 /*-------------------------------------------------------------------------*/
00426 
00427 /*
00428  *                          Miscellaneous
00429  *                          -------------
00430  */
00431 
00432 void printTabledCall(FILE *fp, TabledCallInfo callInfo) {
00433 
00434   int arity, i;
00435   Psc pPSC;
00436   
00437   pPSC = TIF_PSC(CallInfo_TableInfo(callInfo));
00438   fprintf(fp, "%s(", get_name(pPSC));
00439   arity = CallInfo_CallArity(callInfo);
00440   for (i = 0; i < arity; i++) {
00441     printterm( fp, (Cell)(CallInfo_Arguments(callInfo)+i), 25 );
00442     if (i+1 < arity)
00443       fprintf(fp, ",");
00444   }
00445   fprintf(fp, ")");
00446 }
00447 
00448 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
00449 
00450 void printTriePathType(CTXTdeclc FILE *fp, TriePathType type, BTNptr leaf) {
00451 
00452   switch (type) {
00453   case NO_PATH:
00454     fprintf(fp,"No path found :-(\n");
00455     break;
00456   case VARIANT_PATH:
00457     fprintf(fp,"Variant path found: ");
00458     printTriePath(CTXTc fp,leaf,FALSE);
00459     break;
00460   case SUBSUMPTIVE_PATH:
00461     fprintf(fp,"Subsumptive path found: ");
00462     printTriePath(CTXTc fp,leaf,FALSE);
00463     break;
00464   default:
00465     fprintf(fp,"What kind of path? (%d)\n", type);
00466     break;
00467   }
00468 }

Generated on Wed Jul 26 13:30:43 2006 for XSB by  doxygen 1.4.5