Esempio n. 1
0
static int
rasqal_query_write_sparql_select(sparql_writer_context *wc,
                                 raptor_iostream *iostr, 
                                 raptor_sequence* vars_seq)
{
  int count = raptor_sequence_size(vars_seq);
  int i;
  
  for(i = 0; i < count; i++) {
    rasqal_variable* v = (rasqal_variable*)raptor_sequence_get_at(vars_seq, i);
    raptor_iostream_write_byte(' ', iostr);
    rasqal_query_write_sparql_variable(wc, iostr, v);
  }

  return 0;
}
Esempio n. 2
0
/**
 * rasqal_world_get_query_language_description:
 * @world: world object
 * @counter: index into the list of query languages
 *
 * Get query language descriptive information
 *
 * Return value: description or NULL if counter is out of range
 **/
const raptor_syntax_description*
rasqal_world_get_query_language_description(rasqal_world* world,
                                            unsigned int counter)
{
  rasqal_query_language_factory *factory;

  RASQAL_ASSERT_OBJECT_POINTER_RETURN_VALUE(world, rasqal_world, NULL);

  rasqal_world_open(world);

  factory = (rasqal_query_language_factory *)raptor_sequence_get_at(world->query_languages, counter);
  if(!factory)
    return NULL;

  return &factory->desc;
}
Esempio n. 3
0
/*
 * raptor_turtle_emit_subject_list_items:
 * @serializer: #raptor_serializer object
 * @subject: subject node
 * @depth: depth into tree
 * 
 * Emit an rdf list of items (rdf:li) about a subject node.
 * 
 * Return value: non-0 on failure
 **/
static int
raptor_turtle_emit_subject_list_items(raptor_serializer* serializer,
                                      raptor_abbrev_subject* subject,
                                      int depth)
{
  int rv = 0;
  int i = 0;

  RAPTOR_DEBUG5("Emitting subject list items for node %p refcount %d subject %d object %d\n", 
                subject->node,
                subject->node->ref_count, subject->node->count_as_subject, 
                subject->node->count_as_object);

  while(!rv && i < raptor_sequence_size(subject->list_items)) {
    raptor_abbrev_node* object;
    
    object = (raptor_abbrev_node*)raptor_sequence_get_at(subject->list_items,
                                                          i++);
    if(!object)
      continue;
    
    switch(object->term->type) {
      case RAPTOR_TERM_TYPE_URI:
        rv = raptor_turtle_emit_resource(serializer, object, depth+1);
        break;
          
      case RAPTOR_TERM_TYPE_LITERAL:
        rv = raptor_turtle_emit_literal(serializer, object, depth+1);
        break;
          
      case RAPTOR_TERM_TYPE_BLANK:
        rv = raptor_turtle_emit_blank(serializer, object, depth+1);
        break;

      case RAPTOR_TERM_TYPE_UNKNOWN:
      default:
        raptor_log_error_formatted(serializer->world, RAPTOR_LOG_LEVEL_ERROR,
                                   NULL, "Triple has unsupported term type %d", 
                                   object->term->type);
        break;

    }
    
  }
  
  return rv;
}
Esempio n. 4
0
static rasqal_query_results_format_factory*
rasqal_get_query_results_formatter_factory(rasqal_world* world,
                                           const char *name, raptor_uri* uri,
                                           const char *mime_type,
                                           int flags)
{
  int i;
  rasqal_query_results_format_factory* factory = NULL;
  
  for(i = 0; 1; i++) {
    int factory_flags = 0;
    
    factory = (rasqal_query_results_format_factory*)raptor_sequence_get_at(world->query_results_formats,
                                                                         i);
    if(!factory)
      break;

    if(factory->reader)
      factory_flags |= RASQAL_QUERY_RESULTS_FORMAT_FLAG_READER;
    if(factory->writer)
      factory_flags |= RASQAL_QUERY_RESULTS_FORMAT_FLAG_WRITER;

    /* Flags must match */
    if(flags && factory_flags != flags)
      continue;

    if(!name && !uri)
      /* the default is the first registered format */
      break;
    
    if(name && factory->name &&
       !strcmp(factory->name, (const char*)name))
      return factory;


    if(uri && factory->uri_string &&
       !strcmp((const char*)raptor_uri_as_string(uri),
               (const char*)factory->uri_string))
      break;

    if(mime_type && factory->mime_type &&
       !strcmp(factory->mime_type, (const char*)mime_type))
      return factory;
  }
  
  return factory;
}
Esempio n. 5
0
rasqal_variable*
rasqal_variables_table_get(rasqal_variables_table* vt, int idx)
{
  raptor_sequence* seq = NULL;

  if(idx < 0)
    return NULL;
  
  if(idx < vt->variables_count)
    seq = vt->variables_sequence;
  else {
    idx -= vt->variables_count;
    seq = vt->anon_variables_sequence;
  }
  
  return (rasqal_variable*)raptor_sequence_get_at(seq, idx);
}
Esempio n. 6
0
/* Check the list to see if the node is a duplicate. If not, add it
 * to the list.
 */
static void
raptor_dot_serializer_assert_node(raptor_serializer* serializer,
                                  raptor_identifier_type node_type,
                                  const void* node_data,
                                  raptor_uri* datatype,
                                  const unsigned char* language)
{
  raptor_dot_context* context = (raptor_dot_context*)serializer->context;
  raptor_sequence* seq = NULL;
  int i;

  /* Which list are we searching? */
  switch(node_type) {
    case RAPTOR_IDENTIFIER_TYPE_RESOURCE:
    case RAPTOR_IDENTIFIER_TYPE_PREDICATE:
      seq = context->resources;
      break;

    case RAPTOR_IDENTIFIER_TYPE_ANONYMOUS:
      seq = context->bnodes;
      break;

    case RAPTOR_IDENTIFIER_TYPE_LITERAL:
    case RAPTOR_IDENTIFIER_TYPE_XML_LITERAL:
      seq = context->literals;
      break;

    case RAPTOR_IDENTIFIER_TYPE_UNKNOWN:
    case RAPTOR_IDENTIFIER_TYPE_ORDINAL:
      break;
  }

  for( i = 0 ; i < raptor_sequence_size(seq) ; i++ ) {
    raptor_dot_serializer_node* node =
      (raptor_dot_serializer_node*)raptor_sequence_get_at(seq, i);

    if(raptor_dot_serializer_node_matches(node, node_type, node_data,
                                          datatype, language) )
      return;
  }

  raptor_sequence_push(seq,
		       raptor_dot_serializer_new_node(node_type, node_data,
						      datatype, language));
}
Esempio n. 7
0
static void*
raptor_avltree_search(raptor_avltree* tree, const void* p_data)
{
  int size;
  int i;

  if(!tree)
    return NULL;
  
  size = raptor_sequence_size(tree->seq);
  for(i = 0; i < size; i++) {
    void* data = raptor_sequence_get_at(tree->seq, i);
    if(!tree->compare_handler(p_data, data))
      return data;
  }

  return NULL;
}
Esempio n. 8
0
/**
 * raptor_world_get_serializer_description:
 * @world: world object
 * @counter: index into the list of serializers
 *
 * Get serializer descriptive syntax information
 * 
 * Return value: description or NULL if counter is out of range
 **/
