Beispiel #1
0
void statement_in_tl(GSList ** list_, librdf_statement * statement)
{
	GSList * list=*list_;

	ssTriple_t * triple;

	librdf_node* sub_node ;
	librdf_node* pred_node;
	librdf_node* obj_node ;

	sub_node  = librdf_statement_get_subject(statement);
	pred_node = librdf_statement_get_predicate(statement);
	obj_node  = librdf_statement_get_object(statement);

	triple  =(ssTriple_t *) g_new0(ssTriple_t,1);

	triple->subject=	g_strdup(librdf_uri_as_string( librdf_node_get_uri(sub_node)));
	triple->predicate =	g_strdup(librdf_uri_as_string( librdf_node_get_uri(pred_node)));

	if (librdf_node_is_literal(obj_node))
	{	triple->object =  g_strdup(librdf_node_get_literal_value(obj_node));
		triple->objType = ssElement_TYPE_LIT;
	}
	else
	{	triple->object =  g_strdup(librdf_uri_as_string( librdf_node_get_uri(obj_node)));
		triple->objType = ssElement_TYPE_URI;
	}

	//printf ("statement to TL %s_%s_%s_%d\n", (char *)triple->subject, (char *)triple->predicate, (char *)triple->object, (bool *)triple->objType);
	list=g_slist_prepend(list, triple);
	///////////////////////////////////////////////////////////

	*list_=list;

}
Beispiel #2
0
static int
librdf_storage_trees_node_compare(librdf_node* n1, librdf_node* n2)
{
  if (n1 == n2) {
    return 0;
  } else if (n1->type != n2->type) {
    return n2->type - n1->type;
  } else {
    switch (n1->type) {
      case LIBRDF_NODE_TYPE_RESOURCE:
        return librdf_uri_compare(librdf_node_get_uri(n1), librdf_node_get_uri(n2));
      case LIBRDF_NODE_TYPE_LITERAL:
        if (1) {
          const char *s;
          size_t l1;
          size_t l2;
          size_t l;
          int r;

          s = librdf_node_get_literal_value_language(n1);
          l1 = s ? strlen(s) : 0;
          s = librdf_node_get_literal_value_language(n2);
          l2 = s ? strlen(s) : 0;

          l = (l1 < l2) ? l1 : l2;

          /* compare first by data type */
          r = librdf_uri_compare(librdf_node_get_literal_value_datatype_uri(n1),
                                 librdf_node_get_literal_value_datatype_uri(n2));
          if (r)
            return r;

          /* if data type is equal, compare by value */
          r = strcmp((const char*)librdf_node_get_literal_value(n1),
                     (const char*)librdf_node_get_literal_value(n2));
          if (r)
            return r;

          /* if both data type and value are equal, compare by language */
          if (l) {
            return strncmp(librdf_node_get_literal_value_language(n1),
                           librdf_node_get_literal_value_language(n2),
                           (size_t)l);
          } else {
            /* if l == 0 strncmp will always return 0; in that case
             * consider the node with no language to be lesser. */
            return l1 - l2;
          }
        }
      case LIBRDF_NODE_TYPE_BLANK:
        return strcmp((char*)n1->value.blank.identifier,
                      (char*)n2->value.blank.identifier);
      case LIBRDF_NODE_TYPE_UNKNOWN:
      default:
        return (char*)n2-(char*)n1; /* ? */
    }
  }
}
Beispiel #3
0
  RdfNode RdfStorePrivate::LibRdfNodeToRdfNode(librdf_node* node) const
  {
    RdfNode mitkNode;

    if (!node) return mitkNode;

    if (librdf_node_is_resource(node))
    {
      mitkNode.SetType(RdfNode::URI);
      librdf_uri *uri = librdf_node_get_uri(node);
      mitkNode.SetValue(LibRdfUriToRdfUri(uri).ToString());
    }
    else if (librdf_node_is_literal(node))
    {
      mitkNode.SetType(RdfNode::LITERAL);
      std::string value = (const char*) librdf_node_get_literal_value(node);
      if (!value.empty()) mitkNode.SetValue(value);
      librdf_uri* typeUri = librdf_node_get_literal_value_datatype_uri(node);
      if (typeUri) mitkNode.SetDatatype(LibRdfUriToRdfUri(typeUri));
    }
    else if (librdf_node_is_blank(node))
    {
      mitkNode.SetType(RdfNode::BLANK);
      std::string str = (const char*) librdf_node_get_blank_identifier(node);
      if (!str.empty()) mitkNode.SetValue(str);
    }
    return mitkNode;
  }
