<commit_before>/*
 * opencog/atomspace/Link.cc
 *
 * Copyright (C) 2008-2010 OpenCog Foundation
 * Copyright (C) 2002-2007 Novamente LLC
 * All Rights Reserved
 *
 * 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 "Link.h"

#include <stdio.h>

#include <opencog/util/platform.h>

#include <opencog/atomspace/AtomSpaceDefinitions.h>
#include <opencog/atomspace/ClassServer.h>
#include <opencog/atomspace/Node.h>
#include <opencog/atomspace/StatisticsMonitor.h>
#include <opencog/atomspace/Trail.h>
#include <opencog/util/exceptions.h>
#include <opencog/util/Logger.h>

//#define DPRINTF printf
#define DPRINTF(...)

using namespace opencog;

void Link::init(const std::vector<Handle>& outgoingVector)
	throw (InvalidParamException)
{
    if (!classserver().isA(type, LINK)) {
        throw InvalidParamException(TRACE_INFO,
             "Link -  invalid link type: %d", type);
    }
    trail = new Trail();
    setOutgoingSet(outgoingVector);
}

Link::~Link()
{
    DPRINTF("Deleting link:\n%s\n", this->toString().c_str());
    delete trail;
}

void Link::setTrail(Trail* t)
{
    if (trail) {
        delete(trail);
    }
    trail = t;
}

Trail* Link::getTrail(void)
{
    return trail;
}

Atom* Link::getOutgoingAtom(int pos) const
{
    if (!atomTable) {
        throw RuntimeException(TRACE_INFO,
            "Link is not embedded in AtomTable, so can't resolve "
            "outgoing handles to Atom pointers.");
    }
    return atomTable->getAtom(getOutgoingHandle(pos));
}

std::string Link::toShortString(void) const
{
    std::string answer;
#define BUFSZ 1024
    char buf[BUFSZ];

    snprintf(buf, BUFSZ, "[%s %s", classserver().getTypeName(type).c_str(),
             (getFlag(HYPOTETHICAL_FLAG) ? "h " : ""));
    answer += buf;
    // Here the targets string is made. If a target is a node, its name is
    // concatenated. If it's a link, all its properties are concatenated.
    answer += "<";
    for (int i = 0; i < getArity(); i++) {
        if (i > 0) answer += ",";
        if (atomTable) {
            Atom* a = atomTable->getAtom(outgoing[i]);
            answer += classserver().isA(a->getType(), NODE) ?
                      ((Node*) a)->getName() :
                      ((Link*) a)->toShortString();
        } else {
            // No AtomTable connected so just print handles
            answer += "#" + outgoing[i];
        }
    }
    answer += ">";
    float mean = this->getTruthValue().getMean();
    float count = this->getTruthValue().getCount();
    if (mean == 0.0f) {
        snprintf(buf, BUFSZ, " 0.0");
    } else {
        snprintf(buf, BUFSZ, " %f", mean);
    }
    answer += buf;
    if (count == 0.0f) {
        snprintf(buf, BUFSZ, " 0.0");
    } else {
        snprintf(buf, BUFSZ, " %.2f", count);
    }
    answer += buf;
    answer += "]";
    return answer;
}

std::string Link::toString(void) const
{
    std::string answer;
    char buf[BUFSZ];

    snprintf(buf, BUFSZ, "link[%s sti:(%d,%d) tv:(%s) ",
             classserver().getTypeName(type).c_str(),
             (int)getAttentionValue().getSTI(),
             (int)getAttentionValue().getLTI(),
             getTruthValue().toString().c_str());
    answer += buf;
    // Here the targets string is made. If a target is a node, its name is
    // concatenated. If it's a link, all its properties are concatenated.
    answer += "<";
    for (int i = 0; i < getArity(); i++) {
        if (i > 0) answer += ",";
        Handle h = outgoing[i];
        if (atomTable) {
            if (atomTable->holds(h)) {
                Atom *a = atomTable->getAtom(h);
                Node *nnn = dynamic_cast<Node *>(a);
                if (nnn) {
                    snprintf(buf, BUFSZ, "[%s ", classserver().getTypeName(a->getType()).c_str());
                    answer += buf;
                    if (nnn->getName() == "")
                        answer += "#" + h;<commit_msg>Change format of toShortString so that giant count values are not printed, instead the confidence is used<commit_after>/*
 * opencog/atomspace/Link.cc
 *
 * Copyright (C) 2008-2010 OpenCog Foundation
 * Copyright (C) 2002-2007 Novamente LLC
 * All Rights Reserved
 *
 * 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 "Link.h"

#include <stdio.h>

#include <opencog/util/platform.h>

#include <opencog/atomspace/AtomSpaceDefinitions.h>
#include <opencog/atomspace/ClassServer.h>
#include <opencog/atomspace/Node.h>
#include <opencog/atomspace/StatisticsMonitor.h>
#include <opencog/atomspace/Trail.h>
#include <opencog/util/exceptions.h>
#include <opencog/util/Logger.h>

//#define DPRINTF printf
#define DPRINTF(...)

using namespace opencog;

void Link::init(const std::vector<Handle>& outgoingVector)
	throw (InvalidParamException)
{
    if (!classserver().isA(type, LINK)) {
        throw InvalidParamException(TRACE_INFO,
             "Link -  invalid link type: %d", type);
    }
    trail = new Trail();
    setOutgoingSet(outgoingVector);
}

Link::~Link()
{
    DPRINTF("Deleting link:\n%s\n", this->toString().c_str());
    delete trail;
}

void Link::setTrail(Trail* t)
{
    if (trail) {
        delete(trail);
    }
    trail = t;
}

Trail* Link::getTrail(void)
{
    return trail;
}

Atom* Link::getOutgoingAtom(int pos) const
{
    if (!atomTable) {
        throw RuntimeException(TRACE_INFO,
            "Link is not embedded in AtomTable, so can't resolve "
            "outgoing handles to Atom pointers.");
    }
    return atomTable->getAtom(getOutgoingHandle(pos));
}

std::string Link::toShortString(void) const
{
    std::stringstream answer;

    answer << "[" << classserver().getTypeName(type) << " ";
    answer << (getFlag(HYPOTETHICAL_FLAG) ? "h " : "");

    // Here the targets string is made. If a target is a node, its name is
    // concatenated. If it's a link, all its properties are concatenated.
    answer << "<";
    for (int i = 0; i < getArity(); i++) {
        if (i > 0) answer << ",";
        if (atomTable) {
            Atom* a = atomTable->getAtom(outgoing[i]);
            if (classserver().isA(a->getType(), NODE))
                answer << ((Node*) a)->getName();
            else 
                answer << ((Link*) a)->toShortString();
        } else {
            // No AtomTable connected so just print handles
            answer << "#" << outgoing[i];
        }
    }
    answer << ">";
    float mean = this->getTruthValue().getMean();
    float confidence = this->getTruthValue().getConfidence();
    if (mean == 0.0f) {
        answer << " 0.0";
    } else {
        answer << " " << mean;
    }
    if (confidence == 0.0f) {
        answer << " 0.0";
    } else {
        answer << " " << confidence;
    }
    answer << "]";
    return answer.str();
}

std::string Link::toString(void) const
{
    std::string answer;
#define BUFSZ 1024
    static char buf[BUFSZ];

    snprintf(buf, BUFSZ, "link[%s sti:(%d,%d) tv:(%s) ",
             classserver().getTypeName(type).c_str(),
             (int)getAttentionValue().getSTI(),
             (int)getAttentionValue().getLTI(),
             getTruthValue().toString().c_str());
    answer += buf;
    // Here the targets string is made. If a target is a node, its name is
    // concatenated. If it's a link, all its properties are concatenated.
    answer += "<";
    for (int i = 0; i < getArity(); i++) {
        if (i > 0) answer += ",";
        Handle h = outgoing[i];
        if (atomTable) {
            if (atomTable->holds(h)) {
                Atom *a = atomTable->getAtom(h);
                Node *nnn = dynamic_cast<Node *>(a);
                if (nnn) {
                    snprintf(buf, BUFSZ, "[%s ", classserver().getTypeName(a->getType()).c_str());
                    answer += buf;
                    if (nnn->getName() == "")
                        answer += "#" + h;
{'fraction_non_alphanumeric': 0.14227997379340468, 'fraction_numerical': 0.009718279100240227, 'mean_word_length': 2.5638132295719847, 'pattern_counts': {'":': 0, '<': 69, '<?xml version=': 0, '>': 55, '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': '484425', 'n_tokens_mistral': 2965, 'n_tokens_neox': 2853, 'n_words': 821}