static void roqet_graph_pattern_walk(rasqal_graph_pattern *gp, int gp_index, FILE *fh, int indent) { int triple_index = 0; rasqal_graph_pattern_operator op; int seen; raptor_sequence *seq; int idx; rasqal_expression* expr; rasqal_variable* var; rasqal_literal* literal; op = rasqal_graph_pattern_get_operator(gp); roqet_write_indent(fh, indent); fprintf(fh, "%s graph pattern", rasqal_graph_pattern_operator_as_string(op)); idx = rasqal_graph_pattern_get_index(gp); if(idx >= 0) fprintf(fh, "[%d]", idx); if(gp_index >= 0) fprintf(fh, " #%d", gp_index); fputs(" {\n", fh); indent += 2; /* look for LET variable and value */ var = rasqal_graph_pattern_get_variable(gp); if(var) { roqet_write_indent(fh, indent); fprintf(fh, "%s := ", var->name); rasqal_expression_print(var->expression, fh); } /* look for GRAPH literal */ literal = rasqal_graph_pattern_get_origin(gp); if(literal) { roqet_write_indent(fh, indent); fputs("origin ", fh); rasqal_literal_print(literal, fh); fputc('\n', fh); } /* look for SERVICE literal */ literal = rasqal_graph_pattern_get_service(gp); if(literal) { roqet_write_indent(fh, indent); rasqal_literal_print(literal, fh); fputc('\n', fh); } /* look for triples */ seen = 0; while(1) { rasqal_triple* t; t = rasqal_graph_pattern_get_triple(gp, triple_index); if(!t) break; if(!seen) { roqet_write_indent(fh, indent); fputs("triples {\n", fh); seen = 1; } roqet_write_indent(fh, indent + 2); fprintf(fh, "triple #%d { ", triple_index); rasqal_triple_print(t, fh); fputs(" }\n", fh); triple_index++; } if(seen) { roqet_write_indent(fh, indent); fputs("}\n", fh); } /* look for sub-graph patterns */ seq = rasqal_graph_pattern_get_sub_graph_pattern_sequence(gp); if(seq && raptor_sequence_size(seq) > 0) { roqet_write_indent(fh, indent); fprintf(fh, "sub-graph patterns (%d) {\n", raptor_sequence_size(seq)); gp_index = 0; while(1) { rasqal_graph_pattern* sgp; sgp = rasqal_graph_pattern_get_sub_graph_pattern(gp, gp_index); if(!sgp) break; roqet_graph_pattern_walk(sgp, gp_index, fh, indent + 2); gp_index++; } roqet_write_indent(fh, indent); fputs("}\n", fh); } /* look for filter */ expr = rasqal_graph_pattern_get_filter_expression(gp); if(expr) { roqet_write_indent(fh, indent); fputs("filter { ", fh); rasqal_expression_print(expr, fh); fputs("}\n", fh); } indent -= 2; roqet_write_indent(fh, indent); fputs("}\n", fh); }
static void rasqal_query_write_sparql_graph_pattern(sparql_writer_context *wc, raptor_iostream* iostr, rasqal_graph_pattern *gp, int gp_index, int indent) { int triple_index = 0; rasqal_graph_pattern_operator op; raptor_sequence *seq; int filters_count = 0; int want_braces = 1; op = rasqal_graph_pattern_get_operator(gp); if(op == RASQAL_GRAPH_PATTERN_OPERATOR_SELECT) { raptor_sequence* vars_seq; rasqal_graph_pattern* where_gp; raptor_iostream_counted_string_write("SELECT", 6, iostr); vars_seq = rasqal_projection_get_variables_sequence(gp->projection); rasqal_query_write_sparql_select(wc, iostr, vars_seq); raptor_iostream_write_byte('\n', iostr); rasqal_query_write_indent(iostr, indent); raptor_iostream_counted_string_write("WHERE ", 6, iostr); where_gp = rasqal_graph_pattern_get_sub_graph_pattern(gp, 0); rasqal_query_write_sparql_graph_pattern(wc, iostr, where_gp, 0, indent); /* FIXME - not implemented: modifiers */ return; } if(op == RASQAL_GRAPH_PATTERN_OPERATOR_LET) { /* LAQRS */ raptor_iostream_counted_string_write("LET (", 5, iostr); rasqal_query_write_sparql_variable(wc, iostr, gp->var); raptor_iostream_counted_string_write(" := ", 4, iostr); rasqal_query_write_sparql_expression(wc, iostr, gp->filter_expression); raptor_iostream_counted_string_write(") .", 3, iostr); return; } if(op == RASQAL_GRAPH_PATTERN_OPERATOR_SERVICE) { rasqal_graph_pattern* service_gp; /* LAQRS */ raptor_iostream_counted_string_write("SERVICE ", 8, iostr); rasqal_query_write_sparql_literal(wc, iostr, gp->origin); raptor_iostream_counted_string_write(" ", 1, iostr); service_gp = rasqal_graph_pattern_get_sub_graph_pattern(gp, 0); rasqal_query_write_sparql_graph_pattern(wc, iostr, service_gp, 0, indent); return; } if(op == RASQAL_GRAPH_PATTERN_OPERATOR_OPTIONAL || op == RASQAL_GRAPH_PATTERN_OPERATOR_GRAPH) { /* prefix verbs */ if(op == RASQAL_GRAPH_PATTERN_OPERATOR_OPTIONAL) raptor_iostream_counted_string_write("OPTIONAL ", 9, iostr); else { raptor_iostream_counted_string_write("GRAPH ", 6, iostr); rasqal_query_write_sparql_literal(wc, iostr, gp->origin); raptor_iostream_write_byte(' ', iostr); } } if(gp->op == RASQAL_GRAPH_PATTERN_OPERATOR_FILTER) want_braces = 0; if(want_braces) { raptor_iostream_counted_string_write("{\n", 2, iostr); indent += 2; } /* look for triples */ while(1) { rasqal_triple* t = rasqal_graph_pattern_get_triple(gp, triple_index); if(!t) break; rasqal_query_write_indent(iostr, indent); rasqal_query_write_sparql_triple(wc, iostr, t); raptor_iostream_write_byte('\n', iostr); triple_index++; } /* look for sub-graph patterns */ seq = rasqal_graph_pattern_get_sub_graph_pattern_sequence(gp); if(seq && raptor_sequence_size(seq) > 0) { for(gp_index = 0; 1; gp_index++) { rasqal_graph_pattern* sgp; sgp = rasqal_graph_pattern_get_sub_graph_pattern(gp, gp_index); if(!sgp) break; if(sgp->op == RASQAL_GRAPH_PATTERN_OPERATOR_FILTER) { filters_count++; continue; } if(!gp_index) rasqal_query_write_indent(iostr, indent); else { if(op == RASQAL_GRAPH_PATTERN_OPERATOR_UNION) /* infix verb */ raptor_iostream_counted_string_write(" UNION ", 7, iostr); else { /* must be prefix verb */ raptor_iostream_write_byte('\n', iostr); rasqal_query_write_indent(iostr, indent); } } rasqal_query_write_sparql_graph_pattern(wc, iostr, sgp, gp_index, indent); } raptor_iostream_write_byte('\n', iostr); } /* look for constraints */ if(filters_count > 0) { for(gp_index = 0; 1; gp_index++) { rasqal_graph_pattern* sgp; rasqal_expression* expr; sgp = rasqal_graph_pattern_get_sub_graph_pattern(gp, gp_index); if(!sgp) break; if(sgp->op != RASQAL_GRAPH_PATTERN_OPERATOR_FILTER) continue; expr = rasqal_graph_pattern_get_filter_expression(sgp); rasqal_query_write_indent(iostr, indent); raptor_iostream_counted_string_write("FILTER( ", 8, iostr); rasqal_query_write_sparql_expression(wc, iostr, expr); raptor_iostream_counted_string_write(" )\n", 3, iostr); } } if(want_braces) { indent -= 2; rasqal_query_write_indent(iostr, indent); raptor_iostream_write_byte('}', iostr); } }