From e3bedc90c7a82db8fe338a3da2f05d0af7cf9f03 Mon Sep 17 00:00:00 2001 From: yidnekachew Date: Thu, 19 Jul 2018 12:20:59 +0300 Subject: [PATCH] init atomese reduct --- moses/atomese/CMakeLists.txt | 2 + moses/atomese/reduct/CMakeLists.txt | 9 ++++ moses/atomese/reduct/reduct.cc | 38 ++++++++++++++++ moses/atomese/reduct/reduct.h | 41 ++++++++++++++++++ tests/atomese/CMakeLists.txt | 1 + tests/atomese/reduct/CMakeLists.txt | 8 ++++ tests/atomese/reduct/reductUTest.cxxtest | 55 ++++++++++++++++++++++++ 7 files changed, 154 insertions(+) create mode 100644 moses/atomese/reduct/CMakeLists.txt create mode 100644 moses/atomese/reduct/reduct.cc create mode 100644 moses/atomese/reduct/reduct.h create mode 100644 tests/atomese/reduct/CMakeLists.txt create mode 100644 tests/atomese/reduct/reductUTest.cxxtest diff --git a/moses/atomese/CMakeLists.txt b/moses/atomese/CMakeLists.txt index 2f9c8e9b24..fd34b6ff84 100644 --- a/moses/atomese/CMakeLists.txt +++ b/moses/atomese/CMakeLists.txt @@ -1,7 +1,9 @@ ADD_SUBDIRECTORY(representation) +ADD_SUBDIRECTORY(reduct) ADD_LIBRARY(atomese SHARED representation/load_table + reduct/reduct ) TARGET_LINK_LIBRARIES (atomese diff --git a/moses/atomese/reduct/CMakeLists.txt b/moses/atomese/reduct/CMakeLists.txt new file mode 100644 index 0000000000..8b1accbfbb --- /dev/null +++ b/moses/atomese/reduct/CMakeLists.txt @@ -0,0 +1,9 @@ +#install header files +INSTALL(FILES + + reduct.h + + DESTINATION + + "include/${PROJECT_NAME}/atomese/reduct" + ) diff --git a/moses/atomese/reduct/reduct.cc b/moses/atomese/reduct/reduct.cc new file mode 100644 index 0000000000..1a3a60af0c --- /dev/null +++ b/moses/atomese/reduct/reduct.cc @@ -0,0 +1,38 @@ +/** reduct.cc --- + * + * Copyright (C) 2018 OpenCog Foundation + * + * Author: Yidnekachew Wondimu + * + * 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 +#include +#include "reduct.h" + +namespace opencog { +namespace atomese { + +Handle full_reduce(combo::combo_tree& tr) +{ +// combo::combo_tree& tr = combo::atomese_combo(to_reduce); + reduct::full_reduce(tr); + return combo::atomese_combo(tr); +} + +} +} diff --git a/moses/atomese/reduct/reduct.h b/moses/atomese/reduct/reduct.h new file mode 100644 index 0000000000..c2a2a82111 --- /dev/null +++ b/moses/atomese/reduct/reduct.h @@ -0,0 +1,41 @@ +/** reduct.h --- + * + * Copyright (C) 2018 OpenCog Foundation + * + * Author: Yidnekachew Wondimu + * + * 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. + */ + +#ifndef _OPENCOG_ATOMESE_REDUCT_H +#define _OPENCOG_ATOMESE_REDUCT_H + +namespace opencog { +namespace atomese { + +/** + * Apply full_reduction on an atomese program. + * + * @param tr a combo_tree to be reduced. + * @todo it should accept a Handle as an argument after the combo->atomese converter is done. + * @return + */ +Handle full_reduce(combo::combo_tree& tr); + +} +} + +#endif //_OPENCOG_ATOMESE_REDUCT_H diff --git a/tests/atomese/CMakeLists.txt b/tests/atomese/CMakeLists.txt index e3a846ccb3..8394afc896 100644 --- a/tests/atomese/CMakeLists.txt +++ b/tests/atomese/CMakeLists.txt @@ -1 +1,2 @@ ADD_SUBDIRECTORY (representation) +ADD_SUBDIRECTORY (reduct) diff --git a/tests/atomese/reduct/CMakeLists.txt b/tests/atomese/reduct/CMakeLists.txt new file mode 100644 index 0000000000..735d9d84b7 --- /dev/null +++ b/tests/atomese/reduct/CMakeLists.txt @@ -0,0 +1,8 @@ +IF (HAVE_GUILE) + ADD_CXXTEST(reductUTest) + TARGET_LINK_LIBRARIES(reductUTest + atomese + ${ATOMSPACE_LIBRARIES} + ${GUILE_LIBRARIES} + ) +ENDIF (HAVE_GUILE) diff --git a/tests/atomese/reduct/reductUTest.cxxtest b/tests/atomese/reduct/reductUTest.cxxtest new file mode 100644 index 0000000000..df52446f8e --- /dev/null +++ b/tests/atomese/reduct/reductUTest.cxxtest @@ -0,0 +1,55 @@ +#include +#include +#include +#include +#include +#include "moses/atomese/reduct/reduct.h" + +using namespace opencog; +using namespace std; + +#define cl createLink +#define cn createNode + +class reductUTest : public CxxTest::TestSuite +{ +public: + reductUTest(); + + ~reductUTest(); + + void test_full_reduce(); +}; + +reductUTest::reductUTest() +{ + logger().set_level(Logger::DEBUG); + logger().set_print_to_stdout_flag(true); +} + +reductUTest::~reductUTest() +{ + // Erase the log file if no assertions failed. + if (!CxxTest::TestTracker::tracker().suiteFailed()) + std::remove(logger().get_filename().c_str()); +} + +// test atomese full_reduction. +void reductUTest::test_full_reduce() +{ + Handle X = cn(PREDICATE_NODE, "1"), + Y = cn(PREDICATE_NODE, "2"); + + // TODO: this should be passed to atomese::full_reduce instead of a combo_tree. + // Handle to_reduce = cl(OR_LINK, + // cl(AND_LINK, X, X), + // Y); + + combo::combo_tree tr = combo::str2combo_tree("or(and($1 $1) $2)", {}); + + Handle result = atomese::full_reduce(tr); + + Handle expected = cl(OR_LINK, X, Y); + + TS_ASSERT(content_eq(expected, result)); +}