comparison examples/runner-rdf/populate.c @ 583:46673db7c6a4

No changes... oddly
author mas01mj
date Tue, 28 Jul 2009 13:49:49 +0000
parents 29f3289bb183
children 404222029e27
comparison
equal deleted inserted replaced
582:29f3289bb183 583:46673db7c6a4
12 " PREFIX af: <http://purl.org/ontology/af/>" 12 " PREFIX af: <http://purl.org/ontology/af/>"
13 " PREFIX dc: <http://purl.org/dc/elements/1.1/>" 13 " PREFIX dc: <http://purl.org/dc/elements/1.1/>"
14 " PREFIX mo: <http://purl.org/ontology/mo/>" 14 " PREFIX mo: <http://purl.org/ontology/mo/>"
15 " PREFIX tl: <http://purl.org/NET/c4dm/timeline.owl#>" 15 " PREFIX tl: <http://purl.org/NET/c4dm/timeline.owl#>"
16 16
17 " SELECT ?key ?value ?sample_rate ?window_length ?hop_size" 17 " SELECT ?key ?value ?sample_rate ?window_length ?hop_size ?dimensions ?typetitle"
18 " FROM <file:///home/csr21/tmp/rdf/test.n3> " 18 " FROM <file:///home/csr21/tmp/rdf/test2.n3> "
19 19
20 " WHERE { " 20 " WHERE { "
21 21 " ?key a mo:AudioFile . "
22 " ?signal mo:available_as ?key ." 22 " ?signal a mo:Signal . "
23 23 " ?key mo:encodes ?signal ."
24 " ?signal mo:time [ tl:onTimeLine ?signal_timeline ] . " 24 " ?signal mo:time [ tl:onTimeLine ?signal_timeline ] . "
25 25
26 " ?timeline_map a tl:UniformSamplingWindowingMap ; " 26 " ?timeline_map a tl:UniformSamplingWindowingMap ; "
27 " tl:rangeTimeLine ?feature_timeline ; " 27 " tl:rangeTimeLine ?feature_timeline ; "
28 " tl:domainTimeLine ?signal_timeline ; " 28 " tl:domainTimeLine ?signal_timeline ; "
33 " ?signal af:signal_feature ?feature . " 33 " ?signal af:signal_feature ?feature . "
34 34
35 " ?feature a ?feature_signal_type ; " 35 " ?feature a ?feature_signal_type ; "
36 " mo:time [ tl:onTimeLine ?feature_timeline ] ; " 36 " mo:time [ tl:onTimeLine ?feature_timeline ] ; "
37 " af:value ?value . " 37 " af:value ?value . "
38 38 " ?feature af:dimensions ?dimensions ."
39 " ?feature_signal_type dc:title \"Key Strength Plot\"" 39
40 " ?feature_signal_type dc:title ?typetitle "
40 41
41 " } " 42 " } "
42 ; 43 ;
43 44
44 double *parse_value_string(const char *value_string, size_t *nelements) { 45 double *parse_value_string(const char *value_string, size_t *nelements) {
45 /* What error checking? */ 46 /* What error checking? */
47
46 48
47 *nelements = 0; 49 *nelements = 0;
48 50
49 const char *current = value_string; 51 const char *current = value_string;
50 char *next = 0; 52 char *next = 0;
51 53
52 size_t size = 1; 54 size_t size = 1;
53 double *buf = (double *) malloc(size * sizeof(double)); 55 double *buf = (double *) malloc(size * sizeof(double));
54 double value = strtod(current, &next); 56 double value = strtod(current, &next);
55 while(next != current) { 57 while(next != current) {
56 printf("Value: %f\n", value);
57 buf[(*nelements)++] = value; 58 buf[(*nelements)++] = value;
58 if((*nelements) == size) { 59 if((*nelements) == size) {
59 size *= 2; 60 size *= 2;
60 buf = (double *) realloc(buf, 2 * size * sizeof(double)); 61 buf = (double *) realloc(buf, 2 * size * sizeof(double));
61 } 62 }
63 value = strtod(current, &next); 64 value = strtod(current, &next);
64 } 65 }
65 return buf; 66 return buf;
66 } 67 }
67 68
68 int main() { 69 int main(int argc, char* argv[]) {
70
71 if(argc != 3)
72 {
73 fprintf(stderr, "Usage: %s <n3file> <dbfile>\n", argv[0]);
74 return 2;
75 }
76
77 char* n3file = argv[1];
78 char* dbfile = argv[2];
79
69 librdf_world *world; 80 librdf_world *world;
70 if (!(world = librdf_new_world())) 81 if (!(world = librdf_new_world()))
71 goto librdf_error; 82 goto librdf_error;
72 83
73 librdf_storage *storage; 84 librdf_storage *storage;
75 goto librdf_error; 86 goto librdf_error;
76 87
77 librdf_model *model; 88 librdf_model *model;
78 if (!(model = librdf_new_model(world, storage, NULL))) 89 if (!(model = librdf_new_model(world, storage, NULL)))
79 goto librdf_error; 90 goto librdf_error;
91
92
93 char *fileUri = malloc(sizeof(char)*(strlen(n3file)+6));
94 sprintf(fileUri, "file:%s\0", n3file);
80 95
81 librdf_uri *uri; 96 librdf_uri *uri;
82 if (!(uri = librdf_new_uri(world, "file:data/test.n3"))) 97 if (!(uri = librdf_new_uri(world, fileUri)))
83 goto librdf_error; 98 goto librdf_error;
99
100 free(fileUri);
84 101
85 librdf_parser *parser; 102 librdf_parser *parser;
86 if (!(parser = librdf_new_parser(world, "guess", NULL, NULL))) 103 if (!(parser = librdf_new_parser(world, "guess", NULL, NULL)))
87 goto librdf_error; 104 goto librdf_error;
88 105
99 116
100 if(!librdf_query_results_is_bindings(results)) 117 if(!librdf_query_results_is_bindings(results))
101 goto librdf_error; 118 goto librdf_error;
102 119
103 adb_t *adb; 120 adb_t *adb;
104 if(!(adb = audiodb_open("keyplot.adb", O_RDWR))) { 121 if(!(adb = audiodb_open(dbfile, O_RDWR))) {
122
105 struct stat st; 123 struct stat st;
106 if(!(stat("keyplot.adb", &st))) { 124 if(!(stat(dbfile, &st))) {
107 fprintf(stderr, "keyplot.adb not opened.\n"); 125 fprintf(stderr, "%s not opened.\n", dbfile);
108 return 1; 126 return 1;
109 } else { 127 } else {
110 /* FIXME: if we are doing a proper SPARQL query over a 128 /* FIXME: if we are doing a proper SPARQL query over a
111 * potentially unbounded number of results, we could use 129 * potentially unbounded number of results, we could use
112 * librdf_query_results_get_count() to estimate how much space 130 * librdf_query_results_get_count() to estimate how much space
123 * we maybe do a preliminary query to find out the total time 141 * we maybe do a preliminary query to find out the total time
124 * (or similar) of all the tracks we're about to insert. 142 * (or similar) of all the tracks we're about to insert.
125 143
126 * (also NOTE: this audiodb_create() interface is scheduled for 144 * (also NOTE: this audiodb_create() interface is scheduled for
127 * being made less inelegant.) */ 145 * being made less inelegant.) */
128 if(!(adb = audiodb_create("keyplot.adb", 0, 0, 0))) { 146 if(!(adb = audiodb_create(dbfile, 0, 0, 0))) {
129 fprintf(stderr, "failed to create keyplot.adb.\n"); 147 fprintf(stderr, "failed to create %s.\n", dbfile);
130 return 1; 148 return 1;
131 } 149 }
132 } 150 }
133 } 151 }
134 152
153 if(librdf_query_results_finished(results))
154 {
155 fprintf(stderr, "No features found!\n");
156 return 3;
157 }
158
159
135 while(!librdf_query_results_finished(results)) { 160 while(!librdf_query_results_finished(results)) {
136 int count = librdf_query_results_get_bindings_count(results); 161
137 adb_datum_t datum = {0}; 162 adb_datum_t datum = {0};
138 datum.dim = 25; 163
139 for (int i = 0; i < count; i++) { 164 // Pull out the dimension
140 const char *name = librdf_query_results_get_binding_name(results, i); 165
141 librdf_node *node = librdf_query_results_get_binding_value(results, i); 166 librdf_node *node = librdf_query_results_get_binding_value_by_name(results, "dimensions");
142 if(!node) return 2; 167 char* dimensions = librdf_node_get_literal_value(node);
143 168
144 if(!strcmp(name, "key")) { 169 int dimension = 0;
145 datum.key = librdf_uri_as_string(librdf_node_get_uri(node)); 170 sscanf(dimensions, "%d", &dimension);
146 } else if(!strcmp(name, "value")) { 171 datum.dim = dimension;
147 size_t nelements = 0; 172
148 datum.data = parse_value_string(librdf_node_get_literal_value(node), &nelements); 173 node = librdf_query_results_get_binding_value_by_name(results, "typetitle");
149 if(nelements % 25) return 4; 174 printf("Insert feature of type %s\n", librdf_node_get_literal_value(node));
150 datum.nvectors = nelements / 25; 175
151 } else { 176 // Grab key and value
152 /* do something with the timeline (and other) information */ 177
153 printf("%s: %s\n", name, librdf_node_get_literal_value(node)); 178 node = librdf_query_results_get_binding_value_by_name(results, "key");
154 } 179 datum.key = librdf_uri_as_string(librdf_node_get_uri(node));
155 librdf_free_node(node); 180
156 } 181 size_t nelements = 0;
182 node = librdf_query_results_get_binding_value_by_name(results, "value");
183 datum.data = parse_value_string(librdf_node_get_literal_value(node), &nelements);
184
185 // Validate that we have the correct number of elements
186
187 if(nelements % dimension) return 4;
188 datum.nvectors = nelements / dimension;
189
190 printf("Dimension: %d\n", dimension);
191 printf("Key: %s\n", datum.key);
192 printf("Vectors: %d\n", datum.nvectors);
193
194 librdf_free_node(node);
195
157 if(audiodb_insert_datum(adb, &datum)) { 196 if(audiodb_insert_datum(adb, &datum)) {
158 fprintf(stderr, "failed to insert datum with key %s.\n", datum.key); 197 fprintf(stderr, "failed to insert datum with key %s.\n", datum.key);
159 return 1; 198 return 1;
160 } 199 }
161 free(datum.data); 200 free(datum.data);