summaryrefslogtreecommitdiffstats
path: root/config-model
diff options
context:
space:
mode:
authorJon Bratseth <bratseth@oath.com>2018-02-24 19:46:20 +0100
committerJon Bratseth <bratseth@oath.com>2018-02-24 19:46:20 +0100
commit3777557b0069026d0ba5ace3ff66fca6c7354d92 (patch)
treecacf774dacfcb0f53f6df23b9131930c53ec1520 /config-model
parent81ad685e7517f9d56bca42d85eaf4c7ce24a9ba9 (diff)
Refactor
Diffstat (limited to 'config-model')
-rw-r--r--config-model/src/main/java/com/yahoo/searchdefinition/FeatureNames.java7
-rw-r--r--config-model/src/main/java/com/yahoo/searchdefinition/MapEvaluationTypeContext.java45
-rw-r--r--config-model/src/main/java/com/yahoo/searchdefinition/expressiontransforms/ConstantTensorTransformer.java3
3 files changed, 13 insertions, 42 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 dc59d9cb3e5..9bfa263f942 100644
--- a/config-model/src/main/java/com/yahoo/searchdefinition/FeatureNames.java
+++ b/config-model/src/main/java/com/yahoo/searchdefinition/FeatureNames.java
@@ -31,6 +31,13 @@ public class FeatureNames {
return Reference.simple("query", quoteIfNecessary(propertyName));
}
+ /** Returns true if the given reference is an attribute, constant or query feature */
+ public static boolean isSimpleFeature(Reference reference) {
+ if ( ! reference.isSimple()) return false;
+ String name = reference.name();
+ return name.equals("attribute") || name.equals("constant") || name.equals("query");
+ }
+
/**
* Returns the single argument of the given feature name, without any quotes,
* or empty if it is not a valid query, attribute or constant feature name
diff --git a/config-model/src/main/java/com/yahoo/searchdefinition/MapEvaluationTypeContext.java b/config-model/src/main/java/com/yahoo/searchdefinition/MapEvaluationTypeContext.java
index cec4296196d..8c7398b3dde 100644
--- a/config-model/src/main/java/com/yahoo/searchdefinition/MapEvaluationTypeContext.java
+++ b/config-model/src/main/java/com/yahoo/searchdefinition/MapEvaluationTypeContext.java
@@ -68,9 +68,9 @@ public class MapEvaluationTypeContext extends FunctionReferenceContext implement
}
// A reference to an attribute, query or constant feature?
- if (isSimpleFeature(reference)) {
+ if (FeatureNames.isSimpleFeature(reference)) {
// The argument may be a local identifier bound to the actual value
- String argument = simpleArgument(reference.arguments()).get();
+ String argument = reference.simpleArgument().get();
reference = Reference.simple(reference.name(), bindings.getOrDefault(argument, argument));
return featureTypes.getOrDefault(reference, defaultTypeOf(reference));
}
@@ -97,7 +97,7 @@ public class MapEvaluationTypeContext extends FunctionReferenceContext implement
* Returns the default type for this simple feature, or nullif it does not have a default
*/
public TensorType defaultTypeOf(Reference reference) {
- if ( ! isSimpleFeature(reference))
+ if ( ! FeatureNames.isSimpleFeature(reference))
throw new IllegalArgumentException("This can only be called for simple references, not " + reference);
if (reference.name().equals("query")) // we do not require all query features to be declared, only non-doubles
return TensorType.empty;
@@ -114,43 +114,6 @@ public class MapEvaluationTypeContext extends FunctionReferenceContext implement
return Optional.ofNullable(bindings.get(reference.name()));
}
- /**
- * Return whether the reference (discarding the output) is a simple feature
- * ("attribute(name)", "constant(name)" or "query(name)").
- * We disregard the output because all outputs under a simple feature have the same type.
- */
- // TODO: Move simpleness without knowing the names to Reference, move the name-knowing to FeatureNames
- public static boolean isSimpleFeature(Reference reference) {
- Optional<String> argument = simpleArgument(reference.arguments());
- if ( ! argument.isPresent()) return false;
- return reference.name().equals("attribute") ||
- reference.name().equals("constant") ||
- reference.name().equals("query");
- }
-
- /**
- * If these arguments contains one simple argument string, it is returned.
- * Otherwise null is returned.
- */
- private static Optional<String> simpleArgument(Arguments arguments) {
- if (arguments.expressions().size() != 1) return Optional.empty();
- ExpressionNode argument = arguments.expressions().get(0);
-
- if (argument instanceof ReferenceNode) {
- ReferenceNode refArgument = (ReferenceNode) argument;
-
- if ( ! refArgument.reference().isIdentifier()) return Optional.empty();
-
- return Optional.of(refArgument.getName());
- }
- else if (argument instanceof NameNode) {
- return Optional.of(((NameNode) argument).getValue());
- }
- else {
- return Optional.empty();
- }
- }
-
private Optional<ExpressionFunction> functionInvocation(Reference reference) {
if (reference.output() != null) return Optional.empty();
ExpressionFunction function = functions().get(reference.name());
@@ -171,7 +134,7 @@ public class MapEvaluationTypeContext extends FunctionReferenceContext implement
throw new IllegalArgumentException(reference.name() + " must have one or two arguments");
ExpressionNode arg0 = reference.arguments().expressions().get(0);
- if ( ! ( arg0 instanceof ReferenceNode) || ! isSimpleFeature(((ReferenceNode)arg0).reference()))
+ if ( ! ( arg0 instanceof ReferenceNode) || ! FeatureNames.isSimpleFeature(((ReferenceNode)arg0).reference()))
throw new IllegalArgumentException("The first argument of " + reference.name() +
" must be a simple feature, not " + arg0);
diff --git a/config-model/src/main/java/com/yahoo/searchdefinition/expressiontransforms/ConstantTensorTransformer.java b/config-model/src/main/java/com/yahoo/searchdefinition/expressiontransforms/ConstantTensorTransformer.java
index 2eb778c4b10..4ebf4550b47 100644
--- a/config-model/src/main/java/com/yahoo/searchdefinition/expressiontransforms/ConstantTensorTransformer.java
+++ b/config-model/src/main/java/com/yahoo/searchdefinition/expressiontransforms/ConstantTensorTransformer.java
@@ -1,6 +1,7 @@
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.searchdefinition.expressiontransforms;
+import com.yahoo.searchdefinition.FeatureNames;
import com.yahoo.searchdefinition.MapEvaluationTypeContext;
import com.yahoo.searchlib.rankingexpression.evaluation.TensorValue;
import com.yahoo.searchlib.rankingexpression.evaluation.Value;
@@ -35,7 +36,7 @@ public class ConstantTensorTransformer extends ExpressionTransformer<RankProfile
}
private ExpressionNode transformFeature(ReferenceNode node, RankProfileTransformContext context) {
- if ( ! node.getArguments().isEmpty() && ! MapEvaluationTypeContext.isSimpleFeature(node.reference())) {
+ if ( ! node.getArguments().isEmpty() && ! FeatureNames.isSimpleFeature(node.reference())) {
return transformArguments(node, context);
} else {
return transformConstantReference(node, context);