org.eclipse.rdf4j.query.algebra.Service Java Examples

The following examples show how to use org.eclipse.rdf4j.query.algebra.Service. 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: SAILFederatedService.java    From CostFed with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public boolean ask(Service service, BindingSet bindings, String baseUri) throws QueryEvaluationException {
	RepositoryConnection conn = endpoint.getConn();
	try {
		BooleanQuery query = conn.prepareBooleanQuery(QueryLanguage.SPARQL, service.getAskQueryString(), baseUri);
		Iterator<Binding> bIter = bindings.iterator();
		while (bIter.hasNext()) {
			Binding b = bIter.next();
			if (service.getServiceVars().contains(b.getName()))
				query.setBinding(b.getName(), b.getValue());
		}
		return query.evaluate();
	} catch(Throwable e) {
		throw new QueryEvaluationException(e);
	} finally {
		conn.close();
	}
}
 
Example #2
Source File: HalyardEvaluationStatistics.java    From Halyard with Apache License 2.0 6 votes vote down vote up
@Override
public void meet(Service node) {
    HalyardEvaluationStatistics srvStats = srvProvider != null && node.getServiceRef().hasValue() ? srvProvider.getStatsForService(node.getServiceRef().getValue().stringValue()) : null;
    //try to calculate cardinality also for (Halyard-internally) federated service expressions
    if (srvStats != null) {
        Double servCard = null;
        if (mapToUpdate != null) {
            srvStats.updateCardinalityMap(node.getServiceExpr(), boundVars, priorityVariables, mapToUpdate);
            servCard = mapToUpdate.get(node.getServiceExpr());
        }
        cardinality = servCard != null ? servCard : srvStats.getCardinality(node.getServiceExpr());
    } else {
        super.meet(node);
    }
    updateMap(node);
}
 
Example #3
Source File: TupleExprBuilder.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public Object visit(ASTServiceGraphPattern node, Object data) throws VisitorException {
	GraphPattern parentGP = graphPattern;

	ValueExpr serviceRef = (ValueExpr) node.jjtGetChild(0).jjtAccept(this, null);

	graphPattern = new GraphPattern(parentGP);
	node.jjtGetChild(1).jjtAccept(this, null);
	TupleExpr serviceExpr = graphPattern.buildTupleExpr();

	if (serviceExpr instanceof SingletonSet) {
		return null; // do not add an empty service block
	}

	String serviceExpressionString = node.getPatternString();

	parentGP.addRequiredTE(new Service(mapValueExprToVar(serviceRef), serviceExpr, serviceExpressionString,
			node.getPrefixDeclarations(), node.getBaseURI(), node.isSilent()));

	graphPattern = parentGP;

	return null;
}
 
Example #4
Source File: EvaluationStatistics.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void meet(Service node) {
	if (!node.getServiceRef().hasValue()) {
		// the URI is not available, may be computed in the course of the
		// query
		// => use high cost to order the SERVICE node late in the query plan
		cardinality = UNBOUND_SERVICE_CARDINALITY;
	} else {
		ServiceNodeAnalyzer serviceAnalyzer = new ServiceNodeAnalyzer();
		node.visitChildren(serviceAnalyzer);
		int count = serviceAnalyzer.getStatementCount();

		// more than one free variable in a single triple pattern
		if (count == 1 && node.getServiceVars().size() > 1) {
			cardinality = 100 + node.getServiceVars().size(); // TODO (should
			// be higher
			// than other
			// simple
			// stmts)
		} else {
			// only very selective statements should be better than this
			// => evaluate service expressions first
			cardinality = 1 + (node.getServiceVars().size() * 0.1);
		}
	}
}
 
Example #5
Source File: LimitedSizeEvaluationStrategy.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public CloseableIteration<BindingSet, QueryEvaluationException> evaluate(Join join, BindingSet bindings)
		throws QueryEvaluationException {
	// efficient computation of a SERVICE join using vectored evaluation
	// TODO maybe we can create a ServiceJoin node already in the parser?
	if (join.getRightArg() instanceof Service) {
		CloseableIteration<BindingSet, QueryEvaluationException> leftIter = evaluate(join.getLeftArg(), bindings);
		return new ServiceJoinIterator(leftIter, (Service) join.getRightArg(), bindings, this);
	}

	if (TupleExprs.containsSubquery(join.getRightArg())) {
		return new LimitedSizeHashJoinIteration(this, join, bindings, used, maxSize);
	} else {
		return new JoinIterator(this, join, bindings);
	}
}
 
