示例#1
0
        public IEnumerable <T> GetResources <T>(ResourceQuery query, bool inferenceEnabled = false, ITransaction transaction = null) where T : Resource
        {
            IEnumerable <T> result = ExecuteQuery(query, inferenceEnabled, transaction).GetResources <T>();

            // TODO: Could be done in the SparqlQueryResult for increased performance.
            if (result != null)
            {
                foreach (object r in result)
                {
                    T t = r as T;

                    // NOTE: This safeguard is required because of a bug in ExecuteQuery where
                    // it returns null objects when a rdf:type triple is missing..
                    if (t == null)
                    {
                        continue;
                    }

                    t.SetModel(this);
                    t.IsNew          = false;
                    t.IsSynchronized = true;
                    t.IsReadOnly     = true;

                    yield return(t);
                }
            }
        }
 public ResourceQueryResult(IModel model, ResourceQuery query, bool inferenceEnabled, ITransaction transaction)
 {
     _model            = model;
     _query            = query;
     _inferenceEnabled = inferenceEnabled;
     _transaction      = transaction;
 }
示例#3
0
        /// <summary>
        /// Returns a enumeration of all resources that match the given type.
        /// </summary>
        /// <param name="inferenceEnabled">Indicate that this query should work with enabled inferencing.</param>
        /// <param name="transaction">transaction associated with this action.</param>
        /// <returns>An enumeration of resources that match the given query.</returns>
        public IEnumerable <T> GetResources <T>(bool inferenceEnabled = false, ITransaction transaction = null) where T : Resource
        {
            T             temp  = (T)Activator.CreateInstance(typeof(T), new Uri("semio:desk"));
            ResourceQuery query = new ResourceQuery(temp.GetTypes());

            query.InferencingEnabled = inferenceEnabled;

            return(GetResources <T>(query, inferenceEnabled, transaction));
        }
示例#4
0
        /// <summary>
        /// Serialize a ResourceQuery from certain model. Can also only deliver the Uris of the result resources.
        /// </summary>
        /// <param name="model"></param>
        /// <param name="query"></param>
        /// <param name="onlyUris"></param>
        /// <returns></returns>
        public static string Serialize(IModel model, ResourceQuery query, bool onlyUris = false)
        {
            StringBuilder            result         = new StringBuilder();
            StringBuilder            whereBlock     = new StringBuilder();
            StringBuilder            modifierBlock  = new StringBuilder();
            SortedList <int, string> orderModifiers = new SortedList <int, string>();

            Serialize(model, query, whereBlock, orderModifiers, new List <ResourceQuery>());

            if (orderModifiers.Count > 0)
            {
                modifierBlock.Append("ORDER BY ");

                foreach (string value in orderModifiers.Values)
                {
                    modifierBlock.Append(value);
                }
            }

            if (query.Offset != -1)
            {
                modifierBlock.Append(" OFFSET ");
                modifierBlock.Append(query.Offset);
            }

            if (query.Limit != -1)
            {
                modifierBlock.Append(" LIMIT ");
                modifierBlock.Append(query.Limit);
            }
            if (onlyUris)
            {
                string q = "SELECT ?s0 {0} WHERE {{ ?s0 ?p0 ?o0 . {{ SELECT DISTINCT ?s0 WHERE {{{1}}} {2} }}}}";
                result.Append(string.Format(q, GenerateDatasetClause(model), whereBlock.ToString(), modifierBlock.ToString()));
            }
            else
            {
                if (whereBlock.Length > 0)
                {
                    // Because of a bug in OpenLink Virtuoso where it ignores ORDER BY expressions in
                    // DESCRIBE queries we have to emulate the describe query by a nested SELECT query.
                    string q = "SELECT ?s0 ?p0 ?o0 {0} WHERE {{ ?s0 ?p0 ?o0 . {{ SELECT DISTINCT ?s0 WHERE {{{1}}} {2} }}}}";
                    result.Append(string.Format(q, GenerateDatasetClause(model), whereBlock.ToString(), modifierBlock.ToString()));
                }
                else
                {
                    string q = "DESCRIBE ?s0 {0} WHERE {{ ?s0 ?p0 ?o0 . }} {1}";
                    result.Append(string.Format(q, GenerateDatasetClause(model), modifierBlock.ToString()));
                }
            }

            return(result.ToString());
        }
示例#5
0
        /// <summary>
        /// Executes a resource query and provides an enumeration of matching resources.
        /// </summary>
        /// <param name="query">A ResourceQuery object containing the query that should be executed.</param>
        /// <param name="inferenceEnabled">Indicate that this query should work with enabled inferencing.</param>
        /// <param name="transaction">transaction associated with this action.</param>
        /// <returns>An enumeration of resources that match the given query.</returns>
        public IEnumerable <Resource> GetResources(ResourceQuery query, bool inferenceEnabled = false, ITransaction transaction = null)
        {
            IEnumerable <Resource> result = ExecuteQuery(query, inferenceEnabled, transaction).GetResources <Resource>();

            if (result != null)
            {
                // TODO: Should be done in the SparqlQueryResult for increased performance.
                foreach (Resource r in result)
                {
                    r.SetModel(this);
                    r.IsNew          = false;
                    r.IsSynchronized = true;
                }
            }

            return(result);
        }
