/* * 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.include_object?( object ) -> true or false * * Returns +true+ if the graph contains any statements with the specified +object+. * */ static VALUE rleaf_redleaf_graph_include_object_p( VALUE self, VALUE object ) { rleaf_GRAPH *ptr = rleaf_get_graph( self ); librdf_node *target; librdf_iterator *iter; VALUE rval = Qfalse; rleaf_log_with_context( self, "debug", "Checking for statements with %s as the object", RSTRING_PTR(rb_inspect( object )) ); target = rleaf_value_to_object_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 out for %s", RSTRING_PTR(rb_inspect(object)) ); } /* If it's not empty, there was at least one matching statement. */ if ( ! librdf_iterator_end(iter) ) rval = Qtrue; librdf_free_iterator( iter ); librdf_free_node( target ); return rval; }
/** * librdf_new_statement_from_nodes: * @world: redland world object * @subject: #librdf_node * @predicate: #librdf_node * @object: #librdf_node * * Constructor - create a new #librdf_statement from existing #librdf_node objects. * * The node objects become owned by the new statement (or freed on error). * * Return value: a new #librdf_statement with copy or NULL on failure **/ librdf_statement* librdf_new_statement_from_nodes(librdf_world *world, librdf_node* subject, librdf_node* predicate, librdf_node* object) { librdf_statement* new_statement; librdf_world_open(world); new_statement = librdf_new_statement(world); if(!new_statement) { if(subject) librdf_free_node(subject); if(predicate) librdf_free_node(predicate); if(object) librdf_free_node(object); return NULL; } new_statement->subject=subject; new_statement->predicate=predicate; new_statement->object=object; return new_statement; }
static int sd_add_dataset_description(librdf_model *sd_model, librdf_node *service_node) { librdf_node *dataset_node = NULL, *default_graph_node = NULL; int triple_count = librdf_storage_size(storage); dataset_node = librdf_new_node(world); if (!dataset_node) { redstore_error("Failed to create default dataset bnode - librdf_new_node returned NULL"); return 1; } librdf_model_add(sd_model, librdf_new_node_from_node(service_node), librdf_new_node_from_uri_local_name(world, sd_ns_uri, (const unsigned char *) "defaultDatasetDescription"), librdf_new_node_from_node(dataset_node) ); librdf_model_add(sd_model, librdf_new_node_from_node(dataset_node), librdf_new_node_from_node(LIBRDF_MS_type(world)), librdf_new_node_from_uri_local_name(world, sd_ns_uri, (const unsigned char *) "Dataset") ); default_graph_node = librdf_new_node(world); if (!default_graph_node) { redstore_error("Failed to create default graph bnode - librdf_new_node returned NULL"); return 1; } librdf_model_add(sd_model, librdf_new_node_from_node(dataset_node), librdf_new_node_from_uri_local_name(world, sd_ns_uri, (const unsigned char *) "defaultGraph"), librdf_new_node_from_node(default_graph_node) ); librdf_model_add(sd_model, librdf_new_node_from_node(default_graph_node), librdf_new_node_from_node(LIBRDF_MS_type(world)), librdf_new_node_from_uri_local_name(world, sd_ns_uri, (const unsigned char *) "Graph") ); if (triple_count>=0) { librdf_model_add(sd_model, librdf_new_node_from_node(default_graph_node), librdf_new_node_from_uri_local_name(world, void_ns_uri, (const unsigned char *) "triples"), new_node_from_integer(world, triple_count) ); } if (dataset_node) librdf_free_node(dataset_node); if (default_graph_node) librdf_free_node(default_graph_node); return 0; }
static void* cassandra_results_stream_get_statement(void* context, int flags) { cassandra_results_stream* scontext; const char* s; size_t s_len; const char* p; size_t p_len; const char* o; size_t o_len; const CassRow* row; scontext = (cassandra_results_stream*)context; switch(flags) { case LIBRDF_ITERATOR_GET_METHOD_GET_OBJECT: row = cass_iterator_get_row(scontext->iter); cass_value_get_string(cass_row_get_column(row, 0), &s, &s_len); cass_value_get_string(cass_row_get_column(row, 1), &p, &p_len); cass_value_get_string(cass_row_get_column(row, 2), &o, &o_len); if (scontext->statement) { librdf_free_statement(scontext->statement); scontext->statement = 0; } librdf_node* sn, * pn, * on; sn = node_constructor_helper(scontext->storage->world, s, s_len); pn = node_constructor_helper(scontext->storage->world, p, p_len); on = node_constructor_helper(scontext->storage->world, o, o_len); if (sn == 0 || pn == 0 || on == 0) { if (sn) librdf_free_node(sn); if (pn) librdf_free_node(pn); if (on) librdf_free_node(on); return 0; } scontext->statement = librdf_new_statement_from_nodes(scontext->storage->world, sn, pn, on); return scontext->statement; case LIBRDF_ITERATOR_GET_METHOD_GET_CONTEXT: return scontext->context; default: librdf_log(scontext->storage->world, 0, LIBRDF_LOG_ERROR, LIBRDF_FROM_STORAGE, NULL, "Unknown iterator method flag %d", flags); return NULL; } }
static librdf_statement* librdf_storage_tstore_statement_from_rs_triple(librdf_world* world, rs_triple *triple) { librdf_node *subject_node; librdf_node *predicate_node; librdf_node *object_node; if(triple->subject) { if(!strncmp(triple->subject, "_:",2)) subject_node=librdf_new_node_from_blank_identifier(world, (const unsigned char *)triple->subject+2); else subject_node=librdf_new_node_from_uri_string(world, (const unsigned char *)triple->subject); if(!subject_node) return NULL; } else subject_node=NULL; if(triple->predicate) { predicate_node=librdf_new_node_from_uri_string(world, (const unsigned char *)triple->predicate); if(!predicate_node) { librdf_free_node(subject_node); return NULL; } } else predicate_node=NULL; if(triple->object) { if(triple->literal) object_node=librdf_new_node_from_typed_literal(world, (const unsigned char *)triple->object, NULL, NULL); else if(!strncmp(triple->object, ":", 2)) object_node=librdf_new_node_from_blank_identifier(world, (const unsigned char *)triple->object+2); else object_node=librdf_new_node_from_uri_string(world, (const unsigned char *)triple->object); if(!object_node) { librdf_free_node(subject_node); librdf_free_node(predicate_node); return NULL; } } else object_node=NULL; return librdf_new_statement_from_nodes(world, subject_node, predicate_node, object_node); }
/** * @brief Executes the given delete query * @attention DELETE-Statement not supported in Redland, so the Query has to be SELECT with < ?s ?p ?o > statements! * These statements are removed out of the storage. * * * @param query The query to execute. */ bool RDF_ExecDiscardQuery(unsigned char* query) { librdf_query* rdfquery; librdf_query_results* results; rdfquery = librdf_new_query(world, "sparql", NULL, query, NULL); results = librdf_model_query_execute(model, rdfquery); librdf_free_query(rdfquery); //Get all statements from the select query and remove them from storage while(!librdf_query_results_finished(results)) { const char **names = NULL; librdf_node* values[10]; if(librdf_query_results_get_bindings(results, &names, values)) { break; } librdf_node* subject_tmp = values[0]; librdf_node* predicate_tmp = values[1]; librdf_node* object_tmp = values[2]; librdf_statement* statement_tmp = librdf_new_statement_from_nodes(world, subject_tmp, predicate_tmp, object_tmp); librdf_storage_remove_statement(storage, statement_tmp); librdf_free_node(subject_tmp); librdf_free_node(predicate_tmp); librdf_free_node(object_tmp); librdf_free_statement(statement_tmp); int i = 0; for (i = 0; i < 10; i++) { librdf_free_node(values[i]); } librdf_query_results_next(results); free(names); } if (results == NULL) { printf("Failed to execute discard query!\n"); } librdf_free_query_results(results); return true; }
/* * 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; }
SLV2Values slv2_query_get_variable_bindings(SLV2World world, SLV2Results results, int variable) { SLV2Values result = NULL; if (!librdf_query_results_finished(results->rdf_results)) result = slv2_values_new(); while (!librdf_query_results_finished(results->rdf_results)) { librdf_node* node = librdf_query_results_get_binding_value(results->rdf_results, variable); if (node == NULL) { fprintf(stderr, "SLV2 ERROR: Variable %d bound to NULL.\n", variable); librdf_query_results_next(results->rdf_results); continue; } SLV2Value val = slv2_value_from_librdf_node(world, node); if (val) raptor_sequence_push(result, val); librdf_free_node(node); librdf_query_results_next(results->rdf_results); } return result; }
/** * librdf_finish_concepts: * @world: redland world object * * INTERNAL - Terminate the concepts module. * **/ void librdf_finish_concepts(librdf_world *world) { int i; /* Free resources and set pointers to NULL so that they are cleared * in case the concepts module is initialised again in the same process. */ if(world->concept_ms_namespace_uri) { librdf_free_uri(world->concept_ms_namespace_uri); world->concept_ms_namespace_uri=NULL; } if(world->concept_schema_namespace_uri) { librdf_free_uri(world->concept_schema_namespace_uri); world->concept_schema_namespace_uri=NULL; } if(world->concept_resources) { for (i=0; i<= LIBRDF_CONCEPT_LAST; i++) { /* deletes associated URI too */ if(world->concept_resources[i]) librdf_free_node(world->concept_resources[i]); } LIBRDF_FREE(ptrarray, world->concept_resources); world->concept_resources=NULL; } if(world->concept_uris) { /* uris were freed above, now just free the array */ LIBRDF_FREE(ptrarray, world->concept_uris); world->concept_uris=NULL; } }
static void cassandra_results_stream_finished(void* context) { cassandra_results_stream* scontext; scontext = (cassandra_results_stream*)context; if (scontext->iter) cass_iterator_free(scontext->iter); if (scontext->result) cass_result_free(scontext->result); if (scontext->stmt) cass_statement_free(scontext->stmt); if(scontext->storage) librdf_storage_remove_reference(scontext->storage); if(scontext->statement) librdf_free_statement(scontext->statement); if(scontext->context) librdf_free_node(scontext->context); LIBRDF_FREE(librdf_storage_cassandra_find_statements_stream_context, scontext); }
static void librdf_storage_trees_graph_free(void* data) { librdf_storage_trees_graph* graph = (librdf_storage_trees_graph*)data; #ifdef RDF_STORAGE_TREES_WITH_CONTEXTS librdf_free_node(graph->context); #endif /* Extra index trees have null deleters (statements are shared) */ if (graph->sop_tree) librdf_free_avltree(graph->sop_tree); if (graph->ops_tree) librdf_free_avltree(graph->ops_tree); if (graph->pso_tree) librdf_free_avltree(graph->pso_tree); /* Free spo tree and statements */ librdf_free_avltree(graph->spo_tree); graph->spo_tree=NULL; graph->sop_tree=NULL; graph->ops_tree=NULL; graph->pso_tree=NULL; LIBRDF_FREE(librdf_storage_trees_graph, graph); }
/** * librdf_statement_clear: * @statement: #librdf_statement object * * Empty a librdf_statement of nodes. * **/ void librdf_statement_clear(librdf_statement *statement) { LIBRDF_ASSERT_OBJECT_POINTER_RETURN(statement, librdf_statement); if(statement->subject) { librdf_free_node(statement->subject); statement->subject=NULL; } if(statement->predicate) { librdf_free_node(statement->predicate); statement->predicate=NULL; } if(statement->object) { librdf_free_node(statement->object); statement->object=NULL; } }
/* * call-seq: * graph.has_predicate_about?( subject, predicate ) -> true or false * * Returns +true+ if the graph has at least one statement with the specified +subject+ and * +predicate+. * */ static VALUE rleaf_redleaf_graph_has_predicate_about_p( VALUE self, VALUE subject, VALUE predicate ) { rleaf_GRAPH *ptr = rleaf_get_graph( self ); librdf_node *source, *arc; VALUE rval = Qfalse; rleaf_log_with_context( self, "debug", "checking for presence of a %s predicate about %s", RSTRING_PTR(rb_inspect( predicate )), RSTRING_PTR(rb_inspect( subject )) ); source = rleaf_value_to_subject_node( subject ); arc = rleaf_value_to_predicate_node( predicate ); if ( librdf_model_has_arc_out(ptr->model, source, arc) != 0 ) rval = Qtrue; librdf_free_node( arc ); librdf_free_node( source ); return rval; }
/* * call-seq: * graph.has_predicate_entailing?( predicate, object ) -> true or false * * Returns +true+ if the graph has at least one statement with the specified +predicate+ * and +object+. * */ static VALUE rleaf_redleaf_graph_has_predicate_entailing_p( VALUE self, VALUE predicate, VALUE object ) { rleaf_GRAPH *ptr = rleaf_get_graph( self ); librdf_node *target, *arc; VALUE rval = Qfalse; rleaf_log_with_context( self, "debug", "checking for presence of a %s predicate entailing %s", RSTRING_PTR(rb_inspect( predicate )), RSTRING_PTR(rb_inspect( object )) ); arc = rleaf_value_to_predicate_node( predicate ); target = rleaf_value_to_object_node( object ); if ( librdf_model_has_arc_in(ptr->model, target, arc) != 0 ) rval = Qtrue; librdf_free_node( target ); librdf_free_node( arc ); return rval; }
/* * call-seq: * graph.subject( predicate, object ) -> nodes * * Return one subject of a statement with the specified +predicate+ and +object+. * */ static VALUE rleaf_redleaf_graph_subject( VALUE self, VALUE predicate, VALUE object ) { rleaf_GRAPH *ptr = rleaf_get_graph( self ); librdf_node *source, *arc, *target; VALUE rval = Qnil; arc = rleaf_value_to_predicate_node( predicate ); target = rleaf_value_to_object_node( object ); source = librdf_model_get_source( ptr->model, arc, target ); librdf_free_node( arc ); librdf_free_node( target ); if ( source ) { rval = rleaf_librdf_node_to_value( source ); librdf_free_node( source ); } return rval; }
/* * 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; }
static int sd_add_query_languages(librdf_model *sd_model, librdf_node *service_node) { int i,n; for(i=0; 1; i++) { const raptor_syntax_description* desc = librdf_query_language_get_description(world, i); librdf_node *lang_node = NULL; if (!desc) break; for (n = 0; desc->names[n]; n++) { if (strcmp(desc->names[n], "sparql10")==0) { lang_node = librdf_new_node_from_uri_local_name(world, sd_ns_uri, (unsigned char *) "SPARQL10Query"); break; } else if (strcmp(desc->names[n], "sparql11-query")==0) { lang_node = librdf_new_node_from_uri_local_name(world, sd_ns_uri, (unsigned char *) "SPARQL11Query"); break; } } if (lang_node) { librdf_model_add(sd_model, librdf_new_node_from_node(service_node), librdf_new_node_from_uri_local_name(world, sd_ns_uri, (const unsigned char *) "supportedLanguage"), librdf_new_node_from_node(lang_node) ); librdf_model_add(sd_model, librdf_new_node_from_node(lang_node), librdf_new_node_from_node(LIBRDF_S_comment(world)), librdf_new_node_from_literal(world, (const unsigned char *) desc->label, NULL, 0) ); if (desc->uri_strings) { for (n = 0; desc->uri_strings[n]; n++) { librdf_model_add(sd_model, librdf_new_node_from_node(lang_node), librdf_new_node_from_node(LIBRDF_S_seeAlso(world)), librdf_new_node_from_uri_string(world, (const unsigned char *) desc->uri_strings[n]) ); } } librdf_free_node(lang_node); } } return 0; }
/** * librdf_finish_concepts - Terminate the librdf_concepts module * @world: redland world object **/ void librdf_finish_concepts(librdf_world *world) { int i; if(librdf_concept_ms_namespace_uri) librdf_free_uri(librdf_concept_ms_namespace_uri); if(librdf_concept_schema_namespace_uri) librdf_free_uri(librdf_concept_schema_namespace_uri); for (i=0; i< LIBRDF_CONCEPT_LAST; i++) /* deleted associated URI too */ librdf_free_node(librdf_concept_resources[i]); }
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); }
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); }
static int validate_webid(request_rec *request, const char *subjAltName, char *pkey_n, unsigned int pkey_e_i) { int r = HTTP_UNAUTHORIZED; librdf_world *rdf_world = NULL; librdf_storage *rdf_storage = NULL; librdf_model *rdf_model = NULL; librdf_query *rdf_query = NULL; librdf_query_results *rdf_query_results = NULL; rdf_world = librdf_new_world(); if (rdf_world != NULL) { librdf_world_open(rdf_world); librdf_world_set_logger(rdf_world, (void *)request, ap_rdf_log_proxy); rdf_storage = librdf_new_storage(rdf_world, "uri", subjAltName, NULL); if (rdf_storage != NULL) { rdf_model = librdf_new_model(rdf_world, rdf_storage, NULL); } else ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, request, "WebID: librdf_new_storage returned NULL"); } if (rdf_model != NULL) { char *c_query = apr_psprintf(request->pool, SPARQL_WEBID, subjAltName); ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, request, "WebID: SPARQL query = %s", c_query); rdf_query = librdf_new_query(rdf_world, "sparql", NULL, (unsigned char*)c_query, NULL); } else { ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, request, "WebID: librdf_new_query returned NULL"); } if (rdf_query != NULL) { rdf_query_results = librdf_query_execute(rdf_query, rdf_model); if (rdf_query_results != NULL) { for (; r != OK && librdf_query_results_finished(rdf_query_results)==0; librdf_query_results_next(rdf_query_results)) { librdf_node *m_node, *e_node; unsigned char *rdf_mod; unsigned char *rdf_exp; if (r != OK && NULL != (m_node = librdf_query_results_get_binding_value_by_name(rdf_query_results, "m")) && NULL != (e_node = librdf_query_results_get_binding_value_by_name(rdf_query_results, "e"))) { if (librdf_node_get_type(m_node) == LIBRDF_NODE_TYPE_LITERAL && librdf_node_get_type(e_node) == LIBRDF_NODE_TYPE_LITERAL) { rdf_mod = librdf_node_get_literal_value(m_node); rdf_exp = librdf_node_get_literal_value(e_node); ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, request, "WebID: modulus = %s", rdf_mod); ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, request, "WebID: exponent = %s", rdf_exp); if (rdf_exp != NULL && apr_strtoi64((char*)rdf_exp, NULL, 10) == pkey_e_i && matches_pkey(rdf_mod, pkey_n)) r = OK; librdf_free_node(m_node); librdf_free_node(e_node); } } } librdf_free_query_results(rdf_query_results); } else ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, request, "WebID: librdf_query_execute returned NULL"); librdf_free_query(rdf_query); } else ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, request, "WebID: librdf_new_query returned NULL"); if (rdf_model) librdf_free_model(rdf_model); if (rdf_storage) librdf_free_storage(rdf_storage); if (rdf_world) librdf_free_world(rdf_world); return r; }
int main(int argc, char *argv[]) { librdf_statement *statement; librdf_stream* stream; const char *program=librdf_basename((const char*)argv[0]); librdf_world *world; librdf_uri* prefix_uri; librdf_node* nodes[STREAM_NODES_COUNT]; int i; librdf_iterator* iterator; int count; world=librdf_new_world(); librdf_world_open(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 < STREAM_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_new_static_node_iterator(world, nodes, STREAM_NODES_COUNT); if(!iterator) { fprintf(stderr, "%s: Failed to create static node iterator\n", program); return(1); } statement=librdf_new_statement_from_nodes(world, librdf_new_node_from_uri_string(world, (const unsigned char*)"http://example.org/resource"), librdf_new_node_from_uri_string(world, (const unsigned char*)"http://example.org/property"), NULL); if(!statement) { fprintf(stderr, "%s: Failed to create statement\n", program); return(1); } fprintf(stdout, "%s: Creating stream from node iterator\n", program); stream=librdf_new_stream_from_node_iterator(iterator, statement, LIBRDF_STATEMENT_OBJECT); if(!stream) { fprintf(stderr, "%s: Failed to createstatic node stream\n", program); return(1); } /* This is to check that the stream_from_node_iterator code * *really* takes a copy of what it needs from statement */ fprintf(stdout, "%s: Freeing statement\n", program); librdf_free_statement(statement); fprintf(stdout, "%s: Listing static node stream\n", program); count=0; while(!librdf_stream_end(stream)) { librdf_statement* s_statement=librdf_stream_get_object(stream); if(!s_statement) { fprintf(stderr, "%s: librdf_stream_current returned NULL when not end of stream\n", program); return(1); } fprintf(stdout, "%s: statement %d is: ", program, count); librdf_statement_print(s_statement, stdout); fputc('\n', stdout); librdf_stream_next(stream); count++; } if(count != STREAM_NODES_COUNT) { fprintf(stderr, "%s: Stream returned %d statements, expected %d\n", program, count, STREAM_NODES_COUNT); return(1); } fprintf(stdout, "%s: stream from node iterator worked ok\n", program); fprintf(stdout, "%s: Freeing stream\n", program); librdf_free_stream(stream); fprintf(stdout, "%s: Freeing nodes\n", program); for (i=0; i<STREAM_NODES_COUNT; i++) { librdf_free_node(nodes[i]); } librdf_free_uri(prefix_uri); librdf_free_world(world); /* keep gcc -Wall happy */ return(0); }
static librdf_model * create_service_description(librdf_storage *sd_storage, const char * request_url) { librdf_model *sd_model = NULL; librdf_node *service_node = NULL; char *comment = NULL; sd_model = librdf_new_model(world, sd_storage, NULL); if (!sd_model) { redstore_error("Failed to create model for service description."); return NULL; } service_node = librdf_new_node(world); if (!service_node) { redstore_error("Failed to create service description bnode - librdf_new_node returned NULL"); librdf_free_model(sd_model); return NULL; } librdf_model_add(sd_model, librdf_new_node_from_node(service_node), librdf_new_node_from_node(LIBRDF_MS_type(world)), librdf_new_node_from_uri_local_name(world, sd_ns_uri, (unsigned char *) "Service") ); sd_add_format_descriptions(sd_model, service_node, librdf_parser_get_description, "inputFormat"); sd_add_format_descriptions(sd_model, service_node, librdf_serializer_get_description, "resultFormat"); sd_add_format_descriptions(sd_model, service_node, librdf_query_results_formats_get_description, "resultFormat"); sd_add_query_languages(sd_model, service_node); sd_add_dataset_description(sd_model, service_node); librdf_model_add(sd_model, librdf_new_node_from_node(service_node), librdf_new_node_from_node(LIBRDF_S_label(world)), librdf_new_node_from_literal(world, (unsigned char *) storage_name, NULL, 0) ); #define COMMENT_MAX_LEN (128) comment = malloc(COMMENT_MAX_LEN); snprintf(comment, COMMENT_MAX_LEN, "RedStore %s endpoint using the '%s' storage module.", PACKAGE_VERSION, storage_type); librdf_model_add(sd_model, librdf_new_node_from_node(service_node), librdf_new_node_from_node(LIBRDF_S_comment(world)), librdf_new_node_from_literal(world, (unsigned char *) comment, NULL, 0) ); free(comment); librdf_model_add(sd_model, librdf_new_node_from_node(service_node), librdf_new_node_from_uri_local_name(world, sd_ns_uri, (unsigned char *) "endpoint"), sd_get_endpoint_node(request_url) ); // Redland's default graph is the union of all other graphs librdf_model_add(sd_model, librdf_new_node_from_node(service_node), librdf_new_node_from_uri_local_name(world, sd_ns_uri, (unsigned char *) "feature"), librdf_new_node_from_uri_local_name(world, sd_ns_uri, (unsigned char *) "UnionDefaultGraph") ); librdf_free_node(service_node); return sd_model; }
void run_query2(librdf_world* world, librdf_model* model, const std::string& q) { librdf_stream* strm; std::cout << "** Query: " << q << std::endl; librdf_uri* uri1 = librdf_new_uri(world, (const unsigned char*) "http://bunchy.org"); if (uri1 == 0) throw std::runtime_error("Couldn't parse URI"); librdf_uri* uri2 = librdf_new_uri(world, (const unsigned char*) "http://bunchy.org"); if (uri2 == 0) throw std::runtime_error("Couldn't parse URI"); librdf_query* qry = librdf_new_query(world, "sparql", uri1, (const unsigned char*) q.c_str(), uri2); librdf_free_uri(uri1); librdf_free_uri(uri2); if (qry == 0) throw std::runtime_error("Couldn't parse query."); librdf_query_results* results = librdf_query_execute(qry, model); if (results == 0) throw std::runtime_error("Couldn't execute query"); librdf_free_query(qry); std::cout << "--------------------------------------------------------" << std::endl; while (true) { if (librdf_query_results_finished(results)) break; int bcount = librdf_query_results_get_bindings_count(results); if (bcount < 0) throw std::runtime_error("Couldn't get query bindings count"); for(int i = 0; i < bcount; i++) { if (i != 0) std::cout << " "; librdf_node* value = librdf_query_results_get_binding_value(results, i); if (value == 0) throw std::runtime_error("Couldn't get results binding"); output_node(value); librdf_free_node(value); } std::cout << std::endl; librdf_query_results_next(results); } std::cout << "--------------------------------------------------------" << std::endl; librdf_free_query_results(results); }
/** * librdf_statement_decode2: * @world: redland world * @statement: the statement to deserialise into * @context_node: pointer to #librdf_node context_node to deserialise into * @buffer: the buffer to use * @length: buffer size * * Decodes a statement + context node from a buffer. * * Decodes the serialised statement (as created by librdf_statement_encode() ) * from the given buffer. If a context node is found and context_node is * not NULL, a pointer to the new #librdf_node is stored in *context_node. * * Return value: number of bytes used or 0 on failure (bad encoding, allocation failure) **/ size_t librdf_statement_decode2(librdf_world* world, librdf_statement* statement, librdf_node** context_node, unsigned char *buffer, size_t length) { unsigned char *p; librdf_node* node; unsigned char type; size_t total_length=0; LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(statement, librdf_statement, 0); #if defined(LIBRDF_DEBUG) && LIBRDF_DEBUG > 1 LIBRDF_DEBUG2("Decoding buffer of %d bytes\n", length); #endif /* absolute minimum - first byte is type */ if(length < 1) return 0; p=buffer; if(*p++ != 'x') return 0; length--; total_length++; while(length>0) { size_t node_len; type=*p++; length--; total_length++; if(!length) return 0; if(!(node=librdf_node_decode(world, &node_len, p, length))) return 0; p += node_len; length -= node_len; total_length += node_len; #if defined(LIBRDF_DEBUG) && LIBRDF_DEBUG > 1 LIBRDF_DEBUG3("Found type %c (%d bytes)\n", type, node_len); #endif switch(type) { case 's': /* subject */ statement->subject=node; break; case 'p': /* predicate */ statement->predicate=node; break; case 'o': /* object */ statement->object=node; break; case 'c': /* context */ if(context_node) *context_node=node; else librdf_free_node(node); break; default: librdf_log(world, 0, LIBRDF_LOG_ERROR, LIBRDF_FROM_STATEMENT, NULL, "Illegal statement encoding '%c' seen", p[-1]); return 0; } } return total_length; }
static int sd_add_format_descriptions(librdf_model *sd_model, librdf_node *service_node, description_proc_t desc_proc, const char *type) { librdf_node *format_node = NULL; unsigned int i,n; for(i=0; 1; i++) { const raptor_syntax_description* desc = NULL; int uri_index = 0; desc = desc_proc(world, i); if (!desc) break; // Hack to remove the 'guess' format from the service description if (strcmp(desc->names[0], "guess") == 0) continue; // If the format has a format URI, use that, otherwise create a bnode if (desc->uri_strings && desc->uri_strings[0] && strncmp("http://www.w3.org/ns/formats/", desc->uri_strings[0], 29) == 0) { format_node = librdf_new_node_from_uri_string(world, (const unsigned char *) desc->uri_strings[0]); uri_index++; } else { format_node = librdf_new_node(world); } if (!format_node) { redstore_error("Failed to create new node for format."); return -1; } librdf_model_add(sd_model, librdf_new_node_from_node(format_node), librdf_new_node_from_node(LIBRDF_MS_type(world)), librdf_new_node_from_uri_local_name(world, format_ns_uri, (const unsigned char *) "Format") ); librdf_model_add(sd_model, librdf_new_node_from_node(service_node), librdf_new_node_from_uri_local_name(world, sd_ns_uri, (const unsigned char *) type), librdf_new_node_from_node(format_node) ); for (n = 0; desc->names[n]; n++) { librdf_model_add(sd_model, librdf_new_node_from_node(format_node), librdf_new_node_from_node(LIBRDF_S_label(world)), librdf_new_node_from_literal(world, (const unsigned char *) desc->names[n], NULL, 0) ); } if (desc->label) { librdf_model_add(sd_model, librdf_new_node_from_node(format_node), librdf_new_node_from_node(LIBRDF_S_comment(world)), librdf_new_node_from_literal(world, (const unsigned char *) desc->label, NULL, 0) ); } for (n = 0; n < desc->mime_types_count; n++) { const raptor_type_q mime_type = desc->mime_types[n]; librdf_model_add(sd_model, librdf_new_node_from_node(format_node), librdf_new_node_from_uri_local_name(world, format_ns_uri, (const unsigned char *) "media_type"), librdf_new_node_from_literal(world, (unsigned char *) mime_type.mime_type, NULL, 0) ); } if (desc->uri_strings) { int firstUri = uri_index; for (; desc->uri_strings[uri_index]; uri_index++) { const unsigned char *uri_string = (const unsigned char *) desc->uri_strings[uri_index]; if (firstUri == uri_index) { librdf_model_add(sd_model, librdf_new_node_from_node(format_node), librdf_new_node_from_node(LIBRDF_S_isDefinedBy(world)), librdf_new_node_from_uri_string(world, uri_string) ); } else { librdf_model_add(sd_model, librdf_new_node_from_node(format_node), librdf_new_node_from_node(LIBRDF_S_seeAlso(world)), librdf_new_node_from_uri_string(world, uri_string) ); } } } librdf_free_node(format_node); } return 0; }
int print_query_results(librdf_world* world, librdf_model* model, librdf_query_results *results) { int i; char *name; librdf_stream* stream; librdf_serializer* serializer; const char *query_graph_serializer_syntax_name="rdfxml"; if(librdf_query_results_is_bindings(results)) { fprintf(stdout, ": Query returned bindings results:\n"); while(!librdf_query_results_finished(results)) { fputs("result: [", stdout); for(i=0; i<librdf_query_results_get_bindings_count(results); i++) { librdf_node *value=librdf_query_results_get_binding_value(results, i); name=(char*)librdf_query_results_get_binding_name(results, i); if(i>0) fputs(", ", stdout); fprintf(stdout, "%s=", name); if(value) { librdf_node_print(value, stdout); librdf_free_node(value); } else fputs("NULL", stdout); } fputs("]\n", stdout); librdf_query_results_next(results); } fprintf(stdout, ": Query returned %d results\n", librdf_query_results_get_count(results)); } else if(librdf_query_results_is_boolean(results)) { fprintf(stdout, ": Query returned boolean result: %s\n", librdf_query_results_get_boolean(results) ? "true" : "false"); } else if(librdf_query_results_is_graph(results)) { librdf_storage* tmp_storage; librdf_model* tmp_model; tmp_storage=librdf_new_storage(world, NULL, NULL, NULL); tmp_model=librdf_new_model(world, tmp_storage, NULL); fprintf(stdout, ": Query returned graph result:\n"); stream=librdf_query_results_as_stream(results); if(!stream) { fprintf(stderr, ": Failed to get query results graph\n"); return -1; } librdf_model_add_statements(tmp_model, stream); librdf_free_stream(stream); fprintf(stdout, ": Total %d triples\n", librdf_model_size(model)); serializer=librdf_new_serializer(world, query_graph_serializer_syntax_name, NULL, NULL); if(!serializer) { fprintf(stderr, ": Failed to create serializer type %s\n", query_graph_serializer_syntax_name); return -1; } librdf_serializer_serialize_model_to_file_handle(serializer, stdout, NULL, tmp_model); librdf_free_serializer(serializer); librdf_free_model(tmp_model); librdf_free_storage(tmp_storage); } else { fprintf(stdout, ": Query returned unknown result format\n"); return -1; } return 0; }
/* * librdf_parser_raptor_new_statement_handler - helper callback function for raptor RDF when a new triple is asserted * @context: context for callback * @statement: raptor_statement * * Adds the statement to the list of statements. */ static void librdf_parser_raptor_new_statement_handler(void *context, raptor_statement *rstatement) { librdf_parser_raptor_stream_context* scontext=(librdf_parser_raptor_stream_context*)context; librdf_node* node; librdf_statement* statement; librdf_world* world=scontext->pcontext->parser->world; int rc; statement=librdf_new_statement(world); if(!statement) return; if(rstatement->subject->type == RAPTOR_TERM_TYPE_BLANK) { node = librdf_new_node_from_blank_identifier(world, (const unsigned char*)rstatement->subject->value.blank.string); } else if (rstatement->subject->type == RAPTOR_TERM_TYPE_URI) { node = librdf_new_node_from_uri(world, (librdf_uri*)rstatement->subject->value.uri); } else { librdf_log(world, 0, LIBRDF_LOG_ERROR, LIBRDF_FROM_PARSER, NULL, "Unknown Raptor subject identifier type %d", rstatement->subject->type); librdf_free_statement(statement); return; } if(!node) { librdf_log(world, 0, LIBRDF_LOG_FATAL, LIBRDF_FROM_PARSER, NULL, "Cannot create subject node"); librdf_free_statement(statement); return; } librdf_statement_set_subject(statement, node); if(rstatement->predicate->type == RAPTOR_TERM_TYPE_URI) { node = librdf_new_node_from_uri(world, (librdf_uri*)rstatement->predicate->value.uri); } else { librdf_log(world, 0, LIBRDF_LOG_ERROR, LIBRDF_FROM_PARSER, NULL, "Unknown Raptor predicate identifier type %d", rstatement->predicate->type); librdf_free_statement(statement); return; } if(!node) { librdf_log(world, 0, LIBRDF_LOG_FATAL, LIBRDF_FROM_PARSER, NULL, "Cannot create predicate node"); librdf_free_statement(statement); return; } librdf_statement_set_predicate(statement, node); if(rstatement->object->type == RAPTOR_TERM_TYPE_LITERAL) { node = librdf_new_node_from_typed_literal(world, rstatement->object->value.literal.string, (const char *)rstatement->object->value.literal.language, (librdf_uri*)rstatement->object->value.literal.datatype); } else if(rstatement->object->type == RAPTOR_TERM_TYPE_BLANK) { node = librdf_new_node_from_blank_identifier(world, rstatement->object->value.blank.string); } else if(rstatement->object->type == RAPTOR_TERM_TYPE_URI) { node = librdf_new_node_from_uri(world, (librdf_uri*)rstatement->object->value.uri); } else { librdf_log(world, 0, LIBRDF_LOG_ERROR, LIBRDF_FROM_PARSER, NULL, "Unknown Raptor object identifier type %d", rstatement->object->type); librdf_free_statement(statement); return; } if(!node) { librdf_log(world, 0, LIBRDF_LOG_FATAL, LIBRDF_FROM_PARSER, NULL, "Cannot create object node"); librdf_free_statement(statement); return; } librdf_statement_set_object(statement, node); #if defined(LIBRDF_DEBUG) && LIBRDF_DEBUG > 1 if(1) { raptor_iostream *iostr; iostr = raptor_new_iostream_to_file_handle(world->raptor_world_ptr, stderr); librdf_statement_write(statement, iostr); raptor_free_iostream(iostr); } #endif if(scontext->model) { if(librdf_model_supports_contexts(scontext->model) && rstatement->graph && (rstatement->graph->type == RAPTOR_TERM_TYPE_URI || rstatement->graph->type == RAPTOR_TERM_TYPE_BLANK)) { node = librdf_new_node_from_uri(world, (librdf_uri*)rstatement->graph->value.uri); rc = librdf_model_context_add_statement(scontext->model, node, statement); librdf_free_node(node); } else { rc = librdf_model_add_statement(scontext->model, statement); } librdf_free_statement(statement); } else { rc=librdf_list_add(scontext->statements, statement); if(rc) librdf_free_statement(statement); } if(rc) { librdf_log(world, 0, LIBRDF_LOG_FATAL, LIBRDF_FROM_PARSER, NULL, "Cannot add statement to model"); } }