/// <remarks>
        /// It is important to return UriRefs for correct equality comparision of URIs with fragment identifiers.
        /// </remarks>
        IEnumerable <UriRef> FetchUris(int offset, int limit)
        {
            String queryString = SparqlSerializer.SerializeFetchUris(_model, _query, offset, limit);

            SparqlQuery query = new SparqlQuery(queryString)
            {
                IsInferenceEnabled = _query.IsInferenceEnabled
            };

            using (DataTable queryResults = _store.ExecuteQuery(_store.CreateQuery(query), _transaction))
            {
                IEnumerable <BindingSet> bindings = GenerateBindings(queryResults);

                UriRef previousUri = null;

                foreach (BindingSet binding in bindings)
                {
                    UriRef currentUri = binding[_query.GetGlobalScopeVariableNames()[0]] as UriRef;

                    if (currentUri == null)
                    {
                        continue;
                    }

                    if (!currentUri.Equals(previousUri))
                    {
                        yield return(currentUri);
                    }

                    previousUri = currentUri;
                }
            }
        }
예제 #2
0
        public void TestClone()
        {
            ResourceQuery b = new ResourceQuery(nco.PersonContact);

            b.Where(nco.birthDate).LessThan(new DateTime(1990, 1, 1)).SortAscending();

            ResourceQuery a = new ResourceQuery(nco.PersonContact);

            a.Where(nco.gender);
            a.Where(nie.relatedTo, b);

            ResourceQuery c = b.Clone();

            string q = SparqlSerializer.Serialize(Model, c);

            IResourceQueryResult result = Model.ExecuteQuery(c);

            int i = 0;

            foreach (Resource r in result.GetResources())
            {
                i++;
            }

            Assert.AreEqual(18, i);
        }
예제 #3
0
        public virtual int Count()
        {
            string countQuery = SparqlSerializer.SerializeCount(_model, _query);

            SparqlQuery query = new SparqlQuery(countQuery);

            // TODO: Apply inferencing if enabled.

            object result = _store.ExecuteQuery(query.ToString());

            if (result is SparqlResultSet)
            {
                SparqlResultSet set = result as SparqlResultSet;

                if (set.Count > 0 && set[0].Count > 0)
                {
                    var value = ParseCellValue(set[0][0]);

                    if (value.GetType() == typeof(int))
                    {
                        return((int)value);
                    }
                }
            }

            return(-1);
        }
        public IEnumerable <T> GetResources <T>(int offset = -1, int limit = -1) where T : Resource
        {
            if (!_query.ProvidesStatements())
            {
                throw new ArgumentException("Error: The given SELECT query cannot be resolved into statements.");
            }

            if (!_query.IsInferenceEnabled)
            {
                String queryString = SparqlSerializer.SerializeOffsetLimit(_model, _query, offset, limit);

                SparqlQuery query = new SparqlQuery(queryString);

                using (DataTable queryResults = _store.ExecuteQuery(_store.CreateQuery(query), _transaction))
                {
                    foreach (T t in GenerateResources <T>(queryResults))
                    {
                        yield return(t);
                    }
                }
            }
            else
            {
                // TODO: Make resources which are returned from a inferenced query read-only in order to improve query performance.

                // NOTE: When inferencing is enabled, we are unable to determine which triples were inferred and
                // which not. Therefore we need to issue a query to get the URIs of all the resources the original
                // query would return and issue another query to describe those resources withoud inference.
                List <UriRef> uris = FetchUris(offset, limit).ToList();

                if (!uris.Count.Equals(0))
                {
                    StringBuilder queryBuilder = new StringBuilder();

                    foreach (Uri uri in uris)
                    {
                        queryBuilder.Append(SparqlSerializer.SerializeUri(uri));
                    }

                    SparqlQuery query = new SparqlQuery(string.Format("DESCRIBE {0}", queryBuilder.ToString()));

                    ISparqlQueryResult queryResult = _model.ExecuteQuery(query);

                    if (_isOrdered)
                    {
                        foreach (T t in queryResult.GetResources <T>().OrderBy(o => uris.IndexOf(o.Uri)))
                        {
                            yield return(t);
                        }
                    }
                    else
                    {
                        foreach (T t in queryResult.GetResources <T>())
                        {
                            yield return(t);
                        }
                    }
                }
            }
        }
예제 #5
0
        public void TestStringSerializeResourceEmpty()
        {
            Resource empty    = new Resource("http://test.com/ex");
            var      res      = SparqlSerializer.SerializeResource(empty);
            var      expected = "";

            Assert.AreEqual(expected, res);
        }
예제 #6
0
        public void TestStringSerializeResource()
        {
            Resource r = new Resource("http://example.com/ex");

            r.AddProperty(Ontologies.dc.title, "MyResource");

            string res      = SparqlSerializer.SerializeResource(r);
            string expected = "<http://example.com/ex> <http://purl.org/dc/elements/1.1/title> 'MyResource'. ";

            Assert.AreEqual(expected, res);
        }
예제 #7
0
        public void TestStringSerializeResourceWithMapping()
        {
            PersonContact contact = new PersonContact(new Uri("http://example.com/ex"));

            contact.NameGiven = "Peter";
            var res      = SparqlSerializer.SerializeResource(contact);
            var expected = "<http://example.com/ex> <http://www.semanticdesktop.org/ontologies/2007/03/22/nco#nameGiven> 'Peter'; <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.semanticdesktop.org/ontologies/2007/03/22/nco#PersonContact>. ";

            Assert.AreEqual(expected, res);

            contact.Language = "DE";
            res = SparqlSerializer.SerializeResource(contact);
            Assert.AreEqual(expected, res);
        }
        public int Count()
        {
            string countQuery = SparqlSerializer.SerializeCount(_model, _query);

            SparqlQuery query = new SparqlQuery(countQuery);

            query.IsInferenceEnabled = _query.IsInferenceEnabled;

            string q = _store.CreateQuery(query);

            foreach (BindingSet b in GenerateBindings(_store.ExecuteQuery(q)))
            {
                return((int)b["count"]);
            }

            return(-1);
        }
예제 #9
0
        public void TestStringSerializeResource()
        {
            Resource r = new Resource("http://example.com/ex");

            r.AddProperty(Ontologies.dc.title, "MyResource");

            string res      = SparqlSerializer.SerializeResource(r);
            string expected = "<http://example.com/ex> <http://purl.org/dc/elements/1.1/title> 'MyResource'. ";

            Assert.AreEqual(expected, res);


            PersonContact contact = new PersonContact(new Uri("http://example.com/ex"));

            contact.NameGiven = "Peter";
            res      = SparqlSerializer.SerializeResource(contact);
            expected = "<http://example.com/ex> <http://www.semanticdesktop.org/ontologies/2007/03/22/nco#nameGiven> 'Peter'; <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.semanticdesktop.org/ontologies/2007/03/22/nco#PersonContact>. ";
            Assert.AreEqual(expected, res);

            contact.Language = "DE";
            res = SparqlSerializer.SerializeResource(contact);
            Assert.AreEqual(expected, res);
        }