public void TestGetMatchingTypes() { Class[] types = new[] { MappingTestOntology.BaseClass }; var res = MappingDiscovery.GetMatchingTypes(types, typeof(BaseClass), false); Assert.AreEqual(1, res.Length); Assert.AreEqual(typeof(BaseClass), res.First()); types = new[] { MappingTestOntology.BaseClass, MappingTestOntology.SubClass }; res = MappingDiscovery.GetMatchingTypes(types, typeof(BaseClass), true); //Assert.AreEqual(1, res.Length); Assert.AreEqual(typeof(SubClass), res.First()); types = new[] { MappingTestOntology.SubClass, MappingTestOntology.BaseClass }; res = MappingDiscovery.GetMatchingTypes(types, typeof(BaseClass), true); //Assert.AreEqual(1, res.Length); Assert.AreEqual(typeof(SubClass), res.First()); }
private Dictionary <string, T> FindResourceTypes <T>(bool inferencingEnabled) where T : Resource { Dictionary <string, T> result = new Dictionary <string, T>(); Dictionary <string, List <Class> > types = new Dictionary <string, List <Class> >(); string p; INode s, o; // _tripleProvider.Reset(); // Collect all types for every resource in the types dictionary. // I was going to use _queryResults.Select(), but that doesn't work with Virtuoso. while (_tripleProvider.HasNext) { s = _tripleProvider.S; p = _tripleProvider.P.ToString(); o = _tripleProvider.O; _tripleProvider.SetNext(); if (o.NodeType == NodeType.Uri && p == "http://www.w3.org/1999/02/22-rdf-syntax-ns#type") { if (s is IUriNode) { string suri = ((IUriNode)s).Uri.OriginalString; string obj = ((IUriNode)o).Uri.OriginalString; if (!types.ContainsKey(suri)) { types.Add(suri, new List <Class>()); } if (OntologyDiscovery.Classes.ContainsKey(obj)) { types[suri].Add(OntologyDiscovery.Classes[obj]); } else { types[suri].Add(new Class(new Uri(obj))); } } } } // Iterate over all types and find the right class and instatiate it. foreach (string subject in types.Keys) { Type[] classType = MappingDiscovery.GetMatchingTypes(types[subject], typeof(T), inferencingEnabled); if (classType.Length > 0) { #if DEBUG if (classType.Length > 1) { var msg = "Info: There is more that one assignable type for <{0}>. It was initialized as <{1}>."; Debug.WriteLine(string.Format(msg, subject, classType[0].Name)); } #endif T resource = (T)Activator.CreateInstance(classType[0], new Uri(subject)); resource.Model = _model; resource.IsNew = false; result[subject] = resource; } #if DEBUG else if (typeof(T) != typeof(Resource)) { string msg = "Info: No assignable type found for <{0}>."; if (inferencingEnabled) { msg += " Try disabling inference."; } Debug.WriteLine(string.Format(msg, subject)); } #endif } return(result); }
/// <summary> /// This method gets the RDF classes from the query result /// and tries to match it to a C# class. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="subjectColumn"></param> /// <param name="preducateColumn"></param> /// <param name="objectColumn"></param> /// <param name="queryResults"></param> /// <param name="inferencingEnabled"></param> /// <returns></returns> private Dictionary <string, T> FindResourceTypes <T>(DataTable queryResults, string subjectColumn, string preducateColumn, string objectColumn, bool inferencingEnabled = false) where T : Resource { Dictionary <string, T> result = new Dictionary <string, T>(); Dictionary <string, List <Class> > types = new Dictionary <string, List <Class> >(); string s, p, o; // Collect all types for every resource in the types dictionary. // I was going to use _queryResults.Select(), but that doesn't work with Virtuoso. foreach (DataRow row in queryResults.Rows) { s = row[subjectColumn].ToString(); p = row[preducateColumn].ToString(); if (p == "http://www.w3.org/1999/02/22-rdf-syntax-ns#type") { o = row[objectColumn].ToString(); if (!types.ContainsKey(s)) { types.Add(s, new List <Class>()); } if (OntologyDiscovery.Classes.ContainsKey(o)) { types[s].Add(OntologyDiscovery.Classes[o]); } else { types[s].Add(new Class(new Uri(o))); } } } // Iterate over all types and find the right class and instatiate it. foreach (string subject in types.Keys) { IList <Type> classType = MappingDiscovery.GetMatchingTypes(types[subject], typeof(T), inferencingEnabled); if (classType.Count > 0) { #if DEBUG if (classType.Count > 1) { string msg = "Info: There is more that one assignable type for <{0}>. It was initialized using the first."; Debug.WriteLine(string.Format(msg, subject)); } #endif T resource = (T)Activator.CreateInstance(classType[0], new UriRef(subject)); resource.SetModel(_model); resource.IsNew = false; result[subject] = resource; } #if DEBUG else if (typeof(T) != typeof(Resource)) { string msg = "Info: No assignable type found for <{0}>."; if (inferencingEnabled) { msg += " Try disabling inference."; } Debug.WriteLine(string.Format(msg, subject)); } #endif } return(result); }