/*
* tests/query/LocalQuoteUTest.cxxtest
*
* Copyright (C) 2016 OpenCog Foundation
* All Rights Reserved
* Author: Nil Geisweiller
*
* 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 <cxxtest/TestSuite.h>
using namespace opencog;
#define al _as->add_link
#define an _as->add_node
class LocalQuoteUTest: public CxxTest::TestSuite
{
private:
AtomSpacePtr _as;
Handle X, P, Q, P_X, Q_X, forall,
VT, vardecl_var, body_var, rewrite_var,
vardecl, forall_pattern, forall_get,
A, P_A, Q_A, CT;
public:
LocalQuoteUTest()
{
logger().set_level(Logger::DEBUG);
logger().set_print_to_stdout_flag(true);
logger().set_timestamp_flag(false);
_as = createAtomSpace();
}
void setUp();
void tearDown();
void test_get();
void test_put();
};
void LocalQuoteUTest::setUp()
{
X = an(VARIABLE_NODE, "$X");
P = an(PREDICATE_NODE, "P");
Q = an(PREDICATE_NODE, "Q");
P_X = al(EDGE_LINK, P, X);
Q_X = al(EDGE_LINK, Q, X);
forall = al(LAMBDA_LINK, X, P_X);
VT = an(TYPE_NODE, "VariableNode");
vardecl_var = an(VARIABLE_NODE, "$vardecl");
body_var = an(VARIABLE_NODE, "$body");
rewrite_var = an(VARIABLE_NODE, "$rewrite");
vardecl = al(VARIABLE_LIST,
al(TYPED_VARIABLE_LINK, vardecl_var, VT),
body_var);
forall_pattern = al(LAMBDA_LINK, vardecl_var, body_var);
forall_get = al(COLLECTION_OF_LINK, {al(MEET_LINK, vardecl, al(LOCAL_QUOTE_LINK, forall_pattern))});
A = an(CONCEPT_NODE, "A");
CT = an(TYPE_NODE, "ConceptNode");
P_A = al(EDGE_LINK, P, A);
Q_A = al(EDGE_LINK, Q, A);
}
void LocalQuoteUTest::tearDown()
{
}
/**
* LocalQuoteLink unit test on a ScopeLink pattern query using Meet
*/
void LocalQuoteUTest::test_get()
{
logger().debug("BEGIN TEST: %s", __FUNCTION__);
Handle result = HandleCast(forall_get->execute(_as.get())),
expected = al(SET_LINK, al(LIST_LINK, X, P_X));
logger().debug() << "result = " << oc_to_string(result);
logger().debug() << "expected = " << oc_to_string(expected);
TS_ASSERT_EQUALS(result, expected);
logger().debug("END TEST: %s", __FUNCTION__);
}
/**
* LocalQuoteLink unit test on a RuleLink rewrite using a Put
*/
void LocalQuoteUTest::test_put()
{
logger().info("BEGIN TEST: %s", __FUNCTION__);
Handle
body = al(LOCAL_QUOTE_LINK, al(RULE_LINK, {P_X, P_X})),
vardecl = al(TYPED_VARIABLE_LINK, {X, CT}),
put = al(PUT_LINK, {al(LAMBDA_LINK, {vardecl, body}), A});
Handle putted = HandleCast(put->execute(_as.get()));
Handle expected = al(RULE_LINK, {P_A, P_A});
printf("Executing %s\n", put->to_string().c_str());
printf("Expecting %s\n", expected->to_string().c_str());
printf("Got %s\n", putted->to_string().c_str());
TS_ASSERT_EQUALS(putted, expected);
logger().info("END TEST: %s", __FUNCTION__);
}
#undef al
#undef an