Describes triple full name with name, namespace and prefix. Namespace or prefix may be absent
コード例 #1
0
ファイル: Triples.cs プロジェクト: nhatton/ChronoZoom
        /// <summary>
        /// Delete triple with specified parameters.
        /// </summary>
        /// <param name="subjectName">Subject</param>
        /// <param name="predicateName">Predicate</param>
        /// <param name="objectName">Object</param>
        /// <returns>Returns 'True' if operation completed succeful, 'False' otherwise.</returns>
        public bool DeleteTriplet(TripleName subjectName, TripleName predicateName, TripleName objectName)
        {
            subjectName = EnsurePrefix(subjectName);
            predicateName = EnsurePrefix(predicateName);
            objectName = EnsurePrefix(objectName);

            var shortSubjectStr = subjectName.ToString();
            var shortPredicateStr = predicateName.ToString();
            var shortObjectStr = objectName.ToString();

            var tr = Triples.Where(t => t.Subject == shortSubjectStr && t.Predicate == shortPredicateStr).Include(o => o.Objects).FirstOrDefault();
            if (tr != null)
            {
                var obj = tr.Objects.FirstOrDefault(o => o.Object == shortObjectStr);
                if (obj != null)
                {
                    TripleObjects.Remove(TripleObjects.Find(obj.TripleObject_Id));

                    if (!tr.Objects.Any())
                        Triples.Remove(tr);

                    SaveChanges();
                    return true;
                }
            }
            return false;
        }
コード例 #2
0
        /// <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());
        }
コード例 #3
0
        /// <summary>
        /// Delete triple with specified parameters.
        /// </summary>
        /// <param name="subjectName">Subject</param>
        /// <param name="predicateName">Predicate</param>
        /// <param name="objectName">Object</param>
        /// <returns>Returns 'True' if operation completed succeful, 'False' otherwise.</returns>
        public bool DeleteTriplet(TripleName subjectName, TripleName predicateName, TripleName objectName)
        {
            subjectName   = EnsurePrefix(subjectName);
            predicateName = EnsurePrefix(predicateName);
            objectName    = EnsurePrefix(objectName);

            var shortSubjectStr   = subjectName.ToString();
            var shortPredicateStr = predicateName.ToString();
            var shortObjectStr    = objectName.ToString();

            var tr = Triples.Where(t => t.Subject == shortSubjectStr && t.Predicate == shortPredicateStr).Include(o => o.Objects).FirstOrDefault();

            if (tr != null)
            {
                var obj = tr.Objects.FirstOrDefault(o => o.Object == shortObjectStr);
                if (obj != null)
                {
                    TripleObjects.Remove(TripleObjects.Find(obj.TripleObject_Id));

                    if (!tr.Objects.Any())
                    {
                        Triples.Remove(tr);
                    }

                    SaveChanges();
                    return(true);
                }
            }
            return(false);
        }
コード例 #4
0
ファイル: Triples.cs プロジェクト: nhatton/ChronoZoom
 /// <summary>Ensures that triple name has prefix. If triple name has namespace, it is converted to prefix</summary>
 /// <param name="name">Name of the triple</param>
 /// <returns>Name of the triple with prefix available</returns>
 public TripleName EnsurePrefix(TripleName name)
 {
     if(name.Prefix == null) {
         var found = TripleName.PrefixesAndNamespaces.FirstOrDefault(p => p.Value == name.Namespace );
         if (found.Key != null)
             return new TripleName(name.Namespace, found.Key, name.Name);
         var tr = TriplePrefixes.FirstOrDefault(t => t.Namespace == name.Namespace);
         if(tr != null)
             return new TripleName(name.Namespace, tr.Prefix, name.Name);
         else
             throw new ArgumentException(String.Format("There is no known prefix for namespace {0}", name.Namespace));
     } else
         return name;
 }
コード例 #5
0
        /// <summary>
        /// Add triple with specified parameters.
        /// </summary>
        /// <param name="subjectStr">Subject</param>
        /// <param name="predicateStr">Predicate</param>
        /// <param name="objectStr">Object</param>
        /// <returns>Returns 'True' if operation completed succeful, 'False' otherwise.</returns>
        /// <example>PutTriplet( “czusr:76087518-4f8e-4d3a-9bfb-2fd2332376eb”, “czpred:favorite”, "cztimeline:3a87bb5c-85f7-4305-8b2f-f2002580cd25")</example>
        public bool PutTriplet(TripleName subjectName, TripleName predicateName, TripleName objectName)
        {
            subjectName   = EnsurePrefix(subjectName);
            predicateName = EnsurePrefix(predicateName);
            objectName    = EnsurePrefix(objectName);
            var shortSubjectStr   = subjectName.ToString();
            var shortPredicateStr = predicateName.ToString();
            var shortObjectStr    = objectName.ToString();

            var tr = Triples.Where(t => t.Subject == shortSubjectStr && t.Predicate == shortPredicateStr).Include(o => o.Objects).FirstOrDefault();

            if (tr != null)
            {
                if (tr.Objects.FirstOrDefault(x => x.Object == shortObjectStr) == null)
                {
                    tr.Objects.Add(
                        new TripleObject
                    {
                        Object          = shortObjectStr,
                        TripleObject_Id = Guid.NewGuid()
                    }
                        );
                }
                else
                {
                    return(true);
                }
                SaveChanges();
                return(true);
            }
            var triple = new Triple
            {
                Subject   = shortSubjectStr,
                Predicate = shortPredicateStr,
                Id        = Guid.NewGuid(),
                Objects   = new Collection <TripleObject>
                {
                    new TripleObject
                    {
                        Object          = shortObjectStr,
                        TripleObject_Id = Guid.NewGuid()
                    }
                }
            };

            Triples.Add(triple);
            SaveChanges();
            return(true);
        }
