int add_triple_typed(librdf_world *world, librdf_node *context, librdf_model* model, const char *s, const char *p, const char *o) { librdf_node *subject, *predicate, *object; librdf_statement* statement=NULL; int rc; if(librdf_heuristic_is_blank_node(s)) subject=librdf_new_node_from_blank_identifier(world, (const unsigned char *)librdf_heuristic_get_blank_node(s)); else subject=librdf_new_node_from_uri_string(world, (const unsigned char *)s); predicate=librdf_new_node_from_uri_string(world, (const unsigned char *)p); if(librdf_heuristic_is_blank_node(o)) object=librdf_new_node_from_blank_identifier(world, (const unsigned char *)librdf_heuristic_get_blank_node(o)); else object=librdf_new_node_from_literal(world, (const unsigned char *)o, NULL, 0); statement=librdf_new_statement(world); librdf_statement_set_subject(statement, subject); librdf_statement_set_predicate(statement, predicate); librdf_statement_set_object(statement, object); rc=librdf_model_context_add_statement(model, context, statement); librdf_free_statement(statement); return rc; }
static librdf_statement* librdf_storage_tstore_statement_from_rs_triple(librdf_world* world, rs_triple *triple) { librdf_node *subject_node; librdf_node *predicate_node; librdf_node *object_node; if(triple->subject) { if(!strncmp(triple->subject, "_:",2)) subject_node=librdf_new_node_from_blank_identifier(world, (const unsigned char *)triple->subject+2); else subject_node=librdf_new_node_from_uri_string(world, (const unsigned char *)triple->subject); if(!subject_node) return NULL; } else subject_node=NULL; if(triple->predicate) { predicate_node=librdf_new_node_from_uri_string(world, (const unsigned char *)triple->predicate); if(!predicate_node) { librdf_free_node(subject_node); return NULL; } } else predicate_node=NULL; if(triple->object) { if(triple->literal) object_node=librdf_new_node_from_typed_literal(world, (const unsigned char *)triple->object, NULL, NULL); else if(!strncmp(triple->object, ":", 2)) object_node=librdf_new_node_from_blank_identifier(world, (const unsigned char *)triple->object+2); else object_node=librdf_new_node_from_uri_string(world, (const unsigned char *)triple->object); if(!object_node) { librdf_free_node(subject_node); librdf_free_node(predicate_node); return NULL; } } else object_node=NULL; return librdf_new_statement_from_nodes(world, subject_node, predicate_node, object_node); }
librdf_node* RdfStorePrivate::RdfNodeToLibRdfNode(RdfNode node) const { librdf_node* newNode = nullptr; switch (node.GetType()) { case RdfNode::NOTHING: break; case RdfNode::BLANK: newNode = librdf_new_node_from_blank_identifier(m_World, (const unsigned char*) node.GetValue().c_str()); break; case RdfNode::LITERAL: { if (node.GetDatatype() != RdfUri()) { librdf_uri* typeUri = RdfUriToLibRdfUri(node.GetDatatype()); newNode = librdf_new_node_from_typed_literal(m_World, (const unsigned char*) node.GetValue().c_str(), nullptr, typeUri); } else { newNode = librdf_new_node_from_literal(m_World, (const unsigned char*) node.GetValue().c_str(), nullptr, 0); } } break; case RdfNode::URI: newNode = librdf_new_node_from_uri( m_World, librdf_new_uri(m_World, (const unsigned char*) node.GetValue().c_str()) ); break; default: break; } return newNode; }
static librdf_node* rasqal_literal_to_redland_node(librdf_world *world, rasqal_literal* l) { rasqal_literal_type type; if(!l) return NULL; /* FIXME: Workaround for Issue #0000519 * http://bugs.librdf.org/mantis/view.php?id=519 * * Remove this 'if' when RASQAL_MIN_VERSION is 0.9.30 or larger */ if(l->type == RASQAL_LITERAL_INTEGER_SUBTYPE) type = RASQAL_LITERAL_STRING; else type = rasqal_literal_get_rdf_term_type(l); if(type == RASQAL_LITERAL_URI) return librdf_new_node_from_uri(world, (librdf_uri*)l->value.uri); else if (type == RASQAL_LITERAL_STRING) return librdf_new_node_from_typed_literal(world, (unsigned char*)l->string, l->language, (librdf_uri*)l->datatype); else if (type == RASQAL_LITERAL_BLANK) return librdf_new_node_from_blank_identifier(world, (unsigned char*)l->string); LIBRDF_DEBUG2("Could not convert literal type %d to librdf_node\n", type); return NULL; }
/** * librdf_new_node: * @world: redland world object * * Constructor - create a new #librdf_node object with a private identifier. * * Calls librdf_new_node_from_blank_identifier(world, NULL) to * construct a new redland blank node identifier and make a * new librdf_node object for it. * * Return value: a new #librdf_node object or NULL on failure **/ librdf_node* librdf_new_node(librdf_world *world) { LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(world, librdf_world, NULL); librdf_world_open(world); return librdf_new_node_from_blank_identifier(world, (unsigned char*)NULL); }
/* * 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"); } }
static int librdf_query_rasqal_query_results_update_statement(void* context) { librdf_query_rasqal_stream_context* scontext=(librdf_query_rasqal_stream_context*)context; librdf_world* world=scontext->query->world; librdf_node* node; raptor_statement *rstatement=rasqal_query_results_get_triple(scontext->qcontext->results); if(!rstatement) return 1; scontext->statement=librdf_new_statement(world); if(!scontext->statement) return 1; /* subject */ if(rstatement->subject->type == RAPTOR_TERM_TYPE_BLANK) { node = librdf_new_node_from_blank_identifier(world, rstatement->subject->value.blank.string); } else if(rstatement->subject->type == RAPTOR_TERM_TYPE_URI) { node = librdf_new_node_from_uri_string(world, librdf_uri_as_string((librdf_uri*)rstatement->subject->value.uri)); } else { librdf_log(world, 0, LIBRDF_LOG_ERROR, LIBRDF_FROM_QUERY, NULL, "Unknown Raptor subject identifier type %d", rstatement->subject->type); goto fail; } if(!node) { librdf_log(world, 0, LIBRDF_LOG_ERROR, LIBRDF_FROM_QUERY, NULL, "Could not create subject node"); goto fail; } librdf_statement_set_subject(scontext->statement, node); /* predicate */ if(rstatement->predicate->type == RAPTOR_TERM_TYPE_URI) { node = librdf_new_node_from_uri_string(world, librdf_uri_as_string((librdf_uri*)rstatement->predicate->value.uri)); } else { librdf_log(world, 0, LIBRDF_LOG_ERROR, LIBRDF_FROM_QUERY, NULL, "Unknown Raptor predicate identifier type %d", rstatement->predicate->type); goto fail; } if(!node) { librdf_log(world, 0, LIBRDF_LOG_ERROR, LIBRDF_FROM_QUERY, NULL, "Could not create predicate node"); goto fail; } librdf_statement_set_predicate(scontext->statement, node); /* object */ if(rstatement->object->type == RAPTOR_TERM_TYPE_LITERAL) { node = librdf_new_node_from_typed_literal(world, rstatement->object->value.literal.string, (const char*)rstatement->object->value.literal.language, (librdf_uri*)rstatement->object->value.literal.datatype); } else if(rstatement->object->type == RAPTOR_TERM_TYPE_BLANK) { node = librdf_new_node_from_blank_identifier(world, rstatement->object->value.blank.string); } else if(rstatement->object->type == RAPTOR_TERM_TYPE_URI) { node = librdf_new_node_from_uri_string(world, librdf_uri_as_string((librdf_uri*)rstatement->object->value.uri)); } else { librdf_log(world, 0, LIBRDF_LOG_ERROR, LIBRDF_FROM_PARSER, NULL, "Unknown Raptor object identifier type %d", rstatement->object->type); goto fail; } if(!node) { librdf_log(world, 0, LIBRDF_LOG_ERROR, LIBRDF_FROM_QUERY, NULL, "Could not create object node"); goto fail; } librdf_statement_set_object(scontext->statement, node); return 0; /* success */ fail: librdf_free_statement(scontext->statement); scontext->statement=NULL; return 1; }
/* * Convert the given Ruby +object+ (VALUE) to a librdf_node. */ librdf_node * rleaf_value_to_librdf_node( VALUE object ) { VALUE str, typeuristr, converted_pair; librdf_uri *typeuri; ID id; /* :TODO: how to set language? is_xml flag? */ // rleaf_log( "debug", "Converting %s to a librdf_node.", RSTRING_PTR(rb_inspect( object )) ); switch( TYPE(object) ) { /* nil -> bnode */ case T_NIL: return NULL; case T_SYMBOL: id = SYM2ID( object ); if ( id == rleaf_anon_bnodeid ) { return librdf_new_node_from_blank_identifier( rleaf_rdf_world, NULL ); } else { return librdf_new_node_from_blank_identifier( rleaf_rdf_world, (unsigned char *)rb_id2name(id) ); } /* String -> plain literal */ case T_STRING: return librdf_new_node_from_literal( rleaf_rdf_world, (unsigned char *)RSTRING_PTR(object), NULL, 0 ); break; /* Float -> xsd:float */ case T_FLOAT: str = rb_obj_as_string( object ); typeuri = XSD_FLOAT_TYPE; break; /* Bignum -> xsd:decimal */ case T_BIGNUM: str = rb_obj_as_string( object ); typeuri = XSD_DECIMAL_TYPE; break; /* Fixnum -> xsd:integer */ case T_FIXNUM: str = rb_obj_as_string( object ); typeuri = XSD_INTEGER_TYPE; break; /* TrueClass/FalseClass -> xsd:boolean */ case T_TRUE: case T_FALSE: str = rb_obj_as_string( object ); typeuri = XSD_BOOLEAN_TYPE; break; /* URI -> librdf_uri */ case T_OBJECT: if ( IsURI(object) || IsNamespace(object) ) { // rleaf_log( "debug", "Converting %s object to librdf_uri node", // rb_obj_classname(object) ); str = rb_obj_as_string( object ); return librdf_new_node_from_uri_string( rleaf_rdf_world, (unsigned char*)RSTRING_PTR(str) ); } /* fallthrough */ /* Delegate anything else to Redleaf::NodeUtils.make_object_typed_literal */ default: converted_pair = rb_funcall( rleaf_mRedleafNodeUtils, rb_intern("make_object_typed_literal"), 1, object ); str = rb_obj_as_string( rb_ary_entry(converted_pair, 0) ); typeuristr = rb_obj_as_string( rb_ary_entry(converted_pair, 1) ); typeuri = librdf_new_uri( rleaf_rdf_world, (unsigned char*)RSTRING_PTR(typeuristr) ); } return librdf_new_node_from_typed_counted_literal( rleaf_rdf_world, (unsigned char *)RSTRING_PTR(str), RSTRING_LEN(str), NULL, 0, typeuri ); }