Search in sources :

Example 1 with JoinGroupNode

use of com.bigdata.rdf.sparql.ast.JoinGroupNode 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));
        }
    });
}
Also used : BOp(com.bigdata.bop.BOp) QueryRoot(com.bigdata.rdf.sparql.ast.QueryRoot) IVariable(com.bigdata.bop.IVariable) SubqueryBase(com.bigdata.rdf.sparql.ast.SubqueryBase) JoinGroupNode(com.bigdata.rdf.sparql.ast.JoinGroupNode) StatementPatternNode(com.bigdata.rdf.sparql.ast.StatementPatternNode)

Example 2 with JoinGroupNode

use of com.bigdata.rdf.sparql.ast.JoinGroupNode 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;
}
Also used : Vocabulary(com.bigdata.rdf.vocab.Vocabulary) AbstractTripleStore(com.bigdata.rdf.store.AbstractTripleStore) BigdataValueFactory(com.bigdata.rdf.model.BigdataValueFactory) ConstantNode(com.bigdata.rdf.sparql.ast.ConstantNode) DummyConstantNode(com.bigdata.rdf.sparql.ast.DummyConstantNode) DummyConstantNode(com.bigdata.rdf.sparql.ast.DummyConstantNode) JoinGroupNode(com.bigdata.rdf.sparql.ast.JoinGroupNode) URIImpl(org.openrdf.model.impl.URIImpl) StatementPatternNode(com.bigdata.rdf.sparql.ast.StatementPatternNode) TermNode(com.bigdata.rdf.sparql.ast.TermNode)

Example 3 with JoinGroupNode

use of com.bigdata.rdf.sparql.ast.JoinGroupNode in project wikidata-query-rdf by wikimedia.

the class GeoService method create.

@Override
public BigdataServiceCall create(ServiceCallCreateParams params, ServiceParams serviceParams) {
    if (params == null)
        throw new IllegalArgumentException();
    final JoinGroupNode newGroup = buildServiceNode(params, serviceParams);
    final BigdataValueFactory vf = params.getTripleStore().getValueFactory();
    ServiceNode newServiceNode = new ServiceNode(new DummyConstantNode(vf.asValue(GeoSpatial.SEARCH)), newGroup);
    // Transfer hints
    newServiceNode.setQueryHints(params.getServiceNode().getQueryHints());
    // Call delegate service
    HttpClient client = params.getClientConnectionManager();
    return (BigdataServiceCall) ServiceRegistry.getInstance().toServiceCall(params.getTripleStore(), client, GeoSpatial.SEARCH, newServiceNode, params.getStats());
}
Also used : ServiceNode(com.bigdata.rdf.sparql.ast.service.ServiceNode) BigdataValueFactory(com.bigdata.rdf.model.BigdataValueFactory) DummyConstantNode(com.bigdata.rdf.sparql.ast.DummyConstantNode) HttpClient(org.eclipse.jetty.client.HttpClient) JoinGroupNode(com.bigdata.rdf.sparql.ast.JoinGroupNode) BigdataServiceCall(com.bigdata.rdf.sparql.ast.service.BigdataServiceCall)

Example 4 with JoinGroupNode

use of com.bigdata.rdf.sparql.ast.JoinGroupNode 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"))));
}
Also used : VarNode(com.bigdata.rdf.sparql.ast.VarNode) ServiceNode(com.bigdata.rdf.sparql.ast.service.ServiceNode) IVariable(com.bigdata.bop.IVariable) JoinGroupNode(com.bigdata.rdf.sparql.ast.JoinGroupNode) StatementPatternNode(com.bigdata.rdf.sparql.ast.StatementPatternNode) Test(org.junit.Test)

Example 5 with JoinGroupNode

use of com.bigdata.rdf.sparql.ast.JoinGroupNode in project wikidata-query-rdf by wikimedia.

the class ApiTemplateUnitTest method testServiceOutput.

