Beispiel #1
0
RdfStatement::RdfStatement(IWorld* world, INode *subject, INode *predicate, INode *object) : IStatement(world, subject, predicate, object)
{
    RdfWorld* rdfWorld = dynamic_cast<RdfWorld*>(world);
    assert(rdfWorld && "Failed to dynamic cast IWorld to RdfWorld"); 

    if (rdfWorld)
    {
        RdfNode *rdfSubject   = dynamic_cast<RdfNode*>(subject);
        RdfNode *rdfPredicate = dynamic_cast<RdfNode*>(predicate);
        RdfNode *rdfObject    = dynamic_cast<RdfNode*>(object);

        if (rdfSubject)
            this->subject = new RdfNode(rdfSubject, world);
        if (rdfPredicate)
            this->predicate = new RdfNode(rdfPredicate, world);
        if (rdfObject)
            this->object = new RdfNode(rdfObject, world);

        librdf_node* s = rdfSubject ? rdfSubject->node : 0;
        librdf_node* p = rdfPredicate ? rdfPredicate->node : 0;
        librdf_node* o = rdfObject ? rdfObject->node : 0;
        statement = librdf_new_statement_from_nodes(rdfWorld->world, s, p, o);
    }
    else
        LogError("Failed to cast IWorld to RdfWorld.");
}
static void*
cassandra_results_stream_get_statement(void* context, int flags)
{

    cassandra_results_stream* scontext;
    const char* s;
    size_t s_len;
    const char* p;
    size_t p_len;
    const char* o;
    size_t o_len;
    const CassRow* row;
	
    scontext = (cassandra_results_stream*)context;

    switch(flags) {

    case LIBRDF_ITERATOR_GET_METHOD_GET_OBJECT:

	row = cass_iterator_get_row(scontext->iter);

	cass_value_get_string(cass_row_get_column(row, 0), &s, &s_len);
	cass_value_get_string(cass_row_get_column(row, 1), &p, &p_len);
	cass_value_get_string(cass_row_get_column(row, 2), &o, &o_len);

	if (scontext->statement) {
	    librdf_free_statement(scontext->statement);
	    scontext->statement = 0;
	}

	librdf_node* sn, * pn, * on;
	sn = node_constructor_helper(scontext->storage->world, s, s_len);
	pn = node_constructor_helper(scontext->storage->world, p, p_len);
	on = node_constructor_helper(scontext->storage->world, o, o_len);

	if (sn == 0 || pn == 0 || on == 0) {
	    if (sn) librdf_free_node(sn);
	    if (pn) librdf_free_node(pn);
	    if (on) librdf_free_node(on);
	    return 0;
	}

	scontext->statement =
	    librdf_new_statement_from_nodes(scontext->storage->world,
					    sn, pn, on);

	return scontext->statement;

    case LIBRDF_ITERATOR_GET_METHOD_GET_CONTEXT:
	return scontext->context;

    default:
	librdf_log(scontext->storage->world,
		   0, LIBRDF_LOG_ERROR, LIBRDF_FROM_STORAGE, NULL,
		   "Unknown iterator method flag %d", flags);
	return NULL;
    }
    
}
Beispiel #3
0
  librdf_statement* RdfStorePrivate::RdfTripleToStatement(RdfTriple triple) const
  {
    librdf_node* subject = RdfNodeToLibRdfNode(triple.GetTripleSubject());
    librdf_node* predicate = RdfNodeToLibRdfNode(triple.GetTriplePredicate());
    librdf_node* object = RdfNodeToLibRdfNode(triple.GetTripleObject());

    librdf_statement* statement = librdf_new_statement_from_nodes(m_World, subject, predicate, object);
    if(!statement) return nullptr;
    return statement;
  }
Beispiel #4
0
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);
}
Beispiel #5
0
/**
 * @brief Executes the given delete query
 * @attention DELETE-Statement not supported in Redland, so the Query has to be SELECT with < ?s ?p ?o > statements!
 * 		These statements are removed out of the storage.
 * 		
 *
 * @param query The query to execute.
 */
