Exemplo n.º 1
0
Arquivo: graph.c Projeto: ged/redleaf
/*
 * call-seq:
 *   graph.dup   -> graph
 *
 * Duplicate the receiver and return the copy.
 *
 */
static VALUE
rleaf_redleaf_graph_dup( VALUE self ) {
	rleaf_GRAPH *ptr = rleaf_get_graph( self );
	VALUE dup = rleaf_redleaf_graph_s_allocate( CLASS_OF(self) );
	rleaf_GRAPH *dup_ptr = ALLOC( rleaf_GRAPH );
	librdf_stream *statements = librdf_model_as_stream( ptr->model );

	rleaf_log_with_context( self, "debug", "Duping %s 0x%x", rb_obj_classname(self), self );

	dup_ptr->store = ptr->store;
	dup_ptr->model = librdf_new_model_from_model( ptr->model );
	if ( ! dup_ptr->model ) {
		librdf_free_stream( statements );
		xfree( dup_ptr );
		rb_raise( rleaf_eRedleafError, "couldn't create new model from model <%p>", ptr->model );
	}

	if ( (librdf_model_add_statements(dup_ptr->model, statements)) != 0 ) {
		librdf_free_stream( statements );
		librdf_free_model( dup_ptr->model );
		xfree( dup_ptr );
		rb_raise( rleaf_eRedleafError, "couldn't add statements from the original model" );
	}

	DATA_PTR( dup ) = dup_ptr;
	OBJ_INFECT( dup, self );

	return dup;
}
Exemplo n.º 2
0
redhttp_response_t *handle_description_get(redhttp_request_t * request, void *user_data)
{
  const raptor_syntax_description* desc = NULL;
  redhttp_response_t *response = NULL;
  librdf_storage *sd_storage = NULL;
  librdf_model *sd_model = NULL;
  librdf_stream *sd_stream = NULL;

  desc = redstore_negotiate_format(request, librdf_serializer_get_description, "text/html", NULL);
  if (desc == NULL || strcmp("html", desc->names[0])==0) {
    response = handle_html_description(request, user_data);
  } else {
    const char *request_url = redhttp_request_get_url(request);

    sd_storage = librdf_new_storage(world, NULL, NULL, NULL);
    if (!sd_storage) {
      redstore_error("Failed to create temporary storage for service description.");
      goto CLEANUP;
    }

    sd_model = create_service_description(sd_storage, request_url);
    if (!sd_model) {
      redstore_error("Failed to create model for service description.");
      goto CLEANUP;
    }

    sd_stream = librdf_model_as_stream(sd_model);
    if (!sd_stream) {
      redstore_error("Failed to create stream for service description.");
      goto CLEANUP;
    }

    response = format_graph_stream(request, sd_stream);
    if (!response) {
      redstore_error("Failed to create temporary storage for service description.");
      goto CLEANUP;
    }
  }


CLEANUP:
  if (sd_stream)
    librdf_free_stream(sd_stream);
  if (sd_model)
    librdf_free_model(sd_model);
  if (sd_storage)
    librdf_free_storage(sd_storage);

  return response;
}
Exemplo n.º 3
0
static int
librdf_serializer_raptor_serialize_model(void *context,
                                         FILE *handle, librdf_uri* base_uri,
                                         librdf_model *model) 
{
  /* librdf_serializer_raptor_context* pcontext=(librdf_serializer_raptor_context*)context; */

  librdf_stream *stream=librdf_model_as_stream(model);
  if(!stream)
    return 1;
  while(!librdf_stream_end(stream)) {
    librdf_statement *statement=librdf_stream_get_object(stream);
    librdf_serializer_print_statement_as_ntriple(statement, handle);
    fputc('\n', handle);
    librdf_stream_next(stream);
  }
  librdf_free_stream(stream);
  return 0;
}
Exemplo n.º 4
0
Arquivo: graph.c Projeto: ged/redleaf
/*
 * call-seq:
 *   graph.statements   -> array
 *
 * Return an Array of all the statements in the graph.
 *
 */
static VALUE
rleaf_redleaf_graph_statements( VALUE self ) {
	rleaf_GRAPH *ptr = rleaf_get_graph( self );
	librdf_stream *stream = librdf_model_as_stream( ptr->model );
	VALUE statements = rb_ary_new();

	rleaf_log_with_context( self, "debug",
		"Creating statement objects for %d statements in Graph <0x%x>.",
		librdf_model_size(ptr->model), self );
	while ( ! librdf_stream_end(stream) ) {
		librdf_statement *stmt = librdf_stream_get_object( stream );
		VALUE stmt_obj = rleaf_librdf_statement_to_value( stmt );

		rb_ary_push( statements, stmt_obj );
		librdf_stream_next( stream );
	}
	librdf_free_stream( stream );

	return statements;
}
Exemplo n.º 5
0
Arquivo: graph.c Projeto: ged/redleaf
/*
 * call-seq:
 *   graph.each_statement {|statement| block }   -> graph
 *   graph.each {|statement| block }             -> graph
 *
 * Call +block+ once for each statement in the graph.
 *
 */
static VALUE
rleaf_redleaf_graph_each_statement( VALUE self ) {
	rleaf_GRAPH *ptr = rleaf_get_graph( self );
	librdf_stream *stream;
	librdf_statement *stmt;

	stream = librdf_model_as_stream( ptr->model );
	if ( !stream )
		rb_raise( rleaf_eRedleafError, "Failed to create stream for graph" );

	while ( ! librdf_stream_end(stream) ) {
		stmt = librdf_stream_get_object( stream );
		if ( !stmt ) break;

		rb_yield( rleaf_librdf_statement_to_value(stmt) );
		librdf_stream_next( stream );
	}

	librdf_free_stream( stream );

	return self;
}
Exemplo n.º 6
0
static librdf_stream*
librdf_storage_file_serialise(librdf_storage* storage)
{
    librdf_storage_file_instance* context=(librdf_storage_file_instance*)storage->instance;
    return librdf_model_as_stream(context->model);
}
Exemplo n.º 7
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);
}
Exemplo n.º 8
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;

    }

}
Exemplo n.º 9
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);
}