Semantics.SDK: RdfObjects FAQ

Questions related to ontology objects:

  1. How do I generate .NET objects from an ontology?
  2. How do I get an XSD schema from an ontology object?
  3. How do I output an ontology object as XML?
  4. How do I create an ontology object from XML?
  5. How do I create an explicit relationship between two ontology objects?
  6. How do I create an inferred relationship between two ontology objects?

How do I generate .NET objects from an ontology?

Please click the following link to open the Object Generation User Guide in a new window.

Back to Top

How do I get an XSD schema from an ontology object?

Every RdfObject has an overloaded GetSchema() for obtaining the object's XSD schema. The example below demonstrates outputting the schema to the console.

  1. public void Get_an_XSD_Schema_from_an_Ontology_Object()  
  2. {  
  3.     Person.GetSchema().Write(Console.Out);  
  4. }  

Output:

Due to the length of the output generated by the following code, it will not be displayed.

Back to Top

How do I output an ontology object as XML?

Every RdfObject has an overloaded ToXml() method that provides multiple ways to obtain an XML representation of the ontology object.

  1. public void Output_an_Ontology_Object_as_XML()  
  2. {  
  3.     FoafContext ctx = new FoafContext();  
  4.     Person justin = ctx.Create<Person>(new RdfUri("http://www.intellidimension.com/people#JBarron"));  
  5.   
  6.     /* If an ontology does not define a range for a specific property or a max  
  7.      * cardinatlity of 1 but your intention is to use it as if it had a range and  
  8.      * a max cardinality of one for readability you can do so. The result will be  
  9.      * that the collection will be cleared and a single RdfLiteral value will be  
  10.      * placed in the collection with the value specified on the right side of the  
  11.      * equal sign. This is done using implicit conversion operators. */  
  12.     justin.Birthday = "01-01"/* is equivalent to justin.Birthday.Add(new RdfLiteral("01-01"))  
  13.                                 * or justin.Birthday.Add("01-01"). Except that the justin.Birthday  
  14.                                 * collection will be cleared and only a single value saved */  
  15.   
  16.     justin.FirstName = "Justin";  
  17.     justin.Family_name = "Barron";  
  18.     justin.Gender = "male";  
  19.     justin.MsnChatID.Add("jbarron@intellidimension.com");  
  20.     justin.MsnChatID.Add(new RdfLiteral("jbarron@intellidimension.com")); //equivalent functionally to the line above  
  21.   
  22.     Console.WriteLine(justin.ToXml());              
  23. }  

Output:

<?xml version="1.0" encoding="utf-16"?>
<RdfObjects>
  <RdfObject>
    <Person id="http://www.intellidimension.com/people#JBarron">
      <MsnChatID isUri="false" datatype="" lang="" value="jbarron@intellidimension.com" />
      <Gender isUri="false" datatype="" lang="" value="male" />
      <FirstName isUri="false" datatype="" lang="" value="Justin" />
      <Family_name isUri="false" datatype="" lang="" value="Barron" />
      <Birthday isUri="false" datatype="" lang="" value="01-01" />
    </Person>
  </RdfObject>
</RdfObjects>

Back to Top

How do I create an ontology object from XML?

Every RdfObject has an overloaded FromXml() method that provides multiple ways to create an ontology object from XML.

  1. public void Create_an_Ontology_Object_from_XML()  
  2. {  
  3.     String input = @"<?xml version=""1.0"" encoding=""utf-16""?> 
  4.     <RdfObjects> 
  5.       <RdfObject> 
  6.         <Person id=""http://www.intellidimension.com/people#JBarron""> 
  7.           <MsnChatID isUri=""false"" datatype="""" lang="""" value=""jbarron@intellidimension.com"" /> 
  8.           <Gender isUri=""false"" datatype="""" lang="""" value=""male"" /> 
  9.           <FirstName isUri=""false"" datatype="""" lang="""" value=""Justin"" /> 
  10.           <Family_name isUri=""false"" datatype="""" lang="""" value=""Barron"" /> 
  11.           <Birthday isUri=""false"" datatype="""" lang="""" value=""01-01"" /> 
  12.         </Person> 
  13.       </RdfObject> 
  14.     </RdfObjects>";  
  15.     XmlReader reader = XmlReader.Create(new StringReader(input));  
  16.     FoafContext ctx = new FoafContext();  
  17.     Person justin = ctx.FromXml<Person>(reader);  
  18.   
  19.     Console.WriteLine(justin.ToXml());  
  20. }  

Output:

<?xml version="1.0" encoding="utf-16"?>
<RdfObjects>
  <RdfObject>
    <Person id="http://www.intellidimension.com/people#JBarron">
      <MsnChatID isUri="false" datatype="" lang="" value="jbarron@intellidimension.com" />
      <Gender isUri="false" datatype="" lang="" value="male" />
      <FirstName isUri="false" datatype="" lang="" value="Justin" />
      <Family_name isUri="false" datatype="" lang="" value="Barron" />
      <Birthday isUri="false" datatype="" lang="" value="01-01" />
    </Person>
  </RdfObject>
