📄 generator.c
字号:
/*
* ARTIFICIAL INTELLIGENCE SYSTEM
*
* Copyright (C) 2007-Present Intelligence Realm Inc. All rights reserved.
*
* See LICENSE.TXT document for licensing information.
*/
#include "generator.h"int main(int argc, char **argv)
{ int neuron_count = 0; if (strlen(argv[1]) > 0) { neuron_count = atoi(argv[1]); /* allocate memory for database files ID's */
file_object_id = (char *)malloc((UUID_DATA_LENGTH + 1) * sizeof(char));
file_object_property_id = (char *)malloc((UUID_DATA_LENGTH + 1) * sizeof(char));
file_object_interaction_id = (char *)malloc((UUID_DATA_LENGTH + 1) * sizeof(char));
file_interaction_property_id = (char *)malloc((UUID_DATA_LENGTH + 1) * sizeof(char));
file_property_value_id = (char *)malloc((UUID_DATA_LENGTH + 1) * sizeof(char));
file_archive_property_value_id = (char *)malloc((UUID_DATA_LENGTH + 1) * sizeof(char));
/*
* we store into a list, the open databases handlers so that we can reuse them later on,
* without having to re-open a database each time we need to access its data
*/
db_cache_count = 0; /* initialize variables */
db_transaction_limit = 1000;
db_transaction_count = 0;
db_cache_list = (struct db_cache_type **)malloc(1 * sizeof(struct db_cache_type *)); if (DB_UNIQUE_FILE) neuron_generator_unique(neuron_count); else neuron_generator_multiple(neuron_count); /* clear memory */
int i;
if (db_cache_list)
{
for (i=0; i<db_cache_count; i++)
{
/* only the key's memory must be freed */
if (db_cache_list[i]->status == DB_MEMORY_ALLOCATED)
free(db_cache_list[i]->key);
free(db_cache_list[i]);
}
free(db_cache_list);
}
db_transaction_count = 0;
db_cache_count = 0; free(file_object_id);
free(file_object_property_id);
free(file_object_interaction_id);
free(file_interaction_property_id);
free(file_property_value_id);
free(file_archive_property_value_id); } else printf("Invalid number of neurons argument.\n");
return 0;
}void neuron_generator_unique(int number_neurons)
{
/*
* generate biophysical neurons; we must generate objects, interactions between objects, object properties and
* property values and each of these entities will reside in new database files
*/ char command[100]; db_type *dbo = NULL;
db_type *dboi = NULL;
db_type *dbop = NULL;
db_type *dbip = NULL;
db_type *dbpv = NULL;
db_type *dbapv = NULL;
db_sql *sql = NULL;
int object_id = 0;
int object_property_id = 0;
int property_id = 0;
int object_interaction_id = 0;
int interaction_object_id = 0;
int interaction_property_id = 0;
int property_value_id = 0;
int archive_property_value_id = 0;
char *file_object_path = NULL;
char *file_object_property_path = NULL;
char *file_object_interaction_path = NULL;
char *file_interaction_property_path = NULL;
char *file_property_value_path = NULL;
char *file_archive_property_value_path = NULL;
char *file_path = NULL;
int i;
/* define file path */
file_path = (char *)malloc((MAX_PATH_LENGTH + 1) * sizeof(char));
strcpy(file_path, "");
if (DBG_GENERATION) printf("file_path=%s\n", file_path);
/* allocate memory */
file_object_path = (char *)malloc((MAX_PATH_LENGTH + 1) * sizeof(char));
file_object_property_path = (char *)malloc((MAX_PATH_LENGTH + 1) * sizeof(char));
file_object_interaction_path = (char *)malloc((MAX_PATH_LENGTH + 1) * sizeof(char));
file_interaction_property_path = (char *)malloc((MAX_PATH_LENGTH + 1) * sizeof(char));
file_property_value_path = (char *)malloc((MAX_PATH_LENGTH + 1) * sizeof(char));
file_archive_property_value_path = (char *)malloc((MAX_PATH_LENGTH + 1) * sizeof(char));
/* add file folder */
strcpy(file_object_path, file_path);
strcpy(file_object_property_path, file_path);
strcpy(file_object_interaction_path, file_path);
strcpy(file_interaction_property_path, file_path);
strcpy(file_property_value_path, file_path);
strcpy(file_archive_property_value_path, file_path);
/* get unique id's for each database file */
uuid_create(file_object_id);
strcpy(file_object_property_id, file_object_id);
strcpy(file_object_interaction_id, file_object_id);
strcpy(file_interaction_property_id, file_object_id);
strcpy(file_property_value_id, file_object_id);
strcpy(file_archive_property_value_id, file_object_id);
/* database id's */
strcat(file_object_path, file_object_id);
strcat(file_object_property_path, file_object_property_id);
strcat(file_object_interaction_path, file_object_interaction_id);
strcat(file_interaction_property_path, file_interaction_property_id);
strcat(file_property_value_path, file_property_value_id);
strcat(file_archive_property_value_path, file_archive_property_value_id);
/* add database file extension */
strcat(file_object_path, ".db");
strcat(file_object_property_path, ".db");
strcat(file_object_interaction_path, ".db");
strcat(file_interaction_property_path, ".db");
strcat(file_property_value_path, ".db");
strcat(file_archive_property_value_path, ".db");
/* create empty xml file with same name as database file */ strcpy(file_path, ""); strcpy(file_path, file_object_id); strcat(file_path, ".xml"); xml_write_data(file_path); /* create database file */
db_cache_new(file_object_path, file_object_id, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);
/* Objects */
dbo = db_cache_get(file_object_id, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);
/* Object_Interactions */
dboi = dbo;
/* Object_Properties */
dbop = dbo;
/* Interaction_Properties */
dbip = dbo;
/* Property_Values */
dbpv = dbo;
/* Archive Property_Values */
dbapv = dbo;
if (DBG_GENERATION)
{
printf("file_object_path=%s\n", file_object_path);
printf("file_object_property_path=%s\n", file_object_property_path);
printf("file_object_interaction_path=%s\n", file_object_interaction_path);
printf("file_interaction_property_path=%s\n", file_interaction_property_path);
printf("file_property_value_path=%s\n", file_property_value_path);
printf("file_archive_property_value_path=%s\n", file_archive_property_value_path);
}
/* allocate memory */
sql = (char *)malloc(500 * sizeof(char));
/* create database schema */
strcpy(sql, "BEGIN TRANSACTION");
db_execute_sql(dbo, sql);
/* Objects */
strcpy(sql, "CREATE TABLE Objects (ID INTEGER NOT NULL PRIMARY KEY, Type INTEGER NOT NULL);");
db_execute_sql(dbo, sql);
/* Object_Properties */
strcpy(sql, "CREATE TABLE Object_Properties (ID INTEGER NOT NULL PRIMARY KEY, Object_ID INTEGER NOT NULL, Property_ID INTEGER NOT NULL, Unit_ID INTEGER NOT NULL);");
db_execute_sql(dbop, sql);
/* Object_Interactions */
strcpy(sql, "CREATE TABLE Object_Interactions (ID INTEGER NOT NULL PRIMARY KEY, Object_ID INTEGER NOT NULL, Interaction_Object_ID INTEGER NOT NULL);");
db_execute_sql(dboi, sql);
/* Interaction_Properties */
strcpy(sql, "CREATE TABLE Interaction_Properties (Object_Interaction_ID INTEGER NOT NULL, Property_ID INTEGER NOT NULL);");
db_execute_sql(dbip, sql);
/* Property_Values */
strcpy(sql, "CREATE TABLE Property_Values (Object_Property_ID INTEGER NOT NULL, Time_ID INTEGER NOT NULL, Property_Value VARCHAR(100) NOT NULL);");
db_execute_sql(dbpv, sql);
/* Archive_Property_Values */
strcpy(sql, "CREATE TABLE Archive_Property_Values (Object_Property_ID INTEGER NOT NULL, Time_ID INTEGER NOT NULL, Property_Value VARCHAR(100) NOT NULL);");
db_execute_sql(dbapv, sql);
/* Archive_Property_Values */
strcpy(sql, "CREATE TABLE Settings (ID INTEGER NOT NULL PRIMARY KEY, Setting_Value VARCHAR(100) NOT NULL);");
db_execute_sql(dbo, sql); /* Times */ strcpy(sql, "CREATE TABLE Times (ID INTEGER NOT NULL PRIMARY KEY, Time VARCHAR(100) NOT NULL);");
db_execute_sql(dbo, sql);
strcpy(sql, "COMMIT TRANSACTION");
db_execute_sql(dbo, sql); /* start the transaction mechanism to speed up the insert performance */
db_transaction_count = 0;
strcpy(sql, "BEGIN TRANSACTION");
db_execute_sql(dbo, sql);
if (number_neurons > 0)
{
for (i=0; i<number_neurons; i++)
{
if (DBG_GENERATION) printf("i=%d db_transaction_count=%d\n", i, db_transaction_count);
/* check if we reached the commit point */
if (db_transaction_count == db_transaction_limit)
{
strcpy(sql, "COMMIT TRANSACTION");
db_execute_sql(dbo, sql);
db_transaction_count = 0; /* reset counter */
strcpy(sql, "BEGIN TRANSACTION");
db_execute_sql(dbo, sql);
}
/* generate the data */
sprintf(sql, "INSERT INTO Objects (ID, Type) VALUES (NULL, 0)");
db_execute_sql(dbo, sql); object_id = db_last_insert_id(dbo);
db_transaction_count++; /* we increase the value for objects database only */
if (i % 2 == 1) /* soma */
{
/* we create a new interaction between the dendrite and the soma object */ sprintf(sql, "INSERT INTO Object_Interactions (ID, Object_ID, Interaction_Object_ID) VALUES (NULL, %d, %d)", object_id, interaction_object_id);
db_execute_sql(dboi, sql);
/* we enter the reverse to speed up the access to the object interactions */ sprintf(sql, "INSERT INTO Object_Interactions (ID, Object_ID, Interaction_Object_ID) VALUES (NULL, %d, %d)", interaction_object_id, object_id);
db_execute_sql(dboi, sql);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -