Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
6 changes: 4 additions & 2 deletions src/agents/link_creation_agent/LinkCreationProcessor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void LinkCreationProcessor::thread_process_one_query(shared_ptr<StoppableThread>
proxy->untokenize(proxy->args);
string command = proxy->get_command();
if (command == ServiceBus::LINK_CREATION) {
LOG_INFO("Proxy: " << proxy->to_string());
LOG_INFO("(Thread: " + std::to_string(gettid()) + ") Proxy: " << proxy->to_string());
this->link_creation(monitor, proxy);
} else {
RAISE_ERROR("Invalid command " + command + " in LinkCreationProcessor");
Expand Down Expand Up @@ -182,7 +182,8 @@ void LinkCreationProcessor::link_creation(shared_ptr<StoppableThread> monitor,
count_created += stats.created;
count_used_query_answers++;
count_query_answers_in_cycle++;
LOG_DEBUG("Created links: " + std::to_string(count_query_answers_in_cycle));
LOG_DEBUG("Answers used to create links: " +
std::to_string(count_query_answers_in_cycle));
unproductive_visit = 0;
visit_attempts = 0;
proxy->push(query_answer);
Expand Down Expand Up @@ -210,6 +211,7 @@ void LinkCreationProcessor::link_creation(shared_ptr<StoppableThread> monitor,
}
}
proxy->flush_determiners();
proxy->flush_stimuli();
proxy->cycle_ended();
if (!pm_proxy->finished()) {
// stopping pattern matching query
Expand Down
23 changes: 22 additions & 1 deletion src/agents/link_creation_agent/LinkCreationProxy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ string LinkCreationProxy::MAX_VISIT_ATTEMPTS_PER_ROUND = "max_visit_attempts_per
string LinkCreationProxy::MAX_ROUNDS = "max_rounds";
string LinkCreationProxy::LINK_CREATION_STRENGTH_THRESHOLD = "link_creation_strength_threshold";
string LinkCreationProxy::LINK_CREATION_LOG_FILE_NAME = "link_creation_log_file_name";
string LinkCreationProxy::LINK_CREATION_SPREAD_ACTIVATION = "link_creation_spread_activation";
string LinkCreationProxy::LOG_NEW_LINKS = "log_new_links";
string LinkCreationProxy::LINK_CREATOR_EXTRA_PARAMETERS = "link_creator_extra_parameters";

Expand Down Expand Up @@ -124,7 +125,7 @@ bool LinkCreationProxy::stop_criteria_met() {

void LinkCreationProxy::set_link_creator_function_tag(const string& tag) {
STACK_TRACE();
LOG_INFO("LinkCreator: " + tag);
LOG_DEBUG("LinkCreator: " + tag);
lock_guard<mutex> semaphore(this->api_mutex);
if (tag == LinkCreatorRegistry::REMOTE_FUNCTION) {
RAISE_ERROR("Remote evaluation of link creators is not implemented yet.");
Expand All @@ -147,6 +148,9 @@ void LinkCreationProxy::set_link_creator_function_tag(const string& tag) {
this->parameters.get<bool>(LOG_NEW_LINKS));
this->link_creation_function_object->extra_parameters(
this->parameters.get_or<string>(LINK_CREATOR_EXTRA_PARAMETERS, ""));
this->link_creation_function_object->set_stimuli(
this->parameters.get_or<bool>(LINK_CREATION_SPREAD_ACTIVATION, false));
this->link_creation_function_object->set_context(get_context());
}
}
}
Expand All @@ -173,6 +177,23 @@ void LinkCreationProxy::flush_determiners() {
}
}

void LinkCreationProxy::flush_stimuli() {
STACK_TRACE();
if ((this->link_creation_function_object != nullptr) &&
(this->link_creation_function_object->buffer_stimuli().size() > 0)) {
AttentionBrokerClient::stimulate(this->link_creation_function_object->buffer_stimuli(),
get_context());
this->link_creation_function_object->clear_stimuli();
}
}

void LinkCreationProxy::cycle_ended() {
if (this->link_creation_function_object != nullptr) {
this->link_creation_function_object->reset_visited();
}
BaseQueryProxy::cycle_ended();
}

// ---------------------------------------------------------------------------------------------
// Virtual superclass API and the piggyback methods called by it

Expand Down
30 changes: 23 additions & 7 deletions src/agents/link_creation_agent/LinkCreationProxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,18 @@ class LinkCreationProxy : public BaseQueryProxy {

// Optional parameter which are not part of the configuration file
static string LOG_NEW_LINKS;
static string LINK_CREATOR_EXTRA_PARAMETERS; // LINK_CREATOR_EXTRA_PARAMETERS is tipically
// parsed as a list of tokens separated by a
// character defined in
// CustomizableLinkCreator::EXTRA_PARAMETERS_SPLIT_CHAR
// If the separator character is used inside
// LINK_CREATOR_EXTRA_PARAMETERS, the parsing
// will fail, even if it's quoted or escaped.

static string LINK_CREATION_SPREAD_ACTIVATION; // Flag to indicate if activation spreading is
// supposed to be performed at the end of each
// link creation cycle.

static string LINK_CREATOR_EXTRA_PARAMETERS; // LINK_CREATOR_EXTRA_PARAMETERS is tipically
// parsed as a list of tokens separated by a
// character defined in
Comment thread
andre-senna marked this conversation as resolved.
Resolved
// EXTRA_PARAMETERS_SPLIT_CHAR
// If the separator character is used inside
// LINK_CREATOR_EXTRA_PARAMETERS, the parsing
// will fail, even if it's quoted or escaped.

LinkCreationProxy();

Expand Down Expand Up @@ -102,6 +107,12 @@ class LinkCreationProxy : public BaseQueryProxy {
*/
void flush_determiners();

/**
* Call Attention Broker to spread activation according to a state buffer with accumulated
* stimuli from one entire link creation round. This buffer is emptied as a side effect.
*/
void flush_stimuli();

// ---------------------------------------------------------------------------------------------
// Virtual superclass API and the piggyback methods called by it

Expand All @@ -125,6 +136,11 @@ class LinkCreationProxy : public BaseQueryProxy {
*/
void tokenize(vector<string>& output) override;

/**
* Notifies remote proxy that a cycle just ended.
*/
virtual void cycle_ended() override;

private:
void set_link_creator_function_tag(const string& tag);
void init();
Expand Down
45 changes: 19 additions & 26 deletions src/agents/link_creation_agent/link_creators/AndTwoPredicates.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "AndTwoPredicates.h"

#include "AtomDBUtils.h"
#include "tags.h"

using namespace link_creators;
Expand All @@ -16,6 +17,16 @@ AndTwoPredicates::~AndTwoPredicates() {}

LinkCreationStats AndTwoPredicates::create(shared_ptr<QueryAnswer> query_answer) {
STACK_TRACE();
LinkCreationStats stats;
double strength = 1;
for (string& h : query_answer->get_handles_vector()) {
strength *= AtomDBUtils::get_strength(h);
}
if (!Utils::epsilon_equals(strength, 1.0)) {
Comment thread
andre-senna marked this conversation as resolved.
Resolved
LOG_DEBUG("AndTwoPredicates discarding low strength (" + std::to_string(strength) +
") link. QueryAnswer: " + query_answer->to_string(true));
return stats;
}
string concept_ = query_answer->get(CONCEPT);
string predicates[2];
predicates[0] = query_answer->get(PREDICATE1);
Expand All @@ -25,24 +36,23 @@ LinkCreationStats AndTwoPredicates::create(shared_ptr<QueryAnswer> query_answer)
predicates[0] = predicates[1];
predicates[1] = aux;
}
string key = predicates[0] + " " + predicates[1] + concept_;
string key = predicates[0] + " " + predicates[1] + " " + concept_;

LinkCreationStats stats;
if (predicates[0] != predicates[1]) {
if (!visited(key)) {
visit(key);
stats.visited = true;
AddLinkStatus add_status;
set<string> mentioned_predicates0, mentioned_predicates1;
extract_mentioned_predicates(mentioned_predicates0, predicates[0]);
extract_mentioned_predicates(mentioned_predicates1, predicates[1]);
AtomDBUtils::reachable_terminal_set(mentioned_predicates0, predicates[0], true, true);
AtomDBUtils::reachable_terminal_set(mentioned_predicates1, predicates[1], true, true);
if (!Utils::intersects(mentioned_predicates0, mentioned_predicates1)) {
vector<string> targets = {LOGICAL_AND_HANDLE, predicates[0], predicates[1]};
if (add_or_update_link(targets, 1.0) == CREATED) {
add_status = add_or_update_link(targets, 1.0);
if (add_status == CREATED) {
stats.created++;
}
double strength = 1;
for (string& h : query_answer->get_handles_vector()) {
strength *= get_strength(h);
} else if (add_status == UPDATED) {
stats.updated++;
}
string new_predicate_handle = Hasher::link_handle(EXPRESSION, targets);
AddLinkStatus add_status =
Expand Down Expand Up @@ -73,20 +83,3 @@ LinkCreationStats AndTwoPredicates::create(shared_ptr<QueryAnswer> query_answer)
}
return stats;
}

void AndTwoPredicates::extract_mentioned_predicates(set<string>& mentioned, const string& handle) {
STACK_TRACE();
shared_ptr<Node> node;
shared_ptr<Link> link = atomdb()->get_link(handle);
if (link != nullptr) {
for (string& target_handle : link->targets) {
if ((node = atomdb()->get_node(target_handle)) != nullptr) {
if ((node->name != PREDICATE_TAG) && (node->name != LOGICAL_AND_TAG)) {
mentioned.insert(node->name);
}
} else {
extract_mentioned_predicates(mentioned, target_handle);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ class AndTwoPredicates : public LinkCreator {
private:
static string LOGICAL_AND_HANDLE;
static string EVALUATION_HANDLE;

void extract_mentioned_predicates(set<string>& mentioned, const string& handle);
};

} // namespace link_creators
1 change: 1 addition & 0 deletions src/agents/link_creation_agent/link_creators/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ cc_library(
"//agents/query_engine:pattern_matching_query_proxy",
"//agents/query_engine:query_answer",
"//atomdb:atomdb_singleton",
"//atomdb:atomdbutils",
"//attention_broker:attention_broker_lib",
"//commons:commons_lib",
"//hasher:hasher_lib",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,24 @@ void CustomizableLinkCreator::LinkSpecification::check() {
}
}

CustomizableLinkCreator::CustomizableLinkCreator() {}
CustomizableLinkCreator::CustomizableLinkCreator(bool check_nested_target_reference)
: check_nested_target_reference(check_nested_target_reference) {}

CustomizableLinkCreator::~CustomizableLinkCreator() {}

bool CustomizableLinkCreator::check_targets(vector<string>& targets) {
if (this->check_nested_target_reference) {
if (targets.size() != 2) {
RAISE_ERROR("Nested target references check is only available for links of arity 2");
}
set<string> mentioned0, mentioned1;
AtomDBUtils::reachable_terminal_set(mentioned0, targets[0], true, true);
AtomDBUtils::reachable_terminal_set(mentioned1, targets[1], true, true);
return !Utils::intersects(mentioned0, mentioned1);
}
return true;
}

LinkCreationStats CustomizableLinkCreator::create(shared_ptr<QueryAnswer> query_answer) {
STACK_TRACE();
LinkCreationStats stats;
Expand All @@ -53,21 +67,32 @@ LinkCreationStats CustomizableLinkCreator::create(shared_ptr<QueryAnswer> query_
RAISE_ERROR("Invalid empty target elements or link_type");
break;
}
vector<string> handles;
vector<string> targets, handles;
handles.push_back(Hasher::node_handle(SYMBOL, spec.link_type));
for (QueryAnswerElement& element : spec.target_elements) {
handles.push_back(query_answer->get(element));
string handle = query_answer->get(element);
targets.push_back(handle);
handles.push_back(handle);
}
string key = Utils::join(handles, ' ');
if (!visited(key)) {
string key = spec.link_type + " " + Utils::join(targets, ' ');
if (check_targets(targets) && !visited(key)) {
Comment thread
andre-senna marked this conversation as resolved.
Resolved
visit(key);
stats.visited = true;
AddLinkStatus add_status = add_or_update_link(handles, compute_strength(query_answer, spec));
vector<double> strengths = compute_strength(query_answer, spec);
AddLinkStatus add_status = add_or_update_link(handles, strengths[0]);
if (add_status == CREATED) {
stats.created++;
} else if (add_status == UPDATED) {
stats.updated++;
}
if (spec.create_reverse() && (strengths.size() == 2)) {
add_status = add_or_update_link({handles[0], handles[2], handles[1]}, strengths[1]);
if (add_status == CREATED) {
stats.created++;
} else if (add_status == UPDATED) {
stats.updated++;
}
}
}
}
return stats;
Expand All @@ -81,6 +106,12 @@ void CustomizableLinkCreator::extra_parameters(const string& extra_parameters) {
}
}

string CustomizableLinkCreator::extra_parameters() {
vector<string> tokens;
tokenize(tokens);
return Utils::join(tokens, EXTRA_PARAMETERS_SPLIT_CHAR);
}

void CustomizableLinkCreator::add_link_specification(const vector<QueryAnswerElement>& target_elements,
const vector<QueryAnswerElement>& strength_elements,
const string& link_type,
Expand All @@ -98,6 +129,7 @@ void CustomizableLinkCreator::add_link_specification(const vector<QueryAnswerEle

void CustomizableLinkCreator::tokenize(vector<string>& tokens) {
STACK_TRACE();
tokens.push_back(this->check_nested_target_reference ? "1" : "0");
Comment thread
andre-senna marked this conversation as resolved.
Resolved
tokens.push_back(std::to_string(this->link_specification.size()));
for (LinkSpecification& spec : this->link_specification) {
tokens.push_back(std::to_string(spec.target_elements.size()));
Expand Down Expand Up @@ -128,6 +160,8 @@ static inline string& safe_get_next_token(vector<string>& tokens, unsigned int&
void CustomizableLinkCreator::untokenize(vector<string>& tokens) {
STACK_TRACE();
unsigned int cursor = 0;
unsigned int flag = Utils::string_to_uint(safe_get_next_token(tokens, cursor));
this->check_nested_target_reference = (flag > 0);
unsigned int num_specs = Utils::string_to_uint(safe_get_next_token(tokens, cursor));
for (unsigned int i = 0; i < num_specs; i++) {
vector<QueryAnswerElement> _target_elements;
Expand Down Expand Up @@ -234,7 +268,7 @@ void CustomizableLinkCreator::compute_counts(shared_ptr<QueryAnswer> base_query_
} else {
d = 1;
for (string& h : query_answer->get_handles_vector()) {
d *= get_strength(h);
d *= AtomDBUtils::get_strength(h);
}
handle = query_answer->get(spec.strength_elements[i]);
insert_or_update(count_map[i], handle, d);
Expand Down Expand Up @@ -268,42 +302,48 @@ void CustomizableLinkCreator::compute_counts(shared_ptr<QueryAnswer> base_query_
to_string(count_intersection) + " " + to_string(count_union));
}

double CustomizableLinkCreator::compute_strength(shared_ptr<QueryAnswer> query_answer,
LinkSpecification& spec) {
vector<double> CustomizableLinkCreator::compute_strength(shared_ptr<QueryAnswer> query_answer,
LinkSpecification& spec) {
STACK_TRACE();
double answer = 0.0;
vector<double> answer;
double strength;
double count_A = 0.0;
double count_B = 0.0;
double count_intersection = 0.0;
double count_union = 0.0;

if ((spec.strength_composition == INTERSECTION_OVER_UNION) ||
(spec.strength_composition == INTERSECTION_OVER_A) ||
(spec.strength_composition == INTERSECTION_OVER_B)) {
(spec.strength_composition == INTERSECTION_OVER_A)) {
compute_counts(query_answer, spec, count_A, count_B, count_intersection, count_union);
}

switch (spec.strength_composition) {
case PRODUCT:
answer = 1.0;
strength = 1.0;
for (QueryAnswerElement& element : spec.strength_elements) {
answer *= get_strength(query_answer->get(element));
strength *= AtomDBUtils::get_strength(query_answer->get(element));
}
answer.push_back(strength);
break;
case INTERSECTION_OVER_UNION:
strength = 0.0;
if (!Utils::is_zero(count_union)) {
answer = count_intersection / count_union;
strength = count_intersection / count_union;
}
answer.push_back(strength);
answer.push_back(strength);
break;
case INTERSECTION_OVER_A:
strength = 0.0;
if (!Utils::is_zero(count_A)) {
answer = count_intersection / count_A;
strength = count_intersection / count_A;
}
break;
case INTERSECTION_OVER_B:
answer.push_back(strength);
strength = 0.0;
if (!Utils::is_zero(count_B)) {
answer = count_intersection / count_B;
strength = count_intersection / count_B;
}
answer.push_back(strength);
break;
default:
RAISE_ERROR("Invalid strength composition: " + std::to_string(spec.strength_composition));
Expand Down
Loading
Loading