annotate 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
rev   line source
mas01cr@553 1 #include <sys/stat.h>
mas01cr@553 2 #include <librdf.h>
mas01cr@553 3 #include <fcntl.h>
mas01cr@553 4 #include <string.h>
mas01cr@553 5 #include <stdlib.h>
mas01cr@553 6
mas01cr@553 7 #include <audioDB_API.h>
mas01cr@553 8
mas01cr@553 9 const char * qstring =
mas01cr@553 10 " PREFIX af: <http://purl.org/ontology/af/>"
mas01cr@553 11 " PREFIX dc: <http://purl.org/dc/elements/1.1/>"
mas01cr@553 12 " PREFIX mo: <http://purl.org/ontology/mo/>"
mas01cr@553 13 " PREFIX tl: <http://purl.org/NET/c4dm/timeline.owl#>"
mas01cr@553 14
mas01cr@553 15 " SELECT ?key ?value ?sample_rate ?window_length ?hop_size"
mas01cr@553 16 " FROM <file:///home/csr21/tmp/rdf/test.n3> "
mas01cr@553 17
mas01cr@553 18 " WHERE { "
mas01cr@553 19
mas01cr@553 20 " ?signal mo:available_as ?key ."
mas01cr@553 21
mas01cr@553 22 " ?signal mo:time [ tl:onTimeLine ?signal_timeline ] . "
mas01cr@553 23
mas01cr@553 24 " ?timeline_map a tl:UniformSamplingWindowingMap ; "
mas01cr@553 25 " tl:rangeTimeLine ?feature_timeline ; "
mas01cr@553 26 " tl:domainTimeLine ?signal_timeline ; "
mas01cr@553 27 " tl:sampleRate ?sample_rate ; "
mas01cr@553 28 " tl:windowLength ?window_length ; "
mas01cr@553 29 " tl:hopSize ?hop_size . "
mas01cr@553 30
mas01cr@553 31 " ?signal af:signal_feature ?feature . "
mas01cr@553 32
mas01cr@553 33 " ?feature a ?feature_signal_type ; "
mas01cr@553 34 " mo:time [ tl:onTimeLine ?feature_timeline ] ; "
mas01cr@553 35 " af:value ?value . "
mas01cr@553 36
mas01cr@553 37 " ?feature_signal_type dc:title \"Key Strength Plot\""
mas01cr@553 38
mas01cr@553 39 " } "
mas01cr@553 40 ;
mas01cr@553 41
mas01cr@553 42 double *parse_value_string(const char *value_string, size_t *nelements) {
mas01cr@553 43 /* What error checking? */
mas01cr@553 44
mas01cr@553 45 *nelements = 0;
mas01cr@553 46
mas01cr@553 47 const char *current = value_string;
mas01cr@553 48 char *next = 0;
mas01cr@553 49
mas01cr@553 50 size_t size = 1;
mas01cr@553 51 double *buf = (double *) malloc(size * sizeof(double));
mas01cr@553 52 double value = strtod(current, &next);
mas01cr@553 53 while(next != current) {
mas01cr@553 54 buf[(*nelements)++] = value;
mas01cr@553 55 if((*nelements) == size) {
mas01cr@553 56 size *= 2;
mas01cr@553 57 buf = (double *) realloc(buf, 2 * size * sizeof(double));
mas01cr@553 58 }
mas01cr@553 59 current = next;
mas01cr@553 60 value = strtod(current, &next);
mas01cr@553 61 }
mas01cr@553 62
mas01cr@553 63 }
mas01cr@553 64
mas01cr@553 65 int main() {
mas01cr@553 66 librdf_world *world = librdf_new_world();
mas01cr@553 67 if(world == NULL) return 1;
mas01cr@553 68
mas01cr@553 69 librdf_storage *storage = librdf_new_storage(world, "memory", NULL, NULL);
mas01cr@553 70 if(storage == NULL) return 1;
mas01cr@553 71
mas01cr@553 72 librdf_model *model = librdf_new_model(world, storage, NULL);
mas01cr@553 73 if(model == NULL) return 1;
mas01cr@553 74
mas01cr@553 75 librdf_uri *uri = librdf_new_uri(world, "file:data/test.n3");
mas01cr@553 76 if(uri == NULL) return 1;
mas01cr@553 77
mas01cr@553 78 librdf_parser *parser = librdf_new_parser(world, "guess", NULL, NULL);
mas01cr@553 79 if(parser == NULL) return 1;
mas01cr@553 80
mas01cr@553 81 if(librdf_parser_parse_into_model(parser, uri, NULL, model)) return 1;
mas01cr@553 82
mas01cr@553 83 librdf_query *query =
mas01cr@553 84 librdf_new_query(world, "sparql", NULL, qstring, NULL);
mas01cr@553 85 if(query == NULL) return 1;
mas01cr@553 86
mas01cr@553 87 librdf_query_results *results = librdf_query_execute(query, model);
mas01cr@553 88 if(results == NULL) return 1;
mas01cr@553 89 if(!librdf_query_results_is_bindings(results)) return 1;
mas01cr@553 90
mas01cr@553 91 adb_t *adb = audiodb_open("keyplot.adb", O_RDWR);
mas01cr@553 92 if(!adb) {
mas01cr@553 93 fprintf(stderr, "keyplot.adb not opened\n");
mas01cr@553 94 return 1;
mas01cr@553 95 }
mas01cr@553 96 while(!librdf_query_results_finished(results)) {
mas01cr@553 97 int count = librdf_query_results_get_bindings_count(results);
mas01cr@553 98 adb_datum_t datum = {0};
mas01cr@553 99 datum.dim = 25;
mas01cr@553 100 for (int i = 0; i < count; i++) {
mas01cr@553 101 const char *name = librdf_query_results_get_binding_name(results, i);
mas01cr@553 102 librdf_node *node = librdf_query_results_get_binding_value(results, i);
mas01cr@553 103 if(!node) return 2;
mas01cr@553 104
mas01cr@553 105 if(!strcmp(name, "key")) {
mas01cr@553 106 datum.key = librdf_uri_as_string(librdf_node_get_uri(node));
mas01cr@553 107 } else if(!strcmp(name, "value")) {
mas01cr@553 108 size_t nelements = 0;
mas01cr@553 109 datum.data = parse_value_string(librdf_node_get_literal_value(node), &nelements);
mas01cr@553 110 if(nelements % 25) return 4;
mas01cr@553 111 datum.nvectors = nelements / 25;
mas01cr@553 112 } else {
mas01cr@553 113 printf("%s: %s\n", name, librdf_node_get_literal_value(node));
mas01cr@553 114 }
mas01cr@553 115 librdf_free_node(node);
mas01cr@553 116 }
mas01cr@553 117 if(audiodb_insert_datum(adb, &datum)) {
mas01cr@553 118 return 17;
mas01cr@553 119 }
mas01cr@553 120 librdf_query_results_next(results);
mas01cr@553 121 }
mas01cr@553 122
mas01cr@553 123 audiodb_close(adb);
mas01cr@553 124 librdf_free_query_results(results);
mas01cr@553 125 librdf_free_query(query);
mas01cr@553 126 librdf_free_uri(uri);
mas01cr@553 127 librdf_free_model(model);
mas01cr@553 128 librdf_free_storage(storage);
mas01cr@553 129 librdf_free_world(world);
mas01cr@553 130 }