Skip to content

Instantly share code, notes, and snippets.

@ronmichael
Last active December 15, 2015 22:19
Show Gist options
  • Save ronmichael/5332278 to your computer and use it in GitHub Desktop.
Save ronmichael/5332278 to your computer and use it in GitHub Desktop.
Convert a dotNet C# object into RDF (Turtle)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
// just some rough playing around converting an object to RDF Turtle.
class ObjectToTurtle
{
[System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Struct)]
public class ObjectIDAttribute : System.Attribute
{
private string id;
public ObjectIDAttribute(string id)
{
this.id = id;
}
public string GetObjectID()
{
return id;
}
}
[ObjectID("id")]
public class Organization
{
public int id { get; set; }
public string name;
public User[] users { get; set; }
}
[ObjectID("id")]
public class User
{
public int? id { get; set; }
public string login { get; set; }
}
public static void Test()
{
// some data
Organization[] organizations = new Organization[] {
new Organization {
id = 1,
name = "llc",
users = new User[] {
new User { id=1, name="jane" },
new User { id=2, name="bob" }
}
},
new Organization {
id = 2,
name = "inc",
users = new User[] {
new User { id=3, name="mary" },
new User { id=4, name="joe" }
}
}
};
// convert it
string rdf = "";
Examine(events, ref rdf, "http://fynydd.com/example#");
System.IO.File.WriteAllText("c:\\temp\\turtle.ttl", tx);
System.Diagnostics.Process.Start("c:\\temp\\turtle.ttl");
}
public static List<string> Examine(object obj, ref string output, string defaultgraph = "" )
{
if(String.IsNullOrEmpty(output) && !String.IsNullOrEmpty(defaultgraph))
{
output = "@prefix : <" + defaultgraph + "#> .\r\n\r\n";
}
List<string> items = new List<string>();
Type otype = obj.GetType();
if (otype.IsArray)
{
foreach (object objX in (Array)obj)
{
items.AddRange(Examine(objX, ref output));
}
return items;
}
// find ObjectID property
System.Reflection.MemberInfo info = obj.GetType();
object[] attributes = info.GetCustomAttributes(false);
string idattribute = "?";
foreach(object attribute in info.GetCustomAttributes(false))
{
if(attribute is ObjectIDAttribute)
{
ObjectIDAttribute x = (ObjectIDAttribute)attribute;
idattribute = x.GetObjectID();
}
}
// determine object ID
string id = Guid.NewGuid().ToString();
if (!String.IsNullOrEmpty(idattribute) && obj.GetType().GetProperty(idattribute) != null)
{
object idvalue = obj.GetType().GetProperty(idattribute).GetValue(obj, null);
if (idvalue != null)
id = idvalue.ToString();
}
items.Add(id);
string type = obj.GetType().ToString();
string prefix = " ";
output += ":" + CleanClassName(obj) + "_" + id + "\r\n";
output += prefix + "a :" + CleanClassName(obj) + " ;\r\n";
string output2 = "";
foreach (System.Reflection.PropertyInfo p in obj.GetType().GetProperties())
{
object x = p.GetValue(obj, null);
if (x!=null)
{
output += prefix + ":" + p.Name + " ";
string xtype = x.GetType().ToString();
if (x is System.Boolean)
output += ((bool)x).ToString().ToLower();
else if (xtype.StartsWith("System.Int"))
output += x.ToString();
else if (xtype.StartsWith("System"))
// http://www.w3.org/TeamSubmission/turtle/#sec-strings
output += "\"" + x.ToString().Replace("\\","\\\\").Replace("\r","\\r").Replace("\n","\\n").Replace("\"","\\").Replace(">","\\>").Replace("\t","\\t") + "\"";
else
{
List<string> items2 = Examine(x, ref output2);
if(items2.Count>1)
output += "(\r\n";
foreach (string ll in items2)
{
if (items2.Count > 1) output += prefix + prefix;
output += ":" + CleanClassName(x) + "_" + ll + " ";
if (items2.Count > 1) output += "\r\n";
}
if (items2.Count > 1) output += prefix + ")";
}
output += " ;\r\n";
}
}
output = output.Substring(0, output.Length - 3) + " .\r\n\r\n";
output += output2;
return items;
}
public static string CleanClassName(object obj)
{
string name = obj.GetType().ToString();
int x = name.IndexOf("+");
if (x > 0)
name = name.Substring(x + 1);
name = name.Substring(0, 1).ToLower() + name.Substring(1).Replace("[]","");
return name;
}
}
@prefix : <http://fynydd.com/example#> .
:organization_1
a :organization ;
:id 1 ;
:name "llc" ;
:user (
:user_1
:user_2
) .
:user_1
a :user ;
:id 1 ;
:name "jane" .
:user_2
a :user ;
:id 2 ;
:name "bob" .
:organization_2
a :organization ;
:id 2 ;
:name "inc" ;
:user (
:user_3
:user_4
) .
:user_3
a :user ;
:id 3 ;
:name "mary" .
:user_4
a :user ;
:id 4 ;
:name "joe" .
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment