From 8551b3a8bf7a88042bfbb2cadad8e99b38fdf51f Mon Sep 17 00:00:00 2001 From: fabian klopfer Date: Thu, 28 Jun 2018 16:48:18 +0800 Subject: [PATCH] changed member links to link values --- opencog/attentionbank/AttentionSCM.cc | 83 ++++---------------------- opencog/attentionbank/AttentionSCM.h | 35 +++++++++++ opencog/scm/opencog/attention-bank.scm | 4 +- 3 files changed, 48 insertions(+), 74 deletions(-) create mode 100644 opencog/attentionbank/AttentionSCM.h diff --git a/opencog/attentionbank/AttentionSCM.cc b/opencog/attentionbank/AttentionSCM.cc index c2e3bd0586..7aa4988b95 100644 --- a/opencog/attentionbank/AttentionSCM.cc +++ b/opencog/attentionbank/AttentionSCM.cc @@ -8,41 +8,7 @@ #ifdef HAVE_GUILE -#include -#include -#include - -namespace opencog { - -class AttentionSCM -{ - protected: - static void* init_in_guile(void*); - static void init_in_module(void*); - void init(void); - public: - AttentionSCM(void); - ~AttentionSCM(); - - AttentionValuePtr get_av(const Handle&); - Handle set_av(const Handle&, const AttentionValuePtr&); - Handle inc_vlti(const Handle&); - Handle dec_vlti(const Handle&); - - Handle update_af(int); - int af_size(void); - int set_af_size(int); - Handle stimulate (const Handle&, double); - - Handle af_bindlink(const Handle&); -}; - -} - -#include -#include -#include -#include +#include "AttentionSCM.h" using namespace opencog; @@ -146,55 +112,28 @@ int AttentionSCM::set_af_size (int ssize) */ Handle AttentionSCM::update_af(int n) { + HandleSeq attentionalFocus; + AtomSpace* atomspace = SchemeSmob::ss_get_env_as("cog-af"); - Handle af_anchor = atomspace->add_node(ANCHOR_NODE, - "*-attentional-focus-boundary-*"); - - // Get the atoms that were previously in attention focus - IncomingSet paf(af_anchor->getIncomingSetByType(MEMBER_LINK)); - HandleSet prev_af; - for (const LinkPtr& lp: paf) - { - Handle h(lp->getOutgoingAtom(0)); - if (h != af_anchor) - prev_af.insert(h); - } + Handle af_anchor = atomspace->add_node(ANCHOR_NODE, "*-attentional-focus-boundary-*"); + Handle af_key = atomspace->add_node(PREDICATE_NODE, "AttentionalFocus"); - HandleSeq attentionalFocus; attentionbank(atomspace).get_handle_set_in_attentional_focus(back_inserter(attentionalFocus)); size_t isz = attentionalFocus.size(); size_t N = isz; if (0 < n) N = n; if( N > isz) N = isz; + std::reverse(attentionalFocus.begin(), attentionalFocus.end()); + attentionalFocus.resize(N); - /* Add all the atoms in the current attentional focus that are not - * already there. */ - HandleSet curr_af; - for (size_t i = isz - N; i < isz; i++) - { - Handle hi = attentionalFocus[i]; - auto gone = prev_af.find(hi); - if (prev_af.end() == gone) - { - atomspace->add_link(MEMBER_LINK, hi, af_anchor); - } - else - { - curr_af.insert(hi); - } + std::vector af; + for (Handle& h : attentionalFocus) { + af.push_back(h); } - /* Remove the atoms no longer in attentional focus */ - for (const Handle& h: prev_af) - { - auto gone = curr_af.find(h); - if (curr_af.end() == gone) - { - atomspace->extract_atom(h); - } - } + af_anchor->setValue(af_key, createLinkValue(af)); return af_anchor; } diff --git a/opencog/attentionbank/AttentionSCM.h b/opencog/attentionbank/AttentionSCM.h new file mode 100644 index 0000000000..7b0470f657 --- /dev/null +++ b/opencog/attentionbank/AttentionSCM.h @@ -0,0 +1,35 @@ +#include +#include +#include +#include +#include +#include +#include +#include + + +namespace opencog { + +class AttentionSCM +{ + protected: + static void* init_in_guile(void*); + static void init_in_module(void*); + void init(void); + public: + AttentionSCM(void); + ~AttentionSCM(); + + AttentionValuePtr get_av(const Handle&); + Handle set_av(const Handle&, const AttentionValuePtr&); + Handle inc_vlti(const Handle&); + Handle dec_vlti(const Handle&); + + Handle update_af(int); + int af_size(void); + int set_af_size(int); + Handle stimulate (const Handle&, double); + + Handle af_bindlink(const Handle&); +}; +} diff --git a/opencog/scm/opencog/attention-bank.scm b/opencog/scm/opencog/attention-bank.scm index 07b4f98761..7115f2e8a3 100644 --- a/opencog/scm/opencog/attention-bank.scm +++ b/opencog/scm/opencog/attention-bank.scm @@ -158,8 +158,8 @@ guile> (cog-af 1) (ConceptNode \"ArtificialIntelligence\" (av 15752 0 0)) " - (map gar - (cog-incoming-by-type (cog-update-af n) 'MemberLink)) + (cog-value->list + (cog-value (cog-update-af n) (Predicate "AttentionalFocus"))) ) (export cog-af)