/** * librdf_new_world: * * Create a new Redland execution environment. * * Once this constructor is called to build a #librdf_world object * several functions may be called to set some parameters such as * librdf_world_set_error(), librdf_world_set_warning(), * librdf_world_set_logger(), librdf_world_set_digest(), * librdf_world_set_feature(). * * The world object needs initializing using librdf_world_open() * whether or not the above functions are called. It will be * automatically called by all object constructors in Redland 1.0.6 * or later, but for earlier versions it MUST be called before using * any other part of Redland. * * Returns: a new #librdf_world or NULL on failure */ librdf_world* librdf_new_world(void) { librdf_world *world; #ifdef HAVE_GETTIMEOFDAY struct timeval tv; struct timezone tz; #endif world = (librdf_world*)LIBRDF_CALLOC(librdf_world, sizeof(*world), 1); if(!world) return NULL; #ifdef HAVE_GETTIMEOFDAY if(!gettimeofday(&tv, &tz)) { world->genid_base = tv.tv_sec; } else world->genid_base = 1; #else world->genid_base = 1; #endif world->genid_counter = 1; #ifdef MODULAR_LIBRDF world->ltdl_opened = !(lt_dlinit()); if (world->ltdl_opened) lt_dlsetsearchpath(REDLAND_MODULE_PATH); else LIBRDF_DEBUG1("lt_dlinit() failed\n"); #endif return world; }
/** * librdf_get_storage_factory - Get a storage factory by name * @name: the factory name or NULL for the default factory * * Return value: the factory object or NULL if there is no such factory **/ librdf_storage_factory* librdf_get_storage_factory (const char *name) { librdf_storage_factory *factory; /* return 1st storage if no particular one wanted - why? */ if(!name) { factory=storages; if(!factory) { LIBRDF_DEBUG1("No (default) storages registered\n"); return NULL; } } else { for(factory=storages; factory; factory=factory->next) { if(!strcmp(factory->name, name)) { break; } } /* else FACTORY name not found */ if(!factory) { LIBRDF_DEBUG2("No storage with name %s found\n", name); return NULL; } } return factory; }
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; }
static rasqal_triple_parts rasqal_redland_bind_match(struct rasqal_triples_match_s* rtm, void *user_data, rasqal_variable* bindings[4], rasqal_triple_parts parts) { rasqal_redland_triples_match_context* rtmc=(rasqal_redland_triples_match_context*)rtm->user_data; rasqal_literal* l; librdf_statement* statement; rasqal_triple_parts result=(rasqal_triple_parts)0; librdf_world *world = rtmc->stream->world; statement=librdf_stream_get_object(rtmc->stream); if(!statement) return (rasqal_triple_parts)0; #if defined(LIBRDF_DEBUG) && LIBRDF_DEBUG > 1 LIBRDF_DEBUG1(" matched statement "); librdf_statement_print(statement, stderr); fputc('\n', stderr); #endif /* set 1 or 2 variable values from the fields of statement */ if(bindings[0] && (parts & RASQAL_TRIPLE_SUBJECT)) { #if defined(LIBRDF_DEBUG) && LIBRDF_DEBUG > 1 LIBRDF_DEBUG1("binding subject to variable\n"); #endif l = redland_node_to_rasqal_literal(world, librdf_statement_get_subject(statement)); rasqal_variable_set_value(bindings[0], l); result= RASQAL_TRIPLE_SUBJECT; } if(bindings[1] && (parts & RASQAL_TRIPLE_PREDICATE)) { if(bindings[0] == bindings[1]) { /* check matching(?x, ?x, ...) / subject=predicate */ if(!librdf_node_equals(librdf_statement_get_subject(statement), librdf_statement_get_predicate(statement))) return (rasqal_triple_parts)0; #if defined(LIBRDF_DEBUG) && LIBRDF_DEBUG > 1 LIBRDF_DEBUG1("subject and predicate values match\n"); #endif } else { #if defined(LIBRDF_DEBUG) && LIBRDF_DEBUG > 1 LIBRDF_DEBUG1("binding predicate to variable\n"); #endif l = redland_node_to_rasqal_literal(world, librdf_statement_get_predicate(statement)); rasqal_variable_set_value(bindings[1], l); result= (rasqal_triple_parts)(result | RASQAL_TRIPLE_PREDICATE); } } if(bindings[2] && (parts & RASQAL_TRIPLE_OBJECT)) { int bind=1; if(bindings[0] == bindings[2]) { /* check matching (?x, ..., ?x) / subject=object */ if(!librdf_node_equals(librdf_statement_get_subject(statement), librdf_statement_get_object(statement))) return (rasqal_triple_parts)0; bind=0; #if defined(LIBRDF_DEBUG) && LIBRDF_DEBUG > 1 LIBRDF_DEBUG1("subject and object values match\n"); #endif } if((bindings[1] == bindings[2]) && !(bindings[0] == bindings[1])) { /* check matching (..., ?x, ?x) / predicate=object * Don't do this if matching (?x, ?x, ...) / subject=predicate * was already done since that would mean the match was (?x, ?x, ?x) */ if(!librdf_node_equals(librdf_statement_get_predicate(statement), librdf_statement_get_object(statement))) return (rasqal_triple_parts)0; bind=0; #if defined(LIBRDF_DEBUG) && LIBRDF_DEBUG > 1 LIBRDF_DEBUG1("predicate and object values match\n"); #endif } if(bind) { #if defined(LIBRDF_DEBUG) && LIBRDF_DEBUG > 1 LIBRDF_DEBUG1("binding object to variable\n"); #endif l = redland_node_to_rasqal_literal(world, librdf_statement_get_object(statement)); rasqal_variable_set_value(bindings[2], l); result= (rasqal_triple_parts)(result | RASQAL_TRIPLE_OBJECT); } } /* Contexts */ if(bindings[3] && (parts & RASQAL_TRIPLE_ORIGIN)) { int bind=1; librdf_node* context_node = librdf_stream_get_context2(rtmc->stream); if(bindings[0] == bindings[3]) { /* check matching (?x, ..., ...) in context ?x */ if(!librdf_node_equals(librdf_statement_get_subject(statement), context_node)) return (rasqal_triple_parts)0; bind=0; #if defined(LIBRDF_DEBUG) && LIBRDF_DEBUG > 1 LIBRDF_DEBUG1("subject and context values match\n"); #endif } if(bindings[1] == bindings[3]) { /* check matching (..., ?x, ...) in context ?x */ if(!librdf_node_equals(librdf_statement_get_predicate(statement), context_node)) return (rasqal_triple_parts)0; bind=0; #if defined(LIBRDF_DEBUG) && LIBRDF_DEBUG > 1 LIBRDF_DEBUG1("predicate and context values match\n"); #endif } if(bindings[2] == bindings[3]) { /* check matching (..., ..., ?x) in context ?x */ if(!librdf_node_equals(librdf_statement_get_object(statement), context_node)) return (rasqal_triple_parts)0; bind=0; #if defined(LIBRDF_DEBUG) && LIBRDF_DEBUG > 1 LIBRDF_DEBUG1("object and context values match\n"); #endif } if(bind) { #if defined(LIBRDF_DEBUG) && LIBRDF_DEBUG > 1 LIBRDF_DEBUG1("binding origin to variable\n"); #endif if(context_node) l = redland_node_to_rasqal_literal(world, context_node); else l=NULL; rasqal_variable_set_value(bindings[3], l); result= (rasqal_triple_parts)(result | RASQAL_TRIPLE_ORIGIN); } } return result; }
/** * librdf_get_serializer_factory: * @world: redland world object * @name: the name of the factory (or NULL or empty string if don't care) * @mime_type: the MIME type of the syntax (NULL or empty string if not used) * @type_uri: URI of syntax (NULL if not used) * * Get a serializer factory by name. * * If all fields are NULL, this means any parser supporting * MIME Type "application/rdf+xml" * * Return value: the factory or NULL if not found **/ librdf_serializer_factory* librdf_get_serializer_factory(librdf_world *world, const char *name, const char *mime_type, librdf_uri *type_uri) { librdf_serializer_factory *factory; librdf_world_open(world); if(name && !*name) name=NULL; if(!mime_type || (mime_type && !*mime_type)) { if(!name && !type_uri) name="rdfxml"; else mime_type=NULL; } /* return 1st serializer if no particular one wanted */ if(!name && !mime_type && !type_uri) { factory=(librdf_serializer_factory*)raptor_sequence_get_at(world->serializers, 0); if(!factory) { LIBRDF_DEBUG1("No serializers available\n"); return NULL; } } else { int i; for(i=0; (factory=(librdf_serializer_factory*)raptor_sequence_get_at(world->serializers, i)); i++) { /* next if name does not match */ if(name && strcmp(factory->name, name)) continue; /* MIME type may need to match */ if(mime_type) { if(!factory->mime_type) continue; if(strcmp(factory->mime_type, mime_type)) continue; } /* URI may need to match */ if(type_uri) { if(!factory->type_uri) continue; if(!librdf_uri_equals(factory->type_uri, type_uri)) continue; } /* found it */ break; } /* else FACTORY with given arguments not found */ if(!factory) return NULL; } return factory; }