Index: vtcross/trunk/src/cognitive_engines/CognitiveEngine.cpp
===================================================================
--- vtcross/trunk/src/cognitive_engines/CognitiveEngine.cpp	(revision 230)
+++ vtcross/trunk/src/cognitive_engines/CognitiveEngine.cpp	(revision 231)
@@ -22,4 +22,5 @@
 #include "vtcross/cbr.h"
 
+// TODO this is really bad; need to move to a proper cbr.h
 #include "cbr.c"
 
@@ -28,6 +29,6 @@
 
 
-
 static cbr myCBR;
+
 
 CognitiveEngine::CognitiveEngine()
@@ -41,5 +42,5 @@
 CognitiveEngine::~CognitiveEngine()
 {
-	cbr_free(myCBR);
+    cbr_free(myCBR);
     delete [] pList;
     delete [] oList;
@@ -49,5 +50,5 @@
 
 
-CognitiveEngine::CognitiveEngine(const char* serverName, const char* serverPort, \
+CognitiveEngine::CognitiveEngine(const char *serverName, const char *serverPort, \
         const bool SML)
 {
@@ -72,6 +73,6 @@
 
 void
-CognitiveEngine::ConnectToRemoteComponent(const char* serverName, \
-        const char* serverPort, const bool SML)
+CognitiveEngine::ConnectToRemoteComponent(const char *serverName, \
+        const char *serverPort, const bool SML)
 {
     commandSocketFD = ClientSocket(serverName, serverPort);
@@ -98,13 +99,13 @@
 { 
    LOG("Cognitive Engine:: Receiving feedback.\n");
-	
-	uint32_t numberColumns = 
-		radioInfo->numParameters +
-		radioInfo->numUtilities;
-
-	uint32_t obsColumns = radioInfo->numObservables + 1;
-
-	float valList[numberColumns];
-	float obsVals[numberColumns];
+    
+    uint32_t numberColumns = 
+        radioInfo->numParameters +
+        radioInfo->numUtilities;
+
+    uint32_t obsColumns = radioInfo->numObservables + 1;
+
+    float valList[numberColumns];
+    float obsVals[numberColumns];
     char *nameList[numberColumns];
     char *obsList[obsColumns];
@@ -112,5 +113,5 @@
     size_t columnObsIndex = 0;
     for (size_t i = 0; i < radioInfo->numObservables; i++){
-		obsList[columnObsIndex] = (char*)observables[i].name.c_str();
+        obsList[columnObsIndex] = (char*)observables[i].name.c_str();
         columnObsIndex++;
     }  
@@ -119,38 +120,38 @@
     size_t columnIndex = 0;
     for (size_t i = 0; i < radioInfo->numParameters; i++){
-		nameList[columnIndex] = (char*)parameters[i].name.c_str();
+        nameList[columnIndex] = (char*)parameters[i].name.c_str();
         columnIndex++;
     }   
     for (size_t i = 0; i < radioInfo->numUtilities; i++){
-		nameList[columnIndex] = (char*)uList[i].name.c_str();
+        nameList[columnIndex] = (char*)uList[i].name.c_str();
         columnIndex++;
     }   
 
-	size_t obsValueIndex = 0;
+    size_t obsValueIndex = 0;
     for(size_t i = 0; i < radioInfo->numObservables; i++) {
-		obsVals[obsValueIndex] = observables[i].value;
-		obsValueIndex++;
-	}
-	// Calculate Utility
-	float newUtilityValue = 0;
+        obsVals[obsValueIndex] = observables[i].value;
+        obsValueIndex++;
+    }
+
+    /* Calculate Utility */
+    float newUtilityValue = 0;
 
     for(size_t i = 0; i < radioInfo->numUtilities; i++) {
-		newUtilityValue = newUtilityValue + (uList[i].target - observables[i].value);
-	}
-	obsVals[obsValueIndex] = newUtilityValue;
-
-	size_t returnValueIndex = 0;
+        newUtilityValue = newUtilityValue + (uList[i].target - observables[i].value);
+    }
+    obsVals[obsValueIndex] = newUtilityValue;
+
+    size_t returnValueIndex = 0;
     for(size_t i = 0; i < radioInfo->numParameters; i++) {
-		valList[returnValueIndex] = parameters[i].value;
-		returnValueIndex++;
-	}
+        valList[returnValueIndex] = parameters[i].value;
+        returnValueIndex++;
+    }
     for(size_t i = 0; i < radioInfo->numUtilities; i++) {
-		valList[returnValueIndex] = uList[i].target;
-		returnValueIndex++;
-	}
-
-
-	cbr_update(myCBR, nameList, obsList, valList, obsVals, 
-			numberColumns, obsColumns);
+        valList[returnValueIndex] = uList[i].target;
+        returnValueIndex++;
+    }
+
+    cbr_update(myCBR, nameList, obsList, valList, obsVals, 
+            numberColumns, obsColumns);
 }
 
