Example #1
0
        /// <summary>
        /// Executes a Graph Pattern style query against the Source
        /// </summary>
        /// <param name="graph">Graph Pattern</param>
        /// <param name="options">Query Options</param>
        /// <param name="sink">Results Sink</param>
        /// <remarks>
        /// <para>
        /// This is implemented by transforming the Graph Pattern which is a set of SemWeb Statement templates into a SPARQL Algebra BGP.  The resulting algebra is then executed using the Leviathan engine and the results converted into VariableBindings for SemWeb
        /// </para>
        /// <para>
        /// The only Query Option that is supported is the Limit option
        /// </para>
        /// </remarks>
        public void Query(Statement[] graph, SW.Query.QueryOptions options, SW.Query.QueryResultSink sink)
        {
            ISparqlAlgebra algebra = this.ToAlgebra(graph);

            if (this._store == null)
            {
                this._store = new TripleStore();
                this._store.Add(this._g);
            }

            SparqlEvaluationContext context = new SparqlEvaluationContext(null, new InMemoryDataset(this._store));
            BaseMultiset            results = context.Evaluate(algebra);//algebra.Evaluate(context);

            sink.Init(results.Variables.Select(v => new Variable(v)).ToArray());
            if (results.Count > 0)
            {
                int c = 0;
                foreach (ISet s in results.Sets)
                {
                    //Apply Limit if applicable
                    if (options.Limit > 0 && c >= options.Limit)
                    {
                        sink.Finished();
                        return;
                    }

                    //Convert the Set to VariableBindings for SemWeb
                    Variable[] vars      = s.Variables.Select(v => new Variable(v)).ToArray();
                    Resource[] resources = s.Variables.Select(v => SemWebConverter.ToSemWeb(s[v], this._mapping)).ToArray();
                    SW.Query.VariableBindings bindings = new SW.Query.VariableBindings(vars, resources);

                    //Keep adding results until the sink tells us to stop
                    if (!sink.Add(bindings))
                    {
                        sink.Finished();
                        return;
                    }
                    c++;
                }
                sink.Finished();
            }
            else
            {
                sink.Finished();
            }
        }
Example #2
0
        /// <summary>
        /// Queries the Store using the Graph Pattern specified by the set of Statement Patterns
        /// </summary>
        /// <param name="graph">Graph Pattern</param>
        /// <param name="options">Query Options</param>
        /// <param name="sink">Results Sink</param>
        /// <remarks>
        /// <para>
        /// Implemented by converting the Statement Patterns into a SPARQL SELECT query and executing that against the underlying Store's SPARQL engine
        /// </para>
        /// <para>
        /// The only Query Option that is supported is the Limit option
        /// </para>
        /// </remarks>
        public void Query(Statement[] graph, SW.Query.QueryOptions options, SW.Query.QueryResultSink sink)
        {
            //Implement as a SPARQL SELECT
            SparqlParameterizedString queryString = new SparqlParameterizedString();

            queryString.CommandText = "SELECT * WHERE {";

            int p = 0;

            foreach (Statement stmt in graph)
            {
                //Add Subject
                queryString.CommandText += "\n";
                if (stmt.Subject is Variable)
                {
                    queryString.CommandText += stmt.Subject.ToString();
                }
                else
                {
                    queryString.CommandText += "@param" + p;
                    queryString.SetParameter("param" + p, SemWebConverter.FromSemWeb(stmt.Subject, this._mapping));
                    p++;
                }
                queryString.CommandText += " ";

                //Add Predicate
                if (stmt.Predicate is Variable)
                {
                    queryString.CommandText += stmt.Predicate.ToString();
                }
                else
                {
                    queryString.CommandText += "@param" + p;
                    queryString.SetParameter("param" + p, SemWebConverter.FromSemWeb(stmt.Predicate, this._mapping));
                    p++;
                }
                queryString.CommandText += " ";

                //Add Object
                if (stmt.Object is Variable)
                {
                    queryString.CommandText += stmt.Object.ToString();
                }
                else
                {
                    queryString.CommandText += "@param" + p;
                    queryString.SetParameter("param" + p, SemWebConverter.FromSemWeb(stmt.Object, this._mapping));
                    p++;
                }
                queryString.CommandText += " .";
            }

            queryString.CommandText += "}";

            //Execute the Query and convert the Results
            Object results = this._store.ExecuteQuery(queryString.ToString());

            if (results is SparqlResultSet)
            {
                SparqlResultSet rset = (SparqlResultSet)results;
                sink.Init(rset.Variables.Select(v => new Variable(v)).ToArray());
                if (rset.Count > 0)
                {
                    int c = 0;
                    foreach (SparqlResult r in rset)
                    {
                        //Apply Limit if applicable
                        if (options.Limit > 0 && c >= options.Limit)
                        {
                            sink.Finished();
                            return;
                        }

                        //Convert the Set to VariableBindings for SemWeb
                        Variable[] vars      = r.Variables.Select(v => new Variable(v)).ToArray();
                        Resource[] resources = r.Variables.Select(v => SemWebConverter.ToSemWeb(r[v], this._mapping)).ToArray();
                        SW.Query.VariableBindings bindings = new SW.Query.VariableBindings(vars, resources);

                        //Keep adding results until the sink tells us to stop
                        if (!sink.Add(bindings))
                        {
                            sink.Finished();
                            return;
                        }
                        c++;
                    }
                    sink.Finished();
                }
                else
                {
                    sink.Finished();
                }
            }
            else
            {
                throw new RdfQueryException("Query returned an unexpected result where a SPARQL Result Set was expected");
            }
        }
Example #3
0
        public override void Query(Statement[] graph, SemWeb.Query.QueryOptions options, SelectableSource targetModel, SemWeb.Query.QueryResultSink sink)
        {
            QueryCheckArg(graph);

            // Try to do the inferencing.
            ArrayList evidence = prove(rules, targetModel, graph, -1);

            if (evidence == null)
            {
                return;                 // not provable (in max number of steps, if that were given)
            }
            // Then send the possible bindings to the QueryResultSink.

            // Map variables to indexes.
            Hashtable vars = new Hashtable();

            foreach (Statement s in graph)
            {
                if (s.Subject is Variable && !vars.ContainsKey(s.Subject))
                {
                    vars[s.Subject] = vars.Count;
                }
                if (s.Predicate is Variable && !vars.ContainsKey(s.Predicate))
                {
                    vars[s.Predicate] = vars.Count;
                }
                if (s.Object is Variable && !vars.ContainsKey(s.Object))
                {
                    vars[s.Object] = vars.Count;
                }
            }

            // Prepare the bindings array.
            Variable[] varOrder = new Variable[vars.Count];
            foreach (Variable v in vars.Keys)
            {
                varOrder[(int)vars[v]] = v;
            }

            // Initialize the sink.
            sink.Init(varOrder);

            // Send a binding set for each piece of evidence.
            foreach (EvidenceItem ei in evidence)
            {
                // Write a comment to the results with the actual proof. (nifty actually)
                sink.AddComments(ei.ToProof().ToString());

                // Create the binding array and send it on
                Resource[] variableBindings = new Resource[varOrder.Length];
                foreach (Variable v in vars.Keys)
                {
                    if (ei.env.ContainsKey(v))
                    {
                        variableBindings[(int)vars[v]] = (Resource)ei.env[v];
                    }
                }
                sink.Add(new SemWeb.Query.VariableBindings(varOrder, variableBindings));
            }

            // Close the sink.
            sink.Finished();
        }