/* * rasqal_expression_evaluate_bound: * @e: The expression to evaluate. * @eval_context: Evaluation context * * INTERNAL - Evaluate RASQAL_EXPR_BOUND (variable) expressions. * * Return value: A #rasqal_literal boolean value or NULL on failure. */ static rasqal_literal* rasqal_expression_evaluate_bound(rasqal_expression *e, rasqal_evaluation_context *eval_context, int *error_p) { rasqal_world* world = eval_context->world; rasqal_literal* l1; rasqal_variable* v; /* Do not use rasqal_expression_evaluate() here since * we need to check the argument is a variable, and * that function will flatten such thing to literals * as early as possible. See (FLATTEN_LITERAL) below */ if(!e->arg1 || e->arg1->op != RASQAL_EXPR_LITERAL) goto failed; l1 = e->arg1->literal; if(!l1 || l1->type != RASQAL_LITERAL_VARIABLE) goto failed; v = rasqal_literal_as_variable(l1); if(!v) goto failed; return rasqal_new_boolean_literal(world, (v->value != NULL)); failed: if(error_p) *error_p = 1; return NULL; }
/* * rasqal_expression_evaluate_lang: * @e: The expression to evaluate. * @eval_context: Evaluation context * * INTERNAL - Evaluate RASQAL_EXPR_LANG (literal expr) expression. * * Return value: A #rasqal_literal value or NULL on failure. */ static rasqal_literal* rasqal_expression_evaluate_lang(rasqal_expression *e, rasqal_evaluation_context *eval_context, int *error_p) { rasqal_world* world = eval_context->world; rasqal_literal* l1; int free_literal = 1; rasqal_variable* v = NULL; unsigned char* new_s; l1 = rasqal_expression_evaluate2(e->arg1, eval_context, error_p); if(*error_p || !l1) goto failed; v = rasqal_literal_as_variable(l1); if(v) { rasqal_free_literal(l1); l1 = v->value; /* don't need v after this */ free_literal = 0; if(!l1) goto failed; } if(rasqal_literal_get_rdf_term_type(l1) != RASQAL_LITERAL_STRING) goto failed; if(l1->language) { size_t len = strlen(l1->language); new_s = (unsigned char*)RASQAL_MALLOC(cstring, len + 1); if(!new_s) goto failed; memcpy((char*)new_s, l1->language, len + 1); } else { new_s = (unsigned char*)RASQAL_MALLOC(cstring, 1); if(!new_s) goto failed; *new_s = '\0'; } if(free_literal) rasqal_free_literal(l1); /* after this new_s is owned by result */ return rasqal_new_string_literal(world, new_s, NULL, NULL, NULL); failed: if(error_p) *error_p = 1; if(free_literal) rasqal_free_literal(l1); return NULL; }
/* * rasqal_expression_evaluate_datatype: * @e: The expression to evaluate. * @eval_context: Evaluation context * * INTERNAL - Evaluate RASQAL_EXPR_DATATYPE (string literal) expression. * * Return value: A #rasqal_literal URI value or NULL on failure. */ static rasqal_literal* rasqal_expression_evaluate_datatype(rasqal_expression *e, rasqal_evaluation_context *eval_context, int *error_p) { rasqal_world* world = eval_context->world; rasqal_literal* l1; int free_literal = 1; rasqal_variable* v = NULL; raptor_uri* dt_uri = NULL; l1 = rasqal_expression_evaluate2(e->arg1, eval_context, error_p); if(*error_p || !l1) goto failed; v = rasqal_literal_as_variable(l1); if(v) { rasqal_free_literal(l1); l1 = v->value; /* don't need v after this */ free_literal = 0; if(!l1) goto failed; } if(rasqal_literal_get_rdf_term_type(l1) != RASQAL_LITERAL_STRING) goto failed; if(l1->language) goto failed; /* The datatype of a plain literal is xsd:string */ dt_uri = l1->datatype; if(!dt_uri && l1->type == RASQAL_LITERAL_STRING) dt_uri = rasqal_xsd_datatype_type_to_uri(l1->world, RASQAL_LITERAL_XSD_STRING); if(!dt_uri) goto failed; dt_uri = raptor_uri_copy(dt_uri); if(free_literal) rasqal_free_literal(l1); /* after this dt_uri is owned by result */ return rasqal_new_uri_literal(world, dt_uri); failed: if(error_p) *error_p = 1; if(free_literal) rasqal_free_literal(l1); return NULL; }
rasqal_triples_match* rasqal_new_triples_match(rasqal_query* query, rasqal_triples_source* triples_source, rasqal_triple_meta *m, rasqal_triple *t) { rasqal_triples_match* rtm; if(!triples_source) return NULL; rtm = (rasqal_triples_match *)RASQAL_CALLOC(rasqal_triples_match, 1, sizeof(rasqal_triples_match)); if(rtm) { rtm->world = query->world; /* exact if there are no variables in the triple parts */ rtm->is_exact = 1; if(rasqal_literal_as_variable(t->predicate) || rasqal_literal_as_variable(t->subject) || rasqal_literal_as_variable(t->object)) rtm->is_exact = 0; if(rtm->is_exact) { if(!triples_source->triple_present(triples_source, triples_source->user_data, t)) { rasqal_free_triples_match(rtm); rtm = NULL; } } else { if(triples_source->init_triples_match(rtm, triples_source, triples_source->user_data, m, t)) { rasqal_free_triples_match(rtm); rtm = NULL; } } } return rtm; }
/* * rasqal_expression_evaluate_istype: * @e: The expression to evaluate. * @eval_context: Evaluation context * * INTERNAL - Evaluate RASQAL_EXPR_ISBLANK, RASQAL_EXPR_ISURI, * RASQAL_EXPR_ISLITERAL and RASQAL_EXPR_ISNUMERIC (expr) * expressions. * * Return value: A #rasqal_literal boolean value or NULL on failure. */ static rasqal_literal* rasqal_expression_evaluate_istype(rasqal_expression *e, rasqal_evaluation_context *eval_context, int *error_p) { rasqal_world* world = eval_context->world; rasqal_literal* l1; int free_literal = 1; rasqal_variable *v; int b; l1 = rasqal_expression_evaluate2(e->arg1, eval_context, error_p); if(*error_p || !l1) goto failed; v = rasqal_literal_as_variable(l1); if(v) { rasqal_free_literal(l1); l1 = v->value; /* don't need v after this */ free_literal = 0; if(!l1) goto failed; } if(e->op == RASQAL_EXPR_ISBLANK) b = (l1->type == RASQAL_LITERAL_BLANK); else if(e->op == RASQAL_EXPR_ISLITERAL) b = (rasqal_literal_get_rdf_term_type(l1) == RASQAL_LITERAL_STRING); else if(e->op == RASQAL_EXPR_ISURI) b =(l1->type == RASQAL_LITERAL_URI); else b = (rasqal_literal_is_numeric(l1)); if(free_literal) rasqal_free_literal(l1); return rasqal_new_boolean_literal(world, b); failed: if(error_p) *error_p = 1; if(free_literal && l1) rasqal_free_literal(l1); return NULL; }
/* * rasqal_expression_evaluate_lang: * @e: The expression to evaluate. * @eval_context: Evaluation context * * INTERNAL - Evaluate RASQAL_EXPR_LANG (literal expr) expression. * * Return value: A #rasqal_literal value or NULL on failure. */ static rasqal_literal* rasqal_expression_evaluate_lang(rasqal_expression *e, rasqal_evaluation_context *eval_context, int *error_p) { rasqal_world* world = eval_context->world; rasqal_literal* l1; int free_literal = 1; rasqal_variable* v = NULL; unsigned char* new_s; l1 = rasqal_expression_evaluate2(e->arg1, eval_context, error_p); if(*error_p || !l1) goto failed; v = rasqal_literal_as_variable(l1); if(v) { rasqal_free_literal(l1); l1 = v->value; /* don't need v after this */ free_literal = 0; if(!l1) goto failed; } if(rasqal_literal_get_rdf_term_type(l1) != RASQAL_LITERAL_STRING) goto failed; if(l1->language) { size_t len = strlen(l1->language); new_s = RASQAL_MALLOC(unsigned char*, len + 1); if(!new_s) goto failed; memcpy(RASQAL_GOOD_CAST(char*, new_s), l1->language, len + 1); } else {
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_rowsource* rasqal_algebra_graph_algebra_node_to_rowsource(rasqal_engine_algebra_data* execution_data, rasqal_algebra_node* node, rasqal_engine_error *error_p) { rasqal_query *query = execution_data->query; rasqal_rowsource *rs; rasqal_literal *graph = node->graph; rasqal_variable* v; if(!graph) { RASQAL_DEBUG1("graph algebra node has NULL graph\n"); return NULL; } /* This code checks that #1-#3 below are present and then executes parts #1 and #2 here. The graph rowsource created by rasqal_new_graph_rowsource() executes #3 http://www.w3.org/TR/2008/REC-rdf-sparql-query-20080115/#sparqlAlgebraEval SPARQL Query Language for RDF - Evaluation of a Graph Pattern #1 if IRI is a graph name in D eval(D(G), Graph(IRI,P)) = eval(D(D[IRI]), P) #2 if IRI is not a graph name in D eval(D(G), Graph(IRI,P)) = the empty multiset #3 eval(D(G), Graph(var,P)) = Let R be the empty multiset foreach IRI i in D R := Union(R, Join( eval(D(D[i]), P) , Omega(?var->i) ) the result is R */ v = rasqal_literal_as_variable(graph); if(!v && graph->type != RASQAL_LITERAL_URI) { /* value is neither a variable or URI literal - error */ RASQAL_DEBUG1("graph algebra node is neither variable or URI\n"); return NULL; } if(!v && graph->type == RASQAL_LITERAL_URI) { if(rasqal_query_dataset_contains_named_graph(query, graph->value.uri)) { /* case #1 - IRI is a graph name in D */ /* Set the origin of all triple patterns inside node->node1 to * URI graph->value.uri * * FIXME - this is a hack. The graph URI should be a parameter * to all rowsource constructors. */ rasqal_algebra_node_set_origin(query, node->node1, graph); rs = rasqal_algebra_node_to_rowsource(execution_data, node->node1, error_p); } else { /* case #2 - IRI is not a graph name in D - return empty rowsource */ rasqal_free_algebra_node(node->node1); node->node1 = NULL; rs = rasqal_new_empty_rowsource(query->world, query); } if(!rs || *error_p) rs = NULL; return rs; } /* case #3 - a variable */ rs = rasqal_algebra_node_to_rowsource(execution_data, node->node1, error_p); if(!rs || *error_p) return NULL; return rasqal_new_graph_rowsource(query->world, query, rs, v); }
static void navigator_graph_pattern_load(rasqal_graph_pattern *gp, int gp_index,int indent, GSList** template_query_) { int triple_index = 0; raptor_sequence *seq; rasqal_variable * var; rasqal_triple* t; rasqal_literal_type type; GSList* template_query= *template_query_; indent += 1; /* look for triples */ while(1) { t = rasqal_graph_pattern_get_triple(gp, triple_index); if(!t) break; /* rasqal_literal_type type; printf ("triple\n"); type=rasqal_literal_get_rdf_term_type(t->subject); printf ("S type %d val %s\n", type, rasqal_literal_as_string (t->subject)) ; type=rasqal_literal_get_rdf_term_type(t->predicate); printf ("P type %d val %s\n", type, rasqal_literal_as_string (t->predicate)) ; type=rasqal_literal_get_rdf_term_type(t->object); printf ("O type %d val %s\n", type, rasqal_literal_as_string (t->object)) ; */ //ssTriple_t *ttemp = (ssTriple_t *)g_new0(ssTriple_t,1); ssTriple_t_sparql *ttemp = (ssTriple_t_sparql *)g_new0(ssTriple_t_sparql,1); type=rasqal_literal_get_rdf_term_type(t->subject); //var=rasqal_literal_as_variable(t->subject); //rasqal_variable_print(var, stdout); //printf("\n"); //printf("%s\n", var->name); //rasqal_free_variable (var); if ((type==RASQAL_LITERAL_UNKNOWN) && (rasqal_literal_as_string(t->subject) == NULL)) { //variable , needs a wildcard var=rasqal_literal_as_variable(t->subject); ttemp->subject_var=g_strdup(var->name); rasqal_free_variable (var); ttemp->subject=g_strdup_printf("%s",wildcard1); } else { ttemp->subject=g_strdup(rasqal_literal_as_string (t->subject)); ttemp->subject_var=NULL; } type=rasqal_literal_get_rdf_term_type(t->predicate); if ((type==RASQAL_LITERAL_UNKNOWN) && (rasqal_literal_as_string(t->predicate) == NULL)) { //variable , needs a wildcard var=rasqal_literal_as_variable(t->predicate); ttemp->predicate_var=g_strdup(var->name); rasqal_free_variable (var); ttemp->predicate=g_strdup_printf("%s",wildcard1); } else { ttemp->predicate=g_strdup(rasqal_literal_as_string (t->predicate)); ttemp->predicate_var=NULL; } type=rasqal_literal_get_rdf_term_type(t->object); if ((type==RASQAL_LITERAL_UNKNOWN) && (rasqal_literal_as_string(t->object) == NULL)) { //variable , needs a wildcard var=rasqal_literal_as_variable(t->object); ttemp->object_var=g_strdup(var->name); rasqal_free_variable (var); ttemp->object=g_strdup_printf("%s",wildcard1); } else { if (type==RASQAL_LITERAL_STRING) { ttemp->object=g_strdup(rasqal_literal_as_string (t->object)); ttemp->objType = ssElement_TYPE_LIT; } else { ttemp->object=g_strdup(rasqal_literal_as_string (t->object)); ttemp->objType = ssElement_TYPE_URI; } ttemp->object_var=NULL; } ttemp->gp_index=gp_index; ttemp->indent=indent; //printf("SPARQL Binding triple is: \t%s\t%s\t%s , obj_Type %d \n", ttemp->subject, ttemp->predicate, ttemp->object, ttemp->objType); //printf(" Vars: \t%s\t%s\t%s , gp_index %d indent %d\n", ttemp->subject_var, ttemp->predicate_var, ttemp->object_var,ttemp->gp_index,ttemp->indent); template_query= g_slist_prepend(template_query, ttemp); triple_index++; } /* look for sub-graph patterns */ seq = rasqal_graph_pattern_get_sub_graph_pattern_sequence(gp); if(seq && raptor_sequence_size(seq) > 0) { gp_index = 0; while(1) { rasqal_graph_pattern* sgp; sgp = rasqal_graph_pattern_get_sub_graph_pattern(gp, gp_index); if(!sgp) break; navigator_graph_pattern_load(sgp, gp_index, indent + 1, &template_query); gp_index++; } } indent -= 1; *template_query_=template_query; }