bool RDF_ExecDiscardQuery(unsigned char* query)
{
    librdf_query* rdfquery;
    librdf_query_results* results;

    rdfquery = librdf_new_query(world, "sparql", NULL, query, NULL);
    results = librdf_model_query_execute(model, rdfquery);
    
    librdf_free_query(rdfquery);
    
    //Get all statements from the select query and remove them from storage
    while(!librdf_query_results_finished(results)) 
    {
      const char **names = NULL;
      librdf_node* values[10];
    
      if(librdf_query_results_get_bindings(results, &names, values))
      {
        break;
      }
          
      librdf_node* subject_tmp = values[0];
      librdf_node* predicate_tmp = values[1];
      librdf_node* object_tmp = values[2];
      
      librdf_statement* statement_tmp = librdf_new_statement_from_nodes(world, subject_tmp, predicate_tmp, object_tmp);
      librdf_storage_remove_statement(storage, statement_tmp);
      
      librdf_free_node(subject_tmp);
      librdf_free_node(predicate_tmp);
      librdf_free_node(object_tmp);      
      librdf_free_statement(statement_tmp);
      
      int i = 0;
      
      for (i = 0; i < 10; i++)
      {
          librdf_free_node(values[i]);
      }
          
      
      librdf_query_results_next(results);
      
      free(names);
    }    
    
    if (results == NULL)
    {
        printf("Failed to execute discard query!\n");
    }
    
    librdf_free_query_results(results);
    
    return true;
}
static librdf_stream*
librdf_storage_cassandra_serialise(librdf_storage* storage)
{

    librdf_statement* stmt = 
	    librdf_new_statement_from_nodes(storage->world,
					    0, 0, 0);

    librdf_stream* strm = librdf_storage_cassandra_find_statements(storage,
								   stmt);

    librdf_free_statement(stmt);

    return strm;

}
Beispiel #7
0
/*
 * call-seq:
 *   graph.search( subject, predicate, object )   -> array
 *   graph[ subject, predicate, object ]          -> array
 *
 * Search for statements in the graph with the specified +subject+, +predicate+, and +object+ and
 * return them. If +subject+, +predicate+, or +object+ are nil, they will match any value.
 *
 *   # Match any statements about authors
 *   graph.load( 'http://deveiant.livejournal.com/data/foaf' )
 *
 *   #
 *   graph[ nil, FOAF[:knows], nil ]  # => [...]
 */
