Exemplo n.º 1
0
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);
  }

}
Exemplo n.º 2
0
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);
}
Exemplo n.º 3
0
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;

}