Index: vtcross/trunk/src/include/vtcross/cbr.h
===================================================================
--- vtcross/trunk/src/include/vtcross/cbr.h	(revision 470)
+++ vtcross/trunk/src/include/vtcross/cbr.h	(revision 471)
@@ -15,13 +15,8 @@
 */
 
-/* This file contains the declaration *and* default implementation of a VTCROSS
- * case-based reasoner.
- *
- * This class can be used either as-is, or with re-implementations of some/all
- * of the functions.  All functions are declared virtual, and so may be
- * overridden in the cognitive engine source file as necessary. In addition,
- * internal class members are declared 'protected', so that they may be
- * inherited by child implementations.
+/* This file contains the full implementation of the CBR class - the default
+ * VTCROSS case-based reasoner.
  */
+
 
 
@@ -47,12 +42,35 @@
 
 
+/* This is an internal debugging function used by some sqlite3 function calls.
+ * It is not used otherwise in the VTCROSS codebase. */
+int32_t 
+callback(void *notUsed, int argc, char **argv, char **azColName)
+{
+    for(size_t i = 0; i < argc; i++) {
+        LOG("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
+    }
+    LOG("\n");
+
+    return 0;
+}
+
+
+/* The CBR class is designed to used as either as-is, or as a parent class.  All
+ * functions are declared virtual, and internal members are 'protected' rather
+ * than private. If you require functionality in a CBR not specifically provided
+ * by this class, include this header in your source file, create a new class
+ * that derives from CBR, and implement your desired functionality over the
+ * original virtual functions as necessary.
+ */
 class CBR
 {
     public:
-        /* Constructors for the CBR class. */
+        /* Constructors for the CBR class. Note that the default constructor
+         * must be defined inline here so that super-calls from child classes
+         * don't fail (i.e. we cannot rely on the compiler-provided constructor. */
         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);
+                char *_primcols[], uint32_t _len, uint32_t _primlen);
 
         /* Destructor for the CBR class. Note that this destructor will be
@@ -61,21 +79,31 @@
         virtual ~CBR();
 
+        /* This function opens the VTCROSS database, or if it has not been
+         * created yet, creates it. */
         virtual int32_t OpenDatabase();
 
+        /* Execute a sqlite3 command and return the sqlite3 return code. */
         virtual int32_t ExecuteCommand();
 
+        /* Execute a sqlite3 search command and store the results in the passed
+         * retvals argument. */
         virtual int32_t ExecuteSearchCommand(float *_retvals);
 
+        /* Print the VTCROSS sqlite3 database. */
         virtual void Print();
 
+        /* Search the VTCROSS database for specific fields and store the results
+         * in the passed retvals argument. */
         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, \
+        virtual int32_t SearchRand(char *_names[], int *_ops, float *_vals, uint32_t _n, \
             float *_retvals);
 
+        /* Update a row in the VTCROSS sqlite3 database. */
         virtual int32_t Update(char *_where[], char *_set[], float *_wherevals, \
                 float *_setvals, uint32_t _wherelen, uint32_t _setlen);
 
+        /* Add a row to the VTCROSS sqlite3 database. */
         virtual int32_t AddRow(char *_cols[], float *_vals, uint32_t _len);
 
@@ -89,17 +117,102 @@
 
 
-int32_t 
-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;
-}
-
-
-// open a database or create a database if it does not exist
+CBR::CBR(char *_filename, char *_tablename, char *_cols[], uint32_t _len)
+{
+    // 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");
+        if (i != numColumns-1) // not last entry
+            strcat(command, ", ");
+    }
+    strcat(command, ");");
+
+    // execute create table command
+    ExecuteCommand();
+}
+
+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();
+}
+
+
+
+CBR::~CBR()
+{  
+    // generate command, remove a table with its content
+    strcpy(command, "drop table ");
+    strcat(command, tablename);
+
+    // execute delete command
+    ExecuteCommand();
+
+    // clean the database
+    strcpy(command, "vacuum");
+    ExecuteCommand();
+}
+
+
+
+
 int32_t 
 CBR::OpenDatabase()
@@ -118,5 +231,4 @@
 
 
-// execute command
 int32_t
 CBR::ExecuteCommand()
@@ -135,5 +247,4 @@
 
 
-// execute search command
 int32_t 
 CBR::ExecuteSearchCommand(float *_retvals)
@@ -160,108 +271,4 @@
     return rc;
 }
-
-
-// create database
-CBR::CBR(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;
-    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, ");");
-
-    // execute create table command
-    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();
-}
-
-
-
-// free space
-CBR::~CBR()
-{  
-    // generate command, remove a table with its content
-    strcpy(command, "drop table ");
-    strcat(command, tablename);
-
-    // execute delete command
-    ExecuteCommand();
-
-    // clean the database
-    strcpy(command, "vacuum");
-    ExecuteCommand();
-}
-
-
-// print 
 void 
 CBR::Print()
@@ -279,7 +286,4 @@
 }
 
-//
-// cbr search
-//int cbr_search(cbr _cbr, char *_names[], int * _ops, float *_vals, unsigned int _n, float *_retvals);
 int32_t 
 CBR::Search(
@@ -328,5 +332,4 @@
 }
 
-// cbr search for a sum
 int32_t 
 CBR::SearchSum(
@@ -356,5 +359,4 @@
 
 
-// cbr search
 int32_t 
 CBR::SearchRand(
@@ -409,5 +411,4 @@
 
 
-// update a row 
 int32_t 
 CBR::Update(char *_where[], char*_set[], float *_wherevals, float *_setvals, 
@@ -447,5 +448,4 @@
 }
 
-// cbr add a row
 int32_t 
 CBR::AddRow(char *_cols[], float *_vals, unsigned int _len)
