1 | /* |
---|
2 | Copyright 2009 Virginia Polytechnic Institute and State University |
---|
3 | |
---|
4 | Licensed under the Apache License, Version 2.0 (the "License"); |
---|
5 | you may not use this file except in compliance with the License. |
---|
6 | You may obtain a copy of the License at |
---|
7 | |
---|
8 | http://www.apache.org/licenses/LICENSE-2.0 |
---|
9 | |
---|
10 | Unless required by applicable law or agreed to in writing, software |
---|
11 | distributed under the License is distributed on an "AS IS" BASIS, |
---|
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
13 | See the License for the specific language governing permissions and |
---|
14 | limitations under the License. |
---|
15 | */ |
---|
16 | |
---|
17 | /*! This header contains the declaration and a full implementation of |
---|
18 | * the CBR class - the default CROSS case-based reasoner, which can be used as a |
---|
19 | * backend for cognitive engines. |
---|
20 | */ |
---|
21 | |
---|
22 | |
---|
23 | #ifndef CBR_H |
---|
24 | #define CBR_H |
---|
25 | |
---|
26 | #include <cstdlib> |
---|
27 | #include <cstring> |
---|
28 | #include <cstdio> |
---|
29 | #include <stdint.h> |
---|
30 | #include <string> |
---|
31 | |
---|
32 | #include <sqlite3.h> |
---|
33 | |
---|
34 | #include "vtcross/common.h" |
---|
35 | #include "vtcross/debug.h" |
---|
36 | #include "vtcross/error.h" |
---|
37 | |
---|
38 | |
---|
39 | #define DATABASENAME "cross_cbr" |
---|
40 | |
---|
41 | |
---|
42 | /*! \brief Case-Based Reasoner class declaration. |
---|
43 | * |
---|
44 | * The CBR class is designed to used as either as-is, or as a parent class. All |
---|
45 | * functions are declared virtual, and internal members are 'protected' rather |
---|
46 | * than private. If you require functionality in a CBR not specifically provided |
---|
47 | * by this class, include this header in your source file, create a new class |
---|
48 | * that derives from CBR, and implement your desired functionality over the |
---|
49 | * original virtual functions as necessary. |
---|
50 | */ |
---|
51 | class CBR |
---|
52 | { |
---|
53 | public: |
---|
54 | /*! \brief Constructors for the CBR class. |
---|
55 | * |
---|
56 | * Note that the default constructor |
---|
57 | * must be defined inline here so that super-calls from child classes |
---|
58 | * don't fail (i.e. we cannot rely on the compiler-provided constructor. */ |
---|
59 | CBR(){}; |
---|
60 | CBR(std::string _filename, std::string _tablename, std::string _cols[], uint32_t _len); |
---|
61 | CBR(std::string _filename, std::string _tablename, std::string _cols[], \ |
---|
62 | std::string _primcols[], uint32_t _len, uint32_t _primlen); |
---|
63 | |
---|
64 | /*! \brief Destructors for the CBR class. |
---|
65 | * |
---|
66 | * Destructor for the CBR class. Note that this destructor will be |
---|
67 | * called automatically by any derived classes, and so child classes |
---|
68 | * should not repeat the freeing actions performed in this function. */ |
---|
69 | virtual ~CBR(); |
---|
70 | |
---|
71 | /*! \brief Open/Create a sqlite database for the CBR. |
---|
72 | * |
---|
73 | * This function opens the CROSS database, or if it has not been |
---|
74 | * created yet, creates it. */ |
---|
75 | virtual int32_t OpenDatabase(); |
---|
76 | |
---|
77 | /*! \brief Execute a sqlite command. |
---|
78 | * |
---|
79 | * Construct and execute a sqlite3 command and pass the return code back. */ |
---|
80 | virtual int32_t ExecuteCommand(); |
---|
81 | |
---|
82 | /*! \brief Search the sqlite3 database. |
---|
83 | * |
---|
84 | * Execute a sqlite3 search command and store the results in the passed |
---|
85 | * retvals argument. */ |
---|
86 | virtual int32_t ExecuteSearchCommand(float *_retvals); |
---|
87 | |
---|
88 | /*! \brief Print the CROSS sqlite database. */ |
---|
89 | virtual void Print(); |
---|
90 | |
---|
91 | /*! \brief Search the CBR database. |
---|
92 | * |
---|
93 | * Search the CROSS database for specific fields and store the results |
---|
94 | * in the passed retvals argument. */ |
---|
95 | virtual int32_t Search(std::string _names[], int32_t *_ops, float *_vals, \ |
---|
96 | uint32_t _n, float *_retvals); |
---|
97 | virtual int32_t SearchSum(std::string _name, float *_retvals); |
---|
98 | virtual int32_t SearchRand(std::string _names[], int32_t *_ops, float *_vals, uint32_t _n, \ |
---|
99 | float *_retvals); |
---|
100 | |
---|
101 | /*! \brief Update an entry in the CBR database. */ |
---|
102 | virtual int32_t Update(std::string _where[], std::string _set[], float *_wherevals, \ |
---|
103 | float *_setvals, uint32_t _wherelen, uint32_t _setlen); |
---|
104 | |
---|
105 | |
---|
106 | /*! \brief Add a row to the CROSS sqlite3 database. */ |
---|
107 | virtual int32_t AddRow(std::string _cols[], float *_vals, uint32_t _len); |
---|
108 | |
---|
109 | protected: |
---|
110 | std::string filename; |
---|
111 | std::string tablename; |
---|
112 | std::string command; |
---|
113 | |
---|
114 | sqlite3 *db; |
---|
115 | uint32_t numColumns; |
---|
116 | }; |
---|
117 | |
---|
118 | #endif |
---|