Skip to content

Commit

Permalink
DatabaseOut renamed to Database
Browse files Browse the repository at this point in the history
PR #860 comments
  • Loading branch information
Zbysekz committed Jul 9, 2020
1 parent 85ba939 commit 7c7c092
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 57 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,9 @@ Community projects for working with HTM.
#### HTMPandaVis
This project aspires to create tool that helps **visualize HTM systems in 3D** by using opensource framework for 3D rendering https://www.panda3d.org/

NetworkAPI has region called "DatabaseRegion". This region can be used for generating SQLite file and later on read by PandaVis - DashVis feature,
to show interactive plots in web browser on localhost. See [napi_hello_database](https://github.com/htm-community/htm.core/tree/master/src/examples/napi_hello) for basic usage.

For more info, visit [repository of the project](https://github.com/htm-community/HTMpandaVis)
![pandaVis1](docs/images/pandaVis1.png)

Expand Down
4 changes: 2 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ set(regions_files
htm/regions/FileOutputRegion.hpp
htm/regions/FileInputRegion.cpp
htm/regions/FileInputRegion.hpp
htm/regions/DatabaseOutRegion.cpp
htm/regions/DatabaseOutRegion.hpp
htm/regions/DatabaseRegion.cpp
htm/regions/DatabaseRegion.hpp
)

set(types_files
Expand Down
2 changes: 1 addition & 1 deletion src/examples/napi_hello/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ Each "region" is a wrapper around an algorithm. This wrapper provides a uniform
<td>for writing to a file</td>
</tr>
<tr>
<td>DatabaseOutRegion</td>
<td>DatabaseRegion</td>
<td>for writing to a SQLite3 database file</td>
</tr>
</tbody>
Expand Down
2 changes: 1 addition & 1 deletion src/examples/napi_hello/napi_hello_database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ int main(int argc, char* argv[]) {
std::shared_ptr<Region> encoder = net.addRegion("encoder", "RDSEEncoderRegion", encoder_params);
std::shared_ptr<Region> sp_global = net.addRegion("sp_global", "SPRegion", sp_global_params);
std::shared_ptr<Region> tm = net.addRegion("tm", "TMRegion", tm_params);
std::shared_ptr<Region> output = net.addRegion("output", "DatabaseOutRegion", "{outputFile: '"+ output_file + "'}");
std::shared_ptr<Region> output = net.addRegion("output", "DatabaseRegion", "{outputFile: '"+ output_file + "'}");

// Setup data flows between regions
net.link("encoder", "sp_global", "", "", "encoded", "bottomUpIn");
Expand Down
4 changes: 2 additions & 2 deletions src/htm/engine/RegionImplFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
#include <htm/regions/RDSEEncoderRegion.hpp>
#include <htm/regions/FileOutputRegion.hpp>
#include <htm/regions/FileInputRegion.hpp>
#include <htm/regions/DatabaseOutRegion.hpp>
#include <htm/regions/DatabaseRegion.hpp>
#include <htm/regions/SPRegion.hpp>
#include <htm/regions/TMRegion.hpp>
#include <htm/regions/ClassifierRegion.hpp>
Expand Down Expand Up @@ -96,7 +96,7 @@ RegionImplFactory &RegionImplFactory::getInstance() {
instance.addRegionType("TestNode", new RegisteredRegionImplCpp<TestNode>());
instance.addRegionType("FileOutputRegion", new RegisteredRegionImplCpp<FileOutputRegion>());
instance.addRegionType("FileInputRegion", new RegisteredRegionImplCpp<FileInputRegion>());
instance.addRegionType("DatabaseOutRegion", new RegisteredRegionImplCpp<DatabaseOutRegion>());
instance.addRegionType("DatabaseRegion", new RegisteredRegionImplCpp<DatabaseRegion>());
instance.addRegionType("SPRegion", new RegisteredRegionImplCpp<SPRegion>());
instance.addRegionType("TMRegion", new RegisteredRegionImplCpp<TMRegion>());
instance.addRegionType("ClassifierRegion", new RegisteredRegionImplCpp<ClassifierRegion>());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* --------------------------------------------------------------------- */

/** @file
* Implementation for DatabaseOutRegion class
* Implementation for DatabaseRegion class
*/

#include <iostream>
Expand All @@ -30,18 +30,19 @@
#include <htm/engine/Input.hpp>
#include <htm/engine/Region.hpp>
#include <htm/engine/Spec.hpp>
#include <htm/regions/DatabaseOutRegion.hpp>
#include <htm/regions/DatabaseRegion.hpp>
#include <htm/utils/Log.hpp>

#define MAX_NUMBER_OF_INPUTS 10 // maximal number of inputs/scalar streams in the database

unsigned int auxRowCnt; // Auxiliary variable for getting row count from callback

namespace htm {

static const UInt MAX_NUMBER_OF_INPUTS = 10;// maximal number of inputs/scalar streams in the database
static UInt auxRowCnt; // Auxiliary variable for getting row count from callback

static int SQLcallback(void *data, int argc, char **argv, char **azColName);

DatabaseOutRegion::DatabaseOutRegion(const ValueMap &params, Region* region)

DatabaseRegion::DatabaseRegion(const ValueMap &params, Region* region)
: RegionImpl(region), filename_(""),
dbHandle(nullptr),xTransactionActive(false) {
if (params.contains("outputFile")) {
Expand All @@ -53,22 +54,22 @@ DatabaseOutRegion::DatabaseOutRegion(const ValueMap &params, Region* region)

}

DatabaseOutRegion::DatabaseOutRegion(ArWrapper& wrapper, Region* region)
DatabaseRegion::DatabaseRegion(ArWrapper& wrapper, Region* region)
: RegionImpl(region), filename_(""),
dbHandle(nullptr),xTransactionActive(false) {
cereal_adapter_load(wrapper);
}


DatabaseOutRegion::~DatabaseOutRegion() { closeFile(); }
DatabaseRegion::~DatabaseRegion() { closeFile(); }

void DatabaseOutRegion::initialize() {
void DatabaseRegion::initialize() {
NTA_CHECK(region_ != nullptr);
// We have no outputs or parameters; just need our input.
const std::map<std::string, std::shared_ptr<Input>> inputs = region_->getInputs();


NTA_ASSERT(inputs.size()!=0) << "DatabaseOutRegion::initialize - no inputs configured\n";
NTA_ASSERT(inputs.size()!=0) << "DatabaseRegion::initialize - no inputs configured\n";

iTableCount = 0;
for (const auto & inp : inputs) {
Expand All @@ -81,7 +82,7 @@ void DatabaseOutRegion::initialize() {

}

void DatabaseOutRegion::createTable(const std::string &sTableName){
void DatabaseRegion::createTable(const std::string &sTableName){

/* Create SQL statement */
std::string sql = "CREATE TABLE "+sTableName+" (iteration INTEGER PRIMARY KEY, value REAL);";
Expand All @@ -97,7 +98,7 @@ void DatabaseOutRegion::createTable(const std::string &sTableName){
}
}

void DatabaseOutRegion::insertData(const std::string &sTableName, const std::shared_ptr<Input> inputData){
void DatabaseRegion::insertData(const std::string &sTableName, const std::shared_ptr<Input> inputData){

NTA_ASSERT(inputData->getData().getCount()==1);

Expand All @@ -122,13 +123,13 @@ void DatabaseOutRegion::insertData(const std::string &sTableName, const std::sha
}
}

void DatabaseOutRegion::compute() {
void DatabaseRegion::compute() {


const std::map<std::string, std::shared_ptr<Input>> inputs = region_->getInputs();


NTA_ASSERT(inputs.size()!=0) << "DatabaseOutRegion::initialize - no inputs configured\n";
NTA_ASSERT(inputs.size()!=0) << "DatabaseRegion::initialize - no inputs configured\n";

for (const auto & inp : inputs)
{
Expand All @@ -139,7 +140,7 @@ void DatabaseOutRegion::compute() {
}
}

void DatabaseOutRegion::ExecuteSQLcommand(std::string sqlCommand){
void DatabaseRegion::ExecuteSQLcommand(std::string sqlCommand){

char *zErrMsg;
int returnCode = sqlite3_exec(dbHandle, sqlCommand.c_str(), nullptr, 0, &zErrMsg);
Expand All @@ -153,7 +154,7 @@ void DatabaseOutRegion::ExecuteSQLcommand(std::string sqlCommand){
}
}

void DatabaseOutRegion::closeFile() {
void DatabaseRegion::closeFile() {
if (dbHandle!=NULL) {

if(xTransactionActive){
Expand All @@ -167,7 +168,7 @@ void DatabaseOutRegion::closeFile() {
}
}

void DatabaseOutRegion::openFile(const std::string &filename) {
void DatabaseRegion::openFile(const std::string &filename) {

if (dbHandle != NULL)
closeFile();
Expand All @@ -181,7 +182,7 @@ void DatabaseOutRegion::openFile(const std::string &filename) {
//file exits so delete it
ifile.close();
if(remove(filename.c_str())!=0)
NTA_THROW << "DatabaseOutRegion::openFile -- Error deleting existing database file! Filename:"
NTA_THROW << "DatabaseRegion::openFile -- Error deleting existing database file! Filename:"
<< filename;

} else {
Expand All @@ -196,7 +197,7 @@ void DatabaseOutRegion::openFile(const std::string &filename) {
{

NTA_THROW
<< "DatabaseOutRegion::openFile -- unable to create database file: "
<< "DatabaseRegion::openFile -- unable to create database file: "
<< filename.c_str()
<< " Error code:"
<< result;
Expand All @@ -221,7 +222,7 @@ static int SQLcallback(void *data, int argc, char **argv, char **azColName){
}

//iterates over all tables and sum up row count
UInt DatabaseOutRegion::getRowCount(){
UInt DatabaseRegion::getRowCount(){

UInt sumRowCount = 0;

Expand All @@ -246,7 +247,7 @@ UInt DatabaseOutRegion::getRowCount(){
return sumRowCount;
}

void DatabaseOutRegion::setParameterString(const std::string &paramName,
void DatabaseRegion::setParameterString(const std::string &paramName,
Int64 index, const std::string &s) {

if (paramName == "outputFile") {
Expand All @@ -256,21 +257,21 @@ void DatabaseOutRegion::setParameterString(const std::string &paramName,
closeFile();
openFile(s);
} else {
NTA_THROW << "DatabaseOutRegion -- Unknown string parameter " << paramName;
NTA_THROW << "DatabaseRegion -- Unknown string parameter " << paramName;
}
}

std::string DatabaseOutRegion::getParameterString(const std::string &paramName,
std::string DatabaseRegion::getParameterString(const std::string &paramName,
Int64 index) {
if (paramName == "outputFile") {
return filename_;
} else {
NTA_THROW << "DatabaseOutRegion -- unknown parameter " << paramName;
NTA_THROW << "DatabaseRegion -- unknown parameter " << paramName;
}
}

std::string
DatabaseOutRegion::executeCommand(const std::vector<std::string> &args,
DatabaseRegion::executeCommand(const std::vector<std::string> &args,
Int64 index) {
NTA_CHECK(args.size() > 0);
// Process the flushFile command
Expand All @@ -280,22 +281,22 @@ DatabaseOutRegion::executeCommand(const std::vector<std::string> &args,
return std::to_string(getRowCount());
}else if (args[0] == "commitTransaction") {
if(xTransactionActive){
ExecuteSQLcommand("END TRANSACTION");//ends transaction. Now it flushes cache to the file for.
ExecuteSQLcommand("END TRANSACTION");//ends transaction. Now it flushes cache to the file.
xTransactionActive=false;
}else NTA_THROW << "DatabaseOutRegion: Cannot commit transaction, transaction is not active!";
}else NTA_THROW << "DatabaseRegion: Cannot commit transaction, transaction is not active!";
}
else {
NTA_THROW << "DatabaseOutRegion: Unknown execute '" << args[0] << "'";
NTA_THROW << "DatabaseRegion: Unknown execute '" << args[0] << "'";
}

return "";
}

Spec *DatabaseOutRegion::createSpec() {
Spec *DatabaseRegion::createSpec() {

auto ns = new Spec;
ns->description =
"DatabaseOutRegion is a node that writes multiple scalar streams "
"DatabaseRegion is a node that writes multiple scalar streams "
"to a SQLite3 database file (.db). The target filename is specified "
"using the 'outputFile' parameter at run time. On each "
"compute, all inputs are written "
Expand Down Expand Up @@ -337,16 +338,16 @@ Spec *DatabaseOutRegion::createSpec() {
return ns;
}

size_t DatabaseOutRegion::getNodeOutputElementCount(const std::string &outputName) const {
size_t DatabaseRegion::getNodeOutputElementCount(const std::string &outputName) const {
NTA_THROW
<< "DatabaseOutRegion::getNodeOutputElementCount -- unknown output '"
<< "DatabaseRegion::getNodeOutputElementCount -- unknown output '"
<< outputName << "'";
}


bool DatabaseOutRegion::operator==(const RegionImpl &o) const {
if (o.getType() != "DatabaseOutRegion") return false;
DatabaseOutRegion& other = (DatabaseOutRegion&)o;
bool DatabaseRegion::operator==(const RegionImpl &o) const {
if (o.getType() != "DatabaseRegion") return false;
DatabaseRegion& other = (DatabaseRegion&)o;
if (filename_ != other.filename_) return false;

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
* --------------------------------------------------------------------- */

/** @file
* Declarations for DatabaseOutRegion class
* Declarations for DatabaseRegion class
*/

//----------------------------------------------------------------------

#ifndef SRC_HTM_REGIONS_DATABASEOUTREGION_HPP_
#define SRC_HTM_REGIONS_DATABASEOUTREGION_HPP_
#ifndef SRC_HTM_REGIONS_DatabaseRegion_HPP_
#define SRC_HTM_REGIONS_DatabaseRegion_HPP_

//----------------------------------------------------------------------

Expand All @@ -38,9 +38,12 @@ namespace htm {


/**
* DatabaseOutRegion is region that takes inputs and writes them to
* DatabaseRegion is region that takes inputs and writes them to
* the SQLite3 database file.
*
* The recorded database file can be visualized using HTMPandaVis at
* https://github.com/htm-community/HTMpandaVis
*
* Inputs can be now scalars (floats). they have names like:
* (dataIn0,dataIn1 ... up to MAX_NUMBER_OF_INPUTS)
* For each dataIn input, new sole table will be created with prefix
Expand All @@ -49,7 +52,7 @@ namespace htm {
* it is INTEGER PRIMARY KEY.
*
*/
class DatabaseOutRegion : public RegionImpl, Serializable {
class DatabaseRegion : public RegionImpl, Serializable {
public:
static Spec *createSpec();
size_t getNodeOutputElementCount(const std::string &outputName) const override;
Expand All @@ -59,11 +62,11 @@ class DatabaseOutRegion : public RegionImpl, Serializable {

void initialize() override;

DatabaseOutRegion(const ValueMap &params, Region *region);
DatabaseRegion(const ValueMap &params, Region *region);

DatabaseOutRegion(ArWrapper& wrapper, Region *region);
DatabaseRegion(ArWrapper& wrapper, Region *region);

virtual ~DatabaseOutRegion();
virtual ~DatabaseRegion();


CerealAdapter; // see Serializable.hpp
Expand All @@ -84,7 +87,7 @@ class DatabaseOutRegion : public RegionImpl, Serializable {
}

bool operator==(const RegionImpl &other) const override;
inline bool operator!=(const DatabaseOutRegion &other) const {
inline bool operator!=(const DatabaseRegion &other) const {
return !operator==(other);
}

Expand All @@ -109,14 +112,14 @@ class DatabaseOutRegion : public RegionImpl, Serializable {
bool xTransactionActive;

/// Disable unsupported default constructors
DatabaseOutRegion(const DatabaseOutRegion &);
DatabaseOutRegion &operator=(const DatabaseOutRegion &);
DatabaseRegion(const DatabaseRegion &);
DatabaseRegion &operator=(const DatabaseRegion &);

}; // end class DatabaseOutRegion
}; // end class DatabaseRegion

//----------------------------------------------------------------------

} // namespace htm


#endif /* SRC_HTM_REGIONS_DATABASEOUTREGION_HPP_ */
#endif /* SRC_HTM_REGIONS_DatabaseRegion_HPP_ */
2 changes: 1 addition & 1 deletion src/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ set(regions_tests
unit/regions/SPRegionTest.cpp
unit/regions/TMRegionTest.cpp
unit/regions/VectorFileTest.cpp
unit/regions/DatabaseOutRegionTest.cpp
unit/regions/DatabaseRegionTest.cpp
)


Expand Down
Loading

0 comments on commit 7c7c092

Please sign in to comment.