Beispiel #1
0
static int
librdf_storage_file_add_statement(librdf_storage* storage, librdf_statement* statement)
{
    librdf_storage_file_instance* context=(librdf_storage_file_instance*)storage->instance;
    context->changed=1;
    return librdf_model_add_statement(context->model, statement);
}
template <> Statement::Statement (TConstStatement1 & a) 
  {
    librdf_statement *st;
    st = librdf_new_statement (rdf_world::get_world ());
     cerr << "template statement: sub:" << a.s << " pred:" << a.p << " obj:" << a.o << endl;
    librdf_statement_set_subject (st, node_create_from_uri (a.s.get_uri ()));
    librdf_statement_set_predicate (st, node_create_from_uri (a.p.get_uri ()));
    //librdf_statement_set_object (st, node_create_from_uri (a.o.get_uri ()));
    librdf_statement_set_object (st,node_create_from_string (a.o));
      
    librdf_model_add_statement (rdf_world::get_model (), st);
  }
Beispiel #3
0
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);
}
Beispiel #4
0
/*
 * call-seq:
 *    graph.append_statements( *statements )   -> graph
 *
 * Append one or more Redleaf::Statements to the graph.
 *
 */
static VALUE
rleaf_redleaf_graph_append_statements( int argc, VALUE *argv, VALUE self ) {
	rleaf_GRAPH *ptr = rleaf_get_graph( self );
	librdf_statement *stmt_ptr = NULL;
	VALUE statement = Qnil;
	int i = 0;

	rleaf_log_with_context( self, "debug", "Adding %d statements.", argc );

	for ( i = 0; i < argc; i++ ) {
		statement = argv[i];
		rleaf_log( "debug", "  adding statement %d: %s", i, RSTRING_PTR(rb_inspect(statement)) );
		stmt_ptr = rleaf_get_statement( statement );

		if ( librdf_model_add_statement(ptr->model, stmt_ptr) != 0 )
			rb_raise( rleaf_eRedleafError, "could not add statement %s to graph",
			 	RSTRING_PTR(rb_inspect(statement)) );
	}

	return self;
}
Beispiel #5
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;
}
Beispiel #6
0
  bool RdfStorePrivate::Add(RdfTriple triple)
  {
    librdf_statement* statement = RdfTripleToStatement(triple);

    if (!CheckComplete(statement))
    {
      librdf_free_statement(statement);
      return false;
    }

    // Store already contains statement
    if (Contains(triple)) return true;

    if (librdf_model_add_statement(m_Model, statement) != 0) {
      librdf_free_statement(statement);
      return false;
    }
    else
    {
      librdf_free_statement(statement);
      return true;
    }
  }
Beispiel #7
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) {
    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");
  }
}
Beispiel #8
0
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);
		}
		//////////////////////////////////////////////
	 }



 }
Beispiel #9
0
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);
}
Beispiel #10
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;

    }

}
Beispiel #11
0
int
main(int argc, char *argv[]) 
{
  librdf_world* world;
  librdf_storage *storage, *new_storage;
  librdf_model *model, *new_model;
  librdf_stream *stream;
  char *program=argv[0];
  char *name;
  char *new_name;
  int count;

  if(argc < 2 || argc >3) {
    fprintf(stderr, "USAGE: %s: <Redland BDB name> [new DB name]\n", program);
    return(1);
  }

  name=argv[1];

  if(argc < 3) {
    new_name=librdf_heuristic_gen_name(name);
    if(!new_name) {
      fprintf(stderr, "%s: Failed to create new name from '%s'\n", program,
              name);
      return(1);
    }
  } else {
    new_name=argv[2];
  }
  
  fprintf(stderr, "%s: Upgrading DB '%s' to '%s'\n", program, name, new_name);

  world=librdf_new_world();
  librdf_world_open(world);

  storage=librdf_new_storage(world, "hashes", name, 
                             "hash-type='bdb',dir='.',write='no',new='no'");
  if(!storage) {
    fprintf(stderr, "%s: Failed to open old storage '%s'\n", program, name);
    return(1);
  }

  new_storage=librdf_new_storage(world, "hashes", new_name,
                                 "hash-type='bdb',dir='.',write='yes',new='yes'");
  if(!storage) {
    fprintf(stderr, "%s: Failed to create new storage '%s'\n", program, new_name);
    return(1);
  }

  model=librdf_new_model(world, storage, NULL);
  if(!model) {
    fprintf(stderr, "%s: Failed to create model for '%s'\n", program, name);
    return(1);
  }

  new_model=librdf_new_model(world, new_storage, NULL);
  if(!new_model) {
    fprintf(stderr, "%s: Failed to create new model for '%s'\n", program, new_name);
    return(1);
  }
  
  stream=librdf_model_as_stream(model);
  if(!stream) {
    fprintf(stderr, "%s: librdf_model_as_stream returned NULL stream\n", 
            program);
    return(1);
  } else {
    count=0;
    while(!librdf_stream_end(stream)) {
      librdf_statement *statement=librdf_stream_get_object(stream);
      if(!statement) {
        fprintf(stderr, "%s: librdf_stream_next returned NULL\n", program);
        break;
      }
      
      librdf_model_add_statement(new_model, statement);

      librdf_stream_next(stream);
      count++;
    }
    librdf_free_stream(stream);  
  }

      
  librdf_free_model(model);
  librdf_free_model(new_model);

  librdf_free_storage(storage);
  librdf_free_storage(new_storage);

  librdf_free_world(world);

  if(argc < 3)
    free(new_name);


#ifdef LIBRDF_MEMORY_DEBUG
  librdf_memory_report(stderr);
#endif
	
  /* keep gcc -Wall happy */
  return(0);
}