コード例 #1
0
        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);
                        }
                    }
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// Retrieves a resource from the model.
        /// </summary>
        /// <param name="uri">A Uniform Resource Identifier.</param>
        /// <param name="transaction">Transaction associated with this action.</param>
        /// <returns>A resource with all asserted properties.</returns>
        public IResource GetResource(Uri uri, ITransaction transaction = null)
        {
            ISparqlQuery query = new SparqlQuery("SELECT DISTINCT ?s ?p ?o " + DatasetClause + " WHERE { ?s ?p ?o. FILTER (?s = @subject) }");

            query.Bind("@subject", uri);

            ISparqlQueryResult result = ExecuteQuery(query, transaction: transaction);

            IList resources = result.GetResources().ToList();

            if (resources.Count > 0)
            {
                Resource r = resources[0] as Resource;
                r.IsNew          = false;
                r.IsReadOnly     = true;
                r.IsSynchronized = true;
                r.SetModel(this);

                return((IResource)resources[0]);
            }
            else
            {
                throw new ResourceNotFoundException(uri);
            }
        }
コード例 #3
0
        /// <summary>
        /// Retrieves a resource from the model. Provides a resource object of the given type.
        /// </summary>
        /// <param name="uri">A Uniform Resource Identifier.</param>
        /// <param name="transaction">ransaction associated with this action.</param>
        /// <returns>A resource with all asserted properties.</returns>
        public T GetResource <T>(Uri uri, ITransaction transaction = null) where T : Resource
        {
            ISparqlQuery query = new SparqlQuery("SELECT ?s ?p ?o FROM @model WHERE { ?s ?p ?o. FILTER (?s = @subject) }");

            query.Bind("@model", this.Uri);
            query.Bind("@subject", uri);

            ISparqlQueryResult result = ExecuteQuery(query, transaction: transaction);

            IEnumerable <T> resources = result.GetResources <T>();

            if (resources.Any())
            {
                T r = resources.First();
                r.IsNew          = false;
                r.IsSynchronized = true;
                r.SetModel(this);

                return(r);
            }
            else
            {
                string msg = "Error: Could not find resource <{0}>.";

                throw new ArgumentException(string.Format(msg, uri));
            }
        }
コード例 #4
0
        public IEnumerable <T> GetResources <T>(int offset = -1, int limit = -1) where T : Resource
        {
            _query.Offset = offset;
            _query.Limit  = limit;

            if (_inferenceEnabled)
            {
                SparqlQuery uriQuery = new SparqlQuery(SparqlSerializer.Serialize(_model, _query, true));

                StringBuilder uris    = new StringBuilder();
                var           uriList = FetchUris(uriQuery).ToList();

                foreach (Uri u in uriList)
                {
                    if (u != null)
                    {
                        uris.Append(SparqlSerializer.SerializeUri(u));
                    }
                }

                if (!uriList.Any())
                {
                    return(new List <T>());
                }

                SparqlQuery        query = new SparqlQuery(string.Format("DESCRIBE {0} {1}", uris, SparqlSerializer.GenerateDatasetClause(_model)));
                ISparqlQueryResult res   = _model.ExecuteQuery(query);

                if (IsSorted)
                {
                    return(res.GetResources <T>().OrderBy(o => { return (o != null) ? uriList.IndexOf(o.Uri) : -1; }));
                }
                else
                {
                    return(res.GetResources <T>());
                }
            }
            else
            {
                SparqlQuery query = new SparqlQuery(SparqlSerializer.Serialize(_model, _query));

                return(_model.ExecuteQuery(query, _inferenceEnabled).GetResources <T>());
            }
        }
コード例 #5
0
        public void TestSelect()
        {
            // Retrieving bound variables using the SELECT query form.
            SparqlQuery        query  = new SparqlQuery("SELECT ?name ?birthday WHERE { ?x nco:fullname ?name. ?x nco:birthDate ?birthday. }");
            ISparqlQueryResult result = Model.ExecuteQuery(query);

            Assert.AreEqual(1, result.GetBindings().Count());

            // Retrieving resoures using the SELECT or DESCRIBE query form.
            query  = new SparqlQuery("SELECT ?s ?p ?o WHERE { ?s ?p ?o. ?s nco:fullname 'Hans Wurscht'. }");
            result = Model.ExecuteQuery(query);

            Assert.AreEqual(1, result.GetResources().Count());

            // Test SELECT with custom defined PREFIXes
            query  = new SparqlQuery("PREFIX nco: <http://www.semanticdesktop.org/ontologies/2007/03/22/nco#> SELECT ?s ?p ?o WHERE { ?s ?p ?o. ?s nco:fullname 'Hans Wurscht'. }");
            result = Model.ExecuteQuery(query);

            Assert.AreEqual(1, result.GetResources().Count());

            // Check if the select statement only works on the given model.
            query  = new SparqlQuery("SELECT * WHERE { ?s ?p ?o. }");
            result = Model.ExecuteQuery(query);

            Assert.AreEqual(5, result.GetResources().Count());

            // Check that resource creation is done correctly for Resources containing dashes.
            IResource r0 = Model.CreateResource(new Uri("http://example.org/Something#0"));

            r0.AddProperty(new Property(new Uri("http://example.org/fullName")), "Something");
            r0.Commit();

            IResource r1 = Model.CreateResource(new Uri("http://example.org/Something#1"));

            r1.AddProperty(new Property(new Uri("http://example.org/fullName")), "Anotherthing");
            r1.Commit();

            query  = new SparqlQuery("SELECT * WHERE { ?s ?p ?o. }");
            result = Model.ExecuteQuery(query);

            Assert.AreEqual(7, result.GetResources().Count());
        }
