Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/agents/query_engine/MettaParserActions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ void MettaParserActions::expression_end(bool toplevel, const string& metta_expre
targets,
this->proxy->get_context(),
this->proxy->parameters.get<bool>(PatternMatchingQueryProxy::POSITIVE_IMPORTANCE_FLAG),
this->proxy->parameters.get<bool>(PatternMatchingQueryProxy::UNIQUE_VALUE_FLAG),
this->proxy->parameters.get<bool>(BaseQueryProxy::USE_LINK_TEMPLATE_CACHE)));
}
} else if ((this->current_expression_type == AND) || (this->current_expression_type == OR)) {
Expand Down
1 change: 1 addition & 0 deletions src/agents/query_engine/PatternMatchingQueryProcessor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,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::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::UNIQUE_VALUE_FLAG = "unique_value_flag";
string PatternMatchingQueryProxy::COUNT_FLAG = "count_flag";

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

void PatternMatchingQueryProxy::set_default_parameters() {
this->parameters[POSITIVE_IMPORTANCE_FLAG] = false;
this->parameters[UNIQUE_VALUE_FLAG] = false;
this->parameters[COUNT_FLAG] = false;
}

Expand Down
13 changes: 11 additions & 2 deletions src/agents/query_engine/PatternMatchingQueryProxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,19 @@ 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 answres whose importance > 0
static string POSITIVE_IMPORTANCE_FLAG; // Indicates that only answers whose importance > 0
// are supposed to be returned

static string COUNT_FLAG; // indicates that this query is supposed to count the results and not
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
// from the command executor and the caller of the query).

Expand Down
5 changes: 4 additions & 1 deletion src/agents/query_engine/query_element/LinkTemplate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ LinkTemplate::LinkTemplate(const string& type,
const vector<shared_ptr<QueryElement>>& targets,
const string& context,
bool positive_importance_flag,
bool unique_value_flag,
bool use_cache)
: link_schema(type, targets.size()) {
this->targets = targets;
this->context = context;
this->positive_importance_flag = positive_importance_flag;
this->unique_value_flag = unique_value_flag;
this->use_cache = use_cache;
this->inner_flag = true;
this->arity = targets.size();
Expand Down Expand Up @@ -134,6 +136,7 @@ void LinkTemplate::processor_method(shared_ptr<StoppableThread> monitor) {
LinkTemplate::fetched_links_cache().set(link_schema_handle, handles);
}
LOG_DEBUG("Positive importance flag: " + string(this->positive_importance_flag ? "true" : "false"));
LOG_DEBUG("Unique value flag: " + string(this->unique_value_flag ? "true" : "false"));
LOG_INFO("Fetched " + std::to_string(handles->size()) + " atoms in " + link_schema_handle);

vector<pair<char*, float>> tagged_handles;
Expand All @@ -147,7 +150,7 @@ void LinkTemplate::processor_method(shared_ptr<StoppableThread> monitor) {
}
unsigned int pending = tagged_handles.size();
unsigned int cursor = 0;
Assignment assignment;
Assignment assignment(this->unique_value_flag);
unsigned int count_matched = 0;
while ((pending > 0) && !monitor->stopped()) {
pair<char*, float> tagged_handle = tagged_handles[cursor++];
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 @@ -54,6 +54,7 @@ class LinkTemplate : public QueryElement {
vector<shared_ptr<QueryElement>> targets;
string context;
bool positive_importance_flag;
bool unique_value_flag;
bool use_cache;
bool inner_flag;
LinkSchema link_schema;
Expand All @@ -73,6 +74,7 @@ class LinkTemplate : public QueryElement {
const vector<shared_ptr<QueryElement>>& targets,
const string& context,
bool positive_importance_flag,
bool unique_value_flag,
bool use_cache);

void build();
Expand Down
27 changes: 24 additions & 3 deletions src/commons/Assignment.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ using namespace commons;

string Assignment::EMPTY_VALUE = "";

Assignment::Assignment() {}
Assignment::Assignment(bool unique_assignment_flag) {
this->unique_assignment_flag = unique_assignment_flag;
}

Assignment::~Assignment() {}

Expand Down Expand Up @@ -41,11 +43,21 @@ bool Assignment::is_compatible(const Assignment& other) {
return false;
}
}
if (this->unique_assignment_flag) {
for (auto other_pair : other.table) {
if ((pair.second == other_pair.second) && (pair.first != other_pair.first)) {
return false;
}
}
}
}
return true;
}

void Assignment::copy_from(const Assignment& other) { this->table = other.table; }
void Assignment::copy_from(const Assignment& other) {
this->table = other.table;
this->unique_assignment_flag = other.unique_assignment_flag;
}

void Assignment::add_assignments(const Assignment& other) {
for (auto pair : other.table) {
Expand Down Expand Up @@ -84,4 +96,13 @@ string Assignment::to_string() {

void Assignment::clear() { this->table.clear(); }

bool Assignment::operator==(const Assignment& other) const { return (this->table == other.table); }
bool Assignment::operator==(const Assignment& other) const {
return (this->table == other.table) &&
(this->unique_assignment_flag == other.unique_assignment_flag);
}

Assignment& Assignment::operator=(const Assignment& other) {
this->table = other.table;
this->unique_assignment_flag = other.unique_assignment_flag;
return *this;
}
11 changes: 10 additions & 1 deletion src/commons/Assignment.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ class Assignment {
public:
/**
* Basic constructor.
*
* @param unique_assignment_flag When true, enforces the assignment of different values for different
* variables.
*/
Assignment();
Assignment(bool unique_assignment_flag = false);

/**
* Destructor.
Expand Down Expand Up @@ -116,6 +119,11 @@ class Assignment {
*/
bool operator==(const Assignment& other) const;

/**
* Deep copy
*/
Assignment& operator=(const Assignment& other);

/**
* Clear all assignments.
*/
Expand All @@ -125,6 +133,7 @@ class Assignment {

private:
static string EMPTY_VALUE;
bool unique_assignment_flag;
};

} // namespace commons
17 changes: 17 additions & 0 deletions src/commons/processor/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
load("@rules_cc//cc:cc_library.bzl", "cc_library")

package(default_visibility = ["//visibility:public"])

cc_library(
name = "processor_lib",
srcs = [
"Processor.cc",
],
hdrs = [
"Processor.h",
],
includes = ["."],
deps = [
"//commons:commons_lib",
],
)
79 changes: 79 additions & 0 deletions src/commons/processor/Processor.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#include "Processor.h"

#include "Utils.h"

using namespace commons;
using namespace processor;

// -------------------------------------------------------------------------------------------------
// Public methods

Processor::Processor(const string& id) {
if (id == "") {
Utils::error("Invalid empty id for processor");
}
this->current_state = WAITING_SETUP;
this->id = id;
}

Processor::~Processor() {
if (this->current_state != FINISHED) {
// This doesn't throw an exception
Utils::error("Destroying processor " + this->id + " without explicitly stopping it.", false);
}
}

void Processor::setup() {
lock_guard<mutex> semaphore(this->api_mutex);
check_state("setup", WAITING_SETUP);
this->current_state = WAITING_START;
}

void Processor::start() {
lock_guard<mutex> semaphore(this->api_mutex);
check_state("start", WAITING_START);
this->current_state = WAITING_STOP;
}

void Processor::stop() {
lock_guard<mutex> semaphore(this->api_mutex);
check_state("stop", WAITING_STOP);
this->current_state = FINISHED;
}

bool Processor::is_setup() {
lock_guard<mutex> semaphore(this->api_mutex);
return (this->current_state > WAITING_SETUP);
}

bool Processor::is_finished() {
lock_guard<mutex> semaphore(this->api_mutex);
return (this->current_state == FINISHED);
}

string Processor::to_string() { return this->id; }

// -------------------------------------------------------------------------------------------------
// Private methods

void Processor::check_state(const string& action, State state) {
if (this->current_state != state) {
string error_message = "Invalid attempt to " + action + " processor " + this->id + ". ";
if (this->current_state == WAITING_SETUP) {
error_message += "Processor is not setup.";
} else if (this->current_state == WAITING_START) {
if (action == "setup") {
error_message += "Processor is already setup.";
} else {
error_message += "Processor is not running.";
}
} else if (this->current_state == WAITING_STOP) {
error_message += "Processor is already running.";
} else if (this->current_state == FINISHED) {
error_message += "Processor is finished.";
} else {
error_message += "Processor is in unexpected state: " + this->current_state;
}
Utils::error(error_message);
}
}
33 changes: 33 additions & 0 deletions src/commons/processor/Processor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#pragma once

#include <mutex>

using namespace std;

namespace processor {

/**
*
*/
class Processor {
public:
enum State { UNDEFINED = 0, WAITING_SETUP, WAITING_START, WAITING_STOP, FINISHED };

Processor(const string& id);
~Processor();
virtual void setup();
virtual void start();
virtual void stop();
bool is_setup();
bool is_finished();
string to_string();

private:
void check_state(const string& action, State state);

State current_state;
string id;
mutex api_mutex;
};

} // namespace processor
16 changes: 16 additions & 0 deletions src/tests/cpp/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,22 @@ cc_test(
],
)

cc_test(
name = "processor_test",
size = "small",
srcs = ["processor_test.cc"],
copts = [
"-Iexternal/gtest/googletest/include",
"-Iexternal/gtest/googletest",
],
linkstatic = 1,
deps = [
"//commons:commons_lib",
"//commons/processor:processor_lib",
"@com_github_google_googletest//:gtest_main",
],
)

cc_test(
name = "atom_space_test",
size = "small",
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);
new LinkTemplate("Expression", {similarity, human, v1}, "", 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);
LinkTemplate link_template1("Expression", {similarity, human, v1}, "", 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);
new LinkTemplate(expression, {similarity, v1, v2}, "", false, false, false);
shared_ptr<LinkTemplate> inner_template(inner_template_ptr);
LinkTemplate* outter_template_ptr =
new LinkTemplate(expression, {odd_link, inner_template}, "", false, false);
new LinkTemplate(expression, {odd_link, inner_template}, "", 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);
new LinkTemplate(expression, {similarity, v1, v2}, "", false, false, false);
shared_ptr<LinkTemplate> inner_template(inner_template_ptr);
LinkTemplate* outter_template =
new LinkTemplate(expression, {odd_link, inner_template}, "", false, false);
new LinkTemplate(expression, {odd_link, inner_template}, "", false, false, false);
outter_template->build();
LinkTemplate* human_template =
new LinkTemplate(expression, {similarity, v1, human}, "", false, false);
new LinkTemplate(expression, {similarity, v1, human}, "", 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
Loading