int add_triple_typed(librdf_world *world, librdf_node *context, librdf_model* model, const char *s, const char *p, const char *o) { librdf_node *subject, *predicate, *object; librdf_statement* statement=NULL; int rc; if(librdf_heuristic_is_blank_node(s)) subject=librdf_new_node_from_blank_identifier(world, (const unsigned char *)librdf_heuristic_get_blank_node(s)); else subject=librdf_new_node_from_uri_string(world, (const unsigned char *)s); predicate=librdf_new_node_from_uri_string(world, (const unsigned char *)p); if(librdf_heuristic_is_blank_node(o)) object=librdf_new_node_from_blank_identifier(world, (const unsigned char *)librdf_heuristic_get_blank_node(o)); else object=librdf_new_node_from_literal(world, (const unsigned char *)o, NULL, 0); statement=librdf_new_statement(world); librdf_statement_set_subject(statement, subject); librdf_statement_set_predicate(statement, predicate); librdf_statement_set_object(statement, object); rc=librdf_model_context_add_statement(model, context, statement); librdf_free_statement(statement); return rc; }
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); }
int main(int argc, char *argv[]) { librdf_world* world; librdf_storage *storage; librdf_model* model; librdf_statement* statement; raptor_world *raptor_world_ptr; raptor_iostream* iostr; world=librdf_new_world(); librdf_world_open(world); raptor_world_ptr = librdf_world_get_raptor(world); model=librdf_new_model(world, storage=librdf_new_storage(world, "hashes", "test", "hash-type='bdb',dir='.'"), NULL); librdf_model_add_statement(model, statement=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/creator"), librdf_new_node_from_literal(world, (const unsigned char*)"Dave Beckett", NULL, 0) ) ); librdf_free_statement(statement); iostr = raptor_new_iostream_to_file_handle(raptor_world_ptr, stdout); librdf_model_write(model, iostr); raptor_free_iostream(iostr); 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); }
bool RdfNode::dummy() { librdf_world* world; librdf_storage *storage; librdf_model* model; librdf_statement* statement; raptor_world *raptor_world_ptr; raptor_iostream* iostr; world=librdf_new_world(); librdf_world_open(world); raptor_world_ptr = librdf_world_get_raptor(world); model=librdf_new_model(world, storage=librdf_new_storage(world, "hashes", nullptr, "hash-type='memory'"), nullptr); librdf_model_add_statement(model, statement=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/creator"), librdf_new_node_from_literal(world, (const unsigned char*)"Dave Beckett", nullptr, 0) ) ); librdf_free_statement(statement); iostr = raptor_new_iostream_to_file_handle(raptor_world_ptr, stdout); librdf_model_write(model, iostr); raptor_free_iostream(iostr); librdf_free_model(model); librdf_free_storage(storage); librdf_free_world(world); #ifdef LIBRDF_MEMORY_DEBUG librdf_memory_report(stderr); #endif return true; }
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; }
/* * Check the given Ruby VALUE for suitability as the predicate of a triple and return a * librdf_node pointer to an equivalent node if it is suitable. Raises a * ArgumentError if it is not. * * The caller is responsible for catching the ArgumentError that results if the * +predicate+ is invalid. */ librdf_node * rleaf_value_to_predicate_node( VALUE predicate ) { librdf_node *node = NULL; if ( predicate == Qnil || IsURI(predicate) || IsNamespace(predicate) ) { node = rleaf_value_to_librdf_node( predicate ); } else if ( TYPE(predicate) == T_STRING ) { node = librdf_new_node_from_uri_string( rleaf_rdf_world, (unsigned char *)RSTRING_PTR(predicate) ); } else { rb_raise( rb_eArgError, "Predicate must be a URI" ); } return node; }
/* * Check the given Ruby VALUE for suitability as the subject of a triple and return a * librdf_node pointer to an equivalent node if it is suitable. Raises a * ArgumentError if it is not. * * The caller is responsible for catching the ArgumentError that results if the * +subject+ is invalid. */ librdf_node * rleaf_value_to_subject_node( VALUE subject ) { librdf_node *node = NULL; if ( subject == Qnil || TYPE(subject) == T_SYMBOL || IsURI(subject) || IsNamespace(subject) ) { node = rleaf_value_to_librdf_node( subject ); } else if ( TYPE(subject) == T_STRING ) { node = librdf_new_node_from_uri_string( rleaf_rdf_world, (unsigned char *)RSTRING_PTR(subject) ); } else { rb_raise( rb_eArgError, "Subject must be blank or a URI" ); } return node; }
int main(int argc, char *argv[]) { librdf_statement *statement, *statement2; int size, size2; const char *program=librdf_basename((const char*)argv[0]); char *s, *buffer; librdf_world *world; raptor_iostream *iostr; world=librdf_new_world(); librdf_world_open(world); iostr = raptor_new_iostream_to_file_handle(world->raptor_world_ptr, stdout); fprintf(stdout, "%s: Creating statement\n", program); statement=librdf_new_statement(world); fprintf(stdout, "%s: Empty statement: ", program); librdf_statement_write(statement, iostr); fputs("\n", stdout); librdf_statement_set_subject(statement, librdf_new_node_from_uri_string(world, (const unsigned char*)"http://purl.org/net/dajobe/")); librdf_statement_set_predicate(statement, librdf_new_node_from_uri_string(world, (const unsigned char*)"http://purl.org/dc/elements/1.1/#Creator")); librdf_statement_set_object(statement, librdf_new_node_from_literal(world, (const unsigned char*)"Dave Beckett", NULL, 0)); fprintf(stdout, "%s: Resulting statement: ", program); librdf_statement_write(statement, iostr); fputs("\n", stdout); size = librdf_statement_encode2(world, statement, NULL, 0); fprintf(stdout, "%s: Encoding statement requires %d bytes\n", program, size); buffer=(char*)LIBRDF_MALLOC(cstring, size); fprintf(stdout, "%s: Encoding statement in buffer\n", program); size2 = librdf_statement_encode2(world, statement, (unsigned char*)buffer, size); if(size2 != size) { fprintf(stdout, "%s: Encoding statement used %d bytes, expected it to use %d\n", program, size2, size); return(1); } fprintf(stdout, "%s: Creating new statement\n", program); statement2=librdf_new_statement(world); fprintf(stdout, "%s: Decoding statement from buffer\n", program); if(!librdf_statement_decode2(world, statement2, NULL, (unsigned char*)buffer, size)) { fprintf(stdout, "%s: Decoding statement failed\n", program); return(1); } LIBRDF_FREE(cstring, buffer); fprintf(stdout, "%s: New statement is: ", program); librdf_statement_write(statement, iostr); fputs("\n", stdout); fprintf(stdout, "%s: Freeing statements\n", program); librdf_free_statement(statement2); librdf_free_statement(statement); librdf_free_world(world); /* keep gcc -Wall happy */ return(0); }
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 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; }
static int librdf_query_rasqal_query_results_update_statement(void* context) { librdf_query_rasqal_stream_context* scontext=(librdf_query_rasqal_stream_context*)context; librdf_world* world=scontext->query->world; librdf_node* node; raptor_statement *rstatement=rasqal_query_results_get_triple(scontext->qcontext->results); if(!rstatement) return 1; scontext->statement=librdf_new_statement(world); if(!scontext->statement) return 1; /* subject */ if(rstatement->subject->type == RAPTOR_TERM_TYPE_BLANK) { node = librdf_new_node_from_blank_identifier(world, rstatement->subject->value.blank.string); } else if(rstatement->subject->type == RAPTOR_TERM_TYPE_URI) { node = librdf_new_node_from_uri_string(world, librdf_uri_as_string((librdf_uri*)rstatement->subject->value.uri)); } else { librdf_log(world, 0, LIBRDF_LOG_ERROR, LIBRDF_FROM_QUERY, NULL, "Unknown Raptor subject identifier type %d", rstatement->subject->type); goto fail; } if(!node) { librdf_log(world, 0, LIBRDF_LOG_ERROR, LIBRDF_FROM_QUERY, NULL, "Could not create subject node"); goto fail; } librdf_statement_set_subject(scontext->statement, node); /* predicate */ if(rstatement->predicate->type == RAPTOR_TERM_TYPE_URI) { node = librdf_new_node_from_uri_string(world, librdf_uri_as_string((librdf_uri*)rstatement->predicate->value.uri)); } else { librdf_log(world, 0, LIBRDF_LOG_ERROR, LIBRDF_FROM_QUERY, NULL, "Unknown Raptor predicate identifier type %d", rstatement->predicate->type); goto fail; } if(!node) { librdf_log(world, 0, LIBRDF_LOG_ERROR, LIBRDF_FROM_QUERY, NULL, "Could not create predicate node"); goto fail; } librdf_statement_set_predicate(scontext->statement, node); /* object */ 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_string(world, librdf_uri_as_string((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); goto fail; } if(!node) { librdf_log(world, 0, LIBRDF_LOG_ERROR, LIBRDF_FROM_QUERY, NULL, "Could not create object node"); goto fail; } librdf_statement_set_object(scontext->statement, node); return 0; /* success */ fail: librdf_free_statement(scontext->statement); scontext->statement=NULL; return 1; }
void reasoning(sib_data_structure* param, ssTriple_t* t, gboolean* enable_real_reasoning) { librdf_statement* statement; //Insert object as class if(strcmp(t->predicate,rdf_ex "type")==0 && strcmp(t->object,rdfs_ex "Class")!=0) { statement=librdf_new_statement(param->RDF_world); librdf_statement_set_subject(statement,librdf_new_node_from_uri_string(param->RDF_world, t->object)); librdf_statement_set_predicate(statement, librdf_new_node_from_uri_string(param->RDF_world, rdf_ex "type")); librdf_statement_set_object(statement, librdf_new_node_from_uri_string(param->RDF_world, rdfs_ex "Class")); librdf_model_add_statement(param->RDF_model, statement); //FOR SUBSCRIBE statement_in_tl(&(param->RDF_list_insert),statement); //librdf_model_add_statement(param->RDF_model_insert, statement); librdf_free_statement(statement); if (enable_real_reasoning) ////////////////////////////////////////////// { check_subtype_and_add(param, t->subject,t->object); } /////////////////////////////////////////////// } //Insert subject and object as class else if(strcmp(t->predicate,rdfs_ex "subClassOf")==0) { statement=librdf_new_statement(param->RDF_world); librdf_statement_set_subject(statement,librdf_new_node_from_uri_string(param->RDF_world, t->subject)); librdf_statement_set_predicate(statement, librdf_new_node_from_uri_string(param->RDF_world, rdf_ex "type")); librdf_statement_set_object(statement, librdf_new_node_from_uri_string(param->RDF_world, rdfs_ex "Class")); librdf_model_add_statement(param->RDF_model, statement); //FOR SUBSCRIBE statement_in_tl(&(param->RDF_list_insert),statement); //librdf_model_add_statement(param->RDF_model_insert, statement); librdf_free_statement(statement); statement=librdf_new_statement(param->RDF_world); librdf_statement_set_subject(statement,librdf_new_node_from_uri_string(param->RDF_world, t->object)); librdf_statement_set_predicate(statement, librdf_new_node_from_uri_string(param->RDF_world, rdf_ex "type")); librdf_statement_set_object(statement, librdf_new_node_from_uri_string(param->RDF_world, rdfs_ex "Class")); librdf_model_add_statement(param->RDF_model, statement); //FOR SUBSCRIBE statement_in_tl(&(param->RDF_list_insert),statement); //librdf_model_add_statement(param->RDF_model_insert, statement); librdf_free_statement(statement); ////////////////////////////////////////////// if (enable_real_reasoning) { add_sub_type(param, t->subject,t->object); } /////////////////////////////////////////////// } //Insert subject and object as property else if(strcmp(t->predicate,rdfs_ex "subPropertyOf")==0) { statement=librdf_new_statement(param->RDF_world); librdf_statement_set_subject(statement,librdf_new_node_from_uri_string(param->RDF_world, t->subject)); librdf_statement_set_predicate(statement, librdf_new_node_from_uri_string(param->RDF_world, rdf_ex "type")); librdf_statement_set_object(statement, librdf_new_node_from_uri_string(param->RDF_world, rdf_ex "Property")); librdf_model_add_statement(param->RDF_model, statement); //FOR SUBSCRIBE statement_in_tl(&(param->RDF_list_insert),statement); //librdf_model_add_statement(param->RDF_model_insert, statement); librdf_free_statement(statement); statement=librdf_new_statement(param->RDF_world); librdf_statement_set_subject(statement,librdf_new_node_from_uri_string(param->RDF_world, t->object)); librdf_statement_set_predicate(statement, librdf_new_node_from_uri_string(param->RDF_world, rdf_ex "type")); librdf_statement_set_object(statement, librdf_new_node_from_uri_string(param->RDF_world, rdf_ex "Property")); librdf_model_add_statement(param->RDF_model, statement); //FOR SUBSCRIBE statement_in_tl(&(param->RDF_list_insert),statement); //librdf_model_add_statement(param->RDF_model_insert, statement); librdf_free_statement(statement); //////////////////////////////// if (enable_real_reasoning) { add_sub_properties(param, t->subject, t->object); } //////////////////////////////// } //Insert subject and object as property and class(domain and range) else if(strcmp(t->predicate,rdfs_ex "domain")==0 ) { statement=librdf_new_statement(param->RDF_world); librdf_statement_set_subject(statement,librdf_new_node_from_uri_string(param->RDF_world, t->subject)); librdf_statement_set_predicate(statement, librdf_new_node_from_uri_string(param->RDF_world, rdf_ex "type")); librdf_statement_set_object(statement, librdf_new_node_from_uri_string(param->RDF_world, rdf_ex "Property")); librdf_model_add_statement(param->RDF_model, statement); //FOR SUBSCRIBE statement_in_tl(&(param->RDF_list_insert),statement); //librdf_model_add_statement(param->RDF_model_insert, statement); librdf_free_statement(statement); statement=librdf_new_statement(param->RDF_world); librdf_statement_set_subject(statement,librdf_new_node_from_uri_string(param->RDF_world, t->object)); librdf_statement_set_predicate(statement, librdf_new_node_from_uri_string(param->RDF_world, rdf_ex "type")); librdf_statement_set_object(statement, librdf_new_node_from_uri_string(param->RDF_world, rdfs_ex "Class")); librdf_model_add_statement(param->RDF_model, statement); //FOR SUBSCRIBE statement_in_tl(&(param->RDF_list_insert),statement); //librdf_model_add_statement(param->RDF_model_insert, statement); librdf_free_statement(statement); ////////////////////////////////////////////////////////////////////////////////////// if (enable_real_reasoning) { add_properties_domain(param, t->subject, t->object); } ////////////////////////////////////////////////////////////////////////////////////// } else if(strcmp(t->predicate,rdfs_ex "range")==0) { statement=librdf_new_statement(param->RDF_world); librdf_statement_set_subject(statement,librdf_new_node_from_uri_string(param->RDF_world, t->subject)); librdf_statement_set_predicate(statement, librdf_new_node_from_uri_string(param->RDF_world, rdf_ex "type")); librdf_statement_set_object(statement, librdf_new_node_from_uri_string(param->RDF_world, rdf_ex "Property")); librdf_model_add_statement(param->RDF_model, statement); //FOR SUBSCRIBE statement_in_tl(&(param->RDF_list_insert),statement); //librdf_model_add_statement(param->RDF_model_insert, statement); librdf_free_statement(statement); statement=librdf_new_statement(param->RDF_world); librdf_statement_set_subject(statement,librdf_new_node_from_uri_string(param->RDF_world, t->object)); librdf_statement_set_predicate(statement, librdf_new_node_from_uri_string(param->RDF_world, rdf_ex "type")); librdf_statement_set_object(statement, librdf_new_node_from_uri_string(param->RDF_world, rdfs_ex "Class")); librdf_model_add_statement(param->RDF_model, statement); //FOR SUBSCRIBE statement_in_tl(&(param->RDF_list_insert),statement); //librdf_model_add_statement(param->RDF_model_insert, statement); librdf_free_statement(statement); ////////////////////////////////////////////////////////////////////////////////////// if (enable_real_reasoning) { add_properties_range(param, t->subject, t->object); } ////////////////////////////////////////////////////////////////////////////////////// } // All other cases! // Insert predicate as property (eventually check for subproperties) else if(strcmp(t->predicate,rdf_ex "type")!=0) { statement=librdf_new_statement(param->RDF_world); librdf_statement_set_subject(statement, librdf_new_node_from_uri_string(param->RDF_world, t->predicate)); librdf_statement_set_predicate(statement, librdf_new_node_from_uri_string(param->RDF_world, rdf_ex "type")); librdf_statement_set_object(statement, librdf_new_node_from_uri_string(param->RDF_world, rdf_ex "Property")); librdf_model_add_statement(param->RDF_model, statement); //FOR SUBSCRIBE statement_in_tl(&(param->RDF_list_insert),statement); //librdf_model_add_statement(param->RDF_model_insert, statement); librdf_free_statement(statement); ////////////////////////////////////////////// if (enable_real_reasoning) { check_if_predicate_subproperty_and_add(param,t->subject,t->predicate,t->object); check_if_property_in_domain_and_add_subject_type(param, t->subject, t->predicate); check_if_property_in_range_and_add_object_type(param, t->object, t->predicate); } ////////////////////////////////////////////// } }
int main(int argc, char *argv[]) { const char *program=librdf_basename((const char*)argv[0]); const char *test_serializer_types[]={"rdfxml", "ntriples", NULL}; int i; const char *type; unsigned char *string; size_t string_length; librdf_world *world; librdf_storage *storage; librdf_model* model; librdf_uri* base_uri; librdf_statement* statement; librdf_serializer* serializer; librdf_parser* parser; librdf_stream* stream; FILE *fh; struct stat st_buf; world=librdf_new_world(); librdf_world_open(world); librdf_world_set_logger(world, &LogData, log_handler); for(i=0; (type=test_serializer_types[i]); i++) { fprintf(stderr, "%s: Trying to create new %s serializer\n", program, type); serializer=librdf_new_serializer(world, type, NULL, NULL); if(!serializer) { fprintf(stderr, "%s: Failed to create new serializer type %s\n", program, type); continue; } fprintf(stderr, "%s: Freeing serializer\n", program); librdf_free_serializer(serializer); } storage=librdf_new_storage(world, NULL, NULL, NULL); model=librdf_new_model(world, storage, NULL); /* ERROR: Subject URI is bad UTF-8 */ statement=librdf_new_statement_from_nodes(world, librdf_new_node_from_uri_string(world, (const unsigned char*)"http://example.org/foo\xfc"), librdf_new_node_from_uri_string(world, (const unsigned char*)"http://example.org/bar"), librdf_new_node_from_literal(world, (const unsigned char*)"blah", NULL, 0)); librdf_model_add_statement(model, statement); librdf_free_statement(statement); /* ERROR: Predicate URI is not serializable */ statement=librdf_new_statement_from_nodes(world, librdf_new_node_from_uri_string(world, (const unsigned char*)"http://example.org/foo"), librdf_new_node_from_uri_string(world, (const unsigned char*)"http://bad.example.org/"), librdf_new_node_from_literal(world, (const unsigned char*)"blah", NULL, 0)); librdf_model_add_statement(model, statement); librdf_free_statement(statement); /* ERROR: Object literal is bad UTF-8 */ statement=librdf_new_statement_from_nodes(world, librdf_new_node_from_uri_string(world, (const unsigned char*)"http://example.org/foo"), librdf_new_node_from_uri_string(world, (const unsigned char*)"http://example.org/abc"), librdf_new_node_from_literal(world, (const unsigned char*)"\xfc", NULL, 0)); librdf_model_add_statement(model, statement); librdf_free_statement(statement); serializer=librdf_new_serializer(world, "rdfxml", NULL, NULL); base_uri=librdf_new_uri(world, (const unsigned char*)"http://example.org/base#"); string=librdf_serializer_serialize_model_to_counted_string(serializer, base_uri, model, &string_length); #define EXPECTED_BAD_STRING_LENGTH 382 if(string_length != EXPECTED_BAD_STRING_LENGTH) { fprintf(stderr, "%s: Serialising model to RDF/XML returned string '%s' size %d, expected %d\n", program, string, (int)string_length, EXPECTED_BAD_STRING_LENGTH); return 1; } if(string) free(string); librdf_free_uri(base_uri); base_uri=NULL; librdf_free_model(model); model=NULL; librdf_free_storage(storage); storage=NULL; if(LogData.errors != EXPECTED_ERRORS) { fprintf(stderr, "%s: Serialising to RDF/XML returned %d errors, expected %d\n", program, LogData.errors, EXPECTED_ERRORS); return 1; } if(LogData.warnings != EXPECTED_WARNINGS) { fprintf(stderr, "%s: Serialising to RDF/XML returned %d warnings, expected %d\n", program, LogData.warnings, EXPECTED_WARNINGS); return 1; } /* Good model to serialize */ storage=librdf_new_storage(world, NULL, NULL, NULL); model=librdf_new_model(world, storage, NULL); parser=librdf_new_parser(world, SYNTAX_TYPE, NULL, NULL); if(!parser) { fprintf(stderr, "%s: Failed to create new parser type %s\n", program, SYNTAX_TYPE); return 1; } fprintf(stderr, "%s: Adding %s string content\n", program, SYNTAX_TYPE); if(librdf_parser_parse_string_into_model(parser, (const unsigned char*)SYNTAX_CONTENT, NULL /* no base URI*/, model)) { fprintf(stderr, "%s: Failed to parse RDF from %s string into model\n", SYNTAX_TYPE, program); return 1; } librdf_free_parser(parser); fprintf(stderr, "%s: Serializing stream to a string\n", program); stream=librdf_model_as_stream(model); string_length=0; string=librdf_serializer_serialize_stream_to_counted_string(serializer, NULL, stream, &string_length); #define EXPECTED_GOOD_STRING_LENGTH 668 if(string_length != EXPECTED_GOOD_STRING_LENGTH) { fprintf(stderr, "%s: Serialising stream to RDF/XML returned string '%s' size %d, expected %d\n", program, string, (int)string_length, EXPECTED_GOOD_STRING_LENGTH); return 1; } librdf_free_stream(stream); if(string) free(string); fprintf(stderr, "%s: Serializing stream to a file handle\n", program); stream=librdf_model_as_stream(model); #define FILENAME "test.rdf" fh=fopen(FILENAME, "w"); if(!fh) { fprintf(stderr, "%s: Failed to fopen for writing '%s' - %s\n", program, FILENAME, strerror(errno)); return 1; } librdf_serializer_serialize_stream_to_file_handle(serializer, fh, NULL, stream); fclose(fh); stat(FILENAME, &st_buf); if((int)st_buf.st_size != EXPECTED_GOOD_STRING_LENGTH) { fprintf(stderr, "%s: Serialising stream to file handle returned file '%s' of size %d bytes, expected %d\n", program, FILENAME, (int)st_buf.st_size, EXPECTED_GOOD_STRING_LENGTH); return 1; } unlink(FILENAME); librdf_free_stream(stream); librdf_free_serializer(serializer); serializer=NULL; librdf_free_model(model); model=NULL; librdf_free_storage(storage); storage=NULL; librdf_free_world(world); /* keep gcc -Wall happy */ return(0); }
int main(int argc, char** argv) { try { /*********************************************************************/ /* Initialise */ /*********************************************************************/ std::cout << "** Initialise" << std::endl; librdf_world* world = librdf_new_world(); if (world == 0) throw std::runtime_error("Didn't get world"); librdf_storage* storage = librdf_new_storage(world, STORE, STORE_NAME, "new='yes'"); if (storage == 0) throw std::runtime_error("Didn't get storage"); librdf_model* model = librdf_new_model(world, storage, 0); if (model == 0) throw std::runtime_error("Couldn't construct model"); /*********************************************************************/ /* Create in-memory model */ /*********************************************************************/ std::cout << "** Create in-memory model" << std::endl; librdf_storage* mstorage = librdf_new_storage(world, "memory", 0, 0); if (storage == 0) throw std::runtime_error("Didn't get storage"); librdf_model* mmodel = librdf_new_model(world, mstorage, 0); if (model == 0) throw std::runtime_error("Couldn't construct model"); for(int i = 0; i < 10; i++) { char sbuf[256]; char obuf[256]; sprintf(sbuf, "http://gaffer.test/number#%d", i); sprintf(obuf, "http://gaffer.test/number#%d", i+1); librdf_node *s = librdf_new_node_from_uri_string(world, (const unsigned char *) sbuf); librdf_node *p = librdf_new_node_from_uri_string(world, (const unsigned char *) "http://gaffer.test/number#is_before"); librdf_node *o = librdf_new_node_from_uri_string(world, (const unsigned char*) obuf); librdf_statement* st = librdf_new_statement_from_nodes(world, s, p, o); librdf_model_add_statement(mmodel, st); librdf_free_statement(st); } /*********************************************************************/ /* Size */ /*********************************************************************/ std::cout << "** Model size is " << librdf_model_size(model) << std::endl; /*********************************************************************/ /* Add statement */ /*********************************************************************/ std::cout << "** Add statement" << std::endl; const char* fred = "http://gaffer.test/#fred"; const char* is_a = "http://gaffer.test/#is_a"; const char* cat = "http://gaffer.test/#cat"; librdf_node *s = librdf_new_node_from_uri_string(world, (const unsigned char *) fred); librdf_node *p = librdf_new_node_from_uri_string(world, (const unsigned char *) is_a); librdf_node *o = librdf_new_node_from_uri_string(world, (const unsigned char *) cat); librdf_statement* st = librdf_new_statement_from_nodes(world, s, p, o); librdf_model_add_statement(model, st); /*********************************************************************/ /* Add memory model statements */ /*********************************************************************/ std::cout << "** Add statements" << std::endl; librdf_stream* strm = librdf_model_as_stream(mmodel); if (strm == 0) throw std::runtime_error("Couldn't get memory stream"); int ret = librdf_model_add_statements(model, strm); if (ret != 0) throw std::runtime_error("Couldn't add_statements"); librdf_free_stream(strm); librdf_free_model(mmodel); librdf_free_storage(mstorage); /*********************************************************************/ /* Run queries */ /*********************************************************************/ run_query(world, model, query_string8); run_query(world, model, query_string1); run_query(world, model, query_string2); run_query(world, model, query_string3); run_query(world, model, query_string4); run_query(world, model, query_string5); run_query(world, model, query_string6); run_query2(world, model, query_string7); /*********************************************************************/ /* Remove statement */ /*********************************************************************/ std::cout << "** Remove statements" << std::endl; librdf_model_remove_statement(model, st); /*********************************************************************/ /* Serialise */ /*********************************************************************/ std::cout << "** Serialise" << std::endl; strm = librdf_model_as_stream(model); if (strm == 0) throw std::runtime_error("Couldn't get model as stream"); librdf_serializer* srl = librdf_new_serializer(world, "ntriples", 0, 0); if (srl == 0) throw std::runtime_error("Couldn't create serialiser"); size_t len; unsigned char* out = librdf_serializer_serialize_stream_to_counted_string(srl, 0, strm, &len); std::cout << "--------------------------------------------------------" << std::endl; write(1, out, len); std::cout << "--------------------------------------------------------" << std::endl; free(out); librdf_free_stream(strm); librdf_free_serializer(srl); /*********************************************************************/ /* Cleanup */ /*********************************************************************/ librdf_free_statement(st); librdf_free_model(model); librdf_free_storage(storage); librdf_free_world(world); } catch (std::exception& e) { std::cerr << e.what() << std::endl; } }
/* * Convert the given Ruby +object+ (VALUE) to a librdf_node. */ librdf_node * rleaf_value_to_librdf_node( VALUE object ) { VALUE str, typeuristr, converted_pair; librdf_uri *typeuri; ID id; /* :TODO: how to set language? is_xml flag? */ // rleaf_log( "debug", "Converting %s to a librdf_node.", RSTRING_PTR(rb_inspect( object )) ); switch( TYPE(object) ) { /* nil -> bnode */ case T_NIL: return NULL; case T_SYMBOL: id = SYM2ID( object ); if ( id == rleaf_anon_bnodeid ) { return librdf_new_node_from_blank_identifier( rleaf_rdf_world, NULL ); } else { return librdf_new_node_from_blank_identifier( rleaf_rdf_world, (unsigned char *)rb_id2name(id) ); } /* String -> plain literal */ case T_STRING: return librdf_new_node_from_literal( rleaf_rdf_world, (unsigned char *)RSTRING_PTR(object), NULL, 0 ); break; /* Float -> xsd:float */ case T_FLOAT: str = rb_obj_as_string( object ); typeuri = XSD_FLOAT_TYPE; break; /* Bignum -> xsd:decimal */ case T_BIGNUM: str = rb_obj_as_string( object ); typeuri = XSD_DECIMAL_TYPE; break; /* Fixnum -> xsd:integer */ case T_FIXNUM: str = rb_obj_as_string( object ); typeuri = XSD_INTEGER_TYPE; break; /* TrueClass/FalseClass -> xsd:boolean */ case T_TRUE: case T_FALSE: str = rb_obj_as_string( object ); typeuri = XSD_BOOLEAN_TYPE; break; /* URI -> librdf_uri */ case T_OBJECT: if ( IsURI(object) || IsNamespace(object) ) { // rleaf_log( "debug", "Converting %s object to librdf_uri node", // rb_obj_classname(object) ); str = rb_obj_as_string( object ); return librdf_new_node_from_uri_string( rleaf_rdf_world, (unsigned char*)RSTRING_PTR(str) ); } /* fallthrough */ /* Delegate anything else to Redleaf::NodeUtils.make_object_typed_literal */ default: converted_pair = rb_funcall( rleaf_mRedleafNodeUtils, rb_intern("make_object_typed_literal"), 1, object ); str = rb_obj_as_string( rb_ary_entry(converted_pair, 0) ); typeuristr = rb_obj_as_string( rb_ary_entry(converted_pair, 1) ); typeuri = librdf_new_uri( rleaf_rdf_world, (unsigned char*)RSTRING_PTR(typeuristr) ); } return librdf_new_node_from_typed_counted_literal( rleaf_rdf_world, (unsigned char *)RSTRING_PTR(str), RSTRING_LEN(str), NULL, 0, typeuri ); }
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); }