OverviewFilesCommitsIssuesDocsReleasesPeers

opencog-atomspace / tests / query / RecognizerUTest.cxxtest

183 lines · cxxtest ref:
/*
 * tests/query/RecognizerUTest.cxxtest
 *
 * Copyright (C) 2015,2016 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/guile/SchemeEval.h>
#include <opencog/atomspace/AtomSpace.h>
#include <opencog/util/Logger.h>

using namespace opencog;

class RecognizerUTest: public CxxTest::TestSuite
{
private:
	AtomSpacePtr as;
	SchemeEval* eval;

public:
	RecognizerUTest(void)
	{
		logger().set_level(Logger::DEBUG);
		logger().set_print_to_stdout_flag(true);

		as = createAtomSpace();
		eval = SchemeEval::get_scheme_evaluator(as);
	}

	~RecognizerUTest()
	{
		// 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);

	void test_single_word(void);
	void test_word_phrase(void);
	void test_double_glob(void);
	void test_generic(void);
	void test_zero_to_many(void);
};

void RecognizerUTest::tearDown(void)
{
	as->clear();
}

void RecognizerUTest::setUp(void)
{
	as->clear();
	eval->eval("(add-to-load-path \"" PROJECT_SOURCE_DIR "\")");

	eval->eval("(load-from-path \"tests/query/test_types.scm\")");
}

#define getarity(hand) hand->get_arity()

/*
 * Test a glob in the middle of a list. Should match two patterns.
 */
void RecognizerUTest::test_single_word(void)
{
	logger().debug("BEGIN TEST: %s", __FUNCTION__);

	eval->eval("(load-from-path \"tests/query/recognizer.scm\")");

	Handle love = eval->eval_h("(cog-execute! (DualLink sent))");
	printf("love-you %s\n", love->to_string().c_str());
	TS_ASSERT_EQUALS(2, getarity(love));

	Handle response = eval->eval_h("(SetLink love-star star-you)");
	TS_ASSERT_EQUALS(love, response);

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

/*
 * Test an adverbial word phrase; should match only one glob.
 */
void RecognizerUTest::test_word_phrase(void)
{
	logger().debug("BEGIN TEST: %s", __FUNCTION__);

	eval->eval("(load-from-path \"tests/query/recognizer.scm\")");

	Handle love = eval->eval_h("(cog-execute! (DualLink adv-sent))");
	printf("super-love %s\n", love->to_string().c_str());
	TS_ASSERT_EQUALS(1, getarity(love));

	Handle response = eval->eval_h("(SetLink star-you)");
	TS_ASSERT_EQUALS(love, response);

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

//
// Test an expression with two globs.  This previously exhibited
// bug # 724 -- an off-by-one error in the recognizer.
void RecognizerUTest::test_double_glob(void)
{
	logger().debug("BEGIN TEST: %s", __FUNCTION__);

	eval->eval("(load-from-path \"tests/query/recognizer.scm\")");

	Handle hate = eval->eval_h("(cog-execute! (DualLink hate-speech))");
	printf("haters hating %s\n", hate->to_string().c_str());
	TS_ASSERT_EQUALS(1, getarity(hate));

	Handle response = eval->eval_h("(SetLink a-hate-b)");
	TS_ASSERT_EQUALS(hate, response);

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

void RecognizerUTest::test_generic(void)
{
	logger().debug("BEGIN TEST: %s", __FUNCTION__);

	eval->eval("(load-from-path \"tests/query/recognizer.scm\")");

	Handle anb = eval->eval_h("(cog-execute! (DualLink a-and-b))");
	printf("A and B %s\n", anb->to_string().c_str());
	TS_ASSERT_EQUALS(2, getarity(anb));

	Handle response = eval->eval_h(
		"(SetLink "
		"   (AndLink (Variable \"$x\") (Concept \"B\"))"
		"   (AndLink (Variable \"$x\") (Concept \"A\")))");

	TS_ASSERT_EQUALS(anb, response);

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

void RecognizerUTest::test_zero_to_many(void)
{
	logger().debug("BEGIN TEST: %s", __FUNCTION__);

	eval->eval("(load-from-path \"tests/query/recognizer.scm\")");

	Handle ztm = eval->eval_h("(cog-execute! (DualLink ztm))");
	printf("Zero to many %s\n", ztm->to_string().c_str());
	TS_ASSERT_EQUALS(7, getarity(ztm));

	Handle response = eval->eval_h(
		"(SetLink "
		"   (List (Concept \"A\") (Glob \"$x\"))"
		"   (List (Glob \"$y\") (Concept \"B\"))"
		"   (List (Concept \"A\") (Glob \"$z\") (Concept \"B\"))"
		"   (List (Glob \"$a\") (Concept \"A\") (Glob \"$b\") (Concept \"B\") (Glob \"$c\"))"
		"   (List (Glob \"$d\") (Concept \"A\") (Concept \"B\") (Glob \"$e\"))"
		"   (List (Glob \"$f\") (Glob \"$g\") (Concept \"A\") (Concept \"B\") (Glob \"$h\"))"
		"   (List (Glob \"$i\") (Concept \"A\") (Concept \"B\") (Glob \"$j\") (Glob \"$k\")))");

	TS_ASSERT_EQUALS(ztm, response);

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