[tracker/wip/carlosg/hotdoc: 18/47] docs: Rewrite data model of documentation tool




commit 99ff643e823ee878698822a7677c6054956a632c
Author: Carlos Garnacho <carlosg gnome org>
Date:   Sat May 29 21:24:28 2021 +0200

    docs: Rewrite data model of documentation tool
    
    Instead of parsing ontology TTL by itself, use 2 in-memory
    TrackerSparqlConnections for the task, one to load .description
    files, and another with an empty database.
    
    With these in place, the ontology is fully introspectable, we
    can then query these to fill in our information about the defined
    classes and properties.
    
    One advantage here is that we avoid purpose-specific Turtle
    file parsers and unify this on the sparql library.

 docs/tools/dsc-ontology.gresource.xml              |   5 +
 docs/tools/meson.build                             |  14 +-
 docs/tools/ontology/10-dsc.ontology                |  65 +++
 docs/tools/tracker-ontology-model.c                | 590 +++++++++++++++++++++
 .../{ttl_model.h => tracker-ontology-model.h}      |  50 +-
 docs/tools/ttl2xml.c                               | 110 +---
 docs/tools/ttl_loader.c                            | 460 ----------------
 docs/tools/ttl_loader.h                            |  45 --
 docs/tools/ttl_model.c                             | 206 -------
 docs/tools/ttl_xml.c                               | 121 +++--
 docs/tools/ttl_xml.h                               |  12 +-
 docs/tools/ttlresource2xml.c                       | 181 +++----
 docs/tools/ttlresource2xml.h                       |  18 +-
 13 files changed, 881 insertions(+), 996 deletions(-)
---
diff --git a/docs/tools/dsc-ontology.gresource.xml b/docs/tools/dsc-ontology.gresource.xml
new file mode 100644
index 000000000..c5f9b176f
--- /dev/null
+++ b/docs/tools/dsc-ontology.gresource.xml
@@ -0,0 +1,5 @@
+<gresources>
+  <gresource prefix="/org/freedesktop/tracker/doctool">
+    <file>ontology/10-dsc.ontology</file>
+  </gresource>
+</gresources>
diff --git a/docs/tools/meson.build b/docs/tools/meson.build
index e3672fe2c..6e7ad958e 100644
--- a/docs/tools/meson.build
+++ b/docs/tools/meson.build
@@ -1,11 +1,15 @@
 # These tools are used for generating the ontologies documentation.
 