</RdfObjects>

Back to Top

How do I create an explicit relationship between two ontology objects?

Relationships can be defined between ontology objects depending on the ontology being used. The example below demonstrates the explicit declaration of a foaf:knows relationship between two Person objects generated using the FOAF ontology.

  1. public void Create_an_Explicit_Relationship_Between_Two_Ontology_Objects()  
  2. {  
  3.     FoafContext ctx = new FoafContext();  
  4.   
  5.     Person justin = ctx.Create<Person>(new RdfUri("http://www.intellidimension.com/people#JBarron"));  
  6.     justin.FirstName = "Justin";  
  7.     justin.Family_name = "Barron";  
  8.     justin.Mbox.Add(new RdfLiteral("jbarron@intellidimension.com"));  
  9.   
  10.     Person derrish = ctx.Create<Person>(new RdfUri("http://www.intellidimension.com/people#DRepchick"));  
  11.     derrish.FirstName = "Derrish";  
  12.     derrish.Family_name = "Repchick";  
  13.     derrish.Mbox.Add(new RdfLiteral("drepchick@intellidimension.com"));  
  14.   
  15.     //Since inferencing is not being use, the relationship must be explicitly defined from both sides.  
  16.     justin.Knows.Add(derrish);  
  17.     derrish.Knows.Add(justin);  
  18.   
  19.     //Verify that Justin and Derrish know each other  
  20.     Console.WriteLine("Justin knows Derrish: {0}", justin.Knows.Contains(derrish));  
  21.     Console.WriteLine("Derrish knows Justin: {0}", derrish.Knows.Contains(justin));  
  22. }  

Output:

Justin knows Derrish: True
Derrish knows Justin: True

Back to Top

How do I create an inferred relationship between two ontology objects?

Relationships can be defined between ontology objects depending on the ontology being used. The example below demonstrates the inferred declaration of a foaf:knows relationship between two Person objects generated using the FOAF ontology by introducing an inference rule to the graph.

  1. public void Create_an_Inferred_Relationship_Between_Two_Ontology_Objects()  
  2. {  
  3.     //Sparql query to infer a foaf:knows relationship  
  4.     string ruleSparql = @"prefix foaf: <http://xmlns.com/foaf/0.1/> 
  5.                            with ( 
  6.                                 construct {?o foaf:knows ?s} 
  7.                                      where {?s foaf:knows ?o} 
  8.                            ) 
  9.                            construct {?s ?p ?o} 
  10.                            where {?s ?p ?o}";  
  11.   
  12.     //Parse the rule into an AbstractQuery  
  13.     AbstractQuery rule = new SparqlParser().Parse(ruleSparql, new ClientModel());  
  14.       
  15.     //Create the context with the applicable rule  
  16.     FoafContext ctx = new FoafContext(new GraphDataSource(), rule);  
  17.       
  18.     Person justin = ctx.Create<Person>(new RdfUri("http://www.intellidimension.com/people#JBarron"));  
  19.     justin.FirstName = "Justin";  
  20.   
  21.     Person derrish = ctx.Create<Person>(new RdfUri("http://www.intellidimension.com/people#DRepchick"));  
  22.     derrish.FirstName = "Derrish";  
  23.   
  24.     justin.Knows.Add(derrish);  
  25.   
  26.     //Verify that Justin knows Derrish (explicit), and Derrish knows Justin (inferred)  
  27.     Console.WriteLine("Justin knows Derrish: {0}", justin.Knows.Contains(derrish));  
  28.     Console.WriteLine("Derrish knows Justin: {0}", derrish.Knows.Contains(justin));  
  29.     Console.WriteLine();  
  30.   
  31.     /* There are many ontology related inferences that occur in the RdfObjectContext base.  The next  
  32.      * line will create a new InferenceDataSource that excludes those rules inherent to the RdfObjectContext  
  33.      * base and will output the result. */  
  34.     new InferenceDataSource(((InferenceDataSource)ctx.Datasource).Facts, rule).Format<RdfXmlFormatter>(Console.Out);  
  35. }  

Output:

Justin knows Derrish: True
Derrish knows Justin: True

<?xml version="1.0" encoding="utf-16"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:ns1="http://xmlns.com/foaf/0.1/">
  <rdf:Description rdf:about="http://www.intellidimension.com/people#DRepchick">
    <rdf:type rdf:resource="http://xmlns.com/foaf/0.1/Person" />
    <ns1:firstName>Derrish</ns1:firstName>
    <ns1:knows rdf:resource="http://www.intellidimension.com/people#JBarron" />
  </rdf:Description>
  <rdf:Description rdf:about="http://www.intellidimension.com/people#JBarron">
    <rdf:type rdf:resource="http://xmlns.com/foaf/0.1/Person" />
    <ns1:firstName>Justin</ns1:firstName>
    <ns1:knows rdf:resource="http://www.intellidimension.com/people#DRepchick" />
  </rdf:Description>
</rdf:RDF>

Back to Top