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);
+}
+
Index: /vtcross/trunk/src/include/vtcross/libvtcross.h
===================================================================
--- /vtcross/trunk/src/include/vtcross/libvtcross.h	(revision 222)
+++ /vtcross/trunk/src/include/vtcross/libvtcross.h	(revision 228)
@@ -76,5 +76,6 @@
  * use this?  Shouldn't this be internal to the radio operation? 
  */
-Parameter* GetOptimalParameters(Observable *radioObservables, uint32_t numObservables);
+Parameter* GetOptimalParameters(Observable *radioObservables, uint32_t numObservables, 
+		Parameter *currentParameters, uint32_t numCurrentParameters);
 
 
@@ -103,3 +104,8 @@
 bool Shutdown();
 
+/* Return total number of currently recognized transmission parameters.
+ */
+uint32_t GetNumParameters();
+
+
 #endif
Index: /vtcross/trunk/src/include/vtcross/components.h
===================================================================
--- /vtcross/trunk/src/include/vtcross/components.h	(revision 222)
+++ /vtcross/trunk/src/include/vtcross/components.h	(revision 228)
@@ -260,6 +260,6 @@
          * and active.
          */
-        void GetSolution(Observable *observables);
-        void GetSolution(Observable *observables, std::string service);
+        Parameter *GetSolution(Observable *observables, Parameter *currentParameters);
+        Parameter *GetSolution(Observable *observables, Parameter *currentParameters, std::string service);
 
         /* Receive a feedback from the radio regarding the performance of a
@@ -275,4 +275,13 @@
         void ReceiveFeedback(Observable *observables, \
                 Parameter *parameters, Utility *utilities, std::string service);
+
+
+		/* BuildCognitiveEngine performs the CE implementation specific work
+		 * that defines the internals of a CE.  For example, a CBR CE engine
+		 * would build the case-base reasoner or create the database, a neural
+		 * network based CE may perform the initial training, a GA based CE
+		 * may build the chromosome structure.
+		 */
+		void BuildCognitiveEngine();
 
         /* The SML_present bool reflects whether or not the remote component
Index: /vtcross/trunk/src/shell/CognitiveRadioShell.cpp
===================================================================
--- /vtcross/trunk/src/shell/CognitiveRadioShell.cpp	(revision 227)
+++ /vtcross/trunk/src/shell/CognitiveRadioShell.cpp	(revision 228)
@@ -454,4 +454,5 @@
 {
     char buffer[256];
+    uint32_t numObservables,numParameters,numCurrentParameters;
     char counter[55];
     char var[50];
@@ -476,4 +477,21 @@
     }
 
+    /* Receive Set of Current Parameters */
+    memset(buffer, 0, 256);
+    ReadMessage(commandSocketFD,buffer);
+    numCurrentParameters = atoi(buffer);
+  
+	LOG("Cognitive Radio Shell:: Attempting to get %i parameters.\n",numCurrentParameters);
+    Parameter * cp = new Parameter[numCurrentParameters];
+ 
+    for (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);
+    }
     /* Send to Cognitive Engine 
 	 * TODO: With multiple CEs we need to make a decision about where
@@ -481,14 +499,24 @@
 	 */
 
-	LOG("Cognitive Radio Shell:: Passing on observables to Cognitive Engine\n");
-    SendMessage(ceSocketFD, "get_solution");
-    sprintf(counter, "%i", numObservables);
-    SendMessage(ceSocketFD, counter);
-    for(size_t i = 0; i < numObservables; i++) {
-        SendMessage(ceSocketFD, o[i].name.c_str());
-        sprintf(var, "%f", o[i].value);
-	    SendMessage(ceSocketFD, var);
-    }
-
+	if(!SML_present) { 
+	    LOG("Cognitive Radio Shell:: Passing on observables to Cognitive Engine\n");
+        SendMessage(ceSocketFD,"request_optimization");
+        sprintf(counter,"%i",numObservables);
+        SendMessage(ceSocketFD,counter);
+        for(i = 0; i < numObservables; i++) {
+            SendMessage(ceSocketFD,o[i].name.c_str());
+            sprintf(var,"%f",o[i].value);
+	        SendMessage(ceSocketFD,var);
+        }
+	    
+		LOG("Cognitive Radio Shell:: Passing on current parameters to Cognitive Engine\n");
+        sprintf(counter,"%i",numCurrentParameters);
+        SendMessage(ceSocketFD,counter);
+        for(i = 0; i < numCurrentParameters; i++) {
+            SendMessage(ceSocketFD,cp[i].name.c_str());
+            sprintf(var,"%f",cp[i].value);
+	        SendMessage(ceSocketFD,var);
+        }
+	}
 
 	LOG("Cognitive Radio Shell:: Receiving optimized parameters from Cognitive Engine.\n");