const raptor_syntax_description*
raptor_world_get_serializer_description(raptor_world* world, 
                                        unsigned int counter)
{
  raptor_serializer_factory *factory;

  RAPTOR_ASSERT_OBJECT_POINTER_RETURN_VALUE(world, raptor_world, NULL);

  raptor_world_open(world);

  factory = (raptor_serializer_factory*)raptor_sequence_get_at(world->serializers,
                                                               counter);

  if(!factory)
    return NULL;

  return &factory->desc;
}
Esempio n. 9
0
/**
 * librdf_serializer_check_name:
 * @world: redland world object
 * @name: name of serializer
 *
 * Check if a serializer name is known
 * 
 * Return value: non 0 if name is a known serializer
 **/
int
librdf_serializer_check_name(librdf_world* world, const char *name)
{
  librdf_serializer_factory *factory;
  int i;
  
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(name, char*, 0);

  librdf_world_open(world);

  for(i = 0;
      (factory = (librdf_serializer_factory*)raptor_sequence_get_at(world->serializers, i));
      i++) {
    if(!strcmp(factory->name, name))
      return 1;
  }
  
  return 0;
}
Esempio n. 10
0
/**
 * librdf_serializer_enumerate:
 * @world: redland world object
 * @counter: index into the list of serializers
 * @name: pointer to store the name of the serializer (or NULL)
 * @label: pointer to store syntax readable label (or NULL)
 *
 * Get information on serializers.
 * 
 * Return value: non 0 on failure of if counter is out of range
 **/
int
librdf_serializer_enumerate(librdf_world* world,
                        const unsigned int counter,
                        const char **name, const char **label)
{
  librdf_serializer_factory *factory;
  
  librdf_world_open(world);

  factory=(librdf_serializer_factory*)raptor_sequence_get_at(world->serializers,
                                                         counter);
  if(!factory)
    return 1;
  
  if(name)
    *name=factory->name;
  if(label)
    *label=factory->label;
  return 0;
}
Esempio n. 11
0
/**
 * raptor_xml_element_declare_namespace:
 * @xml_element: XML Element
 * @nspace: raptor_namespace to declare
 * 
 * Declare a namespace on the XML Element.
 *
 * Return value: non-0 if namespace cannot be declared 
 **/
int
raptor_xml_element_declare_namespace(raptor_xml_element* xml_element,
                                     raptor_namespace *nspace)
{
  int i;
  const raptor_namespace *ns;

  if(!xml_element->declared_nspaces)
    xml_element->declared_nspaces = raptor_new_sequence(NULL, NULL);

  if((ns = xml_element->name->nspace)) {
    /* Cannot have same namespace already seen */
    if(ns == nspace ||
       /* ... or two default nspaces */
       (!ns->prefix && !nspace->prefix) ||
       /* ... or two same prefixes */
       (ns->prefix && nspace->prefix &&
        !strcmp((const char*)ns->prefix, (const char*)nspace->prefix))
       )
      return 1;
  }

  
  for(i = 0;
      (ns = (const raptor_namespace*)raptor_sequence_get_at(xml_element->declared_nspaces, i));
      i++) {
    /* Cannot have same namespace already seen */
    if(ns == nspace ||
       /* ... or two default nspaces */
       (!ns->prefix && !nspace->prefix) ||
       /* ... or two same prefixes */
       (ns->prefix && nspace->prefix &&
        !strcmp((const char*)ns->prefix, (const char*)nspace->prefix))
       )
      return 1;
  }

  raptor_sequence_push(xml_element->declared_nspaces, nspace);

  return 0;
}
Esempio n. 12
0
/**
 * rasqal_query_results_get_row_by_offset:
 * @query_results: query result
 * @result_offset: index into result rows
 *
 * Get stored result row by an offset
 *
 * The result_offset index is 0-indexed into the subset of results
 * constrained by any query limit and offset.
 *
 * Return value: row or NULL if @result_offset is out of range
 */
rasqal_row*
rasqal_query_results_get_row_by_offset(rasqal_query_results* query_results,
                                       int result_offset)
{
  rasqal_query* query;
  int check;
  rasqal_row* row;
  int offset = 0;
  
  RASQAL_ASSERT_OBJECT_POINTER_RETURN_VALUE(query_results, rasqal_query_results, NULL);

  if(!query_results->results_sequence)
    return NULL;

  if(result_offset < 0)
    return NULL;

  query = query_results->query;
  if(query)
    offset = rasqal_query_get_offset(query);

  /* Adjust 0-indexed to query results 1-indexed + query result offset */
  result_offset += 1 + offset;

  check = rasqal_query_check_limit_offset(query_results->query,
                                          result_offset);
  /* outside limit/offset range in some way */
  if(check < 0 || check > 0)
    return NULL;

  row = (rasqal_row*)raptor_sequence_get_at(query_results->results_sequence,
                                            result_offset - 1);
  if(row) {
    row = rasqal_new_row_from_row(row);

    /* stored results may not be canonicalized yet - do it lazily */
    rasqal_row_to_nodes(row);
  }
  
  return row;
}
static int
rasqal_rowsequence_rowsource_ensure_variables(rasqal_rowsource* rowsource,
                                              void *user_data)
{
  rasqal_rowsequence_rowsource_context* con;
  int i;
  
  con = (rasqal_rowsequence_rowsource_context*)user_data;

  rowsource->size = 0;
  for(i = 0; i < raptor_sequence_size(con->vars_seq); i++) {
    rasqal_variable* v;
    v = (rasqal_variable*)raptor_sequence_get_at(con->vars_seq, i);
    rasqal_rowsource_add_variable(rowsource, v);
  }
  
  raptor_free_sequence(con->vars_seq);
  con->vars_seq = NULL;
  
  return 0;
}
Esempio n. 14
0
/**
 * raptor_abbrev_subject_add_list_element:
 * @subject: subject node to add to
 * @ordinal: ordinal index
 * @object: object node
 * 
 * Add rdf:li into list element array of a #raptor_abbrev_subject node.
 * 
 * Return value: 
 **/
