Search in sources :

Example 6 with StatementPatternNode

use of com.bigdata.rdf.sparql.ast.StatementPatternNode 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)

Example 7 with StatementPatternNode

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

the class LabelService method findResolutions.

/**
 * Create the resolutions list from the service call parameters.
 */
static List<Resolution> findResolutions(final ServiceNode params) {
    JoinGroupNode g = (JoinGroupNode) params.getGraphPattern();
    List<Resolution> resolutions = new ArrayList<>(g.args().size());
    for (BOp st : g.args()) {
        StatementPatternNode sn = (StatementPatternNode) st;
        if (sn.s().isConstant() && BD.SERVICE_PARAM.equals(sn.s().getValue())) {
            // skip service params
            continue;
        }
        resolutions.add(new Resolution(sn));
    }
    return resolutions;
}
Also used : BOp(com.bigdata.bop.BOp) ArrayList(java.util.ArrayList) JoinGroupNode(com.bigdata.rdf.sparql.ast.JoinGroupNode) StatementPatternNode(com.bigdata.rdf.sparql.ast.StatementPatternNode)

Example 8 with StatementPatternNode

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

the class ApiTemplate method getOutputVars.

/**
 * Create map of output variables from template and service params.
 */
public List<OutputVariable> getOutputVars(final ServiceNode serviceNode) {
    List<OutputVariable> vars = new ArrayList<>(outputVars.size());
    final GraphPatternGroup<IGroupMemberNode> group = serviceNode.getGraphPattern();
    requireNonNull(serviceNode, "Group node is null?");
    String prefix = paramNameToURI("").stringValue();
    group.iterator().forEachRemaining(node -> {
        // ?variable wikibase:output "x/path"
        if (node instanceof StatementPatternNode) {
            final StatementPatternNode sp = (StatementPatternNode) node;
            if (sp.s().isVariable() && sp.o().isConstant() && sp.p().isConstant()) {
                for (OutputVariable.Type varType : OutputVariable.Type.values()) {
                    if (varType.predicate.equals(sp.p().getValue())) {
                        IVariable v = (IVariable) sp.s().getValueExpression();
                        if (varType == ORDINAL) {
                            // Ordinal values ignore the object
                            vars.add(new OutputVariable(varType, v, "."));
                            break;
                        }
                        IV value = sp.o().getValueExpression().get();
                        if (value.isURI()) {
                            String paramName = value.stringValue().substring(prefix.length());
                            vars.add(new OutputVariable(varType, v, outputVars.get(paramName)));
                        } else {
                            vars.add(new OutputVariable(varType, v, value.stringValue()));
                        }
                        break;
                    }
                }
            }
        }
    });
    return vars;
}
Also used : IGroupMemberNode(com.bigdata.rdf.sparql.ast.IGroupMemberNode) IVariable(com.bigdata.bop.IVariable) ArrayList(java.util.ArrayList) StatementPatternNode(com.bigdata.rdf.sparql.ast.StatementPatternNode) IV(com.bigdata.rdf.internal.IV)

Example 9 with StatementPatternNode

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

