/*
* tests/query/BooleanUTest.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 BooleanUTest : public CxxTest::TestSuite
{
private:
AtomSpacePtr as;
Handle clauses, implicand, his_a, hberlin, hcity;
public:
BooleanUTest(void)
{
logger().set_level(Logger::DEBUG);
logger().set_print_to_stdout_flag(true);
}
~BooleanUTest()
{
// 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);
};
/*
* This function sets up the following structures:
* # IF _subj(be, $var1) ^
* _obj(be, $var2) ^
* !DEFINITE-FLAG($var2)
* THEN ^3_isa($var1, $var2)
* i.e. want to assure the *absence* of DEFINITE_FLAG
*/
#define an as->add_node
#define al as->add_link
#define getarity(hand) hand->get_arity()
#define getlink(hand,pos) hand->getOutgoingAtom(pos)
void BooleanUTest::tearDown(void)
{
}
void BooleanUTest::setUp(void)
{
as = createAtomSpace();
// Create a clauses and implicand links that will be tested.
clauses =
al(AND_LINK,
al(EVALUATION_LINK,
an(PREDICATE_NODE, "_subj"),
al(LIST_LINK,
an(CONCEPT_NODE, "be"),
an(VARIABLE_NODE, "$var1")
)
),
al(EVALUATION_LINK,
an(PREDICATE_NODE, "_obj"),
al(LIST_LINK,
an(CONCEPT_NODE, "be"),
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(ABSENT_LINK,
al(INHERITANCE_LINK,
an(VARIABLE_NODE, "$var2"),
an(CONCEPT_NODE, "definite")
)
)
);
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
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"),
hberlin = an(CONCEPT_NODE, "Berlin")
)
);
al(EVALUATION_LINK,
an(PREDICATE_NODE, "_obj"),
al(LIST_LINK,
an(CONCEPT_NODE, "be"),
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")
);
// Create data on which the above pattern should fail
al(INHERITANCE_LINK,
an(CONCEPT_NODE, "Spain"),
an(CONCEPT_NODE, "definite")
);
al(INHERITANCE_LINK,
an(CONCEPT_NODE, "Madrid"),
an(CONCEPT_NODE, "definite")
);
al(INHERITANCE_LINK,
an(CONCEPT_NODE, "capital"),
an(CONCEPT_NODE, "definite")
);
al(EVALUATION_LINK,
an(PREDICATE_NODE, "_subj"),
al(LIST_LINK,
an(CONCEPT_NODE, "be"),
an(CONCEPT_NODE, "capital")
)
);
al(EVALUATION_LINK,
an(PREDICATE_NODE, "_obj"),
al(LIST_LINK,
an(CONCEPT_NODE, "be"),
an(CONCEPT_NODE, "Madrid")
)
);
al(EVALUATION_LINK,
an(PREDICATE_NODE, "of"),
al(LIST_LINK,
an(CONCEPT_NODE, "capital"),
an(CONCEPT_NODE, "Spain")
)
);
al(MEMBER_LINK,
an(CONCEPT_NODE, "Madrid"),
an(CONCEPT_NODE, "sentence-BBB")
);
al(MEMBER_LINK,
an(CONCEPT_NODE, "capital"),
an(CONCEPT_NODE, "sentence-BBB")
);
}
/*
* preposition reasoning test.
*
* Expect to get the following result, given the above data:
*
* EvaluationLink
* PredicateNode "is_a"
* ListLink
* ConceptNode "Berlin"
* ConceptNode "city"
*/
void BooleanUTest::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
// capital is marked 'definite' and the ABSENT_LINK
// should reject that.
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__);
}