Ejemplo n.º 1
0
static librdf_node*
rasqal_literal_to_redland_node(librdf_world *world, rasqal_literal* l)
{
  rasqal_literal_type type;

  if(!l)
    return NULL;
  
  /* FIXME: Workaround for Issue #0000519
   * http://bugs.librdf.org/mantis/view.php?id=519
   *
   * Remove this 'if' when RASQAL_MIN_VERSION is 0.9.30 or larger
   */
  if(l->type == RASQAL_LITERAL_INTEGER_SUBTYPE)
    type = RASQAL_LITERAL_STRING;
  else
    type = rasqal_literal_get_rdf_term_type(l);

  if(type == RASQAL_LITERAL_URI)
    return librdf_new_node_from_uri(world, (librdf_uri*)l->value.uri);
  else if (type == RASQAL_LITERAL_STRING)
    return librdf_new_node_from_typed_literal(world, 
                                              (unsigned char*)l->string, 
                                              l->language, 
                                              (librdf_uri*)l->datatype);
  else if (type == RASQAL_LITERAL_BLANK)
    return librdf_new_node_from_blank_identifier(world,
                                                 (unsigned char*)l->string);

  LIBRDF_DEBUG2("Could not convert literal type %d to librdf_node\n", type);
  return NULL;
}
Ejemplo n.º 2
0
/* 
 * 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;
}
Ejemplo n.º 3
0
/* 
 * 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;
}
Ejemplo n.º 4
0
/* 
 * 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;
}
Ejemplo n.º 5
0
/* 
 * 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  {
Ejemplo n.º 6
0
fs_rid fs_hash_rasqal_literal(struct update_context *uc, rasqal_literal *l, int row)
{
    if (!l) return FS_RID_NULL;

    if (l->type == RASQAL_LITERAL_VARIABLE) {
        if (uc->q) {
            return fs_binding_get_val(uc->q->bb[0], l->value.variable, row, NULL);
        }
        fs_error(LOG_ERR, "no variables bound");

        return FS_RID_NULL;
    }

    rasqal_literal_type type = rasqal_literal_get_rdf_term_type(l);
    switch (type) {
    case RASQAL_LITERAL_URI:
        return fs_hash_uri((char *)raptor_uri_as_string(l->value.uri));
    
    case RASQAL_LITERAL_UNKNOWN:
    case RASQAL_LITERAL_STRING:
    case RASQAL_LITERAL_XSD_STRING: {
        fs_rid attr = 0;
        if (l->datatype) {
            attr = fs_hash_uri((char *)raptor_uri_as_string(l->datatype));
        } else if (l->language) {
            /* lang tags are normalised to upper case internally */
            char *lang = g_ascii_strup((char *)l->language, -1);
            attr = fs_hash_literal(lang, 0);
            g_free(lang);
        }

        return fs_hash_literal((char *)rasqal_literal_as_string(l), attr);
    }

    case RASQAL_LITERAL_BLANK: {
        raptor_term_blank_value bnode;
        bnode.string = (unsigned char *)rasqal_literal_as_string(l);
        bnode.string_len = strlen((char *)bnode.string);

        return fs_bnode_id(uc->link, bnode);
    }

    case RASQAL_LITERAL_VARIABLE:
    case RASQAL_LITERAL_QNAME:
    case RASQAL_LITERAL_PATTERN:
    case RASQAL_LITERAL_BOOLEAN:
    case RASQAL_LITERAL_INTEGER:
    case RASQAL_LITERAL_INTEGER_SUBTYPE:
    case RASQAL_LITERAL_DECIMAL:
    case RASQAL_LITERAL_FLOAT:
    case RASQAL_LITERAL_DOUBLE:
    case RASQAL_LITERAL_DATETIME:
    case RASQAL_LITERAL_UDT:
#if RASQAL_VERSION >= 929
    case RASQAL_LITERAL_DATE:
#endif
        break;
    }
    fs_error(LOG_ERR, "bad rasqal literal (type %d)", type);

    return FS_RID_NULL;
}
Ejemplo n.º 7
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;

}