Index: vtcross/trunk/src/cognitive_engines/DSA_CE/DSA_CognitiveEngine.cpp
===================================================================
--- vtcross/trunk/src/cognitive_engines/DSA_CE/DSA_CognitiveEngine.cpp	(revision 465)
+++ vtcross/trunk/src/cognitive_engines/DSA_CE/DSA_CognitiveEngine.cpp	(revision 469)
@@ -18,6 +18,7 @@
 #include <cstring>
 #include <stdint.h>
-#include <math.h>
-
+#include <cmath>
+
+#include "vtcross/cbr.h"
 #include "vtcross/cognitive_engine.h"
 #include "vtcross/common.h"
@@ -27,11 +28,5 @@
 #include "vtcross/error.h"
 #include "vtcross/socketcomm.h"
-#include "vtcross/cbr.h"
-
-// TODO this is really bad; need to move to a proper cbr.h
-#include "cbr.c"
-
-#include "sqlite3.h"
-#include "sqlite3ext.h"
+
 
 #define INTERFERENCE 0
@@ -45,6 +40,5 @@
 
 
-
-static cbr myCBR;
+static CBR *myCBR;
 
 
@@ -60,5 +54,5 @@
 CognitiveEngine::~CognitiveEngine()
 {
-    cbr_free(myCBR);
+    delete myCBR;
     delete [] pList;
     delete [] oList;
@@ -161,9 +155,9 @@
     std::string channel_name = "channel";
     searchNames[0] = (char *) channel_name.c_str();
-    searchOps[0] = EQ;
+    searchOps[0] = 0;
     searchVals[0] = parameters[0].value; 
 
     /* Execute CBR search and put output into returnValues */
-    cbr_search(myCBR, searchNames, searchOps, searchVals,
+    myCBR->Search(searchNames, searchOps, searchVals,
             1, returnValues);
 
@@ -197,5 +191,5 @@
     }
 
-    cbr_update(myCBR, nameList, obsList, valList, obsVals, 
+    myCBR->Update(nameList, obsList, valList, obsVals, 
             numberColumns, obsColumns);
 }
@@ -697,8 +691,8 @@
 
         searchNames[0] = (char*) pList[0].name.c_str();
-        searchOps[0] = EQ;
+        searchOps[0] = 0;
         searchVals[0] = i+1; 
     
-        uint32_t rc = cbr_search(myCBR, searchNames, searchOps, searchVals,
+        uint32_t rc = myCBR->Search(searchNames, searchOps, searchVals,
             1, returnValues);
 
@@ -728,5 +722,5 @@
                 /* Add the new optimized set to the CBR database */
 
-    		cbr_add_row(myCBR, rowNames, returnValues, numberColumns);
+    		myCBR->AddRow(rowNames, returnValues, numberColumns);
 	}
 
@@ -737,5 +731,5 @@
     // Get sum of all the channel utilities.
     sumSearchName = (char *) utility_name.c_str();
-    uint32_t rc = cbr_search_sum(myCBR, sumSearchName, sumRetVals);
+    uint32_t rc = myCBR->SearchSum(sumSearchName, sumRetVals);
 
     // Psuedo random channel selection based upon utility.
@@ -752,8 +746,8 @@
         
     searchNames[0] = (char*) pList[0].name.c_str();
-    searchOps[0] = EQ;
+    searchOps[0] = 0;
     searchVals[0] = channel; 
   
-    rc = cbr_search(myCBR, searchNames, searchOps, searchVals,
+    rc = myCBR->Search(searchNames, searchOps, searchVals,
         1, returnValues);
 
@@ -800,5 +794,5 @@
 
     /* Add the new optimized set to the CBR database */
-    cbr_add_row(myCBR, allNames, returnValues, returnValueIndex+1);
+    myCBR->AddRow(allNames, returnValues, returnValueIndex+1);
 
 
@@ -863,5 +857,5 @@
     cols[columnIndex] = (char *)utility_name.c_str();
 
-    myCBR = cbr_create_with_primary(filename, tablename, cols, paramCols, numberColumns, radioInfo->numParameters);
-}
-
+    myCBR = new CBR(filename, tablename, cols, paramCols, numberColumns, radioInfo->numParameters);
+}
+
Index: vtcross/trunk/src/cognitive_engines/DSA_CE/Makefile.am
===================================================================
--- vtcross/trunk/src/cognitive_engines/DSA_CE/Makefile.am	(revision 411)
+++ vtcross/trunk/src/cognitive_engines/DSA_CE/Makefile.am	(revision 469)
@@ -20,5 +20,4 @@
 
 bin_PROGRAMS = DSA_Demo
