Ejemplo n.º 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();
            }
        }
Ejemplo n.º 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");
            }
        }
Ejemplo n.º 3
0
        void RewriteGraph(Statement[] graph, SemWeb.Query.QueryOptions options, out Statement[] graph2, out SemWeb.Query.QueryOptions options2, SemWeb.Query.QueryResultSink sink)
        {
            graph2   = new Statement[graph.Length];
            options2 = new SemWeb.Query.QueryOptions();

            options2.DistinguishedVariables = options.DistinguishedVariables;
            options2.Limit = options.Limit;
            options2.VariableKnownValues    = (options.VariableKnownValues == null ? new VarKnownValuesType() : new VarKnownValuesType(options.VariableKnownValues));
            options2.VariableLiteralFilters = options.VariableLiteralFilters;

            for (int i = 0; i < graph.Length; i++)
            {
                graph2[i] = graph[i];

                //ResSet subj = GetQueryRes(graph[i], 0, options);
                ResSet pred = GetQueryRes(graph[i], 1, options);
                ResSet obj  = GetQueryRes(graph[i], 2, options);

                if (pred.Count == 1 && pred.Contains(type))
                {
                    // in an ?x rdf:type ___ query, replace ___ with the subclass closure of ___.
                    if (obj.Count > 0)
                    {
                        Entity[] sc = GetClosure(obj, subclasses, true);
                        if (sc.Length != obj.Count && sink != null)
                        {
                            sink.AddComments("Expanding object of " + graph[i] + " with subclass closure to [" + ToString(sc) + "]");
                        }
                        SetQueryRes(ref graph2[i], 2, options2, sc);
                    }
                }

                // expand properties into subproperties after the above tests,
                // because we want to be sure the property was originally
                // just one of the recognized properties

                if (pred.Count > 0)
                {
                    Entity[] pc = GetClosure(pred, subprops, true);
                    SetQueryRes(ref graph2[i], 1, options2, pc);
                    if (pc.Length != pred.Count && sink != null)
                    {
                        sink.AddComments("Expanding predicate of " + graph[i] + " with subproperty closure to [" + ToString(pc) + "]");
                    }
                }
            }
        }
Ejemplo n.º 4
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();
        }
Ejemplo n.º 5
0
        public override void Query(Statement[] graph, SemWeb.Query.QueryOptions options, SelectableSource data, SemWeb.Query.QueryResultSink sink)
        {
            Statement[] graph2;
            SemWeb.Query.QueryOptions options2;
            RewriteGraph(graph, options, out graph2, out options2, sink);

            // TODO: Because we add variables to the query when we replace things with closures,
            // we should filter the query results so we don't pass back the bindings for those
            // variables to the caller.

            if (!(data is QueryableSource))
            {
                new SimpleEntailment().Query(graph2, options2, data, sink);
            }
            else
            {
                ((QueryableSource)data).Query(graph2, options2, sink);
            }
        }