diff options
author | David Robillard <d@drobilla.net> | 2007-04-20 21:25:38 +0000 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2007-04-20 21:25:38 +0000 |
commit | 09e5c017b096bc84bbbc0b273baffd221144213a (patch) | |
tree | 9a591b9b23132cd0e98e5ec85a20256290e5a141 /src | |
parent | 500fab47e5e9aadb6e09a71a6c3447e97f3a6d6a (diff) | |
download | lilv-09e5c017b096bc84bbbc0b273baffd221144213a.tar.gz lilv-09e5c017b096bc84bbbc0b273baffd221144213a.tar.bz2 lilv-09e5c017b096bc84bbbc0b273baffd221144213a.zip |
Preliminary categories support.
git-svn-id: http://svn.drobilla.net/lad/slv2@465 a436a847-0d15-0410-975c-d299462d15a1
Diffstat (limited to 'src')
-rw-r--r-- | src/Makefile.am | 2 | ||||
-rw-r--r-- | src/categories.c | 90 | ||||
-rw-r--r-- | src/category.c | 57 | ||||
-rw-r--r-- | src/plugin.c | 41 | ||||
-rw-r--r-- | src/pluginlist.c | 6 | ||||
-rw-r--r-- | src/private_types.h | 24 | ||||
-rw-r--r-- | src/query.c | 21 | ||||
-rw-r--r-- | src/world.c | 67 |
8 files changed, 264 insertions, 44 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index 8009ff8..cc5a774 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -7,6 +7,8 @@ libslv2_la_LIBADD = @REDLAND_LIBS@ libslv2_la_SOURCES = \ private_types.h \ world.c \ + category.c \ + categories.c \ plugin.c \ query.c \ port.c \ diff --git a/src/categories.c b/src/categories.c new file mode 100644 index 0000000..133e7ee --- /dev/null +++ b/src/categories.c @@ -0,0 +1,90 @@ +/* SLV2 + * Copyright (C) 2007 Dave Robillard <http://drobilla.net> + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation; either version 2 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#define _XOPEN_SOURCE 500 + +#include <string.h> +#include <limits.h> +#include <librdf.h> +#include <slv2/category.h> +#include <slv2/categories.h> +#include "private_types.h" + + +SLV2Categories +slv2_categories_new() +{ + return raptor_new_sequence((void (*)(void*))&slv2_category_free, NULL); +} + + +void +slv2_categories_free(SLV2Categories list) +{ + //if (list != world->categories) + raptor_free_sequence(list); +} + + +unsigned +slv2_categories_size(SLV2Categories list) +{ + return raptor_sequence_size(list); +} + + +SLV2Category +slv2_categories_get_by_uri(SLV2Categories list, const char* uri) +{ + // good old fashioned binary search + + int lower = 0; + int upper = raptor_sequence_size(list) - 1; + int i; + + if (upper == 0) + return NULL; + + while (upper >= lower) { + i = lower + ((upper - lower) / 2); + + SLV2Category p = raptor_sequence_get_at(list, i); + + int cmp = strcmp(slv2_category_get_uri(p), uri); + + if (cmp == 0) + return p; + else if (cmp > 0) + upper = i - 1; + else + lower = i + 1; + } + + return NULL; +} + + +SLV2Category +slv2_categories_get_at(SLV2Categories list, unsigned index) +{ + if (index > INT_MAX) + return NULL; + else + return (SLV2Category)raptor_sequence_get_at(list, (int)index); +} + diff --git a/src/category.c b/src/category.c new file mode 100644 index 0000000..5eae2af --- /dev/null +++ b/src/category.c @@ -0,0 +1,57 @@ +/* SLV2 + * Copyright (C) 2007 Dave Robillard <http://drobilla.net> + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation; either version 2 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#define _XOPEN_SOURCE 500 + +#include <stdlib.h> +#include <string.h> +#include <slv2/category.h> +#include "private_types.h" + + +SLV2Category +slv2_category_new(const char* uri, const char* label) +{ + SLV2Category category = (SLV2Category)malloc(sizeof(struct _Category)); + category->uri = strdup(uri); + category->label = strdup(label); + return category; +} + + +void +slv2_category_free(SLV2Category category) +{ + free(category->uri); + free(category->label); + free(category); +} + + +const char* +slv2_category_get_uri(SLV2Category category) +{ + return category->uri; +} + + +const char* +slv2_category_get_label(SLV2Category category) +{ + return category->label; +} diff --git a/src/plugin.c b/src/plugin.c index 722caa1..7fe8e1d 100644 --- a/src/plugin.c +++ b/src/plugin.c @@ -37,6 +37,7 @@ slv2_plugin_new(SLV2World world, librdf_uri* uri, const char* binary_uri) plugin->world = world; plugin->plugin_uri = librdf_new_uri_from_uri(uri); plugin->binary_uri = strdup(binary_uri); + plugin->category = NULL; plugin->data_uris = slv2_strings_new(); plugin->ports = raptor_new_sequence((void (*)(void*))&slv2_port_free, NULL); plugin->storage = NULL; @@ -145,8 +146,32 @@ slv2_plugin_load(SLV2Plugin p) librdf_free_uri(data_uri); } - // Load ports + // Load category const unsigned char* query = (const unsigned char*) + "SELECT DISTINCT ?class WHERE { <> a ?class }"; + + librdf_query* q = librdf_new_query(p->world->world, "sparql", + NULL, query, p->plugin_uri); + + librdf_query_results* results = librdf_query_execute(q, p->rdf); + + while (!librdf_query_results_finished(results)) { + librdf_node* class_node = librdf_query_results_get_binding_value(results, 0); + librdf_uri* class_uri = librdf_node_get_uri(class_node); + const char* class_uri_str = (const char*)librdf_uri_as_string(class_uri); + + SLV2Category category = slv2_categories_get_by_uri(p->world->categories, class_uri_str); + + if (category) { + p->category = category; + break; + } + + librdf_query_results_next(results); + } + + // Load ports + query = (const unsigned char*) "PREFIX : <http://lv2plug.in/ontology#>\n" "SELECT DISTINCT ?port ?symbol ?index WHERE {\n" "<> :port ?port .\n" @@ -154,10 +179,10 @@ slv2_plugin_load(SLV2Plugin p) " :index ?index .\n" "}"; - librdf_query* q = librdf_new_query(p->world->world, "sparql", + q = librdf_new_query(p->world->world, "sparql", NULL, query, p->plugin_uri); - librdf_query_results* results = librdf_query_execute(q, p->rdf); + results = librdf_query_execute(q, p->rdf); while (!librdf_query_results_finished(results)) { @@ -217,6 +242,16 @@ slv2_plugin_get_library_uri(SLV2Plugin p) } +SLV2Category +slv2_plugin_get_category(SLV2Plugin p) +{ + if (!p->category) + slv2_plugin_load(p); + + return p->category; +} + + bool slv2_plugin_verify(SLV2Plugin plugin) { diff --git a/src/pluginlist.c b/src/pluginlist.c index d4cd058..da003a5 100644 --- a/src/pluginlist.c +++ b/src/pluginlist.c @@ -17,13 +17,11 @@ */ #define _XOPEN_SOURCE 500 -#include <limits.h> #include <string.h> +#include <limits.h> #include <stdio.h> #include <stdlib.h> -#include <sys/types.h> #include <assert.h> -#include <dirent.h> #include <librdf.h> #include <slv2/types.h> #include <slv2/plugin.h> @@ -308,8 +306,6 @@ slv2_plugins_get_by_uri(SLV2Plugins list, const char* uri) SLV2Plugin slv2_plugins_get_at(SLV2Plugins list, unsigned index) { - assert(list); - if (index > INT_MAX) return NULL; else diff --git a/src/private_types.h b/src/private_types.h index 48bed0a..ff4580c 100644 --- a/src/private_types.h +++ b/src/private_types.h @@ -27,6 +27,7 @@ extern "C" { #include <stddef.h> #include <librdf.h> #include <slv2/pluginlist.h> +#include <slv2/categories.h> /** Reference to a port on some plugin. @@ -53,6 +54,7 @@ struct _Plugin { librdf_uri* plugin_uri; // char* bundle_url; // Bundle directory plugin was loaded from char* binary_uri; // lv2:binary + SLV2Category category; raptor_sequence* data_uris; // rdfs::seeAlso raptor_sequence* ports; librdf_storage* storage; @@ -78,14 +80,26 @@ struct _InstanceImpl { }; +struct _Category { + char* uri; + char* label; +}; + +SLV2Category slv2_category_new(const char* uri, const char* label); +void slv2_category_free(SLV2Category category); + +SLV2Categories slv2_categories_new(); +void slv2_categories_free(); + /** Model of LV2 (RDF) data loaded from bundles. */ struct _World { - librdf_world* world; - librdf_storage* storage; - librdf_model* model; - librdf_parser* parser; - SLV2Plugins plugins; + librdf_world* world; + librdf_storage* storage; + librdf_model* model; + librdf_parser* parser; + SLV2Categories categories; + SLV2Plugins plugins; }; /** Load all bundles found in \a search_path. diff --git a/src/query.c b/src/query.c index 968834c..56a7412 100644 --- a/src/query.c +++ b/src/query.c @@ -132,19 +132,6 @@ slv2_plugin_query(SLV2Plugin plugin, return NULL; } - // Add LV2 ontology to query sources - //librdf_query_add_data_graph(rq, slv2_ontology_uri, - // NULL, RASQAL_DATA_GRAPH_BACKGROUND); - - // Add all plugin data files to query sources - /*for (unsigned i=0; i < slv2_strings_size(plugin->data_uris); ++i) { - const char* file_uri_str = slv2_strings_get_at(plugin->data_uris, i); - raptor_uri* file_uri = raptor_new_uri((const unsigned char*)file_uri_str); - librdf_query_add_data_graph(rq, file_uri, - NULL, RASQAL_DATA_GRAPH_BACKGROUND); - raptor_free_uri(file_uri); - }*/ - librdf_query_results* results = librdf_query_execute(rq, plugin->rdf); librdf_free_query(rq); @@ -190,7 +177,7 @@ slv2_plugin_query_count(SLV2Plugin plugin, } } - +/* size_t slv2_query_count_results(SLV2Plugin p, const char* query) @@ -205,10 +192,6 @@ slv2_query_count_results(SLV2Plugin p, //printf("Query: \n%s\n\n", query_str); - // Add LV2 ontology to query sources - //librdf_query_add_data_graph(rq, slv2_ontology_uri, - // NULL, RASQAL_DATA_GRAPH_BACKGROUND); - librdf_query_results* results = librdf_query_execute(rq, p->world->model); assert(results); @@ -221,4 +204,4 @@ slv2_query_count_results(SLV2Plugin p, return count; } - +*/ diff --git a/src/world.c b/src/world.c index a682a35..c8735ea 100644 --- a/src/world.c +++ b/src/world.c @@ -44,11 +44,10 @@ slv2_world_new() world->parser = librdf_new_parser(world->world, "turtle", NULL, NULL); + world->categories = slv2_categories_new(); + world->plugins = slv2_plugins_new(); - /*slv2_ontology_uri = raptor_new_uri((const unsigned char*) - "file://" LV2_TTL_PATH);*/ - return world; } @@ -56,14 +55,14 @@ slv2_world_new() void slv2_world_free(SLV2World world) { - /*raptor_free_uri(slv2_ontology_uri); - slv2_ontology_uri = NULL;*/ - for (int i=0; i < raptor_sequence_size(world->plugins); ++i) slv2_plugin_free(raptor_sequence_get_at(world->plugins, i)); raptor_free_sequence(world->plugins); world->plugins = NULL; + slv2_categories_free(world->categories); + world->categories = NULL; + librdf_free_parser(world->parser); world->parser = NULL; @@ -163,29 +162,73 @@ slv2_plugin_compare_by_uri(const void* a, const void* b) } */ + +void +slv2_world_load_categories(SLV2World world) +{ + // FIXME: This will need to be a bit more clever when more data is around + // then the ontology (ie classes which aren't LV2 categories) + + unsigned char* query_string = (unsigned char*) + "PREFIX : <http://lv2plug.in/ontology#>\n" + "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n" + "SELECT DISTINCT ?category ?label\n" + "WHERE { ?category a rdfs:Class; rdfs:label ?label }\n" + "ORDER BY ?category\n"; + + librdf_query* q = librdf_new_query(world->world, "sparql", + NULL, query_string, NULL); + + librdf_query_results* results = librdf_query_execute(q, world->model); + + while (!librdf_query_results_finished(results)) { + librdf_node* category_node = librdf_query_results_get_binding_value(results, 0); + librdf_uri* category_uri = librdf_node_get_uri(category_node); + librdf_node* label_node = librdf_query_results_get_binding_value(results, 1); + const char* label = (const char*)librdf_node_get_literal_value(label_node); + + //printf("CATEGORY: %s (%s)\n", librdf_uri_as_string(category_uri), label); + SLV2Category category = slv2_category_new( + (const char*)librdf_uri_as_string(category_uri), + label); + raptor_sequence_push(world->categories, category); + + librdf_query_results_next(results); + } + +} + + void slv2_world_load_all(SLV2World world) { char* lv2_path = getenv("LV2_PATH"); - /* 1. Read all manifest files into model */ + /* 1. Read LV2 ontology into model */ + librdf_uri* ontology_uri = librdf_new_uri(world->world, + (const unsigned char*)"file://" LV2_TTL_PATH); + librdf_parser_parse_into_model(world->parser, ontology_uri, NULL, world->model); + librdf_free_uri(ontology_uri); + + + /* 2. Read all manifest files into model */ if (lv2_path) { slv2_world_load_path(world, lv2_path); } else { const char* const home = getenv("HOME"); - const char* const suffix = "/.lv2:/usr/local/lib/lv2:usr/lib/lv2"; + const char* const suffix = "/.lv2:/usr/local/lib/lv2:/usr/lib/lv2"; lv2_path = slv2_strjoin(home, suffix, NULL); - //fprintf(stderr, "$LV2_PATH is unset. Using default path %s\n", lv2_path); - slv2_world_load_path(world, lv2_path); free(lv2_path); } - /* 2. Query out things to cache */ + /* 3. Query out things to cache */ + + slv2_world_load_categories(world); // Find all plugins and associated data files unsigned char* query_string = (unsigned char*) @@ -233,7 +276,7 @@ slv2_world_load_all(SLV2World world) librdf_query_results_next(results); } - // ORDER BY should (and appears to actually) guarantee this + // 'ORDER BY ?plugin' guarantees this //raptor_sequence_sort(world->plugins, slv2_plugin_compare_by_uri); if (results) |