-include_HEADERS = cbr.c
 
 DSA_Demo_SOURCES = DSA_CognitiveEngine.cpp DSA_Demo.cpp
Index: vtcross/trunk/src/cognitive_engines/DSA_CE/cbr.c
===================================================================
--- vtcross/trunk/src/cognitive_engines/DSA_CE/cbr.c	(revision 411)
+++ 	(revision )
@@ -1,452 +1,0 @@
-/*
- Copyright 2009 Virginia Polytechnic Institute and State University  
-
- Licensed under the Apache License, Version 2.0 (the "License"); 
- you may not use this file except in compliance with the License. 
- You may obtain a copy of the License at 
- 
- http://www.apache.org/licenses/LICENSE-2.0 
-
- Unless required by applicable law or agreed to in writing, software 
- distributed under the License is distributed on an "AS IS" BASIS, 
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
- See the License for the specific language governing permissions and 
- limitations under the License. 
-*/
-
-
-//
-// Case-based reasoner
-//
-
-#include <stdlib.h>
-#include <string.h>
-#include <stdio.h>
-
-#include "vtcross/cbr.h"
-#include "vtcross/common.h"
-
-
-struct cbr_s {
-    char filename[64];
-    char tablename[64];
-    char command[2048];
-    sqlite3 *db; 
-    unsigned int num_columns;
-};
-
-
-// open a database or create a database if it does not exist
-int OpenDatabase(cbr _cbr){
-    int rc;
-    //sqlite3 **db;
-
-    //printf("database name: %s\n", _cbr->filename);
-    //rc = sqlite3_open(_cbr->filename, db);
-    rc = sqlite3_open(_cbr->filename, &(_cbr->db));
-    if (rc) {
-        //fprintf(stderr, "can't open database: %s\n", sqlite3_errmsg(*db));
-        fprintf(stderr, "can't open database: %s\n", sqlite3_errmsg(_cbr->db));
-        sqlite3_close(_cbr->db);
-        exit(1);
-    } else{
-        //printf("database opened.\n");
-    }
-    //_cbr->db = *db;
-    return rc;
-}
-
-
-// simple callback function, display result
-int callback(void *notUsed, int argc, char **argv, char **azColName){
-    int i;
-    for(i=0; i<argc; i++){
-        printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
-    }
-    printf("\n");
-    return 0;
-}
-
-
-// execute command
-int ExecuteCommand(cbr _cbr){
-    int rc;
-    char *zErrMsg = 0;
-
-    //printf("command: %s\n", _cbr->command);
-    rc = sqlite3_exec(_cbr->db, _cbr->command, callback, 0, &zErrMsg);
-    if( rc!=SQLITE_OK){
-        fprintf(stderr, "SQL error: %s: %s\n", zErrMsg,_cbr->command);
-        sqlite3_free(zErrMsg);
-    } else{
-        //printf("command executed.\n");
-    }
-    return rc;
-}
-
-
-// execute search command
-int ExecuteSearchCommand(cbr _cbr, float *_retvals){
-    int rc;
-    unsigned int i;
-
-    sqlite3_stmt * pStatement;
-    rc = sqlite3_prepare_v2(_cbr->db, _cbr->command, -1, &pStatement, NULL);
-    if (rc == SQLITE_OK){ 
-        if (sqlite3_step(pStatement) == SQLITE_ROW){
-            for (i=0; i<_cbr->num_columns; ++i)
-                _retvals[i] = sqlite3_column_double(pStatement, i);
-        } else {
-		    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);
-   
-    return rc;
-}
-
-// create database
-cbr cbr_create_with_primary(char * _filename, char * _tablename, 
-	char * _cols[], char * _primcols[], unsigned int _len,
-	unsigned int _primlen)
-{
-    // cbr is a pointer to struct cbr_s
-    cbr _cbr = (cbr) malloc(sizeof(struct cbr_s));
-
-    // create database
-
-    // copy filename
-    unsigned int i=0;
-    strcpy(_cbr->filename, _filename);
-
-    // execute create database command
-    // database handle
-    //_cbr->db = NULL;
-    OpenDatabase(_cbr);
-
-    // create table
-
-    // copy tablename
-    strcpy(_cbr->tablename, _tablename);
-
-    // number of columns in the table
-    _cbr->num_columns = _len;
-
-    // generate command
-    strcpy(_cbr->command, "CREATE TABLE ");
-    strcat(_cbr->command, _cbr->tablename);
-    strcat(_cbr->command, "(");
-    for (i=0; i<_cbr->num_columns; i++) {
-        strcat(_cbr->command, _cols[i]);
-        strcat(_cbr->command, " FLOAT");
-        strcat(_cbr->command, ", ");
-    }
-   strcat(_cbr->command, "PRIMARY KEY (");
-    for (i=0; i<_primlen; i++) {
-        strcat(_cbr->command, _primcols[i]);
-        if (i != _primlen-1) // not last entry
-            strcat(_cbr->command, ", ");
-    }
-    strcat(_cbr->command, "));");
-
-    // execute create table command
-    ExecuteCommand(_cbr);
-
-    return _cbr;
-}
-
-// create database
-cbr cbr_create(char * _filename, char * _tablename, char * _cols[], unsigned int _len)
-{
-    // cbr is a pointer to struct cbr_s
-    cbr _cbr = (cbr) malloc(sizeof(struct cbr_s));
-
-    // create database
-
-    // copy filename
-    unsigned int i=0;
-    strcpy(_cbr->filename, _filename);
-
-    // execute create database command
-    // database handle
-    //_cbr->db = NULL;
-    OpenDatabase(_cbr);
-
-    // create table
-
-    // copy tablename
-    strcpy(_cbr->tablename, _tablename);
-
-    // number of columns in the table
-    _cbr->num_columns = _len;
-
-    // generate command
-    strcpy(_cbr->command, "CREATE TABLE ");
-    strcat(_cbr->command, _cbr->tablename);
-    strcat(_cbr->command, "(");
-    for (i=0; i<_cbr->num_columns; i++) {
-        strcat(_cbr->command, _cols[i]);
-        strcat(_cbr->command, " FLOAT");
-        if (i != _cbr->num_columns-1) // not last entry
-            strcat(_cbr->command, ", ");
-    }
-    strcat(_cbr->command, ");");
-
-    // execute create table command
-    ExecuteCommand(_cbr);
-
-    return _cbr;
-}
-
-
-// free space
-void cbr_free(cbr _cbr)
-{  
-    // generate command, remove a table with its content
-    strcpy(_cbr->command, "drop table ");
-    strcat(_cbr->command, _cbr->tablename);
-
-    // execute delete command
-    ExecuteCommand(_cbr);
-
-    // clean the database
-    strcpy(_cbr->command, "vacuum");
-    ExecuteCommand(_cbr);
-
-    free(_cbr);
-}
-
-
-// print 
-void cbr_print(cbr _cbr)
-{
-    // generate commandi
-    strcpy(_cbr->command, "select ");
-    strcat(_cbr->command, _cbr->tablename);
-    strcat(_cbr->command, ".* from ");
-    strcat(_cbr->command, _cbr->tablename);
-    strcat(_cbr->command, ";");
-
-    // execute print (select all)  command
-    ExecuteCommand(_cbr);
-    printf("database %s, table %s:\n", _cbr->filename, _cbr->tablename);
-}
-
-
-/*//static
-int cbr_callback(void *notUsed, int argc, char **argv, char **azColName)
-{
-    return 0;
-}*/
-
-
-const char * ops_str[] = {
-    "==", "!=", ">", ">=", "<", "<="};
-
-
-// cbr search for a sum
-int cbr_search_sum(
-    cbr _cbr,
-    char *_name,
-    float *_retvals)
-{   
-    int rc;
-    // generate command
-    strcpy(_cbr->command, "select SUM( ");
-    strcat(_cbr->command, _cbr->tablename);
-    strcat(_cbr->command, ".");
-    strcat(_cbr->command, _name);
-    strcat(_cbr->command, ") from ");
-    strcat(_cbr->command, _cbr->tablename);
-    strcat(_cbr->command, ";");
-
-    //printf("search command: %s\n", _cbr->command);
-
-    rc = ExecuteSearchCommand(_cbr, _retvals);
-    /*printf("search result: ");
-    for (int i=0; i<_cbr->num_columns; i++)
-        printf("%f, ",_retvals[i]);
-    printf("\n");
-    */
-    return rc;
-}
-
-
-// cbr search
-int cbr_search_rand(
-    cbr _cbr,
-    char *_names[],
-    int * _ops,
-    float *_vals,
-    unsigned int _n,
-    float *_retvals)
-{   
-    int rc;
-    // generate command
-    strcpy(_cbr->command, "select ");
-    strcat(_cbr->command, _cbr->tablename);
-    strcat(_cbr->command, ".* from ");
-    strcat(_cbr->command, _cbr->tablename);
-    strcat(_cbr->command, " where ");
-
-    unsigned int i;
-    char str_buffer[64];
-    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(_cbr->command, _names[i]);
-        strcat(_cbr->command, ops_str[_ops[i]]);
-        sprintf(str_buffer, "%E", _vals[i]);
-        strcat(_cbr->command, str_buffer);
-
-        if (i<_n-1)
-            strcat(_cbr->command, " AND ");
-        else
-            strcat(_cbr->command, " order by RAND();");
-    }
-
-    //printf("search command: %s\n", _cbr->command);
-
-    rc = ExecuteSearchCommand(_cbr, _retvals);
-    
-    /*printf("search result: ");
-    for (i=0; i<_cbr->num_columns; i++)
-        printf("%f, ",_retvals[i]);
-    printf("\n");
-*/
-    return rc;
-}
-
-
-// cbr search
-int cbr_search(
-    cbr _cbr,
-    char *_names[],
-    int * _ops,
-    float *_vals,
-    unsigned int _n,
-    float *_retvals)
-{   
-    int rc;
-    // generate command
-    strcpy(_cbr->command, "select ");
-    strcat(_cbr->command, _cbr->tablename);
-    strcat(_cbr->command, ".* from ");
-    strcat(_cbr->command, _cbr->tablename);
-    strcat(_cbr->command, " where ");
-
-    unsigned int i;
-    char str_buffer[64];
-    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(_cbr->command, _names[i]);
-        strcat(_cbr->command, ops_str[_ops[i]]);
-        sprintf(str_buffer, "%E", _vals[i]);
-        strcat(_cbr->command, str_buffer);
-
-        if (i<_n-1)
-            strcat(_cbr->command, " AND ");
-        else
-            strcat(_cbr->command, " order by utility desc;");
-    }
-
-    //printf("search command: %s\n", _cbr->command);
-
-    rc = ExecuteSearchCommand(_cbr, _retvals);
-    
-    /*printf("search result: ");
-    for (i=0; i<_cbr->num_columns; i++)
-        printf("%f, ",_retvals[i]);
-    printf("\n");*/
-
-    return rc;
-}
-
-
-// update a row 
-int cbr_update(cbr _cbr, char *_where[], char*_set[], float *_wherevals, float *_setvals, 
-		unsigned int _wherelen, unsigned int _setlen)
-{
-    unsigned int i;
-    
-    // generate command
-    //printf("%s\n", _cbr->command);
-    strcpy(_cbr->command, "UPDATE ");
-    strcat(_cbr->command, _cbr->tablename);
-    
-    strcat(_cbr->command, " SET ");
-    for (i=0; i<_setlen; i++) {
-        strcat(_cbr->command, _set[i]);
-        strcat(_cbr->command, " = ");
-        sprintf(_cbr->command, "%s%f", _cbr->command, _setvals[i]);
-        strcat(_cbr->command, "  ");
-        if (i != _setlen-1) // not last entry
-            strcat(_cbr->command, ", ");
-    }
-    strcat(_cbr->command, " WHERE ");
-
-    for (i=0; i<_wherelen; i++) {
-        strcat(_cbr->command, _where[i]);
-        strcat(_cbr->command, " = ");
-        sprintf(_cbr->command, "%s%f", _cbr->command, _wherevals[i]);
-        strcat(_cbr->command, "  ");
-        if (i != _wherelen-1) // not last entry
-            strcat(_cbr->command, "AND ");
-    }
-    strcat(_cbr->command, ";");
-    
-    //printf("search command: %s\n", _cbr->command);
-    // execute add command
-    ExecuteCommand(_cbr);
-
-    return 0;
-}
-
-// cbr add a row
-int cbr_add_row(cbr _cbr, char *_cols[], float *_vals, unsigned int _len)
-{
-    unsigned int i;
-    
-    // generate command
-    //printf("%s\n", _cbr->command);
-    strcpy(_cbr->command, "replace into ");
-    strcat(_cbr->command, _cbr->tablename);
-    
-    strcat(_cbr->command, " (");
-    for (i=0; i<_len; i++) {
-        strcat(_cbr->command, _cols[i]);
-        if (i != _cbr->num_columns-1) // not last entry
-            strcat(_cbr->command, ", ");
-    }
-    strcat(_cbr->command, ") ");
-
-    strcat(_cbr->command, " values(");
-    for (i=0; i<_len; i++) {
-        // ???? how to fill the values if _cbr->num_columns != _len
-        // assume = in the following
-        sprintf(_cbr->command, "%s%f", _cbr->command, _vals[i]);
-        if (i != _cbr->num_columns-1) // not last entry
-            strcat(_cbr->command, ", ");
-    }
-    strcat(_cbr->command, ");");
-    
-    //printf("search command: %s\n", _cbr->command);
-    // execute add command
-    ExecuteCommand(_cbr);
-
-    return 0;
-}
-
Index: vtcross/trunk/src/include/vtcross/cbr.h
===================================================================
--- vtcross/trunk/src/include/vtcross/cbr.h	(revision 468)
+++ vtcross/trunk/src/include/vtcross/cbr.h	(revision 469)
@@ -34,4 +34,5 @@
 #include <stdint.h>
 #include <string>
