Пример #1
0
/* 
 * rasqal_expression_evaluate_rand:
 * @e: The expression to evaluate.
 * @eval_context: Evaluation context
 *
 * INTERNAL - Evaluate SPARQL 1.1 RASQAL_EXPR_RAND (integer expr) expression.
 *
 * Return value: A #rasqal_literal xsd:double value in range [0, 1) or NULL on failure.
 */
rasqal_literal*
rasqal_expression_evaluate_rand(rasqal_expression *e,
                                rasqal_evaluation_context *eval_context,
                                int *error_p)
{
  rasqal_world* world = eval_context->world;
  double d;
  
  d = rasqal_random_drand(eval_context->random);

  return rasqal_new_double_literal(world, d);
}
Пример #2
0
int
main(int argc, char *argv[]) 
{
  const char *program = rasqal_basename(argv[0]);
  rasqal_world* world = NULL;
  rasqal_variables_table* vt = NULL;
#define NUM_VARS 3
  const char* var_names[NUM_VARS] = {"normal-null", "normal-value", "anon"};
  unsigned char* names[NUM_VARS];
  rasqal_variable* vars[NUM_VARS];
  rasqal_literal *value = NULL;
  int i;
  int rc = 0;
  
  world = rasqal_new_world();
  if(!world || rasqal_world_open(world)) {
    fprintf(stderr, "%s: rasqal_world init failed\n", program);
    rc = 1;
    goto tidy;
  }
  
  vt = rasqal_new_variables_table(world);
  if(!vt) {
    fprintf(stderr, "%s: Failed to make variables table\n", program);
    rc = 1;
    goto tidy;
  }

  for(i = 0; i < NUM_VARS; i++) {
    size_t len = strlen(var_names[i]);
    names[i] = (unsigned char*)malloc(len+1);
    memcpy(names[i], var_names[i], len + 1);
  }
  
  vars[0] = rasqal_variables_table_add(vt, RASQAL_VARIABLE_TYPE_NORMAL,
                                     names[0], NULL);
  if(!vars[0]) {
    fprintf(stderr, "%s: Failed to make normal variable with NULL value\n",
            program);
    rc = 1;
    goto tidy;
  } else {
    /* now owned by vars[0] owned by vt */
    names[0] = NULL;
  }
  /* vars[0] now owned by vt */

  value = rasqal_new_double_literal(world, 42.0);
  if(!value) {
    fprintf(stderr, "%s: Failed to make double literal\n", program);
    rc = 1;
    goto tidy;
  }
  vars[1] = rasqal_variables_table_add(vt, RASQAL_VARIABLE_TYPE_NORMAL,
                                     names[1], value);
  if(!vars[1]) {
    fprintf(stderr, "%s: Failed to make normal variable with literal value\n",
            program);
    rc = 1;
    goto tidy;
  } else {
    /* now owned by vars[1] owned by vt */
    names[1] = NULL;
    value = NULL;
  }
  /* vars[1] now owned by vt */
  
  vars[2] = rasqal_variables_table_add(vt, RASQAL_VARIABLE_TYPE_ANONYMOUS,
                                     names[2], NULL);
  if(!vars[2]) {
    fprintf(stderr, "%s: Failed to make anonymous variable with NULL value\n",
            program);
    rc = 1;
    goto tidy;
  } else {
    /* now owned by vars[2] owned by vt */
    names[2] = NULL;
  }
  /* vars[2] now owned by vt */
  
  tidy:
  for(i = 0; i < NUM_VARS; i++) {
    if(names[i])
      free(names[i]);
  }
  
  if(value)
    rasqal_free_literal(value);
  if(vt)
    rasqal_free_variables_table(vt);
  
  if(world)
    rasqal_free_world(world);

  return 0;
}