<commit_before>/*
 * opencog/attentionbank/AttentionBank.cc
 *
 * Copyright (C) 2013,2017 <NAME> <<EMAIL>>
 * All Rights Reserved
 *
 * Written by <NAME>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License v3 as
 * published by the Free Software Foundation and including the exceptions
 * at http://opencog.org/wiki/Licenses
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program; if not, write to:
 * Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <functional>
#include <opencog/util/Config.h>
#include <opencog/util/mt19937ar.h>

#include <opencog/atoms/base/Handle.h>
#include <opencog/atomspace/AtomSpace.h>
#include "AttentionBank.h"
#include "AVUtils.h"

using namespace opencog;

AttentionBank::AttentionBank(AtomSpace* asp)
{
    // XXX FIXME -- should not use config() to get these values;
    // The user of this class should call config(), instead!

    startingFundsSTI = fundsSTI = config().get_double("STARTING_STI_FUNDS", 100000);
    startingFundsLTI = fundsLTI = config().get_double("STARTING_LTI_FUNDS", 100000);
    stiFundsBuffer = config().get_double("STI_FUNDS_BUFFER", 10000);
    ltiFundsBuffer = config().get_double("LTI_FUNDS_BUFFER", 10000);
    targetLTI = config().get_double("TARGET_LTI_FUNDS", 10000);
    targetSTI = config().get_double("TARGET_STI_FUNDS", 10000);
    STIAtomWage = config().get_double("ECAN_STARTING_ATOM_STI_WAGE", 10);
    LTIAtomWage = config().get_double("ECAN_STARTING_ATOM_LTI_WAGE", 10);
    maxAFSize = config().get_int("ECAN_MAX_AF_SIZE", 100);

    _remove_signal = &asp->atomRemovedSignal();
    _remove_connection = _remove_signal->connect(
            std::bind(&AttentionBank::remove_atom_from_bank, this,
                std::placeholders::_1));
}

AttentionBank::~AttentionBank()
{
    _remove_signal->disconnect(_remove_connection);
}

void AttentionBank::remove_atom_from_bank(const AtomPtr& atom)
{
    AFMutex.lock();
    auto it = std::find_if(attentionalFocus.begin(), attentionalFocus.end(),
                [atom](std::pair<Handle, AttentionValuePtr> p)
                { return p.first == Handle(atom);});

    if (it != attentionalFocus.end())
        attentionalFocus.erase(it);
    AFMutex.unlock();

   _importanceIndex.removeAtom(Handle(atom));
}
<commit_msg>emove one reason why things crash<commit_after>/*
 * opencog/attentionbank/AttentionBank.cc
 *
 * Copyright (C) 2013,2017 <NAME> <<EMAIL>>
 * All Rights Reserved
 *
 * Written by <NAME>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License v3 as
 * published by the Free Software Foundation and including the exceptions
 * at http://opencog.org/wiki/Licenses
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program; if not, write to:
 * Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <functional>
#include <opencog/util/Config.h>
#include <opencog/util/mt19937ar.h>

#include <opencog/atoms/base/Handle.h>
#include <opencog/atomspace/AtomSpace.h>
#include "AttentionBank.h"
#include "AVUtils.h"

using namespace opencog;

AttentionBank::AttentionBank(AtomSpace* asp)
{
    // XXX FIXME -- should not use config() to get these values;
    // The user of this class should call config(), instead!

    startingFundsSTI = fundsSTI = config().get_double("STARTING_STI_FUNDS", 100000);
    startingFundsLTI = fundsLTI = config().get_double("STARTING_LTI_FUNDS", 100000);
    stiFundsBuffer = config().get_double("STI_FUNDS_BUFFER", 10000);
    ltiFundsBuffer = config().get_double("LTI_FUNDS_BUFFER", 10000);
    targetLTI = config().get_double("TARGET_LTI_FUNDS", 10000);
    targetSTI = config().get_double("TARGET_STI_FUNDS", 10000);
    STIAtomWage = config().get_double("ECAN_STARTING_ATOM_STI_WAGE", 10);
    LTIAtomWage = config().get_double("ECAN_STARTING_ATOM_LTI_WAGE", 10);
    maxAFSize = config().get_int("ECAN_MAX_AF_SIZE", 100);

    _remove_signal = &asp->atomRemovedSignal();
    _remove_connection = _remove_signal->connect(
            std::bind(&AttentionBank::remove_atom_from_bank, this,
                std::placeholders::_1));
}

AttentionBank::~AttentionBank()
{
    // Making the call below leads to a crash in various situations.
    // This can occur when the cogserver is being shutdown, when
    // atomspaces are being deleted, and during Python unit tests.
    // Basically, the teardown sequence is somehow wrong, and I'm
    // too lazy to figure it out. Having a singleton isntance is
    // probably the main flaw; it does not play nice with the rest
    // of the code, which is working with several atomspaces.
    // _remove_signal->disconnect(_remove_connection);
}

void AttentionBank::remove_atom_from_bank(const AtomPtr& atom)
{
    AFMutex.lock();
    auto it = std::find_if(attentionalFocus.begin(), attentionalFocus.end(),
                [atom](std::pair<Handle, AttentionValuePtr> p)
                { return p.first == Handle(atom);});

    if (it != attentionalFocus.end())
        attentionalFocus.erase(it);
    AFMutex.unlock();

   _importanceIndex.removeAtom(Handle(atom));
}

{'fraction_non_alphanumeric': 0.11215431888016388, 'fraction_numerical': 0.02219187435984978, 'mean_word_length': 4.180371352785146, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5093040', 'n_tokens_mistral': 1947, 'n_tokens_neox': 1796, 'n_words': 512}