-ttl_loader_files = [
-    'ttl_loader.c',
-    'ttl_model.c',
+doctool_gresources = gnome.compile_resources('tracker_doctool_gresources', 'dsc-ontology.gresource.xml')
+
+doctool_files = [
+    'tracker-ontology-model.c',
     'ttl_xml.c',
+    'ttlresource2xml.c',
+    'ttl2xml.c',
 ]
 
 ttl2xml = executable('ttl2xml',
-    ttl_loader_files, 'ttl2xml.c', 'ttlresource2xml.c',
-    dependencies: [tracker_data_dep, tracker_sparql_dep])
+    doctool_files,
+    doctool_gresources,
+    dependencies: [tracker_sparql_dep])
diff --git a/docs/tools/ontology/10-dsc.ontology b/docs/tools/ontology/10-dsc.ontology
new file mode 100644
index 000000000..5ff624ad1
--- /dev/null
+++ b/docs/tools/ontology/10-dsc.ontology
@@ -0,0 +1,65 @@
+# Ontology to load ontology descriptions, coming
+# as .description files alongside ontologies.
+@prefix dsc: <http://tracker.api.gnome.org/ontology/v3/dsc#> .
+@prefix nrl: <http://tracker.api.gnome.org/ontology/v3/nrl#> .
+@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
+@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
+@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
+
+dsc: a nrl:Namespace, nrl:Ontology ;
+       nrl:prefix "dsc" ;
+       nrl:lastModified "2021-05-28T14:00:00Z" .
+
+dsc:Ontology a rdfs:Class ;
+       rdfs:subClassOf rdfs:Resource .
+
+dsc:title a rdf:Property ;
+       nrl:maxCardinality 1 ;
+       rdfs:domain dsc:Ontology ;
+       rdfs:range xsd:string .
+
+dsc:description a rdf:Property ;
+       nrl:maxCardinality 1 ;
+       rdfs:domain dsc:Ontology ;
+       rdfs:range xsd:string .
+
+dsc:author a rdf:Property ;
+       rdfs:domain dsc:Ontology ;
+       rdfs:range xsd:string .
+
+dsc:editor a rdf:Property ;
+       rdfs:domain dsc:Ontology ;
+       rdfs:range xsd:string .
+
+dsc:contributor a rdf:Property ;
+       rdfs:domain dsc:Ontology ;
+       rdfs:range xsd:string .
+
+dsc:upstream a rdf:Property ;
+       nrl:maxCardinality 1 ;
+       rdfs:domain dsc:Ontology ;
+       rdfs:range xsd:string .
+
+dsc:gitlog a rdf:Property ;
+       nrl:maxCardinality 1 ;
+       rdfs:domain dsc:Ontology ;
+       rdfs:range xsd:string .
+
+dsc:localPrefix a rdf:Property ;
+       nrl:maxCardinality 1 ;
+       rdfs:domain dsc:Ontology ;
+       rdfs:range xsd:string .
+
+dsc:baseUrl a rdf:Property ;
+       nrl:maxCardinality 1 ;
+       rdfs:domain dsc:Ontology ;
+       rdfs:range xsd:string .
+
+dsc:relativePath a rdf:Property ;
+       nrl:maxCardinality 1 ;
+       rdfs:domain dsc:Ontology ;
+       rdfs:range xsd:string .
+
+dsc:copyright a rdf:Property ;
+       rdfs:domain dsc:Ontology ;
+       rdfs:range xsd:string .
diff --git a/docs/tools/tracker-ontology-model.c b/docs/tools/tracker-ontology-model.c
new file mode 100644
index 000000000..c584e47ac
--- /dev/null
+++ b/docs/tools/tracker-ontology-model.c
@@ -0,0 +1,590 @@
+/*
+ * Copyright (C) 2009, Nokia <ivan frade nokia com>
+ *
+ * This program 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 program 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 Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+#include <libtracker-sparql/tracker-sparql.h>
+
+#include "tracker-ontology-model.h"
+
+struct _TrackerOntologyModel
+{
+       TrackerSparqlConnection *ontology_conn;
+       TrackerSparqlConnection *desc_conn;
+
+       TrackerSparqlStatement *classes_stmt;
+       TrackerSparqlStatement *props_stmt;
+       GHashTable *stmts;
+
+       GHashTable *classes;
+       GHashTable *properties;
+       GHashTable *descriptions;
+};
+
+TrackerOntologyClass *
+ttl_model_class_new (const gchar *classname)
+{
+       TrackerOntologyClass *def = NULL;
+
+       def = g_new0 (TrackerOntologyClass, 1);
+       def->classname = g_strdup (classname);
+
+       return def;
+}
+
+void
+ttl_model_class_free (TrackerOntologyClass *def)
+{
+       g_free (def->classname);
+       g_free (def->shortname);
+       g_free (def->basename);
+
+       g_list_free_full (def->superclasses, (GDestroyNotify) g_free);
+       g_list_free_full (def->subclasses, (GDestroyNotify) g_free);
+       g_list_free_full (def->in_domain_of, (GDestroyNotify) g_free);
+       g_list_free_full (def->in_range_of, (GDestroyNotify) g_free);
+
+       g_free (def->description);
+       g_free (def->specification);
+
+       g_list_free_full (def->instances, (GDestroyNotify) g_free);
+
+       g_free (def);
+}
+
+TrackerOntologyProperty *
+ttl_model_property_new (const gchar *propname)
+{
+       TrackerOntologyProperty *prop;
+
+       prop = g_new0 (TrackerOntologyProperty, 1);
+       prop->propertyname = g_strdup (propname);
+
+       return prop;
+}
+
+void
+ttl_model_property_free (TrackerOntologyProperty *def)
+{
+       g_free (def->propertyname);
+       g_free (def->shortname);
+       g_free (def->basename);
+
+       g_list_free_full (def->domain, (GDestroyNotify) g_free);
+       g_list_free_full (def->range, (GDestroyNotify) g_free);
+       g_list_free_full (def->superproperties, (GDestroyNotify) g_free);
+       g_list_free_full (def->subproperties, (GDestroyNotify) g_free);
+
+       g_free (def->max_cardinality);
+       g_free (def->description);
+       g_free (def->weight);
+       g_free (def->specification);
+       g_free (def);
+}
+
+TrackerOntologyDescription *
+ttl_model_description_new (void)
+{
+       TrackerOntologyDescription *desc;
+
+       desc = g_new0 (TrackerOntologyDescription, 1);
+
+       return desc;
+}
+
+void
+ttl_model_description_free (TrackerOntologyDescription *desc)
+{
+       g_free (desc->title);
+       g_free (desc->description);
+
+       g_list_free_full (desc->authors, (GDestroyNotify) g_free);
+       g_list_free_full (desc->editors, (GDestroyNotify) g_free);
+       g_list_free_full (desc->contributors, (GDestroyNotify) g_free);
+
+       g_free (desc->gitlog);
+       g_free (desc->upstream);
+       g_free (desc->copyright);
+
+       g_free (desc->baseUrl);
+       g_free (desc->relativePath);
+       g_free (desc->localPrefix);
+
+       g_free (desc);
+}
+
+static void
+fill_in_list (TrackerSparqlConnection  *conn,
+              TrackerOntologyModel     *model,
+              GList                   **list,
+              const gchar              *value,
+              const gchar              *query)
+{
+       TrackerSparqlStatement *stmt;
+       TrackerSparqlCursor *cursor;
+       GError *error = NULL;
+
+       stmt = g_hash_table_lookup (model->stmts, query);
+       if (!stmt) {
+               stmt = tracker_sparql_connection_query_statement (conn, query, NULL, &error);
+               g_assert_no_error (error);
+               g_hash_table_insert (model->stmts, g_strdup (query), stmt);
+       }
+
+       tracker_sparql_statement_bind_string (stmt, "var", value);
+       cursor = tracker_sparql_statement_execute (stmt, NULL, &error);
+       g_assert_no_error (error);
+
+       while (tracker_sparql_cursor_next (cursor, NULL, &error)) {
+               const gchar *editor = tracker_sparql_cursor_get_string (cursor, 0, NULL);
+               *list = g_list_prepend (*list, g_strdup (editor));
+       }
+
+       g_assert_no_error (error);
+       g_object_unref (cursor);
+}
+
+static void
+tracker_ontology_model_init_classes (TrackerOntologyModel *model)
+{
+       TrackerOntologyClass *klass;
+       TrackerSparqlCursor *cursor;
+       GError *error = NULL;
+
+       cursor = tracker_sparql_connection_query (model->ontology_conn,
+                                                 "SELECT "
+                                                 "  ?c"
+                                                 "  nrl:classSpecification(?c)"
+                                                 "  rdfs:comment(?c)"
+                                                 "  nrl:notify(?c)"
+                                                 "  nrl:deprecated(?c)"
+                                                 "  (SUBSTR(str(?c), strlen(?o) + 1) AS ?basename)"
+                                                 "  (CONCAT(?prefix, ':', SUBSTR(str(?c), strlen(?o) + 1)) 
AS ?shortname)"
+                                                 "{"
+                                                 "  ?c a rdfs:Class ."
+                                                 "  ?o a nrl:Namespace ;"
+                                                 "     nrl:prefix ?prefix ."
+                                                 "  FILTER (STRSTARTS(?c, ?o))"
+                                                 "}",
+                                                 NULL,
+                                                 &error);
+       g_assert_no_error (error);
+
+       while (tracker_sparql_cursor_next (cursor, NULL, &error)) {
+               const gchar *class_name;
+
+               class_name = tracker_sparql_cursor_get_string (cursor, 0, NULL);
+
+               klass = ttl_model_class_new (class_name);
+               klass->specification = g_strdup (tracker_sparql_cursor_get_string (cursor, 1, NULL));
+               klass->description = g_strdup (tracker_sparql_cursor_get_string (cursor, 2, NULL));
+               klass->notify = tracker_sparql_cursor_get_boolean (cursor, 3);
+               klass->deprecated = tracker_sparql_cursor_get_boolean (cursor, 4);
+               klass->basename = g_strdup (tracker_sparql_cursor_get_string (cursor, 5, NULL));
+               klass->shortname = g_strdup (tracker_sparql_cursor_get_string (cursor, 6, NULL));
+
+               fill_in_list (model->ontology_conn, model, &klass->superclasses, class_name,
+                             "SELECT ?super {"
+                             "  ~var rdfs:subClassOf ?super"
+                             "} ORDER BY DESC ?super");
+               fill_in_list (model->ontology_conn, model, &klass->subclasses, class_name,
+                             "SELECT ?sub {"
+                             "  ?sub rdfs:subClassOf ~var"
+                             "} ORDER BY DESC ?sub");
+               fill_in_list (model->ontology_conn, model, &klass->in_domain_of, class_name,
+                             "SELECT ?prop {"
+                             "  ?prop rdfs:domain ~var"
+                             "} ORDER BY DESC ?prop");
+               fill_in_list (model->ontology_conn, model, &klass->in_range_of, class_name,
+                             "SELECT ?prop {"
+                             "  ?prop rdfs:range ~var"
+                             "} ORDER BY DESC ?prop");
+               fill_in_list (model->ontology_conn, model, &klass->instances, class_name,
+                             "SELECT (CONCAT(?prefix, ':', SUBSTR(str(?c), strlen(?o) + 1)) AS ?shortname) {"
+                             "  ?c a ~var ."
+                             "  ?o a nrl:Namespace ;"
+                             "     nrl:prefix ?prefix ."
+                             "  FILTER (STRSTARTS(?c, ?o))"
+                             "} ORDER BY DESC ?shortname");
+
+               g_hash_table_insert (model->classes, klass->classname, klass);
+       }
+
+       g_assert_no_error (error);
+       g_object_unref (cursor);
+}
+
+TrackerOntologyProperty *
+tracker_ontology_model_init_properties (TrackerOntologyModel *model)
+{
+       TrackerOntologyProperty *prop;
+       TrackerSparqlCursor *cursor;
+       GError *error = NULL;
+
+       cursor = tracker_sparql_connection_query (model->ontology_conn,
+                                                 "SELECT "
+                                                 "  ?p"
+                                                 "  nrl:propertySpecification(?p)"
+                                                 "  rdfs:comment(?p)"
+                                                 "  nrl:maxCardinality(?p)"
+                                                 "  nrl:deprecated(?p)"
+                                                 "  nrl:fulltextIndexed(?p)"
+                                                 "  nrl:weight(?p)"
+                                                 "  (SUBSTR(str(?p), strlen(?o) + 1) AS ?basename)"
+                                                 "  (CONCAT(?prefix, ':', SUBSTR(str(?p), strlen(?o) + 1)) 
AS ?shortname)"
+                                                 "{"
+                                                 "  ?p a rdf:Property ."
+                                                 "  ?o a nrl:Namespace ;"
+                                                 "     nrl:prefix ?prefix ."
+                                                 "  FILTER (STRSTARTS(?p, ?o))"
+                                                 "}",
+                                                 NULL,
+                                                 &error);
+       g_assert_no_error (error);
+
+       while (tracker_sparql_cursor_next (cursor, NULL, &error)) {
+               const gchar *prop_name;
+
+               prop_name = tracker_sparql_cursor_get_string (cursor, 0, NULL);
+
+               prop = ttl_model_property_new (prop_name);
+               prop->specification = g_strdup (tracker_sparql_cursor_get_string (cursor, 1, NULL));
+               prop->description = g_strdup (tracker_sparql_cursor_get_string (cursor, 2, NULL));
+               prop->max_cardinality = g_strdup (tracker_sparql_cursor_get_string (cursor, 3, NULL));
+               prop->deprecated = tracker_sparql_cursor_get_boolean (cursor, 4);
+               prop->fulltextIndexed = tracker_sparql_cursor_get_boolean (cursor, 5);
+               prop->weight = g_strdup (tracker_sparql_cursor_get_string (cursor, 6, NULL));
+               prop->basename = g_strdup (tracker_sparql_cursor_get_string (cursor, 7, NULL));
+               prop->shortname = g_strdup (tracker_sparql_cursor_get_string (cursor, 8, NULL));
+
+               fill_in_list (model->ontology_conn, model, &prop->domain, prop_name,
+                             "SELECT ?class {"
+                             "  ~var rdfs:domain ?class"
+                             "} ORDER BY DESC ?class");
+               fill_in_list (model->ontology_conn, model, &prop->range, prop_name,
+                             "SELECT ?class {"
+                             "  ~var rdfs:range ?class"
+                             "} ORDER BY DESC ?class");
+               fill_in_list (model->ontology_conn, model, &prop->superproperties, prop_name,
+                             "SELECT ?prop {"
+                             "  ~var rdfs:subPropertyOf ?prop"
+                             "} ORDER BY DESC ?prop");
+               fill_in_list (model->ontology_conn, model, &prop->subproperties, prop_name,
+                             "SELECT ?prop {"
+                             "  ?prop rdfs:subPropertyOf ~var"
+                             "} ORDER BY DESC ?prop");
+
+               g_hash_table_insert (model->properties, prop->propertyname, prop);
+       }
+
+       g_assert_no_error (error);
+       g_object_unref (cursor);
+
+       return prop;
+}
+
+TrackerOntologyModel *
+tracker_ontology_model_new (GFile   *ontology_location,
+                           GError **error)
+{
+       TrackerOntologyModel *model;
+       TrackerSparqlConnection *ontology_conn, *desc_conn;
+       GFileEnumerator *enumerator;
+       GFileInfo *info;
+       GFile *dsc_ontology;
+
+       ontology_conn = tracker_sparql_connection_new (TRACKER_SPARQL_CONNECTION_FLAGS_NONE,
+                                                      NULL, ontology_location, NULL,
+                                                      error);
+       if (!ontology_conn)
+               goto error;
+
+       dsc_ontology = g_file_new_for_uri ("resource:///org/freedesktop/tracker/doctool/ontology");
+       desc_conn = tracker_sparql_connection_new (TRACKER_SPARQL_CONNECTION_FLAGS_NONE,
+                                                  NULL,
+                                                  dsc_ontology,
+                                                  NULL,
+                                                  error);
+       g_clear_object (&dsc_ontology);
+
+       if (!desc_conn)
+               goto error;
+
+       /* Load all .description files into desc_conn */
+       enumerator = g_file_enumerate_children (ontology_location,
+                                               G_FILE_ATTRIBUTE_STANDARD_NAME,
+                                               G_FILE_QUERY_INFO_NONE,
+                                               NULL, error);
+       if (!enumerator)
+               goto error;
+
+       while (g_file_enumerator_iterate (enumerator, &info, NULL, NULL, error)) {
+               GError *inner_error = NULL;
+               gchar *uri, *query;
+               GFile *child;
+
+               if (!info)
+                       break;
+               if (!g_str_has_suffix (g_file_info_get_name (info), ".description"))
+                       continue;
+
+               child = g_file_enumerator_get_child (enumerator, info);
+               uri = g_file_get_uri (child);
+               query = g_strdup_printf ("LOAD <%s>", uri);
+               tracker_sparql_connection_update (desc_conn,
+                                                 query,
+                                                 NULL,
+                                                 &inner_error);
+               g_assert_no_error (inner_error);
+               g_object_unref (child);
+               g_free (uri);
+               g_free (query);
+       }
+
+       model = g_new0 (TrackerOntologyModel, 1);
+       model->ontology_conn = ontology_conn;
+       model->desc_conn = desc_conn;
+       model->stmts = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
+                                               (GDestroyNotify) g_object_unref);
+       model->classes = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
+                                               (GDestroyNotify) ttl_model_class_free);
+       model->properties = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
+                                                  (GDestroyNotify) ttl_model_property_free);
+       model->descriptions = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
+                                                    (GDestroyNotify) ttl_model_description_free);
+
+       tracker_ontology_model_init_classes (model);
+       tracker_ontology_model_init_properties (model);
+
+       return model;
+
+error:
+       g_clear_object (&ontology_conn);
+       g_clear_object (&desc_conn);
+       g_clear_object (&enumerator);
+
+       return NULL;
+}
+
+void
+tracker_ontology_model_free (TrackerOntologyModel *model)
+{
+       g_object_unref (model->ontology_conn);
+       g_object_unref (model->desc_conn);
+       g_object_unref (model->classes_stmt);
+       g_object_unref (model->props_stmt);
+       g_hash_table_unref (model->stmts);
+       g_hash_table_unref (model->classes);
+       g_hash_table_unref (model->properties);
+       g_hash_table_unref (model->descriptions);
+       g_free (model);
+}
+
+GStrv
+tracker_ontology_model_get_prefixes (TrackerOntologyModel *model)
+{
+       TrackerSparqlCursor *cursor;
+       GError *error = NULL;
+       GPtrArray *prefixes;
+
+       cursor = tracker_sparql_connection_query (model->ontology_conn,
+                                                 "SELECT ?p { ?u a nrl:Namespace ; nrl:prefix ?p }",
+                                                 NULL,
+                                                 &error);
+       g_assert_no_error (error);
+
+       prefixes = g_ptr_array_new ();
+
+       while (tracker_sparql_cursor_next (cursor, NULL, &error)) {
+               const gchar *prefix;
+
+               prefix = tracker_sparql_cursor_get_string (cursor, 0, NULL);
+               g_ptr_array_add (prefixes, g_strdup (prefix));
+       }
+
+       g_object_unref (cursor);
+       g_ptr_array_add (prefixes, NULL);
+       g_assert_no_error (error);
+
+       return (GStrv) g_ptr_array_free (prefixes, FALSE);
+}
+
+TrackerOntologyDescription *
+tracker_ontology_model_get_description (TrackerOntologyModel *model,
+                                        const gchar          *prefix)
+{
+       TrackerSparqlStatement *stmt;
+       TrackerSparqlCursor *cursor;
+       TrackerOntologyDescription *desc;
+       GError *error = NULL;
+
+       desc = g_hash_table_lookup (model->descriptions, prefix);
+
+       if (!desc) {
+               stmt = tracker_sparql_connection_query_statement (model->desc_conn,
+                                                                 "SELECT "
+                                                                 "  dsc:title(?d) "
+                                                                 "  dsc:description(?d) "
+                                                                 "  dsc:gitlog(?d) "
+                                                                 "  dsc:localPrefix(?d) "
+                                                                 "  dsc:baseUrl(?d) "
+                                                                 "  dsc:relativePath(?d) "
+                                                                 "  dsc:copyright(?d) "
+                                                                 "  dsc:upstream(?d) "
+                                                                 "{"
+                                                                 "  ?d a dsc:Ontology ;"
+                                                                 "     dsc:localPrefix ~prefix"
+                                                                 "}",
+                                                                 NULL,
+                                                                 &error);
+               g_assert_no_error (error);
+
+               tracker_sparql_statement_bind_string (stmt, "prefix", prefix);
+               cursor = tracker_sparql_statement_execute (stmt, NULL, &error);
+               g_object_unref (stmt);
+               g_assert_no_error (error);
+
+               if (!tracker_sparql_cursor_next (cursor, NULL, &error))
+                       return NULL;
+
+               desc = ttl_model_description_new ();
+               desc->title = g_strdup (tracker_sparql_cursor_get_string (cursor, 0, NULL));
+               desc->description = g_strdup (tracker_sparql_cursor_get_string (cursor, 1, NULL));
+               desc->gitlog = g_strdup (tracker_sparql_cursor_get_string (cursor, 2, NULL));
+               desc->localPrefix = g_strdup (tracker_sparql_cursor_get_string (cursor, 3, NULL));
+               desc->baseUrl = g_strdup (tracker_sparql_cursor_get_string (cursor, 4, NULL));
+               desc->relativePath = g_strdup (tracker_sparql_cursor_get_string (cursor, 5, NULL));
+               desc->copyright = g_strdup (tracker_sparql_cursor_get_string (cursor, 6, NULL));
+               desc->upstream = g_strdup (tracker_sparql_cursor_get_string (cursor, 7, NULL));
+
+               g_object_unref (cursor);
+
+               fill_in_list (model->desc_conn, model, &desc->authors, prefix,
+                             "SELECT ?author {"
+                             "  ?d a dsc:Ontology ;"
+                             "     dsc:localPrefix ~var ;"
+                             "     dsc:author ?author ."
+                             "}"
+                             "ORDER BY DESC ?author");
+
+               fill_in_list (model->desc_conn, model, &desc->editors, prefix,
+                             "SELECT ?editor {"
+                             "  ?d a dsc:Ontology ;"
+                             "     dsc:localPrefix ~var ;"
+                             "     dsc:editor ?editor ."
+                             "}"
+                             "ORDER BY DESC ?editor");
+
+               fill_in_list (model->desc_conn, model, &desc->contributors, prefix,
+                             "SELECT ?contributor {"
+                             "  ?d a dsc:Ontology ;"
+                             "     dsc:localPrefix ~var ;"
+                             "     dsc:contributor ?contributor ."
+                             "}"
+                             "ORDER BY DESC ?contributor");
+
+               g_hash_table_insert (model->descriptions, desc->localPrefix, desc);
+       }
+
+       return desc;
+}
+
+GList *
+tracker_ontology_model_list_classes (TrackerOntologyModel *model,
+                                    const gchar          *prefix)
+{
+       TrackerSparqlCursor *cursor;
+       GError *error = NULL;
+       GList *classes = NULL;
+
+       if (!model->classes_stmt) {
+               model->classes_stmt =
+                       tracker_sparql_connection_query_statement (model->ontology_conn,
+                                                                  "SELECT ?u {"
+                                                                  "  ?u a rdfs:Class ."
+                                                                  "  ?o a nrl:Namespace ;"
+                                                                  "     nrl:prefix ~prefix ."
+                                                                  "  FILTER (STRSTARTS(?u, ?o))"
+                                                                  "} ORDER BY DESC ?u",
+                                                                  NULL,
+                                                                  &error);
+               g_assert_no_error (error);
+       }
+
+       tracker_sparql_statement_bind_string (model->classes_stmt, "prefix", prefix);
+       cursor = tracker_sparql_statement_execute (model->classes_stmt, NULL, &error);
+       g_assert_no_error (error);
+
+       while (tracker_sparql_cursor_next (cursor, NULL, &error)) {
+               classes = g_list_prepend (classes, g_strdup (tracker_sparql_cursor_get_string (cursor, 0, 
NULL)));
+       }
+
+       g_assert_no_error (error);
+       g_object_unref (cursor);
+
+       return classes;
+}
+
+GList *
+tracker_ontology_model_list_properties (TrackerOntologyModel *model,
+                                        const gchar          *prefix)
+{
+       TrackerSparqlCursor *cursor;
+       GError *error = NULL;
+       GList *props = NULL;
+
+       if (!model->props_stmt) {
+               model->props_stmt =
+                       tracker_sparql_connection_query_statement (model->ontology_conn,
+                                                                  "SELECT ?u {"
+                                                                  "  ?u a rdf:Property ."
+                                                                  "  ?o a nrl:Namespace ;"
+                                                                  "     nrl:prefix ~prefix ."
+                                                                  "  FILTER (STRSTARTS(?u, ?o))"
+                                                                  "} ORDER BY DESC ?u",
+                                                                  NULL,
+                                                                  &error);
+               g_assert_no_error (error);
+       }
+
+       tracker_sparql_statement_bind_string (model->props_stmt, "prefix", prefix);
+       cursor = tracker_sparql_statement_execute (model->props_stmt, NULL, &error);
+       g_assert_no_error (error);
+
+       while (tracker_sparql_cursor_next (cursor, NULL, &error)) {
+               props = g_list_prepend (props, g_strdup (tracker_sparql_cursor_get_string (cursor, 0, NULL)));
+       }
+
+       g_assert_no_error (error);
+       g_object_unref (cursor);
+
+       return props;
+}
+
+TrackerOntologyClass *
+tracker_ontology_model_get_class (TrackerOntologyModel *model,
+                                 const gchar          *class_name)
+{
+       return g_hash_table_lookup (model->classes, class_name);
+}
+
+TrackerOntologyProperty *
+tracker_ontology_model_get_property (TrackerOntologyModel *model,
+                                     const gchar          *prop_name)
+{
+       return g_hash_table_lookup (model->properties, prop_name);
+}
diff --git a/docs/tools/ttl_model.h b/docs/tools/tracker-ontology-model.h
similarity index 51%
rename from docs/tools/ttl_model.h
rename to docs/tools/tracker-ontology-model.h
index a3758b0a2..60339fe46 100644
--- a/docs/tools/ttl_model.h
+++ b/docs/tools/tracker-ontology-model.h
@@ -17,15 +17,19 @@
  * 02110-1301, USA.
  */
 