示例#6
0
        private bool Clone(out ResourceQuery result, IList <ResourceQuery> processed, IList <ResourceQuery> generated)
        {
            if (!processed.Contains(this))
            {
                result        = new ResourceQuery();
                result.Offset = Offset;
                result.Limit  = Limit;

                processed.Add(this);
                generated.Add(result);

                foreach (ResourceQuery query in DependentQueries)
                {
                    ResourceQuery q;

                    if (query.Clone(out q, processed, generated))
                    {
                        q.DependentQueries.Add(result);
                        result.DependentQueries.Add(q);
                    }
                }

                foreach (StatementPattern pattern in WhereStatements)
                {
                    if (pattern.Object is ResourceQuery && processed.Contains(pattern.Object as ResourceQuery))
                    {
                        result.WhereStatements.Add(new StatementPattern(pattern.Subject, pattern.Predicate, generated[processed.IndexOf(pattern.Object as ResourceQuery)]));
                    }
                    else
                    {
                        result.WhereStatements.Add(pattern);
                    }
                }

                return(true);
            }
            else
            {
                result = null;

                return(false);
            }
        }
示例#7
0
        /// <summary>
        /// Serialize a count query for the given ResourceQuery
        /// </summary>
        /// <param name="model"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public static string SerializeCount(IModel model, ResourceQuery query)
        {
            StringBuilder result = new StringBuilder();

            StringBuilder where = new StringBuilder();

            Serialize(model, query, where, new SortedList <int, string>(), new List <ResourceQuery>());

            if (query.Uri == null)
            {
                result.Append(string.Format("SELECT (COUNT(DISTINCT ?s0) AS ?count) {0} WHERE {{{1}}}", GenerateDatasetClause(model), where.ToString()));
            }
            else
            {
                result.Append(string.Format("SELECT (COUNT(DISTINCT <{2}>) AS ?count) {0} WHERE {{{1}}}", GenerateDatasetClause(model), where.ToString(), query.Uri));
            }

            return(result.ToString());
        }
示例#8
0
        public StatementPattern Where(Property property, object value = null)
        {
            if (value is ResourceQuery)
            {
                ResourceQuery query = value as ResourceQuery;
                query.DependentQueries.Add(this);
                this.DependentQueries.Add(query);
            }

            StatementPattern pattern;

            if (property != null)
            {
                if (value != null)
                {
                    pattern = new StatementPattern(this.Uri, property.Uri, value);
                }
                else
                {
                    pattern = new StatementPattern(this.Uri, property.Uri, null);
                }
            }
            else
            {
                if (value != null)
                {
                    pattern = new StatementPattern(this.Uri, null, value);
                }
                else
                {
                    throw new ArgumentException(string.Format("Error: Invalid arguments {0} {1}.", property, value));
                }
            }

            WhereStatements.Add(pattern);

            return(pattern);
        }
示例#9
0
 public IResourceQueryResult ExecuteQuery(ResourceQuery query, bool inferenceEnabled = false, ITransaction transaction = null)
 {
     return(new ResourceQueryResult(this, query, inferenceEnabled, transaction));
 }
