1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
|
/* -*- Mode: c; c-basic-offset: 2 -*-
*
* example.c - Redland example parsing RDF from a URI, storing on disk as BDB hashes and querying the results
*
* Copyright (C) 2000-2008, David Beckett http://www.dajobe.org/
* Copyright (C) 2000-2004, University of Bristol, UK http://www.bristol.ac.uk/
*
* This package is Free Software and part of Redland http://librdf.org/
*
* It is licensed under the following three licenses as alternatives:
* 1. GNU Lesser General Public License (LGPL) V2.1 or any newer version
* 2. GNU General Public License (GPL) V2 or any newer version
* 3. Apache License, V2.0 or any newer version
*
* You may not use this file except in compliance with at least one of
* the above three licenses.
*
* See LICENSE.html or LICENSE.txt at the top of this package for the
* complete terms and further detail along with the license texts for
* the licenses in COPYING.LIB, COPYING and LICENSE-2.0.txt respectively.
*
*
*/
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <redland.h>
/* one prototype needed */
int main(int argc, char *argv[]);
int
main(int argc, char *argv[])
{
librdf_world* world;
librdf_storage* storage;
librdf_parser* parser;
librdf_model* model;
librdf_stream* stream;
librdf_node *subject, *predicate;
librdf_iterator* iterator;
librdf_statement *partial_statement, *statement2;
char *program=argv[0];
librdf_uri *uri;
char *parser_name=NULL;
int count;
raptor_world *raptor_world_ptr;
raptor_iostream* iostr;
if(argc <2 || argc >3) {
fprintf(stderr, "USAGE: %s: <RDF source URI> [rdf parser name]\n", program);
return(1);
}
world = librdf_new_world();
librdf_world_open(world);
raptor_world_ptr = librdf_world_get_raptor(world);
uri=librdf_new_uri(world, (const unsigned char*)argv[1]);
if(!uri) {
fprintf(stderr, "%s: Failed to create URI\n", program);
return(1);
}
storage=librdf_new_storage(world, "memory", "test", NULL);
if(!storage) {
fprintf(stderr, "%s: Failed to create new storage\n", program);
return(1);
}
model=librdf_new_model(world, storage, NULL);
if(!model) {
fprintf(stderr, "%s: Failed to create model\n", program);
return(1);
}
if(argc==3)
parser_name=argv[2];
parser=librdf_new_parser(world, parser_name, NULL, NULL);
if(!parser) {
fprintf(stderr, "%s: Failed to create new parser\n", program);
return(1);
}
/* PARSE the URI as RDF/XML*/
fprintf(stdout, "%s: Parsing URI %s\n", program, librdf_uri_as_string(uri));
if(librdf_parser_parse_into_model(parser, uri, uri, model)) {
fprintf(stderr, "%s: Failed to parse RDF into model\n", program);
return(1);
}
librdf_free_parser(parser);
statement2=librdf_new_statement_from_nodes(world, librdf_new_node_from_uri_string(world, (const unsigned char*)"http://www.dajobe.org/"),
librdf_new_node_from_uri_string(world, (const unsigned char*)"http://purl.org/dc/elements/1.1/title"),
librdf_new_node_from_literal(world, (const unsigned char*)"My Home Page", NULL, 0)
);
librdf_model_add_statement(model, statement2);
/* Free what we just used to add to the model - now it should be stored */
librdf_free_statement(statement2);
/* Print out the model*/
fprintf(stdout, "%s: Resulting model is:\n", program);
iostr = raptor_new_iostream_to_file_handle(raptor_world_ptr, stdout);
librdf_model_write(model, iostr);
raptor_free_iostream(iostr);
/* Construct the query predicate (arc) and object (target)
* and partial statement bits
*/
subject=librdf_new_node_from_uri_string(world, (const unsigned char*)"http://www.dajobe.org/");
predicate=librdf_new_node_from_uri_string(world, (const unsigned char*)"http://purl.org/dc/elements/1.1/title");
if(!subject || !predicate) {
fprintf(stderr, "%s: Failed to create nodes for searching\n", program);
return(1);
}
partial_statement=librdf_new_statement(world);
librdf_statement_set_subject(partial_statement, subject);
librdf_statement_set_predicate(partial_statement, predicate);
/* QUERY TEST 1 - use find_statements to match */
fprintf(stdout, "%s: Trying to find_statements\n", program);
stream=librdf_model_find_statements(model, partial_statement);
if(!stream) {
fprintf(stderr, "%s: librdf_model_find_statements returned NULL stream\n", program);
} else {
count=0;
while(!librdf_stream_end(stream)) {
librdf_statement *statement=librdf_stream_get_object(stream);
if(!statement) {
fprintf(stderr, "%s: librdf_stream_next returned NULL\n", program);
break;
}
fputs(" Matched statement: ", stdout);
librdf_statement_print(statement, stdout);
fputc('\n', stdout);
librdf_stream_next(stream);
count++;
}
librdf_free_stream(stream);
fprintf(stderr, "%s: got %d matching statements\n", program, count);
}
/* QUERY TEST 2 - use get_targets to do match */
fprintf(stdout, "%s: Trying to get targets\n", program);
iterator=librdf_model_get_targets(model, subject, predicate);
if(!iterator) {
fprintf(stderr, "%s: librdf_model_get_targets failed to return iterator for searching\n", program);
return(1);
}
count=0;
while(!librdf_iterator_end(iterator)) {
librdf_node *target;
target=(librdf_node*)librdf_iterator_get_object(iterator);
if(!target) {
fprintf(stderr, "%s: librdf_iterator_get_object returned NULL\n", program);
break;
}
fputs(" Matched target: ", stdout);
librdf_node_print(target, stdout);
fputc('\n', stdout);
count++;
librdf_iterator_next(iterator);
}
librdf_free_iterator(iterator);
fprintf(stderr, "%s: got %d target nodes\n", program, count);
librdf_free_statement(partial_statement);
/* the above does this since they are still attached */
/* librdf_free_node(predicate); */
/* librdf_free_node(object); */
librdf_free_model(model);
librdf_free_storage(storage);
librdf_free_uri(uri);
librdf_free_world(world);
#ifdef LIBRDF_MEMORY_DEBUG
librdf_memory_report(stderr);
#endif
/* keep gcc -Wall happy */
return(0);
}
|