OverviewFilesCommitsIssuesDocsReleasesPeers

opencog-atomspace / tests / query / Boolean2NotUTest.cxxtest

257 lines · cxxtest ref:
/*
 * tests/query/Boolean2NotUTest.cxxtest
 *
 * Copyright (C) 2009 Linas Vepstas <linasvepstas@gmail.com>
 * 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/atomspace/AtomSpace.h>
#include <opencog/util/Logger.h>

#include "imply.h"

using namespace opencog;

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

		Handle clauses, implicand, his_a, hberlin, hcity;

	public:

		Boolean2NotUTest(void)
		{
			logger().set_level(Logger::DEBUG);
			logger().set_print_to_stdout_flag(true);
		}

		~Boolean2NotUTest()
		{
			// 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_isa(void);
};

/*
 * Unlike the BooleanUTest, this test has two ABSENT_LINK statements in it.
 * This function sets up the following structures:
 *  # IF _subj(be, $var1) ^ 
 *       _obj(be, $var2) ^
 *        !DEFINITE-FLAG($var2) ^
 *        !HYP-FLAG(be)
 *      THEN ^3_isa($var1, $var2)
 * i.e. want to assure the *absence* of DEFINITE_FLAG, and of HYP.
 */
#define an as->add_node
#define al as->add_link
#define getarity(hand) hand->get_arity()
#define getlink(hand,pos) hand->getOutgoingAtom(pos)

