beagle r4591 - in branches/beagle-rdf: RDFAdapter Util/SemWeb



Author: dbera
Date: Sat Mar  8 17:15:51 2008
New Revision: 4591
URL: http://svn.gnome.org/viewvc/beagle?rev=4591&view=rev

Log:
Lots of cleanup and tuning of our RDF code.
* Fix a semweb bug where ArrayList objects were created with IComparable<T> instead of IComparable.
* Predicate entities are of the form http://beagle-project.org/property#prop:t:full:name
* Use delegates in BeagleSource to allow different applications to suitably change the RDF source behaviour. E.g. some applications might want to create Entities for email address, some might want to create something as Entities.


Modified:
   branches/beagle-rdf/RDFAdapter/BeagleSource.cs
   branches/beagle-rdf/RDFAdapter/SemWebClient.cs
   branches/beagle-rdf/Util/SemWeb/Resource.cs

Modified: branches/beagle-rdf/RDFAdapter/BeagleSource.cs
==============================================================================
--- branches/beagle-rdf/RDFAdapter/BeagleSource.cs	(original)
+++ branches/beagle-rdf/RDFAdapter/BeagleSource.cs	Sat Mar  8 17:15:51 2008
@@ -2,6 +2,7 @@
 // BeagleSource.cs
 //
 // Copyright (C) 2007 Enrico Minack <minack l3s de>
+// Copyright (C) 2008 D Bera <dbera web gmail com>
 //
 
 //
