diff options
author | Jon Bratseth <bratseth@oath.com> | 2018-01-31 11:13:51 +0100 |
---|---|---|
committer | Jon Bratseth <bratseth@oath.com> | 2018-01-31 11:13:51 +0100 |
commit | a44edeba9f38c38c431d7b9b6e1ac454e2a0e610 (patch) | |
tree | 21600936cfe396492965764911652b49b4c22731 /searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation | |
parent | 9c4ba9bf5b96b8c62a9b8c5a6c20a9175c698b70 (diff) |
Verify macros
Diffstat (limited to 'searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation')
12 files changed, 61 insertions, 74 deletions
diff --git a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/ArrayContext.java b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/ArrayContext.java index a4d3c111356..5f8daa69ecf 100644 --- a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/ArrayContext.java +++ b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/ArrayContext.java @@ -2,6 +2,7 @@ package com.yahoo.searchlib.rankingexpression.evaluation; import com.yahoo.searchlib.rankingexpression.RankingExpression; +import com.yahoo.tensor.TensorType; import java.util.Arrays; @@ -81,7 +82,7 @@ public class ArrayContext extends AbstractArrayContext implements Cloneable { } @Override - public ValueType getType(String name) { + public TensorType getType(String name) { Integer index = nameToIndex().get(name); if (index == null) return null; return values[index].type(); diff --git a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/Context.java b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/Context.java index a1e79df95e3..861f9565d66 100644 --- a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/Context.java +++ b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/Context.java @@ -4,7 +4,6 @@ package com.yahoo.searchlib.rankingexpression.evaluation; import com.yahoo.searchlib.rankingexpression.rule.Arguments; import com.yahoo.searchlib.rankingexpression.rule.ExpressionNode; import com.yahoo.tensor.Tensor; -import com.yahoo.tensor.TensorType; import com.yahoo.tensor.evaluation.EvaluationContext; import java.util.Set; @@ -25,21 +24,10 @@ public abstract class Context implements EvaluationContext { */ public abstract Value get(String name); - /** Returns the type of the value of the given variable as a tensor type, or null if there is no such variable */ - @Override - public TensorType getTensorType(String name) { - ValueType type = getType(name); - if (type == null) return null; - return type.tensorType(); - } - /** Returns a variable as a tensor */ @Override public Tensor getTensor(String name) { return get(name).asTensor(); } - /** Returns the type of the value of the given variable, or null if there is no such variable */ - public abstract ValueType getType(String name); - /** * <p>Returns the value of a <i>structured variable</i> on the form * <code>name(argument*)(.output)?</code>, where <i>argument</i> is any diff --git a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/DoubleCompatibleValue.java b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/DoubleCompatibleValue.java index c85a8f1c7e1..3ac11cff0cb 100644 --- a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/DoubleCompatibleValue.java +++ b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/DoubleCompatibleValue.java @@ -4,6 +4,7 @@ package com.yahoo.searchlib.rankingexpression.evaluation; import com.yahoo.searchlib.rankingexpression.rule.Function; import com.yahoo.searchlib.rankingexpression.rule.TruthOperator; import com.yahoo.tensor.Tensor; +import com.yahoo.tensor.TensorType; /** * A value which acts as a double in numerical context. @@ -13,7 +14,7 @@ import com.yahoo.tensor.Tensor; public abstract class DoubleCompatibleValue extends Value { @Override - public ValueType type() { return ValueType.doubleType(); } + public TensorType type() { return TensorType.empty; } @Override public boolean hasDouble() { return true; } diff --git a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/DoubleOnlyArrayContext.java b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/DoubleOnlyArrayContext.java index 34cd75df9cb..0625e8506cc 100644 --- a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/DoubleOnlyArrayContext.java +++ b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/DoubleOnlyArrayContext.java @@ -2,6 +2,7 @@ package com.yahoo.searchlib.rankingexpression.evaluation; import com.yahoo.searchlib.rankingexpression.RankingExpression; +import com.yahoo.tensor.TensorType; /** * A variant of an array context variant which supports faster binding of variables but slower lookup @@ -67,7 +68,7 @@ public class DoubleOnlyArrayContext extends AbstractArrayContext { } @Override - public ValueType getType(String name) { return ValueType.doubleType(); } + public TensorType getType(String name) { return TensorType.empty; } /** Perform a slow lookup by name */ @Override diff --git a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/MapContext.java b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/MapContext.java index 2672fe6cd8e..39efe641f26 100644 --- a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/MapContext.java +++ b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/MapContext.java @@ -1,6 +1,8 @@ // Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.searchlib.rankingexpression.evaluation; +import com.yahoo.tensor.TensorType; + import java.util.Collections; import java.util.HashMap; import java.util.Map; @@ -13,7 +15,7 @@ import java.util.Set; */ public class MapContext extends Context { - private Map<String,Value> bindings=new HashMap<>(); + private Map<String, Value> bindings = new HashMap<>(); private boolean frozen = false; @@ -21,16 +23,6 @@ public class MapContext extends Context { } /** - * Freezes this. - * Returns this for convenience. - */ - public MapContext freeze() { - if ( ! frozen) - bindings = Collections.unmodifiableMap(bindings); - return this; - } - - /** * Creates a map context from a map. * The ownership of the map is transferred to this - it cannot be further modified by the caller. * All the Values of the map will be frozen. @@ -41,27 +33,32 @@ public class MapContext extends Context { boundValue.freeze(); } + /** + * Freezes this. + * Returns this for convenience. + */ + public MapContext freeze() { + if ( ! frozen) + bindings = Collections.unmodifiableMap(bindings); + return this; + } + /** Returns the type of the given value key, or null if it is not bound. */ @Override - public ValueType getType(String key) { + public TensorType getType(String key) { Value value = bindings.get(key); if (value == null) return null; return value.type(); } - /** - * Returns the value of a key. 0 is returned if the given key is not bound in this. - */ + /** Returns the value of a key. 0 is returned if the given key is not bound in this. */ @Override public Value get(String key) { return bindings.getOrDefault(key, DoubleValue.zero); } /** - * Sets the value of a key. - * The value is frozen by this. - * - * @since 5.1.5 + * Sets the value of a key.The value is frozen by this. */ @Override public void put(String key,Value value) { diff --git a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/StringValue.java b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/StringValue.java index 874b41ec3e1..c60507310f1 100644 --- a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/StringValue.java +++ b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/StringValue.java @@ -5,6 +5,7 @@ import com.yahoo.javacc.UnicodeUtilities; import com.yahoo.searchlib.rankingexpression.rule.Function; import com.yahoo.searchlib.rankingexpression.rule.TruthOperator; import com.yahoo.tensor.Tensor; +import com.yahoo.tensor.TensorType; /** * A string value. @@ -29,7 +30,7 @@ public class StringValue extends Value { } @Override - public ValueType type() { return ValueType.doubleType(); } + public TensorType type() { return TensorType.empty; } /** Returns the hashcode of this, to enable strings to be encoded (with reasonable safely) as doubles for optimization */ @Override diff --git a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/TensorValue.java b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/TensorValue.java index f1c65dc79d3..c6e456f285d 100644 --- a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/TensorValue.java +++ b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/TensorValue.java @@ -6,6 +6,7 @@ import com.yahoo.searchlib.rankingexpression.rule.Function; import com.yahoo.searchlib.rankingexpression.rule.TruthOperator; import com.yahoo.tensor.Tensor; import com.yahoo.tensor.TensorAddress; +import com.yahoo.tensor.TensorType; /** * A Value containing a tensor. @@ -25,7 +26,7 @@ public class TensorValue extends Value { } @Override - public ValueType type() { return ValueType.doubleType(); } + public TensorType type() { return TensorType.empty; } @Override public double asDouble() { diff --git a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/TypeMapContext.java b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/TypeMapContext.java new file mode 100644 index 00000000000..f2c4ca58f6d --- /dev/null +++ b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/TypeMapContext.java @@ -0,0 +1,27 @@ +package com.yahoo.searchlib.rankingexpression.evaluation;// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +import com.yahoo.tensor.TensorType; +import com.yahoo.tensor.evaluation.TypeContext; + +import java.util.HashMap; +import java.util.Map; + +/** + * A context which only contains type information. + * + * @author bratseth + */ +public class TypeMapContext implements TypeContext { + + private final Map<String, TensorType> featureTypes = new HashMap<>(); + + public void setType(String name, TensorType type) { + featureTypes.put(name, type); + } + + @Override + public TensorType getType(String name) { + return featureTypes.get(name); + } + +} diff --git a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/Value.java b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/Value.java index 856bfb3638d..59d2d95b879 100644 --- a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/Value.java +++ b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/Value.java @@ -13,14 +13,14 @@ import com.yahoo.tensor.TensorType; * Concrete subclasses of this provides implementations of these methods or throws * UnsupportedOperationException if the operation is not supported. * - * @author bratseth + * @author bratseth */ public abstract class Value { private boolean frozen=false; /** Returns the type of this value */ - public abstract ValueType type(); + public abstract TensorType type(); /** Returns this value as a double, or throws UnsupportedOperationException if it cannot be represented as a double */ public abstract double asDouble(); diff --git a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/ValueType.java b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/ValueType.java deleted file mode 100644 index 046ad7861ef..00000000000 --- a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/ValueType.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.yahoo.searchlib.rankingexpression.evaluation;// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. - -import com.yahoo.tensor.TensorType; - -/** - * The type of a ranking expression value - either a double or a tensor. - * - * @author bratseth - */ -public class ValueType { - - private static final ValueType doubleValueType = new ValueType(TensorType.empty); - - private final TensorType tensorType; - - private ValueType(TensorType tensorType) { - this.tensorType = tensorType; - } - - /** Returns true if this is the double type */ - public boolean isDouble() { return tensorType.rank() == 0; } - - /** The type of this as a tensor type. The double type is the empty tensor type (rank 0) */ - public TensorType tensorType() { return tensorType; } - - /** Returns the type representing a double */ - public static ValueType doubleType() { return doubleValueType; } - - /** Returns a type representing the given tensor type */ - public static ValueType of(TensorType type) { return new ValueType(type); } - -} diff --git a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/gbdtoptimization/GBDTForestNode.java b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/gbdtoptimization/GBDTForestNode.java index b4e126f69e0..8ee4cdbf297 100644 --- a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/gbdtoptimization/GBDTForestNode.java +++ b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/gbdtoptimization/GBDTForestNode.java @@ -4,10 +4,11 @@ package com.yahoo.searchlib.rankingexpression.evaluation.gbdtoptimization; import com.yahoo.searchlib.rankingexpression.evaluation.Context; import com.yahoo.searchlib.rankingexpression.evaluation.DoubleValue; import com.yahoo.searchlib.rankingexpression.evaluation.Value; -import com.yahoo.searchlib.rankingexpression.evaluation.ValueType; import com.yahoo.searchlib.rankingexpression.rule.CompositeNode; import com.yahoo.searchlib.rankingexpression.rule.ExpressionNode; import com.yahoo.searchlib.rankingexpression.rule.SerializationContext; +import com.yahoo.tensor.TensorType; +import com.yahoo.tensor.evaluation.TypeContext; import java.util.Deque; @@ -25,7 +26,7 @@ public class GBDTForestNode extends ExpressionNode { } @Override - public final ValueType type(Context context) { return ValueType.doubleType(); } + public final TensorType type(TypeContext context) { return TensorType.empty; } @Override public final Value evaluate(Context context) { diff --git a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/gbdtoptimization/GBDTNode.java b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/gbdtoptimization/GBDTNode.java index f085194a7df..aac635b2545 100644 --- a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/gbdtoptimization/GBDTNode.java +++ b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/gbdtoptimization/GBDTNode.java @@ -4,10 +4,11 @@ package com.yahoo.searchlib.rankingexpression.evaluation.gbdtoptimization; import com.yahoo.searchlib.rankingexpression.evaluation.Context; import com.yahoo.searchlib.rankingexpression.evaluation.DoubleValue; import com.yahoo.searchlib.rankingexpression.evaluation.Value; -import com.yahoo.searchlib.rankingexpression.evaluation.ValueType; import com.yahoo.searchlib.rankingexpression.rule.CompositeNode; import com.yahoo.searchlib.rankingexpression.rule.ExpressionNode; import com.yahoo.searchlib.rankingexpression.rule.SerializationContext; +import com.yahoo.tensor.TensorType; +import com.yahoo.tensor.evaluation.TypeContext; import java.util.Deque; @@ -50,7 +51,7 @@ public final class GBDTNode extends ExpressionNode { public final double[] values() { return values; } @Override - public final ValueType type(Context context) { return ValueType.doubleType(); } + public final TensorType type(TypeContext context) { return TensorType.empty; } @Override public final Value evaluate(Context context) { |