Index: vtcross/branches/trnewman/CR_engines/CBR/src/main_cognitive_engine.c
===================================================================
--- vtcross/branches/trnewman/CR_engines/CBR/src/main_cognitive_engine.c	(revision 77)
+++ vtcross/branches/trnewman/CR_engines/CBR/src/main_cognitive_engine.c	(revision 77)
@@ -0,0 +1,417 @@
+#include <stdio.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <netdb.h>
+//#include <iostream>
+#include <stdlib.h>
+#include <string.h>
+
+#include "cbr.h"
+
+//using namespace std;
+
+struct CE_Info {
+    int numUtilities;
+    int numParameters;
+    int numObservables;
+};
+
+struct Utility {
+    char name[50];
+    char units[50];
+    char goal[50];
+   /* string name;
+    string units;
+    string goal;*/
+    float target;
+};
+
+struct Affect {
+    struct Utility u; 
+    //string relation;
+    char relation[50];
+};      
+
+struct Parameter {
+    /*string name;
+    string units;*/
+    char name[50];
+    char units[50];
+    float min;
+    int numAffects; 
+    struct Affect affection_list[10];
+    float max;
+    float step;
+};      
+        
+struct Observable {
+    //string name;
+    char name[50];
+    struct Affect affection_list[10];
+    int numAffects;
+};
+
+// error handling
+void error(char *msg)
+{
+    perror(msg);
+    exit(0);
+}
+
+void ReadMessage(int socket,char * buffer) {
+    int i,n;
+
+    n = recv(socket,buffer,256,MSG_PEEK);
+    for(i=0;i<256;i++){
+	    if(strcmp(&buffer[i],"\0") == 0) break;
+    }
+    n = recv(socket,buffer,i+1,0);
+    if (n < 0) 
+         error("ERROR reading from socket");
+//    printf("ReadMessage:%s %d\n",buffer,n);
+}
+
+
+int GetUtility(int sockfd, struct Utility uList[], struct CE_Info *ce_info)
+{
+    char buffer[256];
+    int i;
+
+    // read unitilities
+    // numUtilities
+    bzero(buffer,256);
+    ReadMessage(sockfd,buffer);
+    ce_info->numUtilities = atoi(buffer);
+    //printf("number of utilities: %d\n", ce_info->numUtilities);
+        
+    for (i = 0; i < ce_info->numUtilities; i++){
+        bzero(buffer,256);
+	    ReadMessage(sockfd,buffer);
+	    //printf("Name: %s\n", buffer);
+        strcpy(uList[i].name, buffer);
+                 
+        bzero(buffer,256);
+	    ReadMessage(sockfd,buffer);
+	    //printf("Units: %s\n", buffer);
+        strcpy(uList[i].units, buffer);
+
+        bzero(buffer,256);
+	    ReadMessage(sockfd,buffer);
+        //printf("Goal: %s\n", buffer);
+        strcpy(uList[i].goal, buffer);
+        
+	    bzero(buffer,256);
+	    ReadMessage(sockfd,buffer);
+        //printf("Target: %s\n", buffer);
+        uList[i].target = atof(buffer);
+    }
+    return 1;
+}
+
+
+int GetParameter(int sockfd, struct Parameter pList[], struct CE_Info *ce_info)
+{
+    char buffer[256];
+    int i, j;
+
+    // numParameters
+    bzero(buffer,256);
+    ReadMessage(sockfd,buffer);
+    ce_info->numParameters = atoi(buffer);
+    //printf("number of parameters: %d\n", ce_info->numParameters);
+        
+    for (i = 0; i < ce_info->numParameters; i++){
+        bzero(buffer,256);
+	    ReadMessage(sockfd,buffer);
+	    //printf("Name: %s\n", buffer);
+        strcpy(pList[i].name, buffer);
+                 
+        bzero(buffer,256);
+	    ReadMessage(sockfd,buffer);
+	    //printf("Units: %s\n", buffer);
+        strcpy(pList[i].units, buffer);
+
+        bzero(buffer,256);
+	    ReadMessage(sockfd,buffer);
+        //printf("Min: %s\n", buffer);
+        pList[i].min = atof(buffer);
+        
+        bzero(buffer,256);
+	    ReadMessage(sockfd,buffer);
+        //printf("Max: %s\n", buffer);
+        pList[i].max = atof(buffer);
+        
+	    bzero(buffer,256);
+	    ReadMessage(sockfd,buffer);
+        //printf("Step: %s\n", buffer);
+        pList[i].step = atof(buffer);
+    
+        // numAffects
+        bzero(buffer,256);
+        ReadMessage(sockfd,buffer);
+        pList[i].numAffects = atoi(buffer);
+        //printf("number of affects: %d\n", pList[i].numAffects);
+        
+        for (j = 0; j < pList[i].numAffects; j++){
+            bzero(buffer,256);
+	        ReadMessage(sockfd,buffer);
+	        //printf("Utility name: %s\n", buffer);
+            strcpy(pList[i].affection_list[j].u.name, buffer);
+                 
+            bzero(buffer,256);
+	        ReadMessage(sockfd,buffer);
+	        //printf("Relation: %s\n", buffer);
+            strcpy(pList[i].affection_list[j].relation, buffer);
+        }
+    }
+    return 1;
+}
+
+
+int GetObservable(int sockfd, struct Observable oList[], struct CE_Info *ce_info)
+{
+    char buffer[256];
+    int i,j;
+
+    // numParameters
+    bzero(buffer,256);
+    ReadMessage(sockfd,buffer);
+    ce_info->numObservables = atoi(buffer);
+    //printf("number of observables: %d\n", ce_info->numObservables);
+        
+    for (i = 0; i < ce_info->numObservables; i++){
+        bzero(buffer,256);
+	    ReadMessage(sockfd,buffer);
+	    //printf("Name: %s\n", buffer);
+        strcpy(oList[i].name, buffer);
+                 
+        // numAffects
+        bzero(buffer,256);
+        ReadMessage(sockfd,buffer);
+        oList[i].numAffects = atoi(buffer);
+        //printf("number of affects: %d\n", oList[i].numAffects);
+        
+        for (j = 0; j < oList[i].numAffects; j++){
+            bzero(buffer,256);
+	        ReadMessage(sockfd,buffer);
+	        //printf("Utility name: %s\n", buffer);
+            strcpy(oList[i].affection_list[j].u.name, buffer);
+                 
+            bzero(buffer,256);
+	        ReadMessage(sockfd,buffer);
+	        //printf("Relation: %s\n", buffer);
+            strcpy(oList[i].affection_list[j].relation, buffer);
+        }
+    }
+    return 1;
+}
+
+
+int GetXMLFromServer(int sockfd, struct Utility uList[], struct Parameter pList[], 
+        struct Observable oList[], struct CE_Info *ce_info)
+{
+    // get utilities
+    GetUtility(sockfd, uList, ce_info);
+    
+    // get parameters
+    GetParameter(sockfd, pList, ce_info);
+
+    // read obervables
+    GetObservable(sockfd, oList, ce_info);
+
+    printf("xml reception done\n");
+    
+    return 1;
+}
+
+void print_current_config(struct Utility uList[], struct Parameter pList[], 
+        struct Observable oList[], struct CE_Info * ce_info) 
+{    
+    int i = 0;
+    int j = 0;
+
+    // utilities
+    for(i = 0; i < ce_info->numUtilities ; i++) {
+        printf("Utility: %s\n", uList[i].name);
+        printf("    Units: %s\n", uList[i].units);
+        printf("    Goal: %s\n", uList[i].goal);
+        printf("    Target: %f\n", uList[i].target);
+        /*cout << "Utility:  " << uList[i].name << endl;
+        cout << "     Units:  " << uList[i].units << endl;
+        cout << "     Goal:   " << uList[i].goal << endl;
+        cout << "     Target: " << uList[i].target << endl;*/
+    }
+                       
+    // parameters                        
+    for(i = 0; i < ce_info->numParameters; i++) {
+        printf("Paramter: %s\n", pList[i].name);
+        printf("    Units: %s\n", pList[i].units);
+        printf("    Min: %f\n", pList[i].min);
+        printf("    Max: %f\n", pList[i].max);
+        printf("    Step: %f\n", pList[i].step);
+        /*cout << "Parameter:  " << pList[i].name << endl;
+        cout << "       Units:   " << pList[i].units << endl;
+        cout << "       Min:     " << pList[i].min << endl;
+        cout << "       Max:     " << pList[i].max << endl;
+        cout << "       Step:    " << pList[i].step << endl;*/
+        for(j = 0; j < pList[i].numAffects; j++) {
+            printf("        Affect: %s -> %s\n", pList[i].affection_list[j].u.name, pList[i].affection_list[j].relation);
+            //cout << "       Affect: " << pList[i].affection_list[j].u.name << " -> " << pList[i].affection_list[j].relation << endl;
+        }
+    }
+
+    // observables
+    for(i = 0; i < ce_info->numObservables; i++) {
+        printf("Observable: %s\n", oList[i].name);
+        //cout << "Observable:  " << oList[i].name << endl;
+        for(j = 0; j < oList[i].numAffects; j++) {
+            printf("        Affect: %s -> %s\n", oList[i].affection_list[j].u.name, oList[i].affection_list[j].relation);
+            //cout << "       Affect: " << oList[i].affection_list[j].u.name << " -> " << oList[i].affection_list[j].relation << endl;
+        }
+    }
+}
+
+
+// main client socket
+int main(int argc, char *argv[])
+{
+    struct Utility uList[10];
+    struct Parameter pList[10];
+    struct Observable oList[10];
+    struct CE_Info ce_info;
+
+    int sockfd, portno;
+    struct sockaddr_in serv_addr;
+    struct hostent *server;
+
+    // setup client socket connection
+    if (argc < 3) {
+       fprintf(stderr,"usage: %s hostname port\n", argv[0]);
+       exit(0);
+    }
+    // server name
+    server = gethostbyname(argv[1]);
+    if (server == NULL) {
+        fprintf(stderr,"ERROR, no such host\n");
+        exit(0);
+    }
+    // port no.
+    portno = atoi(argv[2]);
+    // socket file descriptor
+    sockfd = socket(AF_INET, SOCK_STREAM, 0);
+    if (sockfd < 0) 
+        error("ERROR opening socket");
+    // initiate struct socketaddr_in
+    bzero((char *) &serv_addr, sizeof(serv_addr));
+    serv_addr.sin_family = AF_INET;
+    serv_addr.sin_port = htons(portno);
+    bcopy((char *)server->h_addr, 
+         (char *)&serv_addr.sin_addr.s_addr,
+         server->h_length);
+    // cast sockaddr_in to sockaddr
+    if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0) 
+        error("ERROR connecting");
+    
+    // get xml info from server
+    GetXMLFromServer(sockfd, uList, pList, oList, &ce_info);
+
+    //printf("utility:\n name: %s, units: %s, goal: %s, target: %f\n", 
+    //        uList[0].name, uList[0].units, uList[0].goal, uList[0].target);
+    
+    print_current_config(uList, pList, oList, &ce_info); 
+
+    // cbr operation
+    int i;
+    unsigned int num_cols;
+
+    num_cols = ce_info.numUtilities + ce_info.numParameters;
+    num_cols = num_cols + ce_info.numObservables;
+    num_cols = num_cols + 1;
+
+    char *cols[] = {"throughput", "spectral_efficiency",
+        "log10_ber", "mod_scheme", "tx_power", "SNR", "utility"};
+    
+    float vals[num_cols];
+    vals[0] = 10e3f;    // throughput
+    vals[1] = 1;        // spectral_efficiency
+    vals[2] = 1e-3f;    // log10_ber
+    vals[3] = 1;        // mod_scheme
+    vals[4] = -3.0f;    // tx_power
+    vals[5] = 10.0f;   // SNR
+    vals[6] = 0.762;    // utility
+    
+    /*num_cols = 6;
+    char * cols[] = {
+        "BER", "throughput",
+        "mod_scheme", "tx_power",
+        "noise_power", "path_loss"
+    };
+
+    float vals[num_cols];
+    vals[0] = 1e-3f;    // BER
+    vals[1] = 10e3f;    // throughput
+    vals[2] = 1;        // mod_scheme
+    vals[3] = -3.0f;    // tx_power
+    vals[4] = -50.0f;   // noise_power
+    vals[5] = 125.0f;   // path_loss
+    //vals[6] = 0.762;    // utility*/
+    
+    printf("number of columns: %d\n", num_cols);
+    printf("column names:");
+    for (i = 0; i<num_cols; i++){
+        printf(" %s",cols[i]);
+    }
+    printf("\n");
+
+    int rc;
+
+    // create cbr database/table
+    printf("create cbr database\n");
+    char filename[] = {"ex1"}, tablename[] = {"data"};
+    cbr mycbr = cbr_create(filename, tablename, cols, num_cols);
+    //cbr mycbr = cbr_create("ex1", "data", cols, num_cols);
+    
+    // add row here
+    printf("add row to cbr table\n");
+    rc = cbr_add_row(mycbr, cols, vals, num_cols);
+    
+    // print
+    printf("cbr table\n");
+    cbr_print(mycbr);
+    
+    /*// simple search: find entry where...
+    //   BER < 1e-2 and path_loss > 100
+    char * search_names[] = {"BER", "path_loss"};
+    int search_ops[] = {LT, GT};
+    float search_vals[] = {1e-2f, 100.f};
+    float retvals[num_cols];
+    printf("search cbr\n");
+    rc = cbr_search(mycbr, search_names, search_ops, search_vals, 2,retvals);*/
+    
+    // simple search: find entry where...
+    //   log10_ber < 1e-2 and SNR > 5
+    char * search_names[] = {"log10_ber", "SNR"};
+    int search_ops[] = {LT, GT};
+    float search_vals[] = {1e-2f, 5.f};
+    float retvals[num_cols];
+    rc = cbr_search(mycbr, search_names, search_ops, 
+            search_vals, 2, retvals);
+
+    // print returned values
+    printf("search result: ");
+    for (i=0;i<num_cols; i++)
+    //for (i=0;i<mycbr->num_columns; i++)
+    printf("%f, ",retvals[i]);
+    printf("\n");
+    
+    // clean up database
+    printf("clean cbr database\n");
+    cbr_free(mycbr);
+    
+    printf("done at client.\n");
+    return 0;
+    
+
+  return 0;
+}
Index: vtcross/branches/trnewman/CR_engines/CBR/src/cbr.c
===================================================================
--- vtcross/branches/trnewman/CR_engines/CBR/src/cbr.c	(revision 76)
+++ vtcross/branches/trnewman/CR_engines/CBR/src/cbr.c	(revision 77)
@@ -220,4 +220,6 @@
     }
 
