/*
This example illustrates the code needed to create a user-defined
function which will print all facts which contain a specified
slot. In particular, the use of GetNextFact and GetFactSlot are
shown. Example use of the facts-containing-slot function is shown
following:

CLIPS> (deftemplate foo (slot a) (slot b))
CLIPS> (deftemplate bar (slot b) (slot c))
CLIPS> 
(assert (foo (a 1) (b 2))
        (foo (a 3) (b 4))
        (bar (b 5) (c 6))
        (yak 7 8 9))
<Fact-3>
CLIPS> (facts-containing-slot a)
(foo (a 1) (b 2))
(foo (a 3) (b 4))
CLIPS> (facts-containing-slot b)
(foo (a 1) (b 2))
(foo (a 3) (b 4))
(bar (b 5) (c 6))
CLIPS> (facts-containing-slot c)
(bar (b 5) (c 6))
CLIPS> (facts-containing-slot d)
CLIPS>

The C code for the facts-containing-slot function follows:
*/

#include "clips.h"

#if ANSI_COMPILER
VOID FactsContainingSlot(void);
VOID UserFunctions(void);
#else
VOID FactsContainingSlot();
VOID UserFunctions();
#endif

/* 
This UserFunctions should be used in place of the 
UserFunctions defined in the MAIN.C file.
*/

VOID UserFunctions()
  {   
   DefineFunction("facts-containing-slot",'v',
                  PTIF FactsContainingSlot,
                  "FactsContainingSlot");
  }
  
VOID FactsContainingSlot()
  {
   VOID *theFact;
   DATA_OBJECT theValue;
   char *slotName;
   
   /*========================================================*/
   /* The function expects one argument: the name of a slot. */
   /*========================================================*/
   
   if (ArgCountCheck("facts-containing-slot",EXACTLY,1) == -1) 
     { return; }
   
   if (ArgTypeCheck("facts-containing-slot",1,SYMBOL,&theValue) == CLIPS_FALSE) 
     { return; }
     
   slotName = DOToString(theValue);

   /*===========================================*/
   /* Loop through every fact in the fact list. */
   /*===========================================*/
   
   for (theFact = GetNextFact(NULL); 
        theFact != NULL;
        theFact = GetNextFact(theFact))
     {
      /*===============================================*/
      /* If the fact contains the specified slot, then */
      /* display the fact. The undocumented function   */
      /* PrintFact is used to display the fact since   */
      /* it doesn't require any user defined memory.   */
      /* Alternately, the GetFactPPForm function could */
      /* have been used.                               */
      /*===============================================*/
      
      if (GetFactSlot(theFact,slotName,&theValue))
        { 
         PrintFact("stdout",theFact);
         PrintCLIPS("stdout","\n");
        }
     }
  }
  

