view examples/runner-rdf/populate.c @ 553:ea341e68649f

Produce embryonic example of integration with runner ("sonic annotator") There's much that is hardwired (though a bit less than in the example sent by e-mail to Casey, Cannam and Raimond). More needs to be done, including thinking about how a typical user will actually be using the combination of tools; this code is proof-of-concept more than anything else.
author mas01cr
date Fri, 13 Feb 2009 11:23:10 +0000
parents
children 1ff30df0aeac
line wrap: on
line source
#include <sys/stat.h>
#include <librdf.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>

#include <audioDB_API.h>

const char * qstring = 
" PREFIX af: <http://purl.org/ontology/af/>"
" PREFIX dc: <http://purl.org/dc/elements/1.1/>"
" PREFIX mo: <http://purl.org/ontology/mo/>"
" PREFIX tl: <http://purl.org/NET/c4dm/timeline.owl#>"

" SELECT ?key ?value ?sample_rate ?window_length ?hop_size"
" FROM <file:///home/csr21/tmp/rdf/test.n3> "

" WHERE { "

"   ?signal mo:available_as ?key ."

"   ?signal mo:time [ tl:onTimeLine ?signal_timeline ] . "

"   ?timeline_map a tl:UniformSamplingWindowingMap ; "
"     tl:rangeTimeLine ?feature_timeline ; "
"     tl:domainTimeLine ?signal_timeline ; "
"     tl:sampleRate ?sample_rate ; "
"     tl:windowLength ?window_length ; "
"     tl:hopSize ?hop_size . "

"   ?signal af:signal_feature ?feature . "

"   ?feature a ?feature_signal_type ; "
"     mo:time [ tl:onTimeLine ?feature_timeline ] ; "
"     af:value ?value . "

"   ?feature_signal_type dc:title \"Key Strength Plot\""

" } "
;

double *parse_value_string(const char *value_string, size_t *nelements) {
  /* What error checking? */

  *nelements = 0;

  const char *current = value_string;
  char *next = 0;

  size_t size = 1;
  double *buf = (double *) malloc(size * sizeof(double));
  double value = strtod(current, &next);
  while(next != current) {
    buf[(*nelements)++] = value;
    if((*nelements) == size) {
      size *= 2;
      buf = (double *) realloc(buf, 2 * size * sizeof(double));
    }
    current = next;
    value = strtod(current, &next);
  }
  
}

int main() {
  librdf_world *world = librdf_new_world();
  if(world == NULL) return 1;

  librdf_storage *storage = librdf_new_storage(world, "memory", NULL, NULL);
  if(storage == NULL) return 1;

  librdf_model *model = librdf_new_model(world, storage, NULL);
  if(model == NULL) return 1;

  librdf_uri *uri = librdf_new_uri(world, "file:data/test.n3");
  if(uri == NULL) return 1;

  librdf_parser *parser = librdf_new_parser(world, "guess", NULL, NULL);
  if(parser == NULL) return 1;

  if(librdf_parser_parse_into_model(parser, uri, NULL, model)) return 1;
  
  librdf_query *query = 
    librdf_new_query(world, "sparql", NULL, qstring, NULL);
  if(query == NULL) return 1;

  librdf_query_results *results = librdf_query_execute(query, model);
  if(results == NULL) return 1;
  if(!librdf_query_results_is_bindings(results)) return 1;

  adb_t *adb = audiodb_open("keyplot.adb", O_RDWR);
  if(!adb) {
    fprintf(stderr, "keyplot.adb not opened\n");
    return 1;
  }
  while(!librdf_query_results_finished(results)) {
    int count = librdf_query_results_get_bindings_count(results);
    adb_datum_t datum = {0};
    datum.dim = 25;
    for (int i = 0; i < count; i++) {
      const char *name = librdf_query_results_get_binding_name(results, i);
      librdf_node *node = librdf_query_results_get_binding_value(results, i);
      if(!node) return 2;

      if(!strcmp(name, "key")) {
        datum.key = librdf_uri_as_string(librdf_node_get_uri(node));
      } else if(!strcmp(name, "value")) {
        size_t nelements = 0;
        datum.data = parse_value_string(librdf_node_get_literal_value(node), &nelements);
        if(nelements % 25) return 4;
        datum.nvectors = nelements / 25;
      } else {
        printf("%s: %s\n", name, librdf_node_get_literal_value(node));
      }
      librdf_free_node(node);
    }
    if(audiodb_insert_datum(adb, &datum)) {
      return 17;
    }
    librdf_query_results_next(results);
  }

  audiodb_close(adb);
  librdf_free_query_results(results);
  librdf_free_query(query);
  librdf_free_uri(uri);
  librdf_free_model(model);
  librdf_free_storage(storage);
  librdf_free_world(world);
}