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