Example #1
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) + "]");
                    }
                }
            }
        }
Example #2
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();
        }