int
raptor_abbrev_subject_add_list_element(raptor_abbrev_subject* subject, 
                                       int ordinal,
                                       raptor_abbrev_node* object)
{
  int rv = 1;
  raptor_abbrev_node* node;

  node = (raptor_abbrev_node*)raptor_sequence_get_at(subject->list_items,
                                                     ordinal);
  if(!node) {
    /* If there isn't already an entry */
    rv = raptor_sequence_set_at(subject->list_items, ordinal, object);
    if(!rv) {
      object->ref_count++;
      object->count_as_subject++;
    }
  }
  
  return rv;
}
Esempio n. 15
0
static void
rasqal_query_write_sparql_triple_data(sparql_writer_context *wc,
                                      raptor_iostream* iostr,
                                      raptor_sequence *triples,
                                      int indent)
{
  int triple_index = 0;
  
  raptor_iostream_counted_string_write("{\n", 2, iostr);
  indent += 2;
  
  /* look for triples */
  while(1) {
    rasqal_triple* t = raptor_sequence_get_at(triples, triple_index);
    if(!t)
      break;
    
    rasqal_query_write_indent(iostr, indent);
    if(t->origin) {
      raptor_iostream_counted_string_write("GRAPH ", 6, iostr);
      rasqal_query_write_sparql_literal(wc, iostr, t->origin);
      raptor_iostream_counted_string_write(" { ", 3, iostr);
    }
    
    rasqal_query_write_sparql_triple(wc, iostr, t);

    if(t->origin)
      raptor_iostream_counted_string_write(" }", 2, iostr);

    raptor_iostream_write_byte('\n', iostr);
    
    triple_index++;
  }
  
  indent -= 2;
  
  rasqal_query_write_indent(iostr, indent);
  raptor_iostream_write_byte('}', iostr);
  
}
Esempio n. 16
0
/*
 * rasqal_variables_write:
 * @seq: sequence of #rasqal_variable to write
 * @iostr: the #raptor_iostream handle to write to
 *
 * INTERNAL - Write a sequence of Rasqal variable to an iostream in a debug format.
 *
 * The write debug format may change in any release.
 *
 **/
int
rasqal_variables_write(raptor_sequence* seq, raptor_iostream* iostr)
{
  int vars_size;
  int i;

  if(!seq || !iostr)
    return 1;

  vars_size = raptor_sequence_size(seq);
  for(i = 0; i < vars_size; i++) {
    rasqal_variable* v;

    v = (rasqal_variable*)raptor_sequence_get_at(seq, i);
    if(i > 0)
      raptor_iostream_counted_string_write(", ", 2, iostr);

    rasqal_variable_write(v, iostr);
  }

  return 0;
}
static rasqal_row*
rasqal_rowsequence_rowsource_read_row(rasqal_rowsource* rowsource,
                                      void *user_data)
{
  rasqal_rowsequence_rowsource_context* con;
  rasqal_row* row = NULL;
  
  con = (rasqal_rowsequence_rowsource_context*)user_data;
  if(con->failed || con->offset < 0)
    return NULL;

  row = (rasqal_row*)raptor_sequence_get_at(con->seq, con->offset);
  if(!row) {
    /* finished */
    con->offset = -1;
  } else {
    row = rasqal_new_row_from_row(row);
    con->offset++;
  }

  return row;
}
Esempio n. 18
0
static int
rasqal_algebra_visitor_set_origin(rasqal_query* query,
                                  rasqal_algebra_node* node,
                                  void *user_data)
{
  rasqal_literal *origin = (rasqal_literal*)user_data;
  int i;
  
  if(node->op != RASQAL_ALGEBRA_OPERATOR_BGP)
    return 0;

  for(i = node->start_column; i <= node->end_column; i++) {
    rasqal_triple *t;
    rasqal_literal *o = NULL;
    
    t = (rasqal_triple*)raptor_sequence_get_at(node->triples, i);
    if(origin)
      o = rasqal_new_literal_from_literal(origin);
    
    rasqal_triple_set_origin(t, o);
  }
  return 0;
}
Esempio n. 19
0
/* 
 * rasqal_expression_evaluate_coalesce:
 * @e: The expression to evaluate.
 * @eval_context: Evaluation context
 *
 * INTERNAL - Evaluate RASQAL_EXPR_COALESCE (expr list) expressions.
 *
 * Return value: A #rasqal_literal value or NULL on failure.
 */
static rasqal_literal*
rasqal_expression_evaluate_coalesce(rasqal_expression *e,
                                    rasqal_evaluation_context *eval_context,
                                    int *error_p)
{
  int size = raptor_sequence_size(e->args);
  int i;
  
  for(i = 0; i < size; i++) {
    rasqal_expression* arg_e;

    arg_e = (rasqal_expression*)raptor_sequence_get_at(e->args, i);
    if(arg_e) {
      rasqal_literal* result;
      result = rasqal_expression_evaluate2(arg_e, eval_context, error_p);
      if(result)
        return result;
    }
  }
  
  /* No arguments evaluated to an RDF term, return an error (NULL) */
  return NULL;
}
Esempio n. 20
0
/**
 * rasqal_rowsource_get_variable_offset_by_name:
 * @rowsource: rasqal rowsource
 * @name: variable name
 *
 * Get the offset of a variable into the list of variables
 *
 * Return value: offset or <0 if not present
 **/
int
rasqal_rowsource_get_variable_offset_by_name(rasqal_rowsource *rowsource,
        const unsigned char* name)
{
    int offset= -1;
    int i;

    rasqal_rowsource_ensure_variables(rowsource);

    if(!rowsource->variables_sequence)
        return -1;

    for(i=0; i < raptor_sequence_size(rowsource->variables_sequence); i++) {
        rasqal_variable* v;
        v = (rasqal_variable*)raptor_sequence_get_at(rowsource->variables_sequence, i);
        if(!strcmp((const char*)v->name, (const char*)name)) {
            offset = i;
            break;
        }
    }

    return offset;
}
static void
raptor_turtle_ensure_writen_header(raptor_serializer* serializer,
                                   raptor_turtle_context* context) 
{
  int i;

  if(context->written_header)
    return;
  
  if(!context->turtle_writer)
    return;

  for(i=0; i< raptor_sequence_size(context->namespaces); i++) {
    raptor_namespace* ns;
    ns=(raptor_namespace*)raptor_sequence_get_at(context->namespaces, i);
    raptor_turtle_writer_namespace_prefix(context->turtle_writer, ns);
    raptor_namespace_copy(context->nstack, ns, 0);
  }
  
  raptor_turtle_writer_raw(context->turtle_writer, (const unsigned char*)"\n");

  context->written_header=1;
}
static raptor_sequence*
rasqal_rowsequence_rowsource_read_all_rows(rasqal_rowsource* rowsource,
                                           void *user_data)
{
  rasqal_rowsequence_rowsource_context* con;
  raptor_sequence* seq;
  
  con = (rasqal_rowsequence_rowsource_context*)user_data;
  if(con->offset < 0)
    return NULL;

  seq = raptor_new_sequence((raptor_data_free_handler)rasqal_free_row,
                            (raptor_data_print_handler)rasqal_row_print);
  if(seq) {
    int i;
    int size = raptor_sequence_size(con->seq);
    for(i = 0; i < size; i++) {
      rasqal_row *row = (rasqal_row*)raptor_sequence_get_at(con->seq, i);
      raptor_sequence_push(seq, rasqal_new_row_from_row(row));
    }
  }
  
  return seq;
}
Esempio n. 23
0
raptor_abbrev_subject*
raptor_abbrev_subject_find(raptor_sequence *sequence,
                           raptor_identifier_type node_type,
                           const void *node_data, int *idx)
{
  raptor_abbrev_subject* rv_subject = NULL;
  int i;
  
  for(i=0; i < raptor_sequence_size(sequence); i++) {
    raptor_abbrev_subject* subject=(raptor_abbrev_subject*)raptor_sequence_get_at(sequence, i);

    if(subject &&
       raptor_abbrev_node_matches(subject->node, node_type, node_data, NULL, NULL)) {
      rv_subject = subject;
      break;
    }
    
  }

  if(idx)
    *idx = i;
  
  return rv_subject;
}
/* add a namespace */
static int
raptor_turtle_serialize_declare_namespace_from_namespace(raptor_serializer* serializer, 
                                                         raptor_namespace *nspace)
{
  raptor_turtle_context* context=(raptor_turtle_context*)serializer->context;
  int i;
 
  if(context->written_header)
    return 1;
  
  for(i=0; i< raptor_sequence_size(context->namespaces); i++) {
    raptor_namespace* ns;
    ns=(raptor_namespace*)raptor_sequence_get_at(context->namespaces, i);

    /* If prefix is already declared, ignore it */
    if(!ns->prefix && !nspace->prefix)
      return 1;
    
    if(ns->prefix && nspace->prefix && 
       !strcmp((const char*)ns->prefix, (const char*)nspace->prefix))
      return 1;

    if(ns->uri && nspace->uri &&
       raptor_uri_equals_v2(serializer->world, ns->uri, nspace->uri))
      return 1;
  }

  nspace=raptor_new_namespace_from_uri(context->nstack,
                                       nspace->prefix, nspace->uri,
                                       TURTLE_NAMESPACE_DEPTH);
  if(!nspace)
    return 1;
  
  raptor_sequence_push(context->namespaces, nspace);
  return 0;
}
Esempio n. 25
0
/**
 * rasqal_query_results_get_triple:
 * @query_results: #rasqal_query_results query_results
 *
 * Get the current triple in the result.
 *
 * The return value is a shared #raptor_statement.
 * 
 * Return value: #raptor_statement or NULL if failed or results exhausted
 **/