+
 #include <sqlite3.h>
 
@@ -49,7 +50,9 @@
 {
     public:
-        /* Default constructor and preferred constructor for the CBR class. */
+        /* Constructors for the CBR class. */
         CBR();
         CBR(char *_filename, char *_tablename, char *_cols[], uint32_t _len);
+        CBR(char *_filename, char *_tablename, char *_cols[], \
+                char *_primcols[], uint32_t _len, uint32_t  _primlen);
 
         /* Destructor for the CBR class. Note that this destructor will be
@@ -68,4 +71,7 @@
         virtual int32_t Search(char *_names[], int32_t *_ops, float *_vals, \
                 uint32_t _n, float *_retvals);
+        virtual int32_t SearchSum(char *_name, float *_retvals);
+        virtual int32_t SearchRand(char *_names[], int * _ops, float *_vals, unsigned int _n, \
+            float *_retvals);
 
         virtual int32_t Update(char *_where[], char *_set[], float *_wherevals, \
@@ -167,5 +173,5 @@
     // execute create database command
     // database handle
-    //_cbr->db = NULL;
+    //db = NULL;
     OpenDatabase();
 
@@ -193,4 +199,50 @@
     ExecuteCommand();
 }
+
+// create database
+CBR::CBR(char * _filename, char * _tablename, 
+	char * _cols[], char * _primcols[], unsigned int _len,
+	unsigned int _primlen)
+{
+    // create database
+
+    // copy filename
+    unsigned int i=0;
+    strcpy(filename, _filename);
+
+    // execute create database command
+    // database handle
+    //db = NULL;
+    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");
+        strcat(command, ", ");
+    }
+   strcat(command, "PRIMARY KEY (");
+    for (i=0; i<_primlen; i++) {
+        strcat(command, _primcols[i]);
+        if (i != _primlen-1) // not last entry
+            strcat(command, ", ");
+    }
+    strcat(command, "));");
+
+    // execute create table command
+    ExecuteCommand();
+}
+
 
 
@@ -276,4 +328,84 @@
 }
 
+// cbr search for a sum
+int32_t 
+CBR::SearchSum(
+    char *_name,
+    float *_retvals)
+{   
+    int rc;
+    // generate command
+    strcpy(command, "select SUM( ");
+    strcat(command, tablename);
+    strcat(command, ".");
+    strcat(command, _name);
+    strcat(command, ") from ");
+    strcat(command, tablename);
+    strcat(command, ";");
+
+    //printf("search command: %s\n", command);
+
+    rc = ExecuteSearchCommand(_retvals);
+    /*printf("search result: ");
+    for (int i=0; i<numColumns; i++)
+        printf("%f, ",_retvals[i]);
+    printf("\n");
+    */
+    return rc;
+}
+
+
+// cbr search
+int32_t 
+CBR::SearchRand(
+    char *_names[],
+    int * _ops,
+    float *_vals,
+    unsigned int _n,
+    float *_retvals)
+{   
+        const char *ops_str[] = {"==", "!=", ">", ">=", "<", "<="};
+    int rc;
+    // generate command
+    strcpy(command, "select ");
+    strcat(command, tablename);
+    strcat(command, ".* from ");
+    strcat(command, tablename);
+    strcat(command, " where ");
+
+    unsigned int i;
+    char str_buffer[64];
+    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, ops_str[_ops[i]]);
+        sprintf(str_buffer, "%E", _vals[i]);
+        strcat(command, str_buffer);
+
+        if (i<_n-1)
+            strcat(command, " AND ");
+        else
+            strcat(command, " order by RAND();");
+    }
+
+    //printf("search command: %s\n", command);
+
+    rc = ExecuteSearchCommand(_retvals);
+    
+    /*printf("search result: ");
+    for (i=0; i<numColumns; i++)
+        printf("%f, ",_retvals[i]);
+    printf("\n");
+*/
+    return rc;
+}
+
+
+
 
 // update a row 
@@ -335,5 +467,5 @@
     strcat(command, " values(");
     for (i=0; i<_len; i++) {
-        // ???? how to fill the values if _cbr->numColumns != _len
+        // ???? how to fill the values if numColumns != _len
         // assume = in the following
         sprintf(command, "%s%f", command, _vals[i]);