@@ -546,7 +574,14 @@
     } else if(strcmp(buffer, "deregister_sml") == 0) {
         DeregisterSML(socketFD);
-    } else if(strcmp(buffer, "request_optimization") == 0) {
+    } else if(strcmp(buffer,"get_number_parameters") == 0) {
+		char numParameters[20];
+		sprintf(numParameters,"%i",radio_info->numParameters);
+        SendMessage(commandSocketFD,numParameters);
+    } else if(strcmp(buffer,"request_optimization") == 0) {
         /* Receive optimization request and current environment */
         GetOptimalParameters(socketFD);  
+    } else if(strcmp(buffer,"request_optimization_service") == 0) {
+        /* Receive optimization request and current environment */
+        //GetOptimalParametersService(socketFD);  
     }
 }
Index: /vtcross/trunk/src/libvtcross/libvtcross_demo.cpp
===================================================================
--- /vtcross/trunk/src/libvtcross/libvtcross_demo.cpp	(revision 225)
+++ /vtcross/trunk/src/libvtcross/libvtcross_demo.cpp	(revision 228)
@@ -23,16 +23,35 @@
 main(int32_t argc, char *argv[])
 {
-    Observable * o = new Observable[1];
+    Observable *o = new Observable[1];
+    Parameter *currentParameters = new Parameter[2];
     Parameter * p;
 
-    o[0].name = "Test123";
-	o[0].value = 1243.00;
+	uint32_t numParameters;
 
-    p = GetOptimalParameters(o,1);
+    o[0].name = "PER";
+	o[0].value = 12.00;
+
+    currentParameters[0].name = "tx_power";
+    currentParameters[0].value = 10.0;
+    currentParameters[1].name = "bandwidth";
+    currentParameters[1].value = 300.0;
+
+    p = GetOptimalParameters(o,1,currentParameters,2);
+    numParameters = GetNumParameters();
 
 	printf("Application:: Received the following parameters.\n");
-	printf("%s:: %f\n",p[0].name.c_str(),p[0].value);
+	
+	for(size_t i = 0; i < numParameters; i++) {
+		printf("%s:: %f\n",p[i].name.c_str(),p[i].value);
+	}
 
-    delete [] p; 
+	// Try them out! Do they work?
+    
+    Utility *resultingUtility = new Utility[2];
+	
+	// UpdateParameterPerformance(p,o,resultingUtility);	
+	
+	
+	delete [] p; 
     delete [] o; 
 
Index: /vtcross/trunk/src/libvtcross/libvtcross.cpp
===================================================================
--- /vtcross/trunk/src/libvtcross/libvtcross.cpp	(revision 227)
+++ /vtcross/trunk/src/libvtcross/libvtcross.cpp	(revision 228)
@@ -27,4 +27,19 @@
 }
 
+uint32_t
+GetNumParameters()
+{
+	uint32_t socketfd, numParameters;
+    char buffer[256];
+
+    socketfd = ConnectToRemoteComponent();
+    SendMessage(socketfd,"get_number_parameters");
+    
+    memset(buffer, 0, 256);
+    ReadMessage(socketfd, buffer);
+    numParameters = atoi(buffer);
+	
+	return numParameters; 
+}
 
 /* Given a certain set of observables, ask the radio to find the optimum radio
@@ -38,7 +53,7 @@
  * memory when it is done using it.
  */
-Parameter* 
-GetOptimalParameters(Observable *radioObservables, uint32_t numObservables)
-{
+Parameter* GetOptimalParameters(Observable *radioObservables, uint32_t numObservables,
+		Parameter *currentParameters, uint32_t numCurrentParameters) {
+    uint32_t i,socketfd,numParameters;
     char var[50];
     char counter[55];
@@ -61,4 +76,13 @@
     }
 
+    // Send Observables
+    memset(counter, 0, 55);
+    sprintf(counter,"%i",numCurrentParameters);
+    SendMessage(socketfd,counter);
+    for(i = 0; i < numCurrentParameters; i++) {
+        SendMessage(socketfd,currentParameters[i].name.c_str());
+        sprintf(var,"%f",currentParameters[i].value);
+        SendMessage(socketfd,var);	
+    }
     /* Receive Set of Parameters */
     memset(buffer, 0, 256);
