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();
        }
        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();
        }
		public override void Run(SelectableSource targetModel, QueryResultSink result) {
			CheckInit();
			
			foreach (Statement s in novariablestatements)
				if (!targetModel.Contains(s))
					return;
			
			VariableBinding[] finalbindings = new VariableBinding[variables.Length];
			for (int i = 0; i < variables.Length; i++)
				finalbindings[i].Variable = variableEntities[i];
			
			result.Init(finalbindings, true, false);
			
			Debug("Begnning Query");
			
			BindingSet bindings = new BindingSet(this);
			for (int group = 0; group < statements.Length; group++) {
				bool ret = Query(group, bindings, targetModel);
				if (!ret) {
					// A false return value indicates the query
					// certainly failed -- a non-optional statement
					// failed to match at all.
					result.Finished();
					return;
				}
			}

			int ctr = -1;
			foreach (QueryResult r in bindings.Results) {
				Permutation permutation = new Permutation(r.Bindings);
				do {
					ctr++;
					if (ctr < ReturnStart) continue;
					for (int i = 0; i < variables.Length; i++)
						finalbindings[i].Target = permutation[i];
					result.Add(finalbindings);
					if (ReturnLimit != -1 && ctr == ReturnStart+ReturnLimit) break;	
				} while (permutation.Next());
				if (ReturnLimit != -1 && ctr == ReturnStart+ReturnLimit) break;	
			}

			
			result.Finished();
		}
		public void Query(Statement[] graph, QueryOptions options, QueryResultSink sink) {
			if (options.DistinguishedVariables != null && options.DistinguishedVariables.Count == 0)
				throw new ArgumentException("options.DistinguishedVariables cannot be an empty list.");
		
			StringBuilder query = new StringBuilder();
			
			query.Append("SELECT ");

			// Get a list of variables and map them to fresh names
			#if !DOTNET2
			Hashtable variableNames = new Hashtable();
			#else
			Dictionary<Variable,string> variableNames = new Dictionary<Variable,string>();
			#endif
			Hashtable variableNames2 = new Hashtable();
			foreach (Statement s in graph) {
				for (int j = 0; j < 3; j++) {
					Variable v = s.GetComponent(j) as Variable;
					if (v == null) continue;
					if (variableNames.ContainsKey(v)) continue;
					variableNames2["v" + variableNames.Count] = v;
					variableNames[v] = "?v" + variableNames.Count;
				}
			}
			
			// What variables will we select on?
			ArrayList selectedVars = new ArrayList();
			foreach (Variable v in
				options.DistinguishedVariables != null
					? options.DistinguishedVariables
					: variableNames.Keys) {
				if (!variableNames.ContainsKey(v)) continue; // in case distinguished variables list
															 // has more than what actually appears in query
				if (selectedVars.Contains(v)) continue; // don't select more than once
				query.Append(variableNames[v]);
				query.Append(' ');
				selectedVars.Add(v);
			}
			
			if (selectedVars.Count == 0) {
				if (options.DistinguishedVariables == null)
					throw new ArgumentException("There were no variables in the query.");
				else
					throw new ArgumentException("None of the variables in the query were distinguished.");
			}
			
			// Bnodes are not allowed here -- we can't query on them.
			foreach (Statement s in graph) {
				for (int j = 0; j < 3; j++) {
					if (s.GetComponent(j) is BNode && !(s.GetComponent(j) is Variable)) {
						Variable[] varArray = (Variable[])selectedVars.ToArray(typeof(Variable));
						sink.Init(varArray);
						sink.Finished();
						return;
					}
				}
			}
			
			// Build the graph pattern.
			query.Append("WHERE {\n");
			
			ResSet firstVarUse = new ResSet();
			foreach (Statement s in graph) {
				for (int j = 0; j < 3; j++) {
					Resource r = s.GetComponent(j);
					query.Append(S(r, r is Variable && variableNames.ContainsKey((Variable)r) ? (string)variableNames[(Variable)r] : null));
					query.Append(" ");
				}
				query.Append(" . \n");
				if (options.VariableKnownValues != null) {
					for (int j = 0; j < 3; j++) {
						Resource r = s.GetComponent(j);
						if (firstVarUse.Contains(r)) continue;
						firstVarUse.Add(r);
						if (r is Variable && variableNames.ContainsKey((Variable)r) &&
						#if !DOTNET2
						options.VariableKnownValues.Contains(r)
						#else
						options.VariableKnownValues.ContainsKey((Variable)r)
						#endif
						)
							query.Append(SL(options.VariableKnownValues[(Variable)r], (string)variableNames[(Variable)r], true));
					}
				}
				// And what about meta...?
			}
			
			query.Append("}");
			
			if (options.Limit > 0) {
				query.Append(" LIMIT ");
				query.Append(options.Limit);
			}
			
			Load(query.ToString(), new QueryResultsWrapper(sink, variableNames2));
		}
        public override void Run(SelectableSource targetModel, QueryResultSink result)
        {
            CheckInit();

            foreach (Statement s in novariablestatements)
            {
                if (!targetModel.Contains(s))
                {
                    return;
                }
            }

            VariableBinding[] finalbindings = new VariableBinding[variables.Length];
            for (int i = 0; i < variables.Length; i++)
            {
                finalbindings[i].Variable = variableEntities[i];
            }

            result.Init(finalbindings, true, false);

            Debug("Begnning Query");

            BindingSet bindings = new BindingSet(this);

            for (int group = 0; group < statements.Length; group++)
            {
                bool ret = Query(group, bindings, targetModel);
                if (!ret)
                {
                    // A false return value indicates the query
                    // certainly failed -- a non-optional statement
                    // failed to match at all.
                    result.Finished();
                    return;
                }
            }

            int ctr = -1;

            foreach (QueryResult r in bindings.Results)
            {
                Permutation permutation = new Permutation(r.Bindings);
                do
                {
                    ctr++;
                    if (ctr < ReturnStart)
                    {
                        continue;
                    }
                    for (int i = 0; i < variables.Length; i++)
                    {
                        finalbindings[i].Target = permutation[i];
                    }
                    result.Add(finalbindings);
                    if (ReturnLimit != -1 && ctr == ReturnStart + ReturnLimit)
                    {
                        break;
                    }
                } while (permutation.Next());
                if (ReturnLimit != -1 && ctr == ReturnStart + ReturnLimit)
                {
                    break;
                }
            }


            result.Finished();
        }