@@ -177,5 +178,5 @@
             /* Receive Set of current Parameters */
             memset(buffer, 0, 256);
-            ReadMessage(commandSocketFD,buffer);
+            ReadMessage(commandSocketFD, buffer);
             uint32_t numParameters = atoi(buffer);
    
@@ -193,4 +194,30 @@
 
             /* Receive Set of Observables */
+            memset(buffer, 0, 256);
+            ReadMessage(commandSocketFD, buffer);
+            uint32_t numObservables = atoi(buffer);
+   
+            Observable *o = new Observable[numObservables];
+ 
+            for(size_t i = 0; i < numObservables; i++) {
+                memset(buffer, 0, 256);
+                ReadMessage(commandSocketFD, buffer);
+                o[i].name = std::string(buffer);
+    
+                memset(buffer, 0, 256);
+                ReadMessage(commandSocketFD, buffer);
+                o[i].value = atof(buffer);
+            }  
+
+            ReceiveFeedback(o,p);
+
+            delete [] o;
+            delete [] p;
+        } 
+        else if(strcmp(buffer, "request_optimization") == 0) {
+            
+            /* Receive Set of Observables */
+            LOG("\nCognitive Engine:: Receiving Observable Parameters\n");
+
             memset(buffer, 0, 256);
             ReadMessage(commandSocketFD,buffer);
@@ -209,35 +236,9 @@
             }  
 
-			ReceiveFeedback(o,p);
-
-            delete [] o;
-            delete [] p;
-		} 
-		else if(strcmp(buffer, "request_optimization") == 0) {
-            
-            /* Receive Set of Observables */
-			LOG("\nCognitive Engine:: Receiving Observable Parameters\n");
+            /* Receive Set of current Parameters */
+            LOG("Cognitive Engine:: Receiving Current Transmission Parameters\n");
 
             memset(buffer, 0, 256);
-            ReadMessage(commandSocketFD,buffer);
-            uint32_t numObservables = atoi(buffer);
-   
-            Observable *o = new Observable[numObservables];
- 
-            for(size_t i = 0; i < numObservables; i++) {
-                memset(buffer, 0, 256);
-                ReadMessage(commandSocketFD, buffer);
-                o[i].name = std::string(buffer);
-    
-                memset(buffer, 0, 256);
-                ReadMessage(commandSocketFD, buffer);
-                o[i].value = atof(buffer);
-            }  
-
-            /* Receive Set of current Parameters */
-			LOG("Cognitive Engine:: Receiving Current Transmission Parameters\n");
-
-            memset(buffer, 0, 256);
-            ReadMessage(commandSocketFD,buffer);
+            ReadMessage(commandSocketFD, buffer);
             uint32_t numCurrentParameters = atoi(buffer);
    
@@ -253,23 +254,23 @@
                 cp[i].value = atof(buffer);
             }  
-			LOG("Cognitive Engine:: Processing parameters....\n");
+            LOG("Cognitive Engine:: Processing parameters....\n");
 
             Parameter *solutionSet;
-			
-			solutionSet = GetSolution(o,cp);
+            
+            solutionSet = GetSolution(o,cp);
 
             // TODO need to actually do something with the observables here
            
