static void print_nodes(FILE* fh, librdf_iterator* iterator) { int count; librdf_node* context_node; librdf_node* node; /* (Common code) Print out nodes */ count=0; while(!librdf_iterator_end(iterator)) { context_node=(librdf_node*)librdf_iterator_get_context(iterator); node=(librdf_node*)librdf_iterator_get_object(iterator); if(!node) { fprintf(stderr, ": librdf_iterator_get_object returned NULL\n"); break; } fputs("Matched node: ", fh); librdf_node_print(node, fh); if(context_node) { fputs(" with context ", fh); librdf_node_print(context_node, fh); } fputc('\n', fh); count++; librdf_iterator_next(iterator); } librdf_free_iterator(iterator); fprintf(stderr, ": matching nodes: %d\n", count); }
/* * call-seq: * graph.subjects( predicate, object ) -> [ nodes ] * * Return an Array of subject nodes from the graph that have the specified +predicate+ and +object+. * */ static VALUE rleaf_redleaf_graph_subjects( VALUE self, VALUE predicate, VALUE object ) { rleaf_GRAPH *ptr = rleaf_get_graph( self ); librdf_node *arc, *target; librdf_iterator *iter; VALUE rval = rb_ary_new(); arc = rleaf_value_to_predicate_node( predicate ); target = rleaf_value_to_object_node( object ); iter = librdf_model_get_sources( ptr->model, arc, target ); if ( !iter ) { librdf_free_node( arc ); librdf_free_node( target ); rb_raise( rleaf_eRedleafError, "failed to get sources for {? -%s-> %s}", RSTRING_PTR(rb_inspect(predicate)), RSTRING_PTR(rb_inspect(object)) ); } while ( ! librdf_iterator_end(iter) ) { librdf_node *source = librdf_iterator_get_object( iter ); VALUE subject = rleaf_librdf_node_to_value( source ); rb_ary_push( rval, subject ); librdf_iterator_next( iter ); } librdf_free_iterator( iter ); return rval; }
/* * call-seq: * graph.predicates_entailing( object ) -> nodes * * Returns an Array of predicates (URI objects) that point to the specified +object+. * */ static VALUE rleaf_redleaf_graph_predicates_entailing( VALUE self, VALUE object ) { rleaf_GRAPH *ptr = rleaf_get_graph( self ); librdf_node *target; librdf_iterator *iter; VALUE rval = rb_ary_new(); target = rleaf_value_to_subject_node( object ); iter = librdf_model_get_arcs_in( ptr->model, target ); if ( !iter ) { librdf_free_node( target ); rb_raise( rleaf_eRedleafError, "could not get arcs in for %s", RSTRING_PTR(rb_inspect(object)) ); } while ( ! librdf_iterator_end(iter) ) { librdf_node *arc = librdf_iterator_get_object( iter ); VALUE predicate; if ( !arc ) { librdf_free_iterator( iter ); rb_raise( rleaf_eRedleafError, "iterator returned NULL arc" ); } predicate = rleaf_librdf_node_to_value( arc ); rb_ary_push( rval, predicate ); librdf_iterator_next( iter ); } librdf_free_iterator( iter ); return rval; }
/* * librdf_stream_update_current_statement - helper function to get the next element with map applied * @stream: #librdf_stream object * * A helper function that gets the next element subject to the user * defined map function, if set by librdf_stream_add_map(), * * Return value: the next statement or NULL at end of stream */ static librdf_statement* librdf_stream_update_current_statement(librdf_stream* stream) { librdf_statement* statement=NULL; if(stream->is_updated) return stream->current; stream->is_updating=1; /* find next statement subject to map */ while(!stream->is_end_method(stream->context)) { librdf_iterator* map_iterator; /* Iterator over stream->map_list librdf_list */ statement=(librdf_statement*)stream->get_method(stream->context, LIBRDF_STREAM_GET_METHOD_GET_OBJECT); if(!statement) break; if(!stream->map_list || !librdf_list_size(stream->map_list)) break; map_iterator=librdf_list_get_iterator(stream->map_list); if(!map_iterator) { statement=NULL; break; } while(!librdf_iterator_end(map_iterator)) { librdf_stream_map *map=(librdf_stream_map*)librdf_iterator_get_object(map_iterator); if(!map) break; /* apply the map to the element */ statement=map->fn(stream, map->context, statement); if(!statement) break; librdf_iterator_next(map_iterator); } librdf_free_iterator(map_iterator); /* found something, return it */ if(statement) break; stream->next_method(stream->context); } stream->current=statement; if(!stream->current) stream->is_finished=1; stream->is_updated=1; stream->is_updating=0; return statement; }
/* * librdf_iterator_update_current_element - Update the current iterator element with filtering * @iterator: the #librdf_iterator object * * Helper function to set the iterator->current to the current * element as filtered optionally by a user defined * map function as set by librdf_iterator_add_map() * * Return value: the next element or NULL if the iterator has ended */ static void* librdf_iterator_update_current_element(librdf_iterator* iterator) { void *element=NULL; if(iterator->is_updated) return iterator->current; iterator->is_updating=1; /* find next element subject to map */ while(!iterator->is_end_method(iterator->context)) { librdf_iterator* map_iterator; /* Iterator over iterator->map_list librdf_list */ element=iterator->get_method(iterator->context, LIBRDF_ITERATOR_GET_METHOD_GET_OBJECT); if(!element) break; if(!iterator->map_list || !librdf_list_size(iterator->map_list)) break; map_iterator=librdf_list_get_iterator(iterator->map_list); if(!map_iterator) break; while(!librdf_iterator_end(map_iterator)) { librdf_iterator_map *map=(librdf_iterator_map*)librdf_iterator_get_object(map_iterator); if(!map) break; /* apply the map to the element */ element=map->fn(iterator, map->context, element); if(!element) break; librdf_iterator_next(map_iterator); } librdf_free_iterator(map_iterator); /* found something, return it */ if(element) break; iterator->next_method(iterator->context); } iterator->current=element; if(!iterator->current) iterator->is_finished=1; iterator->is_updated=1; iterator->is_updating=0; return element; }
static void* librdf_storage_trees_serialise_get_statement(void* context, int flags) { librdf_storage_trees_serialise_stream_context* scontext=(librdf_storage_trees_serialise_stream_context*)context; switch(flags) { case LIBRDF_ITERATOR_GET_METHOD_GET_OBJECT: return (librdf_statement*)librdf_iterator_get_object(scontext->iterator); #ifdef RDF_STORAGE_TREES_WITH_CONTEXTS case LIBRDF_ITERATOR_GET_METHOD_GET_CONTEXT: return scontext->context_node; #endif default: return NULL; } }
static void* librdf_stream_from_node_iterator_get_statement(void* context, int flags) { librdf_stream_from_node_iterator_stream_context* scontext=(librdf_stream_from_node_iterator_stream_context*)context; librdf_node* node; switch(flags) { case LIBRDF_ITERATOR_GET_METHOD_GET_OBJECT: if(!(node=(librdf_node*)librdf_iterator_get_object(scontext->iterator))) return NULL; /* The node object above is shared, no need to free it before * assigning to the statement, which is also shared, and * return to the user. */ switch(scontext->field) { case LIBRDF_STATEMENT_SUBJECT: librdf_statement_set_subject(scontext->current, node); break; case LIBRDF_STATEMENT_PREDICATE: librdf_statement_set_predicate(scontext->current, node); break; case LIBRDF_STATEMENT_OBJECT: librdf_statement_set_object(scontext->current, node); break; case LIBRDF_STATEMENT_ALL: default: librdf_log(scontext->iterator->world, 0, LIBRDF_LOG_ERROR, LIBRDF_FROM_STREAM, NULL, "Illegal statement field %d seen", scontext->field); return NULL; } return scontext->current; case LIBRDF_ITERATOR_GET_METHOD_GET_CONTEXT: return librdf_iterator_get_context(scontext->iterator); default: librdf_log(scontext->iterator->world, 0, LIBRDF_LOG_ERROR, LIBRDF_FROM_STREAM, NULL, "Unknown iterator method flag %d", flags); return NULL; } }
/* * call-seq: * graph.predicates_about( subject ) -> nodes * * Returns an Array of predicates (URI objects) that point from the specified +subject+. * */ static VALUE rleaf_redleaf_graph_predicates_about( VALUE self, VALUE subject ) { rleaf_GRAPH *ptr = rleaf_get_graph( self ); librdf_node *source; librdf_iterator *iter; VALUE rval = rb_ary_new(); rleaf_log_with_context( self, "debug", "fetching predicates about %s", RSTRING_PTR(rb_inspect( subject )) ); source = rleaf_value_to_subject_node( subject ); iter = librdf_model_get_arcs_out( ptr->model, source ); if ( !iter ) { librdf_free_node( source ); rb_raise( rleaf_eRedleafError, "could not get arcs out for %s", RSTRING_PTR(rb_inspect(subject)) ); } while ( ! librdf_iterator_end(iter) ) { librdf_node *arc = librdf_iterator_get_object( iter ); VALUE predicate; if ( !arc ) { librdf_free_iterator( iter ); librdf_free_node( source ); rb_raise( rleaf_eRedleafError, "iterator returned NULL arc" ); } predicate = rleaf_librdf_node_to_value( arc ); rb_ary_push( rval, predicate ); librdf_iterator_next( iter ); } librdf_free_iterator( iter ); librdf_free_node( source ); return rval; }
/* print the items in the tree in order (for debugging) */ void librdf_avltree_print(librdf_world* world, librdf_avltree* tree, FILE* stream, librdf_avltree_data_print_function print_fn) { int i; int rv = 0; librdf_iterator* iter; fprintf(stream, "AVL Tree size %zu\n", tree->size); for(i = 0, (iter = librdf_avltree_get_iterator_start(world, tree, NULL, NULL)); iter && !rv; i++, (rv = librdf_iterator_next(iter))) { const void* data = librdf_iterator_get_object(iter); if(!data) continue; fprintf(stream, "%d) ", i); if(print_fn) print_fn(stream, data); else fprintf(stream, "Data Node %p\n", data); } /*assert(i == tree->size);*/ }
int main(int argc, char *argv[]) { librdf_world *world; librdf_uri* prefix_uri; librdf_node* nodes[ITERATOR_NODES_COUNT]; int i; librdf_iterator* iterator; int count; char *program=argv[0]; world=librdf_new_world(); librdf_world_init_mutex(world); librdf_init_hash(world); librdf_init_uri(world); librdf_init_node(world); prefix_uri=librdf_new_uri(world, (const unsigned char*)NODE_URI_PREFIX); if(!prefix_uri) { fprintf(stderr, "%s: Failed to create prefix URI\n", program); return(1); } for(i=0; i < ITERATOR_NODES_COUNT; i++) { unsigned char buf[2]; buf[0]='a'+i; buf[1]='\0'; nodes[i]=librdf_new_node_from_uri_local_name(world, prefix_uri, buf); if(!nodes[i]) { fprintf(stderr, "%s: Failed to create node %i (%s)\n", program, i, buf); return(1); } } fprintf(stdout, "%s: Creating static node iterator\n", program); iterator=librdf_node_static_iterator_create(nodes, ITERATOR_NODES_COUNT); if(!iterator) { fprintf(stderr, "%s: Failed to createstatic node iterator\n", program); return(1); } fprintf(stdout, "%s: Listing static node iterator\n", program); count=0; while(!librdf_iterator_end(iterator)) { librdf_node* i_node=(librdf_node*)librdf_iterator_get_object(iterator); if(!i_node) { fprintf(stderr, "%s: librdf_iterator_current return NULL when not end o fiterator\n", program); return(1); } fprintf(stdout, "%s: node %d is: ", program, count); librdf_node_print(i_node, stdout); fputc('\n', stdout); if(!librdf_node_equals(i_node, nodes[count])) { fprintf(stderr, "%s: static node iterator node %i returned unexpected node\n", program, count); librdf_node_print(i_node, stderr); fputs(" rather than ", stdout); librdf_node_print(nodes[count], stderr); fputc('\n', stdout); return(1); } librdf_iterator_next(iterator); count++; } librdf_free_iterator(iterator); if(count != ITERATOR_NODES_COUNT) { fprintf(stderr, "%s: Iterator returned %d nodes, expected %d\n", program, count, ITERATOR_NODES_COUNT); return(1); } fprintf(stdout, "%s: Static node iterator worked ok\n", program); fprintf(stdout, "%s: Freeing nodes\n", program); for (i=0; i<ITERATOR_NODES_COUNT; i++) { librdf_free_node(nodes[i]); } librdf_free_uri(prefix_uri); librdf_finish_node(world); librdf_finish_uri(world); librdf_finish_hash(world); LIBRDF_FREE(librdf_world, world); /* keep gcc -Wall happy */ return(0); }
int main(int argc, char *argv[]) { const char *program = librdf_basename(argv[0]); #define ITEM_COUNT 8 const char *items[ITEM_COUNT+1] = { "ron", "amy", "jen", "bij", "jib", "daj", "jim", "def", NULL }; #define DELETE_COUNT 2 const char *delete_items[DELETE_COUNT+1] = { "daj", "jim", NULL }; #define RESULT_COUNT (ITEM_COUNT-DELETE_COUNT) const char *results[RESULT_COUNT+1] = { "amy", "bij", /*"daj",*/ "def", "jen", "jib", /*"jim",*/"ron", NULL}; librdf_world* world; librdf_avltree* tree; librdf_iterator* iter; visit_state vs; int i; world = librdf_new_world(); tree = librdf_new_avltree(compare_strings, NULL); /* no free as they are static pointers above */ if(!tree) { fprintf(stderr, "%s: Failed to create tree\n", program); exit(1); } for(i = 0; items[i]; i++) { int rc; void* node; #if LIBRDF_DEBUG > 1 fprintf(stderr, "%s: Adding tree item '%s'\n", program, items[i]); #endif rc = librdf_avltree_add(tree, (void*)items[i]); if(rc) { fprintf(stderr, "%s: Adding tree item %d '%s' failed, returning error %d\n", program, i, items[i], rc); exit(1); } #ifdef LIBRDF_DEBUG librdf_avltree_check(tree); #endif node = librdf_avltree_search(tree, (void*)items[i]); if(!node) { fprintf(stderr, "%s: Tree did NOT contain item %d '%s' as expected\n", program, i, items[i]); exit(1); } } #if LIBRDF_DEBUG > 1 fprintf(stderr, "%s: Printing tree\n", program); vs.fh = stderr; vs.count = 0; librdf_avltree_visit(tree, print_string, &vs); //fprintf(stderr, "%s: Dumping tree\n", program); //librdf_avltree_dump(tree, stderr, print_string); #endif for(i = 0; delete_items[i]; i++) { int rc; #if LIBRDF_DEBUG > 1 fprintf(stderr, "%s: Deleting tree item '%s'\n", program, delete_items[i]); #endif rc = librdf_avltree_delete(tree, (void*)delete_items[i]); if(!rc) { fprintf(stderr, "%s: Deleting tree item %d '%s' failed, returning error %d\n", program, i, delete_items[i], rc); exit(1); } #ifdef LIBRDF_DEBUG librdf_avltree_check(tree); #endif } #if LIBRDF_DEBUG > 1 fprintf(stderr, "%s: Walking tree forwards via iterator\n", program); #endif iter = librdf_avltree_get_iterator_start(world, tree, NULL, NULL); for(i = 0; 1; i++) { const char* data = (const char*)librdf_iterator_get_object(iter); const char* result = results[i]; if((!data && data != result) || (data && strcmp(data, result))) { fprintf(stderr, "%3d: Forwards iterator expected '%s' but found '%s'\n", i, result, data); exit(1); } #if LIBRDF_DEBUG > 1 fprintf(stderr, "%3d: Got '%s'\n", i, data); #endif if(librdf_iterator_next(iter)) break; if(i > RESULT_COUNT) { fprintf(stderr, "Forward iterator did not end on result %i as expected\n", i); exit(1); } } #if LIBRDF_DEBUG > 1 fprintf(stderr, "%s: Checking tree\n", program); #endif vs.count = 0; vs.results=results; vs.failed = 0; librdf_avltree_visit(tree, check_string, &vs); if(vs.failed) { fprintf(stderr, "%s: Checking tree failed\n", program); exit(1); } for(i = 0; results[i]; i++) { const char* result = results[i]; char* data = (char*)librdf_avltree_remove(tree, (void*)result); if(!data) { fprintf(stderr, "%s: remove %i failed at item '%s'\n", program, i, result); exit(1); } if(strcmp(data, result)) { fprintf(stderr, "%s: remove %i returned %s not %s as expected\n", program, i, data, result); exit(1); } } #if LIBRDF_DEBUG > 1 fprintf(stderr, "%s: Freeing tree\n", program); #endif librdf_free_avltree(tree); librdf_free_world(world); /* keep gcc -Wall happy */ return(0); }
static int rasqal_redland_new_triples_source(rasqal_query* rdf_query, void *factory_user_data, void *user_data, rasqal_triples_source *rts) { librdf_world *world=(librdf_world*)factory_user_data; rasqal_redland_triples_source_user_data* rtsc=(rasqal_redland_triples_source_user_data*)user_data; raptor_sequence *seq; librdf_query_rasqal_context *context; librdf_iterator* cit; rtsc->world = world; rtsc->query = (librdf_query*)rasqal_query_get_user_data(rdf_query); context = (librdf_query_rasqal_context*)rtsc->query->context; rtsc->model = context->model; seq = rasqal_query_get_data_graph_sequence(rdf_query); /* FIXME: queries with data graphs in them (such as FROM in SPARQL) * are deleted, so that there are no unexpected data loads */ if(seq) { while(raptor_sequence_size(seq)) { rasqal_data_graph* dg=(rasqal_data_graph*)raptor_sequence_pop(seq); rasqal_free_data_graph(dg); } } if(librdf_model_supports_contexts(rtsc->model)) { /* Add all contexts (named graphs) to the query so Rasqal can bind them */ cit = librdf_model_get_contexts(rtsc->model); while(!librdf_iterator_end(cit)) { librdf_node* node = (librdf_node*)librdf_iterator_get_object(cit); librdf_uri* uri; raptor_uri* source_uri; rasqal_data_graph* dg; uri = librdf_node_get_uri(node); source_uri = (raptor_uri*)raptor_new_uri(world->raptor_world_ptr, librdf_uri_as_string(uri)); dg = rasqal_new_data_graph_from_uri(world->rasqal_world_ptr, source_uri, source_uri, RASQAL_DATA_GRAPH_NAMED, NULL, NULL, NULL); rasqal_query_add_data_graph(rdf_query, dg); raptor_free_uri(source_uri); librdf_iterator_next(cit); } librdf_free_iterator(cit); } #ifdef RASQAL_TRIPLES_SOURCE_MIN_VERSION rts->version = 1; #endif rts->init_triples_match=rasqal_redland_init_triples_match; rts->triple_present=rasqal_redland_triple_present; rts->free_triples_source=rasqal_redland_free_triples_source; return 0; }
int main(int argc, char *argv[]) { librdf_world* world; librdf_parser* parser; librdf_serializer* serializer; librdf_storage *storage; librdf_model* model; librdf_node *source, *arc, *target, *node; librdf_node *subject, *predicate, *object; librdf_node* context_node=NULL; librdf_stream* stream; librdf_iterator* iterator; librdf_uri *uri; librdf_uri *base_uri=NULL; librdf_query *query; librdf_query_results *results; librdf_hash *options; int count; int rc; int transactions=0; const char* storage_name; const char* storage_options; const char* context; const char* identifier; const char* results_format; librdf_statement* statement=NULL; char* query_cmd=NULL; char* s; /* * Initialize */ storage_name="virtuoso"; results_format="xml"; context=DEFAULT_CONTEXT; identifier=DEFAULT_IDENTIFIER; /* * Get connection options */ if(argc == 2 && argv[1][0] != '\0') storage_options=argv[1]; else if((s=getenv ("VIRTUOSO_STORAGE_OPTIONS")) != NULL) storage_options=s; else storage_options=DEFAULT_STORAGE_OPTIONS; world=librdf_new_world(); librdf_world_set_logger(world, world, log_handler); librdf_world_open(world); options=librdf_new_hash(world, NULL); librdf_hash_open(options, NULL, 0, 1, 1, NULL); librdf_hash_put_strings(options, "contexts", "yes"); transactions=1; librdf_hash_from_string(options, storage_options); storage=librdf_new_storage_with_options(world, storage_name, identifier, options); if(!storage) { fprintf(stderr, ": Failed to open %s storage '%s'\n", storage_name, identifier); return(1); } model=librdf_new_model(world, storage, NULL); if(!model) { fprintf(stderr, ": Failed to create model\n"); return(1); } if(transactions) librdf_model_transaction_start(model); /* Do this or gcc moans */ stream=NULL; iterator=NULL; parser=NULL; serializer=NULL; source=NULL; arc=NULL; target=NULL; subject=NULL; predicate=NULL; object=NULL; uri=NULL; node=NULL; query=NULL; results=NULL; context_node=librdf_new_node_from_uri_string(world, (const unsigned char *)context); /**** Test 1 *******/ startTest(1, " Remove all triples in <%s> context\n", context); { rc=librdf_model_context_remove_statements(model, context_node); if(rc) endTest(0, " failed to remove context triples from the graph\n"); else endTest(1, " removed context triples from the graph\n"); } /**** Test 2 *******/ startTest(2, " Add triples to <%s> context\n", context); { rc=0; rc |= add_triple(world, context_node, model, "aa", "bb", "cc"); rc |= add_triple(world, context_node, model, "aa", "bb1", "cc"); rc |= add_triple(world, context_node, model, "aa", "a2", "_:cc"); rc |= add_triple_typed(world, context_node, model, "aa", "a2", "cc"); rc |= add_triple_typed(world, context_node, model, "mm", "nn", "Some long literal with language@en"); rc |= add_triple_typed(world, context_node, model, "oo", "pp", "12345^^<http://www.w3.org/2001/XMLSchema#int>"); if(rc) endTest(0, " failed add triple\n"); else endTest(1, " add triple to context\n"); } /**** Test 3 *******/ startTest(3, " Print all triples in <%s> context\n", context); { raptor_iostream* iostr = raptor_new_iostream_to_file_handle(librdf_world_get_raptor(world), stdout); librdf_model_write(model, iostr); raptor_free_iostream(iostr); endTest(1, "\n"); } /***** Test 4 *****/ startTest(4, " Count of triples in <%s> context\n", context); { count=librdf_model_size(model); if(count >= 0) endTest(1, " graph has %d triples\n", count); else endTest(0, " graph has unknown number of triples\n"); } /***** Test 5 *****/ startTest(5, " Exec: ARC aa bb \n"); { subject=librdf_new_node_from_uri_string(world, (const unsigned char*)"aa"); predicate=librdf_new_node_from_uri_string(world, (const unsigned char*)"bb"); node=librdf_model_get_target(model, subject, predicate); librdf_free_node(subject); librdf_free_node(predicate); if(!node) { endTest(0, " Failed to get arc\n"); } else { print_node(stdout, node); librdf_free_node(node); endTest(1, "\n"); } } /***** Test 6 *****/ startTest(6, " Exec: ARCS aa cc \n"); { subject=librdf_new_node_from_uri_string(world, (const unsigned char*)"aa"); object=librdf_new_node_from_uri_string(world, (const unsigned char*)"cc"); iterator=librdf_model_get_arcs(model, subject, object); librdf_free_node(subject); librdf_free_node(object); if(!iterator) { endTest(0, " Failed to get arcs\n"); } else { print_nodes(stdout, iterator); endTest(1, "\n"); } } /***** Test 7 *****/ startTest(7, " Exec: ARCS-IN cc \n"); { object=librdf_new_node_from_uri_string(world, (const unsigned char*)"cc"); iterator=librdf_model_get_arcs_in(model, object); librdf_free_node(object); if(!iterator) { endTest(0, " Failed to get arcs in\n"); } else { int ok=1; count=0; while(!librdf_iterator_end(iterator)) { context_node=(librdf_node*)librdf_iterator_get_context(iterator); node=(librdf_node*)librdf_iterator_get_object(iterator); /*returns SHARED pointer */ if(!node) { ok=0; endTest(ok, " librdf_iterator_get_next returned NULL\n"); break; } fputs("Matched arc: ", stdout); librdf_node_print(node, stdout); if(context_node) { fputs(" with context ", stdout); librdf_node_print(context_node, stdout); } fputc('\n', stdout); count++; librdf_iterator_next(iterator); } librdf_free_iterator(iterator); endTest(ok, " matching arcs: %d\n", count); } } /***** Test 8 *****/ startTest(8, " Exec: ARCS-OUT aa \n"); { subject=librdf_new_node_from_uri_string(world, (const unsigned char*)"aa"); iterator=librdf_model_get_arcs_out(model, subject); librdf_free_node(subject); if(!iterator) endTest(0, " Failed to get arcs out\n"); else { int ok=1; count=0; while(!librdf_iterator_end(iterator)) { context_node=(librdf_node*)librdf_iterator_get_context(iterator); node=(librdf_node*)librdf_iterator_get_object(iterator); if(!node) { ok=0; endTest(ok, " librdf_iterator_get_next returned NULL\n"); break; } fputs("Matched arc: ", stdout); librdf_node_print(node, stdout); if(context_node) { fputs(" with context ", stdout); librdf_node_print(context_node, stdout); } fputc('\n', stdout); count++; librdf_iterator_next(iterator); } librdf_free_iterator(iterator); endTest(ok, " matching arcs: %d\n", count); } } /***** Test 9 *****/ startTest(9, " Exec: CONTAINS aa bb1 cc \n"); { subject=librdf_new_node_from_uri_string(world, (const unsigned char*)"aa"); predicate=librdf_new_node_from_uri_string(world, (const unsigned char*)"bb1"); object=librdf_new_node_from_uri_string(world, (const unsigned char*)"cc"); statement=librdf_new_statement(world); librdf_statement_set_subject(statement, subject); librdf_statement_set_predicate(statement, predicate); librdf_statement_set_object(statement, object); if(librdf_model_contains_statement(model, statement)) endTest(1, " the graph contains the triple\n"); else endTest(0, " the graph does not contain the triple\n"); librdf_free_statement(statement); } /***** Test 10 *****/ startTest(10, " Exec: FIND aa - - \n"); { subject=librdf_new_node_from_uri_string(world, (const unsigned char*)"aa"); statement=librdf_new_statement(world); librdf_statement_set_subject(statement, subject); stream=librdf_model_find_statements_in_context(model, statement, context_node); if(!stream) { endTest(0, " FIND returned no results (NULL stream)\n"); } else { librdf_node* ctxt_node=NULL; int ok=1; count=0; while(!librdf_stream_end(stream)) { librdf_statement *stmt=librdf_stream_get_object(stream); ctxt_node=(librdf_node*)librdf_stream_get_context(stream); if(!stmt) { ok=0; endTest(ok, " librdf_stream_next returned NULL\n"); break; } fputs("Matched triple: ", stdout); librdf_statement_print(stmt, stdout); if(context) { fputs(" with context ", stdout); librdf_node_print(ctxt_node, stdout); } fputc('\n', stdout); count++; librdf_stream_next(stream); } librdf_free_stream(stream); endTest(ok, " matching triples: %d\n", count); } librdf_free_statement(statement); } /***** Test 11 *****/ startTest(11, " Exec: HAS-ARC-IN cc bb \n"); { predicate=librdf_new_node_from_uri_string(world, (const unsigned char*)"bb"); object=librdf_new_node_from_uri_string(world, (const unsigned char*)"cc"); if(librdf_model_has_arc_in(model, object, predicate)) endTest(1, " the graph contains the arc\n"); else endTest(0, " the graph does not contain the arc\n"); librdf_free_node(predicate); librdf_free_node(object); } /***** Test 12 *****/ startTest(12, " Exec: HAS-ARC-OUT aa bb \n"); { subject=librdf_new_node_from_uri_string(world, (const unsigned char*)"aa"); predicate=librdf_new_node_from_uri_string(world, (const unsigned char*)"bb"); if(librdf_model_has_arc_out(model, subject, predicate)) endTest(1, " the graph contains the arc\n"); else endTest(0, " the graph does not contain the arc\n"); librdf_free_node(predicate); librdf_free_node(subject); } /***** Test 13 *****/ startTest(13, " Exec: SOURCE aa cc \n"); { predicate=librdf_new_node_from_uri_string(world, (const unsigned char*)"bb"); object=librdf_new_node_from_uri_string(world, (const unsigned char*)"cc"); node=librdf_model_get_source(model, predicate, object); librdf_free_node(predicate); librdf_free_node(object); if(!node) { endTest(0, " Failed to get source\n"); } else { print_node(stdout, node); librdf_free_node(node); endTest(1, "\n"); } } /***** Test 14 *****/ startTest(14, " Exec: SOURCES bb cc \n"); { predicate=librdf_new_node_from_uri_string(world, (const unsigned char*)"bb"); object=librdf_new_node_from_uri_string(world, (const unsigned char*)"cc"); iterator=librdf_model_get_sources(model, predicate, object); librdf_free_node(predicate); librdf_free_node(object); if(!iterator) { endTest(0, " Failed to get sources\n"); } else { print_nodes(stdout, iterator); endTest(1, "\n"); } } /***** Test 15 *****/ startTest(15, " Exec: TARGET aa bb \n"); { subject=librdf_new_node_from_uri_string(world, (const unsigned char*)"aa"); predicate=librdf_new_node_from_uri_string(world, (const unsigned char*)"bb"); node=librdf_model_get_target(model, subject, predicate); librdf_free_node(subject); librdf_free_node(predicate); if(!node) { endTest(0, " Failed to get target\n"); } else { print_node(stdout, node); librdf_free_node(node); endTest(1, "\n"); } } /***** Test 16 *****/ startTest(16, " Exec: TARGETS aa bb \n"); { subject=librdf_new_node_from_uri_string(world, (const unsigned char*)"aa"); predicate=librdf_new_node_from_uri_string(world, (const unsigned char*)"bb"); iterator=librdf_model_get_targets(model, subject, predicate); librdf_free_node(subject); librdf_free_node(predicate); if(!iterator) { endTest(0, " Failed to get targets\n"); } else { print_nodes(stdout, iterator); endTest(1, "\n"); } } /***** Test 17 *****/ startTest(17, " Exec: REMOVE aa bb1 cc \n"); { subject=librdf_new_node_from_uri_string(world, (const unsigned char*)"aa"); predicate=librdf_new_node_from_uri_string(world, (const unsigned char*)"bb1"); object=librdf_new_node_from_uri_string(world, (const unsigned char*)"cc"); statement=librdf_new_statement(world); librdf_statement_set_subject(statement, subject); librdf_statement_set_predicate(statement, predicate); librdf_statement_set_object(statement, object); if(librdf_model_context_remove_statement(model, context_node, statement)) endTest(0, " failed to remove triple from the graph\n"); else endTest(1, " removed triple from the graph\n"); librdf_free_statement(statement); } /***** Test 18 *****/ query_cmd=(char *)"CONSTRUCT {?s ?p ?o} FROM <http://red> WHERE {?s ?p ?o}"; startTest(18, " Exec: QUERY \"%s\" \n", query_cmd); { query=librdf_new_query(world, (char *)"vsparql", NULL, (const unsigned char *)query_cmd, NULL); if(!(results=librdf_model_query_execute(model, query))) { endTest(0, " Query of model with '%s' failed\n", query_cmd); librdf_free_query(query); query=NULL; } else { stream=librdf_query_results_as_stream(results); if(!stream) { endTest(0, " QUERY returned no results (NULL stream)\n"); } else { librdf_node* ctxt=NULL; count=0; while(!librdf_stream_end(stream)) { librdf_statement *stmt=librdf_stream_get_object(stream); /*returns SHARED pointer */ ctxt=(librdf_node*)librdf_stream_get_context(stream); if(!stmt) { endTest(0, " librdf_stream_next returned NULL\n"); break; } fputs("Matched triple: ", stdout); librdf_statement_print(stmt, stdout); if(ctxt) { fputs(" with context ", stdout); librdf_node_print(ctxt, stdout); } fputc('\n', stdout); count++; librdf_stream_next(stream); } librdf_free_stream(stream); endTest(1, " matching triples: %d\n", count); librdf_free_query_results(results); } } librdf_free_query(query); } /***** Test 19 *****/ query_cmd=(char *)"SELECT * WHERE {graph <http://red> { ?s ?p ?o }}"; startTest(19, " Exec1: QUERY_AS_BINDINGS \"%s\" \n", query_cmd); { query=librdf_new_query(world, (char *)"vsparql", NULL, (const unsigned char *)query_cmd, NULL); if(!(results=librdf_model_query_execute(model, query))) { endTest(0, " Query of model with '%s' failed\n", query_cmd); librdf_free_query(query); query=NULL; } else { raptor_iostream *iostr; librdf_query_results_formatter *formatter; fprintf(stderr, "**: Formatting query result as '%s':\n", results_format); iostr = raptor_new_iostream_to_file_handle(librdf_world_get_raptor(world), stdout); formatter = librdf_new_query_results_formatter2(results, results_format, NULL /* mime type */, NULL /* format_uri */); base_uri = librdf_new_uri(world, (const unsigned char*)"http://example.org/"); librdf_query_results_formatter_write(iostr, formatter, results, base_uri); librdf_free_query_results_formatter(formatter); raptor_free_iostream(iostr); librdf_free_uri(base_uri); endTest(1, "\n"); librdf_free_query_results(results); } librdf_free_query(query); } /***** Test 20 *****/ query_cmd=(char *)"SELECT * WHERE {graph <http://red> { ?s ?p ?o }}"; startTest(20, " Exec2: QUERY_AS_BINDINGS \"%s\" \n", query_cmd); { query=librdf_new_query(world, (char *)"vsparql", NULL, (const unsigned char *)query_cmd, NULL); if(!(results=librdf_model_query_execute(model, query))) { endTest(0, " Query of model with '%s' failed\n", query_cmd); librdf_free_query(query); query=NULL; } else { if(print_query_results(world, model, results)) endTest(0, "\n"); else endTest(1, "\n"); librdf_free_query_results(results); } librdf_free_query(query); } getTotal(); if(transactions) librdf_model_transaction_commit(model); librdf_free_node(context_node); librdf_free_node(context_node); librdf_free_hash(options); librdf_free_model(model); librdf_free_storage(storage); librdf_free_world(world); #ifdef LIBRDF_MEMORY_DEBUG librdf_memory_report(stderr); #endif /* keep gcc -Wall happy */ return(0); }