Re: Normalizing Every Thing with XDb2

From: Neo <neo55592_at_hotmail.com>
Date: 28 Jul 2004 16:24:05 -0700
Message-ID: <4b45d3ad.0407281524.59c214e9_at_posting.google.com>


> x wrote: I wonder if you can do math with those quantities. > For example could one ask for the total weight of all boxes.

In the example below, the quantities have been classified as integers.

/**************************************************
Create two boxes with thickness, weight and cost. Loop thru boxes and sum their weights.
**************************************************/
void Example_BoxProperties()
{
// Create integer

    NLI_create_r(_T("*integer cls thing"));

// Create quantity

    NLI_create_r(_T("*quantity cls thing"));

    // Create 1

      NLI_create_r(_T("*"));
      NLI_create_r(_T("* name '1'"));
      NLI_create_r(_T("* cls quantity"));
      NLI_create_r(_T("* cls integer"));

    // Create 10
      NLI_create_r(_T("*"));
      NLI_create_r(_T("* name '10'"));
      NLI_create_r(_T("* cls quantity"));
      NLI_create_r(_T("* cls integer"));

// Create unit

    NLI_create_r(_T("*unit cls thing"));
    NLI_create_r(_T("*cm cls unit"));
    NLI_create_r(_T("*kg cls unit"));
    NLI_create_r(_T("*$ cls unit"));

// Create distance

    NLI_create_r(_T("*distance cls thing"));

    // Create 1 cm

      NLI_create_r(_T("*"));
      NLI_create_r(_T("* cls distance"));
      NLI_create_r(_T("* quantity 1"));
      NLI_create_r(_T("* unit cm"));

// Create mass

    NLI_create_r(_T("*mass cls thing"));

    // Create 1 kg

      NLI_create_r(_T("*"));
      NLI_create_r(_T("* cls mass"));
      NLI_create_r(_T("* quantity 1"));
      NLI_create_r(_T("* unit kg"));

    // Create 10 kg
      NLI_create_r(_T("*"));
      NLI_create_r(_T("* cls mass"));
      NLI_create_r(_T("* quantity 10"));
      NLI_create_r(_T("* unit kg"));

// Create money

    NLI_create_r(_T("*money cls thing"));

    // Create 1 $

      NLI_create_r(_T("*"));
      NLI_create_r(_T("* cls money"));
      NLI_create_r(_T("* quantity 1"));
      NLI_create_r(_T("* unit $"));

// Create relators

    NLI_create_r(_T("*thickness cls relator"));
    NLI_create_r(_T("*weight cls relator"));
    NLI_create_r(_T("*cost cls relator"));

// Create box

    NLI_create_r(_T("*box cls thing"));

    // Create box1

      NLI_create_r(_T("*box1 cls box"));
      NLI_create_r(_T("box1 thickness (distance)((quantity)1)((unit)cm)"));
      NLI_create_r(_T("box1 weight (mass)(1)(kg)"));
      NLI_create_r(_T("box1 cost (money)(1)($)"));

    // Create box2
      NLI_create_r(_T("*box2 cls box"));
      NLI_create_r(_T("box2 thickness (distance)((quantity)1)((unit)cm)"));
      NLI_create_r(_T("box2 weight (mass)(10)(kg)"));
      NLI_create_r(_T("box2 cost (money)(1)($)"));

// Get ptr to things created above
// Note: one of several methods

    int* pBox = Str_getDefT(_T("box"));
    int* pQty = Str_getDefT(_T("quantity"));
    int* pWeight = Str_getDefT(_T("weight"));
    int* pInteger = Str_getDefT(_T("integer"));

// Loop thru box instances
    int totalWeight = 0;
    int* pE[32];
    Expr_SVx_get_r(pBox, pReInst_g, pE);     while (int* pBoxX = X2(pE)){

      // Get boxX's weight
        int* pWeightX = S_SVx_1st(pBoxX, pWeight);
        if (pWeightX){
          // Get weightX's qty
            int* pQtyX = S_SVx_1st(pWeightX, pQty);
            if (pQtyX){
              // If qtyX is an integer
                if (S_SVO_1st(pQtyX, pReCls_g, pInteger)){
                  // Get qtyX's name as an ascii str
                    TCHAR sQtyX[kStrSz_g+1] = _T("");
                    T_Name_get(pQtyX, sQtyX, kStrSz_g);

                  // Convert ascii str to integer
                    int iQtyX = _ttoi(sQtyX);

                  // Sum weight
                    totalWeight = totalWeight + iQtyX;
                }
            }
        }

    }     

// Print total weight of boxes (prints 11)
    TRACE(_T("Total weight of boxes is: %d\n"), totalWeight); } Received on Thu Jul 29 2004 - 01:24:05 CEST

Original text of this message