Changeset 169

Show
Ignore:
Timestamp:
03/20/09 13:59:53 (15 years ago)
Author:
bhilburn
Message:

First pass at refactoring the parse_ce_config function. Still tons of work to do
in this function alone.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • vtcross/trunk/src/shell/cr_shell.cpp

    r168 r169  
    6262    for(size_t i = 0; i < ce_info->numObservables; i++) { 
    6363                LOG("Observable: %s\n", oList[i]->name); 
     64 
    6465                for(size_t j = 0; j < oList[i]->numAffects; j++) { 
    6566                        LOG("\t\tAffect %s -> %s ", oList[i]->affection_list[j].u->name, \ 
     
    7576{ 
    7677 
    77         TiXmlElement* pElem;    //!current element 
    78         TiXmlElement* pChild;   //!current child of pElem 
    79         TiXmlElement* pChild1;  //!current child of pElem 
    80         TiXmlElement* pSecondChild;     //!current child of pElem 
    81         TiXmlHandle hDoc(doc);  //!handle to xml document 
    82         TiXmlHandle hRoot(0); //! handle to root element  
     78        TiXmlElement *pElem; 
     79        TiXmlElement *pChild; 
     80        TiXmlElement *pChild1; 
     81        TiXmlElement *pSecondChild; 
     82        TiXmlHandle hDoc(doc); 
     83        TiXmlHandle hRoot(0); 
    8384 
    8485        int32_t count = 0; 
    85         int32_t i = 0; 
    86         int32_t j = 0; 
    87         int32_t k = 0; 
    88         int32_t match_found = 0; 
     86        size_t item_count = 0; 
     87    size_t affect_count = 0; 
     88        int32_t attribute_count = 0; 
     89    bool match_found = false; 
    8990 
    9091        pElem = hDoc.FirstChildElement().Element(); 
    91         if(!pElem) { cout << "no valid root! quit-ing function!" << endl; return 0; } 
     92 
     93        if(!pElem)  
     94        ERROR(1, "No valid root!"); 
     95 
    9296        hRoot = TiXmlHandle(pElem); 
    9397 
    94         // Pull utility information from XML file. 
    95  
    9698        pElem = hRoot.FirstChild("utilities").Element(); 
    97         pChild1 = hRoot.Child("utilities",count).Element(); 
    98  
    99  
    100         for(pChild = pChild1->FirstChildElement("utility"); pChild; pChild = pChild->NextSiblingElement()) 
    101         { 
    102                 u[i] = new Utility; 
     99        pChild1 = hRoot.Child("utilities", count).Element(); 
     100 
     101        for(pChild = pChild1->FirstChildElement("utility"); pChild; \ 
     102            pChild = pChild->NextSiblingElement()) { 
     103 
     104                u[item_count] = new Utility; 
     105 
    103106                const char *uName = pChild->Attribute("name"); 
    104                 if(uName) u[i]->name = uName;    
     107                if(uName) 
     108            u[item_count]->name = uName;         
     109 
    105110                const char *uUnits = pChild->Attribute("units"); 
    106                 if(uUnits) u[i]->units = uUnits; 
     111                if(uUnits)  
     112            u[item_count]->units = uUnits; 
     113 
    107114                const char *uGoal = pChild->Attribute("goal"); 
    108                 if(uGoal) u[i]->goal = uGoal; 
    109                 if(pChild->QueryFloatAttribute("target",&u[i]->target) != TIXML_SUCCESS) u[i]->target = -1; 
    110                 i++; 
    111         } 
    112         ce_info->numUtilities = i;       
    113         cout << "Initialize:: Parsed " << ce_info->numUtilities << " utilities." << endl; 
    114  
    115         // Pull observable information from XML file. 
    116         i = 0; 
     115                if(uGoal)  
     116            u[item_count]->goal = uGoal; 
     117 
     118                if(pChild->QueryFloatAttribute("target", &u[i]->target) != TIXML_SUCCESS)  
     119            u[i]->target = -1; 
     120 
     121                item_count++; 
     122        } 
     123 
     124        ce_info->numUtilities = item_count;      
     125        LOG("Initialize:: Parsed %d utilities.\n", ce_info->numUtilities); 
     126 
     127        item_count = 0; 
    117128        pElem = hRoot.FirstChild("observables").Element(); 
    118         pChild1 = hRoot.Child("observables",count).Element(); 
    119          
    120         for(pChild = pChild1->FirstChildElement("observable"); pChild; pChild = pChild->NextSiblingElement()) 
    121         { 
     129        pChild1 = hRoot.Child("observables", count).Element(); 
     130 
     131        for(pChild = pChild1->FirstChildElement("observable"); pChild; \ 
     132            pChild = pChild->NextSiblingElement()) { 
    122133 
    123134                const char *oName = pChild->Attribute("name"); 
    124                 o[i] = new Observable; 
    125  
    126                 if(oName) o[i]->name = oName; 
     135                o[item_count] = new Observable; 
     136                if(oName)  
     137            o[item_count]->name = oName; 
    127138                 
    128                 j = 0; 
    129                 for(pSecondChild = pChild->FirstChildElement("affect"); pSecondChild; pSecondChild = pSecondChild->NextSiblingElement()) 
    130                 { 
     139                affect_count = 0; 
     140                for(pSecondChild = pChild->FirstChildElement("affect"); pSecondChild; \ 
     141                pSecondChild = pSecondChild->NextSiblingElement()) { 
     142 
    131143                        const char *oUtilName = pSecondChild->Attribute("utility"); 
    132  
    133                         // If a utility affects this parameter find the utility object and assign it 
    134144                        if(oUtilName) { 
    135                                 // Search for correct utility 
    136                                 for( k=0 ; u[k]!=NULL ; k++ ){ 
    137                                         if(u[k]->name == oUtilName) { 
    138                                                 o[i]->affection_list[j].u = u[k]; 
    139                                                 // Set relationship 
     145                                for(attribute_count = 0; u[attribute_count]! = NULL; attribute_count++ ) { 
     146                                        if(u[attribute_count]->name == oUtilName) { 
     147                                                o[i]->affection_list[affect_count].u = u[attribute_count]; 
     148 
    140149                                                const char *oRelate = pSecondChild->Attribute("relationship"); 
    141                                                 if(oRelate) o[i]->affection_list[j].relation = oRelate; 
    142                                                 j++; 
    143                                                 match_found = 1; 
     150                                                if(oRelate)  
     151                            o[item_count]->affection_list[affect_count].relation = oRelate; 
     152 
     153                                                affect_count++; 
     154                                                match_found = true; 
    144155                                                break; 
    145156                                        } 
    146157                                } 
    147158                        } 
    148                         if(!match_found) cout << "Error: " << o[i]->name << ": " << oUtilName << " not a valid utility: Affect not added." << endl;      
    149                         match_found = 0;         
     159 
     160                        if(!match_found)  
     161                ERROR(1, "Error: %s: %s is not a valid utility.\n", \ 
     162                        o[i]->name, oUtilName); 
     163            else 
     164                match_found = false;     
    150165                } 
    151                 o[i]->numAffects = j;  
    152                 i++; 
    153         } 
    154         ce_info->numObservables = i;     
    155         cout << "Initialize:: Parsed " << ce_info->numObservables << " observables." << endl; 
     166 
     167                o[item_count]->numAffects = affect_count;  
     168                item_count++; 
     169        } 
     170 
     171        ce_info->numObservables = item_count;    
     172        LOG("Initialize:: Parsed %d observables.\n", ce_info->numObservables); 
     173 
     174        pElem = hRoot.FirstChild("parameters").Element(); 
     175        pChild1 = hRoot.Child("parameters", count).Element(); 
    156176         
    157  
    158         // Pull parameter information from XML file. 
    159         pElem = hRoot.FirstChild("parameters").Element(); 
    160         pChild1 = hRoot.Child("parameters",count).Element(); 
    161          
    162         i = 0; 
    163         for(pChild = pChild1->FirstChildElement("parameter"); pChild; pChild = pChild->NextSiblingElement()) 
    164         { 
    165                 p[i] = new Parameter; 
     177        item_count = 0; 
     178        for(pChild = pChild1->FirstChildElement("parameter"); pChild; \ 
     179            pChild = pChild->NextSiblingElement()) { 
     180 
     181                p[item_count] = new Parameter; 
    166182 
    167183                const char *pName = pChild->Attribute("name"); 
    168                 if(pName) p[i]->name = pName;    
     184                if(pName)  
     185            p[item_count]->name = pName;         
     186 
    169187                const char *pUnits = pChild->Attribute("units"); 
    170                 if(pUnits) p[i]->units = pUnits; 
    171  
    172                 if(pChild->QueryFloatAttribute("min",&p[i]->min) != TIXML_SUCCESS) p[i]->min = -1; 
    173                 if(pChild->QueryFloatAttribute("max",&p[i]->max) != TIXML_SUCCESS) p[i]->max = -1; 
    174                 if(pChild->QueryFloatAttribute("step",&p[i]->step) != TIXML_SUCCESS) p[i]->step = -1; 
     188                if(pUnits)  
     189            p[item_count]->units = pUnits; 
     190 
     191                if(pChild->QueryFloatAttribute("min", &p[i]->min) != TIXML_SUCCESS)  
     192            p[item_count]->min = -1; 
     193 
     194                if(pChild->QueryFloatAttribute("max", &p[i]->max) != TIXML_SUCCESS)  
     195            p[item_count]->max = -1; 
     196 
     197                if(pChild->QueryFloatAttribute("step", &p[i]->step) != TIXML_SUCCESS)  
     198            p[item_count]->step = -1; 
    175199                 
    176                 j = 0; 
    177                 for(pSecondChild = pChild->FirstChildElement("affect"); pSecondChild; pSecondChild = pSecondChild->NextSiblingElement()) 
    178                 { 
     200                affect_count = 0; 
     201                for(pSecondChild = pChild->FirstChildElement("affect"); pSecondChild; \ 
     202                pSecondChild = pSecondChild->NextSiblingElement()) { 
     203 
    179204                        const char *pUtilName = pSecondChild->Attribute("utility"); 
    180                          
    181                         // If a utility affects this parameter find the utility object and assign it 
    182205                        if(pUtilName) { 
    183                                 // Search for correct utility 
    184                                 for( k=0 ; u[k]!=NULL ; k++ ){ 
    185                                         if(u[k]->name == pUtilName) { 
    186                                                 // If match found, assign it to this index 
    187                                                 p[i]->affection_list[j].u = u[k];        
     206                                for(attribute_count = 0; u[attribute_count] != NULL; attribute_count++) { 
     207                                        if(u[attribute_count]->name == pUtilName) { 
     208                                                p[item_count]->affection_list[affect_count].u = u[attribute_count];      
     209 
    188210                                                const char *pRelate = pSecondChild->Attribute("relationship"); 
    189                                                 if(pRelate) { 
    190                                                         p[i]->affection_list[j].relation = pRelate; 
    191                                                 } else { 
    192                                                         cout << "Error: No relation found." << endl; 
    193                                                 } 
    194                                                 match_found = 1; 
    195                                                 j++; 
     211                                                if(pRelate) 
     212                                                        p[item_count]->affection_list[affect_count].relation = pRelate; 
     213                                                else 
     214                                                        LOG("Error: No relation found.\n"); 
     215 
     216                                                match_found = true; 
     217                                                affect_count++; 
    196218                                                break; 
    197219                                        } 
    198220                                } 
    199221                        } 
    200                         if(!match_found) cout << "Error: " << p[i]->name << ": " << pUtilName << " not a valid utility: Affect not added." << endl;      
    201                         match_found = 0;         
     222 
     223                        if(!match_found) { 
     224                ERROR(1, "Error: %s: %s is not a valid utility.\n", \ 
     225                        p[item_count]->name, pUtilName); 
     226            } 
     227 
     228                        match_found = false;     
    202229                } 
    203                 p[i]->numAffects = j;  
    204                 i++; 
    205  
    206         } 
    207         ce_info->numParameters = i;      
    208         cout << "Initialize:: Parsed " << ce_info->numParameters << " parameters." << endl; 
     230 
     231                p[item_count]->numAffects = affect_count; 
     232                item_count++; 
     233        } 
     234 
     235        ce_info->numParameters = item_count; 
     236        LOG("Initialize:: Parsed %d parameters.\n", ce_info->numParameters); 
     237 
    209238        return 1; 
    210239}