diff options
author | Jon Bratseth <bratseth@oath.com> | 2018-02-17 16:32:51 +0100 |
---|---|---|
committer | Jon Bratseth <bratseth@oath.com> | 2018-02-17 16:32:51 +0100 |
commit | d7e1e3b5b24b0f9f0e3dfcc6d1e37d442f1de4e8 (patch) | |
tree | 6520dc0340d605bba01bfbd7e3c9d3fd59bd4f5a | |
parent | 80f0982ad44485879cc98aeb6e7bde3bd6b3bbb3 (diff) |
Static type check reference parameters
11 files changed, 31 insertions, 44 deletions
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 f31dba74e9e..0b312d40815 100644 --- a/config-model/src/main/java/com/yahoo/searchdefinition/MapEvaluationTypeContext.java +++ b/config-model/src/main/java/com/yahoo/searchdefinition/MapEvaluationTypeContext.java @@ -28,7 +28,7 @@ import java.util.Optional; * * @author bratseth */ -public class MapEvaluationTypeContext extends FunctionReferenceContext implements TypeContext { +public class MapEvaluationTypeContext extends FunctionReferenceContext implements TypeContext<Reference> { private final Map<Reference, TensorType> featureTypes = new HashMap<>(); @@ -43,19 +43,12 @@ public class MapEvaluationTypeContext extends FunctionReferenceContext implement this.featureTypes.putAll(featureTypes); } - public void setType(Name name, TensorType type) { - // TODO: Use a type parameter if we do this both here and in getType ... - if ( ! (name instanceof Reference)) - throw new IllegalArgumentException("Not expecting unstructured names here"); - featureTypes.put((Reference)name, type); + public void setType(Reference reference, TensorType type) { + featureTypes.put(reference, type); } @Override - public TensorType getType(Name name) { - if ( ! (name instanceof Reference)) - throw new IllegalArgumentException("Not expecting unstructured names here"); - Reference reference = (Reference)name; - + public TensorType getType(Reference reference) { Optional<String> binding = boundIdentifier(reference); if (binding.isPresent()) { try { 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 486affe9371..ee5952d9aea 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.searchlib.rankingexpression.Reference; import com.yahoo.tensor.TensorType; import java.util.Arrays; @@ -82,8 +83,8 @@ public class ArrayContext extends AbstractArrayContext implements Cloneable { } @Override - public TensorType getType(Name name) { - Integer index = nameToIndex().get(name.toString()); + public TensorType getType(Reference reference) { + Integer index = nameToIndex().get(reference.toString()); 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 9779e01bf51..4102d4078e6 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 @@ -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.searchlib.rankingexpression.evaluation; +import com.yahoo.searchlib.rankingexpression.Reference; import com.yahoo.searchlib.rankingexpression.rule.Arguments; import com.yahoo.searchlib.rankingexpression.rule.ExpressionNode; import com.yahoo.tensor.Tensor; @@ -14,7 +15,7 @@ import java.util.stream.Collectors; * * @author bratseth */ -public abstract class Context implements EvaluationContext { +public abstract class Context implements EvaluationContext<Reference> { /** * Returns the value of a simple variable name. 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 01b8bffe995..0004036da4b 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.searchlib.rankingexpression.Reference; import com.yahoo.tensor.TensorType; /** @@ -68,7 +69,9 @@ public class DoubleOnlyArrayContext extends AbstractArrayContext { } @Override - public TensorType getType(Name name) { return TensorType.empty; } + public TensorType getType(Reference reference) { + return TensorType.empty; // Double only + } /** 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 c7679ea9e55..4ef24d60bba 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,7 @@ // 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.searchlib.rankingexpression.Reference; import com.yahoo.tensor.TensorType; import java.util.Collections; @@ -15,7 +16,7 @@ import java.util.Set; */ public class MapContext extends Context { - private Map<String, Value> bindings = new HashMap<>(); + private Map<String, Value> bindings = new HashMap<>(); // TODO: Change String to Reference private boolean frozen = false; @@ -42,8 +43,8 @@ public class MapContext extends Context { /** Returns the type of the given value key, or null if it is not bound. */ @Override - public TensorType getType(Name key) { - Value value = bindings.get(key); + public TensorType getType(Reference key) { + Value value = bindings.get(key.toString()); if (value == null) return null; return value.type(); } diff --git a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/MapTypeContext.java b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/MapTypeContext.java index 4ce7dd561e2..985878cfd66 100644 --- a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/MapTypeContext.java +++ b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/MapTypeContext.java @@ -1,6 +1,7 @@ // Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. 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.searchlib.rankingexpression.Reference; import com.yahoo.tensor.TensorType; import com.yahoo.tensor.evaluation.TypeContext; @@ -13,20 +14,20 @@ import java.util.Map; * * @author bratseth */ -public class MapTypeContext implements TypeContext { +public class MapTypeContext implements TypeContext<Reference> { - private final Map<Name, TensorType> featureTypes = new HashMap<>(); + private final Map<Reference, TensorType> featureTypes = new HashMap<>(); - public void setType(Name name, TensorType type) { - featureTypes.put(name, type); + public void setType(Reference reference, TensorType type) { + featureTypes.put(reference, type); } @Override - public TensorType getType(Name name) { - return featureTypes.get(name); + public TensorType getType(Reference reference) { + return featureTypes.get(reference); } /** Returns an unmodifiable map of the bindings in this */ - public Map<Name, TensorType> bindings() { return Collections.unmodifiableMap(featureTypes); } + public Map<Reference, TensorType> bindings() { return Collections.unmodifiableMap(featureTypes); } } diff --git a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/EvaluationTypeContext.java b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/EvaluationTypeContext.java deleted file mode 100644 index 3807fbe2207..00000000000 --- a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/EvaluationTypeContext.java +++ /dev/null @@ -1,11 +0,0 @@ -// 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.rule; - -import com.yahoo.tensor.TensorType; -import com.yahoo.tensor.evaluation.TypeContext; - -public interface EvaluationTypeContext extends TypeContext { - - TensorType getType(String name, Arguments arguments, String output); - -} diff --git a/searchlib/src/test/java/com/yahoo/searchlib/rankingexpression/rule/ArgumentsTestCase.java b/searchlib/src/test/java/com/yahoo/searchlib/rankingexpression/rule/ArgumentsTestCase.java index 867331e99ce..303135888d8 100644 --- a/searchlib/src/test/java/com/yahoo/searchlib/rankingexpression/rule/ArgumentsTestCase.java +++ b/searchlib/src/test/java/com/yahoo/searchlib/rankingexpression/rule/ArgumentsTestCase.java @@ -9,13 +9,13 @@ import java.util.Collections; import static org.junit.Assert.*; /** - * @author <a href="mailto:simon@yahoo-inc.com">Simon Thoresen</a> + * @author Simon Thoresen */ public class ArgumentsTestCase { @Test public void requireThatAccessorsWork() { - Arguments args = new Arguments(null); + Arguments args = new Arguments(); assertTrue(args.expressions().isEmpty()); args = new Arguments(Collections.<ExpressionNode>emptyList()); diff --git a/vespajlib/src/main/java/com/yahoo/tensor/evaluation/EvaluationContext.java b/vespajlib/src/main/java/com/yahoo/tensor/evaluation/EvaluationContext.java index 3fb94f1251b..8a969180113 100644 --- a/vespajlib/src/main/java/com/yahoo/tensor/evaluation/EvaluationContext.java +++ b/vespajlib/src/main/java/com/yahoo/tensor/evaluation/EvaluationContext.java @@ -10,7 +10,7 @@ import com.yahoo.tensor.Tensor; * @author bratseth */ @Beta -public interface EvaluationContext extends TypeContext { +public interface EvaluationContext<NAMETYPE extends TypeContext.Name> extends TypeContext<NAMETYPE> { /** Returns the tensor bound to this name, or null if none */ Tensor getTensor(String name); diff --git a/vespajlib/src/main/java/com/yahoo/tensor/evaluation/MapEvaluationContext.java b/vespajlib/src/main/java/com/yahoo/tensor/evaluation/MapEvaluationContext.java index 078f2e39815..74457a163fd 100644 --- a/vespajlib/src/main/java/com/yahoo/tensor/evaluation/MapEvaluationContext.java +++ b/vespajlib/src/main/java/com/yahoo/tensor/evaluation/MapEvaluationContext.java @@ -11,12 +11,10 @@ import java.util.HashMap; * @author bratseth */ @Beta -public class MapEvaluationContext implements EvaluationContext { +public class MapEvaluationContext implements EvaluationContext<TypeContext.Name> { private final java.util.Map<String, Tensor> bindings = new HashMap<>(); - static MapEvaluationContext empty() { return new MapEvaluationContext(); } - public void put(String name, Tensor tensor) { bindings.put(name, tensor); } @Override diff --git a/vespajlib/src/main/java/com/yahoo/tensor/evaluation/TypeContext.java b/vespajlib/src/main/java/com/yahoo/tensor/evaluation/TypeContext.java index ecb3a801324..3674b373db0 100644 --- a/vespajlib/src/main/java/com/yahoo/tensor/evaluation/TypeContext.java +++ b/vespajlib/src/main/java/com/yahoo/tensor/evaluation/TypeContext.java @@ -8,7 +8,7 @@ import com.yahoo.tensor.TensorType; * * @author bratseth */ -public interface TypeContext { +public interface TypeContext<NAMETYPE extends TypeContext.Name> { /** * Returns the type of the tensor with this name. @@ -16,7 +16,7 @@ public interface TypeContext { * @return returns the type of the tensor which will be returned by calling getTensor(name) * or null if getTensor will return null. */ - TensorType getType(Name name); + TensorType getType(NAMETYPE name); /** A name which is just a string. Names are value objects. */ class Name { |