tracker-0.16.2/utils/ontology/data-validator.c

No issues found

  1 /*
  2  * Copyright (C) 2009, Nokia <ivan.frade@nokia.com>
  3  *
  4  * This program is free software; you can redistribute it and/or
  5  * modify it under the terms of the GNU General Public License
  6  * as published by the Free Software Foundation; either version 2
  7  * of the License, or (at your option) any later version.
  8  *
  9  * This program is distributed in the hope that it will be useful,
 10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 12  * GNU General Public License for more details.
 13  *
 14  * You should have received a copy of the GNU General Public License
 15  * along with this program; if not, write to the Free Software
 16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 17  * 02110-1301, USA.
 18  */
 19 
 20 #include "config.h"
 21 
 22 #include <string.h>
 23 
 24 #include <glib.h>
 25 #include <glib/gstdio.h>
 26 #include <gio/gio.h>
 27 
 28 #include <libtracker-data/tracker-data.h>
 29 
 30 static gchar         *ontology_dir = NULL;
 31 static gchar         *ttl_file = NULL;
 32 
 33 static GOptionEntry   entries[] = {
 34 	{ "ttl-file", 't', 0, G_OPTION_ARG_FILENAME, &ttl_file,
 35 	  "Turtle file to validate",
 36 	  NULL
 37 	},
 38 	{ "ontology-dir", 'o', 0, G_OPTION_ARG_FILENAME, &ontology_dir,
 39 	  "Directory containing the ontology description files (TTL FORMAT)",
 40 	  NULL
 41 	},
 42 	{ NULL }
 43 };
 44 
 45 #define CLASS "http://www.w3.org/2000/01/rdf-schema#Class"
 46 #define PROPERTY "http://www.w3.org/1999/02/22-rdf-syntax-ns#Property"
 47 #define IS "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"
 48 
 49 static gboolean error_flag = FALSE;
 50 
 51 static GList *unknown_items = NULL;
 52 static GList *known_items = NULL;
 53 
 54 static gboolean
 55 exists_or_already_reported (const gchar *item)
 56 {
 57 	if (!g_list_find_custom (known_items,
 58 	                         item,
 59 	                         (GCompareFunc) g_strcmp0)){
 60 		if (!g_list_find_custom (unknown_items,
 61 		                         item,
 62 		                         (GCompareFunc) g_strcmp0)) {
 63 			return FALSE;
 64 		}
 65 	}
 66 
 67 	return TRUE;
 68 }
 69 
 70 static void
 71 turtle_load_ontology (const gchar *turtle_subject,
 72                       const gchar *turtle_predicate,
 73                       const gchar *turtle_object)
 74 {
 75 
 76 	if (!g_strcmp0 (turtle_predicate, IS)) {
 77 		known_items = g_list_prepend (known_items, g_strdup (turtle_subject));
 78 	}
 79 }
 80 
 81 static void
 82 turtle_statement_handler (const gchar *turtle_subject,
 83                           const gchar *turtle_predicate,
 84                           const gchar *turtle_object)
 85 {
 86 	/* Check that predicate exists in the ontology */
 87 	if (!exists_or_already_reported (turtle_predicate)){
 88 		g_print ("Unknown property %s\n", turtle_predicate);
 89 		unknown_items = g_list_prepend (unknown_items, g_strdup (turtle_predicate));
 90 		error_flag = TRUE;
 91 	}
 92 
 93 	/* And if it is a type... check the object is also there */
 94 	if (!g_strcmp0 (turtle_predicate, IS)) {
 95 		if (!exists_or_already_reported (turtle_object)){
 96 			g_print ("Unknown class %s\n", turtle_object);
 97 			error_flag = TRUE;
 98 			unknown_items = g_list_prepend (unknown_items, g_strdup (turtle_object));
 99 		}
100 	}
101 }
102 
103 static void
104 load_ontology_files (const gchar *services_dir)
105 {
106 	GList       *files = NULL;
107 	GDir        *services;
108 	const gchar *conf_file;
109 	GFile       *f;
110 	gchar       *dir_uri, *fullpath;
111 	gint         counter = 0;
112 
113 	f = g_file_new_for_path (services_dir);
114 	dir_uri = g_file_get_path (f);
115 
116 	services = g_dir_open (dir_uri, 0, NULL);
117 
118 	conf_file = g_dir_read_name (services);
119 
120 	while (conf_file) {
121 		TrackerTurtleReader *reader;
122 		GError *error = NULL;
123 
124 		if (!g_str_has_suffix (conf_file, "ontology")) {
125 			conf_file = g_dir_read_name (services);
126 			continue;
127 		}
128 
129 		fullpath = g_build_filename (dir_uri, conf_file, NULL);
130 
131 		reader = tracker_turtle_reader_new (fullpath, NULL);
132 
133 		while (error == NULL && tracker_turtle_reader_next (reader, &error)) {
134 			turtle_load_ontology (tracker_turtle_reader_get_subject (reader),
135 			                      tracker_turtle_reader_get_predicate (reader),
136 			                      tracker_turtle_reader_get_object (reader));
137 		}
138 
139 		g_object_unref (reader);
140 
141 		if (error) {
142 			g_message ("Turtle parse error: %s", error->message);
143 			g_error_free (error);
144 		}
145 
146 		g_free (fullpath);
147 		counter += 1;
148 		conf_file = g_dir_read_name (services);
149 	}
150 
151 	g_dir_close (services);
152 
153 	g_list_foreach (files, (GFunc) g_free, NULL);
154 	g_object_unref (f);
155 	g_free (dir_uri);
156 	g_debug ("Loaded %d ontologies\n", counter);
157 }
158 
159 gint
160 main (gint argc, gchar **argv)
161 {
162 	GOptionContext *context;
163 	TrackerTurtleReader *reader;
164 	GError *error = NULL;
165 
166 	/* Translators: this messagge will apper immediately after the  */
167 	/* usage string - Usage: COMMAND [OPTION]... <THIS_MESSAGE>     */
168 	context = g_option_context_new ("- Validate a turtle file against the ontology");
169 
170 	/* Translators: this message will appear after the usage string */
171 	/* and before the list of options.                              */
172 	g_option_context_add_main_entries (context, entries, NULL);
173 	g_option_context_parse (context, &argc, &argv, NULL);
174 
175 	if (!ontology_dir || !ttl_file) {
176 		gchar *help;
177 
178 		g_printerr ("%s\n\n",
179 		            "Ontology directory and turtle file are mandatory");
180 
181 		help = g_option_context_get_help (context, TRUE, NULL);
182 		g_option_context_free (context);
183 		g_printerr ("%s", help);
184 		g_free (help);
185 
186 		return -1;
187 	}
188 
189 	//"/home/ivan/devel/codethink/tracker-ssh/data/services"
190 	load_ontology_files (ontology_dir);
191 
192 	reader = tracker_turtle_reader_new (ttl_file, NULL);
193 
194 	while (error == NULL && tracker_turtle_reader_next (reader, &error)) {
195 		turtle_statement_handler (tracker_turtle_reader_get_subject (reader),
196 		                          tracker_turtle_reader_get_predicate (reader),
197 		                          tracker_turtle_reader_get_object (reader));
198 	}
199 
200 	g_object_unref (reader);
201 
202 	if (error) {
203 		g_message ("Turtle parse error: %s", error->message);
204 		g_error_free (error);
205 	}
206 
207 	if (!error_flag) {
208 		g_debug ("%s seems OK.", ttl_file);
209 	}
210 
211 	return 0;
212 }