Beispiel #4
0
/**
 * librdf_init_concepts:
 * @world: redland world object
 *
 * INTERNAL - Initialise the concepts module.
 *
 **/
void
librdf_init_concepts(librdf_world *world)
{
    int i;

    /* Create the Unique URI objects */
    world->concept_ms_namespace_uri=librdf_new_uri(world, librdf_concept_ms_namespace);
    world->concept_schema_namespace_uri=librdf_new_uri(world, librdf_concept_schema_namespace);
    if(!world->concept_ms_namespace_uri || !world->concept_schema_namespace_uri)
        LIBRDF_FATAL1(world, LIBRDF_FROM_CONCEPTS, "Failed to create M&S or Schema URIs");

    /* Create arrays for the M&S and Schema resource nodes and uris */
    world->concept_uris=
        (librdf_uri**)LIBRDF_CALLOC(ptrarray, LIBRDF_CONCEPT_LAST+1, sizeof(librdf_uri*));
    world->concept_resources=
        (librdf_node**)LIBRDF_CALLOC(ptrarray, LIBRDF_CONCEPT_LAST+1, sizeof(librdf_node*));
    if(!world->concept_uris || !world->concept_resources)
        LIBRDF_FATAL1(world, LIBRDF_FROM_CONCEPTS, "Out of memory creating node/uri arrays");

    /* Create the M&S and Schema resource nodes */
    for (i=0; i<= LIBRDF_CONCEPT_LAST; i++) {
        librdf_uri* ns_uri=(i < LIBRDF_CONCEPT_FIRST_S_ID) ? world->concept_ms_namespace_uri :
                           world->concept_schema_namespace_uri;
        const unsigned char * token=(const unsigned char *)librdf_concept_tokens[i];

        world->concept_resources[i]=librdf_new_node_from_uri_local_name(world, ns_uri, token);
        if(!world->concept_resources[i])
            LIBRDF_FATAL1(world, LIBRDF_FROM_CONCEPTS, "Failed to create Node from URI\n");

        /* keep shared copy of URI from node */
        world->concept_uris[i]=librdf_node_get_uri(world->concept_resources[i]);
    }
}
Beispiel #5
0
/**
 * librdf_storage_tstore_context_add_statement:
 * @storage: #librdf_storage object
 * @context_node: #librdf_node object
 * @statement: #librdf_statement statement to add
 *
 * Add a statement to a storage context.
 * 
 * Return value: non 0 on failure
 **/