示例#10
0
        /// <summary>
        /// Serialize a query from the given parameters
        /// </summary>
        /// <param name="model"></param>
        /// <param name="query"></param>
        /// <param name="whereBlock"></param>
        /// <param name="solutionModifiers"></param>
        /// <param name="processed"></param>
        public static void Serialize(IModel model, ResourceQuery query, StringBuilder whereBlock, SortedList <int, string> solutionModifiers, IList <ResourceQuery> processed)
        {
            // The index of the currently processed resource query used for generating variable names.
            int i = processed.Count;

            // Prevent endless loops for cyclic references.
            processed.Add(query);

            // Recursively serialize all the queries which reference to this query.
            foreach (ResourceQuery dependentQuery in query.DependentQueries)
            {
                if (!processed.Contains(dependentQuery))
                {
                    Serialize(model, dependentQuery, whereBlock, solutionModifiers, processed);

                    whereBlock.Append(" ");
                }
            }

            if (query.WhereStatements.Count > 0)
            {
                // Counts the variables used for this resource.
                string s      = "?s" + i;
                string p      = "?p" + i;
                string o      = "?o" + i;
                int    pCount = 0;
                int    oCount = 0;

                // We iterate over the asserted properties and generate SPARQL statements.
                foreach (StatementPattern pattern in query.WhereStatements)
                {
                    if (pattern.Object is ResourceQuery && !processed.Contains(pattern.Object as ResourceQuery))
                    {
                        continue;
                    }

                    if (pattern.Subject == null)
                    {
                        pattern.SubjectName = s;

                        whereBlock.Append(pattern.SubjectName + " ");
                    }
                    else
                    {
                        whereBlock.Append(SerializeUri(pattern.Subject) + " ");
                    }

                    // PREDICATE
                    if (pattern.Predicate is Uri)
                    {
                        // If there is a property we write the absolute URI..
                        whereBlock.Append(SerializeUri(pattern.Predicate) + " ");
                    }
                    else
                    {
                        pattern.PredicateName = p + pCount++;

                        whereBlock.Append(pattern.PredicateName + " ");
                    }

                    // OBJECT
                    if (pattern.Object is ResourceQuery)
                    {
                        ResourceQuery q = pattern.Object as ResourceQuery;

                        if (q.Uri != null)
                        {
                            // If the value is a reference to another query we write its variable name and close the statement.
                            whereBlock.Append(SerializeUri(q.Uri) + " . ");
                        }
                        else
                        {
                            // If the value is a reference to another query we write its variable name and close the statement.
                            whereBlock.Append("?s" + processed.IndexOf(q) + " . ");
                        }
                    }
                    else if (pattern.FilterOperation != FilterOperation.None)
                    {
                        pattern.ObjectName = o + oCount++;

                        switch (pattern.FilterOperation)
                        {
                        case FilterOperation.None:
                            break;

                        case FilterOperation.Equal:
                            whereBlock.Append(string.Format("{0} . FILTER({0} == {1}) . ", pattern.ObjectName, SerializeValue(pattern.Object)));
                            break;

                        case FilterOperation.NotEqual:
                            whereBlock.Append(string.Format("{0} . FILTER({0} != {1}) . ", pattern.ObjectName, SerializeValue(pattern.Object)));
                            break;

                        case FilterOperation.LessThan:
                            whereBlock.Append(string.Format("{0} . FILTER({0} < {1}) . ", pattern.ObjectName, SerializeValue(pattern.Object)));
                            break;

                        case FilterOperation.LessOrEqual:
                            whereBlock.Append(string.Format("{0} . FILTER({0} <= {1}) . ", pattern.ObjectName, SerializeValue(pattern.Object)));
                            break;

                        case FilterOperation.GreaterThan:
                            whereBlock.Append(string.Format("{0} . FILTER({0} > {1}) . ", pattern.ObjectName, SerializeValue(pattern.Object)));
                            break;

                        case FilterOperation.GreaterOrEqual:
                            whereBlock.Append(string.Format("{0} . FILTER({0} >= {1}) . ", pattern.ObjectName, SerializeValue(pattern.Object)));
                            break;

                        case FilterOperation.Contains:
                            whereBlock.Append(string.Format("{0} . FILTER ISLITERAL({0}) . FILTER REGEX(STR({0}), \"{1}\", \"i\") . ", pattern.ObjectName, pattern.Object));
                            break;
                        }
                    }
                    else if (pattern.Object != null)
                    {
                        // If a literal value was specified we append it and close the statement.
                        whereBlock.Append(SerializeValue(pattern.Object) + " . ");
                    }
                    else
                    {
                        // Save the current variable for later processing the sort descriptions..
                        pattern.ObjectName = o + oCount++;

                        // If the value is null we append a variable to the statement.
                        whereBlock.Append(pattern.ObjectName + " . ");
                    }

                    if (!string.IsNullOrEmpty(pattern.ObjectName))
                    {
                        if (pattern.SortDirection == SortDirection.Ascending)
                        {
                            solutionModifiers.Add(pattern.SortPriority, " ASC(" + pattern.ObjectName + ")");
                        }
                        else if (pattern.SortDirection == SortDirection.Descending)
                        {
                            solutionModifiers.Add(pattern.SortPriority, " DESC(" + pattern.ObjectName + ")");
                        }
                    }
                }
            }
            else if (query.Uri != null)
            {
                whereBlock.Append(SerializeUri(query.Uri) + " ?p" + i + " ?o" + i + " . ");
            }
            else
            {
                whereBlock.Append("?s" + i + " ?p" + i + " ?o" + i + " . ");
            }
        }
示例#11
0
 public ResourceProvider(string name, ResourceQuery query)
 {
 }
示例#12
0
 public ResourceQueryItemsProvider(IModel model, ResourceQuery query, bool inferenceEnabled = true)
 {
     _queryResult = model.ExecuteQuery(query, inferenceEnabled);
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="VirtualizingResourceCollection{T}"/> class.
 /// </summary>
 public VirtualizingResourceCollection(IModel model, ResourceQuery query, bool inferenceEnabled = false)
     : base(new ResourceQueryItemsProvider <T>(model, query, inferenceEnabled))
 {
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="VirtualizingResourceCollection{T}"/> class.
 /// </summary>
 public VirtualizingResourceCollection(IModel model, ResourceQuery query, int pageSize, bool inferenceEnabled = true)
     : base(new ResourceQueryItemsProvider <T>(model, query, inferenceEnabled), pageSize)
 {
 }