-			LOG("Cognitive Engine:: Sending Optimal Parameters to Application.\n");
-			char numParametersChar[10];
-			char solutionValue[50];
-			sprintf(numParametersChar,"%i",radioInfo->numParameters);
-			SendMessage(commandSocketFD,numParametersChar);
+            LOG("Cognitive Engine:: Sending Optimal Parameters to Application.\n");
+            char numParametersChar[10];
+            char solutionValue[50];
+            sprintf(numParametersChar, "%i", radioInfo->numParameters);
+            SendMessage(commandSocketFD, numParametersChar);
             for(size_t i = 0; i < radioInfo->numParameters; i++) {
-                SendMessage(commandSocketFD,solutionSet[i].name.c_str());
+                SendMessage(commandSocketFD, solutionSet[i].name.c_str());
                 memset(solutionValue, 0, 50);
-			    sprintf(solutionValue,"%f",solutionSet[i].value);
-                SendMessage(commandSocketFD,solutionValue);
-			}
+                sprintf(solutionValue, "%f", solutionSet[i].value);
+                SendMessage(commandSocketFD, solutionValue);
+            }
 
             delete [] o;
@@ -430,22 +431,22 @@
     /* Receive Set of Utilities */
     memset(buffer, 0, 256);
-    ReadMessage(commandSocketFD,buffer);
+    ReadMessage(commandSocketFD, buffer);
     radioInfo->numUtilities = atoi(buffer);
     
     for(size_t i = 0; i < radioInfo->numUtilities; i++) {
         memset(buffer, 0, 256);
-        ReadMessage(commandSocketFD,buffer);
+        ReadMessage(commandSocketFD, buffer);
         uList[i].name = std::string(buffer);
    
         memset(buffer, 0, 256);
-        ReadMessage(commandSocketFD,buffer);
+        ReadMessage(commandSocketFD, buffer);
         uList[i].units = std::string(buffer);
 
         memset(buffer, 0, 256);
-        ReadMessage(commandSocketFD,buffer);
+        ReadMessage(commandSocketFD, buffer);
         uList[i].goal = std::string(buffer);
     
         memset(buffer, 0, 256);
-        ReadMessage(commandSocketFD,buffer);
+        ReadMessage(commandSocketFD, buffer);
         uList[i].target = atof(buffer);
     }
@@ -484,6 +485,7 @@
             memset(buffer, 0, 256);
             ReadMessage(commandSocketFD,buffer);
