use of com.bigdata.rdf.sparql.ast.StatementPatternNode in project wikidata-query-rdf by wikimedia.
the class EmptyLabelServiceOptimizer method optimizeJoinGroup.
@Override
protected void optimizeJoinGroup(AST2BOpContext ctx, StaticAnalysis sa, IBindingSet[] bSets, JoinGroupNode op) {
final QueryRoot root = sa.getQueryRoot();
if (root.getQueryType() == QueryType.ASK) {
return;
}
if (root.getWhereClause() == op) {
op.setProperty(LABEL_SERVICE_PROJECTION, root.getProjection());
}
op.getChildren(SubqueryBase.class).forEach(node -> {
if (node.getWhereClause() != null) {
BOp whereClause = node.getWhereClause();
whereClause.setProperty(LABEL_SERVICE_PROJECTION, node.getProjection());
}
});
// Prepare a set of vars, which might be bound both outside of the service and by LabelService
// Fix for the issue: https://phabricator.wikimedia.org/T159723
// See also patch for the com.bigdata.rdf.sparql.ast.eval.AST2BOpUtility.addServiceCall()
Set<IVariable<?>> uncertainVars = collectUncertainVars(sa, bSets, op);
getLabelServiceNodes(op).forEach(service -> {
service.setUncertainVars(uncertainVars);
JoinGroupNode g = (JoinGroupNode) service.getGraphPattern();
boolean foundArg = false;
for (BOp st : g.args()) {
StatementPatternNode sn = (StatementPatternNode) st;
if (sn.s().isConstant() && BD.SERVICE_PARAM.equals(sn.s().getValue())) {
continue;
}
foundArg = true;
break;
}
if (restoreExtracted(service)) {
foundArg = true;
}
if (!foundArg) {
addResolutions(ctx, g, getProjectionNode(service));
}
});
}
use of com.bigdata.rdf.sparql.ast.StatementPatternNode in project wikidata-query-rdf by wikimedia.
the class MWApiServiceFactory method serviceParamsFromNode.
/**
* Get service params from Service Node.
* FIXME: copypaste from ServiceParams.java, should be integrated there
*/
private ServiceParams serviceParamsFromNode(final ServiceNode serviceNode) {
requireNonNull(serviceNode, "Service node is null?");
final GraphPatternGroup<IGroupMemberNode> group = serviceNode.getGraphPattern();
requireNonNull(group, "Group node is null?");
final ServiceParams serviceParams = new ServiceParams();
for (IGroupMemberNode node : group) {
if (node instanceof StatementPatternNode) {
final StatementPatternNode sp = (StatementPatternNode) node;
final TermNode s = sp.s();
if (s.isConstant() && BD.SERVICE_PARAM.equals(s.getValue())) {
if (sp.p().isVariable()) {
throw new RuntimeException("not a valid service param triple pattern, " + "predicate must be constant: " + sp);
}
final URI param = (URI) sp.p().getValue();
serviceParams.add(param, sp.o());
}
}
}
return serviceParams;
}
use of com.bigdata.rdf.sparql.ast.StatementPatternNode in project wikidata-query-rdf by wikimedia.
the class GeoAroundService method buildServiceNode.
@Override
protected JoinGroupNode buildServiceNode(ServiceCallCreateParams params, ServiceParams serviceParams) {
final AbstractTripleStore store = params.getTripleStore();
final Vocabulary voc = store.getVocabulary();
BigdataValueFactory vf = store.getValueFactory();
final StatementPatternNode pattern = getPatternNode(params);
final TermNode searchVar = pattern.s();
final TermNode predicate = pattern.p();
final TermNode locationVar = pattern.o();
final JoinGroupNode newGroup = new JoinGroupNode();
// ?var geo:search "inCircle" .
newGroup.addArg(new StatementPatternNode(searchVar, new DummyConstantNode(vf.asValue(GeoSpatial.SEARCH)), new DummyConstantNode(vf.createLiteral(GeoFunction.IN_CIRCLE.toString()))));
// ?var geo:predicate wdt:P625 .
newGroup.addArg(new StatementPatternNode(searchVar, new DummyConstantNode(vf.asValue(GeoSpatial.PREDICATE)), predicate));
// ?var geo:searchDatatype ogc:wktLiteral .
newGroup.addArg(new StatementPatternNode(searchVar, new DummyConstantNode(vf.asValue(GeoSpatial.SEARCH_DATATYPE)), new ConstantNode(voc.getConstant(new URIImpl(GeoSparql.WKT_LITERAL)))));
// ?var geo:spatialCircleCenter ?parisLoc .
newGroup.addArg(new StatementPatternNode(searchVar, new DummyConstantNode(vf.asValue(GeoSpatial.SPATIAL_CIRCLE_CENTER)), getParam(serviceParams, CENTER_PARAM)));
// ?var geo:spatialCircleRadius "1" .
newGroup.addArg(new StatementPatternNode(searchVar, new DummyConstantNode(vf.asValue(GeoSpatial.SPATIAL_CIRCLE_RADIUS)), getParam(serviceParams, RADIUS_PARAM)));
// ?var geo:locationValue ?location .
newGroup.addArg(new StatementPatternNode(searchVar, new DummyConstantNode(vf.asValue(GeoSpatial.LOCATION_VALUE)), locationVar));
// ?var geo:coordSystem "0" .
newGroup.addArg(new StatementPatternNode(searchVar, new DummyConstantNode(vf.asValue(GeoSpatial.COORD_SYSTEM)), getGlobeNode(vf, serviceParams)));
final TermNode distance = serviceParams.get(DISTANCE_PARAM, null);
if (distance != null) {
// ?var geo:distanceValue ?distance .
newGroup.addArg(new StatementPatternNode(searchVar, new DummyConstantNode(vf.asValue(GeoSpatial.DISTANCE_VALUE)), distance));
}
return newGroup;
}
use of com.bigdata.rdf.sparql.ast.StatementPatternNode in project wikidata-query-rdf by wikimedia.
the class GeoService method getRequiredBound.
@Override
public Set<IVariable<?>> getRequiredBound(final ServiceNode serviceNode) {
/**
* This method extracts exactly those variables that are incoming,
* i.e. must be bound before executing the execution of the service.
*
* Those can be only in service parameters.
*/
final Set<IVariable<?>> requiredBound = new HashSet<>();
for (StatementPatternNode sp : getStatementPatterns(serviceNode)) {
final TermNode subj = sp.s();
final IVariableOrConstant<?> object = sp.o().getValueExpression();
if (subj.isConstant() && BD.SERVICE_PARAM.equals(subj.getValue()) && object instanceof IVariable<?>) {
// the subject var is what we return
requiredBound.add((IVariable<?>) object);
}
}
return requiredBound;
}
use of com.bigdata.rdf.sparql.ast.StatementPatternNode in project wikidata-query-rdf by wikimedia.
the class LabelServiceUnitTest method desiredVars.
@SuppressWarnings("unchecked")
@Test
public void desiredVars() {
JoinGroupNode patterns = new JoinGroupNode();
// Label
patterns.addArg(new StatementPatternNode(new VarNode("item"), createURI(RDFS.LABEL), new VarNode("itemLabel")));
// Description
patterns.addArg(new StatementPatternNode(new VarNode("item2"), createURI(SchemaDotOrg.DESCRIPTION), new VarNode("itemDesc")));
// Fixed name
patterns.addArg(new StatementPatternNode(createURI(uris().entityIdToURI("Q123")), createURI(RDFS.LABEL), new VarNode("qLabel")));
// Parameters
patterns.addArg(new StatementPatternNode(createURI(BD.SERVICE_PARAM), createURI(LabelService.LANGUAGE_PARAM), createConstant("en,fr")));
ServiceNode serviceNode = new ServiceNode(createURI(LabelService.SERVICE_KEY), patterns);
final LabelService service = new LabelService();
Set<IVariable<?>> vars = service.getDesiredBound(serviceNode);
assertThat(vars, hasSize(2));
assertThat(vars, hasItems(equalTo(Var.var("item")), equalTo(Var.var("item2"))));
}
Aggregations