+    printf("search command: %s\n", _cbr->command);
+
     //ExecuteCommand(_cbr);
     ExecuteSearchCommand(_cbr, _retvals);
Index: vtcross/branches/trnewman/CR_engines/CBR/Makefile
===================================================================
--- vtcross/branches/trnewman/CR_engines/CBR/Makefile	(revision 76)
+++ vtcross/branches/trnewman/CR_engines/CBR/Makefile	(revision 77)
@@ -7,5 +7,5 @@
 #all: cognitive_engine
 
-CC = g++
+#CC = g++
 cc = gcc
 CFLAGS = -Wall -O3
@@ -15,5 +15,5 @@
 
 COBJS = sqlite3.o cbr.o
-CHEADERS = src/sqlite3.h src/sqlite3ext.h src/cbr.h
+CHEADERS = src/sqlite3.h src/cbr.h
 
 $(COBJS) : %.o : src/%.c $(CHEADERS)
@@ -28,6 +28,6 @@
 
 
-$(TESTS) : % : src/main_%.cpp $(COBJS)
-	$(CC) $(CFLAGS) $(COBJS) $(LDFLAGS) $< -o $@
+$(TESTS) : % : src/main_%.c $(COBJS)
+	$(cc) $(CFLAGS) $(COBJS) $(LDFLAGS) $< -o $@
 
 #cognitive_engine: src/main_cognitive_engine.cpp cbr.o sqlite3.o