-#ifndef __TTL_MODEL_H__
-#define __TTL_MODEL_H__
+#ifndef TRACKER_ONTOLOGY_MODEL_H
+#define TRACKER_ONTOLOGY_MODEL_H
 
 #include <glib.h>
 
 G_BEGIN_DECLS
 
+typedef struct _TrackerOntologyModel TrackerOntologyModel;
+
 typedef struct {
        gchar *classname;
+       gchar *shortname;
+       gchar *basename;
        gchar *specification;
        GList *superclasses;
        GList *subclasses;
@@ -35,12 +39,13 @@ typedef struct {
        GList *instances;
        gboolean notify;
        gboolean deprecated;
-} OntologyClass;
+} TrackerOntologyClass;
 
 typedef struct {
        gchar *propertyname;
+       gchar *shortname;
+       gchar *basename;
        gchar *specification;
-       GList *type;
        GList *domain;
        GList *range;
        GList *superproperties;
@@ -50,7 +55,7 @@ typedef struct {
        gboolean deprecated;
         gboolean fulltextIndexed;
         gchar *weight;
-} OntologyProperty;
+} TrackerOntologyProperty;
 
 typedef struct {
        gchar *title;
@@ -64,30 +69,27 @@ typedef struct {
        gchar *baseUrl;
        gchar *localPrefix;
        gchar *relativePath;
-} OntologyDescription;
-
-typedef struct {
-       GHashTable *classes;
-       GHashTable *properties;
-       GHashTable *prefixes;
-} Ontology;
+} TrackerOntologyDescription;
 
