/*
* BasicNetworkUTest.cxxtest
*
* Basic, simple random network unit test. Generates stochastic random
* networks, where all edge types are the same, and all node types are
* the same, except for the node arity.
*
* Copyright (C) 2020 Linas Vepstas <linasvepstas@gmail.com>
* All Rights Reserved
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
#include <opencog/atoms/atom_types/atom_types.h>
#include <opencog/atomspace/AtomSpace.h>
#include <opencog/guile/SchemeEval.h>
#include <opencog/generate/Aggregate.h>
#include <opencog/generate/BasicParameters.h>
#include <opencog/generate/RandomCallback.h>
#include <cxxtest/TestSuite.h>
using namespace opencog;
#define al as->add_link
#define an as->add_node
class BasicNetworkUTest: public CxxTest::TestSuite
{
private:
AtomSpacePtr asp;
AtomSpace* as;
SchemeEval* eval;
Aggregate* ag;
Dictionary* dict;
public:
BasicNetworkUTest();
~BasicNetworkUTest();
void setUp();
void tearDown();
void setup_dict();
void check_dipole(Handle, size_t);
void test_network();
};
BasicNetworkUTest::BasicNetworkUTest()
{
logger().set_level(Logger::DEBUG);
logger().set_print_to_stdout_flag(true);
logger().set_timestamp_flag(false);
}
BasicNetworkUTest::~BasicNetworkUTest()
{
logger().info("Completed running BasicNetworkUTest");
// erase the log file if no assertions failed
if (!CxxTest::TestTracker::tracker().suiteFailed())
std::remove(logger().get_filename().c_str());
else
{
logger().info("BasicNetworkUTest failed!");
logger().flush();
}
}
void BasicNetworkUTest::setUp()
{
asp = createAtomSpace();
as = asp.get();
eval = SchemeEval::get_scheme_evaluator(as);
eval->eval("(add-to-load-path \"" PROJECT_SOURCE_DIR "\")");
ag = new Aggregate(as);
}
void BasicNetworkUTest::tearDown()
{
delete ag;
}
void BasicNetworkUTest::setup_dict()
{
dict = new Dictionary(as);
// The directions to connect.
Handle any = an(CONNECTOR_DIR_NODE, "*");
dict->add_pole_pair(any, any);
// The lexis to use XXX we should generate not read from file.
UnorderedHandleSet lex;
as->get_handles_by_type(lex, SECTION);
dict->add_to_lexis(lex);
}
// Start out real simple...
void BasicNetworkUTest::test_network()
{
logger().debug("BEGIN TEST: %s", __FUNCTION__);
eval->eval("(load-from-path \"tests/generate/basic-network.scm\")");
// logger().set_level(Logger::FINE);
setup_dict();
Handle weights = eval->eval_h("(Predicate \"weights\")");
BasicParameters basic;
RandomCallback cb(as, *dict, basic);
cb.set_weight_key(weights);
Handle root = eval->eval_h("(Concept \"peep 3\")");
ag->aggregate({root}, cb);
Handle result = cb.get_solutions();
TSM_ASSERT("Bad result!", result != Handle::UNDEFINED);
// printf("result is %s\n", result->to_string().c_str());
printf("have %lu results\n", result->get_arity());
TSM_ASSERT("Expected more results!", 10 < result->get_arity());
logger().debug("END TEST: %s", __FUNCTION__);
}