Example #6
Source File: SAILFederatedService.java    From CostFed with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public CloseableIteration<BindingSet, QueryEvaluationException> select(Service service, Set<String> projectionVars, BindingSet bindings, String baseUri) throws QueryEvaluationException {
	RepositoryConnection conn = endpoint.getConn();
	try {
		TupleQuery query = conn.prepareTupleQuery(QueryLanguage.SPARQL, service.getSelectQueryString(projectionVars), baseUri);
		Iterator<Binding> bIter = bindings.iterator();
		while (bIter.hasNext()) {
			Binding b = bIter.next();
			if (service.getServiceVars().contains(b.getName()))
				query.setBinding(b.getName(), b.getValue());
		}
		TupleQueryResult qRes = query.evaluate();
		return new InsertBindingsIteration(qRes, bindings);
	} catch(Throwable e) {
		throw new QueryEvaluationException(e);
	} finally {
		conn.close();
	}
}
 
Example #7
Source File: ServiceOptimizer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void meet(Service service) {
	// create an optimized service, which may wrap the original service
	// the latter is the case, if we cannot optimize the SERVICE node in FedX
	TupleExpr newExpr = optimizeService(service);
	service.replaceWith(newExpr);
}
 
Example #8
Source File: ServiceTests.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public CloseableIteration<BindingSet, QueryEvaluationException> evaluate(Service service,
		CloseableIteration<BindingSet, QueryEvaluationException> bindings, String baseUri)
		throws QueryEvaluationException {
	boundJoinRequestCount.incrementAndGet();
	return super.evaluate(service, bindings, baseUri);
}
 
Example #9
Source File: SpinRenderer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void meet(Service node) throws RDFHandlerException {
	listEntry();
	handler.handleStatement(valueFactory.createStatement(subject, RDF.TYPE, SP.SERVICE_CLASS));
	predicate = SP.SERVICE_URI_PROPERTY;
	node.getServiceRef().visit(this);
	predicate = null;
	Resource elementsList = valueFactory.createBNode();
	handler.handleStatement(valueFactory.createStatement(subject, SP.ELEMENTS_PROPERTY, elementsList));
	ListContext elementsCtx = newList(elementsList);
	node.getArg().visit(this);
	endList(elementsCtx);
}
 
Example #10
Source File: TupleFunctionFederatedService.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public boolean ask(Service service, BindingSet bindings, String baseUri) throws QueryEvaluationException {
	try (final CloseableIteration<BindingSet, QueryEvaluationException> iter = evaluate(service,
			new SingletonIteration<>(bindings), baseUri);) {
		while (iter.hasNext()) {
			BindingSet bs = iter.next();
			String firstVar = service.getBindingNames().iterator().next();
			return QueryEvaluationUtil.getEffectiveBooleanValue(bs.getValue(firstVar));
		}
	}
	return false;
}
 
Example #11
Source File: RepositoryFederatedService.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * @param inputBindings
 * @throws QueryEvaluationException
 */
public BatchingServiceIteration(CloseableIteration<BindingSet, QueryEvaluationException> inputBindings,
		int blockSize, Service service) throws QueryEvaluationException {
	super(inputBindings, null, EmptyBindingSet.getInstance());
	this.blockSize = blockSize;
	this.service = service;
	run();
}
 
Example #12
Source File: GenericInfoOptimizer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void meet(Service service) {
	if (services == null) {
		services = new ArrayList<>();
	}
	services.add(service);
}
 
Example #13
Source File: RepositoryFederatedService.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public FallbackServiceIteration(Service service,
		List<BindingSet> allBindings, String baseUri) {
	super(null, null, null);
	this.service = service;
	this.allBindings = allBindings;
	this.baseUri = baseUri;
	run();
}
 
Example #14
Source File: RepositoryFederatedService.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public CloseableIteration<BindingSet, QueryEvaluationException> evaluate(Service service,
		CloseableIteration<BindingSet, QueryEvaluationException> bindings, String baseUri)
		throws QueryEvaluationException {

	if (boundJoinBlockSize > 0) {
		return new BatchingServiceIteration(bindings, boundJoinBlockSize, service);
	} else {
		// if blocksize is 0 (i.e. disabled) the entire iteration is used as
		// block
		return evaluateInternal(service, bindings, service.getBaseURI());
	}
}
 