+TrackerOntologyModel * tracker_ontology_model_new (GFile   *ontology_location,
+                                                   GError **error);
+void tracker_ontology_model_free (TrackerOntologyModel *model);
 
-OntologyClass *       ttl_model_class_new        (const gchar *classname);
-void                  ttl_model_class_free       (OntologyClass *klass);
+GStrv tracker_ontology_model_get_prefixes (TrackerOntologyModel *model);
 
-OntologyDescription * ttl_model_description_new  (void);
-void                  ttl_model_description_free (OntologyDescription *desc);
+TrackerOntologyDescription * tracker_ontology_model_get_description (TrackerOntologyModel *model,
+                                                                     const gchar          *prefix);
 
-OntologyProperty *    ttl_model_property_new     (const gchar *propname);
-void                  ttl_model_property_free    (OntologyProperty *property);
+GList * tracker_ontology_model_list_classes (TrackerOntologyModel *model,
+                                             const gchar          *prefix);
+GList * tracker_ontology_model_list_properties (TrackerOntologyModel *model,
+                                                const gchar          *prefix);
 
-gchar *               ttl_model_name_to_shortname (Ontology    *ontology,
-                                                   const gchar *name,
-                                                   const gchar *separator);
+TrackerOntologyClass * tracker_ontology_model_get_class (TrackerOntologyModel *model,
+                                                         const gchar          *class_name);
+TrackerOntologyProperty * tracker_ontology_model_get_property (TrackerOntologyModel *model,
+                                                               const gchar          *prop_name);
 
-gchar *               ttl_model_name_to_basename (Ontology    *ontology,
-                                                  const gchar *name);
 G_END_DECLS
 