static VALUE
rleaf_redleaf_graph_search( VALUE self, VALUE subject, VALUE predicate, VALUE object ) {
	rleaf_GRAPH *ptr = rleaf_get_graph( self );
	librdf_node *subject_node, *predicate_node, *object_node;
	librdf_statement *search_statement, *stmt;
	librdf_stream *stream;
	int count = 0;
	VALUE rval = rb_ary_new();

	rleaf_log_with_context( self, "debug", "searching for statements matching {%s, %s, %s}",
		RSTRING_PTR(rb_inspect(subject)),
		RSTRING_PTR(rb_inspect(predicate)),
		RSTRING_PTR(rb_inspect(object)) );

	subject_node   = rleaf_value_to_subject_node( subject );
	predicate_node = rleaf_value_to_predicate_node( predicate );
	object_node    = rleaf_value_to_object_node( object );

	search_statement = librdf_new_statement_from_nodes( rleaf_rdf_world, subject_node, predicate_node, object_node );
	if ( !search_statement )
		rb_raise( rleaf_eRedleafError, "could not create a statement from nodes [%s, %s, %s]",
			RSTRING_PTR(rb_inspect(subject)),
			RSTRING_PTR(rb_inspect(predicate)),
			RSTRING_PTR(rb_inspect(object)) );

 	stream = librdf_model_find_statements( ptr->model, search_statement );
	if ( !stream ) {
		librdf_free_statement( search_statement );
		rb_raise( rleaf_eRedleafError, "could not create a stream when searching" );
	}

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

		count++;
		rb_ary_push( rval, rleaf_librdf_statement_to_value(stmt) );
		librdf_stream_next( stream );
	}

	rleaf_log_with_context( self, "debug", "found %d statements", count );

	librdf_free_stream( stream );
	librdf_free_statement( search_statement );

	return rval;
}
Beispiel #8
0
RdfStatement::RdfStatement(IWorld* world, librdf_statement* statement) :
  IStatement(world)
{
    RdfNode *s = new RdfNode(librdf_statement_get_subject(statement), world);
    RdfNode *p = new RdfNode(librdf_statement_get_predicate(statement), world);
    RdfNode *o = new RdfNode(librdf_statement_get_object(statement), world);

    subject = s;
    predicate = p;
    object = o;

    RdfWorld* rdfWorld = dynamic_cast<RdfWorld*>(world);
    assert(rdfWorld && "Failed to dynamic cast IWorld to RdfWorld");

    if (rdfWorld)
        this->statement = librdf_new_statement_from_nodes(rdfWorld->world, s->node, p->node, o->node);
    else
        LogError("Failed to cast IWorld to RdfWorld.");
}
Beispiel #9
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 #10
0
static int
rasqal_redland_triple_present(rasqal_triples_source *rts, void *user_data, 
                              rasqal_triple *t) 
{
  rasqal_redland_triples_source_user_data* rtsc=(rasqal_redland_triples_source_user_data*)user_data;
  librdf_node* nodes[3];
  librdf_statement *s;
  int rc;
  
  /* ASSUMPTION: all the parts of the triple are not variables */
  /* FIXME: and no error checks */
  nodes[0]=rasqal_literal_to_redland_node(rtsc->world, t->subject);
  nodes[1]=rasqal_literal_to_redland_node(rtsc->world, t->predicate);
  nodes[2]=rasqal_literal_to_redland_node(rtsc->world, t->object);

  s=librdf_new_statement_from_nodes(rtsc->world, nodes[0], nodes[1], nodes[2]);
  
  rc=librdf_model_contains_statement(rtsc->model, s);
  librdf_free_statement(s);
  return rc;
}
Beispiel #11
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 #12
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);
}
Beispiel #13
0
static int
rasqal_redland_init_triples_match(rasqal_triples_match* rtm,
                                  rasqal_triples_source *rts, void *user_data,
                                  rasqal_triple_meta *m, rasqal_triple *t)
{
  rasqal_redland_triples_source_user_data* rtsc=(rasqal_redland_triples_source_user_data*)user_data;
  rasqal_redland_triples_match_context* rtmc;
  rasqal_variable* var;

  rtm->bind_match=rasqal_redland_bind_match;
  rtm->next_match=rasqal_redland_next_match;
  rtm->is_end=rasqal_redland_is_end;
  rtm->finish=rasqal_redland_finish_triples_match;

  rtmc = LIBRDF_CALLOC(rasqal_redland_triples_match_context*, 1, sizeof(*rtmc));
  if(!rtmc)
    return 1;

  rtm->user_data=rtmc;


  /* at least one of the triple terms is a variable and we need to
   * do a triplesMatching() aka librdf_model_find_statements
   *
   * redland find_statements will do the right thing and internally
   * pick the most efficient, indexed way to get the answer.
   */

  if((var=rasqal_literal_as_variable(t->subject))) {
    if(var->value)
      rtmc->nodes[0]=rasqal_literal_to_redland_node(rtsc->world, var->value);
    else
      rtmc->nodes[0]=NULL;
  } else
    rtmc->nodes[0]=rasqal_literal_to_redland_node(rtsc->world, t->subject);

  m->bindings[0]=var;
  

  if((var=rasqal_literal_as_variable(t->predicate))) {
    if(var->value)
      rtmc->nodes[1]=rasqal_literal_to_redland_node(rtsc->world, var->value);
    else
      rtmc->nodes[1]=NULL;
  } else
    rtmc->nodes[1]=rasqal_literal_to_redland_node(rtsc->world, t->predicate);

  m->bindings[1]=var;
  

  if((var=rasqal_literal_as_variable(t->object))) {
    if(var->value)
      rtmc->nodes[2]=rasqal_literal_to_redland_node(rtsc->world, var->value);
    else
      rtmc->nodes[2]=NULL;
  } else
    rtmc->nodes[2]=rasqal_literal_to_redland_node(rtsc->world, t->object);

  m->bindings[2]=var;
  

  if(t->origin) {
    if((var=rasqal_literal_as_variable(t->origin))) {
      if(var->value)
        rtmc->origin=rasqal_literal_to_redland_node(rtsc->world, var->value);
    } else
      rtmc->origin=rasqal_literal_to_redland_node(rtsc->world, t->origin);
    m->bindings[3]=var;
  }


  rtmc->qstatement=librdf_new_statement_from_nodes(rtsc->world, 
                                                   rtmc->nodes[0],
                                                   rtmc->nodes[1], 
                                                   rtmc->nodes[2]);
  if(!rtmc->qstatement)
    return 1;

#if defined(LIBRDF_DEBUG) && LIBRDF_DEBUG > 1
  LIBRDF_DEBUG1("query statement: ");
  librdf_statement_print(rtmc->qstatement, stderr);
  if(rtmc->origin) {
    fput(" with context node: ", stderr);
    librdf_node_print(rtmc->origin, stderr);
  }
  fputc('\n', stderr);
#endif
  
  if(rtmc->origin)
    rtmc->stream=librdf_model_find_statements_in_context(rtsc->model, 
                                                         rtmc->qstatement,
                                                         rtmc->origin);
  else
    rtmc->stream=librdf_model_find_statements(rtsc->model, rtmc->qstatement);

  if(!rtmc->stream)
    return 1;

#if defined(LIBRDF_DEBUG) && LIBRDF_DEBUG > 1
  LIBRDF_DEBUG1("rasqal_init_triples_match done\n");
#endif

  return 0;
}
Beispiel #14
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 #15
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;

    }

}