Exemple #1
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));
            }
        }
Exemple #2
0
        /// <summary>
        /// Indicates wheter a given resource is part of the model.
        /// </summary>
        /// <param name="uri">A Uniform Resource Identifier.</param>
        /// <param name="transaction">Transaction associated with this action.</param>
        /// <returns>True if the resource is part of the model, False if not.</returns>
        public bool ContainsResource(Uri uri, ITransaction transaction = null)
        {
            ISparqlQuery query = new SparqlQuery("ASK FROM @graph { @subject ?p ?o . }");

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

            return(ExecuteQuery(query, transaction: transaction).GetAnwser());
        }
Exemple #3
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);
            }
        }
Exemple #4
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 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);
        }