static int
librdf_storage_tstore_context_add_statement(librdf_storage* storage,
                                            librdf_node* context_node,
                                            librdf_statement* statement) 
{
  librdf_storage_tstore_instance* context=(librdf_storage_tstore_instance*)storage->instance;
  librdf_node *subject_node=statement->subject;
  librdf_node *predicate_node=statement->predicate;
  librdf_node *object_node=statement->object;
  char *subject;
  char *predicate;
  char *object;
  rs_obj_type type;

  
  if(librdf_node_is_blank(subject_node)) {
    subject=(char*)librdf_node_get_blank_identifier(subject_node);
  } else
    subject=(char*)librdf_uri_as_string(librdf_node_get_uri(subject_node));
  

  predicate=(char*)librdf_uri_as_string(librdf_node_get_uri(predicate_node));
  
  /* Assumptions - FIXME */
  if(librdf_node_is_literal(object_node)) {
    object=(char*)librdf_node_get_literal_value(object_node);
    type = ObjLiteral;
  } else if(librdf_node_is_blank(object_node)) {
    object=(char*)librdf_node_get_blank_identifier(object_node);
    type = ObjURI;
  } else {
    object=(char*)librdf_uri_as_string(librdf_node_get_uri(object_node));
    type = ObjURI;
  }
  

  if(rs_assert_triple(context->rdfsql, subject, predicate, object, type))
    return 1;

  return 0;
}
Beispiel #6
0
void output_node(librdf_node* n)
{

    if (librdf_node_is_literal(n)) {
	std::cout << librdf_node_get_literal_value(n);
    }

    if (librdf_node_is_resource(n)) {
	librdf_uri* uri = librdf_node_get_uri(n);
	std::cout << librdf_uri_as_string(uri);
    }

}
Beispiel #7
0
rasqal_literal*
redland_node_to_rasqal_literal(librdf_world* world, librdf_node *node)
{
  rasqal_literal* l;
  
  if(librdf_node_is_resource(node)) {
    raptor_uri* uri=(raptor_uri*)librdf_new_uri_from_uri(librdf_node_get_uri(node));
    l=rasqal_new_uri_literal(world->rasqal_world_ptr, uri); /* transfer uri ownership to literal */
  } else if(librdf_node_is_literal(node)) {
    unsigned char *string;
    librdf_uri *uri;
    unsigned char *new_string;
    char *new_language=NULL;
    raptor_uri *new_datatype=NULL;
    size_t len;

    string=librdf_node_get_literal_value_as_counted_string(node, &len);
    new_string=(unsigned char*)rasqal_alloc_memory(len+1);
    if(!new_string)
      return NULL;
    strcpy((char*)new_string, (const char*)string);

    string=(unsigned char*)librdf_node_get_literal_value_language(node);
    if(string) {
      new_language=(char*)rasqal_alloc_memory(strlen((const char*)string)+1);
      if(!new_language) {
        rasqal_free_memory((void*)new_string);
        return NULL;
      }
      strcpy((char*)new_language, (const char*)string);
    }
    uri=librdf_node_get_literal_value_datatype_uri(node);
    if(uri)
      new_datatype=(raptor_uri*)librdf_new_uri_from_uri(uri);
    /* transfer new_string,new_language,new_datatype ownership to literal */
    l = rasqal_new_string_literal(world->rasqal_world_ptr, (const unsigned char*)new_string, new_language, new_datatype, NULL);
  } else {
    unsigned char *blank=librdf_node_get_blank_identifier(node);
    unsigned char *new_blank;
    if(!blank)
      return NULL;
    new_blank=(unsigned char*)rasqal_alloc_memory(strlen((const char*)blank)+1);
    if(!new_blank)
      return NULL;
    strcpy((char*)new_blank, (const char*)blank);
    /* transfer new_blank ownership to literal */
    l = rasqal_new_simple_literal(world->rasqal_world_ptr, RASQAL_LITERAL_BLANK, (const unsigned char*)new_blank);
  }

  return l;
}
Beispiel #8
0
/* FIXME returns an alloced triple pointing to shared strings */
static rs_triple*
librdf_storage_tstore_statement_as_rs_triple(librdf_statement *statement)
{
  librdf_node *subject_node=statement->subject;
  librdf_node *predicate_node=statement->predicate;
  librdf_node *object_node=statement->object;
  rs_triple* triple=LIBRDF_MALLOC(rs_triple, sizeof(rs_triple));

  if(subject_node) {
    if(librdf_node_is_blank(subject_node))
      triple->subject=(char*)librdf_node_get_blank_identifier(subject_node);
    else
      triple->subject=(char*)librdf_uri_as_string(librdf_node_get_uri(subject_node));
  } else
    triple->subject=NULL;

  if(predicate_node)
    triple->predicate=(char*)librdf_uri_as_string(librdf_node_get_uri(predicate_node));
  else
    triple->predicate=NULL;
  
  /* Assumptions - FIXME */
  triple->literal = 0;
  if(object_node) {
    if(librdf_node_is_literal(object_node)) {
      triple->object=(char*)librdf_node_get_literal_value(object_node);
      triple->literal = 1;
    } else if(librdf_node_is_blank(object_node)) {
      triple->object=(char*)librdf_node_get_blank_identifier(object_node);
    } else {
      triple->object=(char*)librdf_uri_as_string(librdf_node_get_uri(object_node));
    }
  } else
    triple->object=NULL;
  
  return triple;
}
Beispiel #9
0
/*
 * Convert the given resource librdf_node to a Ruby URI object and return it.
 */
