summaryrefslogtreecommitdiffstats
path: root/searchlib/src/main
diff options
context:
space:
mode:
authorJon Bratseth <bratseth@gmail.com>2022-02-16 16:43:20 +0100
committerJon Bratseth <bratseth@gmail.com>2022-02-16 16:43:20 +0100
commite25d913b884339afc4f8e3073e4e4b795e55d930 (patch)
tree408e9fded165a07fae202fd691f6f2864680ac63 /searchlib/src/main
parent6f99bd502132cd378124a40060ac1d74d54f5e92 (diff)
Resolve slice dimension
Diffstat (limited to 'searchlib/src/main')
-rw-r--r--searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/Arguments.java8
-rwxr-xr-xsearchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/ReferenceNode.java4
-rw-r--r--searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/SerializationContext.java33
-rw-r--r--searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/TensorFunctionNode.java30
4 files changed, 56 insertions, 19 deletions
diff --git a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/Arguments.java b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/Arguments.java
index fe22a5b1267..e770e6ac038 100644
--- a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/Arguments.java
+++ b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/Arguments.java
@@ -15,7 +15,9 @@ import java.util.List;
* @author bratseth
*/
public final class Arguments implements Serializable {
+
public static final Arguments EMPTY = new Arguments();
+
private final ImmutableList<ExpressionNode> expressions;
public Arguments() {
@@ -47,9 +49,9 @@ public final class Arguments implements Serializable {
/** Evaluate all arguments in this */
public Value[] evaluate(Context context) {
- Value[] values=new Value[expressions.size()];
- for (int i=0; i<expressions.size(); i++)
- values[i]=expressions.get(i).evaluate(context);
+ Value[] values = new Value[expressions.size()];
+ for (int i = 0; i < expressions.size(); i++)
+ values[i] = expressions.get(i).evaluate(context);
return values;
}
diff --git a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/ReferenceNode.java b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/ReferenceNode.java
index 93b8b8aca5e..8ac1829b16b 100755
--- a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/ReferenceNode.java
+++ b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/ReferenceNode.java
@@ -42,9 +42,11 @@ public final class ReferenceNode extends CompositeNode {
return reference.name();
}
+ @Override
public int hashCode() {
return reference.hashCode();
}
+
/** Returns the arguments, never null */
public Arguments getArguments() { return reference.arguments(); }
@@ -118,7 +120,7 @@ public final class ReferenceNode extends CompositeNode {
throw new IllegalArgumentException(reference + " is invalid", e);
}
if (type == null)
- throw new IllegalArgumentException("Unknown feature '" + toString() + "'");
+ throw new IllegalArgumentException("Unknown feature '" + this + "'");
return type;
}
diff --git a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/SerializationContext.java b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/SerializationContext.java
index 535ad013caf..1f3203f2e35 100644
--- a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/SerializationContext.java
+++ b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/SerializationContext.java
@@ -4,13 +4,16 @@ package com.yahoo.searchlib.rankingexpression.rule;
import com.google.common.collect.ImmutableMap;
import com.yahoo.searchlib.rankingexpression.ExpressionFunction;
import com.yahoo.searchlib.rankingexpression.RankingExpression;
+import com.yahoo.searchlib.rankingexpression.Reference;
import com.yahoo.tensor.TensorType;
+import com.yahoo.tensor.evaluation.TypeContext;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
+import java.util.Optional;
/**
* Context needed to serialize an expression to a string. This has the lifetime of a single serialization
@@ -22,6 +25,8 @@ public class SerializationContext extends FunctionReferenceContext {
/** Serialized form of functions indexed by name */
private final Map<String, String> serializedFunctions;
+ private final Optional<TypeContext<Reference>> typeContext;
+
/** Create a context for a single serialization task */
public SerializationContext() {
this(Collections.emptyList());
@@ -29,7 +34,7 @@ public class SerializationContext extends FunctionReferenceContext {
/** Create a context for a single serialization task */
public SerializationContext(Collection<ExpressionFunction> functions) {
- this(functions, Collections.emptyMap(), new LinkedHashMap<>());
+ this(functions, Collections.emptyMap(), Optional.empty(), new LinkedHashMap<>());
}
/** Create a context for a single serialization task */
@@ -39,7 +44,13 @@ public class SerializationContext extends FunctionReferenceContext {
/** Create a context for a single serialization task */
public SerializationContext(Collection<ExpressionFunction> functions, Map<String, String> bindings) {
- this(functions, bindings, new LinkedHashMap<>());
+ this(functions, bindings, Optional.empty(), new LinkedHashMap<>());
+ }
+
+ /** Create a context for a single serialization task */
+ public SerializationContext(Collection<ExpressionFunction> functions, Map<String, String> bindings,
+ TypeContext<Reference> typeContext) {
+ this(functions, bindings, Optional.of(typeContext), new LinkedHashMap<>());
}
/**
@@ -47,14 +58,19 @@ public class SerializationContext extends FunctionReferenceContext {
*
* @param functions the functions of this
* @param bindings the arguments of this
+ * @param typeContext the type context of this: Serialization may depend on type resolution
* @param serializedFunctions a cache of serializedFunctions - the ownership of this map
* is <b>transferred</b> to this and will be modified in it
*/
- public SerializationContext(Collection<ExpressionFunction> functions, Map<String, String> bindings,
+ private SerializationContext(Collection<ExpressionFunction> functions, Map<String, String> bindings,
+ Optional<TypeContext<Reference>> typeContext,
Map<String, String> serializedFunctions) {
- this(toMap(functions), bindings, serializedFunctions);
+ this(toMap(functions), bindings, typeContext, serializedFunctions);
}
+ /** Returns the type context of this, if it is able to resolve types. */
+ public Optional<TypeContext<Reference>> typeContext() { return typeContext; }
+
private static Map<String, ExpressionFunction> toMap(Collection<ExpressionFunction> list) {
Map<String,ExpressionFunction> mapBuilder = new HashMap<>();
for (ExpressionFunction function : list)
@@ -70,9 +86,16 @@ public class SerializationContext extends FunctionReferenceContext {
* @param serializedFunctions a cache of serializedFunctions - the ownership of this map
* is <b>transferred</b> to this and will be modified in it
*/
+ public SerializationContext(Map<String, ExpressionFunction> functions, Map<String, String> bindings,
+ Map<String, String> serializedFunctions) {
+ this(functions, bindings, Optional.empty(), serializedFunctions);
+ }
+
public SerializationContext(Map<String,ExpressionFunction> functions, Map<String, String> bindings,
+ Optional<TypeContext<Reference>> typeContext,
Map<String, String> serializedFunctions) {
super(functions, bindings);
+ this.typeContext = typeContext;
this.serializedFunctions = serializedFunctions;
}
@@ -88,7 +111,7 @@ public class SerializationContext extends FunctionReferenceContext {
serializedFunctions.put(name, expressionString);
}
- /** Adds the serialization of the an argument type to a function */
+ /** Adds the serialization of the argument type to a function */
public void addArgumentTypeSerialization(String functionName, String argumentName, TensorType type) {
serializedFunctions.put("rankingExpression(" + functionName + ")." + argumentName + ".type", type.toString());
}
diff --git a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/TensorFunctionNode.java b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/TensorFunctionNode.java
index d873963bb6e..52d54c9163e 100644
--- a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/TensorFunctionNode.java
+++ b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/TensorFunctionNode.java
@@ -168,8 +168,8 @@ public class TensorFunctionNode extends CompositeNode {
}
@Override
- public String toString(ToStringContext c) {
- ToStringContext outermost = c;
+ public String toString(ToStringContext<Reference> c) {
+ ToStringContext<Reference> outermost = c;
while (outermost.parent() != null)
outermost = outermost.parent();
@@ -251,15 +251,17 @@ public class TensorFunctionNode extends CompositeNode {
}
@Override
- public String toString(ToStringContext c) {
- ToStringContext outermost = c;
+ public String toString(ToStringContext<Reference> c) {
+ ToStringContext<Reference> outermost = c;
while (outermost.parent() != null)
outermost = outermost.parent();
if (outermost instanceof ExpressionToStringContext) {
ExpressionToStringContext context = (ExpressionToStringContext)outermost;
return expression.toString(new StringBuilder(),
- new ExpressionToStringContext(context.wrappedSerializationContext, c, context.path, context.parent),
+ new ExpressionToStringContext(context.wrappedSerializationContext, c,
+ context.path,
+ context.parent),
context.path,
context.parent)
.toString();
@@ -281,9 +283,9 @@ public class TensorFunctionNode extends CompositeNode {
* to add more context, we need to keep track of both these contexts here separately and map between
* contexts as seen in the toString methods of the function classes above.
*/
- private static class ExpressionToStringContext extends SerializationContext implements ToStringContext {
+ private static class ExpressionToStringContext extends SerializationContext implements ToStringContext<Reference> {
- private final ToStringContext wrappedToStringContext;
+ private final ToStringContext<Reference> wrappedToStringContext;
private final SerializationContext wrappedSerializationContext;
private final Deque<String> path;
private final CompositeNode parent;
@@ -297,7 +299,7 @@ public class TensorFunctionNode extends CompositeNode {
}
ExpressionToStringContext(SerializationContext wrappedSerializationContext,
- ToStringContext wrappedToStringContext,
+ ToStringContext<Reference> wrappedToStringContext,
Deque<String> path,
CompositeNode parent) {
this.wrappedSerializationContext = wrappedSerializationContext;
@@ -328,6 +330,12 @@ public class TensorFunctionNode extends CompositeNode {
/** Returns a function or null if it isn't defined in this context */
public ExpressionFunction getFunction(String name) { return wrappedSerializationContext.getFunction(name); }
+ /** Returns the type context of this, or empty if none. */
+ @Override
+ public Optional<TypeContext<Reference>> typeContext() {
+ return wrappedSerializationContext.typeContext();
+ }
+
/** @deprecated Use {@link #getFunctions()} instead */
@SuppressWarnings("removal")
@Deprecated(forRemoval = true, since = "7")
@@ -335,9 +343,10 @@ public class TensorFunctionNode extends CompositeNode {
return ImmutableMap.copyOf(wrappedSerializationContext.getFunctions());
}
- @Override protected Map<String, ExpressionFunction> getFunctions() { return wrappedSerializationContext.getFunctions(); }
+ @Override
+ protected Map<String, ExpressionFunction> getFunctions() { return wrappedSerializationContext.getFunctions(); }
- public ToStringContext parent() { return wrappedToStringContext; }
+ public ToStringContext<Reference> parent() { return wrappedToStringContext; }
/** Returns the resolution of an identifier, or null if it isn't defined in this context */
@Override
@@ -361,6 +370,7 @@ public class TensorFunctionNode extends CompositeNode {
SerializationContext serializationContext = new SerializationContext(getFunctions(), null, serializedFunctions());
return new ExpressionToStringContext(serializationContext, null, path, parent);
}
+
}
/** Turns an EvaluationContext into a Context */