Search in sources :

Example 1 with VarNode

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

the class EmptyLabelServiceOptimizer method addResolutionIfSuffix.

/**
 * Add the join group to resolve a variable if it matches a suffix,
 * returning true if it matched, false otherwise.
 */
@SuppressFBWarnings(value = "OCP_OVERLY_CONCRETE_PARAMETER", justification = "Using AST2BOpContext makes sense since it is the only type that will ever be passed")
private boolean addResolutionIfSuffix(AST2BOpContext ctx, JoinGroupNode g, String suffix, URI labelType, IVariable<IV> iVar) {
    if (!iVar.getName().endsWith(suffix)) {
        return false;
    }
    String source = iVar.getName().substring(0, iVar.getName().length() - suffix.length());
    IConstant<IV> labelTypeAsConstant = ctx.getAbstractTripleStore().getVocabulary().getConstant(labelType);
    g.addArg(new StatementPatternNode(new VarNode(source), new ConstantNode(labelTypeAsConstant), new VarNode(iVar)));
    return true;
}
Also used : VarNode(com.bigdata.rdf.sparql.ast.VarNode) ConstantNode(com.bigdata.rdf.sparql.ast.ConstantNode) StatementPatternNode(com.bigdata.rdf.sparql.ast.StatementPatternNode) IV(com.bigdata.rdf.internal.IV) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 2 with VarNode

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

the class GeoBoxService method getSubstituteVar.

/**
 * Generate substitute node for the term.
 *
 * @param term
 *            Term to substitute
 * @return
 */
private TermNode getSubstituteVar(TermNode term) {
    if (term.isVariable() && ((IVariable<?>) term.getValueExpression()).isAnonymous()) {
        throw new IllegalArgumentException("Anonymous vars not supported as box corners");
    }
    final VarNode newnode = VarNode.freshVarNode();
    term.setProperty(VAR_ANNOTATION, newnode);
    return newnode;
}
Also used : VarNode(com.bigdata.rdf.sparql.ast.VarNode)

Example 3 with VarNode

use of com.bigdata.rdf.sparql.ast.VarNode 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 4 with VarNode

use of com.bigdata.rdf.sparql.ast.VarNode 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 5 with VarNode

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

the class ApiTemplateUnitTest method testServiceInput.

@Test
public void testServiceInput() throws Exception {
    JsonNode json = parseJson(JSON_CONFIG);
    ApiTemplate template = ApiTemplate.fromJSON(json);
    Map<String, String> fixed = template.getFixedParams();
    // Fixed params
    assertThat(fixed, hasKey("action"));
    assertThat(fixed, hasKey("prop"));
    assertThat(fixed, not(hasKey("cllimit")));
    assertThat(fixed, not(hasKey("titles")));
    // Input params with default
    assertThat(template.getInputDefault("cllimit"), equalTo("500"));
    assertThat(template.getInputDefault("cldir"), equalTo(""));
    assertNull(template.getInputDefault("titles"));
    // Bound params
    ServiceParams serviceParams = new ServiceParams();
    serviceParams.add(paramNameToURI("titles"), createConstant("sometitle"));
    serviceParams.add(paramNameToURI("name"), new VarNode("somevar"));
    serviceParams.add(paramNameToURI("ducks"), createConstant("extraParam"));
    Map<String, IVariableOrConstant> input = template.getInputVars(serviceParams);
    assertThat(input, hasKey("titles"));
    assertThat(input, hasKey("name"));
    assertThat(input, hasKey("cllimit"));
    assertThat(input, hasKey("ducks"));
    assertThat(input, hasKey("cldir"));
    // Bound constant
    assertTrue(input.get("titles").isConstant());
    assertTrue(input.get("ducks").isConstant());
    // Bound var
    assertTrue(input.get("name").isVar());
    assertThat(input.get("name").getName(), equalTo("somevar"));
    // Unbound vars have nulls
    assertNull(input.get("cllimit"));
    assertNull(input.get("cldir"));
}
Also used : VarNode(com.bigdata.rdf.sparql.ast.VarNode) IVariableOrConstant(com.bigdata.bop.IVariableOrConstant) ServiceParams(com.bigdata.rdf.sparql.ast.eval.ServiceParams) JsonNode(com.fasterxml.jackson.databind.JsonNode) Test(org.junit.Test)

Aggregations

VarNode (com.bigdata.rdf.sparql.ast.VarNode)5 StatementPatternNode (com.bigdata.rdf.sparql.ast.StatementPatternNode)3 Test (org.junit.Test)3 JoinGroupNode (com.bigdata.rdf.sparql.ast.JoinGroupNode)2 ServiceNode (com.bigdata.rdf.sparql.ast.service.ServiceNode)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 IVariable (com.bigdata.bop.IVariable)1 IVariableOrConstant (com.bigdata.bop.IVariableOrConstant)1 IV (com.bigdata.rdf.internal.IV)1 ConstantNode (com.bigdata.rdf.sparql.ast.ConstantNode)1 ServiceParams (com.bigdata.rdf.sparql.ast.eval.ServiceParams)1 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1 URI (org.openrdf.model.URI)1 OutputVariable (org.wikidata.query.rdf.blazegraph.mwapi.ApiTemplate.OutputVariable)1 MWApiServiceFactory.paramNameToURI (org.wikidata.query.rdf.blazegraph.mwapi.MWApiServiceFactory.paramNameToURI)1