mas01cr@554
|
1 #include <sys/types.h>
|
mas01cr@553
|
2 #include <sys/stat.h>
|
mas01cr@644
|
3 #include <libgen.h>
|
mas01cr@554
|
4 #include <unistd.h>
|
mas01cr@553
|
5 #include <librdf.h>
|
mas01cr@553
|
6 #include <fcntl.h>
|
mas01cr@553
|
7 #include <string.h>
|
mas01cr@553
|
8 #include <stdlib.h>
|
mas01cr@553
|
9
|
mas01cr@553
|
10 #include <audioDB_API.h>
|
mas01cr@553
|
11
|
mas01cr@553
|
12 const char * qstring =
|
mas01cr@553
|
13 " PREFIX af: <http://purl.org/ontology/af/>"
|
mas01cr@553
|
14 " PREFIX dc: <http://purl.org/dc/elements/1.1/>"
|
mas01cr@553
|
15 " PREFIX mo: <http://purl.org/ontology/mo/>"
|
mas01cr@553
|
16 " PREFIX tl: <http://purl.org/NET/c4dm/timeline.owl#>"
|
mas01cr@553
|
17
|
mas01cr@644
|
18 " SELECT ?key ?value ?sample_rate ?window_length ?hop_size ?dimensions"
|
mas01cr@553
|
19
|
mas01cr@553
|
20 " WHERE { "
|
mas01mj@583
|
21 " ?key a mo:AudioFile . "
|
mas01mj@583
|
22 " ?signal a mo:Signal . "
|
mas01mj@583
|
23 " ?key mo:encodes ?signal ."
|
mas01cr@553
|
24 " ?signal mo:time [ tl:onTimeLine ?signal_timeline ] . "
|
mas01cr@553
|
25
|
mas01cr@553
|
26 " ?timeline_map a tl:UniformSamplingWindowingMap ; "
|
mas01cr@553
|
27 " tl:rangeTimeLine ?feature_timeline ; "
|
mas01cr@553
|
28 " tl:domainTimeLine ?signal_timeline ; "
|
mas01cr@553
|
29 " tl:sampleRate ?sample_rate ; "
|
mas01cr@553
|
30 " tl:windowLength ?window_length ; "
|
mas01cr@553
|
31 " tl:hopSize ?hop_size . "
|
mas01cr@553
|
32
|
mas01cr@553
|
33 " ?signal af:signal_feature ?feature . "
|
mas01cr@553
|
34
|
mas01cr@553
|
35 " ?feature a ?feature_signal_type ; "
|
mas01cr@553
|
36 " mo:time [ tl:onTimeLine ?feature_timeline ] ; "
|
mas01cr@553
|
37 " af:value ?value . "
|
mas01mj@583
|
38 " ?feature af:dimensions ?dimensions ."
|
mas01cr@553
|
39
|
mas01cr@553
|
40 " } "
|
mas01cr@553
|
41 ;
|
mas01cr@553
|
42
|
mas01cr@553
|
43 double *parse_value_string(const char *value_string, size_t *nelements) {
|
mas01cr@553
|
44 /* What error checking? */
|
mas01cr@553
|
45
|
mas01mj@583
|
46
|
mas01cr@553
|
47 *nelements = 0;
|
mas01cr@553
|
48
|
mas01cr@553
|
49 const char *current = value_string;
|
mas01cr@553
|
50 char *next = 0;
|
mas01cr@553
|
51
|
mas01cr@553
|
52 size_t size = 1;
|
mas01cr@553
|
53 double *buf = (double *) malloc(size * sizeof(double));
|
mas01cr@553
|
54 double value = strtod(current, &next);
|
mas01cr@553
|
55 while(next != current) {
|
mas01cr@553
|
56 buf[(*nelements)++] = value;
|
mas01cr@553
|
57 if((*nelements) == size) {
|
mas01cr@553
|
58 size *= 2;
|
mas01cr@553
|
59 buf = (double *) realloc(buf, 2 * size * sizeof(double));
|
mas01cr@553
|
60 }
|
mas01cr@553
|
61 current = next;
|
mas01cr@553
|
62 value = strtod(current, &next);
|
mas01cr@553
|
63 }
|
mas01cr@571
|
64 return buf;
|
mas01cr@553
|
65 }
|
mas01cr@553
|
66
|
mas01mj@583
|
67 int main(int argc, char* argv[]) {
|
mas01mj@583
|
68
|
mas01mj@583
|
69 if(argc != 3)
|
mas01mj@583
|
70 {
|
mas01mj@583
|
71 fprintf(stderr, "Usage: %s <n3file> <dbfile>\n", argv[0]);
|
mas01mj@583
|
72 return 2;
|
mas01mj@583
|
73 }
|
mas01mj@583
|
74
|
mas01mj@583
|
75 char* n3file = argv[1];
|
mas01mj@583
|
76 char* dbfile = argv[2];
|
mas01mj@583
|
77
|
mas01cr@554
|
78 librdf_world *world;
|
mas01cr@554
|
79 if (!(world = librdf_new_world()))
|
mas01cr@554
|
80 goto librdf_error;
|
mas01cr@553
|
81
|
mas01cr@554
|
82 librdf_storage *storage;
|
mas01cr@554
|
83 if (!(storage = librdf_new_storage(world, "memory", NULL, NULL)))
|
mas01cr@554
|
84 goto librdf_error;
|
mas01cr@553
|
85
|
mas01cr@554
|
86 librdf_model *model;
|
mas01cr@554
|
87 if (!(model = librdf_new_model(world, storage, NULL)))
|
mas01cr@554
|
88 goto librdf_error;
|
mas01mj@583
|
89
|
mas01cr@554
|
90 librdf_uri *uri;
|
mas01cr@644
|
91 if (!(uri = librdf_new_uri_from_filename(world, n3file)))
|
mas01cr@554
|
92 goto librdf_error;
|
mas01cr@553
|
93
|
mas01cr@554
|
94 librdf_parser *parser;
|
mas01cr@554
|
95 if (!(parser = librdf_new_parser(world, "guess", NULL, NULL)))
|
mas01cr@554
|
96 goto librdf_error;
|
mas01cr@553
|
97
|
mas01cr@554
|
98 if(librdf_parser_parse_into_model(parser, uri, NULL, model))
|
mas01cr@554
|
99 goto librdf_error;
|
mas01cr@553
|
100
|
mas01cr@554
|
101 librdf_query *query;
|
mas01cr@554
|
102 if (!(query = librdf_new_query(world, "sparql", NULL, qstring, NULL)))
|
mas01cr@554
|
103 goto librdf_error;
|
mas01cr@553
|
104
|
mas01cr@554
|
105 librdf_query_results *results;
|
mas01cr@554
|
106 if (!(results = librdf_query_execute(query, model)))
|
mas01cr@554
|
107 goto librdf_error;
|
mas01cr@553
|
108
|
mas01cr@554
|
109 if(!librdf_query_results_is_bindings(results))
|
mas01cr@554
|
110 goto librdf_error;
|
mas01cr@554
|
111
|
mas01cr@554
|
112 adb_t *adb;
|
mas01mj@583
|
113 if(!(adb = audiodb_open(dbfile, O_RDWR))) {
|
mas01mj@583
|
114
|
mas01cr@554
|
115 struct stat st;
|
mas01mj@583
|
116 if(!(stat(dbfile, &st))) {
|
mas01mj@583
|
117 fprintf(stderr, "%s not opened.\n", dbfile);
|
mas01cr@554
|
118 return 1;
|
mas01cr@554
|
119 } else {
|
mas01cr@554
|
120 /* FIXME: if we are doing a proper SPARQL query over a
|
mas01cr@554
|
121 * potentially unbounded number of results, we could use
|
mas01cr@554
|
122 * librdf_query_results_get_count() to estimate how much space
|
mas01cr@554
|
123 * our database will need.
|
mas01cr@554
|
124
|
mas01cr@554
|
125 * If we're doing a SPARQL query over a number of RDF files,
|
mas01cr@554
|
126 * with an expected number of datasets per file of 1 (as might
|
mas01cr@554
|
127 * be produced by runner(?)) then obviously we can use that as
|
mas01cr@554
|
128 * an estimate of number of tracks instead.
|
mas01cr@554
|
129
|
mas01cr@554
|
130 * Specifying the data dimensionality should be easy from the
|
mas01cr@554
|
131 * semantics of the feature being inserted; it's not immediately
|
mas01cr@554
|
132 * obvious to me how to estimate the data size required, unless
|
mas01cr@554
|
133 * we maybe do a preliminary query to find out the total time
|
mas01cr@554
|
134 * (or similar) of all the tracks we're about to insert.
|
mas01cr@554
|
135
|
mas01cr@554
|
136 * (also NOTE: this audiodb_create() interface is scheduled for
|
mas01cr@554
|
137 * being made less inelegant.) */
|
mas01mj@583
|
138 if(!(adb = audiodb_create(dbfile, 0, 0, 0))) {
|
mas01mj@583
|
139 fprintf(stderr, "failed to create %s.\n", dbfile);
|
mas01cr@554
|
140 return 1;
|
mas01cr@554
|
141 }
|
mas01cr@554
|
142 }
|
mas01cr@553
|
143 }
|
mas01cr@554
|
144
|
mas01mj@583
|
145 if(librdf_query_results_finished(results))
|
mas01mj@583
|
146 {
|
mas01mj@583
|
147 fprintf(stderr, "No features found!\n");
|
mas01mj@583
|
148 return 3;
|
mas01mj@583
|
149 }
|
mas01mj@583
|
150
|
mas01mj@583
|
151
|
mas01cr@553
|
152 while(!librdf_query_results_finished(results)) {
|
mas01mj@583
|
153
|
mas01cr@553
|
154 adb_datum_t datum = {0};
|
mas01mj@583
|
155
|
mas01mj@583
|
156 // Pull out the dimension
|
mas01mj@583
|
157
|
mas01mj@583
|
158 librdf_node *node = librdf_query_results_get_binding_value_by_name(results, "dimensions");
|
mas01mj@583
|
159 char* dimensions = librdf_node_get_literal_value(node);
|
mas01mj@583
|
160
|
mas01mj@583
|
161 int dimension = 0;
|
mas01mj@583
|
162 sscanf(dimensions, "%d", &dimension);
|
mas01mj@583
|
163 datum.dim = dimension;
|
mas01cr@644
|
164
|
mas01mj@583
|
165 // Grab key and value
|
mas01mj@583
|
166
|
mas01mj@583
|
167 node = librdf_query_results_get_binding_value_by_name(results, "key");
|
mas01cr@644
|
168 char *key = strdup(librdf_uri_to_filename(librdf_node_get_uri(node)));
|
mas01cr@644
|
169 datum.key = basename(key);
|
mas01mj@583
|
170
|
mas01mj@583
|
171 size_t nelements = 0;
|
mas01mj@583
|
172 node = librdf_query_results_get_binding_value_by_name(results, "value");
|
mas01mj@583
|
173 datum.data = parse_value_string(librdf_node_get_literal_value(node), &nelements);
|
mas01mj@583
|
174
|
mas01mj@583
|
175 // Validate that we have the correct number of elements
|
mas01mj@583
|
176
|
mas01mj@583
|
177 if(nelements % dimension) return 4;
|
mas01mj@583
|
178 datum.nvectors = nelements / dimension;
|
mas01mj@583
|
179
|
mas01mj@583
|
180 printf("Dimension: %d\n", dimension);
|
mas01mj@583
|
181 printf("Key: %s\n", datum.key);
|
mas01mj@583
|
182 printf("Vectors: %d\n", datum.nvectors);
|
mas01mj@583
|
183
|
mas01mj@583
|
184 librdf_free_node(node);
|
mas01mj@583
|
185
|
mas01cr@553
|
186 if(audiodb_insert_datum(adb, &datum)) {
|
mas01cr@554
|
187 fprintf(stderr, "failed to insert datum with key %s.\n", datum.key);
|
mas01cr@554
|
188 return 1;
|
mas01cr@553
|
189 }
|
mas01cr@571
|
190 free(datum.data);
|
mas01cr@553
|
191 librdf_query_results_next(results);
|
mas01cr@553
|
192 }
|
mas01cr@553
|
193
|
mas01cr@553
|
194 audiodb_close(adb);
|
mas01cr@553
|
195 librdf_free_query_results(results);
|
mas01cr@553
|
196 librdf_free_query(query);
|
mas01cr@571
|
197 librdf_free_parser(parser);
|
mas01cr@553
|
198 librdf_free_uri(uri);
|
mas01cr@553
|
199 librdf_free_model(model);
|
mas01cr@553
|
200 librdf_free_storage(storage);
|
mas01cr@553
|
201 librdf_free_world(world);
|
mas01cr@554
|
202
|
mas01cr@554
|
203 return 0;
|
mas01cr@554
|
204
|
mas01cr@554
|
205 librdf_error:
|
mas01cr@554
|
206 fprintf(stderr, "Something went wrong in librdf.\n");
|
mas01cr@554
|
207 return 1;
|
mas01cr@553
|
208 }
|