+            // TODO for + if{break} = while?
             for(size_t k = 0; k < radioInfo->numUtilities; k++) {
-	        if(uList[k].name == std::string(buffer)){    
+                if(uList[k].name == std::string(buffer)) {    
                     pList[i].affection_list[j].u = &uList[k];   
                     break;
@@ -492,5 +494,5 @@
 
             memset(buffer, 0, 256);
-            ReadMessage(commandSocketFD,buffer);
+            ReadMessage(commandSocketFD, buffer);
             pList[i].affection_list[j].relation = std::string(buffer);   
         }
@@ -499,21 +501,22 @@
     /* Receive Set of Observables */
     memset(buffer, 0, 256);
-    ReadMessage(commandSocketFD,buffer);
+    ReadMessage(commandSocketFD, buffer);
     radioInfo->numObservables = atoi(buffer);
     
     for(size_t i = 0; i < radioInfo->numObservables; i++) {
         memset(buffer, 0, 256);
-        ReadMessage(commandSocketFD,buffer);
+        ReadMessage(commandSocketFD, buffer);
         oList[i].name = std::string(buffer);
     
         memset(buffer, 0, 256);
-        ReadMessage(commandSocketFD,buffer);
+        ReadMessage(commandSocketFD, buffer);
         oList[i].numAffects = atoi(buffer);
     
         for(size_t j = 0; j < oList[i].numAffects; j++) {
             memset(buffer, 0, 256);
-            ReadMessage(commandSocketFD,buffer);
+            ReadMessage(commandSocketFD, buffer);
+            // TODO for + if{break} = while?
             for(size_t k = 0; k < radioInfo->numUtilities; k++) {
-	        if(uList[k].name == std::string(buffer)){    
+                if(uList[k].name == std::string(buffer)){    
                     oList[i].affection_list[j].u = &uList[k];   
                     break;
@@ -522,5 +525,5 @@
  
             memset(buffer, 0, 256);
-            ReadMessage(commandSocketFD,buffer);
+            ReadMessage(commandSocketFD, buffer);
             oList[i].affection_list[j].relation = std::string(buffer);   
         }
@@ -529,5 +532,5 @@
     SendMessage(commandSocketFD, "receive_config_ack");
 
-	BuildCognitiveEngine();
+    BuildCognitiveEngine();
 }
 
@@ -541,8 +544,8 @@
     /* Receive number of experience entries */
     memset(buffer, 0, 256);
-    ReadMessage(commandSocketFD,buffer);
+    ReadMessage(commandSocketFD, buffer);
     numberExp = atoi(buffer);
 
-    LOG("Cognitive Engine:: Waiting for %i number of entries.\n",numberExp);
+    LOG("Cognitive Engine:: Waiting for %i number of entries.\n", numberExp);
  
     SendMessage(commandSocketFD, "receive_exp_ack");
@@ -558,109 +561,107 @@
     for(size_t i = 0; i < radioInfo->numUtilities; i++) {
         searchNames[i] = (char*)observables[i].name.c_str();
-	}
+    }
 
     float searchVals[radioInfo->numUtilities];
 
-	for(size_t i = 0; i < radioInfo->numUtilities; i++) {
-		searchVals[i] = uList[i].target;
+    for(size_t i = 0; i < radioInfo->numUtilities; i++) {
+        searchVals[i] = uList[i].target;
     }
 
     uint32_t numberColumns = 
-		radioInfo->numUtilities +
-		radioInfo->numParameters +
-		radioInfo->numObservables + 1;
-	
-	float returnValues[numberColumns];
-	
-	int searchOps[radioInfo->numUtilities];
+        radioInfo->numUtilities +
+        radioInfo->numParameters +
+        radioInfo->numObservables + 1;
+    
+    float returnValues[numberColumns];
+    
+    int searchOps[radioInfo->numUtilities];
     for(size_t i = 0; i < radioInfo->numUtilities; i++) {
 
         /* If the goal is to maximum, set the search operation to
-		 * return values greater than the target.
-		 *
-		 * If the goal is to minimize, set the search operation to
-		 * return values less than the target.
-		 */
-
-		if(strcmp(uList[i].goal.c_str(),"max") == 0) {
-		    searchOps[i] = GT;
-		} else if(strcmp(uList[i].goal.c_str(),"min") == 0) {
-			searchOps[i] = LT;
-		}
-	}
-
-	/* CBR specific call */
-	uint32_t rc = cbr_search(myCBR, searchNames, searchOps, searchVals,
-			radioInfo->numUtilities, returnValues);
-
-	if(rc == 0){
+         * return values greater than the target.
+         *
+         * If the goal is to minimize, set the search operation to
+         * return values less than the target.
+         */
+        if(strcmp(uList[i].goal.c_str(), "max") == 0) {
+            searchOps[i] = GT;
+        } else if(strcmp(uList[i].goal.c_str(), "min") == 0) {
+            searchOps[i] = LT;
+        }
+    }
+
+    /* CBR specific call */
+    uint32_t rc = cbr_search(myCBR, searchNames, searchOps, searchVals,
+            radioInfo->numUtilities, returnValues);
+
+    if(rc == 0){
         /* Adapt the returned parameters to meet the objective */
-	    
         WARNING("Cognitive Engine:: Found\n");
 
-		// Should do a random adaptation.. 
-		if(returnValues[numberColumns-1] < 0) {
-			returnValues[2] = returnValues[2] - 15;
-			returnValues[3] = returnValues[3] - 2;
+        /* Should do a random adaptation.. */
+        if(returnValues[numberColumns-1] < 0) {
+            returnValues[2] = returnValues[2] - 15;
+            returnValues[3] = returnValues[3] - 2;
         } else {
-			returnValues[2] = returnValues[2] + 15;
-			returnValues[3] = returnValues[3] + 2;
-		}
-    } else if(rc == 31337){
+            returnValues[2] = returnValues[2] + 15;
+            returnValues[3] = returnValues[3] + 2;
+        }
+    } else if(rc == 31337) {
         WARNING("Cognitive Engine:: Not Found.\n");
-		/* No rows in the CBR, pick default parameters */
-		// Currently this is hard coded and implementation specific!
-	    returnValues[2] = currentParameters[0].value + 5;
-	    returnValues[3] = currentParameters[1].value + 10;
+        /* No rows in the CBR, pick default parameters */
+        /* Currently this is hard coded and implementation specific! */
+        returnValues[2] = currentParameters[0].value + 5;
+        returnValues[3] = currentParameters[1].value + 10;
         
-	} else {
+    } else {
         WARNING("Cognitive Engine:: Search return an invalid value.\n");
-	}
-
-	size_t returnValueIndex = 0;
+    }
+
+    size_t returnValueIndex = 0;
     for(size_t i = 0; i < radioInfo->numUtilities; i++) {
-		uList[i].value = returnValues[returnValueIndex];
-		returnValueIndex++;
-	}
+        uList[i].value = returnValues[returnValueIndex];
+        returnValueIndex++;
+    }
     for(size_t i = 0; i < radioInfo->numParameters; i++) {
-		pList[i].value = returnValues[returnValueIndex];
-		returnValueIndex++;
-	}
+        pList[i].value = returnValues[returnValueIndex];
+        returnValueIndex++;
+    }
     for(size_t i = 0; i < radioInfo->numObservables; i++) {
-		oList[i].value = returnValues[returnValueIndex];
-		returnValueIndex++;
-	}
+        oList[i].value = returnValues[returnValueIndex];
+        returnValueIndex++;
+    }
     returnValues[returnValueIndex] = 0;
 
     char *allNames[numberColumns];
-	size_t allNameIndex = 0;
+    size_t allNameIndex = 0;
     for(size_t i = 0; i < radioInfo->numUtilities; i++) {
         allNames[allNameIndex] = (char*)uList[i].name.c_str();
-		returnValues[allNameIndex] = uList[i].target;
-		allNameIndex++;
-	}
+        returnValues[allNameIndex] = uList[i].target;
+        allNameIndex++;
+    }
     for(size_t i = 0; i < radioInfo->numParameters; i++) {
         allNames[allNameIndex] = (char*)pList[i].name.c_str();
-		allNameIndex++;
-	}
+        allNameIndex++;
+    }
     for(size_t i = 0; i < radioInfo->numObservables; i++) {
         allNames[allNameIndex] = (char*)oList[i].name.c_str();
-		returnValues[allNameIndex] = 0;
-		allNameIndex++;
-	}
+        returnValues[allNameIndex] = 0;
+        allNameIndex++;
+    }
     allNames[allNameIndex] = "utility";
 