void Boolean2NotUTest::tearDown(void)
{
}
void Boolean2NotUTest::setUp(void)
{
	as = createAtomSpace();

	// Create an implication link that will be tested.
	clauses =
		al(AND_LINK,
			al(EVALUATION_LINK,
				an(PREDICATE_NODE, "_subj"),
				al(LIST_LINK,
					an(VARIABLE_NODE, "$winst"),
					an(VARIABLE_NODE, "$var1")
				)
			),
			al(EVALUATION_LINK,
				an(PREDICATE_NODE, "_obj"),
				al(LIST_LINK,
					an(VARIABLE_NODE, "$winst"),
					an(VARIABLE_NODE, "$var2")
				)
			),
			// used to guarantee that both are in the same "sentence"
			al(MEMBER_LINK,
				an(VARIABLE_NODE, "$var1"),
				an(VARIABLE_NODE, "$sent")
			),
			al(MEMBER_LINK,
				an(VARIABLE_NODE, "$var2"),
				an(VARIABLE_NODE, "$sent")
			),
			al(MEMBER_LINK,
				an(VARIABLE_NODE, "$winst"),
				an(VARIABLE_NODE, "$sent")
			),
			al(CONTEXT_LINK,
				an(VARIABLE_NODE, "$winst"),
				an(CONCEPT_NODE, "be")
			),
			al(ABSENT_LINK,
				al(INHERITANCE_LINK,
					an(VARIABLE_NODE, "$var2"),
					an(CONCEPT_NODE, "definite")
				)
			),
			al(ABSENT_LINK,
				al(INHERITANCE_LINK,
					an(VARIABLE_NODE, "$winst"),
					an(CONCEPT_NODE, "hyp")
				)
			)
		);
	implicand =
		al(EVALUATION_LINK,
			his_a = an(PREDICATE_NODE, "is_a"),
			al(LIST_LINK,
				an(VARIABLE_NODE, "$var1"),
				an(VARIABLE_NODE, "$var2")
			)
		);

	// Create data on which the above pattern should match
	// This is an assertion "Berlin is a city"
	al(INHERITANCE_LINK,
		an(CONCEPT_NODE, "Berlin"),
		an(CONCEPT_NODE, "definite")
	);
	al(EVALUATION_LINK,
		an(PREDICATE_NODE, "_subj"),
		al(LIST_LINK,
			an(CONCEPT_NODE, "be@1111"),
			hberlin = an(CONCEPT_NODE, "Berlin")
		)
	);
	al(EVALUATION_LINK,
		an(PREDICATE_NODE, "_obj"),
		al(LIST_LINK,
			an(CONCEPT_NODE, "be@1111"),
			hcity = an(CONCEPT_NODE, "city")
		)
	);
	al(MEMBER_LINK,
		an(CONCEPT_NODE, "Berlin"),
		an(CONCEPT_NODE, "sentence-AAA")
	);
	al(MEMBER_LINK,
		an(CONCEPT_NODE, "city"),
		an(CONCEPT_NODE, "sentence-AAA")
	);
	al(MEMBER_LINK,
		an(CONCEPT_NODE, "be@1111"),
		an(CONCEPT_NODE, "sentence-AAA")
	);
	al(CONTEXT_LINK,
		an(CONCEPT_NODE, "be@1111"),
		an(CONCEPT_NODE, "be")
	);

	// Create data on which the above pattern should fail
	// This is a question: "Is Madrid a city?", and thus the copula "be"
	// is labelled as "hyp". The pattern match should rehject the presence
	// of this "hyp" clause.
	al(INHERITANCE_LINK,
		an(CONCEPT_NODE, "Madrid"),
		an(CONCEPT_NODE, "definite")
	);
	al(INHERITANCE_LINK,
		an(CONCEPT_NODE, "be@2222"),
		an(CONCEPT_NODE, "hyp")
	);
	al(INHERITANCE_LINK,
		an(CONCEPT_NODE, "be@2222"),
		an(CONCEPT_NODE, "truth-query")
	);
	al(EVALUATION_LINK,
		an(PREDICATE_NODE, "_subj"),
		al(LIST_LINK,
			an(CONCEPT_NODE, "be@2222"),
			an(CONCEPT_NODE, "Madrid")
		)
	);
	al(EVALUATION_LINK,
		an(PREDICATE_NODE, "_obj"),
		al(LIST_LINK,
			an(CONCEPT_NODE, "be@2222"),
			an(CONCEPT_NODE, "city")
		)
	);
	al(MEMBER_LINK,
		an(CONCEPT_NODE, "Madrid"),
		an(CONCEPT_NODE, "sentence-BBB")
	);
	al(MEMBER_LINK,
		an(CONCEPT_NODE, "city"),
		an(CONCEPT_NODE, "sentence-BBB")
	);
	al(MEMBER_LINK,
		an(CONCEPT_NODE, "be@2222"),
		an(CONCEPT_NODE, "sentence-BBB")
	);
	al(CONTEXT_LINK,
		an(CONCEPT_NODE, "be@2222"),
		an(CONCEPT_NODE, "be")
	);
}

/*
 * preposition reasoning test.
 *
 * Expect to get the following result, given the above data:
 *
 *    EvaluationLink
 *       PredicateNode "is_a"
 *       ListLink
 *          ConceptNode "Berlin"
 *          ConceptNode "city"
 */
void Boolean2NotUTest::test_isa(void)
{
	logger().debug("BEGIN TEST: %s", __FUNCTION__);

	// Result should be a ListLink
	Handle result = imply(as, clauses, implicand);

	// There should be only one solution: the Berlin one.
	// The Madrid graph should be rejected because of the 
	// ABSENT_LINK rejects the definite feature of 'captial'.
	TSM_ASSERT_EQUALS("wrong number of solutions found", 1, getarity(result));

	// Then, evaluationLink, as above.
	Handle heval = getlink(result, 0);
	Handle vis_a = getlink(heval, 0);
	Handle hlist = getlink(heval, 1);
	Handle vberlin = getlink(hlist, 0);
	Handle vcity = getlink(hlist, 1);

	TSM_ASSERT_EQUALS("incorrect is_a", his_a, vis_a);
	TSM_ASSERT_EQUALS("incorrect berlin", hberlin, vberlin);
	TSM_ASSERT_EQUALS("incorrect city", hcity, vcity);

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