コード例 #6
0
 public TripleName TryEnsureNamespace(TripleName name)
 {
     if (name.Namespace == null)
     {
         if (TripleName.PrefixesAndNamespaces.ContainsKey(name.Prefix))
         {
             return(new TripleName(TripleName.PrefixesAndNamespaces[name.Prefix], name.Prefix, name.Name));
         }
         var tr = TriplePrefixes.FirstOrDefault(x => x.Prefix == name.Prefix);
         return(tr != null ? new TripleName(tr.Namespace, name.Prefix, name.Name) : name);
     }
     else
     {
         return(name);
     }
 }
コード例 #7
0
        /// <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());
        }
コード例 #8
0
 /// <summary>Ensures that triple name has prefix. If triple name has namespace, it is converted to prefix</summary>
 /// <param name="name">Name of the triple</param>
 /// <returns>Name of the triple with prefix available</returns>
 public TripleName EnsurePrefix(TripleName name)
 {
     if (name.Prefix == null)
     {
         var found = TripleName.PrefixesAndNamespaces.FirstOrDefault(p => p.Value == name.Namespace);
         if (found.Key != null)
         {
             return(new TripleName(name.Namespace, found.Key, name.Name));
         }
         var tr = TriplePrefixes.FirstOrDefault(t => t.Namespace == name.Namespace);
         if (tr != null)
         {
             return(new TripleName(name.Namespace, tr.Prefix, name.Name));
         }
         else
         {
             throw new ArgumentException(String.Format("There is no known prefix for namespace {0}", name.Namespace));
         }
     }
     else
     {
         return(name);
     }
 }
コード例 #9
0
ファイル: Storage.cs プロジェクト: sadeghsm/ChronoZoom
 /// <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;
     }
 }
コード例 #10
0
ファイル: Storage.cs プロジェクト: yonyonson604/ChronoZoom
        /// <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);
            }
        }
コード例 #11
0
ファイル: Triples.cs プロジェクト: nhatton/ChronoZoom
 public TripleName TryEnsureNamespace(TripleName name)
 {
     if(name.Namespace == null)
     {
         if (TripleName.PrefixesAndNamespaces.ContainsKey(name.Prefix))
             return new TripleName(TripleName.PrefixesAndNamespaces[name.Prefix], name.Prefix, name.Name);
         var tr = TriplePrefixes.FirstOrDefault(x => x.Prefix == name.Prefix);
         return tr != null ? new TripleName(tr.Namespace, name.Prefix, name.Name) : name;
     } else
         return name;
 }
コード例 #12
0
ファイル: Triples.cs プロジェクト: nhatton/ChronoZoom
        /// <summary>
        /// Add triple with specified parameters.
        /// </summary>
        /// <param name="subjectStr">Subject</param>
        /// <param name="predicateStr">Predicate</param>
        /// <param name="objectStr">Object</param>
        /// <returns>Returns 'True' if operation completed succeful, 'False' otherwise.</returns>
        /// <example>PutTriplet( “czusr:76087518-4f8e-4d3a-9bfb-2fd2332376eb”, “czpred:favorite”, "cztimeline:3a87bb5c-85f7-4305-8b2f-f2002580cd25")</example>
        public bool PutTriplet(TripleName subjectName, TripleName predicateName, TripleName objectName)
        {
            subjectName = EnsurePrefix(subjectName);
            predicateName = EnsurePrefix(predicateName);
            objectName = EnsurePrefix(objectName);
            var shortSubjectStr = subjectName.ToString();
            var shortPredicateStr = predicateName.ToString();
            var shortObjectStr = objectName.ToString();

            var tr = Triples.Where(t => t.Subject == shortSubjectStr && t.Predicate == shortPredicateStr).Include(o => o.Objects).FirstOrDefault();
            if (tr != null)
            {
                if (tr.Objects.FirstOrDefault(x => x.Object == shortObjectStr) == null)
                {
                    tr.Objects.Add(
                        new TripleObject
                            {
                                Object = shortObjectStr,
                                TripleObject_Id = Guid.NewGuid()
                            }
                    );
                }
                else
                {
                    return true;
                }
                SaveChanges();
                return true;
            }
            var triple = new Triple
                {
                    Subject = shortSubjectStr,
                    Predicate = shortPredicateStr,
                    Id = Guid.NewGuid(),
                    Objects = new Collection<TripleObject>
                        {
                            new TripleObject
                                {
                                    Object = shortObjectStr,
                                    TripleObject_Id = Guid.NewGuid()
                                }
                        }
                };
            Triples.Add(triple);
            SaveChanges();
            return true;
        }