VALUE
rleaf_librdf_uri_node_to_object( librdf_node *node ) {
	VALUE node_object = Qnil;
	librdf_uri *uri;
	const unsigned char *uristring = NULL;

	if ( !librdf_node_is_resource(node) )
		rb_raise( rleaf_eRedleafError, "cannot convert a non-resource to a URI" );
	if ( (uri = librdf_node_get_uri( node )) == NULL )
		rb_raise( rleaf_eRedleafError, "unable to fetch a uri from resource node" );
	if ( (uristring = librdf_uri_as_string( uri )) == NULL )
		rb_raise( rleaf_eRedleafError, "unable to fetch a string from uri" );

	// rleaf_log( "debug", "converting %s to a URI object", uristring );
	node_object = rb_funcall( rleaf_rb_cURI, rb_intern("parse"), 1,
		rb_str_new2((const char *)uristring) );

	return node_object;
}
Beispiel #10
0
/** Create a new SLV2Value from a librdf_node, or return NULL if impossible */
SLV2Value
slv2_value_from_librdf_node(SLV2World world, librdf_node* node)
{
	SLV2Value result = NULL;

	librdf_uri* datatype_uri = NULL;
	SLV2ValueType type = SLV2_VALUE_STRING;

	switch (librdf_node_get_type(node)) {
	case LIBRDF_NODE_TYPE_RESOURCE:
		type = SLV2_VALUE_URI;
		result = slv2_value_new_librdf_uri(world, librdf_node_get_uri(node));
		break;
	case LIBRDF_NODE_TYPE_LITERAL:
		datatype_uri = librdf_node_get_literal_value_datatype_uri(node);
		if (datatype_uri) {
			if (!strcmp((const char*)librdf_uri_as_string(datatype_uri),
						"http://www.w3.org/2001/XMLSchema#integer"))
				type = SLV2_VALUE_INT;
			else if (!strcmp((const char*)librdf_uri_as_string(datatype_uri),
						"http://www.w3.org/2001/XMLSchema#decimal"))
				type = SLV2_VALUE_FLOAT;
			else
				fprintf(stderr, "Unknown datatype %s\n", librdf_uri_as_string(datatype_uri));
		}
		result = slv2_value_new(world, type, (const char*)librdf_node_get_literal_value(node));
		break;
	case LIBRDF_NODE_TYPE_BLANK:
		type = SLV2_VALUE_STRING;
		result = slv2_value_new(world, type, (const char*)librdf_node_get_blank_identifier(node));
		break;
	case LIBRDF_NODE_TYPE_UNKNOWN:
	default:
		fprintf(stderr, "Unknown RDF node type %d\n", librdf_node_get_type(node));
		break;
	}

	return result;
}
Beispiel #11
0
/**
 * librdf_init_concepts - Initialise the concepts module.
 * @world: redland world object
 * 
 **/
