Github user rvesse commented on a diff in the pull request:
https://github.com/apache/jena/pull/449#discussion_r207150857
--- Diff:
jena-arq/src/main/java/org/apache/jena/query/ParameterizedSparqlString.java ---
@@ -1734,4 +1739,250 @@ public String toString() {
}
}
+
+ /**
+ * Assign a VALUES varName with a multiple items.<br>
+ * Can be used to assign multiple values to a single variable or single
+ * value to multiple variables (if using a List) in the SPARQL
query.<br>
+ * See setGroupedValues to assign multiple values to multiple
variables.<br>
+ * Using "var" with list(prop_A, obj_A) on query "VALUES (?p ?o)
{?var}"
+ * would produce "VALUES (?p ?o) {(prop_A obj_A)}".
+ *
+ *
+ * @param varName
+ * @param items
+ */
+ public void setValues(String varName, Collection<? extends RDFNode>
items) {
+ items.forEach(item -> validateParameterValue(item.asNode()));
+ this.valuesReplacements.put(varName, new ValueReplacement(varName,
items));
+ }
+
+ /**
+ * Assign a VALUES varName with a single item.<br>
+ * Using "var" with Literal obj_A on query "VALUES ?o {?var}" would
produce
+ * "VALUES ?o {obj_A}".
+ *
+ * @param varName
+ * @param item
+ */
+ public void setValues(String varName, RDFNode item) {
+ setValues(varName, Arrays.asList(item));
+ }
+
+ /**
+ * **
+ * Sets a map of VALUES varNames and their items.<br>
+ * Can be used to assign multiple values to a single variable or single
+ * value to multiple variables (if using a List) in the SPARQL
query.<br>
+ * See setGroupedValues to assign multiple values to multiple
variables.
+ *
+ * @param itemsMap
+ */
+ public void setValues(Map<String, Collection<? extends RDFNode>>
itemsMap) {
+ itemsMap.forEach(this::setValues);
+ }
+
+ /**
+ * Allocate multiple lists of variables to a single VALUES varName.<br>
+ * Using "vars" with list(list(prop_A, obj_A), list(prop_B, obj_B)) on
query
+ * "VALUES (?p ?o) {?vars}" would produce "VALUES (?p ?o) {(prop_A
obj_A)
+ * (prop_B obj_B)}".
+ *
+ * @param varName
+ * @param groupedItems
+ */
+ public void setGroupedValues(String varName, Collection<List<? extends
RDFNode>> groupedItems) {
+ groupedItems.forEach(collection -> collection.forEach(item ->
validateParameterValue(item.asNode())));
+ this.valuesReplacements.put(varName, new ValueReplacement(varName,
groupedItems, true));
+ }
+
+ private String applyValues(String command) {
+
+ for (ValueReplacement valueReplacement :
valuesReplacements.values()) {
+ command = valueReplacement.apply(command);
+ }
+ return command;
+ }
+
+ private static final String VALUES_KEYWORD = "values";
+
+ protected static String[] extractTargetVars(String command, String
varName) {
+ String[] targetVars;
+
+ int varIndex = command.indexOf(varName);
+ if (varIndex > -1) {
+ String subCmd = command.substring(0, varIndex).toLowerCase();
//Truncate the command at the varName. Lowercase to search both types of values.
+ int valuesIndex = subCmd.lastIndexOf(VALUES_KEYWORD);
+ int bracesIndex = subCmd.lastIndexOf("{");
+ String vars = command.substring(valuesIndex +
VALUES_KEYWORD.length(), bracesIndex);
+ targetVars = vars.replaceAll("[(?)]", "").trim().split(" ");
+ } else {
+ targetVars = new String[]{};
+ }
+ return targetVars;
+ }
+
+ protected static boolean checkParenthesis(String command, String
varName) {
--- End diff --
Per @afs's comments it might be simpler to just always include the
parenthesis since the un-paranthesised form is simply a shortcut for the single
variable case
---