-	// Add row to CBR. 
-	cbr_add_row(myCBR, allNames, returnValues, returnValueIndex+1);
-
-	return pList;
-
+    // Add row to CBR. 
+    cbr_add_row(myCBR, allNames, returnValues, returnValueIndex+1);
+
+    return pList;
 }
 
 
 Parameter* 
-CognitiveEngine::GetSolution(Observable *observables, Parameter *currentParameters, std::string service)
-{
-    LOG("Cognitive Engine:: Generating solution for %s service.\n",service.c_str());
+CognitiveEngine::GetSolution(Observable *observables, \
+        Parameter *currentParameters, std::string service)
+{
+    LOG("Cognitive Engine:: Generating solution for %s service.\n", service.c_str());
 
     return pList;
@@ -679,11 +680,11 @@
 CognitiveEngine::BuildCognitiveEngine()
 {
-	char filename[] = {"ex1"};
-	char tablename[] = {"data"};
+    char filename[] = {"ex1"};
+    char tablename[] = {"data"};
 
     uint32_t numberColumns = 
-		radioInfo->numUtilities +
-		radioInfo->numParameters +
-		radioInfo->numObservables + 1;
+        radioInfo->numUtilities +
+        radioInfo->numParameters +
+        radioInfo->numObservables + 1;
 
     char *cols[numberColumns];
@@ -691,13 +692,13 @@
     size_t columnIndex = 0;
     for (size_t i = 0; i < radioInfo->numUtilities; i++){
-		cols[columnIndex] = (char*)uList[i].name.c_str();
+        cols[columnIndex] = (char*)uList[i].name.c_str();
         columnIndex++;
     }   
     for (size_t i = 0; i < radioInfo->numParameters; i++){
-		cols[columnIndex] = (char*)pList[i].name.c_str();
+        cols[columnIndex] = (char*)pList[i].name.c_str();
         columnIndex++;
     }   
     for (size_t i = 0; i < radioInfo->numObservables; i++){
-		cols[columnIndex] = (char*)oList[i].name.c_str();
+        cols[columnIndex] = (char*)oList[i].name.c_str();
         columnIndex++;
     }   