raptor_statement*
rasqal_query_results_get_triple(rasqal_query_results* query_results)
{
  rasqal_query* query;
  int rc;
  rasqal_triple *t;
  rasqal_literal *s, *p, *o;
  raptor_statement *rs = NULL;
  unsigned char *nodeid;
  int skipped;
  
  RASQAL_ASSERT_OBJECT_POINTER_RETURN_VALUE(query_results, rasqal_query_results, NULL);

 if(query_results->failed || query_results->finished)
    return NULL;
  
  if(!rasqal_query_results_is_graph(query_results))
    return NULL;
  
  query = query_results->query;
  if(!query)
    return NULL;
  
  if(query->verb == RASQAL_QUERY_VERB_DESCRIBE)
    return NULL;

 
  /* ensure we have a row to work on */
  if(rasqal_query_results_ensure_have_row_internal(query_results))
    return NULL;

  skipped = 0;
  while(1) {
    if(skipped) {
      rc = rasqal_query_results_next(query_results);
      if(rc) {
        rs = NULL;
        break;
      }
      query_results->current_triple_result = -1;
    }
    
    if(query_results->current_triple_result < 0)
      query_results->current_triple_result = 0;

    t = (rasqal_triple*)raptor_sequence_get_at(query->constructs,
                                               query_results->current_triple_result);

    rs = &query_results->result_triple;

    s = rasqal_literal_as_node(t->subject);
    if(!s) {
      rasqal_log_error_simple(query_results->world,
                              RAPTOR_LOG_LEVEL_WARN,
                              &query->locator,
                              "Triple with unbound subject skipped");
      skipped = 1;
      continue;
    }

    /* raptor v2 terms are copied, not shared */
    if(rs->subject) {
      raptor_free_term(rs->subject);
      rs->subject = NULL;
    }

    switch(s->type) {
      case RASQAL_LITERAL_URI:
        rs->subject = raptor_new_term_from_uri(query_results->world->raptor_world_ptr,
                                               s->value.uri);
        break;

      case RASQAL_LITERAL_BLANK:
        nodeid = rasqal_prefix_id(query_results->result_count,
                                  (unsigned char*)s->string);
        rasqal_free_literal(s);
        if(!nodeid) {
          rasqal_log_error_simple(query_results->world, RAPTOR_LOG_LEVEL_FATAL,
                                  &query->locator,
                                  "Could not prefix subject blank identifier");
          return NULL;
        }
        s = rasqal_new_simple_literal(query_results->world, RASQAL_LITERAL_BLANK,
                                      nodeid);
        if(!s) {
          rasqal_log_error_simple(query_results->world, RAPTOR_LOG_LEVEL_FATAL,
                                  &query->locator,
                                  "Could not create a new subject blank literal");
          return NULL;
        }
        rs->subject = raptor_new_term_from_blank(query_results->world->raptor_world_ptr,
                                                 nodeid);
        break;

      case RASQAL_LITERAL_QNAME:
      case RASQAL_LITERAL_PATTERN:
      case RASQAL_LITERAL_XSD_STRING:
      case RASQAL_LITERAL_BOOLEAN:
      case RASQAL_LITERAL_INTEGER:
      case RASQAL_LITERAL_DOUBLE:
      case RASQAL_LITERAL_FLOAT:
      case RASQAL_LITERAL_VARIABLE:
      case RASQAL_LITERAL_DECIMAL:
      case RASQAL_LITERAL_DATETIME:
      case RASQAL_LITERAL_UDT:
      case RASQAL_LITERAL_INTEGER_SUBTYPE:
        /* QNames should be gone by the time expression eval happens
         * Everything else is removed by rasqal_literal_as_node() above. 
         */

      case RASQAL_LITERAL_STRING:
        /* string [literal] subjects are not RDF */

      case RASQAL_LITERAL_UNKNOWN:
      default:
        /* case RASQAL_LITERAL_STRING: */
        rasqal_log_error_simple(query_results->world,
                                RAPTOR_LOG_LEVEL_WARN,
                                &query->locator,
                                "Triple with non-URI/blank node subject skipped");
        skipped = 1;
        break;
    }
    if(skipped) {
      if(s)
        rasqal_free_literal(s);
      continue;
    }
    

    p = rasqal_literal_as_node(t->predicate);
    if(!p) {
      rasqal_log_error_simple(query_results->world,
                              RAPTOR_LOG_LEVEL_WARN,
                              &query->locator,
                              "Triple with unbound predicate skipped");
      rasqal_free_literal(s);
      skipped = 1;
      continue;
    }
    switch(p->type) {
      case RASQAL_LITERAL_URI:
        /* raptor v2 terms are copied, not shared */
        if(rs->predicate) {
          raptor_free_term(rs->predicate);
          rs->predicate = NULL;
        }
        rs->predicate = raptor_new_term_from_uri(query_results->world->raptor_world_ptr,
                                                 p->value.uri);
        break;

      case RASQAL_LITERAL_QNAME:
      case RASQAL_LITERAL_PATTERN:
      case RASQAL_LITERAL_XSD_STRING:
      case RASQAL_LITERAL_BOOLEAN:
      case RASQAL_LITERAL_INTEGER:
      case RASQAL_LITERAL_DOUBLE:
      case RASQAL_LITERAL_FLOAT:
      case RASQAL_LITERAL_VARIABLE:
      case RASQAL_LITERAL_DECIMAL:
      case RASQAL_LITERAL_DATETIME:
      case RASQAL_LITERAL_UDT:
      case RASQAL_LITERAL_INTEGER_SUBTYPE:
        /* QNames should be gone by the time expression eval happens
         * Everything else is removed by rasqal_literal_as_node() above. 
         */

      case RASQAL_LITERAL_BLANK:
      case RASQAL_LITERAL_STRING:
        /* blank node or string [literal] predicates are not RDF */

      case RASQAL_LITERAL_UNKNOWN:
      default:
        rasqal_log_error_simple(query_results->world,
                                RAPTOR_LOG_LEVEL_WARN,
                                &query->locator,
                                "Triple with non-URI predicate skipped");
        skipped = 1;
        break;
    }
    if(skipped) {
      rasqal_free_literal(s);
      if(p)
        rasqal_free_literal(p);
      continue;
    }

    o = rasqal_literal_as_node(t->object);
    if(!o) {
      rasqal_log_error_simple(query_results->world,
                              RAPTOR_LOG_LEVEL_WARN,
                              &query->locator,
                              "Triple with unbound object skipped");
      rasqal_free_literal(s);
      rasqal_free_literal(p);
      skipped = 1;
      continue;
    }

    /* raptor v2 terms are copied, not shared */
    if(rs->object) {
      raptor_free_term(rs->object);
      rs->object = NULL;
    }

    switch(o->type) {
      case RASQAL_LITERAL_URI:
        rs->object = raptor_new_term_from_uri(query_results->world->raptor_world_ptr,
                                              o->value.uri);
        break;

      case RASQAL_LITERAL_BLANK:
        nodeid = rasqal_prefix_id(query_results->result_count,
                                  (unsigned char*)o->string);
        rasqal_free_literal(o);
        if(!nodeid) {
          rasqal_log_error_simple(query_results->world, RAPTOR_LOG_LEVEL_FATAL,
                                  &query->locator,
                                  "Could not prefix blank identifier");
          rasqal_free_literal(s);
          rasqal_free_literal(p);
          return NULL;
        }
        o = rasqal_new_simple_literal(query_results->world, RASQAL_LITERAL_BLANK,
                                      nodeid);
        if(!o) {
          rasqal_log_error_simple(query_results->world, RAPTOR_LOG_LEVEL_FATAL,
                                  &query->locator,
                                  "Could not create a new subject blank literal");
          rasqal_free_literal(s);
          rasqal_free_literal(p);
          return NULL;
        }
        rs->object = raptor_new_term_from_blank(query_results->world->raptor_world_ptr,
                                                nodeid);
        break;

      case RASQAL_LITERAL_STRING:
        rs->object = raptor_new_term_from_literal(query_results->world->raptor_world_ptr,
                                                  o->string,
                                                  o->datatype,
                                                  (const unsigned char*)o->language);
        break;

      case RASQAL_LITERAL_QNAME:
      case RASQAL_LITERAL_PATTERN:
      case RASQAL_LITERAL_XSD_STRING:
      case RASQAL_LITERAL_BOOLEAN:
      case RASQAL_LITERAL_INTEGER:
      case RASQAL_LITERAL_DOUBLE:
      case RASQAL_LITERAL_FLOAT:
      case RASQAL_LITERAL_VARIABLE:
      case RASQAL_LITERAL_DECIMAL:
      case RASQAL_LITERAL_DATETIME:
      case RASQAL_LITERAL_UDT:
      case RASQAL_LITERAL_INTEGER_SUBTYPE:
        /* QNames should be gone by the time expression eval happens
         * Everything else is removed by rasqal_literal_as_node() above. 
         */

      case RASQAL_LITERAL_UNKNOWN:
      default:
        rasqal_log_error_simple(query_results->world,
                                RAPTOR_LOG_LEVEL_WARN,
                                &query->locator,
                                "Triple with unknown object skipped");
        skipped = 1;
        break;
    }
    if(skipped) {
      rasqal_free_literal(s);
      rasqal_free_literal(p);
      if(o)
        rasqal_free_literal(o);
      continue;
    }
    
    /* dispose previous triple if any */
    if(query_results->triple) {
      rasqal_free_triple(query_results->triple);
      query_results->triple = NULL;
    }

    /* for saving s, p, o for later disposal */
    query_results->triple = rasqal_new_triple(s, p, o);

    /* got triple, return it */
    break;
  }
  
  return rs;
}
/* destroy a serializer */
static void
raptor_turtle_serialize_terminate(raptor_serializer* serializer)
{
  raptor_turtle_context* context=(raptor_turtle_context*)serializer->context;

  if(context->turtle_writer) {
    raptor_free_turtle_writer(context->turtle_writer);
    context->turtle_writer=NULL;
  }

  if(context->rdf_nspace) {
    raptor_free_namespace(context->rdf_nspace);
    context->rdf_nspace=NULL;
  }

  if(context->namespaces) {
    int i;

    /* Note: item 0 in the list is rdf:RDF's namespace and freed above */
    for(i=1; i< raptor_sequence_size(context->namespaces); i++) {
      raptor_namespace* ns;
      ns =(raptor_namespace*)raptor_sequence_get_at(context->namespaces, i);
      if(ns)
        raptor_free_namespace(ns);
    }
    raptor_free_sequence(context->namespaces);
    context->namespaces=NULL;
  }

  if(context->subjects) {
    raptor_free_avltree(context->subjects);
    context->subjects=NULL;
  }

  if(context->blanks) {
    raptor_free_avltree(context->blanks);
    context->blanks=NULL;
  }

  if(context->nodes) {
    raptor_free_avltree(context->nodes);
    context->nodes=NULL;
  }

  if(context->nstack) {
    raptor_free_namespaces(context->nstack);
    context->nstack=NULL;
  }

  if(context->rdf_type) {
    raptor_free_abbrev_node(context->rdf_type);
    context->rdf_type=NULL;
  }

  if(context->rdf_xml_literal_uri) {
    raptor_free_uri_v2(serializer->world, context->rdf_xml_literal_uri);
    context->rdf_xml_literal_uri=NULL;
  }

  if(context->rdf_first_uri) {
    raptor_free_uri_v2(serializer->world, context->rdf_first_uri);
    context->rdf_first_uri=NULL;
  }

  if(context->rdf_rest_uri) {
    raptor_free_uri_v2(serializer->world, context->rdf_rest_uri);
    context->rdf_rest_uri=NULL;
  }

  if(context->rdf_nil_uri) {
    raptor_free_uri_v2(serializer->world, context->rdf_nil_uri);
    context->rdf_nil_uri=NULL;
  }
}
Esempio n. 27
0
/* end a serialize */
static int
raptor_dot_serializer_end(raptor_serializer* serializer)
{
  raptor_dot_context* context=(raptor_dot_context*)serializer->context;
  raptor_dot_serializer_node* node;
  int i;

  /* Print our nodes. */
  raptor_iostream_write_string(serializer->iostream,
                               (const unsigned char *)"\n\t// Resources\n");
  for( i = 0 ; i < raptor_sequence_size(context->resources) ; i++ ) {
    node = (raptor_dot_serializer_node*)raptor_sequence_get_at(context->resources, i);
    raptor_iostream_write_string(serializer->iostream,
                                 (const unsigned char *)"\t\"R");
    raptor_dot_serializer_write_node(serializer, node->value.resource.uri,
				     RAPTOR_IDENTIFIER_TYPE_RESOURCE, NULL, NULL);
    raptor_iostream_write_string(serializer->iostream,
                                 (const unsigned char *)"\" [ label=\"");
    raptor_dot_serializer_write_node(serializer, node->value.resource.uri,
				     RAPTOR_IDENTIFIER_TYPE_RESOURCE, NULL, NULL);
    raptor_iostream_write_string(serializer->iostream,
                                 (const unsigned char *)"\", shape=ellipse");
    raptor_dot_serializer_write_colors(serializer,
                                       RAPTOR_IDENTIFIER_TYPE_RESOURCE);
    raptor_iostream_write_string(serializer->iostream,
                                 (const unsigned char *)" ];\n");
    
  }
  raptor_free_sequence(context->resources);

  raptor_iostream_write_string(serializer->iostream,
                               (const unsigned char *)"\n\t// Anonymous nodes\n");
  for( i = 0 ; i < raptor_sequence_size(context->bnodes) ; i++ ) {
    node = (raptor_dot_serializer_node *)raptor_sequence_get_at(context->bnodes, i);
    raptor_iostream_write_string(serializer->iostream,
                                 (const unsigned char *)"\t\"B");
    raptor_dot_serializer_write_node(serializer, node->value.resource.uri,
				   RAPTOR_IDENTIFIER_TYPE_ANONYMOUS, NULL, NULL);
    raptor_iostream_write_string(serializer->iostream,
                                 (const unsigned char *)"\" [ label=\"");
    raptor_iostream_write_string(serializer->iostream,
                                 (const unsigned char *)"\", shape=circle");
    raptor_dot_serializer_write_colors(serializer,
                                       RAPTOR_IDENTIFIER_TYPE_ANONYMOUS);
    raptor_iostream_write_string(serializer->iostream,
                                 (const unsigned char *)" ];\n");
  }
  raptor_free_sequence(context->bnodes);

  raptor_iostream_write_string(serializer->iostream,
                               (const unsigned char *)"\n\t// Literals\n");
  for( i = 0 ; i < raptor_sequence_size(context->literals) ; i++ ) {
    node = (raptor_dot_serializer_node *)raptor_sequence_get_at(context->literals, i);
    raptor_iostream_write_string(serializer->iostream,
                                 (const unsigned char *)"\t\"L");
    raptor_dot_serializer_write_node(serializer, node->value.literal.string,
				     RAPTOR_IDENTIFIER_TYPE_LITERAL,
                                     node->value.literal.datatype,
				     node->value.literal.language);
    raptor_iostream_write_string(serializer->iostream,
                                 (const unsigned char *)"\" [ label=\"");
    raptor_dot_serializer_write_node(serializer, node->value.literal.string,
				     RAPTOR_IDENTIFIER_TYPE_LITERAL,
                                     node->value.literal.datatype,
				     node->value.literal.language);
    raptor_iostream_write_string(serializer->iostream,
                                 (const unsigned char *)"\", shape=record");
    raptor_dot_serializer_write_colors(serializer,
                                       RAPTOR_IDENTIFIER_TYPE_LITERAL);
    raptor_iostream_write_string(serializer->iostream,
                                 (const unsigned char *)" ];\n");
  }
  raptor_free_sequence(context->literals);

  raptor_iostream_write_string(serializer->iostream,
                               (const unsigned char *)"\n\tlabel=\"\\n\\nModel:\\n");
  if(serializer->base_uri)
    raptor_iostream_write_string(serializer->iostream,
                                 raptor_uri_as_string(serializer->base_uri));
  else
    raptor_iostream_write_string(serializer->iostream, "(Unknown)");

  if(raptor_sequence_size(context->namespaces)) {
    raptor_iostream_write_string(serializer->iostream,
                                 (const unsigned char *)"\\n\\nNamespaces:\\n");

    for( i = 0 ; i < raptor_sequence_size(context->namespaces) ; i++ ) {
      raptor_namespace* ns =
	(raptor_namespace*)raptor_sequence_get_at(context->namespaces, i);
      const unsigned char* prefix = raptor_namespace_get_prefix(ns);

      if(prefix) {
	raptor_iostream_write_string(serializer->iostream,
				     (const unsigned char *)ns->prefix);
	raptor_iostream_write_string(serializer->iostream,
				     (const unsigned char *)": ");
      }
      raptor_iostream_write_string(serializer->iostream,
				   raptor_uri_as_string(ns->uri));
      raptor_iostream_write_string(serializer->iostream,
				   (const unsigned char *)"\\n");
    }

    raptor_free_sequence(context->namespaces);
  }

  raptor_iostream_write_string(serializer->iostream,
                               (const unsigned char *)"\";\n");

  raptor_iostream_write_string(serializer->iostream,
                               (const unsigned char *) "}\n");

  return 0;
}
Esempio n. 28
0
rasqal_rowsource*
rasqal_new_aggregation_rowsource(rasqal_world *world, rasqal_query* query,
                                 rasqal_rowsource* rowsource,
                                 raptor_sequence* exprs_seq,
                                 raptor_sequence* vars_seq)
{
  rasqal_aggregation_rowsource_context* con;
  int flags = 0;
  int size;
  int i;
  
  if(!world || !query || !rowsource || !exprs_seq || !vars_seq)
    goto fail;

  exprs_seq = rasqal_expression_copy_expression_sequence(exprs_seq);
  vars_seq = rasqal_variable_copy_variable_sequence(vars_seq);

  size = raptor_sequence_size(exprs_seq);
  if(size != raptor_sequence_size(vars_seq)) {
    RASQAL_DEBUG3("expressions sequence size %d does not match vars sequence size %d\n", size, raptor_sequence_size(vars_seq));
    goto fail;
  }


  con = (rasqal_aggregation_rowsource_context*)RASQAL_CALLOC(rasqal_aggregation_rowsource_context, 1, sizeof(*con));
  if(!con)
    goto fail;

  con->rowsource = rowsource;

  con->exprs_seq = exprs_seq;
  con->vars_seq = vars_seq;
  
  /* allocate per-expr data */
  con->expr_count = size;
  con->expr_data = (rasqal_agg_expr_data*)RASQAL_CALLOC(rasqal_agg_expr_data, 
                                                        sizeof(rasqal_agg_expr_data),
                                                        size);
  if(!con->expr_data)
    goto fail;

  /* Initialise per-expr data */
  for(i = 0; i < size; i++) {
    rasqal_expression* expr = (rasqal_expression *)raptor_sequence_get_at(exprs_seq, i);
    rasqal_variable* variable = (rasqal_variable*)raptor_sequence_get_at(vars_seq, i);
    rasqal_agg_expr_data* expr_data = &con->expr_data[i];

    expr_data->expr = rasqal_new_expression_from_expression(expr);
    expr_data->variable = variable;

    /* Prepare expression arguments sequence in per-expr data */
    if(expr->args) {
      /* list of #rasqal_expression arguments already in expr
       * #RASQAL_EXPR_FUNCTION and #RASQAL_EXPR_GROUP_CONCAT 
       */
      expr_data->exprs_seq = rasqal_expression_copy_expression_sequence(expr->args);
    } else {
      /* single argument */
      
      expr_data->exprs_seq = raptor_new_sequence((raptor_data_free_handler)rasqal_free_expression,
                                               (raptor_data_print_handler)rasqal_expression_print);
      raptor_sequence_push(expr_data->exprs_seq,
                           rasqal_new_expression_from_expression(expr->arg1));
    }
  }
  
  
  return rasqal_new_rowsource_from_handler(world, query,
                                           con,
                                           &rasqal_aggregation_rowsource_handler,
                                           query->vars_table,
                                           flags);

  fail:

  if(rowsource)
    rasqal_free_rowsource(rowsource);
  if(exprs_seq)
    raptor_free_sequence(exprs_seq);
  if(vars_seq)
    raptor_free_sequence(vars_seq);

  return NULL;
}
Esempio n. 29
0
static int
rasqal_builtin_agg_expression_execute_step(void* user_data,
                                           raptor_sequence* literals)
{
  rasqal_builtin_agg_expression_execute* b;
  rasqal_literal* l;
  int i;

  b = (rasqal_builtin_agg_expression_execute*)user_data;

  if(b->error)
    return 1;
  
  if(b->expr->op == RASQAL_EXPR_COUNT) {
    /* COUNT(*) : counts every row (does not care about literals) */
    if(b->expr->arg1->op == RASQAL_EXPR_VARSTAR)
      b->count++;
    /* COUNT(expr list) : counts rows with non-empty sequence of literals */
    else if(raptor_sequence_size(literals) > 0)
      b->count++;
    
    return 0;
  }
    

  /* Other aggregate functions count every row */
  b->count++;

  for(i = 0; (l = (rasqal_literal*)raptor_sequence_get_at(literals, i)); i++) {
    rasqal_literal* result = NULL;

    if(b->expr->op == RASQAL_EXPR_SAMPLE) {
      /* Sample chooses the first literal it sees */
      if(!b->l)
        b->l = rasqal_new_literal_from_literal(l);

      break;
    }

    if(b->expr->op == RASQAL_EXPR_GROUP_CONCAT) {
      const unsigned char* str;
      int error = 0;
      
      str = (const unsigned char*)rasqal_literal_as_string_flags(l, 0, &error);

      if(!error) {
        if(raptor_stringbuffer_length(b->sb))
          raptor_stringbuffer_append_counted_string(b->sb, b->separator, 1, 1);

        raptor_stringbuffer_append_string(b->sb, str, 1); 
      }
      continue;
    }
  
    
    if(!b->l)
      result = rasqal_new_literal_from_literal(l);
    else {
      if(b->expr->op == RASQAL_EXPR_SUM || b->expr->op == RASQAL_EXPR_AVG) {
        result = rasqal_literal_add(b->l, l, &b->error);
      } else if(b->expr->op == RASQAL_EXPR_MIN) {
        int cmp = rasqal_literal_compare(b->l, l, 0, &b->error);
        if(cmp <= 0)
          result = rasqal_new_literal_from_literal(b->l);
        else
          result = rasqal_new_literal_from_literal(l);
      } else if(b->expr->op == RASQAL_EXPR_MAX) {
        int cmp = rasqal_literal_compare(b->l, l, 0, &b->error);
        if(cmp >= 0)
          result = rasqal_new_literal_from_literal(b->l);
        else
          result = rasqal_new_literal_from_literal(l);
      } else {
        RASQAL_FATAL2("Builtin aggregation operation %d is not implemented", 
                      b->expr->op);
      }

      rasqal_free_literal(b->l);

      if(!result)
        b->error = 1;
    }
    
    b->l = result;

#if RASQAL_DEBUG > 1
    RASQAL_DEBUG3("Aggregation step result %s (error=%d)\n", 
                  (result ? (const char*)rasqal_literal_as_string(result) : "(NULL)"),
                  b->error);
#endif
    
    if(b->error)
      break;
  }
  
  return b->error;
}
Esempio n. 30
0
int
main(int argc, char *argv[]) 
{
  const char *program = rasqal_basename(argv[0]);
  rasqal_rowsource *rowsource = NULL;
  rasqal_world* world = NULL;
  rasqal_query* query = NULL;
  raptor_sequence* row_seq = NULL;
  raptor_sequence* expr_args_seq = NULL;
  int failures = 0;
  rasqal_variables_table* vt;
  rasqal_rowsource *input_rs = NULL;
  raptor_sequence* vars_seq = NULL;
  raptor_sequence* exprs_seq = NULL;
  int test_id;

  world = rasqal_new_world();
  if(!world || rasqal_world_open(world)) {
    fprintf(stderr, "%s: rasqal_world init failed\n", program);
    return(1);
  }
  
  query = rasqal_new_query(world, "sparql", NULL);

  vt = query->vars_table;
  
  for(test_id = 0; test_id < AGGREGATION_TESTS_COUNT; test_id++) {
    int input_vars_count = test_data[test_id].input_vars;
    int output_rows_count = test_data[test_id].output_rows;
    int output_vars_count = test_data[test_id].output_vars;
    const int* input_group_ids = test_data[test_id].group_ids;
    const int* result_int_data = test_data[test_id].result_data;
    const char* const* result_string_data = test_data[test_id].result_string_data;
    rasqal_op op  = test_data[test_id].op;
    raptor_sequence* seq = NULL;
    int count;
    int size;
    int i;
    char* output_var_name;
    rasqal_variable* output_var;
    rasqal_expression* expr;
    int output_row_size = (input_vars_count + output_vars_count);
    
    if(output_vars_count != 1) {
      fprintf(stderr,
              "%s: test %d expects %d variables which is not supported. Test skipped\n",
              program, test_id, output_vars_count);
      failures++;
      goto tidy;
    }

    row_seq = rasqal_new_row_sequence(world, vt, test_data[test_id].data,
                                      test_data[test_id].input_vars, &vars_seq);
    if(row_seq) {
      for(i = 0; i < test_data[test_id].input_rows; i++) {
        rasqal_row* row = (rasqal_row*)raptor_sequence_get_at(row_seq, i);
        row->group_id = input_group_ids[i];
      }
      
      input_rs = rasqal_new_rowsequence_rowsource(world, query, vt, 
                                                  row_seq, vars_seq);
      /* vars_seq and row_seq are now owned by input_rs */
      vars_seq = row_seq = NULL;
    }
    if(!input_rs) {
      fprintf(stderr, "%s: failed to create rowsequence rowsource\n", program);
      failures++;
      goto tidy;
    }

    expr_args_seq = raptor_new_sequence((raptor_data_free_handler)rasqal_free_expression,
                                        (raptor_data_print_handler)rasqal_expression_print);

    if(test_data[test_id].expr_agg_vars[0] != NULL) {
      int vindex;
      const unsigned char* var_name;
      
      for(vindex = 0;
          (var_name = (const unsigned char*)test_data[test_id].expr_agg_vars[vindex] );
          vindex++) {
        rasqal_variable* v;
        rasqal_literal *l = NULL;
        rasqal_expression* e = NULL;

        v = rasqal_variables_table_get_by_name(vt, var_name);
        if(v)
          l = rasqal_new_variable_literal(world, v);

        if(l)
          e = rasqal_new_literal_expression(world, l);

        if(e)
          raptor_sequence_push(expr_args_seq, e);
        else {
          fprintf(stderr, "%s: failed to create variable %s\n", program,
                  (const char*)var_name);
          failures++;
          goto tidy;
        }
        
      }
    } /* if vars */


    output_var_name = (char*)RASQAL_MALLOC(cstring, 5);
    memcpy(output_var_name, "fake", 5);
    output_var = rasqal_variables_table_add(vt, RASQAL_VARIABLE_TYPE_ANONYMOUS, 
                                            (const unsigned char*)output_var_name, NULL);

    expr = make_test_expr(world, expr_args_seq, op);
    /* expr_args_seq is now owned by expr */
    expr_args_seq = NULL;

    exprs_seq = raptor_new_sequence((raptor_data_free_handler)rasqal_free_expression,
                                    (raptor_data_print_handler)rasqal_expression_print);
    raptor_sequence_push(exprs_seq, expr);
    /* expr is now owned by exprs_seq */
    expr = NULL;
    
    vars_seq = raptor_new_sequence((raptor_data_free_handler)rasqal_free_variable,
                                   (raptor_data_print_handler)rasqal_variable_print);
    output_var = rasqal_new_variable_from_variable(output_var);
    raptor_sequence_push(vars_seq, output_var);

    rowsource = rasqal_new_aggregation_rowsource(world, query, input_rs,
                                                 exprs_seq, vars_seq);
    /* exprs_seq, vars_seq and input_rs are now owned by rowsource */
    exprs_seq = NULL; vars_seq = NULL; input_rs = NULL;

    if(!rowsource) {
      fprintf(stderr, "%s: failed to create aggregation rowsource\n", program);
      failures++;
      goto tidy;
    }


    /* Test the rowsource */
    seq = rasqal_rowsource_read_all_rows(rowsource);
    if(!seq) {
      fprintf(stderr,
              "%s: test %d rasqal_rowsource_read_all_rows() returned a NULL seq for a aggregation rowsource\n",
              program, test_id);
      failures++;
      goto tidy;
    }
    count = raptor_sequence_size(seq);
    if(count != output_rows_count) {
      fprintf(stderr,
              "%s: test %d rasqal_rowsource_read_all_rows() returned %d rows for a aggregation rowsource, expected %d\n",
              program, test_id, count, output_rows_count);
      failures++;
      goto tidy;
    }

    size = rasqal_rowsource_get_size(rowsource);
    if(size != output_row_size) {
      fprintf(stderr,
              "%s: test %d rasqal_rowsource_get_size() returned %d columns (variables) for a aggregation rowsource, expected %d\n",
              program, test_id, size, output_row_size);
      failures++;
      goto tidy;
    }

    if(result_int_data) {
      for(i = 0; i < output_rows_count; i++) {
        rasqal_row* row = (rasqal_row*)raptor_sequence_get_at(seq, i);
        rasqal_literal* value;
        int integer;
        int expected_integer = result_int_data[i];
        int vc;
        
        if(row->size != output_row_size) {
          fprintf(stderr,
                  "%s: test %d row #%d is size %d expected %d\n",
                  program, test_id, i, row->size, output_row_size);
          failures++;
          goto tidy;
        }
        
        /* Expected variable ordering in output row is:
         * {input vars} {output_vars}
         */
        for(vc = 0; vc < output_vars_count; vc++) {
          rasqal_variable* row_var;
          int offset = input_vars_count + vc;
          
          row_var = rasqal_rowsource_get_variable_by_offset(rowsource, offset);
          value = row->values[offset]; 

          if(!value) {
            fprintf(stderr,
                    "%s: test %d row #%d %s value #%d result is NULL\n",
                    program, test_id, i, row_var->name, vc);
            failures++;
            goto tidy;
          }

          if(value->type != RASQAL_LITERAL_INTEGER) {
            fprintf(stderr,
                    "%s: test %d row #%d %s value #%d result is type %s expected integer\n",
                    program, test_id, i, row_var->name, vc,
                    rasqal_literal_type_label(value->type));
            failures++;
            goto tidy;
          }

          integer = rasqal_literal_as_integer(value, NULL);

          if(integer != expected_integer) {
            fprintf(stderr,
                    "%s: test %d row #%d %s value #%d result is %d expected %d\n",
                    program, test_id, i, row_var->name, vc,
                    integer, expected_integer);
            failures++;
            goto tidy;
          }
        }
        
      }
    }
    
    if(result_string_data) {
      for(i = 0; i < output_rows_count; i++) {
        rasqal_row* row = (rasqal_row*)raptor_sequence_get_at(seq, i);
        rasqal_literal* value;
        const unsigned char* str;
        const char* expected_string = result_string_data[i];
        int vc;
        
        if(row->size != output_row_size) {
          fprintf(stderr,
                  "%s: test %d row #%d is size %d expected %d\n",
                  program, test_id, i, row->size, output_row_size);
          failures++;
          goto tidy;
        }
        
        /* Expected variable ordering in output row is:
         * {input vars} {output_vars}
         */
        for(vc = 0; vc < output_vars_count; vc++) {
          rasqal_variable* row_var;
          int offset = input_vars_count + vc;

          row_var = rasqal_rowsource_get_variable_by_offset(rowsource, offset);
          value = row->values[offset]; 

          if(!value) {
            fprintf(stderr,
                    "%s: test %d row #%d %s value #%d result is NULL\n",
                    program, test_id, i, row_var->name, vc);
            failures++;
            goto tidy;
          }

          if(value->type != RASQAL_LITERAL_STRING) {
            fprintf(stderr,
                    "%s: test %d row #%d %s value #%d is type %s expected integer\n",
                    program, test_id, i, row_var->name, vc,
                    rasqal_literal_type_label(value->type));
            failures++;
            goto tidy;
          }

          str = rasqal_literal_as_string(value);

          if(strcmp((const char*)str, expected_string)) {
            fprintf(stderr,
                    "%s: test %d row #%d %s value #%d is %s expected %s\n",
                    program, test_id, i, row_var->name, vc,
                    str, expected_string);
            failures++;
            goto tidy;
          }
        }
        
      }
    }
    

#ifdef RASQAL_DEBUG
    rasqal_rowsource_print_row_sequence(rowsource, seq, stderr);
#endif

    raptor_free_sequence(seq); seq = NULL;

    rasqal_free_rowsource(rowsource); rowsource = NULL;

    if(expr_args_seq)
      raptor_free_sequence(expr_args_seq);
    expr_args_seq = NULL;
  }
  
  
  tidy:
  if(exprs_seq)
    raptor_free_sequence(exprs_seq);
  if(vars_seq)
    raptor_free_sequence(vars_seq);
  if(expr_args_seq)
    raptor_free_sequence(expr_args_seq);
  if(rowsource)
    rasqal_free_rowsource(rowsource);
  if(input_rs)
    rasqal_free_rowsource(input_rs);
  if(query)
    rasqal_free_query(query);
  if(world)
    rasqal_free_world(world);

  return failures;
}