@@ -30,6 +31,26 @@
 
 public class BeagleSource : SelectableSource {
 
+	public delegate void RDFToBeagleHook (Entity subject, Entity predicate, Resource obj, out Uri s, out string p, out string o);
+	public delegate void BeagleToRDFHook (Property property, out Resource resource); // Convert a property to the correct RDF resouce to be used as an object
+
+	private RDFToBeagleHook rdf_to_beagle_hook;
+	private BeagleToRDFHook property_to_rdf_hook;
+
+	public RDFToBeagleHook RDFToBeagle {
+		set { rdf_to_beagle_hook = value; }
+	}
+
+	public BeagleToRDFHook BeagleToRDF {
+		set { property_to_rdf_hook = value; }
+	}
+
+	public BeagleSource () : base ()
+	{
+		rdf_to_beagle_hook = new RDFToBeagleHook (DefaultRDFToBeagle);
+		property_to_rdf_hook = new BeagleToRDFHook (DefaultPropertyToRDF);
+	}
+
 	// counts the statements that match the template and returns if this number > 0
 	public bool Contains (Statement template)
 	{
@@ -63,18 +84,9 @@
 		Resource obj = template.Object;
 
 		// convert the SemWeb fields to the RDFQuery fields
-		Uri s    = (subj == null) ? null : new Uri (subj.Uri);
-		string p = (pred == null) ? null : pred.Uri;
-		string o = null;
-
-		if (obj != null) {
-			if (obj is Literal) {
-				Literal l = (Literal) obj;
-				o = l.Value;
-			} else {
-				o = obj.Uri;
-			}
-		}
+		Uri s;
+		string p, o;
+		rdf_to_beagle_hook (subj, pred, obj, out s, out p, out o);
 
 		RDFQuery query = new RDFQuery (s, p, o);
 		RDFQueryResult result = (RDFQueryResult) query.Send ();
@@ -82,15 +94,10 @@
 		foreach (Hit hit in result.Hits) {
 			Entity subject = new Entity (hit.Uri.ToString ()); //FIXME: Do we have to use strings here?
 			foreach (Property prop in hit.Properties) {
-				Entity predicate = new Entity (prop.Key);
-				Resource _object = null;
+				Entity predicate = BeaglePropertyToEntity (prop.Type, prop.Key);
+				Resource _object;
+				property_to_rdf_hook (prop, out _object);
 			
-				// for some properties the object is actually an URI (Entity)
-				if (predicate == "Uri" || predicate == "ParentUri" || predicate == "ParentDirUri")
-					_object = new Entity(prop.Value);
-					else
-				_object = new Literal(prop.Value);
-
 				// now create a the statement and add it to the result
 				Statement st = new Statement (subject, predicate, _object);
 				sink.Add (st);
@@ -118,4 +125,68 @@
 			return true;
 		}
 	}
+
+	public const string Prefix = "http://beagle-project.org/property#";;
+
+	public static Entity BeaglePropertyToEntity (string propname)
+	{
+		return BeaglePropertyToEntity (PropertyType.Internal, propname);
+	}
+
+	public static Entity BeaglePropertyToEntity (PropertyType type, string propname)
+	{
+		if (String.IsNullOrEmpty (propname))
+			throw new Exception ("bad property name");
+
+		return new Entity (Prefix + PropertyToFieldName (type, propname));
+	}
+
+	//////////// Internal knowledge (LuceneCommon.cs)
+
+	static private char TypeToCode (PropertyType type)
+	{
+		switch (type) {
+		case PropertyType.Text:    return 't';
+		case PropertyType.Keyword: return 'k';
+		case PropertyType.Date:    return 'd';
+		}
+		throw new Exception ("Bad property type: " + type);
+	}
+
+	private static string PropertyToFieldName (PropertyType type, string key)
+	{
+		if (type == PropertyType.Internal)
+			return key;
+		return String.Format ("prop:{0}:{1}", TypeToCode (type), key);
+
+	}
+
+	//////////////////////////////////////////////////
+
+	private static void DefaultRDFToBeagle (Entity subj, Entity pred, Resource obj, out Uri s, out string p, out string o)
+	{
+		s = (subj == null || String.IsNullOrEmpty (subj.Uri)) ? null : new Uri (subj.Uri);
+		p = (pred == null || String.IsNullOrEmpty (pred.Uri)) ? null : pred.Uri.Substring (Prefix.Length);
+		o = null;
+
+		if (obj != null) {
+			if (obj is Literal) {
+				Literal l = (Literal) obj;
+				o = l.Value;
+			} else {
+				o = obj.Uri;
+			}
+		}
+	}
+
+	private static void DefaultPropertyToRDF (Property prop, out Resource _object)
+	{
+		_object = null;
+
+		// for some properties the object is actually an URI (Entity)
+		if (prop.Key == "ParentDirUri")
+			_object = new Entity (prop.Value);
+		else
+			_object = new Literal (prop.Value);
+	}
 }

Modified: branches/beagle-rdf/RDFAdapter/SemWebClient.cs
==============================================================================
--- branches/beagle-rdf/RDFAdapter/SemWebClient.cs	(original)
+++ branches/beagle-rdf/RDFAdapter/SemWebClient.cs	Sat Mar  8 17:15:51 2008
@@ -24,29 +24,64 @@
 // SOFTWARE.
 //
 
+using System;
+using Beagle;
+using Beagle.Util;
 using SemWeb;
 
 public class SemWebClient {
 	public static void Main (string[] args)
 	{
-		System.Console.Out.WriteLine();
-		System.Console.Out.WriteLine("Querying for all Triples with MimeType:");
-		query(new Statement(null, new Entity("prop:k:beagle:MimeType"), null));
-		
-		System.Console.Out.WriteLine();
-		System.Console.Out.WriteLine("Querying for all Triples with FileSize:");
-		query(new Statement(null, new Entity("prop:k:fixme:filesize"), null));
+		BeagleSource source = new BeagleSource ();
+		source.RDFToBeagle = new BeagleSource.RDFToBeagleHook (RDFToBeagle);
+		source.BeagleToRDF = new BeagleSource.BeagleToRDFHook (EmailToEntity);
+
+		System.Console.Out.WriteLine ();
+		System.Console.Out.WriteLine ("Querying for all Triples with MimeType:");
+		query (source, new Statement (null, new Entity ("prop:k:beagle:MimeType"), null));
+
+		System.Console.Out.WriteLine ();
+		System.Console.Out.WriteLine ("Querying for all Triples with FileSize:");
+		query (source, new Statement (null, BeagleSource.BeaglePropertyToEntity ("prop:k:fixme:filesize"), null));
 		
-		System.Console.Out.WriteLine();
-		System.Console.Out.WriteLine("Querying for all Triples:");
-		query(Statement.All);
+		System.Console.Out.WriteLine ();
+		System.Console.Out.WriteLine ("Querying for all Triples:");
+		query (source, Statement.All);
 	}
 	
-	public static void query(Statement filter) {
-		RdfWriter writer = new N3Writer (System.Console.Out);
-		SelectableSource source = new BeagleSource();
-		source.Select (filter, writer);
-		writer.Close ();
+	public static void query (SelectableSource source, Statement filter) {
+		using (RdfWriter writer = new N3Writer (System.Console.Out))
+			source.Select (filter, writer);
+	}
+
+	// Make URIs out of certain objects
+	private static void RDFToBeagle (Entity subj, Entity pred, Resource obj, out Uri s, out string p, out string o)
+	{
+		s = (subj == null || String.IsNullOrEmpty (subj.Uri)) ? null : new Uri (subj.Uri);
+		p = (pred == null || String.IsNullOrEmpty (pred.Uri)) ? null : pred.Uri.Substring (BeagleSource.Prefix.Length);
+		o = null;
+
+		if (obj != null) {
+			if (obj is Literal) {
+				Literal l = (Literal) obj;
+				o = l.Value;
+			} else {
+				o = obj.Uri;
+				if (o.StartsWith ("mailto://";))
+					o = o.Substring (9);
+			}
+		}
+	}
+
+	private static void EmailToEntity (Property prop, out Resource _object)
+	{
+		_object = null;
+
+		// Create URIs for email addresses
+		if (prop.Key == "fixme:from_address" || prop.Key == "fixme:cc_address" || prop.Key == "fixme:to_address")
+			_object = new Entity ("mailto://"; + prop.Value);
+		else
+			_object = new Literal (prop.Value);
 	}
 }
 

Modified: branches/beagle-rdf/Util/SemWeb/Resource.cs
==============================================================================
--- branches/beagle-rdf/Util/SemWeb/Resource.cs	(original)
+++ branches/beagle-rdf/Util/SemWeb/Resource.cs	Sat Mar  8 17:15:51 2008
@@ -5,11 +5,7 @@
 namespace SemWeb {
 	
 	public abstract class Resource :
-#if DOTNET2
-	IComparable<Resource>
-#else
 	IComparable
-#endif
 	{
 		internal object ekKey, ekValue;
 		internal ArrayList extraKeys;
@@ -71,11 +67,7 @@
 			extraKeys.Add(k);
 		}
 
-#if DOTNET2
-		public int CompareTo(Resource other) {
-#else
 		public int CompareTo(object other) {
-#endif
 			// We'll make an ordering over resources.
 			// First named entities, then bnodes, then literals.
 			// Named entities are sorted by URI.



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