private void AddTriples <T>(T entity, TripleContainer container, PropertyInfo property) { RdfMapperPredicateAttribute attribute; ValidatePredicate(property, out attribute); string mappedPredicate = MapName(attribute.Name); if (string.IsNullOrWhiteSpace(mappedPredicate)) { throw new RdfMapperException(string.Format("{0} '{1}' could not be mapped", typeof(RdfMapperPredicateAttribute).Name, attribute.Name)); } var value = property.GetValue(entity, null); if (value != null) { if (property.IsCollection()) { foreach (var item in (IEnumerable)value) { if (entity.ContainsAttribute(typeof(RdfMapperSubjectAttribute))) { container.Triples.Add(new Triple(container.Subject, mappedPredicate, MapSubjectUri(item))); } } } else if (property.IsString() || property.IsValueType()) { container.Triples.Add(new Triple(container.Subject, mappedPredicate, MapObjectToString(value, attribute))); } } }
private void AddTriplesRecursive <T>(T entity, TripleContainer container) { Validate(entity); PropertyInfo[] properties = entity.GetType().GetProperties(); // Predicates foreach (PropertyInfo property in properties.Where(p => p.IsDefined(typeof(RdfMapperPredicateAttribute)))) { AddTriples(entity, container, property); } // Extended subjects foreach (PropertyInfo property in properties.Where(p => p.IsDefined(typeof(RdfMapperExtendedSubjectAttribute)))) { var value = property.GetValue(entity, null); if (value != null) { if (property.IsCollection()) { foreach (var item in (IEnumerable)value) { AddTriplesRecursive(item, container); } } else { AddTriplesRecursive(value, container); } } } }
public void RdfMapper_MapTriples_ObjectVersion2() { //assemble var mapper = new RdfMapper(new RdfMapperJsonReader(), MappingDocumentPath, ns2); var model = new ObjectVersion() { Id = 11 }; //act TripleContainer container = mapper.MapTriples(model); foreach (TestTriple triple in container.Triples.Select(t => new TestTriple(t))) { Debug.WriteLine(triple.ToString()); } //assert Assert.AreEqual(ns1 + "objects/11", container.Subject); }
/// <summary> /// Maps the provided class to RDF triples. /// </summary> /// <typeparam name="T">The type of the class to be mapped.</typeparam> /// <param name="entity">The class to be mapped.</param> /// <returns>A container of RDF triples.</returns> public TripleContainer MapTriples <T>(T entity) where T : class { RdfMapperSubjectAttribute subjectAttribute; ValidateSubject(entity, out subjectAttribute); TripleContainer container = new TripleContainer(); container.Subject = MapSubjectUri(entity); if (!string.IsNullOrWhiteSpace(subjectAttribute.RdfType)) { string mappedRdfType = MapName(subjectAttribute.RdfType); if (!string.IsNullOrWhiteSpace(mappedRdfType)) { container.Triples.Add(new Triple(container.Subject, RdfMapperConstants.RDF_TYPE, mappedRdfType)); } } AddTriplesRecursive(entity, container); return(container); }
public void RdfMapper_MapTriples_ObjectVersion1() { //assemble var mapper = new RdfMapper(new RdfMapperJsonReader(), MappingDocumentPath, ns2); var model = new ObjectVersion() { Id = 11, Description = "test description", CreatedDate = new DateTime(2016, 4, 27), Segmentation = new Segmentation() { MethodDescription = "test segmentation method description" }, ObjectVersionType = new ObjectVersionType() { RdfUri = ns4 + "raw_image" } }; model.Data.Add(new Data.Model.Data() { Id = 21, OriginalFileName = "test1.dcm", Extension = ".dcm" }); model.Data.Add(new Data.Model.Data() { Id = 22, OriginalFileName = "test2.dcm", Extension = ".dcm" }); model.AnatomicalRegions.Add(new AnatomicalRegion() { Id = 50801, Name = "Brain" }); model.AnatomicalRegions.Add(new AnatomicalRegion() { Id = 9611, Name = "Femur" }); //act TripleContainer container = mapper.MapTriples(model); IEnumerable <TestTriple> triples = container.Triples.Select(t => new TestTriple(t)); foreach (TestTriple triple in triples) { Debug.WriteLine(triple.ToString()); } //assert Assert.AreEqual(ns1 + "objects/11", container.Subject); Assert.IsFalse(triples.Contains(new TestTriple("", "", ""))); Assert.IsTrue(triples.Contains(new TestTriple(ns1 + "objects/11", RdfMapperConstants.RDF_TYPE, ns3 + "object"))); Assert.IsTrue(triples.Contains(new TestTriple(ns1 + "objects/11", ns3 + "object_id", "11"))); Assert.IsTrue(triples.Contains(new TestTriple(ns1 + "objects/11", ns3 + "object_created_date", "27.04.2016 00:00:00"))); Assert.IsTrue(triples.Contains(new TestTriple(ns1 + "objects/11", ns3 + "object_description", "test description"))); Assert.IsTrue(triples.Contains(new TestTriple(ns1 + "objects/11", ns3 + "object_file", ns1 + "files/21"))); Assert.IsTrue(triples.Contains(new TestTriple(ns1 + "objects/11", ns3 + "object_file", ns1 + "files/22"))); Assert.IsTrue(triples.Contains(new TestTriple(ns1 + "objects/11", ns3 + "object_type", ns4 + "raw_image"))); Assert.IsTrue(triples.Contains(new TestTriple(ns1 + "objects/11", ns3 + "object_segmentation_method_description", "test segmentation method description"))); Assert.IsTrue(triples.Contains(new TestTriple(ns1 + "objects/11", ns3 + "object_anatomical_region", "http://purl.org/obo/owlapi/fma#FMA_50801"))); Assert.IsTrue(triples.Contains(new TestTriple(ns1 + "objects/11", ns3 + "object_anatomical_region", "http://purl.org/obo/owlapi/fma#FMA_9611"))); }