Search in sources :

Example 1 with IVariableOrConstant

use of com.bigdata.bop.IVariableOrConstant in project wikidata-query-rdf by wikimedia.

the class MWApiServiceCall method getRequestParams.

/**
 * Get parameter string from binding.
 */
public Map<String, String> getRequestParams(IBindingSet binding) {
    // Add fixed params
    final Map<String, String> params = new HashMap<>(template.getFixedParams());
    // Resolve variable params
    for (Map.Entry<String, IVariableOrConstant> term : inputVars.entrySet()) {
        String value;
        IV boundValue = null;
        if (term.getValue() != null) {
            boundValue = (IV) term.getValue().get(binding);
        }
        if (boundValue == null) {
            // try default
            value = template.getInputDefault(term.getKey());
            if (value != null && value.isEmpty()) {
                // Empty default means omit if not supplied, and it's ok
                continue;
            }
        } else {
            value = boundValue.stringValue();
        }
        if (value == null) {
            if (template.isRequiredParameter(term.getKey())) {
                throw new IllegalArgumentException("Could not find binding for parameter " + term.getKey());
            } else {
                continue;
            }
        }
        params.put(term.getKey(), value);
    }
    return params;
}
Also used : IVariableOrConstant(com.bigdata.bop.IVariableOrConstant) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) NamedNodeMap(org.w3c.dom.NamedNodeMap) IV(com.bigdata.rdf.internal.IV)

Example 2 with IVariableOrConstant

use of com.bigdata.bop.IVariableOrConstant in project wikidata-query-rdf by wikimedia.

the class MWApiServiceFactory method getRequiredBound.

@Override
public Set<IVariable<?>> getRequiredBound(final ServiceNode serviceNode) {
    ServiceParams params = serviceParamsFromNode(serviceNode);
    ApiTemplate api = getServiceTemplate(params);
    Map<String, IVariableOrConstant> potentialVars = api.getInputVars(params);
    // Extract params that have variables linked to them
    return potentialVars.entrySet().stream().filter(entry -> entry.getValue() != null && entry.getValue().isVar()).map(entry -> (IVariable<?>) entry.getValue()).collect(ImmutableSet.toImmutableSet());
}
Also used : IVariableOrConstant(com.bigdata.bop.IVariableOrConstant) URIImpl(org.openrdf.model.impl.URIImpl) Ontology(org.wikidata.query.rdf.common.uri.Ontology) LoggerFactory(org.slf4j.LoggerFactory) AbstractServiceFactory(com.bigdata.rdf.sparql.ast.eval.AbstractServiceFactory) BigdataServiceCall(com.bigdata.rdf.sparql.ast.service.BigdataServiceCall) IVariableOrConstant(com.bigdata.bop.IVariableOrConstant) BigdataNativeServiceOptions(com.bigdata.rdf.sparql.ast.service.BigdataNativeServiceOptions) Objects.requireNonNull(java.util.Objects.requireNonNull) Map(java.util.Map) URI(org.openrdf.model.URI) Path(java.nio.file.Path) GraphPatternGroup(com.bigdata.rdf.sparql.ast.GraphPatternGroup) StatementPatternNode(com.bigdata.rdf.sparql.ast.StatementPatternNode) Logger(org.slf4j.Logger) ImmutableSet(com.google.common.collect.ImmutableSet) MalformedURLException(java.net.MalformedURLException) Files(java.nio.file.Files) ServiceRegistry(com.bigdata.rdf.sparql.ast.service.ServiceRegistry) Set(java.util.Set) IOException(java.io.IOException) Reader(java.io.Reader) ServiceParams(com.bigdata.rdf.sparql.ast.eval.ServiceParams) IGroupMemberNode(com.bigdata.rdf.sparql.ast.IGroupMemberNode) StandardCharsets(java.nio.charset.StandardCharsets) BD(com.bigdata.rdf.store.BD) ServiceNode(com.bigdata.rdf.sparql.ast.service.ServiceNode) Paths(java.nio.file.Paths) TermNode(com.bigdata.rdf.sparql.ast.TermNode) IVariable(com.bigdata.bop.IVariable) Timer(com.codahale.metrics.Timer) Mediawiki(org.wikidata.query.rdf.common.uri.Mediawiki) IServiceOptions(com.bigdata.rdf.sparql.ast.service.IServiceOptions) ServiceCallCreateParams(com.bigdata.rdf.sparql.ast.service.ServiceCallCreateParams) ServiceParams(com.bigdata.rdf.sparql.ast.eval.ServiceParams) IVariable(com.bigdata.bop.IVariable)

Example 3 with IVariableOrConstant

use of com.bigdata.bop.IVariableOrConstant 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

IVariableOrConstant (com.bigdata.bop.IVariableOrConstant)3 ServiceParams (com.bigdata.rdf.sparql.ast.eval.ServiceParams)2 Map (java.util.Map)2 IVariable (com.bigdata.bop.IVariable)1 IV (com.bigdata.rdf.internal.IV)1 GraphPatternGroup (com.bigdata.rdf.sparql.ast.GraphPatternGroup)1 IGroupMemberNode (com.bigdata.rdf.sparql.ast.IGroupMemberNode)1 StatementPatternNode (com.bigdata.rdf.sparql.ast.StatementPatternNode)1 TermNode (com.bigdata.rdf.sparql.ast.TermNode)1 VarNode (com.bigdata.rdf.sparql.ast.VarNode)1 AbstractServiceFactory (com.bigdata.rdf.sparql.ast.eval.AbstractServiceFactory)1 BigdataNativeServiceOptions (com.bigdata.rdf.sparql.ast.service.BigdataNativeServiceOptions)1 BigdataServiceCall (com.bigdata.rdf.sparql.ast.service.BigdataServiceCall)1 IServiceOptions (com.bigdata.rdf.sparql.ast.service.IServiceOptions)1 ServiceCallCreateParams (com.bigdata.rdf.sparql.ast.service.ServiceCallCreateParams)1 ServiceNode (com.bigdata.rdf.sparql.ast.service.ServiceNode)1 ServiceRegistry (com.bigdata.rdf.sparql.ast.service.ServiceRegistry)1 BD (com.bigdata.rdf.store.BD)1 Timer (com.codahale.metrics.Timer)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1