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;
}
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());
}
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"));
}
Aggregations