Index: vtcross/trunk/src/cognitive_engines/OSSIE_DEMO_CE/OSSIE_CE.cpp
===================================================================
--- vtcross/trunk/src/cognitive_engines/OSSIE_DEMO_CE/OSSIE_CE.cpp	(revision 470)
+++ vtcross/trunk/src/cognitive_engines/OSSIE_DEMO_CE/OSSIE_CE.cpp	(revision 477)
@@ -13,4 +13,5 @@
 #include <stdint.h>
 #include <math.h>
+#include <string>
 
 #include "vtcross/cbr.h"
@@ -33,4 +34,11 @@
 
 
+/* ossieCBR Class 
+ *
+ * Derives from the default VTCROSS CBR class to add the following functionality
+ * necessary for the OSSIE demo:
+ *
+ * TODO
+ */
 class ossieCBR : public CBR
 {
@@ -38,115 +46,82 @@
         ossieCBR();
 
-        ossieCBR(char * _filename, char * _tablename, char * _cols[], unsigned int _len);
+        ossieCBR(string _filename, string _tablename, string _cols[], uint32_t _len);
 
         ~ossieCBR(){};
 
-        int32_t Update(char *_where[], char*_set[], float *_wherevals, float *_setvals, 
+        int32_t Update(string _where[], string _set[], float *_wherevals, float *_setvals, \
                 unsigned int _wherelen, unsigned int _setlen);
 
-        int32_t Search(char *_names[], int * _ops, float *_vals, unsigned int _n,
-            float *_retvals, int evf);
+        int32_t Search(string _names[], int32_t *_ops, float *_vals, uint32_t _n, \
+                float *_retvals, int32_t evf);
 };
 
 
-
-ossieCBR::ossieCBR(char * _filename, char * _tablename, char * _cols[], unsigned int _len)
-{
-    // create database
-
-    // copy filename
-    unsigned int i=0;
-    strcpy(filename, _filename);
-
-    // execute create database command
-    // database handle
-    //db = NULL;
+ossieCBR::ossieCBR(string _filename, string _tablename, string _cols[], uint32_t _len)
+{
+    /* Store database properties. */
+    filename = _filename;
+    tablename = _tablename;
+    numColumns = _len;
+
+    /* Create the database (or open it if it already exists). */
     OpenDatabase();
 
-    // create table
-
-    // copy tablename
-    strcpy(tablename, _tablename);
-
-    // number of columns in the table
-    numColumns = _len;
-
-    // generate command
-    strcpy(command, "CREATE TABLE ");
-    strcat(command, tablename);
-    strcat(command, "(");
-    for (i=0; i<numColumns; i++) {
-        strcat(command, _cols[i]);
-        strcat(command, " FLOAT");
-        if (i != numColumns-1) // not last entry
-            strcat(command, ", ");
-    }
-    strcat(command, ", timestamp DATE, PRIMARY KEY(tx_power));");
-
-    // execute create table command
+    /* Generate the command that will create the initial table within the
+     * VTCROSS database. */
+    command = "CREATE TABLE " + tablename + "(";
+    for(size_t i = 0; i < numColumns; i++) {
+        command += _cols[i] + " FLOAT";
+
+        /* If this column is not the last entry, add a comma to the command in
+         * preperation for the next entry. */
+        if(i != numColumns - 1)
+            command += ", ";
+    }
+    command += ", timestamp DATE, PRIMARY KEY(tx_power));";
+
+    /* Execute the generated command. At this point, the database is ready for
+     * use. */
     ExecuteCommand();
 }
 
 
-
-// cbr search
 int32_t 
-ossieCBR::Search(
-    char *_names[],
-    int * _ops,
-    float *_vals,
-    unsigned int _n,
-    float *_retvals,
-    int evf)
+ossieCBR::Search(string _names[], int32_t *_ops, float *_vals, uint32_t _n, \
+        float *_retvals, int32_t evf)
 {   
-    int rc;
     float lower_limit;
     float upper_limit;
-    // generate command
-    strcpy(command, "select ");
-    strcat(command, tablename);
-    strcat(command, ".* from ");
-    strcat(command, tablename);
-    strcat(command, " where ");
-
-    unsigned int i;
     char str_buffer[64];
-    //printf("number of ops %d:\n", _n);
-    for (i=0; i<_n; i++) {
-        // ensure valid ops value
-        if (_ops[i] < 0 || _ops[i] > 5) {
-            printf("error: cbr_search(), invalid ops id : %d\n", _ops[i]);
-            exit(1);
-        }
-
-        strcat(command, _names[i]);
-        strcat(command, " between ");
+
+    command = "select " + tablename + ".* from " + tablename + " where ";
+
+    for(size_t i = 0; i < _n; i++) {
+        /* Make sure that the passed ops value is valid. */
+        if((_ops[i] < 0) || (_ops[i] > 5)) {
+            ERROR(1, "Error: cbr_search(), invalid ops id : %d\n", _ops[i]);
+        }
+
+        command += _names[i] + " between ";
         
-        lower_limit = _vals[i]*(1-((float)evf/100));
-        upper_limit = _vals[i]*(1+((float)evf/100));
-
-	printf("%f %f %f\n", lower_limit, upper_limit, _vals[i]);
-
-        sprintf(command, "%s%f", command, lower_limit);
-        strcat(command, " and ");
-        sprintf(command, "%s%f", command, upper_limit);
-
-
-        if (i<_n-1)
-            strcat(command, " AND ");
+        lower_limit = _vals[i] * (1 - ((float) evf / 100));
+        upper_limit = _vals[i] * (1 + ((float) evf / 100));
+
+	    LOG("%f %f %f\n", lower_limit, upper_limit, _vals[i]);
+
+        sprintf(str_buffer, "%f", lower_limit);
+        command += string(str_buffer) + " and ";
+        sprintf(str_buffer, "%f", upper_limit);
+        command += string(str_buffer);
+
+        if(i < _n - 1)
+            command += " AND ";
         else
-            strcat(command, " order by abs(utility) asc, timestamp desc;");
-    }
-
-    printf("search command: %s\n", command);
-
-    rc = ExecuteSearchCommand(_retvals);
-    
-    printf("search result: ");
-    for (i=0; i<numColumns +1; i++)
-        printf("%f, ",_retvals[i]);
-    printf("\n");
-
-    return rc;
+            command += " order by abs(utility) asc, timestamp desc;";
+    }
+
+    LOG("Search command: %s\n", command.c_str());
+
+    return ExecuteSearchCommand(_retvals);
 }
 
@@ -154,46 +129,43 @@
 // update a row 
 int32_t 
-ossieCBR::Update(char *_where[], char*_set[], float *_wherevals, float *_setvals, 
+ossieCBR::Update(string _where[], string _set[], float *_wherevals, float *_setvals, 
 		uint32_t _wherelen, uint32_t _setlen)
 {
-    unsigned int i;
-    
-    // generate command
-    //printf("%s\n", command);
-    strcpy(command, "UPDATE ");
-    strcat(command, tablename);
-    
-    strcat(command, " SET ");
-    for (i=0; i<_setlen; i++) {
-        strcat(command, _set[i]);
-        strcat(command, " = ");
-        sprintf(command, "%s%f", command, _setvals[i]);
-        strcat(command, "  ");
-        if (i != _setlen-1) // not last entry
-            strcat(command, ", ");
+    char str_buffer[64];
+
+    command = "UPDATE " + tablename + " SET ";
+
+    for(size_t i = 0; i < _setlen; i++) {
+        command += _set[i] + " = ";
+
+        sprintf(str_buffer, "%f", _setvals[i]);
+        command += string(str_buffer) + "  ";
+
+        if(i != _setlen - 1)
+            command += ", ";
     }
    
-    strcat(command, ", timestamp = DATETIME('NOW')");
-    
-    strcat(command, " WHERE ");
-
-    for (i=0; i<_wherelen; i++) {
-        strcat(command, _where[i]);
-        strcat(command, " = ");
-        sprintf(command, "%s%f", command, _wherevals[i]);
-        strcat(command, "  ");
-        if (i != _wherelen-1) // not last entry
-            strcat(command, "AND ");
-    }
-    strcat(command, ";");
-    
-    //printf("update command: %s\n", command);
-    // execute add command
-    ExecuteCommand();
-
-    return 0;
-}
+    command += ", timestamp = DATETIME('NOW') WHERE ";
+    
+    for(size_t j = 0; j < _wherelen; j++) {
+        command += _where[j] + " = ";
+
+        sprintf(str_buffer, "%f", _wherevals[j]);
+        command += string(str_buffer) + "  ";
+
+        if(j != _wherelen - 1)
+            command += "AND ";
+    }
+    command += ";";
+    
+    return ExecuteCommand();
+}
+
+
+/******************** END DEFINITION OF OSSIE CBR CLASS ****************/
+
 
 static ossieCBR *myCBR;
+
 
 CognitiveEngine::CognitiveEngine()
@@ -270,6 +242,5 @@
    LOG("Cognitive Engine:: Receiving feedback.\n");
     
-    uint32_t numberColumns = 
-        radioInfo->numParameters;
+    uint32_t numberColumns = radioInfo->numParameters;
 
     uint32_t utilColumns = radioInfo->numUtilities + 1;
@@ -277,17 +248,17 @@
     float valList[numberColumns];
     float newUtilityVals[numberColumns];
-    char *nameList[numberColumns];
-    char *utilList[utilColumns];
+    string nameList[numberColumns];
+    string utilList[utilColumns];
 
     size_t columnUtilIndex = 0;
     for (size_t i = 0; i < radioInfo->numUtilities; i++){
-        utilList[columnUtilIndex] = (char*)uList[i].name.c_str();
+        utilList[columnUtilIndex] = uList[i].name;
         columnUtilIndex++;
     }  
-    utilList[columnUtilIndex] = (char*) "utility";
+    utilList[columnUtilIndex] = "utility";
 
     size_t columnIndex = 0;
     for (size_t i = 0; i < radioInfo->numParameters; i++){
-        nameList[columnIndex] = (char*)parameters[i].name.c_str();
+        nameList[columnIndex] = parameters[i].name;
         columnIndex++;
     }   
@@ -799,8 +770,8 @@
     LOG("Cognitive Engine:: Generating solution.\n");
 
-    char *searchNames[radioInfo->numObservables];
+    string searchNames[radioInfo->numObservables];
 
     for(size_t i = 0; i < radioInfo->numObservables; i++) {
-        searchNames[i] = (char*)observables[i].name.c_str();
+        searchNames[i] = observables[i].name;
     }
 
@@ -811,8 +782,6 @@
     }
 
-    uint32_t numberColumns = 
-        radioInfo->numUtilities +
-        radioInfo->numParameters +
-        radioInfo->numObservables + 1;
+    uint32_t numberColumns = radioInfo->numUtilities + radioInfo->numParameters + \
+                             radioInfo->numObservables + 1;
     
     float returnValues[numberColumns];
@@ -879,21 +848,21 @@
     returnValues[returnValueIndex] = 0;
 
-    char *allNames[numberColumns];
+    string allNames[numberColumns];
     size_t allNameIndex = 0;
     for(size_t i = 0; i < radioInfo->numUtilities; i++) {
-        allNames[allNameIndex] = (char*)uList[i].name.c_str();
+        allNames[allNameIndex] = uList[i].name;
         returnValues[allNameIndex] = uList[i].target;
         allNameIndex++;
     }
     for(size_t i = 0; i < radioInfo->numParameters; i++) {
-        allNames[allNameIndex] = (char*)pList[i].name.c_str();
+        allNames[allNameIndex] = pList[i].name;
         allNameIndex++;
     }
     for(size_t i = 0; i < radioInfo->numObservables; i++) {
-        allNames[allNameIndex] = (char*)oList[i].name.c_str();
+        allNames[allNameIndex] = oList[i].name;
         returnValues[allNameIndex] = observables[i].value;
         allNameIndex++;
     }
-    allNames[allNameIndex] = (char *) "utility";
+    allNames[allNameIndex] = "utility";
 
     // Add row to CBR. 
@@ -925,28 +894,26 @@
 CognitiveEngine::BuildCognitiveEngine()
 {
-    char filename[] = {"ex1"};
-    char tablename[] = {"data"};
-
-    uint32_t numberColumns = 
-        radioInfo->numUtilities +
-        radioInfo->numParameters +
-        radioInfo->numObservables + 1;
-
-    char *cols[numberColumns];
+    string filename = "ex1";
+    string tablename = "data";
+
+    uint32_t numberColumns = radioInfo->numUtilities + radioInfo->numParameters + \
+                             radioInfo->numObservables + 1;
+
+    string cols[numberColumns];
 
     size_t columnIndex = 0;
     for (size_t i = 0; i < radioInfo->numUtilities; i++){
-        cols[columnIndex] = (char*)uList[i].name.c_str();
+        cols[columnIndex] = uList[i].name;
         columnIndex++;
     }   
     for (size_t i = 0; i < radioInfo->numParameters; i++){
-        cols[columnIndex] = (char*)pList[i].name.c_str();
+        cols[columnIndex] = pList[i].name;
         columnIndex++;
     }   
     for (size_t i = 0; i < radioInfo->numObservables; i++){
-        cols[columnIndex] = (char*)oList[i].name.c_str();
+        cols[columnIndex] = oList[i].name;
         columnIndex++;
     }   
-    cols[columnIndex] = (char *)"utility";
+    cols[columnIndex] = "utility";
 
     myCBR = new ossieCBR(filename, tablename, cols, numberColumns);
