/// <summary>Build list of triples that reference specified object</summary> /// <param name="predicate">Optional predicate name</param> /// <param name="obj">Object full name</param> /// <returns>List of triples</returns> public List <Triple> GetIncomingTriplets(string predicate, string obj) { var objectName = EnsurePrefix(TripleName.Parse(obj)).ToString(); IQueryable <Triple> triples; if (!String.IsNullOrEmpty(predicate)) { var predicateName = EnsurePrefix(TripleName.Parse(predicate)).ToString(); triples = Triples.Where(t => t.Predicate == predicateName); } else { // Not very efficient as it enumerates entire database triples = Triples; } return(triples. Include(o => o.Objects). // Include Objects collection Where(t => t.Objects.Any(o => o.Object == objectName)). ToArray(). // Stop LINQ to SQL and use simple collection transformations Select(t => new Triple { Subject = t.Subject, Predicate = t.Predicate, Objects = new Collection <TripleObject>(t.Objects.Where(o => o.Object == objectName).ToArray()) }).ToList()); }
/// <summary> /// Get triple by specified parameters. /// </summary> /// <param name="subject">Subject name with prefix</param> /// <param name="predicate">Predicate name with prefix or null</param> /// <param name="obj">Object name with prefix or null</param> /// <param name="expandPrefixes">If 'True' returns namespaces, else - prefixes.</param> /// <returns>Returns list of triplets.</returns> /// <example>GetTriplet( “czusr:76087518-4f8e-4d3a-9bfb-2fd2332376eb”, “czpred:favorite”, null, false)</example> public List <Triple> GetTriplet(string subject, string predicate = null, string obj = null, bool expandPrefixes = false) { if (subject == null) { throw new ArgumentNullException("subject"); } var subjectName = EnsurePrefix(TripleName.Parse(subject)); string subjectStr = subjectName.ToString(); IQueryable <Triple> triples; if (predicate != null) { var predicateName = EnsurePrefix(TripleName.Parse(predicate)); var predicateStr = predicateName.ToString(); if (obj != null) { var objectName = EnsurePrefix(TripleName.Parse(obj)); var objectStr = objectName.ToString(); triples = Triples.Where(t => t.Subject == subjectStr && t.Predicate == predicateStr).Include(o => o.Objects).Where(t => t.Objects.FirstOrDefault(x => x.Object == objectStr) != null); } else { triples = Triples.Where(t => t.Subject == subjectStr && t.Predicate == predicateStr).Include(o => o.Objects); } } else { triples = Triples.Where(t => t.Subject == subjectStr).Include(o => o.Objects); } if (expandPrefixes) { //ToDo:Use Join triples.ToList().ForEach(x => { x.Subject = TryEnsureNamespace(TripleName.Parse(x.Subject)).ToExpandedString(); x.Predicate = TryEnsureNamespace(TripleName.Parse(x.Predicate)).ToExpandedString(); foreach (var o in x.Objects) { o.Object = TryEnsureNamespace(TripleName.Parse(o.Object)).ToExpandedString(); } }); } return(triples.ToList()); }
/// <summary>Gets owner of the triplet</summary> /// <param name="name">Name of the triplet. Triplets referring to timelines, exhibits, artifacts and bNodes are supported. /// In all other cases method returns null.</param> /// <param name="bNodes">List of previously examined bNodes to avoid endless recursion</param> /// <returns>String representation of owning user ID</returns> public string GetSubjectOwner(TripleName name, List <string> bNodes = null) { name = EnsurePrefix(name); switch (name.Prefix) { case TripleName.UserPrefix: return(name.Name); case TripleName.TimelinePrefix: var timelineId = Guid.Parse(name.Name); var timeline = Timelines.Where(t => t.Id == timelineId).FirstOrDefault(); if (timeline == null) { return(null); } Entry(timeline).Reference(t => t.Collection).Load(); return(GetCollectionOwner(timeline.Collection)); case TripleName.ExhibitPrefix: var exhibitId = Guid.Parse(name.Name); var exhibit = Exhibits.Where(e => e.Id == exhibitId).FirstOrDefault(); if (exhibit == null) { return(null); } Entry(exhibit).Reference(e => e.Collection).Load(); return(GetCollectionOwner(exhibit.Collection)); case TripleName.ArtifactPrefix: var artifactId = Guid.Parse(name.Name); var artifact = ContentItems.Where(c => c.Id == artifactId).FirstOrDefault(); if (artifact == null) { return(null); } Entry(artifact).Reference(a => a.Collection).Load(); return(GetCollectionOwner(artifact.Collection)); case TripleName.TourPrefix: var tourId = Guid.Parse(name.Name); var tour = Tours.FirstOrDefault(t => t.Id == tourId); if (tour == null) { return(null); } Entry(tour).Reference(t => t.Collection).Load(); return(GetCollectionOwner(tour.Collection)); case "_": var subject = name.ToString(); // Guard against infinite loop if (bNodes != null && bNodes.Contains(subject)) { return(null); } // Find subject of any triple that uses passed subject as object var linkedSubject = Triples. Include(t => t.Objects). Where(t => t.Objects.Any(o => o.Object == subject)). Select(t => t.Subject).FirstOrDefault(); if (String.IsNullOrEmpty(linkedSubject)) { return(null); } // Get owner of linked subject if (bNodes == null) { bNodes = new List <string>(new string[] { subject }); } else { bNodes.Add(subject); } return(GetSubjectOwner(TripleName.Parse(linkedSubject), bNodes)); default: return(null); } }