Example #15
Source File: RepositoryFederatedService.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Evaluate the service expression for the given lists of bindings using {@link FallbackServiceIteration}, i.e.
 * basically as a simple join without VALUES clause.
 *
 * @param service     the SERVICE
 * @param allBindings all bindings to be processed
 * @param baseUri     the base URI
 * @return resulting iteration
 */
private CloseableIteration<BindingSet, QueryEvaluationException> evaluateInternalFallback(Service service,
		List<BindingSet> allBindings, String baseUri) {

	CloseableIteration<BindingSet, QueryEvaluationException> res = new FallbackServiceIteration(service,
			allBindings, baseUri);

	if (service.isSilent()) {
		res = new SilentIteration(res);
	}
	return res;

}
 
Example #16
Source File: StrictEvaluationStrategy.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public CloseableIteration<BindingSet, QueryEvaluationException> evaluate(Join join, BindingSet bindings)
		throws QueryEvaluationException {
	// efficient computation of a SERVICE join using vectored evaluation
	// TODO maybe we can create a ServiceJoin node already in the parser?
	if (join.getRightArg() instanceof Service) {
		CloseableIteration<BindingSet, QueryEvaluationException> leftIter = evaluate(join.getLeftArg(), bindings);
		return new ServiceJoinIterator(leftIter, (Service) join.getRightArg(), bindings, this);
	}

	if (isOutOfScopeForLeftArgBindings(join.getRightArg())) {
		return new HashJoinIteration(this, join, bindings);
	} else {
		return new JoinIterator(this, join, bindings);
	}
}
 
Example #17
Source File: StrictEvaluationStrategy.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public CloseableIteration<BindingSet, QueryEvaluationException> evaluate(UnaryTupleOperator expr,
		BindingSet bindings) throws QueryEvaluationException {
	if (expr instanceof Projection) {
		return evaluate((Projection) expr, bindings);
	} else if (expr instanceof MultiProjection) {
		return evaluate((MultiProjection) expr, bindings);
	} else if (expr instanceof Filter) {
		return evaluate((Filter) expr, bindings);
	} else if (expr instanceof Service) {
		return evaluate((Service) expr, bindings);
	} else if (expr instanceof Slice) {
		return evaluate((Slice) expr, bindings);
	} else if (expr instanceof Extension) {
		return evaluate((Extension) expr, bindings);
	} else if (expr instanceof Distinct) {
		return evaluate((Distinct) expr, bindings);
	} else if (expr instanceof Reduced) {
		return evaluate((Reduced) expr, bindings);
	} else if (expr instanceof Group) {
		return evaluate((Group) expr, bindings);
	} else if (expr instanceof Order) {
		return evaluate((Order) expr, bindings);
	} else if (expr instanceof QueryRoot) {
		// new query, reset shared return value for successive calls of
		// NOW()
		this.sharedValueOfNow = null;
		return evaluate(expr.getArg(), bindings);
	} else if (expr instanceof DescribeOperator) {
		return evaluate((DescribeOperator) expr, bindings);
	} else if (expr == null) {
		throw new IllegalArgumentException("expr must not be null");
	} else {
		throw new QueryEvaluationException("Unknown unary tuple operator type: " + expr.getClass());
	}
}
 
Example #18
Source File: StrictEvaluationStrategy.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public CloseableIteration<BindingSet, QueryEvaluationException> evaluate(Service service, String serviceUri,
		CloseableIteration<BindingSet, QueryEvaluationException> bindings) throws QueryEvaluationException {
	try {
		FederatedService fs = serviceResolver.getService(serviceUri);
		return fs.evaluate(service, bindings, service.getBaseURI());
	} catch (QueryEvaluationException e) {
		// suppress exceptions if silent
		if (service.isSilent()) {
			return bindings;
		} else {
			throw new QueryEvaluationException(e);
		}
	}
}
 
Example #19
Source File: SAILFederatedService.java    From CostFed with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public CloseableIteration<BindingSet, QueryEvaluationException> evaluate(
		Service service,
		CloseableIteration<BindingSet, QueryEvaluationException> bindings,
		String baseUri) throws QueryEvaluationException {
	
	throw new UnsupportedOperationException("NOT YET IMPLEMENTED");
}
 
