Transitive Closure with XDb1

From: Neo <neo55592_at_hotmail.com>
Date: 11 May 2004 12:18:10 -0700
Message-ID: <4b45d3ad.0405111118.1851852f_at_posting.google.com>



This code demos transitive closure in XDb1 by creating the following simple hierarchy and looping thru it:

Mars is part of Universe.
John is part of Mars.
Venus is part of Universe.
Mary is part of Venus.

The code also loops thru persons, planets and moves John to Venus and Mary to Mars.

void Example003()
{

// Create universe and classify as a thing

      int* pUniverse = T_create();
      T_relate(&pUniverse, rCls, &pThing_g);
      T_Name_relate(&pUniverse, _T("universe"));


// Create planet and classify as a thing
int* pPlanet = T_create(); T_relate(&pPlanet, rCls, &pThing_g); T_Name_relate(&pPlanet, _T("planet"));
// Create mars and classify as a planet
int* pMars = T_create(); T_relate(&pMars, rCls, &pThing_g); T_Name_relate(&pMars, _T("mars"));
// Create venus and classify as a planet
int* pVenus = T_create(); T_relate(&pVenus, rCls, &pThing_g); T_Name_relate(&pVenus, _T("venus"));
// Create person and classify as a thing
int* pPerson = T_create(); T_relate(&pPerson, rCls, &pThing_g); T_Name_relate(&pPerson, _T("person"));
// Create john and classify as a person
int* pJohn = T_create(); T_relate(&pJohn, rCls, &pPerson); T_Name_relate(&pJohn, _T("john"));
// Create mary and classify as a person
int* pMary = T_create(); T_relate(&pMary, rCls, &pPerson); T_Name_relate(&pMary, _T("mary"));

// Create universe hierarchy
// Note: rPart/rAsm are reciprocal relators

      T_relate(&pMars, rAsm, &pUniverse);   // or (&pUniverse, rPart, &pMars)
      T_relate(&pVenus, rAsm, &pUniverse);  // or (&pUniverse, rPart, &pVenus)
      T_relate(&pJohn, rAsm, &pMars);       // or (&pMars, rPart, &pJohn)
      T_relate(&pMary, rAsm, &pVenus);      // or (&pVenus, rPart, &pMary)
      

// Demo transitive closure by
// looping thru parts of universe
// Prints: mars, john, venus, mary

      #define maxTreeDepth 64
      int* pDescX_a[maxTreeDepth] = {T_R_PriRecip(pUniverse), NULL};
      while (int i=T_Relative(pDescX_a, maxTreeDepth, rPart, TRUE)){
         // Print name of thing
            TCHAR sName[kNmSz_g+1] = _T("");
            int* pT = R_Dest(pDescX_a[i]);
            T_Name_get(pT, sName, kNmSz_g);
            TRACE(_T("%s\n"), sName);
      }

// Print names of persons: john and mary
// Loop thru person's relations

      int* pR = pPerson;
      while (*(++pR)){
         // If relation is to an instance
            if (rInst == R_Type_get(pR)){
               // Print name of instance
                  TCHAR sName[kNmSz_g+1] = _T("");
                  int* pInst = R_Dest(pR);
                  T_Name_get(pInst, sName, kNmSz_g);
                  TRACE(_T("%s\n"), sName);
            }
      }

// Print names of planets: mars and venu
// Loop thru planet's relations

      pR = pPlanet;
      while (*(++pR)){
         // If relation is to an instance
            if (rInst == R_Type_get(pR)){
               // Print name of instance
                  TCHAR sName[kNmSz_g+1] = _T("");
                  int* pInst = R_Dest(pR);
                  T_Name_get(pInst, sName, kNmSz_g);
                  TRACE(_T("%s\n"), sName);
            }
      }


// Move john from mars to venus
// Get mar's relation to john pR = T_R_For(pMars, pJohn); // Move relation to venus R_move(pR, &pVenus);
// Move mary from venus to mars
// Get venus's relation to mary pR = T_R_For(pVenus, pMary); // Move relation to mars R_move(pR, &pMars);

}

For more info, see www.xdb1.com/Example/Ex003.asp Received on Tue May 11 2004 - 21:18:10 CEST

Original text of this message