diff --git a/packages/ProbLog/problog_examples/100 b/packages/ProbLog/problog_examples/100 new file mode 100644 index 000000000..e69de29bb diff --git a/packages/raptor/CMakeLists.txt b/packages/raptor/CMakeLists.txt index 7359765ee..a72a9f8c4 100644 --- a/packages/raptor/CMakeLists.txt +++ b/packages/raptor/CMakeLists.txt @@ -14,6 +14,57 @@ IF (NOT YAP_FOUND) MESSAGE (SEND_ERROR "YAP was not found!") ENDIF (NOT YAP_FOUND) +macro_optional_find_package (LibXml2 ON) +macro_log_feature (LIBXML2_FOUND "RAPTOR" + "Use XML2 Library" + "http://www.r.org" FALSE) +IF (LIBXML2_FOUND) + # LIBXML2_FOUND - System has LibXml2 +# LIBXML2_INCLUDE_DIR - The LibXml2 include directory +# LIBXML2_LIBRARIES - The libraries needed to use LibXml2 +# LIBXML2_DEFINITIONS - Compiler switches required for using LibXml2 +# LIBXML2_XMLLINT_EXECUTABLE - The XML checking tool xmllint coming with LibXml2 +# LIBXML2_VERSION_STRING - the version of LibXml2 found (since CMake 2.8.8) + + +INCLUDE_DIRECTORIES( + ../../H + ${LIBXML2_INCLUDE_DIR} + ${CMAKE_CURRENT_BINARY_DIR} + ) + + SET ( LIBXML2_SOURCES + xml2_yap.c + ) + + ADD_LIBRARY(libxml2 SHARED ${LIBXML2_SOURCES} ) + + if(DEFINED YAP_MAJOR_VERSION) + TARGET_LINK_LIBRARIES(libxml2 + ${LIBXML2_LIBRARIES} + libYap + ) + else() + TARGET_LINK_LIBRARIES(libxml2 + ${LIBXML2_LIBRARIES} + ${YAP_LIBRARY} + ) + endif() + + check_include_files( libxml2/libxml2.h HAVE_LIBXML2_LIBXML2_H ) + + check_include_files( libxml2.h HAVE_LIBXML2_H ) + + set_target_properties (libxml2 PROPERTIES PREFIX "") + + install(TARGETS libxml2 + LIBRARY DESTINATION ${dlls} + ) + + INSTALL(FILES xml2.yap DESTINATION ${libpl}) + +ENDIF (LIBXML2_FOUND) + macro_optional_find_package (RAPTOR ON) macro_log_feature (RAPTOR_FOUND "RAPTOR" "Use RAPTOR Library" @@ -53,9 +104,6 @@ IF (RAPTOR_FOUND) set_target_properties (raptor PROPERTIES PREFIX "") - configure_file ("raptor_config.h.cmake" "raptor_config.h" ) - - install(TARGETS raptor LIBRARY DESTINATION ${dlls} ) @@ -64,3 +112,7 @@ IF (RAPTOR_FOUND) INSTALL(FILES rdf.yap DESTINATION ${libpl}) ENDIF (RAPTOR_FOUND) + + + + configure_file ("raptor_config.h.cmake" "raptor_config.h" ) diff --git a/packages/raptor/xml2.yap b/packages/raptor/xml2.yap new file mode 100644 index 000000000..9640aaaaf --- /dev/null +++ b/packages/raptor/xml2.yap @@ -0,0 +1,4 @@ + +:- module( xml2, [load_xml/2] ). + +:- load_foreign_files(['libxml2'],[],libxml2_yap_init). diff --git a/packages/raptor/xml2_yap.c b/packages/raptor/xml2_yap.c new file mode 100644 index 000000000..5cd22be7f --- /dev/null +++ b/packages/raptor/xml2_yap.c @@ -0,0 +1,166 @@ +/* Copyright (C) 2013 David Vaz + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version + * 2 of the License, or (at your option) any later version. + * + * 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 General Public + * License along with this program; if not, write to the Free + * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ +#include +#include +#include +#include +#include + + +#include "Yap.h" +#include "Yatom.h" + +#include +#include + +void libxml2_yap_init (void); + +struct exo_aux { + YAP_Functor functor; + YAP_PredEntryPtr pred; + size_t n; +}; + + +static Term read_atts(xmlAttr *att_node, sigjmp_buf *ji USES_REGS) +{ + if (att_node == NULL) + return TermNil; + Term ttail = read_atts(att_node->next, ji PASS_REGS); + Term thead; + Term tf; + if (HR > ASP-1024) + siglongjmp(*ji, 2); + if (att_node->children) { + if (att_node->children->type == XML_TEXT_NODE) { + Term ts[2]; + ts[0] = MkAtomTerm(Yap_LookupAtom((const char *)att_node->name)); + ts[1] = MkStringTerm((const char *)att_node->children->content); + thead = Yap_MkApplTerm(FunctorEq, 2, ts ); + tf = MkPairTerm(thead, ttail); + } else { + // error + tf = TermNil; + } + } else { + tf = MkAtomTerm(Yap_LookupAtom((const char *)att_node->name)); + } + if (att_node->ns) { + Term ts[2]; + ts[0] = MkAtomTerm(Yap_LookupAtom((const char *)att_node->ns->prefix)); + ts[1] = tf; + tf = Yap_MkApplTerm( FunctorModule, 2, ts ); + } + return tf; +} + +/** + * load_element_names: + * @a_node: the initial xml node to consider. + * + * Prints the names of the all the xml elements + * that are siblings or children of a given xml node. + */ +static Term +print_element_names(xmlNode * a_node, sigjmp_buf *ji USES_REGS) +{ + xmlNode *cur_node = NULL; + int count = 0; + + for (cur_node = a_node->children; cur_node; cur_node = cur_node->next) { + if (cur_node->type == XML_ELEMENT_NODE) { + count++; + } + } + if (HR > ASP-(1024+count)) + siglongjmp(*ji, 1); + Atom at = Yap_LookupAtom((char *)a_node->name); + Functor f = Yap_MkFunctor(at, count+1); + Term t = Yap_MkNewApplTerm(f, count+1); + CELL *s = RepAppl(t) + 2; + s[-1] = read_atts(a_node->properties, ji PASS_REGS); + int i = 0; + for (cur_node = a_node->children; cur_node; cur_node = cur_node->next) { + if (cur_node->type == XML_ELEMENT_NODE) { + s[i++] = print_element_names(cur_node, ji PASS_REGS); + } + } + if (a_node->ns) { + Term ts[2]; + ts[0] = MkAtomTerm(Yap_LookupAtom((const char *)a_node->ns->prefix)); + ts[1] = t; + t = Yap_MkApplTerm( FunctorModule, 2, ts ); + } + return t; +} + +static Int +load_xml ( void ) +{ + CACHE_REGS + sigjmp_buf jmp_info; + xmlDoc *doc = NULL; + xmlNode *root_element = NULL; + CELL *h0 = HR; + + const char *f = AtomOfTerm(Deref(ARG1))->StrOfAE; + + /* + * this initialize the library and check potential ABI mismatches + * between the version it was compiled for and the actual shared + * library used. + */ + LIBXML_TEST_VERSION + + /*parse the file and get the DOM */ + doc = xmlReadFile(f, NULL, 0); + + if (doc == NULL) { + fprintf(stderr, "error: could not parse file %s\n", f); + return false; + } + + + /*Get the root element node */ + root_element = xmlDocGetRootElement(doc); + if (sigsetjmp(jmp_info, 0)) { + HR = h0; + Yap_gc(2, ENV, P); + h0 = HR; + } + Term t = print_element_names(root_element, &jmp_info PASS_REGS); + + /*free the document */ + xmlFreeDoc(doc); + + /* + *Free the global variables that may + *have been allocated by the parser. + */ + xmlCleanupParser(); + + return Yap_unify(ARG2, t); +} + +extern Int YAP_UserCPredicate(const char *, Int f(void), int arity); + +void libxml2_yap_init (void) +{ + YAP_UserCPredicate("load_xml", load_xml, 2); + YAP_UserCPredicate("load_xml2", load_xml, 2); +}