/*
* tests/atoms/join/CompoundJoinUTest.cxxtest
*
* Copyright (C) 2020 Linas Vepstas
* 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 <opencog/atoms/value/LinkValue.h>
#include <opencog/atomspace/AtomSpace.h>
#include <opencog/guile/SchemeEval.h>
#include <opencog/util/Logger.h>
using namespace opencog;
class CompoundJoinUTest: public CxxTest::TestSuite
{
private:
AtomSpacePtr _as;
SchemeEval* eval;
public:
CompoundJoinUTest(void)
{
logger().set_level(Logger::INFO);
logger().set_print_to_stdout_flag(true);
logger().set_timestamp_flag(false);
logger().set_sync_flag(true);
_as = createAtomSpace();
eval = SchemeEval::get_scheme_evaluator(_as);
eval->eval("(add-to-load-path \"" PROJECT_SOURCE_DIR "\")");
}
~CompoundJoinUTest()
{
// Erase the log file if no assertions failed.
if (!CxxTest::TestTracker::tracker().suiteFailed())
std::remove(logger().get_filename().c_str());
}
void setUp(void);
void tearDown(void);
// Test the examples on the wiki page
// https://wiki.opencog.org/w/JoinLink
void test_compound(void);
};
void CompoundJoinUTest::tearDown(void)
{
_as->clear();
}
void CompoundJoinUTest::setUp(void)
{
_as->clear();
}
#define N _as->add_node
#define L _as->add_link
/*
* Wiki page "Compound structures"
*/
void CompoundJoinUTest::test_compound(void)
{
logger().info("BEGIN TEST: %s", __FUNCTION__);
eval->eval("(load-from-path \"tests/atoms/join/compound-join.scm\")");
// ----------------------------------
ValuePtr vp = eval->eval_v("(cog-execute! min-compound)");
printf("min-compound = %s\n", vp->to_string().c_str());
TS_ASSERT(nameserver().isA(vp->get_type(), LINK_VALUE));
HandleSeq results = LinkValueCast(vp)->to_handle_seq();
TS_ASSERT_EQUALS(results.size(), 1);
Handle X = N(VARIABLE_NODE, "X");
Handle Y = N(VARIABLE_NODE, "Y");
Handle LXY = L(LIST_LINK, X,Y);
TS_ASSERT_EQUALS(results[0], LXY);
// ----------------------------------
vp = eval->eval_v("(cog-execute! max-compound)");
printf("max-compound = %s\n", vp->to_string().c_str());
TS_ASSERT(nameserver().isA(vp->get_type(), LINK_VALUE));
results = LinkValueCast(vp)->to_handle_seq();
TS_ASSERT_EQUALS(results.size(), 1);
Handle E = L(EVALUATION_LINK, N(PREDICATE_NODE, "part of"), LXY);
TS_ASSERT_EQUALS(results[0], E);
// ------------
logger().info("END TEST: %s", __FUNCTION__);
}