@Test
public void testServiceOutput() throws Exception {
    JsonNode json = parseJson(JSON_CONFIG);
    ApiTemplate template = ApiTemplate.fromJSON(json);
    assertThat(template.getItemsPath(), equalTo("/api/query/pages/page/categories/cl"));
    JoinGroupNode patterns = new JoinGroupNode();
    // predefined variable
    patterns.addArg(new StatementPatternNode(new VarNode("somevar"), createURI(ApiTemplate.OutputVariable.Type.STRING.predicate()), createURI(paramNameToURI("category"))));
    // User-defined variable
    patterns.addArg(new StatementPatternNode(new VarNode("var2"), createURI(ApiTemplate.OutputVariable.Type.URI.predicate()), createConstant("@somedata")));
    // User-defined path variable
    patterns.addArg(new StatementPatternNode(new VarNode("var3"), createURI(ApiTemplate.OutputVariable.Type.ITEM.predicate()), createConstant("item/@wikibase_id")));
    // Variable with ordinal
    patterns.addArg(new StatementPatternNode(new VarNode("var4"), createURI(ApiTemplate.OutputVariable.Type.ORDINAL.predicate()), createConstant("goat")));
    ServiceNode serviceNode = new ServiceNode(createConstant("test"), patterns);
    List<OutputVariable> outputs = template.getOutputVars(serviceNode);
    assertThat(outputs.size(), equalTo(4));
    // Pre-defined variable
    OutputVariable var = outputs.get(0);
    assertThat(var.getName(), equalTo("somevar"));
    assertThat(var.getPath(), equalTo("@title"));
    assertFalse(var.isOrdinal());
    // User-defined variable
    var = outputs.get(1);
    assertThat(var.getName(), equalTo("var2"));
    assertThat(var.getPath(), equalTo("@somedata"));
    assertTrue(var.isURI());
    assertFalse(var.isOrdinal());
    assertThat(var.getURI("http://test.com/"), instanceOf(URI.class));
    // URI keeps the case
    assertThat(var.getURI("http://test.com/test").toString(), endsWith("test"));
    // User-defined variable which is an item
    var = outputs.get(2);
    assertThat(var.getName(), equalTo("var3"));
    assertThat(var.getPath(), equalTo("item/@wikibase_id"));
    assertTrue(var.isURI());
    assertFalse(var.isOrdinal());
    assertThat(var.getURI("test"), instanceOf(URI.class));
    // T172642: Item URIs will be uppercased
    assertThat(var.getURI("test").toString(), endsWith("TEST"));
    // Ordinal
    var = outputs.get(3);
    assertThat(var.getName(), equalTo("var4"));
    assertThat(var.getPath(), equalTo("."));
    assertFalse(var.isURI());
    assertTrue(var.isOrdinal());
}
Also used : VarNode(com.bigdata.rdf.sparql.ast.VarNode) ServiceNode(com.bigdata.rdf.sparql.ast.service.ServiceNode) JsonNode(com.fasterxml.jackson.databind.JsonNode) JoinGroupNode(com.bigdata.rdf.sparql.ast.JoinGroupNode) StatementPatternNode(com.bigdata.rdf.sparql.ast.StatementPatternNode) URI(org.openrdf.model.URI) MWApiServiceFactory.paramNameToURI(org.wikidata.query.rdf.blazegraph.mwapi.MWApiServiceFactory.paramNameToURI) OutputVariable(org.wikidata.query.rdf.blazegraph.mwapi.ApiTemplate.OutputVariable) Test(org.junit.Test)

Aggregations

JoinGroupNode (com.bigdata.rdf.sparql.ast.JoinGroupNode)9 StatementPatternNode (com.bigdata.rdf.sparql.ast.StatementPatternNode)8 BOp (com.bigdata.bop.BOp)3 BigdataValueFactory (com.bigdata.rdf.model.BigdataValueFactory)3 DummyConstantNode (com.bigdata.rdf.sparql.ast.DummyConstantNode)3 ServiceNode (com.bigdata.rdf.sparql.ast.service.ServiceNode)3 IVariable (com.bigdata.bop.IVariable)2 ConstantNode (com.bigdata.rdf.sparql.ast.ConstantNode)2 QueryRoot (com.bigdata.rdf.sparql.ast.QueryRoot)2 TermNode (com.bigdata.rdf.sparql.ast.TermNode)2 VarNode (com.bigdata.rdf.sparql.ast.VarNode)2 AbstractTripleStore (com.bigdata.rdf.store.AbstractTripleStore)2 Vocabulary (com.bigdata.rdf.vocab.Vocabulary)2 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 URIImpl (org.openrdf.model.impl.URIImpl)2 SubqueryBase (com.bigdata.rdf.sparql.ast.SubqueryBase)1 BigdataServiceCall (com.bigdata.rdf.sparql.ast.service.BigdataServiceCall)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 List (java.util.List)1