OverviewFilesCommitsIssuesDocsReleasesPeers

opencog-atomspace / tests / query / QueryUTest.cxxtest

195 lines · cxxtest ref:
/*
 * tests/query/QueryUTest.cxxtest
 *
 * Copyright (C) 2017 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 <thread>

#include <opencog/guile/SchemeEval.h>
#include <opencog/atoms/core/UnorderedLink.h>
#include <opencog/atoms/value/LinkValue.h>
#include <opencog/atoms/value/QueueValue.h>
#include <opencog/atomspace/AtomSpace.h>
#include <opencog/util/Logger.h>
#include <cxxtest/TestSuite.h>

using namespace opencog;

#define al as->add_link
#define an as->add_node

class QueryUTest: public CxxTest::TestSuite
{
private:
	AtomSpacePtr as;

public:
	QueryUTest(void)
	{
		// logger().set_level(Logger::FINE);
		logger().set_print_to_stdout_flag(true);
		logger().set_timestamp_flag(false);

		as = createAtomSpace();
		SchemeEval* eval = SchemeEval::get_scheme_evaluator(as);
		eval->eval("(add-to-load-path \"" PROJECT_SOURCE_DIR "\")");

	}

	~QueryUTest()
	{
		// 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_basic(void);
	void test_meet(void);
	void test_exclusive(void);
};

void QueryUTest::tearDown(void)
{
}

void QueryUTest::setUp(void)
{
}

/*
 * Basic QueryLink unit test.
 */
void QueryUTest::test_basic(void)
{
	logger().debug("BEGIN TEST: %s", __FUNCTION__);

	SchemeEval* eval = SchemeEval::get_scheme_evaluator(as);
	eval->eval("(load-from-path \"tests/query/query.scm\")");
	TS_ASSERT_EQUALS(false, eval->eval_error());

	// Can we create it?
	Handle h = eval->eval_h("(Query (List (Variable \"x\")) (Variable \"x\"))");
	TS_ASSERT_EQUALS(h->get_type(), QUERY_LINK);

	// Can we execute it?
	ValuePtr sv = h->execute(as.get());
	TS_ASSERT(nameserver().isA(sv->get_type(), LINK_VALUE));
	TS_ASSERT_EQUALS(LinkValueCast(sv)->value().size(), 0);

	// A more complex query
	ValuePtr v = eval->eval_v("(cog-execute! query)");
	TS_ASSERT(nameserver().isA(v->get_type(), LINK_VALUE));
	TS_ASSERT_EQUALS(LinkValueCast(v)->value().size(), 3);
	printf("Got: %s\n", v->to_string().c_str());

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

/*
 * Basic MeetLink unit test.
 */
void QueryUTest::test_meet(void)
{
	logger().debug("BEGIN TEST: %s", __FUNCTION__);

	SchemeEval* eval = SchemeEval::get_scheme_evaluator(as);
	eval->eval("(load-from-path \"tests/query/query-meet.scm\")");
	TS_ASSERT_EQUALS(false, eval->eval_error());

	// Can we create it?
	Handle h = eval->eval_h("(Meet (List (Variable \"x\")))");
	TS_ASSERT_EQUALS(h->get_type(), MEET_LINK);

	// Can we execute it?
	ValuePtr sv = h->execute(as.get());
	TS_ASSERT(nameserver().isA(sv->get_type(), LINK_VALUE));
	TS_ASSERT_EQUALS(LinkValueCast(sv)->value().size(), 0);

	// A more complex query
	ValuePtr v = eval->eval_v("(cog-execute! meet)");
	TS_ASSERT(nameserver().isA(v->get_type(), LINK_VALUE));
	LinkValuePtr lv(LinkValueCast(v));
	printf("Got: %s\n", v->to_string().c_str());
	TS_ASSERT_EQUALS(lv->value().size(), 3);

	Handle uv(createUnorderedLink(lv->to_handle_seq()));
	printf("Got as Unordered: %s\n", uv->to_string().c_str());

	Handle euv = eval->eval_h("(UnorderedLink "
		"(ConceptNode \"thing\")"
		"(ConceptNode \"story\")"
		"(ConceptNode \"joke\")) ");

	printf("Expected: %s\n", euv->to_string().c_str());
	TS_ASSERT(content_eq(euv, uv));

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

/*
 * ExclusiveLink unit test.
 */
void QueryUTest::test_exclusive(void)
{
	logger().debug("BEGIN TEST: %s", __FUNCTION__);

	SchemeEval* eval = SchemeEval::get_scheme_evaluator(as);
	eval->eval("(load-from-path \"tests/query/query-exclusive.scm\")");
	TS_ASSERT_EQUALS(false, eval->eval_error());

	// expect empty set.
	ValuePtr v = eval->eval_v("(cog-execute! exclude-red)");
	TS_ASSERT(nameserver().isA(v->get_type(), LINK_VALUE));
	TS_ASSERT_EQUALS(LinkValueCast(v)->value().size(), 0);

	v = eval->eval_v("(cog-execute! not-red)");
	TS_ASSERT(nameserver().isA(v->get_type(), LINK_VALUE));
	TS_ASSERT_EQUALS(LinkValueCast(v)->value().size(), 0);

	eval->eval_h("(State (Concept \"stop light\") (Concept \"green\"))");

	v = eval->eval_v("(cog-execute! exclude-red)");
	TS_ASSERT(nameserver().isA(v->get_type(), LINK_VALUE));
	TS_ASSERT_EQUALS(LinkValueCast(v)->value().size(), 1);
	printf("Got: %s\n", v->to_string().c_str());

	ValuePtr ev = eval->eval_v("(UnisetValue (Concept \"green\"))");
	printf("Expected: %s\n", ev->to_string().c_str());

	LinkValuePtr lv(LinkValueCast(v));
	LinkValuePtr lev(LinkValueCast(ev));
	TS_ASSERT_EQUALS(*lev, *lv);

	v = eval->eval_v("(cog-execute! not-red)");
	TS_ASSERT(nameserver().isA(v->get_type(), LINK_VALUE));
	TS_ASSERT_EQUALS(LinkValueCast(v)->value().size(), 1);

	lv = LinkValueCast(v);
	TS_ASSERT_EQUALS(*lev, *lv);

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

#undef al
#undef an