the class GeoBoxService 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 "inRectangle" .
    newGroup.addArg(new StatementPatternNode(searchVar, new DummyConstantNode(vf.asValue(GeoSpatial.SEARCH)), new DummyConstantNode(vf.createLiteral(GeoFunction.IN_RECTANGLE.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)))));
    if (serviceParams.contains(NE_PARAM)) {
        // ?var geo:spatialRectangleNorthEast ?ne .
        newGroup.addArg(new StatementPatternNode(searchVar, new DummyConstantNode(vf.asValue(GeoSpatial.SPATIAL_RECTANGLE_NORTH_EAST)), getParam(serviceParams, NE_PARAM)));
        // ?var geo:spatialRectangleNorthEast ?sw .
        newGroup.addArg(new StatementPatternNode(searchVar, new DummyConstantNode(vf.asValue(GeoSpatial.SPATIAL_RECTANGLE_SOUTH_WEST)), getParam(serviceParams, SW_PARAM)));
    } else if (serviceParams.contains(EAST_PARAM)) {
        final TermNode east = getParam(serviceParams, EAST_PARAM);
        final TermNode west = getParam(serviceParams, WEST_PARAM);
        if (east instanceof ConstantNode && west instanceof ConstantNode) {
            // Easy case - both constants
            final WikibasePoint eastWP = pointFromIV(((ConstantNode) east).getValue().getIV());
            final WikibasePoint westWP = pointFromIV(((ConstantNode) west).getValue().getIV());
            final GeoUtils.Box box = new GeoUtils.Box(eastWP, westWP);
            TermNode ne;
            TermNode sw;
            if (box.switched()) {
                ne = new DummyConstantNode(vf.asValue(vf.createLiteral(box.northEast().toString(), new URIImpl(GeoSparql.WKT_LITERAL))));
                sw = new DummyConstantNode(vf.asValue(vf.createLiteral(box.southWest().toString(), new URIImpl(GeoSparql.WKT_LITERAL))));
            } else {
                ne = east;
                sw = west;
            }
            // ?var geo:spatialRectangleNorthEast ?ne .
            newGroup.addArg(new StatementPatternNode(searchVar, new DummyConstantNode(vf.asValue(GeoSpatial.SPATIAL_RECTANGLE_NORTH_EAST)), ne));
            // ?var geo:spatialRectangleNorthEast ?sw .
            newGroup.addArg(new StatementPatternNode(searchVar, new DummyConstantNode(vf.asValue(GeoSpatial.SPATIAL_RECTANGLE_SOUTH_WEST)), sw));
        } else {
            // Hard case - non-constants
            // Add dummy var to the node
            serviceParams.add(WRAP_PARAM, VarNode.freshVarNode());
            // ?var geo:spatialRectangleNorthEast ?ne .
            newGroup.addArg(new StatementPatternNode(searchVar, new DummyConstantNode(vf.asValue(GeoSpatial.SPATIAL_RECTANGLE_NORTH_EAST)), getSubstituteVar(east)));
            // ?var geo:spatialRectangleNorthEast ?sw .
            newGroup.addArg(new StatementPatternNode(searchVar, new DummyConstantNode(vf.asValue(GeoSpatial.SPATIAL_RECTANGLE_SOUTH_WEST)), getSubstituteVar(west)));
        }
    } else {
        throw new IllegalArgumentException("Box corner parameters are required");
    }
    // ?var geo:locationValue ?location .
    newGroup.addArg(new StatementPatternNode(searchVar, new DummyConstantNode(vf.asValue(GeoSpatial.LOCATION_VALUE)), locationVar));
    // ?var geo:coordSystem "2" .
    newGroup.addArg(new StatementPatternNode(searchVar, new DummyConstantNode(vf.asValue(GeoSpatial.COORD_SYSTEM)), getGlobeNode(vf, serviceParams)));
    return newGroup;
}
Also used : Vocabulary(com.bigdata.rdf.vocab.Vocabulary) AbstractTripleStore(com.bigdata.rdf.store.AbstractTripleStore) DummyConstantNode(com.bigdata.rdf.sparql.ast.DummyConstantNode) URIImpl(org.openrdf.model.impl.URIImpl) TermNode(com.bigdata.rdf.sparql.ast.TermNode) WikibasePoint(org.wikidata.query.rdf.common.WikibasePoint) BigdataValueFactory(com.bigdata.rdf.model.BigdataValueFactory) DummyConstantNode(com.bigdata.rdf.sparql.ast.DummyConstantNode) ConstantNode(com.bigdata.rdf.sparql.ast.ConstantNode) JoinGroupNode(com.bigdata.rdf.sparql.ast.JoinGroupNode) StatementPatternNode(com.bigdata.rdf.sparql.ast.StatementPatternNode)

Example 10 with StatementPatternNode

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

the class EmptyLabelServiceOptimizer method restoreExtracted.

/**
 * Restore extracted statement from label service node.
 */
@SuppressWarnings("unchecked")
private boolean restoreExtracted(ServiceNode service) {
    boolean found = false;
    JoinGroupNode g = (JoinGroupNode) service.getGraphPattern();
    final List<StatementPatternNode> extractedList = (List<StatementPatternNode>) service.annotations().get(LabelServiceExtractOptimizer.EXTRACTOR_ANNOTATION);
    if (extractedList != null && !extractedList.isEmpty()) {
        for (StatementPatternNode st : extractedList) {
            g.addArg(st);
        }
        found = true;
    }
    service.annotations().remove(LabelServiceExtractOptimizer.EXTRACTOR_ANNOTATION);
    return found;
}
Also used : JoinGroupNode(com.bigdata.rdf.sparql.ast.JoinGroupNode) List(java.util.List) StatementPatternNode(com.bigdata.rdf.sparql.ast.StatementPatternNode)

Aggregations

StatementPatternNode (com.bigdata.rdf.sparql.ast.StatementPatternNode)13 JoinGroupNode (com.bigdata.rdf.sparql.ast.JoinGroupNode)8 IVariable (com.bigdata.bop.IVariable)4 TermNode (com.bigdata.rdf.sparql.ast.TermNode)4 BOp (com.bigdata.bop.BOp)3 ConstantNode (com.bigdata.rdf.sparql.ast.ConstantNode)3 VarNode (com.bigdata.rdf.sparql.ast.VarNode)3 ServiceNode (com.bigdata.rdf.sparql.ast.service.ServiceNode)3 ArrayList (java.util.ArrayList)3 IV (com.bigdata.rdf.internal.IV)2 BigdataValueFactory (com.bigdata.rdf.model.BigdataValueFactory)2 DummyConstantNode (com.bigdata.rdf.sparql.ast.DummyConstantNode)2 IGroupMemberNode (com.bigdata.rdf.sparql.ast.IGroupMemberNode)2 QueryRoot (com.bigdata.rdf.sparql.ast.QueryRoot)2 AbstractTripleStore (com.bigdata.rdf.store.AbstractTripleStore)2 Vocabulary (com.bigdata.rdf.vocab.Vocabulary)2 Test (org.junit.Test)2 URI (org.openrdf.model.URI)2 URIImpl (org.openrdf.model.impl.URIImpl)2 SubqueryBase (com.bigdata.rdf.sparql.ast.SubqueryBase)1