Ex305b - Pizza Orders / Source Code

Description:
Below C code creates Example 305 programmatically.
void Example305b() 
{
  // Create classes
    // Create order
      int* pOrder = N_create();
      N_Name_setWithAStr(pOrder, _T("order"));
      N_SVO_set(pDbDb_g, pItem_g, pOrder);

    // Create pizza
      int* pPizza = N_create();
      N_Name_setWithAStr(pPizza, _T("pizza"));
      N_SVO_set(pDbDb_g, pItem_g, pPizza);

    // Create size
      int* pSize = N_create();
      N_Name_setWithAStr(pSize, _T("size"));
      N_SVO_set(pDbDb_g, pItem_g, pSize);

    // Create crust
      int* pCrust = N_create();
      N_Name_setWithAStr(pCrust, _T("crust"));
      N_SVO_set(pDbDb_g, pItem_g, pCrust);

    // Create cheese
      int* pCheese = N_create();
      N_Name_setWithAStr(pCheese, _T("cheese"));
      N_SVO_set(pDbDb_g, pItem_g, pCheese);

    // Create topping
      int* pTopping = N_create();
      N_Name_setWithAStr(pTopping, _T("topping"));
      N_SVO_set(pDbDb_g, pItem_g, pTopping);

    // Create drink
      int* pDrink = N_create();
      N_Name_setWithAStr(pDrink, _T("drink"));
      N_SVO_set(pDbDb_g, pItem_g, pDrink);

    // Create drinkType
      int* pDrinkType = N_create();
      N_Name_setWithAStr(pDrinkType, _T("drinkType"));
      N_SVO_set(pDbDb_g, pItem_g, pDrinkType);

    // Note: no need to create quantity
    // It already exists in db.
      int* pQuantity = AStr_getNamed(_T("quantity"));


  // Create order#1
    int* pOrder1 = N_create();
    N_SVO_set(pOrder, pInst_g, pOrder1);
    N_Name_setWithAStr(pOrder1, _T("order#1"));

    // Create a pizza
      int* pAPizza = N_create();
      N_SVO_set(pPizza, pInst_g, pAPizza);

      // Relate pizza as part of order 1
        N_SVO_set(pOrder1, pPart_g, pAPizza);

      // Relate pizza's size to small
        N_EA_setV_wAStr(pAPizza, pSize, _T("small"));

      // Relate pizza's crust to thin
        N_EA_setV_wAStr(pAPizza, pCrust, _T("thin"));

      // Relate pizza's cheese to mozzarella
        N_EA_setV_wAStr(pAPizza, pCheese, _T("mozzarella"));

      // Relate pizza's topping to veggies
        N_EA_setV_wAStr(pAPizza, pTopping, _T("veggies"));


  // Create order#2
    int* pOrder2 = N_create();
    N_SVO_set(pOrder, pInst_g, pOrder2);
    N_Name_setWithAStr(pOrder2, _T("order#2"));

    // Create a pizza
      pAPizza = N_create();
      N_SVO_set(pPizza, pInst_g, pAPizza);

      // Relate pizza as part of order 2
        N_SVO_set(pOrder2, pPart_g, pAPizza);

      // Relate pizza's size to large
        N_EA_setV_wAStr(pAPizza, pSize, _T("large"));

      // Relate pizza's crust to thick
        N_EA_setV_wAStr(pAPizza, pCrust, _T("thick"));

      // Relate pizza's cheese to mozzarella
        N_EA_setV_wAStr(pAPizza, pCheese, _T("mozzarella"));

      // Relate pizza's cheese to mozzarella
        N_EA_setV_wAStr(pAPizza, pCheese, _T("parmesan"));

      // Relate pizza's topping to sausage
        N_EA_setV_wAStr(pAPizza, pTopping, _T("sausage"));

      // Relate pizza's topping to pepperoni
        N_EA_setV_wAStr(pAPizza, pTopping, _T("pepperoni"));

      // Relate pizza's topping to olive
        N_EA_setV_wAStr(pAPizza, pTopping, _T("olive"));

    // Create a drink
      int* pADrink = N_create();
      N_SVO_set(pDrink, pInst_g, pADrink);

      // Relate drink as part of order 2
        N_SVO_set(pOrder2, pPart_g, pADrink);

      // Relate drink's drinkType to coke
        N_EA_setV_wAStr(pADrink, pDrinkType, _T("coke"));

      // Relate drink's qty to 4
        N_EA_setV_wAStr(pADrink, pQuantity, _T("4"));


  // Create query to find order with a thin pizza with veggies
    TCHAR str[kStrSz_g+1] = _T("");
    _tcscat(str, _T("(and (get order instance *)"));
    _tcscat(str, _T("     (get * part (and (get pizza instance *)"));
    _tcscat(str, _T("                      (get * crust thin)"));
    _tcscat(str, _T("                      (get * topping veggies))))"));
    int* pQ = Xp_compile(str);

  // Loop thru things satisfying query
  // Prints "order#1"
    while (int* pT=Xp_execute(pQ)){
      TCHAR sOrder[kStrSz_g+1] = _T("");
      N_Name_get(pT, sOrder, kStrSz_g);
      TRACE(_T("%s\n"), sOrder);
    }
}

Notes:

CM ©2000-2007