summaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/searchdefinition/FeatureNames.java
diff options
context:
space:
mode:
authorJon Bratseth <jonbratseth@yahoo.com>2018-02-22 16:20:48 +0100
committerGitHub <noreply@github.com>2018-02-22 16:20:48 +0100
commit1791ba9fc169c821bee826ed39d9a42ffa03def0 (patch)
tree4601457cb3cdf0cc2a4b84efc44668748edfd8f3 /config-model/src/main/java/com/yahoo/searchdefinition/FeatureNames.java
parent7358ce6a49eedf51b3761a855921c58a5f813321 (diff)
Revert "Revert "Bratseth/typecheck all 3""
Diffstat (limited to 'config-model/src/main/java/com/yahoo/searchdefinition/FeatureNames.java')
-rw-r--r--config-model/src/main/java/com/yahoo/searchdefinition/FeatureNames.java110
1 files changed, 23 insertions, 87 deletions
diff --git a/config-model/src/main/java/com/yahoo/searchdefinition/FeatureNames.java b/config-model/src/main/java/com/yahoo/searchdefinition/FeatureNames.java
index dd03cb8b2a7..dc59d9cb3e5 100644
--- a/config-model/src/main/java/com/yahoo/searchdefinition/FeatureNames.java
+++ b/config-model/src/main/java/com/yahoo/searchdefinition/FeatureNames.java
@@ -5,11 +5,10 @@
*/
package com.yahoo.searchdefinition;
-import java.util.Arrays;
-import java.util.List;
+import com.yahoo.searchlib.rankingexpression.Reference;
+
import java.util.Optional;
import java.util.regex.Pattern;
-import java.util.stream.Collectors;
/**
* Utility methods for query, document and constant rank feature names
@@ -20,85 +19,16 @@ public class FeatureNames {
private static final Pattern identifierRegexp = Pattern.compile("[A-Za-z0-9_][A-Za-z0-9_-]*");
- /**
- * <p>Returns the given query, document or constant feature in canonical form.
- * A feature name consists of a feature type name (query, attribute or constant),
- * followed by one argument enclosed in quotes.
- * The argument may be an identifier or any string single or double quoted.</p>
- *
- * <p>Argument string values may not contain comma, single quote nor double quote characters.</p>
- *
- * <p><i>The canonical form use no quotes for arguments which are identifiers, and double quotes otherwise.</i></p>
- *
- * <p>Note that the above definition is not true for features in general, which accept any ranking expression
- * as argument.</p>
- *
- * @throws IllegalArgumentException if the feature name is not valid
- */
- // Note that this implementation is more general than what is described above:
- // It accepts any number of arguments and an optional output
- public static String canonicalize(String feature) {
- return canonicalizeIfValid(feature).orElseThrow(() ->
- new IllegalArgumentException("A feature name must be on the form query(name), attribute(name) or " +
- "constant(name), but was '" + feature + "'"
- ));
- }
-
- /**
- * Canonicalizes the given argument as in canonicalize, but returns empty instead of throwing an exception if
- * the argument is not a valid feature
- */
- public static Optional<String> canonicalizeIfValid(String feature) {
- int startParenthesis = feature.indexOf('(');
- if (startParenthesis < 0)
- return Optional.empty();
- int endParenthesis = feature.lastIndexOf(')');
- String featureType = feature.substring(0, startParenthesis);
- if ( ! ( featureType.equals("query") || featureType.equals("attribute") || featureType.equals("constant")))
- return Optional.empty();
- if (startParenthesis < 1) return Optional.of(feature); // No arguments
- if (endParenthesis < startParenthesis)
- return Optional.empty();
- String argumentString = feature.substring(startParenthesis + 1, endParenthesis);
- List<String> canonicalizedArguments =
- Arrays.stream(argumentString.split(","))
- .map(FeatureNames::canonicalizeArgument)
- .collect(Collectors.toList());
- return Optional.of(featureType + "(" +
- canonicalizedArguments.stream().collect(Collectors.joining(",")) +
- feature.substring(endParenthesis));
- }
-
- /** Canomicalizes a single argument */
- private static String canonicalizeArgument(String argument) {
- if (argument.startsWith("'")) {
- if ( ! argument.endsWith("'"))
- throw new IllegalArgumentException("Feature arguments starting by a single quote " +
- "must end by a single quote, but was \"" + argument + "\"");
- argument = argument.substring(1, argument.length() - 1);
- }
- if (argument.startsWith("\"")) {
- if ( ! argument.endsWith("\""))
- throw new IllegalArgumentException("Feature arguments starting by a double quote " +
- "must end by a double quote, but was '" + argument + "'");
- argument = argument.substring(1, argument.length() - 1);
- }
- if (identifierRegexp.matcher(argument).matches())
- return argument;
- else
- return "\"" + argument + "\"";
- }
-
- public static String asConstantFeature(String constantName) {
- return canonicalize("constant(\"" + constantName + "\")");
+ public static Reference asConstantFeature(String constantName) {
+ return Reference.simple("constant", quoteIfNecessary(constantName));
}
- public static String asAttributeFeature(String attributeName) {
- return canonicalize("attribute(\"" + attributeName + "\")");
+ public static Reference asAttributeFeature(String attributeName) {
+ return Reference.simple("attribute", quoteIfNecessary(attributeName));
}
- public static String asQueryFeature(String propertyName) {
- return canonicalize("query(\"" + propertyName + "\")");
+ public static Reference asQueryFeature(String propertyName) {
+ return Reference.simple("query", quoteIfNecessary(propertyName));
}
/**
@@ -106,15 +36,21 @@ public class FeatureNames {
* or empty if it is not a valid query, attribute or constant feature name
*/
public static Optional<String> argumentOf(String feature) {
- return canonicalizeIfValid(feature).map(f -> {
- int startParenthesis = f.indexOf("(");
- int endParenthesis = f.indexOf(")");
- String possiblyQuotedArgument = f.substring(startParenthesis + 1, endParenthesis);
- if (possiblyQuotedArgument.startsWith("\""))
- return possiblyQuotedArgument.substring(1, possiblyQuotedArgument.length() - 1);
- else
- return possiblyQuotedArgument;
- });
+ Optional<Reference> reference = Reference.simple(feature);
+ if ( ! reference.isPresent()) return Optional.empty();
+ if ( ! ( reference.get().name().equals("attribute") ||
+ reference.get().name().equals("constant") ||
+ reference.get().name().equals("query")))
+ return Optional.empty();
+
+ return Optional.of(reference.get().arguments().expressions().get(0).toString());
+ }
+
+ private static String quoteIfNecessary(String s) {
+ if (identifierRegexp.matcher(s).matches())
+ return s;
+ else
+ return "\"" + s + "\"";
}
}