void
librdf_init_concepts(librdf_world *world)
{
  int i;


  /* Create the Unique URI objects */
  librdf_concept_ms_namespace_uri=librdf_new_uri(world, librdf_concept_ms_namespace);
  librdf_concept_schema_namespace_uri=librdf_new_uri(world, librdf_concept_schema_namespace);

  /* Create the M&S and Schema resource nodes */
  for (i=0; i< LIBRDF_CONCEPT_LAST; i++) {
    librdf_uri* ns_uri=(i < LIBRDF_CONCEPT_FIRST_S_ID) ? librdf_concept_ms_namespace_uri :
      librdf_concept_schema_namespace_uri;
    const unsigned char * token=(const unsigned char *)librdf_concept_tokens[i];

    librdf_concept_resources[i]=librdf_new_node_from_uri_local_name(world, ns_uri, token);
    if(!librdf_concept_resources[i])
      LIBRDF_FATAL1(world, "Failed to create Node from URI\n");

    /* keep shared copy of URI from node */
    librdf_concept_uris[i]=librdf_node_get_uri(librdf_concept_resources[i]);
  }
}
static
char* node_helper(librdf_storage* storage, librdf_node* node)
{

    librdf_uri* uri;
    librdf_uri* dt_uri;

    const char* integer_type = "http://www.w3.org/2001/XMLSchema#integer";
    const char* float_type = "http://www.w3.org/2001/XMLSchema#float";
    const char* datetime_type = "http://www.w3.org/2001/XMLSchema#dateTime";

    char* name;
    char data_type;

    switch(librdf_node_get_type(node)) {

    case LIBRDF_NODE_TYPE_RESOURCE:
	uri = librdf_node_get_uri(node);
	name = librdf_uri_as_string(uri);
	data_type = 'u';
	break;
	
    case LIBRDF_NODE_TYPE_LITERAL:
	dt_uri = librdf_node_get_literal_value_datatype_uri(node);
	if (dt_uri == 0)
	    data_type = 's';
	else {
	    const char* type_uri = librdf_uri_as_string(dt_uri);
	    if (strcmp(type_uri, integer_type) == 0)
		data_type = 'i';
	    else if (strcmp(type_uri, float_type) == 0)
		data_type = 'f';
	    else if (strcmp(type_uri, datetime_type) == 0)
		data_type = 'd';
	    else
		data_type = 's';
	}
	name = librdf_node_get_literal_value(node);
	break;

    case LIBRDF_NODE_TYPE_BLANK:
	name = librdf_node_get_blank_identifier(node);
	data_type = 'b';
	break;

    case LIBRDF_NODE_TYPE_UNKNOWN:
	break;
	
    }

    char* term = malloc(5 + strlen(name));
    if (term == 0) {
	fprintf(stderr, "malloc failed");
	return 0;
    }
    
    sprintf(term, "%c:%s", data_type, name);

    return term;

}
static void
librdf_serializer_print_statement_as_ntriple(librdf_statement * statement,
                                             FILE *stream) 
{
  librdf_node *subject=librdf_statement_get_subject(statement);
  librdf_node *predicate=librdf_statement_get_predicate(statement);
  librdf_node *object=librdf_statement_get_object(statement);
  char *lang=NULL;
  librdf_uri *dt_uri=NULL;
  
  if(librdf_node_is_blank(subject))
    fprintf(stream, "_:%s", librdf_node_get_blank_identifier(subject));
  else if(librdf_node_is_resource(subject)) {
    /* Must be a URI */
    fputc('<', stream);
    raptor_print_ntriples_string(stream, librdf_uri_as_string(librdf_node_get_uri(subject)), '\0');
    fputc('>', stream);
  } else {
    LIBRDF_ERROR2(statement->world, "Do not know how to print triple subject type %d\n", librdf_node_get_type(subject));
    return;
  }

  if(!librdf_node_is_resource(predicate)) {
    LIBRDF_ERROR2(statement->world, "Do not know how to print triple predicate type %d\n", librdf_node_get_type(predicate));
    return;
  }

  fputc(' ', stream);
  fputc('<', stream);
  raptor_print_ntriples_string(stream, librdf_uri_as_string(librdf_node_get_uri(predicate)), '\0');
  fputc('>', stream);
  fputc(' ', stream);

  switch(librdf_node_get_type(object)) {
    case LIBRDF_NODE_TYPE_LITERAL:
      fputc('"', stream);
      raptor_print_ntriples_string(stream, librdf_node_get_literal_value(object), '"');
      fputc('"', stream);
      lang=librdf_node_get_literal_value_language(object);
      dt_uri=librdf_node_get_literal_value_datatype_uri(object);
      if(lang) {
        fputc('@', stream);
        fputs(lang, stream);
      }
      if(dt_uri) {
        fputs("^^<", stream); 
        raptor_print_ntriples_string(stream, librdf_uri_as_string(dt_uri), '\0');
        fputc('>', stream);
      }
      break;
    case LIBRDF_NODE_TYPE_BLANK:
      fputs("_:", stream);
      fputs((const char*)librdf_node_get_blank_identifier(object), stream);
      break;
    case LIBRDF_NODE_TYPE_RESOURCE:
      fputc('<', stream);
      raptor_print_ntriples_string(stream, librdf_uri_as_string(librdf_node_get_uri(object)), '\0');
      fputc('>', stream);
      break;
    default:
      LIBRDF_ERROR2(statement->world, "Do not know how to print triple object type %d\n", librdf_node_get_type(object));
      return;
  }
  fputs(" .", stream);
}
Beispiel #14
0
static int
rasqal_redland_new_triples_source(rasqal_query* rdf_query,
                                  void *factory_user_data,
                                  void *user_data,
                                  rasqal_triples_source *rts)
{
  librdf_world *world=(librdf_world*)factory_user_data;
  rasqal_redland_triples_source_user_data* rtsc=(rasqal_redland_triples_source_user_data*)user_data;
  raptor_sequence *seq;
  librdf_query_rasqal_context *context;
  librdf_iterator* cit;

  rtsc->world = world;
  rtsc->query = (librdf_query*)rasqal_query_get_user_data(rdf_query);
  context = (librdf_query_rasqal_context*)rtsc->query->context;
  rtsc->model = context->model;

  seq = rasqal_query_get_data_graph_sequence(rdf_query);
  
  /* FIXME: queries with data graphs in them (such as FROM in SPARQL)
   * are deleted, so that there are no unexpected data loads
   */
  if(seq) {
    while(raptor_sequence_size(seq)) {
      rasqal_data_graph* dg=(rasqal_data_graph*)raptor_sequence_pop(seq);
      rasqal_free_data_graph(dg);
    }
  }

  if(librdf_model_supports_contexts(rtsc->model)) {
    /* Add all contexts (named graphs) to the query so Rasqal can bind them */
    cit = librdf_model_get_contexts(rtsc->model);
    while(!librdf_iterator_end(cit)) {
      librdf_node* node = (librdf_node*)librdf_iterator_get_object(cit);
      librdf_uri* uri;
      raptor_uri* source_uri;
      rasqal_data_graph* dg;

      uri = librdf_node_get_uri(node);
      source_uri = (raptor_uri*)raptor_new_uri(world->raptor_world_ptr,
                                               librdf_uri_as_string(uri));

      dg = rasqal_new_data_graph_from_uri(world->rasqal_world_ptr,
                                          source_uri, source_uri,
                                          RASQAL_DATA_GRAPH_NAMED,
                                          NULL, NULL, NULL);
      rasqal_query_add_data_graph(rdf_query, dg);

      raptor_free_uri(source_uri);
      librdf_iterator_next(cit);
    }
    librdf_free_iterator(cit);
  }

#ifdef RASQAL_TRIPLES_SOURCE_MIN_VERSION
  rts->version = 1;
#endif

  rts->init_triples_match=rasqal_redland_init_triples_match;
  rts->triple_present=rasqal_redland_triple_present;
  rts->free_triples_source=rasqal_redland_free_triples_source;

  return 0;
}