OverviewFilesCommitsIssuesDocsReleasesPeers

opencog-atomspace / tests / atoms / join / BlueJoinUTest.cxxtest

253 lines · cxxtest ref:
/*
 * tests/atoms/join/BlueJoinUTest.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 BlueJoinUTest: public CxxTest::TestSuite
{
private:
	AtomSpacePtr _as;
	SchemeEval* eval;

public:
	BlueJoinUTest(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 "\")");
	}

	~BlueJoinUTest()
	{
		// 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_naming(void);
	void test_region(void);
	void test_embed(void);
};

void BlueJoinUTest::tearDown(void)
{
	_as->clear();
}

void BlueJoinUTest::setUp(void)
{
	_as->clear();
}

#define N _as->add_node
#define L _as->add_link

/*
 * Slightly more complicated Replacement
 * Wiki page "Naming subcomponents"
 */
void BlueJoinUTest::test_naming(void)
{
	logger().info("BEGIN TEST: %s", __FUNCTION__);

	eval->eval("(load-from-path \"tests/atoms/join/blue-sky.scm\")");

	ValuePtr vp = eval->eval_v("(cog-execute! min-blue-sky)");
	printf("min-replace = %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");
	TS_ASSERT_EQUALS(results[0], X);

	// ------------
	vp = eval->eval_v("(cog-execute! max-blue-sky)");
	printf("max-replace = %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 M = L(CONTEXT_LINK,
		N(CONCEPT_NODE, "moon"),
		L(SIMILARITY_LINK, X, N(CONCEPT_NODE, "unlikely")));
	TS_ASSERT_EQUALS(results[0], M);

	// ------------
	vp = eval->eval_v("(cog-execute! min-replace)");
	printf("min-replace = %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 G = N(CONCEPT_NODE, "green cheese");
	TS_ASSERT_EQUALS(results[0], G);

	// ------------
	vp = eval->eval_v("(cog-execute! max-replace)");
	printf("max-replace = %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);

	M = L(CONTEXT_LINK,
		N(CONCEPT_NODE, "moon"),
		L(SIMILARITY_LINK, G, N(CONCEPT_NODE, "unlikely")));
	TS_ASSERT_EQUALS(results[0], M);

	// ------------
	logger().info("END TEST: %s", __FUNCTION__);
}

/*
 * Variable regions
 * Wiki page "Variable regions in subcomponents"
 */
void BlueJoinUTest::test_region(void)
{
	logger().info("BEGIN TEST: %s", __FUNCTION__);

	eval->eval("(load-from-path \"tests/atoms/join/blue-sky.scm\")");

	ValuePtr vp = eval->eval_v("(cog-execute! min-blue-out)");
	printf("min-blue-out = %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");
	TS_ASSERT_EQUALS(results[0], X);

	// ------------
	vp = eval->eval_v("(cog-execute! max-blue-out)");
	printf("max-blue-out = %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 MX = L(CONTEXT_LINK,
		N(CONCEPT_NODE, "moon"),
		L(SIMILARITY_LINK, X, N(CONCEPT_NODE, "unlikely")));
	TS_ASSERT_EQUALS(results[0], MX);

	// ------------
	vp = eval->eval_v("(cog-execute! min-blue-rep)");
	printf("min-blue-rep = %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 G = N(CONCEPT_NODE, "green cheese");
	TS_ASSERT_EQUALS(results[0], G);

	// ------------
	vp = eval->eval_v("(cog-execute! max-blue-rep)");
	printf("max-blue-rep = %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 MG = L(CONTEXT_LINK,
		N(CONCEPT_NODE, "moon"),
		L(SIMILARITY_LINK, G, N(CONCEPT_NODE, "unlikely")));
	TS_ASSERT_EQUALS(results[0], MG);

	// ------------
	logger().info("END TEST: %s", __FUNCTION__);
}

/*
 * Embedded names
 * Wiki page "Embedded names"
 */
void BlueJoinUTest::test_embed(void)
{
	logger().info("BEGIN TEST: %s", __FUNCTION__);

	eval->eval("(load-from-path \"tests/atoms/join/blue-sky.scm\")");

	ValuePtr vp = eval->eval_v("(cog-execute! min-thing)");
	printf("min-thing = %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 T = N(VARIABLE_NODE, "$thing");
	TS_ASSERT_EQUALS(results[0], T);

	// ------------
	vp = eval->eval_v("(cog-execute! max-thing)");
	printf("max-thing = %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 ET = L(EVALUATION_LINK,
		N(PREDICATE_NODE, "is blue"), L(LIST_LINK, T));
	Handle MT = L(CONTEXT_LINK,
		N(CONCEPT_NODE, "moon"),
		L(SIMILARITY_LINK, ET, N(CONCEPT_NODE, "unlikely")));
	TS_ASSERT_EQUALS(results[0], MT);

	// ------------
	vp = eval->eval_v("(cog-execute! min-thing-rep)");
	printf("min-thing-rep = %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 G = N(CONCEPT_NODE, "green cheese");
	TS_ASSERT_EQUALS(results[0], G);

	// ------------
	vp = eval->eval_v("(cog-execute! max-thing-rep)");
	printf("max-thing-rep = %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 EG = L(EVALUATION_LINK,
		N(PREDICATE_NODE, "is blue"), L(LIST_LINK, G));
	Handle MG = L(CONTEXT_LINK,
		N(CONCEPT_NODE, "moon"),
		L(SIMILARITY_LINK, EG, N(CONCEPT_NODE, "unlikely")));
	TS_ASSERT_EQUALS(results[0], MG);

	// ------------
	logger().info("END TEST: %s", __FUNCTION__);
}