Example #20
Source File: HalyardTupleExprEvaluation.java    From Halyard with Apache License 2.0 5 votes vote down vote up
/**
 * Switch logic for evaluation of any instance of a {@link UnaryTupleOperator} query model node
 * @param parent
 * @param expr
 * @param bindings
 */
private void evaluateUnaryTupleOperator(BindingSetPipe parent, UnaryTupleOperator expr, BindingSet bindings) {
    if (expr instanceof Projection) {
        evaluateProjection(parent, (Projection) expr, bindings);
    } else if (expr instanceof MultiProjection) {
        evaluateMultiProjection(parent, (MultiProjection) expr, bindings);
    } else if (expr instanceof Filter) {
        evaluateFilter(parent, (Filter) expr, bindings);
    } else if (expr instanceof Service) {
        evaluateService(parent, (Service) expr, bindings);
    } else if (expr instanceof Slice) {
        evaluateSlice(parent, (Slice) expr, bindings);
    } else if (expr instanceof Extension) {
        evaluateExtension(parent, (Extension) expr, bindings);
    } else if (expr instanceof Distinct) {
        evaluateDistinct(parent, (Distinct) expr, bindings);
    } else if (expr instanceof Reduced) {
        evaluateReduced(parent, (Reduced) expr, bindings);
    } else if (expr instanceof Group) {
        evaluateGroup(parent, (Group) expr, bindings);
    } else if (expr instanceof Order) {
        evaluateOrder(parent, (Order) expr, bindings);
    } else if (expr instanceof QueryRoot) {
        parentStrategy.sharedValueOfNow = null;
        evaluateTupleExpr(parent, ((QueryRoot) expr).getArg(), bindings);
    } else if (expr instanceof DescribeOperator) {
        evaluateDescribeOperator(parent, (DescribeOperator) expr, bindings);
    } else if (expr == null) {
        parent.handleException(new IllegalArgumentException("expr must not be null"));
    } else {
        parent.handleException(new QueryEvaluationException("Unknown unary tuple operator type: " + expr.getClass()));
    }
}
 
Example #21
Source File: ServiceOptimizer.java    From CostFed with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void meet(Service service) {
	// create an optimized service, which may wrap the original service
	// the latter is the case, if we cannot optimize the SERVICE node in FedX
	TupleExpr newExpr = optimizeService(service);
	service.replaceWith(newExpr);
}
 
Example #22
Source File: StrictEvaluationStrategy.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private Set<Var> getBoundVariables(Service service) {
	BoundVarVisitor visitor = new BoundVarVisitor();
	visitor.meet(service);
	return visitor.boundVars;
}
 
Example #23
Source File: StrictEvaluationStrategy.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public CloseableIteration<BindingSet, QueryEvaluationException> evaluate(Service service, BindingSet bindings)
		throws QueryEvaluationException {
	Var serviceRef = service.getServiceRef();

	String serviceUri;
	if (serviceRef.hasValue()) {
		serviceUri = serviceRef.getValue().stringValue();
	} else {
		if (bindings != null && bindings.getValue(serviceRef.getName()) != null) {
			serviceUri = bindings.getBinding(serviceRef.getName()).getValue().stringValue();
		} else {
			throw new QueryEvaluationException("SERVICE variables must be bound at evaluation time.");
		}
	}

	try {

		FederatedService fs = serviceResolver.getService(serviceUri);

		// create a copy of the free variables, and remove those for which
		// bindings are available (we can set them as constraints!)
		Set<String> freeVars = new HashSet<>(service.getServiceVars());
		freeVars.removeAll(bindings.getBindingNames());

		// Get bindings from values pre-bound into variables.
		MapBindingSet allBindings = new MapBindingSet();
		for (Binding binding : bindings) {
			allBindings.addBinding(binding.getName(), binding.getValue());
		}

		Set<Var> boundVars = getBoundVariables(service);
		for (Var boundVar : boundVars) {
			freeVars.remove(boundVar.getName());
			allBindings.addBinding(boundVar.getName(), boundVar.getValue());
		}
		bindings = allBindings;

		String baseUri = service.getBaseURI();

		// special case: no free variables => perform ASK query
		if (freeVars.isEmpty()) {
			boolean exists = fs.ask(service, bindings, baseUri);

			// check if triples are available (with inserted bindings)
			if (exists) {
				return new SingletonIteration<>(bindings);
			} else {
				return new EmptyIteration<>();
			}

		}

		// otherwise: perform a SELECT query
		return fs.select(service, freeVars, bindings,
				baseUri);

	} catch (RuntimeException e) {
		// suppress exceptions if silent
		if (service.isSilent()) {
			return new SingletonIteration<>(bindings);
		} else {
			throw e;
		}
	}

}
 
