Exemplo n.º 1
0
        public override void Run(SelectableSource source, QueryResultSink resultsink)
        {
            if (!(query is SelectQuery))
                throw new InvalidOperationException("Only SELECT queries are supported by this method (" + query.GetType() + ").");

            // Perform the query
            SelectQuery squery = (SelectQuery)query;
            RdfSourceWrapper sourcewrapper = BindLogic(source);

            RdfBindingSet results;
            try {
                results = squery.execute(sourcewrapper);
            } catch (name.levering.ryan.sparql.common.QueryException e) {
                throw new QueryExecutionException("Error executing query: " + e.Message, e);
            }

            // Prepare binding objects
            java.util.List vars = results.getVariables();
            SparqlVariable[] svars = new SparqlVariable[vars.size()];
            SemWebVariable[] vars2 = new SemWebVariable[vars.size()];
            for (int i = 0; i < svars.Length; i++) {
                svars[i] = (SparqlVariable)vars.get(i);
                vars2[i] = new SemWebVariable(svars[i].getName());
            }

            // Initialize the result sink
            resultsink.Init(vars2); // set distinct and ordered

            // Set the comments
            resultsink.AddComments(queryString + "\n");
            resultsink.AddComments(sourcewrapper.GetLog());

            // Iterate the bindings
            java.util.Iterator iter = results.iterator();
            long ctr = -1, ctr2 = 0;
            while (iter.hasNext()) {
                RdfBindingRow row = (RdfBindingRow)iter.next();

                // Since SPARQL processing may be lazy-delayed,
                // add any new comments that might have been logged.
                resultsink.AddComments(sourcewrapper.GetLog());

                ctr++;

                if (ctr < ReturnStart && ReturnStart != -1) continue;

                Resource[] bindings = new Resource[vars2.Length];

                for (int i = 0; i < bindings.Length; i++) {
                    Resource r = sourcewrapper.ToResource(row.getValue(svars[i]));
                    r = sourcewrapper.Persist(r);
                    bindings[i] = r;
                }

                resultsink.AddComments(sourcewrapper.GetLog());

                resultsink.Add(new VariableBindings(vars2, bindings));

                ctr2++;
                if (ctr2 >= ReturnLimit && ReturnLimit != -1) break;
            }

            resultsink.AddComments(sourcewrapper.GetLog());

            // Close the result sink.
            resultsink.Finished();
        }
Exemplo n.º 2
0
        public override void Run(SelectableSource source, QueryResultSink resultsink)
        {
            if (!(query is SelectQuery))
            {
                throw new InvalidOperationException("Only SELECT queries are supported by this method (" + query.GetType() + ").");
            }

            // Perform the query
            SelectQuery squery = (SelectQuery)query;

            RdfSourceWrapper sourcewrapper = new RdfSourceWrapper(source, QueryMeta, this);

            MyLogicFactory logic = new MyLogicFactory();

            foreach (RdfFunction f in extFunctions)
            {
                logic.registerExternalFunction(
                    new URIWrapper(f.Uri),
                    new ExtFuncWrapper(sourcewrapper, f));
            }

            squery.bindLogic(logic);

            RdfBindingSet results;

            try {
                results = squery.execute(sourcewrapper);
            } catch (java.lang.Exception e) {
                throw new QueryExecutionException("Error executing query: " + e.Message, e);
            }

            // Prepare binding objects
            java.util.List    vars     = results.getVariables();
            VariableBinding[] bindings = new VariableBinding[vars.size()];
            SparqlVariable[]  svars    = new SparqlVariable[vars.size()];
            SemWebVariable[]  vars2    = new SemWebVariable[vars.size()];
            for (int i = 0; i < bindings.Length; i++)
            {
                svars[i]    = (SparqlVariable)vars.get(i);
                vars2[i]    = new SemWebVariable(svars[i].getName());
                bindings[i] = new VariableBinding(vars2[i], null);
            }

            // Initialize the result sink
            resultsink.Init(bindings, false, false);             // set distinct and ordered

            // Set the comments
            resultsink.AddComments(queryString + "\n");
            resultsink.AddComments(sourcewrapper.GetLog());

            // Iterate the bindings
            java.util.Iterator iter = results.iterator();
            long ctr = -1, ctr2 = 0;

            while (iter.hasNext())
            {
                RdfBindingRow row = (RdfBindingRow)iter.next();

                // Since SPARQL processing may be lazy-delayed,
                // add any new comments that might have been logged.
                resultsink.AddComments(sourcewrapper.GetLog());

                ctr++;

                if (ctr < ReturnStart && ReturnStart != -1)
                {
                    continue;
                }

                for (int i = 0; i < bindings.Length; i++)
                {
                    Resource r = sourcewrapper.ToResource(row.getValue(svars[i]));
                    r           = sourcewrapper.Persist(r);
                    bindings[i] = new VariableBinding(bindings[i].Variable, r);
                }

                resultsink.AddComments(sourcewrapper.GetLog());

                resultsink.Add(bindings);

                ctr2++;
                if (ctr2 >= ReturnLimit && ReturnLimit != -1)
                {
                    break;
                }
            }

            resultsink.AddComments(sourcewrapper.GetLog());

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