コード例 #6
0
        public void TestDescribe()
        {
            SparqlQuery        query  = new SparqlQuery("DESCRIBE <http://example.org/Hans>");
            ISparqlQueryResult result = Model.ExecuteQuery(query);

            IList resources = result.GetResources().ToList();

            Assert.AreEqual(1, resources.Count);

            query  = new SparqlQuery("DESCRIBE ?s WHERE { ?s nco:fullname 'Hans Wurscht'. }");
            result = Model.ExecuteQuery(query);

            resources = result.GetResources <PersonContact>().ToList();
            Assert.AreEqual(1, resources.Count);

            foreach (Contact c in resources)
            {
                Assert.AreEqual(c.GetType(), typeof(PersonContact));
            }
        }
コード例 #7
0
        public void TestConstruct()
        {
            SparqlQuery query = new SparqlQuery(@"
                CONSTRUCT
                {
                    ?x  vcard:N _:v .
                    _:v vcard:givenName ?name .
                }
                WHERE
                {
                    ?x nco:fullname ?name .
                }");

            ISparqlQueryResult result = Model.ExecuteQuery(query);

            IList resources = result.GetResources().ToList();

            Assert.AreEqual(2, resources.Count);
        }
コード例 #8
0
        public void TestConstruct()
        {
            Assert.Inconclusive("Blank nodes are currently problematic.");
            SparqlQuery query = new SparqlQuery(@"
                CONSTRUCT
                {
                    ?x  vcard:N _:v .
                    _:v vcard:givenName ?name .
                }
                WHERE
                {
                    ?x nco:fullname ?name .
                }");

            ISparqlQueryResult result = Model.ExecuteQuery(query);

            IList resources = result.GetResources().ToList();

            Assert.AreEqual(2, resources.Count);
        }
コード例 #9
0
ファイル: ModelGroup.cs プロジェクト: DarthStem/trinity-rdf
        public T GetResource <T>(Uri uri, ITransaction transaction = null) where T : Resource
        {
            SparqlQuery        query  = new SparqlQuery(String.Format("DESCRIBE {0} {1}", SparqlSerializer.SerializeUri(uri), DatasetClause));
            ISparqlQueryResult result = ExecuteQuery(query, transaction: transaction);

            IList resources = result.GetResources <T>().ToList();

            if (resources.Count > 0)
            {
                T res = resources[0] as T;
                res.IsNew          = false;
                res.IsSynchronized = true;
                res.IsReadOnly     = true;
                res.SetModel(this);
                return(res);
            }
            else
            {
                string msg = "Error: Could not find resource <{0}>.";
                throw new ArgumentException(string.Format(msg, uri));
            }
        }
コード例 #10
0
ファイル: Model.cs プロジェクト: joephayes/trinity-rdf
        /// <summary>
        /// Retrieves a resource from the model.
        /// </summary>
        /// <param name="uri">A Uniform Resource Identifier.</param>
        /// <param name="transaction">Transaction associated with this action.</param>
        /// <returns>A resource with all asserted properties.</returns>
        public IResource GetResource(Uri uri, ITransaction transaction = null)
        {
            ISparqlQuery query = new SparqlQuery("SELECT DISTINCT ?s ?p ?o FROM @model WHERE { ?s ?p ?o. FILTER (?s = @subject) }");

            query.Bind("@model", this.Uri);
            query.Bind("@subject", uri);

            ISparqlQueryResult result = ExecuteQuery(query, transaction: transaction);

            IEnumerable <Resource> resources = result.GetResources();

            foreach (Resource r in resources)
            {
                r.IsNew          = false;
                r.IsSynchronized = true;
                r.SetModel(this);

                return(r);
            }

            throw new ResourceNotFoundException(uri);
        }
コード例 #11
0
        public void TestSetLimit()
        {
            SparqlQuery query = new SparqlQuery(@"
                SELECT ?s0 ?p0 ?o0 WHERE
                {
                    ?s0 ?p0 ?o0 .
                    {
                        SELECT DISTINCT ?s0 WHERE
                        {
                            ?s ?p ?o.
                            ?s @type @class .

                            {
                                ?s ?p1 ?o1 .
                                FILTER ISLITERAL(?o1) . FILTER REGEX(STR(?o1), '', 'i') .
                            }
                            UNION
                            {
                                ?s ?p1 ?s1 .
                                ?s1 ?p2 ?o2 .
                                FILTER ISLITERAL(?o2) . FILTER REGEX(STR(?o2), '', 'i') .
                            }
                       }
                       ORDER BY ?o
                    }
                }");

            query.Bind("@type", rdf.type);
            query.Bind("@class", tmo.Task);

            MethodInfo method = query.GetType().GetMethod("SetLimit", BindingFlags.NonPublic | BindingFlags.Instance);

            method.Invoke(query, new object[] { 10 });

            ISparqlQueryResult result = Model.ExecuteQuery(query);

            List <Resource> resources = result.GetResources().ToList();
        }
コード例 #12
0
 /// <summary>
 /// Enumerator of the items. Should be narrowed with offset and limit.
 /// </summary>
 /// <param name="offset">Offset of the element where to start.</param>
 /// <param name="limit">Number of elements.</param>
 /// <returns></returns>
 public IEnumerable <T> GetItems(int offset, int limit)
 {
     return(_queryResult.GetResources <T>(offset, limit));
 }