-#endif /* __TRACKER_TTL_MODEL_H__ */
+#endif /* TRACKER_ONTOLOGY_MODEL_H */
diff --git a/docs/tools/ttl2xml.c b/docs/tools/ttl2xml.c
index 7129c1770..655e1ef10 100644
--- a/docs/tools/ttl2xml.c
+++ b/docs/tools/ttl2xml.c
@@ -21,10 +21,9 @@
 #include <gio/gio.h>
 #include <string.h>
 #include <stdio.h>
-#include "ttl_loader.h"
-#include "ttl_model.h"
+
+#include "tracker-ontology-model.h"
 #include "ttl_xml.h"
-#include "ttlresource2xml.h"
 
 static gchar *ontology_dir = NULL;
 static gchar *output_dir = NULL;
@@ -46,69 +45,16 @@ static GOptionEntry   entries[] = {
        { NULL }
 };
 
-static gint
-compare_files (gconstpointer a,
-              gconstpointer b)
-{
-       const GFile *file_a = a, *file_b = b;
-       gchar *basename_a, *basename_b;
-       gint res;
-
-       basename_a = g_file_get_basename ((GFile*) file_a);
-       basename_b = g_file_get_basename ((GFile*) file_b);
-       res = strcmp (basename_a, basename_b);
-
-       g_free (basename_a);
-       g_free (basename_b);
-
-       return res;
-}
-
-static GList *
-get_description_files (GFile *dir)
-{
-       GFileEnumerator *enumerator;
-       GFileInfo *info;
-       GFile *desc_file;
-       GList *files;
-       const gchar *name;
-
-       enumerator = g_file_enumerate_children (dir,
-                                               G_FILE_ATTRIBUTE_STANDARD_NAME,
-                                               G_FILE_QUERY_INFO_NONE,
-                                               NULL, NULL);
-
-       if (!enumerator) {
-               return NULL;
-       }
-
-       files = NULL;
-
-       while ((info = g_file_enumerator_next_file (enumerator, NULL, NULL)) != NULL) {
-               name = g_file_info_get_name (info);
-
-               if (g_str_has_suffix (name, ".description")) {
-                       desc_file = g_file_enumerator_get_child (enumerator, info);
-                       files = g_list_insert_sorted (files, desc_file, compare_files);
-               }
-
-               g_object_unref (info);
-       }
-
-       g_object_unref (enumerator);
-
-       return files;
-}
-
 gint
 main (gint argc, gchar **argv)
 {
        GOptionContext *context;
-       OntologyDescription *description = NULL;
-       GList *description_files, *l;
+       TrackerOntologyDescription *description = NULL;
+       TrackerOntologyModel *model = NULL;
        g_autoptr(GFile) ontology_file = NULL, output_file = NULL;
        gchar *path;
-       gboolean success;
+       GStrv prefixes = NULL;
+       gint i;
        g_autoptr(GError) error = NULL;
 
        /* Translators: this messagge will apper immediately after the  */
@@ -136,47 +82,31 @@ main (gint argc, gchar **argv)
 
        ontology_file = g_file_new_for_commandline_arg (ontology_dir);
        output_file = g_file_new_for_commandline_arg (output_dir);
-       description_files = get_description_files (ontology_file);
 
-       if (!description_files) {
-               g_printerr ("Ontology description files not found in dir\n");
+       model = tracker_ontology_model_new (ontology_file, &error);
+       if (error) {
+               g_printerr ("Error loading ontology: %s\n", error->message);
+               g_error_free (error);
                return -1;
        }
 
+       prefixes = tracker_ontology_model_get_prefixes (model);
+
        path = g_file_get_path (output_file);
        g_mkdir_with_parents (path, 0755);
        g_free (path);
 
-       success = TRUE;
-       for (l = description_files; l; l = l->next) {
-               Ontology *ontology = NULL;
-               g_autoptr(GFile) ttl_file = NULL, ttl_output_file = NULL;
-               gchar *filename;
-
-               description = ttl_loader_load_description (l->data);
-               ttl_file = g_file_get_child (ontology_file, description->relativePath);
-
-               filename = g_strdup_printf ("%s-ontology.xml", description->localPrefix);
-               ttl_output_file = g_file_get_child (output_file, filename);
-               g_free (filename);
-
-               ontology = ttl_loader_new_ontology ();
-
-               success &= ttl_loader_load_ontology (ontology, ttl_file, &error);
-
-               if (error) {
-                       g_printerr ("%s: Turtle parse error: %s\n", g_file_peek_path (ttl_file), 
error->message);
-                       g_clear_error (&error);
-               }
-
-               ttl_xml_print (description, ontology, ttl_output_file, description_dir);
+       for (i = 0; prefixes[i]; i++) {
+               description = tracker_ontology_model_get_description (model, prefixes[i]);
+               if (!description)
+                       continue;
 
-               ttl_loader_free_ontology (ontology);
-               ttl_loader_free_description (description);
+               ttl_xml_print (description, model, prefixes[i], output_file, description_dir);
        }
-       g_list_free_full (description_files, (GDestroyNotify) g_object_unref);
 
+       g_strfreev (prefixes);
+       tracker_ontology_model_free (model);
        g_option_context_free (context);
 
-       return !(success);
+       return 0;
 }
diff --git a/docs/tools/ttl_xml.c b/docs/tools/ttl_xml.c
index a5a0af308..375a8f7bb 100644
--- a/docs/tools/ttl_xml.c
+++ b/docs/tools/ttl_xml.c
@@ -23,8 +23,8 @@
 #include "ttlresource2xml.h"
 
 typedef struct {
-       Ontology *ontology;
-       OntologyDescription *description;
+       TrackerOntologyModel *model;
+       TrackerOntologyDescription *description;
        FILE *output;
 } CallbackInfo;
 
@@ -88,7 +88,7 @@ print_deprecated_message (FILE *f)
 #endif
 
 static void
-print_xml_header (FILE *f, OntologyDescription *desc)
+print_xml_header (FILE *f, TrackerOntologyDescription *desc)
 {
        g_fprintf (f, "<?xml version='1.0' encoding='UTF-8'?>\n");
        g_fprintf (f, "<!DOCTYPE book PUBLIC \"-//OASIS//DTD DocBook XML V4.5//EN\"\n"
@@ -107,7 +107,7 @@ print_xml_header (FILE *f, OntologyDescription *desc)
 }
 
 static void
-print_xml_footer (FILE *f, OntologyDescription *desc)
+print_xml_footer (FILE *f, TrackerOntologyDescription *desc)
 {
        g_fprintf (f, "<refsect1>\n");
        g_fprintf (f, "<title>Credits and Copyright</title>\n");
@@ -131,12 +131,6 @@ print_xml_footer (FILE *f, OntologyDescription *desc)
        g_fprintf (f, "</refentry>\n");
 }
 
-static gint compare_class (gconstpointer a,
-                           gconstpointer b)
-{
-       return strcmp (((OntologyClass *)a)->classname, ((OntologyClass *)b)->classname);
-}
-
 /* By default we list properties under their respective class.
  *
  * Ontologies can contain properties whose class is in a different
@@ -146,8 +140,9 @@ static gint compare_class (gconstpointer a,
  * extra properties provided for that class.
  */
 static GHashTable *
-get_extra_properties (GList *classes,
-                      GList *properties)
+get_extra_properties (TrackerOntologyModel *model,
+                      GList                *classes,
+                      GList                *properties)
 {
        GList *l, *c;
        GHashTable *extra_properties;
@@ -158,17 +153,27 @@ get_extra_properties (GList *classes,
        extra_properties = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
 
        for (l = properties; l; l = l->next) {
-               OntologyProperty *prop = l->data;
+               TrackerOntologyProperty *prop;
                gboolean has_domain_in_this_ontology = FALSE;
 
-               for (c = classes; c; c = c->next) {
-                       OntologyClass *klass;
+               prop = tracker_ontology_model_get_property (model, l->data);
+
+               for (c = prop->domain; c; c = c->next) {
+                       TrackerOntologyDescription *desc = NULL;
+                       TrackerOntologyClass *klass;
+                       gchar *prefix;
+                       const gchar *sep;
+
+                       klass = tracker_ontology_model_get_class (model, c->data);
+                       sep = strstr (klass->shortname, ":");
 
-                       klass = c->data;
-                       if (g_list_find_custom (prop->domain, klass->classname, (GCompareFunc)strcmp)) {
-                               has_domain_in_this_ontology = TRUE;
-                               break;
+                       if (sep) {
+                               prefix = g_strndup (klass->shortname, sep - klass->shortname);
+                               desc = tracker_ontology_model_get_description (model, prefix);
+                               g_free (prefix);
                        }
+
+                       has_domain_in_this_ontology = desc != NULL;
                }
 
                if (!has_domain_in_this_ontology) {
@@ -194,8 +199,8 @@ get_extra_properties (GList *classes,
 }
 
 static void
-print_synopsis (FILE                 *f,
-                OntologyDescription *desc)
+print_synopsis (FILE                       *f,
+                TrackerOntologyDescription *desc)
 {
        g_fprintf (f, "<refsynopsisdiv>\n");
        g_fprintf (f, "<synopsis>\n");
@@ -205,10 +210,10 @@ print_synopsis (FILE                 *f,
 }
 
 static void
-print_toc_classes (FILE       *f,
-                   Ontology   *ontology,
-                   const char *id,
-                   GList      *classes)
+print_toc_classes (FILE                 *f,
+                   TrackerOntologyModel *model,
+                   const char           *id,
+                   GList                *classes)
 {
        GList *l;
 
@@ -219,12 +224,12 @@ print_toc_classes (FILE       *f,
        g_fprintf (f, "<title>Classes</title>");
 
        for (l = classes; l; l = l->next) {
-               OntologyClass *klass;
-               g_autofree char *basename = NULL, *id = NULL;
+               TrackerOntologyClass *klass;
+               const char *basename = NULL, *id = NULL;
 
-               klass = l->data;
-               basename = ttl_model_name_to_basename (ontology, klass->classname);
-               id = ttl_model_name_to_shortname (ontology, klass->classname, "-");
+               klass = tracker_ontology_model_get_class (model, l->data);
+               basename = klass->basename;
+               id = klass->shortname;
 
                if (l != classes) {
                        g_fprintf (f, ", ");
@@ -236,10 +241,10 @@ print_toc_classes (FILE       *f,
 }
 
 static void
-print_toc_extra_properties (FILE       *f,
-                            Ontology   *ontology,
-                            const char *id,
-                            GHashTable *extra_properties)
+print_toc_extra_properties (FILE                 *f,
+                            TrackerOntologyModel *model,
+                            const char           *id,
+                            GHashTable           *extra_properties)
 {
        GList *props_for_class, *c, *l;
        g_autoptr(GList) classes = NULL;
@@ -259,13 +264,12 @@ print_toc_extra_properties (FILE       *f,
                classname = c->data;
                props_for_class = g_hash_table_lookup (extra_properties, classname);
                for (l = props_for_class; l; l = l->next) {
-                       OntologyProperty *prop;
-                       g_autofree char *basename = NULL, *prop_id = NULL;
-
-                       prop = g_hash_table_lookup (ontology->properties, l->data);
+                       TrackerOntologyProperty *prop;
+                       const char *basename = NULL, *prop_id = NULL;
 
-                       basename = ttl_model_name_to_basename (ontology, prop->propertyname);
-                       prop_id = ttl_model_name_to_shortname (ontology, prop->propertyname, "-");
+                       prop = tracker_ontology_model_get_property (model, l->data);
+                       basename = prop->basename;
+                       prop_id = prop->shortname;
 
                        if (print_comma) {
                                g_fprintf (f, ", ");
@@ -282,33 +286,38 @@ print_toc_extra_properties (FILE       *f,
 
 /* Generate docbook XML document for one ontology. */
 void
-ttl_xml_print (OntologyDescription *description,
-               Ontology            *ontology,
-               GFile               *file,
-               const gchar         *description_dir)
+ttl_xml_print (TrackerOntologyDescription *description,
+               TrackerOntologyModel       *model,
+              const gchar                *prefix,
+               GFile                      *output_location,
+               const gchar                *description_dir)
 {
-       gchar *upper_name, *path, *introduction, *basename;
+       gchar *upper_name, *path, *introduction, *basename, *filename;
        g_autoptr(GList) classes = NULL, properties = NULL, extra_classes = NULL;
        g_autoptr(GHashTable) extra_properties = NULL;
+       GFile *file;
        GList *l;
        FILE *f;
 
+       filename = g_strdup_printf ("%s-ontology.xml", description->localPrefix);
+       file = g_file_get_child (output_location, filename);
+       g_free (filename);
+
        path = g_file_get_path (file);
        f = fopen (path, "w");
        g_assert (f != NULL);
        g_free (path);
 
        upper_name = g_ascii_strup (description->localPrefix, -1);
-       classes = g_list_sort (g_hash_table_get_values (ontology->classes), compare_class);
-       properties = g_hash_table_get_values (ontology->properties);
-
-       extra_properties = get_extra_properties (classes, properties);
+       classes = tracker_ontology_model_list_classes (model, prefix);
+       properties = tracker_ontology_model_list_properties (model, prefix);
+       extra_properties = get_extra_properties (model, classes, properties);
 
        print_xml_header (f, description);
 
        print_synopsis (f, description);
-       print_toc_classes (f, ontology, description->localPrefix, classes);
-       print_toc_extra_properties (f, ontology, description->localPrefix, extra_properties);
+       print_toc_classes (f, model, description->localPrefix, classes);
+       print_toc_extra_properties (f, model, description->localPrefix, extra_properties);
 
        basename = g_strdup_printf ("%s-introduction.xml", description->localPrefix);
        introduction = g_build_filename (description_dir, basename, NULL);
@@ -320,11 +329,14 @@ ttl_xml_print (OntologyDescription *description,
        }
 
        if (classes != NULL) {
-       g_fprintf (f, "<refsect1 id='%s-classes'>\n", description->localPrefix);
+               g_fprintf (f, "<refsect1 id='%s-classes'>\n", description->localPrefix);
                g_fprintf (f, "<title>Class Details</title>\n");
 
                for (l = classes; l; l = l->next) {
-                       print_ontology_class (ontology, l->data, f);
+                       TrackerOntologyClass *klass;
+
+                       klass = tracker_ontology_model_get_class (model, l->data);
+                       print_ontology_class (model, klass, f);
                }
 
                g_fprintf (f, "</refsect1>\n");
@@ -344,7 +356,7 @@ ttl_xml_print (OntologyDescription *description,
 
                        properties_for_class = g_hash_table_lookup (extra_properties, classname);
                        if (properties_for_class) {
-                               print_ontology_extra_properties (ontology, description->localPrefix, 
classname, properties_for_class, f);
+                               print_ontology_extra_properties (model, description->localPrefix, classname, 
properties_for_class, f);
                        }
                }
 
@@ -355,5 +367,6 @@ ttl_xml_print (OntologyDescription *description,
 
        g_free (upper_name);
        g_free (introduction);
+       g_object_unref (file);
        fclose (f);
 }
diff --git a/docs/tools/ttl_xml.h b/docs/tools/ttl_xml.h
index f22745621..840182bb8 100644
--- a/docs/tools/ttl_xml.h
+++ b/docs/tools/ttl_xml.h
@@ -21,15 +21,15 @@
 #define __TTL_XML_H__
 
 #include <gio/gio.h>
-#include "ttl_model.h"
-#include <stdio.h>
+#include "tracker-ontology-model.h"
 
 G_BEGIN_DECLS
 
-void ttl_xml_print (OntologyDescription *description,
-                    Ontology            *ontology,
-                    GFile               *file,
-                    const gchar         *description_dir);
+void ttl_xml_print (TrackerOntologyDescription *description,
+                    TrackerOntologyModel       *model,
+                   const gchar                *prefix,
+                    GFile                      *output_location,
+                    const gchar                *description_dir);
 
 G_END_DECLS
 
diff --git a/docs/tools/ttlresource2xml.c b/docs/tools/ttlresource2xml.c
index 784680446..446a8f8b2 100644
--- a/docs/tools/ttlresource2xml.c
+++ b/docs/tools/ttlresource2xml.c
@@ -23,39 +23,34 @@
 #include <glib/gprintf.h>
 #include <gio/gio.h>
 #include <stdlib.h>
-#include "ttl_loader.h"
-#include "ttl_model.h"
+#include "tracker-ontology-model.h"
 #include "ttl_xml.h"
 #include "ttlresource2xml.h"
 
 static void
-class_get_parent_hierarchy (Ontology       *ontology,
-                            const gchar    *class_name,
-                            GList         **list)
+class_get_parent_hierarchy (TrackerOntologyModel  *model,
+                            const gchar           *class_name,
+                            GList                **list)
 {
-       OntologyClass *klass;
+       TrackerOntologyClass *klass;
        GList *l;
 
        /* Ensure we only got the same class there once */
        *list = g_list_remove (*list, (gpointer) class_name);
        *list = g_list_prepend (*list, (gpointer) class_name);
 
-       klass = g_hash_table_lookup (ontology->classes, class_name);
-
-       if (!klass) {
-               klass = ttl_model_class_new (class_name);
-               g_hash_table_insert (ontology->classes, g_strdup (klass->classname), klass);
+       klass = tracker_ontology_model_get_class (model, class_name);
+       if (!klass)
                return;
-       }
 
        for (l = klass->superclasses; l; l = l->next) {
-               class_get_parent_hierarchy (ontology, l->data, list);
+               class_get_parent_hierarchy (model, l->data, list);
        }
 }
 
 static GList *
-class_get_hierarchy (Ontology      *ontology,
-                     OntologyClass *klass)
+class_get_hierarchy (TrackerOntologyModel *model,
+                     TrackerOntologyClass *klass)
 {
        GList *hierarchy = NULL, *l;
 
@@ -68,40 +63,33 @@ class_get_hierarchy (Ontology      *ontology,
                hierarchy = g_list_prepend (hierarchy, l->data);
        }
 
-       class_get_parent_hierarchy (ontology, klass->classname, &hierarchy);
+       class_get_parent_hierarchy (model, klass->classname, &hierarchy);
 
        return hierarchy;
 }
 
 static void
-print_predefined_instances (FILE          *f,
-                            OntologyClass *klass,
-                            Ontology      *ontology)
+print_predefined_instances (FILE                 *f,
+                            TrackerOntologyClass *klass,
+                            TrackerOntologyModel *model)
 {
-       gchar *shortname, *id;
+       const gchar *id;
        GList *l;
 
        if (!klass->instances)
                return;
 
-       shortname = ttl_model_name_to_shortname (ontology, klass->classname, NULL);
-       id = ttl_model_name_to_shortname (ontology, klass->classname, "-");
+       id = klass->shortname;
 
        g_fprintf (f, "<refsect3 id='%s.predefined-instances'>", id);
        g_fprintf (f, "<title>Predefined instances</title><para>");
-       g_fprintf (f, "%s has the following predefined instances: ", shortname);
+       g_fprintf (f, "%s has the following predefined instances: ", klass->shortname);
         g_fprintf (f, "<itemizedlist>\n");
 
-       g_free (shortname);
-       g_free (id);
-
        for (l = klass->instances; l; l = l->next) {
-               shortname = ttl_model_name_to_shortname (ontology, l->data, NULL);
-
                g_fprintf (f, "<listitem><para>");
-               g_fprintf (f, "<literal>%s</literal>", shortname);
+               g_fprintf (f, "<literal>%s</literal>", (gchar*) l->data);
                g_fprintf (f, "</para></listitem>\n");
-               g_free (shortname);
        }
 
        g_fprintf (f, "</itemizedlist></para></refsect3>\n");
@@ -113,7 +101,7 @@ typedef struct {
 } HierarchyString;
 
 typedef struct {
-       OntologyClass *class;
+       TrackerOntologyClass *class;
        GList *hierarchy;
        GHashTable *resolved_children;
        GHashTable *resolved_parents;
@@ -180,15 +168,15 @@ list_filter (GList *original,
 }
 
 static HierarchyContext *
-hierarchy_context_new (OntologyClass *klass,
-                       Ontology      *ontology)
+hierarchy_context_new (TrackerOntologyClass *klass,
+                       TrackerOntologyModel *model)
 {
        HierarchyContext *context;
        GList *l;
 
        context = g_new0 (HierarchyContext, 1);
        context->class = klass;
-       context->hierarchy = class_get_hierarchy (ontology, klass);
+       context->hierarchy = class_get_hierarchy (model, klass);
        context->placed = g_hash_table_new (g_str_hash, g_str_equal);
        context->resolved_parents = g_hash_table_new_full (g_str_hash,
                                                           g_str_equal,
@@ -201,7 +189,7 @@ hierarchy_context_new (OntologyClass *klass,
        context->strings = g_ptr_array_new_with_free_func ((GDestroyNotify) hierarchy_string_free);
 
        for (l = context->hierarchy; l; l = l->next) {
-               OntologyClass *cl = g_hash_table_lookup (ontology->classes, l->data);
+               TrackerOntologyClass *cl = tracker_ontology_model_get_class (model, l->data);
 
                g_hash_table_insert (context->resolved_parents,
                                     cl->classname,
@@ -230,9 +218,9 @@ hierarchy_context_free (HierarchyContext *context)
 }
 
 static GList *
-hierarchy_context_get_single_parented_children (HierarchyContext *context,
-                                                OntologyClass    *klass,
-                                                Ontology         *ontology)
+hierarchy_context_get_single_parented_children (HierarchyContext     *context,
+                                                TrackerOntologyClass *klass,
+                                                TrackerOntologyModel *model)
 {
        GList *filtered = NULL, *children, *l;
 
@@ -282,23 +270,22 @@ check_parents_placed (GList      *parents,
 }
 
 static void
-hierarchy_context_resolve_class (HierarchyContext *context,
-                                 OntologyClass    *klass,
-                                 Ontology         *ontology)
+hierarchy_context_resolve_class (HierarchyContext     *context,
+                                 TrackerOntologyClass *klass,
+                                 TrackerOntologyModel *model)
 {
        GList *l = g_list_find_custom (context->hierarchy, klass->classname,
                                       (GCompareFunc) g_strcmp0);
        gint pos = g_list_position (context->hierarchy, l);
        GList *children, *parents;
-       gchar *shortname, *link;
+       const gchar *shortname, *link;
        HierarchyString *str;
        gboolean is_child;
 
        if (pos < 0)
                return;
 
-       shortname = ttl_model_name_to_shortname (ontology, klass->classname, NULL);
-       link = ttl_model_name_to_shortname (ontology, klass->classname, "-");
+       shortname = link = klass->shortname;
        parents = g_hash_table_lookup (context->resolved_parents,
                                       klass->classname);
 
@@ -367,7 +354,7 @@ hierarchy_context_resolve_class (HierarchyContext *context,
                /* Step 2: Modify all strings downwards, adding the lineart
                 * necessary for all children of this class.
                 */
-               children = hierarchy_context_get_single_parented_children (context, klass, ontology);
+               children = hierarchy_context_get_single_parented_children (context, klass, model);
                l = l->next;
                pos++;
 
@@ -406,27 +393,24 @@ hierarchy_context_resolve_class (HierarchyContext *context,
                        }
                }
        }
-
-       g_free (shortname);
-       g_free (link);
 }
 
 static GPtrArray *
-class_get_parent_hierarchy_strings (OntologyClass *klass,
-                                    Ontology      *ontology)
+class_get_parent_hierarchy_strings (TrackerOntologyClass *klass,
+                                    TrackerOntologyModel *model)
 {
        HierarchyContext *context;
        GList *c;
        GPtrArray *strings = NULL;
 
-       context = hierarchy_context_new (klass, ontology);
+       context = hierarchy_context_new (klass, model);
 
        /* Proceed from parent to child classes, populating the 
         * context->strings array.
         */
        for (c = context->hierarchy; c; c = c->next) {
-               OntologyClass *cl = g_hash_table_lookup (ontology->classes, c->data);
-               hierarchy_context_resolve_class (context, cl, ontology);
+               TrackerOntologyClass *cl = tracker_ontology_model_get_class (model, c->data);
+               hierarchy_context_resolve_class (context, cl, model);
        }
 
        strings = g_ptr_array_ref (context->strings);
@@ -436,27 +420,25 @@ class_get_parent_hierarchy_strings (OntologyClass *klass,
 }
 
 static void
-print_class_hierarchy (FILE          *f,
-                       OntologyClass *klass,
-                       Ontology      *ontology)
+print_class_hierarchy (FILE                 *f,
+                       TrackerOntologyClass *klass,
+                       TrackerOntologyModel *model)
 {
        GPtrArray *strings;
-       gchar *id;
+       const gchar *id;
        gsize i;
 
-       strings = class_get_parent_hierarchy_strings (klass, ontology);
+       strings = class_get_parent_hierarchy_strings (klass, model);
 
        if (!strings)
                return;
 
-       id = ttl_model_name_to_shortname (ontology, klass->classname, "-");
+       id = klass->shortname;
 
        g_fprintf (f, "<refsect3 id='%s.hierarchy'>", id);
        g_fprintf (f, "<title>Class hierarchy</title>");
        g_fprintf (f, "<screen>");
 
-       g_free (id);
-
        for (i = 0; i < strings->len; i++) {
                HierarchyString *str = g_ptr_array_index (strings, i);
                g_fprintf (f, "    %s\n", str->str->str);
@@ -467,7 +449,7 @@ print_class_hierarchy (FILE          *f,
 }
 
 static void
-print_flag (FILE *f,
+print_flag (FILE        *f,
             const gchar *flag_property_link,
             const gchar *icon_name,
             const gchar *flag_description)
@@ -484,10 +466,10 @@ print_flag (FILE *f,
 }
 
 static void
-print_property_table (FILE          *f,
-                      Ontology      *ontology,
-                      const char    *id,
-                      GList         *properties)
+print_property_table (FILE                 *f,
+                      TrackerOntologyModel *model,
+                      const char           *id,
+                      GList                *properties)
 {
        GList *l, *m;
        g_autoptr(GList) properties_sorted = NULL;
@@ -509,16 +491,17 @@ print_property_table (FILE          *f,
 
        g_fprintf (f, "<tbody>");
        for (l = properties_sorted; l; l = l->next) {
-               OntologyProperty *prop;
-               g_autofree gchar *shortname = NULL, *basename = NULL, *type_name = NULL, *type_class_id = 
NULL, *prop_id = NULL;
+               TrackerOntologyProperty *prop;
+               TrackerOntologyClass *range;
+               const gchar *shortname = NULL, *basename = NULL, *type_name = NULL, *type_class_id = NULL, 
*prop_id = NULL;
 
-               prop = g_hash_table_lookup (ontology->properties, l->data);
+               prop = tracker_ontology_model_get_property (model, l->data);
+               range = tracker_ontology_model_get_class (model, prop->range->data);
 
-               prop_id = ttl_model_name_to_shortname (ontology, prop->propertyname, "-");
-               shortname = ttl_model_name_to_shortname (ontology, prop->propertyname, NULL);
-               basename = ttl_model_name_to_basename (ontology, prop->propertyname);
-               type_name = ttl_model_name_to_basename (ontology, prop->range->data);
-               type_class_id = ttl_model_name_to_shortname (ontology, prop->range->data, "-");
+               prop_id = shortname = prop->shortname;
+               basename = prop->basename;
+               type_name = range->basename;
+               type_class_id = range->shortname;
 
                g_fprintf (f, "<row role=\"member\">");
 
@@ -548,11 +531,12 @@ print_property_table (FILE          *f,
 
                if (prop->superproperties) {
                        for (m = prop->superproperties; m; m = m->next) {
-                               g_autofree gchar *shortname = NULL, *superprop_id = NULL;
+                               const gchar *shortname = NULL, *superprop_id = NULL;
                                g_autofree gchar *message = NULL;
+                               TrackerOntologyProperty *superprop;
 
-                               shortname = ttl_model_name_to_shortname (ontology, m->data, NULL);
-                               superprop_id = ttl_model_name_to_shortname (ontology, m->data, "-");
+                               superprop = tracker_ontology_model_get_property (model, m->data);
+                               shortname = superprop_id = superprop->shortname;
 
                                message = g_strdup_printf ("This property extends %s", shortname);
 
@@ -596,16 +580,16 @@ print_property_table (FILE          *f,
 }
 
 void
-print_ontology_class (Ontology      *ontology,
-                      OntologyClass *klass,
-                      FILE          *f)
+print_ontology_class (TrackerOntologyModel *model,
+                      TrackerOntologyClass *klass,
+                      FILE                 *f)
 {
-       g_autofree gchar *name = NULL, *id = NULL;
+       const gchar *name = NULL, *id = NULL;
 
        g_return_if_fail (f != NULL);
 
-       name = ttl_model_name_to_basename (ontology, klass->classname);
-       id = ttl_model_name_to_shortname (ontology, klass->classname, "-");
+       name = klass->basename;
+       id = klass->shortname;
 
        /* Anchor for external links. */
        g_fprintf (f, "<anchor id='%s' />\n", name);
@@ -645,29 +629,29 @@ print_ontology_class (Ontology      *ontology,
                g_fprintf (f, "</refsect3>\n");
        }
 
-       print_class_hierarchy (f, klass, ontology);
-       print_predefined_instances (f, klass, ontology);
+       print_class_hierarchy (f, klass, model);
+       print_predefined_instances (f, klass, model);
 
-       print_property_table (f, ontology, id, klass->in_domain_of);
+       print_property_table (f, model, id, klass->in_domain_of);
 
        g_fprintf (f, "</refsect2>\n");
 }
 
 void
-print_ontology_extra_properties (Ontology      *ontology,
-                                 const char    *ontology_prefix,
-                                 const char    *classname,
-                                 GList         *properties_for_class,
-                                 FILE          *f)
+print_ontology_extra_properties (TrackerOntologyModel *model,
+                                 const char           *ontology_prefix,
+                                 const char           *classname,
+                                 GList                *properties_for_class,
+                                 FILE                 *f)
 {
-       g_autofree gchar *short_classname = NULL;
-       g_autofree gchar *section_id = NULL, *class_id = NULL;
+       TrackerOntologyClass *klass;
+       const gchar *short_classname = NULL, *class_id = NULL;
+       gchar *section_id = NULL;
 
        g_return_if_fail (f != NULL);
 
-       short_classname = ttl_model_name_to_shortname (ontology, classname, ":");
-
-       class_id = ttl_model_name_to_shortname (ontology, classname, "-");
+       klass = tracker_ontology_model_get_class (model, classname);
+       short_classname = class_id = klass->shortname;
        section_id = g_strconcat (ontology_prefix, ".", class_id, NULL);
 
        g_fprintf (f, "<refsect2 role='rdf-property-list' id='%s'>\n", section_id);
@@ -679,6 +663,9 @@ print_ontology_extra_properties (Ontology      *ontology,
                   short_classname);
        g_fprintf (f, "</refsect3>\n");
 
-       print_property_table (f, ontology, section_id, properties_for_class);
+       print_property_table (f, model, section_id, properties_for_class);
        g_fprintf (f, "</refsect2>\n");
+
+       g_free (section_id);
 }
+
diff --git a/docs/tools/ttlresource2xml.h b/docs/tools/ttlresource2xml.h
index 748e5bff0..80ba0bb4b 100644
--- a/docs/tools/ttlresource2xml.h
+++ b/docs/tools/ttlresource2xml.h
@@ -23,18 +23,18 @@
 #define __TTLRESOURCE2XML_H__
 
 #include <glib.h>
-#include "ttl_model.h"
+#include "tracker-ontology-model.h"
 
 G_BEGIN_DECLS
 
-void print_ontology_class (Ontology      *ontology,
-                           OntologyClass *klass,
-                           FILE          *f);
-void print_ontology_extra_properties (Ontology      *ontology,
-                                      const char    *ontology_prefix,
-                                      const char    *classname,
-                                      GList         *properties_for_class,
-                                      FILE          *f);
+void print_ontology_class (TrackerOntologyModel *model,
+                           TrackerOntologyClass *klass,
+                           FILE                 *f);
+void print_ontology_extra_properties (TrackerOntologyModel *model,
+                                      const char           *ontology_prefix,
+                                      const char           *classname,
+                                      GList                *properties_for_class,
+                                      FILE                 *f);
 
 G_END_DECLS
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]