Index: vtcross/trunk/src/cognitive_engines/cbr.c
===================================================================
--- vtcross/trunk/src/cognitive_engines/cbr.c	(revision 161)
+++ vtcross/trunk/src/cognitive_engines/cbr.c	(revision 228)
@@ -60,5 +60,5 @@
     rc = sqlite3_exec(_cbr->db, _cbr->command, callback, 0, &zErrMsg);
     if( rc!=SQLITE_OK){
-        fprintf(stderr, "SQL error: %s\n", zErrMsg);
+        fprintf(stderr, "SQL error: %s: %s\n", zErrMsg,_cbr->command);
         sqlite3_free(zErrMsg);
     } else{
@@ -72,39 +72,22 @@
 int ExecuteSearchCommand(cbr _cbr, float *_retvals){
     int rc;
-    //char *zErrMsg = 0;
     unsigned int i;
 
-    //printf("command: %s\n", _cbr->command);
     sqlite3_stmt * pStatement;
     rc = sqlite3_prepare_v2(_cbr->db, _cbr->command, -1, &pStatement, NULL);
-    //if (rc == SQLITE_OK && sqlite3_step(pStatement) == SQLITE_ROW){
     if (rc == SQLITE_OK){ 
         if (sqlite3_step(pStatement) == SQLITE_ROW){
-        printf("there is search return data\n");
-        for (i=0; i<_cbr->num_columns; ++i)
-            _retvals[i] = sqlite3_column_double(pStatement, i);
+            for (i=0; i<_cbr->num_columns; ++i)
+                _retvals[i] = sqlite3_column_double(pStatement, i);
         } else {
-            printf("no matched rearch results. use default values\n");
-            
-            _retvals[0] = 100;  // throughput
-            _retvals[1] = 1;    // spectral_efficiency
-            _retvals[2] = -2;   // log10_ber
-            _retvals[3] = 1;    // mod_scheme
-            _retvals[4] = -10;  // tx_power
-            _retvals[5] = 10;   // snr
-            _retvals[6] = 0.5;  // utility
-        }
+		    printf("CBR:: No matched results returning default.\n");
+			rc=31337;
+		}
+    } else {
+		printf("CBR:: Error executing SQL statement. rc = %i\n%s\n",rc,_cbr->command);
     }
 
     sqlite3_finalize(pStatement);
-    
-    /*rc = sqlite3_exec(_cbr->db, _cbr->command, SearchCallback, 0, &zErrMsg);
-    if( rc!=SQLITE_OK){
-        fprintf(stderr, "SQL error: %s\n", zErrMsg);
-        sqlite3_free(zErrMsg);
-    } else{
-        printf("command executed.\n");
-    }*/
-
+   
     return rc;
 }
@@ -185,5 +168,5 @@
     // execute print (select all)  command
     ExecuteCommand(_cbr);
-    //printf("database %s, table %s:\n", _cbr->filename, _cbr->tablename);
+    printf("database %s, table %s:\n", _cbr->filename, _cbr->tablename);
 }
 
@@ -209,4 +192,5 @@
     float *_retvals)
 {   
+    int rc;
     // generate command
     strcpy(_cbr->command, "select ");
@@ -231,4 +215,5 @@
         strcat(_cbr->command, str_buffer);
 
+
         if (i<_n-1)
             strcat(_cbr->command, " AND ");
@@ -237,8 +222,8 @@
     }
 
-    //printf("search command: %s\n", _cbr->command);
+    printf("search command: %s\n", _cbr->command);
 
     //ExecuteCommand(_cbr);
-    ExecuteSearchCommand(_cbr, _retvals);
+    rc = ExecuteSearchCommand(_cbr, _retvals);
     
     /*printf("search result: ");
@@ -247,5 +232,5 @@
     printf("\n");*/
 
-    return 0;
+    return rc;
 }
 
Index: vtcross/trunk/src/cognitive_engines/CognitiveEngine.cpp
===================================================================
--- vtcross/trunk/src/cognitive_engines/CognitiveEngine.cpp	(revision 224)
+++ vtcross/trunk/src/cognitive_engines/CognitiveEngine.cpp	(revision 228)
@@ -19,5 +19,14 @@
 #include "vtcross/error.h"
 #include "vtcross/socketcomm.h"
-
+#include "vtcross/cbr.h"
+
+#include "cbr.c"
+
+#include <sqlite3.h>
+#include <sqlite3ext.h>
+
+
+
+static cbr myCBR;
 
 CognitiveEngine::CognitiveEngine()
@@ -31,4 +40,5 @@
 CognitiveEngine::~CognitiveEngine()
 {
+	cbr_free(myCBR);
     delete [] pList;
     delete [] oList;
@@ -82,5 +92,4 @@
     }
 }
-
 
 void 
@@ -101,5 +110,5 @@
         // MUCH faster since instead of donig string compares we can simply
         // switch on the integer value...
