From 92c147350eb23103a99f06828d8f91e8e02536dd Mon Sep 17 00:00:00 2001 From: CCG Date: Tue, 3 Feb 2026 17:02:50 -0300 Subject: [PATCH] Remove old atom_space Context logic. --- src/atom_space/AtomSpace.cc | 19 -- src/atom_space/AtomSpace.h | 28 --- src/atom_space/BUILD | 16 -- src/atom_space/Context.cc | 203 ------------------ src/atom_space/Context.h | 51 ----- src/tests/cpp/BUILD | 1 - src/tests/cpp/context_test.cc | 69 ------ .../main/implication_query_evolution_main.cc | 1 - src/tests/main/word_query_evolution_main.cc | 1 - 9 files changed, 389 deletions(-) delete mode 100644 src/atom_space/Context.cc delete mode 100644 src/atom_space/Context.h diff --git a/src/atom_space/AtomSpace.cc b/src/atom_space/AtomSpace.cc index 3c88d26b9..f21f64b80 100644 --- a/src/atom_space/AtomSpace.cc +++ b/src/atom_space/AtomSpace.cc @@ -150,25 +150,6 @@ void AtomSpace::commit_changes(Scope scope) { } // ------------------------------------------------------------------------------------------------- -shared_ptr AtomSpace::create_context(const string& context_name, Atom& atom_key) { - Context* context = new Context(context_name, atom_key); - return shared_ptr(context); -} - -shared_ptr AtomSpace::create_context(const string& context_name) { - UntypedVariable v1("v1"); - Context* context = new Context(context_name, v1); - return shared_ptr(context); -} - -shared_ptr AtomSpace::create_context( - const string& context_name, - const vector& query, - const vector> determiner_schema, - vector stimulus_schema) { - Context* context = new Context(context_name, query, determiner_schema, stimulus_schema); - return shared_ptr(context); -} // PRIVATE METHODS ///////////////////////////////////////////////////////////////////////////////// diff --git a/src/atom_space/AtomSpace.h b/src/atom_space/AtomSpace.h index 60acd5b70..0aef79e57 100644 --- a/src/atom_space/AtomSpace.h +++ b/src/atom_space/AtomSpace.h @@ -4,7 +4,6 @@ #include "AtomDBAPITypes.h" #include "AtomDBSingleton.h" -#include "Context.h" #include "Link.h" #include "Node.h" #include "PatternMatchingQueryProxy.h" @@ -176,33 +175,6 @@ class AtomSpace { */ void commit_changes(Scope scope = LOCAL_AND_REMOTE); - /** - * Create and return a context object. - * - * Context objects are used to make queries in the atom space. - * - * @param name Context name. - * @param atom_key Key used to match toplevel atoms; only matching toplevel atoms will be considered. - * @return A newly created Context object. - */ - shared_ptr create_context(const string& context_name, Atom& atom_key); - - shared_ptr create_context( - const string& context_name, - const vector& query, - const vector> determiner_schema, - vector stimulus_schema); - - /** - * Create and return a context object passing UntypedVariable (which matches everything) as atom key. - * - * Context objects are used to make queries in the atom space. - * - * @param name Context name. - * @return A newly created Context object. - */ - shared_ptr create_context(const string& context_name); - protected: shared_ptr db; // to allow mocking in tests diff --git a/src/atom_space/BUILD b/src/atom_space/BUILD index 74f90c37d..b850a8e6d 100644 --- a/src/atom_space/BUILD +++ b/src/atom_space/BUILD @@ -8,7 +8,6 @@ cc_library( hdrs = ["AtomSpace.h"], includes = ["."], deps = [ - ":context", "//agents/query_engine:query_engine_lib", "//atomdb:atomdb_api_types", "//atomdb:atomdb_singleton", @@ -19,18 +18,3 @@ cc_library( "//service_bus:service_bus_lib", ], ) - -cc_library( - name = "context", - srcs = ["Context.cc"], - hdrs = ["Context.h"], - includes = ["."], - deps = [ - "//agents/query_engine:pattern_matching_query_proxy", - "//atomdb:atomdb_singleton", - "//attention_broker:attention_broker_client", - "//commons/atoms:atoms_lib", - "//hasher:hasher_lib", - "//service_bus:service_bus_lib", - ], -) diff --git a/src/atom_space/Context.cc b/src/atom_space/Context.cc deleted file mode 100644 index 4d667174f..000000000 --- a/src/atom_space/Context.cc +++ /dev/null @@ -1,203 +0,0 @@ -#include "Context.h" - -#define LOG_LEVEL INFO_LEVEL -#include -#include - -#include "AtomDBSingleton.h" -#include "AttentionBrokerClient.h" -#include "Hasher.h" -#include "Logger.h" -#include "PatternMatchingQueryProxy.h" -#include "ServiceBusSingleton.h" - -using namespace atom_space; -using namespace atoms; -using namespace atomdb; -using namespace attention_broker; -using namespace query_engine; -using namespace service_bus; -using namespace commons; - -// ------------------------------------------------------------------------------------------------- -// Public methods - -Context::Context(const string& name, Atom& atom_key) { - // Toplevel Atom - init(name); - if (!this->cached) { - string ID = RedisMongoDB::MONGODB_FIELD_NAME[MONGODB_FIELD::ID]; - string TARGETS = RedisMongoDB::MONGODB_FIELD_NAME[MONGODB_FIELD::TARGETS]; - string TYPE = RedisMongoDB::MONGODB_FIELD_NAME[MONGODB_FIELD::NAMED_TYPE]; - auto documents = AtomDBSingleton::get_instance()->get_matching_atoms(true, atom_key); - vector determiners; - for (const auto& document : documents) { - if (document->contains(TARGETS)) { // if is link - determiners.clear(); - string handle = string(document->get(ID)); - determiners.push_back(handle); - this->to_stimulate[handle] = 1; - unsigned int arity = document->get_size(TARGETS); - for (unsigned int i = 1; i < arity; i++) { - determiners.push_back(string(document->get(TARGETS, i))); - } - this->determiner_request.push_back(determiners); - } - } - } - update_attention_broker(); -} - -Context::Context(const string& name, - const vector& query, - const vector>& determiner_schema, - vector& stimulus_schema) { - // Query - init(name); - if (!this->cached) { - add_determiners(query, determiner_schema, stimulus_schema); - cache_write(); - } - update_attention_broker(); -} - -void Context::add_determiners( - const vector& query, - const vector>& determiner_schema, - vector& stimulus_schema) { - this->determiner_request.clear(); - this->to_stimulate.clear(); - - // Query - auto proxy = make_shared(query, this->key); - proxy->parameters[BaseQueryProxy::UNIQUE_ASSIGNMENT_FLAG] = true; - proxy->parameters[BaseQueryProxy::ATTENTION_UPDATE_FLAG] = false; - proxy->parameters[BaseQueryProxy::USE_LINK_TEMPLATE_CACHE] = false; - proxy->parameters[PatternMatchingQueryProxy::POSITIVE_IMPORTANCE_FLAG] = false; - proxy->parameters[BaseQueryProxy::USE_METTA_AS_QUERY_TOKENS] = false; - - ServiceBusSingleton::get_instance()->issue_bus_command(proxy); -#if LOG_LEVEL >= DEBUG_LEVEL - unsigned int count_query_answer = 0; -#endif - string source, target; - while (!proxy->finished()) { - shared_ptr answer = proxy->pop(); - if (answer != NULL) { - for (auto pair : determiner_schema) { - source = answer->get(pair.first, true); - target = answer->get(pair.second, true); - if ((source != "") && (target != "")) { - this->determiner_request.push_back({source, target}); - } - } - for (auto element : stimulus_schema) { - target = answer->get(element, true); - if (target != "") { - this->to_stimulate[answer->get(element)] = 1; - } - } -#if LOG_LEVEL >= DEBUG_LEVEL - if (!(++count_query_answer % 100000)) { - LOG_DEBUG("Number of QueryAnswer objects processed so far: " + - std::to_string(count_query_answer)); - } -#endif - } else { - Utils::sleep(); - } - } -} - -Context::~Context() { - // TODO delete context in AttentionBroker -} - -void Context::init(const string& name) { - this->name = name; - this->key = Hasher::context_handle(name); - this->cache_file_name = CACHE_FILE_NAME_PREFIX + this->key + ".txt"; - this->cached = cache_read(); -} - -void Context::cache_write() { - LOG_INFO("Caching computed context into file: " + this->cache_file_name); - - ofstream file(this->cache_file_name); - - if (!file.is_open()) { - LOG_ERROR("Couldn't open file " + this->cache_file_name + " for writing"); - LOG_INFO("Context info wont be cached"); - return; - } - - file << this->to_stimulate.size() << endl; - for (auto pair : this->to_stimulate) { - file << pair.first << endl; - } - file << this->determiner_request.size() << endl; - for (auto sub_vector : this->determiner_request) { - file << sub_vector.size() << endl; - for (string handle : sub_vector) { - file << handle << endl; - } - } - - file.close(); -} - -static inline void read_line(ifstream& file, string& line) { - if (!getline(file, line)) { - Utils::error("Error reading a line from cache file"); - } -} - -bool Context::cache_read() { - ifstream file(this->cache_file_name); - if (file.is_open()) { - LOG_INFO("Reading Context info from cache file: " + this->cache_file_name); - } else { - LOG_INFO("Couldn't open file " + this->cache_file_name + " for reading"); - LOG_INFO("No cached info will be used"); - return false; - } - - string line; - read_line(file, line); - int size = Utils::string_to_int(line); - for (int i = 0; i < size; i++) { - read_line(file, line); - this->to_stimulate[line] = 1; - } - read_line(file, line); - size = Utils::string_to_int(line); - this->determiner_request.reserve(size); - vector sub_vector; - for (int i = 0; i < size; i++) { - read_line(file, line); - int sub_vector_size = Utils::string_to_int(line); - sub_vector.clear(); - sub_vector.reserve(sub_vector_size); - for (int j = 0; j < sub_vector_size; j++) { - read_line(file, line); - sub_vector.push_back(line); - } - this->determiner_request.push_back(sub_vector); - } - - file.close(); - return true; -} - -void Context::update_attention_broker() { - AttentionBrokerClient::set_determiners(this->determiner_request, this->key); - if (this->to_stimulate.size() > 0) { - AttentionBrokerClient::stimulate(this->to_stimulate, this->key); - } - this->to_stimulate.clear(); - this->determiner_request.clear(); -} - -const string& Context::get_name() { return this->name; } - -const string& Context::get_key() { return this->key; } diff --git a/src/atom_space/Context.h b/src/atom_space/Context.h deleted file mode 100644 index acb4f1999..000000000 --- a/src/atom_space/Context.h +++ /dev/null @@ -1,51 +0,0 @@ -#pragma once - -#include -#include - -#include "Atom.h" -#include "QueryAnswer.h" - -#define CACHE_FILE_NAME_PREFIX "_CONTEXT_CACHE_" - -using namespace std; -using namespace atoms; -using namespace query_engine; - -namespace atom_space { - -/** - * - */ -class Context { - friend class AtomSpace; - - public: - ~Context(); - void add_determiners(const vector& query, - const vector>& determiner_schema, - vector& stimulus_schema); - const string& get_key(); - const string& get_name(); - - protected: - Context(const string& name, Atom& atom_key); - Context(const string& name, - const vector& query, - const vector>& determiner_schema, - vector& stimulus_schema); - - private: - void init(const string& name); - void cache_write(); - bool cache_read(); - void update_attention_broker(); - map to_stimulate; - vector> determiner_request; - bool cached; - string cache_file_name; - string name; - string key; -}; - -} // namespace atom_space diff --git a/src/tests/cpp/BUILD b/src/tests/cpp/BUILD index 1d27f686e..11e66f14d 100644 --- a/src/tests/cpp/BUILD +++ b/src/tests/cpp/BUILD @@ -489,7 +489,6 @@ cc_test( linkstatic = 1, deps = [ "//agents/context_broker:context_broker_lib", - "//atom_space:context", "//atomdb", "//attention_broker:attention_broker_lib", "//tests/cpp/test_commons", diff --git a/src/tests/cpp/context_test.cc b/src/tests/cpp/context_test.cc index 5306322c0..aefb85491 100644 --- a/src/tests/cpp/context_test.cc +++ b/src/tests/cpp/context_test.cc @@ -1,5 +1,3 @@ -#include "Context.h" - #include "AtomDBSingleton.h" #include "AttentionBrokerClient.h" #include "ContextBrokerProcessor.h" @@ -20,7 +18,6 @@ #include "Logger.h" using namespace atomdb; -using namespace atom_space; using namespace attention_broker; using namespace context_broker; using namespace query_engine; @@ -48,72 +45,6 @@ class ContextTest : public ::testing::Test { shared_ptr db; }; -class TestContext : public Context { - public: - TestContext(const string& name, Atom& atom_key) : Context(name, atom_key) {} -}; - -TEST_F(ContextTest, basics) { - random_init(); - - string node1 = Hasher::node_handle("Symbol", "\"human\""); // 0 - string node2 = Hasher::node_handle("Symbol", "\"monkey\""); // 1 - string node3 = Hasher::node_handle("Symbol", "\"chimp\""); // 2 - string node4 = Hasher::node_handle("Symbol", "\"ent\""); // 3 - string node5 = Hasher::node_handle("Symbol", "Similarity"); // 4 - string link1 = Hasher::link_handle("Expression", {node5, node1, node2}); // 5 - string link2 = Hasher::link_handle("Expression", {node5, node1, node3}); // 6 - string link3 = Hasher::link_handle("Expression", {node5, node1, node4}); // 7 - - vector tokens = {"LINK_TEMPLATE", - "Expression", - "3", - "NODE", - "Symbol", - "Similarity", - "NODE", - "Symbol", - "\"human\"", - "VARIABLE", - "v1"}; - vector handles = {node1, node2, node3, node4, node5, link1, link2, link3}; - vector importance; - - // Toplevel Atom based context - LinkSchema atom_key(tokens); - TestContext* context1 = new TestContext(random_handle(), atom_key); - - importance.clear(); - AttentionBrokerClient::get_importance(handles, context1->get_key(), importance); - for (unsigned int i = 0; i < handles.size(); i++) { - if (i <= 4) { - EXPECT_TRUE(importance[i] == 0); - } else { - EXPECT_TRUE(importance[i] > 0); - } - cout << "importance[" << i << "]: " << importance[i] << endl; - } - EXPECT_TRUE(double_equals(importance[5], importance[6])); - EXPECT_TRUE(double_equals(importance[5], importance[7])); - - AttentionBrokerClient::stimulate({{node2, 1}}, context1->get_key()); - importance.clear(); - AttentionBrokerClient::get_importance(handles, context1->get_key(), importance); - for (unsigned int i = 0; i < handles.size(); i++) { - cout << "importance[" << i << "]: " << importance[i] << endl; - } - EXPECT_TRUE(importance[0] == 0); - EXPECT_TRUE(importance[1] > 0); - EXPECT_TRUE(importance[2] == 0); - EXPECT_TRUE(importance[3] == 0); - EXPECT_TRUE(importance[4] == 0); - EXPECT_TRUE(importance[5] > 0); - EXPECT_TRUE(importance[6] > 0); - EXPECT_TRUE(importance[7] > 0); - EXPECT_TRUE(importance[5] > importance[6]); - EXPECT_TRUE(double_equals(importance[6], importance[7])); -} - TEST_F(ContextTest, ContextBroker) { string query_agent_id = "localhost:50000"; string context_processor_id = "localhost:50001"; diff --git a/src/tests/main/implication_query_evolution_main.cc b/src/tests/main/implication_query_evolution_main.cc index bdb241338..31a9eb40c 100644 --- a/src/tests/main/implication_query_evolution_main.cc +++ b/src/tests/main/implication_query_evolution_main.cc @@ -10,7 +10,6 @@ #include "AtomDBSingleton.h" #include "AtomSpace.h" #include "AttentionBrokerClient.h" -#include "Context.h" #include "ContextBrokerProxy.h" #include "CountLetterFunction.h" #include "FitnessFunctionRegistry.h" diff --git a/src/tests/main/word_query_evolution_main.cc b/src/tests/main/word_query_evolution_main.cc index b551be73e..8233dbf82 100644 --- a/src/tests/main/word_query_evolution_main.cc +++ b/src/tests/main/word_query_evolution_main.cc @@ -5,7 +5,6 @@ #include #include "AtomDBSingleton.h" -#include "Context.h" #include "ContextBrokerProxy.h" #include "CountLetterFunction.h" #include "FitnessFunctionRegistry.h"