org.eclipse.rdf4j.query.Dataset Java Examples
The following examples show how to use
org.eclipse.rdf4j.query.Dataset.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ContextAwareConnectionTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 6 votes |
@Test public void testGraphQuery() throws Exception { RepositoryConnection stub = new RepositoryConnectionStub() { @Override public GraphQuery prepareGraphQuery(QueryLanguage ql, String query, String baseURI) throws MalformedQueryException, RepositoryException { assertEquals(SPARQL, ql); assertEquals(queryString, query); return new GraphQueryStub() { @Override public void setDataset(Dataset dataset) { Set<IRI> contexts = Collections.singleton(context); assertEquals(contexts, dataset.getDefaultGraphs()); super.setDataset(dataset); } }; } }; Repository repo = stub.getRepository(); ContextAwareConnection con = new ContextAwareConnection(repo, stub); con.setReadContexts(context); con.setQueryLanguage(SERQL); con.prepareGraphQuery(SPARQL, queryString, null); }
Example #2
Source File: TimeAwareHBaseSail.java From Halyard with Apache License 2.0 | 6 votes |
@Override public CloseableIteration<BindingSet, QueryEvaluationException> evaluate(TupleExpr tupleExpr, Dataset dataset, BindingSet bindings, boolean includeInferred) throws SailException { Binding b = bindings.getBinding(TIMESTAMP_CALLBACK_BINDING_NAME); final TimestampCallbackBinding timestampBinding = (b instanceof TimestampCallbackBinding) ? (TimestampCallbackBinding) b : null; CloseableIteration<BindingSet, QueryEvaluationException> iter = super.evaluate(tupleExpr, dataset, bindings, includeInferred); //push back the actual timestamp binding to the callback binding if requested if (timestampBinding != null) { iter = new FilterIteration<BindingSet, QueryEvaluationException>(iter) { @Override protected boolean accept(BindingSet bindings) throws QueryEvaluationException { Binding b = bindings.getBinding(TIMESTAMP_BINDING_NAME); //push back actual time if the timestamp binding is not provided timestampBinding.v = b == null ? SimpleValueFactory.getInstance().createLiteral(System.currentTimeMillis()) : b.getValue(); return true; } }; } return iter; }
Example #3
Source File: LuceneSailConnection.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 6 votes |
@Override public synchronized CloseableIteration<? extends BindingSet, QueryEvaluationException> evaluate(TupleExpr tupleExpr, Dataset dataset, BindingSet bindings, boolean includeInferred) throws SailException { QueryContext qctx = new QueryContext(); SearchIndexQueryContextInitializer.init(qctx, luceneIndex); final CloseableIteration<? extends BindingSet, QueryEvaluationException> iter; qctx.begin(); try { iter = evaluateInternal(tupleExpr, dataset, bindings, includeInferred); } finally { qctx.end(); } // NB: Iteration methods may do on-demand evaluation hence need to wrap // these too return new QueryContextIteration(iter, qctx); }
Example #4
Source File: ContextAwareConnectionTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 6 votes |
@Test public void testTupleQuery() throws Exception { RepositoryConnection stub = new RepositoryConnectionStub() { @Override public TupleQuery prepareTupleQuery(QueryLanguage ql, String query, String baseURI) throws MalformedQueryException, RepositoryException { assertEquals(SPARQL, ql); assertEquals(queryString, query); return new TupleQueryStub() { @Override public void setDataset(Dataset dataset) { Set<IRI> contexts = Collections.singleton(context); assertEquals(contexts, dataset.getDefaultGraphs()); super.setDataset(dataset); } }; } }; Repository repo = stub.getRepository(); ContextAwareConnection con = new ContextAwareConnection(repo, stub); con.setReadContexts(context); con.setQueryLanguage(SERQL); con.prepareTupleQuery(SPARQL, queryString, null); }
Example #5
Source File: OwnedTupleExpr.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 6 votes |
public CloseableIteration<BindingSet, QueryEvaluationException> evaluate(Dataset dataset, BindingSet bindings) throws QueryEvaluationException { CloseableIteration<BindingSet, QueryEvaluationException> rval = null; if (query != null) { try { synchronized (query) { for (String name : variables.keySet()) { if (bindings.hasBinding(name)) { Value value = bindings.getValue(name); query.setBinding(variables.get(name), value); } else { query.removeBinding(variables.get(name)); } } query.setDataset(dataset); TupleQueryResult result = query.evaluate(); rval = new InsertBindingSetCursor(result, bindings); } } catch (IllegalArgumentException e) { // NOPMD // query does not support BNode bindings } } return rval; }
Example #6
Source File: Transaction.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 6 votes |
/** * @param queryLn * @param sparqlUpdateString * @param baseURI * @param includeInferred * @param dataset * @param bindings * @throws ExecutionException * @throws InterruptedException */ void executeUpdate(QueryLanguage queryLn, String sparqlUpdateString, String baseURI, boolean includeInferred, Dataset dataset, Map<String, Value> bindings) throws InterruptedException, ExecutionException { Future<Boolean> result = submit(() -> { Update update = txnConnection.prepareUpdate(queryLn, sparqlUpdateString, baseURI); update.setIncludeInferred(includeInferred); if (dataset != null) { update.setDataset(dataset); } for (String bindingName : bindings.keySet()) { update.setBinding(bindingName, bindings.get(bindingName)); } update.execute(); return true; }); getFromFuture(result); }
Example #7
Source File: CachedSourceSelector.java From semagrow with Apache License 2.0 | 6 votes |
@Override @Loggable public Collection<SourceMetadata> getSources(StatementPattern pattern, Dataset dataset, BindingSet bindings) { Collection<SourceMetadata> retv; if( cache.containsKey(pattern) ) { retv = cache.get(pattern); } else { Collection<SourceMetadata> list = super.getSources(pattern, dataset, bindings); cache.put(pattern, list); retv = list; } return retv; }
Example #8
Source File: QueryStringUtil.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
/** * Construct a SELECT query for the provided statement with LIMIT 1. Such query can be used for source selection * instead of ASK queries. * * @param stmt * @param bindings * @return the SELECT query string */ public static String selectQueryStringLimit1(StatementPattern stmt, BindingSet bindings, Dataset dataset) { Set<String> varNames = new HashSet<>(); String s = constructStatement(stmt, varNames, bindings); StringBuilder res = new StringBuilder(); res.append("SELECT * "); appendDatasetClause(res, dataset); res.append("WHERE { "); res.append(s).append(" } LIMIT 1"); return res.toString(); }
Example #9
Source File: SPARQLComplianceTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
protected void uploadDataset(Dataset dataset) throws Exception { try (RepositoryConnection con = getDataRepository().getConnection()) { // Merge default and named graphs to filter duplicates Set<IRI> graphURIs = new HashSet<>(); graphURIs.addAll(dataset.getDefaultGraphs()); graphURIs.addAll(dataset.getNamedGraphs()); for (Resource graphURI : graphURIs) { upload(((IRI) graphURI), graphURI); } } }
Example #10
Source File: QueryStringUtil.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
/** * Construct a boolean ASK query for the provided statement. * * @param stmt * @param bindings * @return the ASK query string */ public static String askQueryString(StatementPattern stmt, BindingSet bindings, Dataset dataset) { Set<String> varNames = new HashSet<>(); String s = constructStatement(stmt, varNames, bindings); StringBuilder res = new StringBuilder(); res.append("ASK "); appendDatasetClause(res, dataset); res.append(" { "); res.append(s).append(" }"); return res.toString(); }
Example #11
Source File: SimpleQueryCompiler.java From semagrow with Apache License 2.0 | 5 votes |
@Override public Plan compile(QueryRoot query, Dataset dataset, BindingSet bindings) { // transformations on logical query. rewrite(query.getArg(), dataset, bindings); // split query to queryblocks. QueryBlock blockRoot = blockify(query, dataset, bindings); // infer interesting properties for each query block. blockRoot.visit(new InterestingPropertiesVisitor()); // infer interesting properties for each block // traverse Blocks and compile them bottom-up. Collection<Plan> plans = blockRoot.getPlans(getContext()); // enforce Site = Local RequestedPlanProperties props = new RequestedPlanProperties(); props.setSite(LocalSite.getInstance()); plans = getContext().enforceProps(plans, props); getContext().prune(plans); if (plans.isEmpty()) return null; else return plans.iterator().next(); }
Example #12
Source File: SailConnectionBooleanQuery.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Override public boolean evaluate() throws QueryEvaluationException { ParsedBooleanQuery parsedBooleanQuery = getParsedQuery(); TupleExpr tupleExpr = parsedBooleanQuery.getTupleExpr(); Dataset dataset = getDataset(); if (dataset == null) { // No external dataset specified, use query's own dataset (if any) dataset = parsedBooleanQuery.getDataset(); } try { SailConnection sailCon = getSailConnection(); CloseableIteration<? extends BindingSet, QueryEvaluationException> bindingsIter; bindingsIter = sailCon.evaluate(tupleExpr, dataset, getBindings(), getIncludeInferred()); bindingsIter = enforceMaxQueryTime(bindingsIter); try { return bindingsIter.hasNext(); } finally { bindingsIter.close(); } } catch (SailException e) { throw new QueryEvaluationException(e.getMessage(), e); } }
Example #13
Source File: AbstractFederationConnection.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Override public CloseableIteration<? extends BindingSet, QueryEvaluationException> evaluateInternal(TupleExpr query, Dataset dataset, BindingSet bindings, boolean inf) throws SailException { TripleSource tripleSource = new FederationTripleSource(inf); EvaluationStrategy strategy = federation.createEvaluationStrategy(tripleSource, dataset, getFederatedServiceResolver()); TupleExpr qry = optimize(query, dataset, bindings, inf, strategy); try { return strategy.evaluate(qry, EmptyBindingSet.getInstance()); } catch (QueryEvaluationException e) { throw new SailException(e); } }
Example #14
Source File: SPARQLProtocolSession.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
public void sendTupleQuery(QueryLanguage ql, String query, String baseURI, Dataset dataset, boolean includeInferred, int maxQueryTime, TupleQueryResultHandler handler, Binding... bindings) throws IOException, TupleQueryResultHandlerException, RepositoryException, MalformedQueryException, UnauthorizedException, QueryInterruptedException { HttpUriRequest method = getQueryMethod(ql, query, baseURI, dataset, includeInferred, maxQueryTime, bindings); getTupleQueryResult(method, handler); }
Example #15
Source File: SailUpdate.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Override public void execute() throws UpdateExecutionException { ParsedUpdate parsedUpdate = getParsedUpdate(); List<UpdateExpr> updateExprs = parsedUpdate.getUpdateExprs(); Map<UpdateExpr, Dataset> datasetMapping = parsedUpdate.getDatasetMapping(); SailUpdateExecutor executor = new SailUpdateExecutor(con.getSailConnection(), con.getValueFactory(), con.getParserConfig()); boolean localTransaction = false; try { if (!getConnection().isActive()) { localTransaction = true; beginLocalTransaction(); } for (UpdateExpr updateExpr : updateExprs) { Dataset activeDataset = getMergedDataset(datasetMapping.get(updateExpr)); try { executor.executeUpdate(updateExpr, activeDataset, getBindings(), getIncludeInferred(), getMaxExecutionTime()); } catch (RDF4JException | IOException e) { logger.warn("exception during update execution: ", e); if (!updateExpr.isSilent()) { throw new UpdateExecutionException(e); } } } if (localTransaction) { commitLocalTransaction(); localTransaction = false; } } finally { if (localTransaction) { rollbackLocalTransaction(); } } }
Example #16
Source File: SPARQLQueryTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
protected final void uploadDataset(Dataset dataset) throws Exception { RepositoryConnection con = dataRep.getConnection(); try { // Merge default and named graphs to filter duplicates Set<IRI> graphURIs = new HashSet<>(); graphURIs.addAll(dataset.getDefaultGraphs()); graphURIs.addAll(dataset.getNamedGraphs()); for (Resource graphURI : graphURIs) { upload(((IRI) graphURI), graphURI); } } finally { con.close(); } }
Example #17
Source File: VOIDSourceSelector.java From semagrow with Apache License 2.0 | 5 votes |
private Collection<SourceMetadata> getSources(Iterable<StatementPattern> patterns, Dataset dataset, BindingSet bindings) { Collection<SourceMetadata> metadata = new LinkedList<SourceMetadata>(); for (StatementPattern pattern : patterns) { metadata.addAll(getSources(pattern, dataset, bindings)); } return metadata; }
Example #18
Source File: UpdateContext.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
public UpdateContext(UpdateExpr updateExpr, Dataset dataset, BindingSet bindings, boolean includeInferred) { assert updateExpr != null; this.updateExpr = updateExpr; if (dataset == null) { this.dataset = new SimpleDataset(); } else { this.dataset = dataset; } if (bindings == null) { this.bindings = EmptyBindingSet.getInstance(); } else { this.bindings = bindings; } this.includeInferred = includeInferred; }
Example #19
Source File: NativeStoreConnectionExt.java From CostFed with GNU Affero General Public License v3.0 | 5 votes |
protected CloseableIteration<? extends BindingSet, QueryEvaluationException> evaluatePrecompiledInternal( TupleExpr tupleExpr, Dataset dataset, BindingSet bindings, boolean includeInferred) { /* replaceValues(tupleExpr); */ TripleSource tripleSource = null; //new RepositoryTripleSource(nativeStore, includeInferred, transactionActive()); StrictEvaluationStrategy strategy = new StrictEvaluationStrategy(tripleSource, dataset, null); return strategy.evaluate(tupleExpr, EmptyBindingSet.getInstance()); }
Example #20
Source File: QueryStringUtil.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
private static StringBuilder appendDatasetClause(StringBuilder sb, Dataset dataset) { if (dataset == null) { return sb; } for (IRI context : dataset.getDefaultGraphs()) { sb.append("FROM <").append(context.stringValue()).append("> "); } for (IRI namedContext : dataset.getNamedGraphs()) { sb.append("FROM NAMED <").append(namedContext.stringValue()).append("> "); } return sb; }
Example #21
Source File: FedXUtil.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
/** * Convert the given {@link Dataset} to an array of contexts * * @param ds * @return */ public static Resource[] toContexts(Dataset ds) { if (ds == null) { return new Resource[0]; } return ds.getDefaultGraphs().toArray(new Resource[0]); }
Example #22
Source File: SemagrowSailConnection.java From semagrow with Apache License 2.0 | 5 votes |
public final CloseableIteration<? extends BindingSet, QueryEvaluationException> evaluate( TupleExpr tupleExpr, Dataset dataset, BindingSet bindings, boolean includeInferred, boolean includeProvenance, Collection<IRI> includeOnlySources, Collection<IRI> excludeSources) throws SailException { //FIXME: flushPendingUpdates(); connectionLock.readLock().lock(); try { verifyIsOpen(); boolean registered = false; CloseableIteration<? extends BindingSet, QueryEvaluationException> iteration = evaluateInternal(tupleExpr, dataset, bindings, includeInferred, includeProvenance, includeOnlySources, excludeSources); try { CloseableIteration<? extends BindingSet, QueryEvaluationException> registeredIteration = registerIteration(iteration); registered = true; return registeredIteration; } finally { if (!registered) { try { iteration.close(); } catch (QueryEvaluationException e) { throw new SailException(e); } } } } finally { connectionLock.readLock().unlock(); } }
Example #23
Source File: RDF4JProtocolSession.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Override protected HttpUriRequest getUpdateMethod(QueryLanguage ql, String update, String baseURI, Dataset dataset, boolean includeInferred, int maxExecutionTime, Binding... bindings) { RequestBuilder builder = null; String transactionURL = getTransactionURL(); if (transactionURL != null) { builder = RequestBuilder.put(transactionURL); builder.addHeader("Content-Type", Protocol.SPARQL_UPDATE_MIME_TYPE + "; charset=utf-8"); builder.addParameter(Protocol.ACTION_PARAM_NAME, Action.UPDATE.toString()); for (NameValuePair nvp : getUpdateMethodParameters(ql, null, baseURI, dataset, includeInferred, maxExecutionTime, bindings)) { builder.addParameter(nvp); } // in a PUT request, we carry the only actual update string as the // request body - the rest is sent as request parameters builder.setEntity(new StringEntity(update, UTF8)); pingTransaction(); } else { builder = RequestBuilder.post(getUpdateURL()); builder.addHeader("Content-Type", Protocol.FORM_MIME_TYPE + "; charset=utf-8"); builder.setEntity(new UrlEncodedFormEntity(getUpdateMethodParameters(ql, update, baseURI, dataset, includeInferred, maxExecutionTime, bindings), UTF8)); } // functionality to provide custom http headers as required by the // applications for (Map.Entry<String, String> additionalHeader : getAdditionalHttpHeaders().entrySet()) { builder.addHeader(additionalHeader.getKey(), additionalHeader.getValue()); } return builder.build(); }
Example #24
Source File: StrictEvaluationStrategy.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
public StrictEvaluationStrategy(TripleSource tripleSource, Dataset dataset, FederatedServiceResolver serviceResolver, long iterationCacheSyncTreshold, EvaluationStatistics evaluationStatistics, boolean trackResultSize) { this.tripleSource = tripleSource; this.dataset = dataset; this.serviceResolver = serviceResolver; this.iterationCacheSyncThreshold = iterationCacheSyncTreshold; this.pipeline = new StandardQueryOptimizerPipeline(this, tripleSource, evaluationStatistics); this.uuid = UUID.randomUUID(); EvaluationStrategies.register(this); this.trackResultSize = trackResultSize; }
Example #25
Source File: SailSourceEvaluationStrategyFactory.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Override public EvaluationStrategy createEvaluationStrategy(Dataset dataset, TripleSource tripleSource, EvaluationStatistics evaluationStatistics) { EvaluationStrategy delegateStrategy = delegate.createEvaluationStrategy(dataset, tripleSource, evaluationStatistics); return new SailSourceEvaluationStrategy(delegateStrategy, dataset); }
Example #26
Source File: HalyardFilterOptimizer.java From Halyard with Apache License 2.0 | 5 votes |
@Override public void optimize(TupleExpr tupleExpr, Dataset dataset, BindingSet bindings) { tupleExpr.visit(new FilterOptimizer.FilterFinder(tupleExpr) { @Override public void meet(Filter filter) { super.meet(filter); filter.visit(new FilterOptimizer.FilterRelocator(filter) { { filterVars.retainAll(filter.getArg().getBindingNames()); } }); } }); }
Example #27
Source File: AskSourceSelector.java From semagrow with Apache License 2.0 | 5 votes |
@Override public Collection<SourceMetadata> getSources( TupleExpr expr, Dataset dataset, BindingSet bindings ) { if( expr instanceof StatementPattern ) { return getSources((StatementPattern)expr, dataset, bindings); } Collection<StatementPattern> patterns = StatementPatternCollector.process( expr ); return getSources( patterns, dataset, bindings ); }
Example #28
Source File: SimplePlanGenerator.java From semagrow with Apache License 2.0 | 5 votes |
@Override public PlanCollection accessPlans(TupleExpr expr, BindingSet bindings, Dataset dataset) { PlanCollection plans = new PlanCollectionImpl(expr); if (expr instanceof BindingSetAssignment) { plans.add(create(expr)); return plans; } // get sources for each pattern Collection<SourceMetadata> sources = getSources(expr, dataset, bindings); List<Plan> sourcePlans = new LinkedList<Plan>(); if (sources.isEmpty()) { plans.add(create(new EmptySet())); } else { for (SourceMetadata sourceMetadata : sources) { //URI source = sourceMetadata.getSites().get(0); //Plan p1 = createPlan(exprLabel, sourceMetadata.target(), source, ctx); // FIXME: Don't use always the first source. Plan p1 = createPlan(sourceMetadata.target().clone(), sourceMetadata); sourcePlans.add(p1); } Plan p = createUnionPlan(sourcePlans); plans.add(p); } return plans; }
Example #29
Source File: PrepareOwnedTupleExpr.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Override public void optimize(TupleExpr query, Dataset dataset, BindingSet bindings) { try { query.visit(this); } catch (RepositoryException e) { throw new UndeclaredThrowableException(e); } }
Example #30
Source File: AbstractParserQuery.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
/** * Gets the "active" dataset for this query. The active dataset is either the dataset that has been specified using * {@link #setDataset(Dataset)} or the dataset that has been specified in the query, where the former takes * precedence over the latter. * * @return The active dataset, or <tt>null</tt> if there is no dataset. */ public Dataset getActiveDataset() { if (dataset != null) { return FallbackDataset.fallback(dataset, parsedQuery.getDataset()); } // No external dataset specified, use query's own dataset (if any) return parsedQuery.getDataset(); }