-        if(strcmp(buffer, "get_solution") == 0) {
+        if(strcmp(buffer, "request_optimization") == 0) {
             
             /* Receive Set of Observables */
@@ -122,15 +131,44 @@
             }  
 
+            /* Receive Set of current Parameters */
+			LOG("Cognitive Engine:: Receiving Current Transmission Parameters\n");
+
+            memset(buffer, 0, 256);
+            ReadMessage(commandSocketFD,buffer);
+            uint32_t numCurrentParameters = atoi(buffer);
+   
+            Parameter *cp = new Parameter[numCurrentParameters];
+ 
+            for(size_t i = 0; i < numCurrentParameters; i++) {
+                memset(buffer, 0, 256);
+                ReadMessage(commandSocketFD, buffer);
+                cp[i].name = std::string(buffer);
+    
+                memset(buffer, 0, 256);
+                ReadMessage(commandSocketFD, buffer);
+                cp[i].value = atof(buffer);
+            }  
 			LOG("Cognitive Engine:: Processing parameters....\n");
+
+            Parameter *solutionSet;
+			
+			solutionSet = GetSolution(o,cp);
 
             // TODO need to actually do something with the observables here
             
 			LOG("Cognitive Engine:: Sending Optimal Parameters to Application.\n");
-
-            SendMessage(commandSocketFD,"1");
-            SendMessage(commandSocketFD,"txPower");
-            SendMessage(commandSocketFD,"10");
+			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());
+                memset(solutionValue, 0, 50);
+			    sprintf(solutionValue,"%f",solutionSet[i].value);
+                SendMessage(commandSocketFD,solutionValue);
+			}
 
             delete [] o;
+            delete [] cp;
         }
         else if(strcmp(buffer, "query_component_type") == 0) {
@@ -295,5 +333,5 @@
         ReadMessage(commandSocketFD,buffer);
         uList[i].name = std::string(buffer);
-    
+   
         memset(buffer, 0, 256);
         ReadMessage(commandSocketFD,buffer);
@@ -386,4 +424,6 @@
 
     SendMessage(commandSocketFD, "receive_config_ack");
+
+	BuildCognitiveEngine();
 }
 
@@ -405,15 +445,91 @@
 }
 
-void 
-CognitiveEngine::GetSolution(Observable *observables)
+Parameter* 
+CognitiveEngine::GetSolution(Observable *observables, Parameter *currentParameters)
 {
     LOG("Cognitive Engine:: Generating solution.\n");
-}
-
-void 
-CognitiveEngine::GetSolution(Observable *observables, std::string service)
+
+    char *searchNames[radioInfo->numUtilities];
+
+    for(size_t i = 0; i < radioInfo->numUtilities; i++) {
+        searchNames[i] = (char*)uList[i].name.c_str();
+	}
+
+    float searchVals[radioInfo->numUtilities];
+
+	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];
+    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){
+        /* Adapt the returned parameters to meet the objective */
+
+    } else if(rc == 31337){
+		/* 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 {
+        WARNING("Cognitive Engine:: Search return an invalid value.\n");
+	}
+
+	size_t returnValueIndex = 0;
+    for(size_t i = 0; i < radioInfo->numUtilities; i++) {
+		uList[i].value = returnValues[returnValueIndex];
+		returnValueIndex++;
+	}
+    for(size_t i = 0; i < radioInfo->numParameters; i++) {
+		pList[i].value = returnValues[returnValueIndex];
+		returnValueIndex++;
+	}
+    for(size_t i = 0; i < radioInfo->numObservables; i++) {
+		oList[i].value = returnValues[returnValueIndex];
+		returnValueIndex++;
+	}
+
+	// Add row to CBR. 
+
+	return pList;
+
+}
+
+
+Parameter* 
+CognitiveEngine::GetSolution(Observable *observables, Parameter *currentParameters, std::string service)
 {
     LOG("Cognitive Engine:: Generating solution for %s service.\n",service.c_str());
-}
+
+    return pList;
+}
+
 
 void 
@@ -424,4 +540,5 @@
 }
 
+
 void 
 CognitiveEngine::ReceiveFeedback(Observable *observables, Parameter *parameters, \
@@ -430,2 +547,35 @@
     LOG("Cognitive Engine:: Receiving feedback.\n");
 }
+
+
+void
+CognitiveEngine::BuildCognitiveEngine()
+{
+	char filename[] = {"ex1"};
+	char tablename[] = {"data"};
+
+    uint32_t numberColumns = 
+		radioInfo->numUtilities +
+		radioInfo->numParameters +
+		radioInfo->numObservables + 1;
+
+    char *cols[numberColumns];
+
+    size_t columnIndex = 0;
+    for (size_t i = 0; i < radioInfo->numUtilities; i++){
+		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();
+        columnIndex++;
+    }   
+    for (size_t i = 0; i < radioInfo->numObservables; i++){
+		cols[columnIndex] = (char*)oList[i].name.c_str();
+        columnIndex++;
+    }   
+    cols[columnIndex] = "utility";
+
+    myCBR = cbr_create(filename, tablename, cols, numberColumns);
+}
+
