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