Skip to content
12 changes: 12 additions & 0 deletions src/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,18 @@ cc_binary(
],
)

cc_binary(
name = "evaluation_evolution",
srcs = [],
defines = ["BAZEL_BUILD"],
linkstatic = 1,
deps = [
"//commons:commons_lib",
"//tests/main:evaluation_evolution_main_lib",
"@mbedtls",
],
)

cc_binary(
name = "busnode",
srcs = [],
Expand Down
4 changes: 2 additions & 2 deletions src/agents/context_broker/ContextBrokerProcessor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ void ContextBrokerProcessor::create_context(shared_ptr<StoppableThread> monitor,
proxy->parameters.get_or(BaseQueryProxy::ATTENTION_UPDATE_FLAG, false);
pattern_proxy->parameters[BaseQueryProxy::USE_LINK_TEMPLATE_CACHE] =
proxy->parameters.get_or(BaseQueryProxy::USE_LINK_TEMPLATE_CACHE, false);
pattern_proxy->parameters[PatternMatchingQueryProxy::POSITIVE_IMPORTANCE_FLAG] =
proxy->parameters.get_or(PatternMatchingQueryProxy::POSITIVE_IMPORTANCE_FLAG, false);
pattern_proxy->parameters[PatternMatchingQueryProxy::POSITIVE_IMPORTANCE_FLAG] = false;
pattern_proxy->parameters[PatternMatchingQueryProxy::DISREGARD_IMPORTANCE_FLAG] = true;
pattern_proxy->parameters[BaseQueryProxy::USE_METTA_AS_QUERY_TOKENS] =
proxy->parameters.get_or(BaseQueryProxy::USE_METTA_AS_QUERY_TOKENS, false);

Expand Down
20 changes: 13 additions & 7 deletions src/agents/evolution/QueryEvolutionProcessor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ float eval_word(const string& handle, string& word) {
void QueryEvolutionProcessor::correlate_similar(shared_ptr<QueryEvolutionProxy> proxy,
shared_ptr<QueryAnswer> correlation_query_answer) {
vector<string> query_tokens;
vector<string> handle_list;
map<string, vector<string>> handle_lists;
vector<vector<string>> correlation_queries = proxy->get_correlation_queries();
vector<map<string, QueryAnswerElement>> correlation_replacements =
proxy->get_correlation_replacements();
Expand Down Expand Up @@ -349,24 +349,30 @@ void QueryEvolutionProcessor::correlate_similar(shared_ptr<QueryEvolutionProxy>
}

// Update AttentionBroker
handle_list.clear();
handle_list.push_back(correlation_query_answer->get(correlation_mappings[0].first));
handle_lists.clear();
auto pm_query = issue_correlation_query(proxy, query_tokens);
while (!pm_query->finished()) {
shared_ptr<QueryAnswer> answer = pm_query->pop();
string word;
if (answer != NULL) {
for (auto pair : correlation_mappings) {
string handle = answer->get(pair.second);
if (handle != "") {
handle_list.push_back(handle);
string key = answer->get(pair.first, true);
string handle = answer->get(pair.second, true);
if ((key != "") && (handle != "")) {
if (handle_lists.find(key) == handle_lists.end()) {
handle_lists[key] = {key};
} else {
handle_lists[key].push_back(handle);
}
}
}
} else {
Utils::sleep();
}
}
AttentionBrokerClient::asymmetric_correlate(handle_list, proxy->get_context());
for (auto pair : handle_lists) {
AttentionBrokerClient::asymmetric_correlate(pair.second, proxy->get_context());
}
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/agents/query_engine/MettaParserActions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,15 @@ void MettaParserActions::expression_end(bool toplevel, const string& metta_expre
this->element_stack.push(make_shared<Terminal>(MettaMapping::EXPRESSION_LINK_TYPE, targets));
} else {
LOG_DEBUG("Pushing LINK_TEMPLATE");
this->element_stack.push(make_shared<LinkTemplate>(
auto new_link_template = make_shared<LinkTemplate>(
MettaMapping::EXPRESSION_LINK_TYPE,
targets,
this->proxy->get_context(),
this->proxy->parameters.get<bool>(PatternMatchingQueryProxy::POSITIVE_IMPORTANCE_FLAG),
this->proxy->parameters.get<bool>(PatternMatchingQueryProxy::DISREGARD_IMPORTANCE_FLAG),
this->proxy->parameters.get<bool>(PatternMatchingQueryProxy::UNIQUE_VALUE_FLAG),
this->proxy->parameters.get<bool>(BaseQueryProxy::USE_LINK_TEMPLATE_CACHE)));
this->proxy->parameters.get<bool>(BaseQueryProxy::USE_LINK_TEMPLATE_CACHE));
this->element_stack.push(new_link_template);
}
} else if ((this->current_expression_type == AND) || (this->current_expression_type == OR)) {
if (element_stack.size() >= 2) {
Expand Down
4 changes: 3 additions & 1 deletion src/agents/query_engine/PatternMatchingQueryProcessor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ void PatternMatchingQueryProcessor::update_attention_broker_single_answer(
for (auto pair : answer->assignment.table) {
single_answer.insert(pair.second);
joint_answer.insert(pair.second);
single_answer.insert(pair.second);
}

/*
// Correlate handles which are the query answer
for (string handle : answer->handles) {
execution_stack.push(handle);
Expand All @@ -109,6 +109,7 @@ void PatternMatchingQueryProcessor::update_attention_broker_single_answer(
}
}
}
*/
if (single_answer.size() > 1) {
AttentionBrokerClient::correlate(single_answer, proxy->get_context());
} else {
Expand Down Expand Up @@ -342,6 +343,7 @@ shared_ptr<QueryElement> PatternMatchingQueryProcessor::build_link_template(
targets,
proxy->get_context(),
proxy->parameters.get<bool>(PatternMatchingQueryProxy::POSITIVE_IMPORTANCE_FLAG),
proxy->parameters.get<bool>(PatternMatchingQueryProxy::DISREGARD_IMPORTANCE_FLAG),
proxy->parameters.get<bool>(PatternMatchingQueryProxy::UNIQUE_VALUE_FLAG),
proxy->parameters.get<bool>(BaseQueryProxy::USE_LINK_TEMPLATE_CACHE));
return link_template;
Expand Down
2 changes: 2 additions & 0 deletions src/agents/query_engine/PatternMatchingQueryProxy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ using namespace query_engine;
string PatternMatchingQueryProxy::COUNT = "count";

string PatternMatchingQueryProxy::POSITIVE_IMPORTANCE_FLAG = "positive_importance_flag";
string PatternMatchingQueryProxy::DISREGARD_IMPORTANCE_FLAG = "disregard_importance_flag";
string PatternMatchingQueryProxy::UNIQUE_VALUE_FLAG = "unique_value_flag";
string PatternMatchingQueryProxy::COUNT_FLAG = "count_flag";

Expand All @@ -32,6 +33,7 @@ PatternMatchingQueryProxy::PatternMatchingQueryProxy(const vector<string>& token

void PatternMatchingQueryProxy::set_default_parameters() {
this->parameters[POSITIVE_IMPORTANCE_FLAG] = false;
this->parameters[DISREGARD_IMPORTANCE_FLAG] = false;
this->parameters[UNIQUE_VALUE_FLAG] = false;
this->parameters[COUNT_FLAG] = false;
}
Expand Down
26 changes: 15 additions & 11 deletions src/agents/query_engine/PatternMatchingQueryProxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,21 @@ class PatternMatchingQueryProxy : public BaseQueryProxy {
static string COUNT; // Delivery of the final result of a count_only query

// Query command's optional parameters
static string POSITIVE_IMPORTANCE_FLAG; // Indicates that only answers whose importance > 0
// are supposed to be returned

static string UNIQUE_VALUE_FLAG; // When true, QueryAnswers won't be allowed to have the same
// handle assigned to different values. For instance, if a
// variable V1 is assigned to a handle H1, if this parameter
// is true then it's assured that no other variable will be
// assigned with the same value H1. When this parameter is
// false (which is the default value, btw), it's possible
// to have a QueryAnswer with an assignment like this, for
// example: V1: H1, V2: H2, V3, H1.
static string POSITIVE_IMPORTANCE_FLAG; // Indicates that only answers whose importance > 0
// are supposed to be returned

static string DISREGARD_IMPORTANCE_FLAG; // When set true, importance values are not fetched from
// Attention Broker, thus the order of query answers
// are not determined by importance.

static string UNIQUE_VALUE_FLAG; // When true, QueryAnswers won't be allowed to have the same
// handle assigned to different values. For instance, if a
// variable V1 is assigned to a handle H1, if this parameter
// is true then it's assured that no other variable will be
// assigned with the same value H1. When this parameter is
// false (which is the default value, btw), it's possible
// to have a QueryAnswer with an assignment like this, for
// example: V1: H1, V2: H2, V3, H1.

static string COUNT_FLAG; // Indicates that this query is supposed to count the results and not
// actually provide the query answers (i.e. no QueryAnswer is sent
Expand Down
38 changes: 32 additions & 6 deletions src/agents/query_engine/query_element/LinkTemplate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,31 @@ LinkTemplate::LinkTemplate(const string& type,
const vector<shared_ptr<QueryElement>>& targets,
const string& context,
bool positive_importance_flag,
bool disregard_importance_flag,
bool unique_value_flag,
bool use_cache)
: link_schema(type, targets.size()) {
this->targets = targets;
this->context = context;
if (positive_importance_flag && disregard_importance_flag) {
Utils::error("Conficting settings for positive_importance_flag and disregard_importance_flag");
}
this->positive_importance_flag = positive_importance_flag;
this->disregard_importance_flag = disregard_importance_flag;
this->unique_value_flag = unique_value_flag;
this->use_cache = use_cache;
this->inner_flag = true;
this->arity = targets.size();
this->processor = nullptr;
this->random_generator =
new std::mt19937(std::chrono::system_clock::now().time_since_epoch().count());
unsigned int max_reverse_nesting = 0;
for (auto element : targets) {
if (element->reverse_nesting_level > max_reverse_nesting) {
max_reverse_nesting = element->reverse_nesting_level;
}
}
this->reverse_nesting_level = max_reverse_nesting + 1;
}

LinkTemplate::~LinkTemplate() {
Expand Down Expand Up @@ -135,8 +147,12 @@ void LinkTemplate::processor_method(shared_ptr<StoppableThread> monitor) {
handles = db->query_for_pattern(this->link_schema);
LinkTemplate::fetched_links_cache().set(link_schema_handle, handles);
}
bool flat_pattern_flag = (this->reverse_nesting_level <= 1);
LOG_DEBUG("Positive importance flag: " + string(this->positive_importance_flag ? "true" : "false"));
LOG_DEBUG("Disregard importance flag: " +
string(this->disregard_importance_flag ? "true" : "false"));
LOG_DEBUG("Unique value flag: " + string(this->unique_value_flag ? "true" : "false"));
LOG_DEBUG("Flat pattern flag: " + string(flat_pattern_flag ? "true" : "false"));
LOG_INFO("Fetched " + std::to_string(handles->size()) + " atoms in " + link_schema_handle);

vector<pair<char*, float>> tagged_handles;
Expand All @@ -146,9 +162,12 @@ void LinkTemplate::processor_method(shared_ptr<StoppableThread> monitor) {
while ((handle = iterator->next()) != nullptr) {
tagged_handles.push_back(make_pair<char*, float>((char*) handle, 0));
}
compute_importance(tagged_handles);
if (!this->disregard_importance_flag) {
compute_importance(tagged_handles);
}
}
unsigned int pending = tagged_handles.size();
unsigned int processed = 0;
unsigned int cursor = 0;
Assignment assignment(this->unique_value_flag);
unsigned int count_matched = 0;
Expand All @@ -165,13 +184,20 @@ void LinkTemplate::processor_method(shared_ptr<StoppableThread> monitor) {
handles->get_assignments_by_handle(tagged_handle.first),
handles->get_metta_expressions_by_handle(tagged_handle.first));
count_matched++;
} else if (this->link_schema.match(string(tagged_handle.first), assignment, *db.get())) {
this->source_element->add_handle(
tagged_handle.first, tagged_handle.second, assignment);
assignment.clear();
count_matched++;
} else {
if (this->link_schema.match(string(tagged_handle.first), assignment, *db.get())) {
this->source_element->add_handle(
tagged_handle.first, tagged_handle.second, assignment);
assignment.clear();
count_matched++;
}
}
}
if (!(++processed % 1000000)) {
LOG_INFO("Processed " + std::to_string(processed) + "/" +
std::to_string(tagged_handles.size()) + ". " + std::to_string(count_matched) +
" matched so far.");
}
pending--;
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/agents/query_engine/query_element/LinkTemplate.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class LinkTemplate : public QueryElement {
vector<shared_ptr<QueryElement>> targets;
string context;
bool positive_importance_flag;
bool disregard_importance_flag;
bool unique_value_flag;
bool use_cache;
bool inner_flag;
Expand All @@ -75,6 +76,7 @@ class LinkTemplate : public QueryElement {
const vector<shared_ptr<QueryElement>>& targets,
const string& context,
bool positive_importance_flag,
bool disregard_importance_flag,
bool unique_value_flag,
bool use_cache);

Expand Down
1 change: 1 addition & 0 deletions src/agents/query_engine/query_element/QueryElement.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ QueryElement::QueryElement() {
this->is_terminal = false;
this->is_operator = false;
this->arity = 0;
this->reverse_nesting_level = 0;
}

QueryElement::~QueryElement() {}
Expand Down
1 change: 1 addition & 0 deletions src/agents/query_engine/query_element/QueryElement.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class QueryElement {
string id;
string subsequent_id;
unsigned int arity;
unsigned int reverse_nesting_level;

/**
* Basic constructor which solely initialize variables.
Expand Down
2 changes: 0 additions & 2 deletions src/attention_broker/AttentionBrokerServer.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#pragma once

#define DEBUG

#include <string>
#include <unordered_map>

Expand Down
2 changes: 2 additions & 0 deletions src/scripts/bazel_build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ if [ "$BUILD_BINARIES" = true ]; then
BUILD_TARGETS+=" //:word_query"
BUILD_TARGETS+=" //:word_query_evolution"
BUILD_TARGETS+=" //:implication_query_evolution"
BUILD_TARGETS+=" //:evaluation_evolution"
BUILD_TARGETS+=" //:tests_db_loader"

# Move targets
Expand All @@ -54,6 +55,7 @@ if [ "$BUILD_BINARIES" = true ]; then
MOVE_BIN_TARGETS+=" bazel-bin/word_query"
MOVE_BIN_TARGETS+=" bazel-bin/word_query_evolution"
MOVE_BIN_TARGETS+=" bazel-bin/implication_query_evolution"
MOVE_BIN_TARGETS+=" bazel-bin/evaluation_evolution"
MOVE_BIN_TARGETS+=" bazel-bin/tests_db_loader"


Expand Down
2 changes: 1 addition & 1 deletion src/tests/cpp/iterator_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ TEST(Iterator, link_template_integration) {
auto human = make_shared<Terminal>(symbol, "\"human\"");

LinkTemplate* link_template =
new LinkTemplate("Expression", {similarity, human, v1}, "", false, false, false);
new LinkTemplate("Expression", {similarity, human, v1}, "", false, false, false, false);
link_template->build();
Iterator query_answer_iterator(link_template->get_source_element());

Expand Down
2 changes: 1 addition & 1 deletion src/tests/cpp/link_template_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ TEST(LinkTemplate, basics) {
similarity->handle = Hasher::node_handle(symbol, "Similarity");
auto human = make_shared<Terminal>(symbol, "\"human\"");

LinkTemplate link_template1("Expression", {similarity, human, v1}, "", false, false, false);
LinkTemplate link_template1("Expression", {similarity, human, v1}, "", false, false, false, false);
link_template1.build();
link_template1.get_source_element()->subsequent_id = server_node_id;
link_template1.get_source_element()->setup_buffers();
Expand Down
10 changes: 5 additions & 5 deletions src/tests/cpp/nested_link_template_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ TEST(LinkTemplate, basics) {
auto odd_link = make_shared<Terminal>(symbol, "OddLink");

LinkTemplate* inner_template_ptr =
new LinkTemplate(expression, {similarity, v1, v2}, "", false, false, false);
new LinkTemplate(expression, {similarity, v1, v2}, "", false, false, false, false);
shared_ptr<LinkTemplate> inner_template(inner_template_ptr);
LinkTemplate* outter_template_ptr =
new LinkTemplate(expression, {odd_link, inner_template}, "", false, false, false);
new LinkTemplate(expression, {odd_link, inner_template}, "", false, false, false, false);
outter_template_ptr->build();
Iterator iterator(outter_template_ptr->get_source_element());

Expand Down Expand Up @@ -59,13 +59,13 @@ TEST(LinkTemplate, nested_variables) {
auto human = make_shared<Terminal>(symbol, "\"human\"");

LinkTemplate* inner_template_ptr =
new LinkTemplate(expression, {similarity, v1, v2}, "", false, false, false);
new LinkTemplate(expression, {similarity, v1, v2}, "", false, false, false, false);
shared_ptr<LinkTemplate> inner_template(inner_template_ptr);
LinkTemplate* outter_template =
new LinkTemplate(expression, {odd_link, inner_template}, "", false, false, false);
new LinkTemplate(expression, {odd_link, inner_template}, "", false, false, false, false);
outter_template->build();
LinkTemplate* human_template =
new LinkTemplate(expression, {similarity, v1, human}, "", false, false, false);
new LinkTemplate(expression, {similarity, v1, human}, "", false, false, false, false);
human_template->build();
auto and_operator = make_shared<And<2>>(array<shared_ptr<QueryElement>, 2>(
{human_template->get_source_element(), outter_template->get_source_element()}));
Expand Down
12 changes: 12 additions & 0 deletions src/tests/main/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,15 @@ cc_library(
"//service_bus:service_bus_lib",
],
)

cc_library(
name = "evaluation_evolution_main_lib",
srcs = ["evaluation_evolution.cc"],
deps = [
"//agents/context_broker:context_broker_lib",
"//agents/evolution:evolution_lib",
"//atomdb:atomdb_singleton",
"//hasher:hasher_lib",
"//service_bus:service_bus_lib",
],
)
Loading