public IEnumerable <IDataObject> BindDataObjects(SparqlResultSet sparqlResultSet, bool resultsAreOrdered = false)
        {
            if (sparqlResultSet == null)
            {
                throw new ArgumentNullException(nameof(sparqlResultSet));
            }
            var    resourceTriples   = new Dictionary <string, List <Triple> >();
            string lastLoadedSubject = null;

            switch (sparqlResultSet.Variables.Count())
            {
            case 1:
                // Single column results set contains only data object IRIs
                foreach (var uriNode in sparqlResultSet.Select(row => row[0] as IUriNode).Where(uriNode => uriNode?.Uri != null))
                {
                    yield return(_storeContext.MakeDataObject(uriNode.Uri.ToString()));
                }
                break;

            case 3:
                // Columns are triples, s, p, o in that order
                foreach (var t in sparqlResultSet.Select(row => MakeTriple(row[0], row[1], row[2])))
                {
                    if (resourceTriples.ContainsKey(t.Subject))
                    {
                        resourceTriples[t.Subject].Add(t);
                    }
                    else
                    {
                        resourceTriples[t.Subject] = new List <Triple> {
                            t
                        };
                    }
                    if (resultsAreOrdered && lastLoadedSubject != null && !lastLoadedSubject.Equals(t.Subject))
                    {
                        // Have collected all the triples we are going to see for the previously encountered subject, so emit its data object now
                        yield return(MakeDataObject(lastLoadedSubject, resourceTriples[lastLoadedSubject]));
                    }
                    lastLoadedSubject = t.Subject;
                }
                if (resultsAreOrdered && lastLoadedSubject != null)
                {
                    // Emit the final result
                    yield return(MakeDataObject(lastLoadedSubject, resourceTriples[lastLoadedSubject]));
                }
                else
                {
                    // We have batched up all of the triples and can now emit the separate data objects
                    foreach (var entry in resourceTriples)
                    {
                        yield return(MakeDataObject(entry.Key, entry.Value));
                    }
                }
                break;

            default:
                throw new ArgumentException(
                          $"Expected a result set with either 1 or 3 columns. Got a result set with {sparqlResultSet.Variables.Count()} columns", nameof(sparqlResultSet));
            }
        }
        private IDataObject BindRdfDataObject(string resourceAddress, string resourceType, XmlReader reader)
        {
            var resourceElementLevel = reader.Depth;

            if (SkipToStartElement(reader))
            {
                var dataObject = _storeContext.MakeDataObject(resourceAddress) as DataObject;
                var triples    = new List <Triple>();
                if (!String.IsNullOrEmpty(resourceType))
                {
                    triples.Add(new Triple {
                        Subject = resourceAddress, Predicate = DataObject.TypeDataObject.Identity, Object = resourceType
                    });
                }
                while (!reader.EOF)
                {
                    if (reader.Depth == resourceElementLevel)
                    {
                        break;
                    }
                    if (reader.IsStartElement())
                    {
                        var pred = reader.NamespaceURI + reader.LocalName;
                        var obj  = reader.GetAttribute("resource", RdfNamespace);
                        if (obj != null)
                        {
                            triples.Add(new Triple {
                                Subject = resourceAddress, Predicate = pred, Object = obj
                            });
                            reader.Read();
                        }
                        else if (!reader.IsEmptyElement)
                        {
                            var dt   = reader.GetAttribute("datatype", RdfNamespace);
                            var lang = reader.GetAttribute("xml:lang");
                            obj = reader.ReadElementContentAsString();
                            triples.Add(new Triple
                            {
                                Subject   = resourceAddress,
                                Predicate = pred,
                                IsLiteral = true,
                                Object    = obj,
                                LangCode  = lang,
                                DataType  = dt ?? RdfDatatypes.String
                            });
                        }
                    }
                    else
                    {
                        reader.Read();
                    }
                }
                dataObject.BindTriples(triples);
                return(dataObject);
            }
            return(null);
        }
        public void TestInsertDataObject()
        {
            var k = _store.MakeDataObject("p:kal");

            k.AddProperty("foaf:name", "Kal Ahmed");
            _store.SaveChanges();

            Assert.That(_store.GetDataObject("p:kal"), Is.Not.Null);
            Assert.That(_store.GetDataObject("foaf:name"), Is.Not.Null);
            Assert.That(_store.GetDataObject("foaf:person"), Is.Not.Null);
        }
Beispiel #4
0
 internal IDataObject GetDataObject(Uri identity, bool loadNow)
 {
     if (loadNow)
     {
         var dataObject = _store.GetDataObject(identity.ToString());
         if (dataObject == null)
         {
             throw new EntityFrameworkException(
                       String.Format("Could not find resource with identity {0}", identity));
         }
         return(dataObject);
     }
     return(_store.MakeDataObject(identity.ToString()));
 }
Beispiel #5
0
        private IDataObject BindRdfDataObject(IUriNode dataObjectResource, IGraph graph)
        {
            var triples = new List <Triple>();

            foreach (var t in graph.GetTriplesWithSubject(dataObjectResource))
            {
                if (t.Subject is IUriNode && t.Predicate is IUriNode)
                {
                    // Only handling triples that have a URI predicate
                    // subject will always be a UriNode, because that is what we used in the lookup
                    var subject   = (t.Subject as IUriNode).Uri.ToString();
                    var predicate = (t.Predicate as IUriNode).Uri.ToString();
                    if (t.Object is ILiteralNode)
                    {
                        var lit = t.Object as ILiteralNode;
                        triples.Add(new Triple
                        {
                            Subject   = subject,
                            Predicate = predicate,
                            IsLiteral = true,
                            Object    = lit.Value,
                            DataType  = lit.DataType == null ? Constants.DefaultDatatypeUri : lit.DataType.ToString(),
                            LangCode  = lit.Language
                        });
                    }
                    else if (t.Object is IUriNode)
                    {
                        var uriNode = t.Object as IUriNode;
                        triples.Add(new Triple
                        {
                            Subject   = subject,
                            Predicate = predicate,
                            IsLiteral = false,
                            Object    = uriNode.Uri.ToString()
                        });
                    }
                }
            }
            var dataObject = _storeContext.MakeDataObject(dataObjectResource.Uri.ToString()) as DataObject;

            dataObject.BindTriples(triples);
            return(dataObject);
        }