protected override RdfBindingSet runTripleConstraints(java.util.List tripleConstraints, name.levering.ryan.sparql.model.logic.ConstraintLogic.CallParams p) { if (DisableQuery) return null; RdfSourceWrapper s = (RdfSourceWrapper)p.source; if (s.source is QueryableSource) { QueryableSource qs = (QueryableSource)s.source; QueryOptions opts = new QueryOptions(); opts.Limit = p.limit; VariableList distinguishedVars = new VariableList(); VariableList undistinguishedVars = new VariableList(); opts.VariableKnownValues = new VarKnownValuesType(); Statement[] graph = new Statement[tripleConstraints.size()]; Hashtable varMap1 = new Hashtable(); Hashtable varMap2 = new Hashtable(); Entity metaField; // In this case, we want to treat the meta fields of all of the statements // in this group as bound by a single variable. if (p.graphVariable != null) { metaField = ToRes(p.graphVariable, p.knownValues, true, varMap1, varMap2, s, opts, distinguishedVars, undistinguishedVars, p.distinguishedVariables) as Entity; // Otherwise, we are told what graph to use. If sourceDatasets is null, // we are looking in the default graph. } else if (p.sourceDatasets == null) { /*if (p.defaultDatasets.size() == 0) { metaField = Statement.DefaultMeta; } else if (p.defaultDatasets.size() == 1) { metaField = s.ToEntity((Value)p.defaultDatasets.iterator().next()); } else { metaField = new SemWebVariable(); opts.VariableKnownValues[(Variable)metaField] = s.ToEntities((Value[])p.defaultDatasets.toArray(new Value[0])); }*/ // For the default Graph, we always pass DefaultMeta. metaField = Statement.DefaultMeta; // Otherwise, we are looking in the indicated graphs. } else { if (p.sourceDatasets.size() == 0) { metaField = new SemWebVariable(); } else if (p.sourceDatasets.size() == 1) { metaField = s.ToEntity((Value)p.sourceDatasets.iterator().next()); } else { metaField = new SemWebVariable(); opts.VariableKnownValues[(Variable)metaField] = s.ToEntities((Value[])p.sourceDatasets.toArray(new Value[0])); } } for (int i = 0; i < tripleConstraints.size(); i++) { TripleConstraintData triple = tripleConstraints.get(i) as TripleConstraintData; if (triple == null) return null; graph[i] = new Statement(null, null, null, null); // I don't understand why this should be necessary for a struct, but I get a null reference exception otherwise (yet, that didn't happen initially) graph[i].Subject = ToRes(triple.getSubjectExpression(), p.knownValues, true, varMap1, varMap2, s, opts, distinguishedVars, undistinguishedVars, p.distinguishedVariables) as Entity; graph[i].Predicate = ToRes(triple.getPredicateExpression(), p.knownValues, true, varMap1, varMap2, s, opts, distinguishedVars, undistinguishedVars, p.distinguishedVariables) as Entity; graph[i].Object = ToRes(triple.getObjectExpression(), p.knownValues, false, varMap1, varMap2, s, opts, distinguishedVars, undistinguishedVars, p.distinguishedVariables); graph[i].Meta = metaField; if (graph[i].AnyNull) return new RdfBindingSetImpl(); if (!(graph[i].Subject is Variable) && !(graph[i].Predicate is Variable) && !(graph[i].Object is Variable) && !(graph[i].Meta is Variable)) return null; // we could use Contains(), but we'll just abandon the Query() path altogether } if (p.distinguishedVariables == null) { opts.DistinguishedVariables = null; } else if (distinguishedVars.Count > 0) { opts.DistinguishedVariables = distinguishedVars; } else if (undistinguishedVars.Count > 0) { // we don't mean to make it distinguished, but we need at least one, // and for now we'll just take the first opts.DistinguishedVariables = new VariableList(); ((VariableList)opts.DistinguishedVariables).Add(undistinguishedVars[0]); } else { // no variables! return null; } opts.VariableLiteralFilters = new LitFilterMap(); foreach (DictionaryEntry kv in varMap1) { if (p.knownFilters != null && p.knownFilters.containsKey(kv.Key)) { LitFilterList filters = new LitFilterList(); for (java.util.Iterator iter = ((java.util.List)p.knownFilters.get(kv.Key)).iterator(); iter.hasNext(); ) filters.Add((LiteralFilter)iter.next()); opts.VariableLiteralFilters[(Variable)kv.Value] = filters; } } // too expensive to do... //if (!qs.MetaQuery(graph, opts).QuerySupported) // return null; // TODO: We could also check if any part has NoData, we can abandon the query entirely QueryResultBuilder builder = new QueryResultBuilder(); builder.varMap = varMap2; builder.source = s; qs.Query(graph, opts, builder); return builder.bindings; } return null; }
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(); }