Example #24
Source File: TupleFunctionFederatedService.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public final CloseableIteration<BindingSet, QueryEvaluationException> evaluate(Service service,
		CloseableIteration<BindingSet, QueryEvaluationException> bindings, String baseUri)
		throws QueryEvaluationException {
	if (!bindings.hasNext()) {
		return new EmptyIteration<>();
	}

	TupleExpr expr = service.getArg();
	if (!(expr instanceof TupleFunctionCall)) {
		return new EmptyIteration<>();
	}

	TupleFunctionCall funcCall = (TupleFunctionCall) expr;
	TupleFunction func = tupleFunctionRegistry.get(funcCall.getURI())
			.orElseThrow(() -> new QueryEvaluationException("Unknown tuple function '" + funcCall.getURI() + "'"));

	List<ValueExpr> argExprs = funcCall.getArgs();

	List<CloseableIteration<BindingSet, QueryEvaluationException>> resultIters = new ArrayList<>();
	while (bindings.hasNext()) {
		BindingSet bs = bindings.next();
		Value[] argValues = new Value[argExprs.size()];
		for (int i = 0; i < argExprs.size(); i++) {
			ValueExpr argExpr = argExprs.get(i);
			Value argValue;
			if (argExpr instanceof Var) {
				argValue = getValue((Var) argExpr, bs);
			} else if (argExpr instanceof ValueConstant) {
				argValue = ((ValueConstant) argExpr).getValue();
			} else {
				throw new ValueExprEvaluationException(
						"Unsupported ValueExpr for argument " + i + ": " + argExpr.getClass().getSimpleName());
			}
			argValues[i] = argValue;
		}
		resultIters
				.add(TupleFunctionEvaluationStrategy.evaluate(func, funcCall.getResultVars(), bs, vf, argValues));
	}
	return (resultIters.size() > 1) ? new DistinctIteration<>(new UnionIteration<>(resultIters))
			: resultIters.get(0);
}
 
Example #25
Source File: OrderComparatorTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public CloseableIteration<BindingSet, QueryEvaluationException> evaluate(Service expr, String serviceUri,
		CloseableIteration<BindingSet, QueryEvaluationException> bindings) throws QueryEvaluationException {
	throw new UnsupportedOperationException();
}
 
Example #26
Source File: AbstractQueryModelVisitor.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void meet(Service node) throws X {
	meetNode(node);
}
 
Example #27
Source File: HBaseSail.java    From Halyard with Apache License 2.0 4 votes vote down vote up
@Override
public boolean ask(Service service, BindingSet bindings, String baseUri) throws QueryEvaluationException {
    try (CloseableIteration<BindingSet, QueryEvaluationException> res = evaluate(new ServiceRoot(service.getArg()), null, bindings, true)) {
        return res.hasNext();
    }
}
 
Example #28
Source File: HBaseSail.java    From Halyard with Apache License 2.0 4 votes vote down vote up
@Override
public CloseableIteration<BindingSet, QueryEvaluationException> select(Service service, Set<String> projectionVars, BindingSet bindings, String baseUri) throws QueryEvaluationException {
    return new InsertBindingSetCursor(evaluate(new ServiceRoot(service.getArg()), null, bindings, true), bindings);
}
 
Example #29
Source File: HBaseSail.java    From Halyard with Apache License 2.0 4 votes vote down vote up
@Override
public CloseableIteration<BindingSet, QueryEvaluationException> evaluate(Service service, CloseableIteration<BindingSet, QueryEvaluationException> bindings, String baseUri) throws QueryEvaluationException {
    throw new UnsupportedOperationException();
}
 
Example #30
Source File: HalyardEvaluationStrategy.java    From Halyard with Apache License 2.0 4 votes vote down vote up
/**
 * Called by RDF4J to evaluate a query or part of a query using a service
 */
@Override
public CloseableIteration<BindingSet, QueryEvaluationException> evaluate(Service service, String serviceUri, CloseableIteration<BindingSet, QueryEvaluationException> bindings) throws QueryEvaluationException {
    throw new UnsupportedOperationException();
}