/*
* tests/query/PatternCrashUTest.cxxtest
*
* Copyright (C) 2015 OpenCog Foundation
* 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/util/Logger.h>
#include <opencog/atomspace/AtomSpace.h>
#include <opencog/atoms/pattern/PatternLink.h>
#include <opencog/atoms/free/FindUtils.h>
#include <opencog/guile/SchemeEval.h>
#include <opencog/query/TermMatchMixin.h>
#include <opencog/query/InitiateSearchMixin.h>
#include <opencog/query/SatisfyMixin.h>
using namespace opencog;
// This implements the most basic, simplest test of the pattern matcher.
class PatternCrashUTest: public CxxTest::TestSuite
{
private:
AtomSpacePtr _as;
SchemeEval *_eval;
public:
PatternCrashUTest(void)
{
#undef DEBUG
logger().set_level(Logger::DEBUG);
logger().set_print_to_stdout_flag(true);
_as = createAtomSpace();
_eval = SchemeEval::get_scheme_evaluator(_as);
}
~PatternCrashUTest()
{
// 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_one_thing(void);
void test_empty_vars(void);
};
class PMCB :
public InitiateSearchMixin,
public TermMatchMixin,
public SatisfyMixin
{
public:
PMCB(AtomSpace* as) :
InitiateSearchMixin(as), TermMatchMixin(as) {}
virtual void set_pattern(const Variables& vars,
const Pattern& pat)
{
InitiateSearchMixin::set_pattern(vars, pat);
TermMatchMixin::set_pattern(vars, pat);
}
virtual bool propose_grounding(const HandleMap &var_soln,
const HandleMap &pred_soln);
HandleMap preds;
HandleMap vars;
};
bool PMCB::propose_grounding(const HandleMap &var_soln,
const HandleMap &pred_soln)
{
logger().debug("Inside grounding with %z var_soln", var_soln.size());
for (auto& p : var_soln)
logger().debug("var_soln map " + p.first->to_short_string() + " to " + p.second->to_short_string());
return true;
}
#define getlink(hand,pos) hand->getOutgoingAtom(pos)
void PatternCrashUTest::tearDown(void)
{
}
void PatternCrashUTest::setUp(void)
{
}
void PatternCrashUTest::test_one_thing(void)
{
logger().debug("BEGIN TEST: %s", __FUNCTION__);
// populate AtomSpace
_eval->eval(R"(
(EvaluationLink
(PredicateNode "chirps")
(ConceptNode "Tweety")
)
(EvaluationLink
(PredicateNode "eats_flies")
(ConceptNode "Tweety")
)
(InheritanceLink
(ConceptNode "Tweety")
(ConceptNode "Yello")
)
(Query
(VariableList
(VariableNode "$A")
(VariableNode "$B")
)
(AndLink
(VariableNode "$A")
(ImplicationLink
(VariableNode "$A")
(VariableNode "$B")
)
)
(ExecutionOutputLink
(GroundedSchemaNode "scm: pln-formula-simple-modus-ponens")
(ListLink
(VariableNode "$B")
(ImplicationLink
(VariableNode "$A")
(VariableNode "$B")
))))
(ListLink
(InheritanceLink
(ConceptNode "Fritz")
(ConceptNode "green")
)
(ImplicationLink
(InheritanceLink
(ConceptNode "Fritz")
(ConceptNode "Frog")
)
(InheritanceLink
(ConceptNode "Fritz")
(ConceptNode "green")
)
)
)
(ListLink
(InheritanceLink
(ConceptNode "Fritz")
(ConceptNode "Frog")
)
(ImplicationLink
(AndLink
(EvaluationLink
(PredicateNode "croaks")
(ConceptNode "Fritz")
)
(EvaluationLink
(PredicateNode "eats_flies")
(ConceptNode "Fritz")
)
)
(InheritanceLink
(ConceptNode "Fritz")
(ConceptNode "Frog")
)
)
)
(ImplicationLink
(AndLink
(EvaluationLink
(PredicateNode "chirps")
(VariableNode "$Y")
)
(EvaluationLink
(PredicateNode "sings")
(VariableNode "$Y")
)
)
(InheritanceLink
(VariableNode "$Y")
(ConceptNode "Canary")
)
)
(ImplicationLink
(AndLink
(EvaluationLink
(PredicateNode "croaks")
(VariableNode "$X")
)
(EvaluationLink
(PredicateNode "eats_flies")
(VariableNode "$X")
)
)
(InheritanceLink
(VariableNode "$X")
(ConceptNode "Frog")
)
)
(ImplicationLink
(InheritanceLink
(VariableNode "$Z")
(ConceptNode "Frog")
)
(InheritanceLink
(VariableNode "$Z")
(ConceptNode "green")
)
)
(ImplicationLink
(InheritanceLink
(VariableNode "$A")
(ConceptNode "Canary")
)
(InheritanceLink
(VariableNode "$A")
(ConceptNode "yellow")
)
)
)");
Handle htarget = _eval->eval_h(R"(
(AndLink
(InheritanceLink
(VariableNode "$Z")
(ConceptNode "Frog")
)
(ImplicationLink
(InheritanceLink
(VariableNode "$Z")
(ConceptNode "Frog")
)
(InheritanceLink
(VariableNode "$Z")
(ConceptNode "green")
)
)
)
)");
// Get all VariableNodes (unquoted)
FindAtoms fv(VARIABLE_NODE);
fv.search_set(htarget);
HandleSeq vars;
for (auto& h : fv.varset)
vars.push_back(h);
Handle htarget_vardecl = _as->add_atom(Handle(createVariableList(std::move(vars))));
Handle hpattern = _as->add_atom(Handle(createPatternLink(htarget_vardecl, htarget)));
PatternLinkPtr sl = PatternLinkCast(hpattern);
PMCB pmcb(_as.get());
logger().debug("Before Pattern Matching");
pmcb.satisfy(sl);
logger().debug("END TEST: %s", __FUNCTION__);
}
void PatternCrashUTest::test_empty_vars(void)
{
logger().debug("BEGIN TEST: %s", __FUNCTION__);
Handle hpattern = _eval->eval_h(R"(
(SatisfactionLink
(ConceptNode "ABC")
)
)");
logger().debug("Before Pattern Matching");
PatternLinkPtr sl = PatternLinkCast(hpattern);
PMCB pmcb(_as.get());
pmcb.satisfy(sl);
logger().debug("END TEST: %s", __FUNCTION__);
}