This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
yap-6.3/packages/raptor/raptor_yap.c

168 lines
4.6 KiB
C
Raw Normal View History

2015-10-13 08:17:51 +01:00
/* Copyright (C) 2013 David Vaz <davidvaz@dcc.fc.up.pt>
*
* 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 <stdarg.h>
2016-10-20 04:44:59 +01:00
#include <stdio.h>
2015-10-13 08:17:51 +01:00
#include <stdlib.h>
#include <string.h>
#include "YapInterface.h"
2016-10-20 04:44:59 +01:00
#include "raptor_config.h"
#ifdef HAVE_RAPTOR2_RAPTOR2_H
2015-10-13 08:17:51 +01:00
#include "raptor2/raptor2.h"
#else
#include "raptor2.h"
#endif
X_API void raptor_yap_init(void);
2015-10-13 08:17:51 +01:00
2016-10-20 04:44:59 +01:00
raptor_world *world;
2015-10-13 08:17:51 +01:00
struct exo_aux {
YAP_Functor functor;
YAP_PredEntryPtr pred;
size_t n;
};
2016-10-20 04:44:59 +01:00
static YAP_Atom term_load(const raptor_term *term) {
2015-10-13 08:17:51 +01:00
size_t len;
2016-10-20 04:44:59 +01:00
switch (term->type) {
case RAPTOR_TERM_TYPE_LITERAL:
// fprintf(stderr, "%s,", term->value.literal.string);
return YAP_LookupAtom((const char *)term->value.literal.string);
case RAPTOR_TERM_TYPE_BLANK:
// fprintf(stderr, "%s,", term->value.blank.string);
return YAP_LookupAtom((const char *)term->value.blank.string);
case RAPTOR_TERM_TYPE_URI:
// fprintf(stderr, "%s,",
// raptor_uri_as_counted_string(term->value.uri, &len));
return YAP_LookupAtom(
(const char *)raptor_uri_as_counted_string(term->value.uri, &len));
case RAPTOR_TERM_TYPE_UNKNOWN:
default:
raptor_log_error_formatted(term->world, RAPTOR_LOG_LEVEL_ERROR, NULL,
2016-10-20 04:44:59 +01:00
"Triple has unsupported term type %d",
term->type);
break;
}
2015-10-13 08:17:51 +01:00
2016-10-20 04:44:59 +01:00
return NULL;
2015-10-13 08:17:51 +01:00
}
static int so_far = 0;
2016-10-20 04:44:59 +01:00
static void load_triples(void *user_data, raptor_statement *triple) {
struct exo_aux *aux = (struct exo_aux *)user_data;
2015-10-13 08:17:51 +01:00
YAP_Term args[4];
2016-10-20 04:44:59 +01:00
// args[0] = (YAP_CELL)aux->functor;
2015-10-13 08:17:51 +01:00
args[0] = YAP_MkAtomTerm(term_load(triple->subject));
args[1] = YAP_MkAtomTerm(term_load(triple->predicate));
args[2] = YAP_MkAtomTerm(term_load(triple->object));
2016-10-20 04:44:59 +01:00
// fprintf(stderr, "\n");
2015-10-13 08:17:51 +01:00
2016-10-20 04:44:59 +01:00
YAP_AssertTuples(aux->pred, args, so_far++, 1);
2015-10-13 08:17:51 +01:00
}
2016-10-20 04:44:59 +01:00
static void count_triples(void *user_data, raptor_statement *triple) {
unsigned int *count_p = (unsigned int *)user_data;
2015-10-13 08:17:51 +01:00
(*count_p)++;
term_load(triple->subject);
term_load(triple->predicate);
term_load(triple->object);
2016-10-20 04:44:59 +01:00
// fprintf(stderr, "\n");
2015-10-13 08:17:51 +01:00
}
2016-10-20 04:44:59 +01:00
static YAP_Bool load(void) {
2015-10-13 08:17:51 +01:00
YAP_Term tfn = YAP_ARG1;
YAP_Term mod = YAP_ARG2;
YAP_Term tfunctor = YAP_ARG3;
const char *filename;
2016-10-20 04:44:59 +01:00
raptor_parser *rdf_parser = NULL;
2015-10-13 08:17:51 +01:00
unsigned int count;
unsigned char *uri_string;
raptor_uri *uri, *base_uri;
if (YAP_IsVarTerm(tfn) || !YAP_IsAtomTerm(tfn)) {
2016-10-20 04:44:59 +01:00
return FALSE;
}
2015-10-13 08:17:51 +01:00
filename = YAP_AtomName(YAP_AtomOfTerm(tfn));
rdf_parser = raptor_new_parser(world, "rdfxml");
raptor_parser_set_statement_handler(rdf_parser, &count, count_triples);
uri_string = raptor_uri_filename_to_uri_string(filename);
uri = raptor_new_uri(world, uri_string);
base_uri = raptor_uri_copy(uri);
count = 0;
2016-10-20 04:44:59 +01:00
if (!raptor_parser_parse_file(rdf_parser, uri, base_uri)) {
// fprintf(stderr, "%s : %d triples\n", filename, count);
2015-10-13 08:17:51 +01:00
} else {
2016-10-20 04:44:59 +01:00
fprintf(stderr, "%s : failed to parse\n", filename);
return FALSE;
2015-10-13 08:17:51 +01:00
}
/* now lets load */
{
struct exo_aux aux;
size_t sz;
aux.functor = YAP_MkFunctor(YAP_AtomOfTerm(tfunctor), 3);
2016-10-20 04:44:59 +01:00
aux.pred = YAP_FunctorToPredInModule(aux.functor, mod);
sz = 3 * sizeof(YAP_CELL) * count;
if (!YAP_NewExo(aux.pred, sz, NULL)) {
2015-10-13 08:17:51 +01:00
fprintf(stderr, "Failed to alocate space\n");
return FALSE;
}
aux.n = 0;
2016-10-20 04:44:59 +01:00
raptor_parser_set_statement_handler(rdf_parser, (void *)&aux, load_triples);
if (!raptor_parser_parse_file(rdf_parser, uri, base_uri)) {
fprintf(stderr, "%s : %d triples\n", filename, count);
2015-10-13 08:17:51 +01:00
}
}
raptor_free_uri(base_uri);
raptor_free_uri(uri);
raptor_free_memory(uri_string);
raptor_free_parser(rdf_parser);
return TRUE;
}
2016-10-20 04:44:59 +01:00
static inline void raptor_yap_halt(int exit, void *world) {
raptor_free_world((raptor_world *)world);
2015-10-13 08:17:51 +01:00
}
X_API void raptor_yap_init(void) {
2015-10-13 08:17:51 +01:00
world = raptor_new_world();
2016-10-20 04:44:59 +01:00
YAP_HaltRegisterHook(raptor_yap_halt, (void *)world);
2015-10-13 08:17:51 +01:00
YAP_UserCPredicate("rdf_load", load, 3);
}