Exemplo n.º 1
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);
        }
Exemplo n.º 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());
        }
Exemplo n.º 3
0
 public virtual void Merge(BaseGraph graph)
 {
     foreach (var triple in graph.Triples)
     {
         Triples.Add(triple);
     }
 }
Exemplo n.º 4
0
        /// <summary>
        /// Gets the Serialization Information for serializing a Graph.
        /// </summary>
        /// <param name="info">Serialization Information.</param>
        /// <param name="context">Streaming Context.</param>
        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("base", BaseUri.ToSafeString());
            info.AddValue("triples", Triples.ToList(), typeof(List <Triple>));
            IEnumerable <KeyValuePair <String, String> > ns = from p in NamespaceMap.Prefixes
                                                              select new KeyValuePair <String, String>(p, NamespaceMap.GetNamespaceUri(p).AbsoluteUri);

            info.AddValue("namespaces", ns.ToList(), typeof(List <KeyValuePair <String, String> >));
        }
Exemplo n.º 5
0
        /// <summary>
        /// Clears all Triples from the Graph.
        /// </summary>
        /// <remarks>
        /// <para>
        /// The Graph will raise the <see cref="ClearRequested">ClearRequested</see> event at the start of the Clear operation which allows for aborting the operation if the operation is cancelled by an event handler.  On completing the Clear the <see cref="Cleared">Cleared</see> event will be raised.
        /// </para>
        /// </remarks>
        public virtual void Clear()
        {
            if (!RaiseClearRequested())
            {
                return;
            }

            Retract(Triples.ToList());

            RaiseCleared();
        }
Exemplo n.º 6
0
 public bool RemoveGraph(Uri graphUri)
 {
     if (HasGraph(graphUri))
     {
         IUriNode sourceGraph = RDFUtil.CreateUriNode(graphUri);
         this.Retract(sourceGraph, RDF.PropertyType, SD.ClassGraph);
         this.Assert(RDFUtil.CreateUriNode(BaseUri), RDFRuntime.PropertyRemovesGraph, sourceGraph);
         return(true);
     }
     Retract(Triples.Where(t => t.Involves(graphUri)).ToList());
     return(true);
 }
Exemplo n.º 7
0
        private T GetNode <T>(T node) where T : INode
        {
            var withSubject = Triples.WithSubject(node);
            var withObject  = Triples.WithObject(node);

            var firstWithSubject = withSubject.FirstOrDefault();

            if (firstWithSubject != null)
            {
                return((T)firstWithSubject.Subject);
            }

            return((T)withObject.FirstOrDefault()?.Object);
        }
Exemplo n.º 8
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);
        }
Exemplo n.º 9
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());
        }
Exemplo n.º 10
0
        private T GetNode <T>(T node) where T : INode
        {
            var ret = (T)Triples.WithSubject(node).FirstOrDefault()?.Subject;

            if (ret != null)
            {
                return(ret);
            }

            ret = (T)Triples.WithObject(node).FirstOrDefault()?.Object;
            if (ret != null)
            {
                return(ret);
            }

            ret = (T)Triples.WithPredicate(node).FirstOrDefault()?.Predicate;
            return(ret);
        }
Exemplo n.º 11
0
 public IEnumerable <Triple> SelectSubject(string subject)
 {
     return(Triples.Where(t => StringComparer.Ordinal.Equals(t.Subject.GetValue(), subject)));
 }
Exemplo n.º 12
0
 public IEnumerable <Triple> SelectPredicate(string predicate)
 {
     return(Triples.Where(t => StringComparer.Ordinal.Equals(t.Subject.GetValue(), predicate)));
 }
Exemplo n.º 13
0
 public void Assert(Triple triple)
 {
     Triples.Add(triple);
 }
Exemplo n.º 14
0
 public void Assert(RDFDataset.Quad quad)
 {
     Triples.Add(new Triple(quad.GetSubject(), quad.GetPredicate(), quad.GetObject()));
 }
Exemplo n.º 15
0
 /// <summary>
 /// Gets the uri types of this data object
 /// </summary>
 /// <returns>A list of uri types</returns>
 public IList <string> GetTypes()
 {
     CheckLoaded();
     return(Triples.Where(t => t.Predicate == TypeDataObject.Identity).Select(t => t.Object).ToList());
 }
Exemplo n.º 16
0
    public static void Main(string[] args)
    {
        Triples t = new Triples();

        t.FindTriples();
    }
Exemplo n.º 17
0
        /// <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);
            }
        }
Exemplo n.º 18
0
 public void Assert(RDFDataset.Node subNode, RDFDataset.Node predNode, RDFDataset.Node objNode)
 {
     Triples.Add(new Triple(subNode, predNode, objNode));
 }