summaryrefslogtreecommitdiffstats
path: root/vespajlib/src
diff options
context:
space:
mode:
authorJon Bratseth <bratseth@oath.com>2018-01-31 11:13:51 +0100
committerJon Bratseth <bratseth@oath.com>2018-01-31 11:13:51 +0100
commita44edeba9f38c38c431d7b9b6e1ac454e2a0e610 (patch)
tree21600936cfe396492965764911652b49b4c22731 /vespajlib/src
parent9c4ba9bf5b96b8c62a9b8c5a6c20a9175c698b70 (diff)
Verify macros
Diffstat (limited to 'vespajlib/src')
-rw-r--r--vespajlib/src/main/java/com/yahoo/tensor/TensorType.java2
-rw-r--r--vespajlib/src/main/java/com/yahoo/tensor/evaluation/EvaluationContext.java11
-rw-r--r--vespajlib/src/main/java/com/yahoo/tensor/evaluation/MapEvaluationContext.java2
-rw-r--r--vespajlib/src/main/java/com/yahoo/tensor/evaluation/TypeContext.java18
-rw-r--r--vespajlib/src/main/java/com/yahoo/tensor/evaluation/VariableTensor.java26
-rw-r--r--vespajlib/src/main/java/com/yahoo/tensor/functions/CompositeTensorFunction.java3
-rw-r--r--vespajlib/src/main/java/com/yahoo/tensor/functions/Concat.java12
-rw-r--r--vespajlib/src/main/java/com/yahoo/tensor/functions/ConstantTensor.java3
-rw-r--r--vespajlib/src/main/java/com/yahoo/tensor/functions/Generate.java3
-rw-r--r--vespajlib/src/main/java/com/yahoo/tensor/functions/Join.java3
-rw-r--r--vespajlib/src/main/java/com/yahoo/tensor/functions/Map.java3
-rw-r--r--vespajlib/src/main/java/com/yahoo/tensor/functions/Reduce.java3
-rw-r--r--vespajlib/src/main/java/com/yahoo/tensor/functions/Rename.java3
-rw-r--r--vespajlib/src/main/java/com/yahoo/tensor/functions/TensorFunction.java3
14 files changed, 70 insertions, 25 deletions
diff --git a/vespajlib/src/main/java/com/yahoo/tensor/TensorType.java b/vespajlib/src/main/java/com/yahoo/tensor/TensorType.java
index b396f831de0..5b98a1b4fb5 100644
--- a/vespajlib/src/main/java/com/yahoo/tensor/TensorType.java
+++ b/vespajlib/src/main/java/com/yahoo/tensor/TensorType.java
@@ -19,7 +19,6 @@ import java.util.stream.Collectors;
* A tensor type with its dimensions. This is immutable.
* <p>
* A dimension can be indexed (bound or unbound) or mapped.
- * Currently, we only support tensor types where all dimensions have the same type.
*
* @author geirst
* @author bratseth
@@ -27,6 +26,7 @@ import java.util.stream.Collectors;
@Beta
public class TensorType {
+ /** The empty tensor type - which is the same as a double */
public static final TensorType empty = new TensorType(Collections.emptyList());
/** Sorted list of the dimensions of this */
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 e18b77a0434..3fb94f1251b 100644
--- a/vespajlib/src/main/java/com/yahoo/tensor/evaluation/EvaluationContext.java
+++ b/vespajlib/src/main/java/com/yahoo/tensor/evaluation/EvaluationContext.java
@@ -3,7 +3,6 @@ package com.yahoo.tensor.evaluation;
import com.google.common.annotations.Beta;
import com.yahoo.tensor.Tensor;
-import com.yahoo.tensor.TensorType;
/**
* An evaluation context which is passed down to all nested functions during evaluation.
@@ -11,15 +10,7 @@ import com.yahoo.tensor.TensorType;
* @author bratseth
*/
@Beta
-public interface EvaluationContext {
-
- /**
- * Returns tye type of the tensor with this name.
- *
- * @return returns the type of the tensor which will be returned by calling getTensor(name)
- * or null if getTensor will return null.
- */
- TensorType getTensorType(String name);
+public interface EvaluationContext extends TypeContext {
/** 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 6bdfe8f19b6..9fe6b7d053f 100644
--- a/vespajlib/src/main/java/com/yahoo/tensor/evaluation/MapEvaluationContext.java
+++ b/vespajlib/src/main/java/com/yahoo/tensor/evaluation/MapEvaluationContext.java
@@ -20,7 +20,7 @@ public class MapEvaluationContext implements EvaluationContext {
public void put(String name, Tensor tensor) { bindings.put(name, tensor); }
@Override
- public TensorType getTensorType(String name) {
+ public TensorType getType(String name) {
Tensor tensor = bindings.get(name);
if (tensor == null) return null;
return tensor.type();
diff --git a/vespajlib/src/main/java/com/yahoo/tensor/evaluation/TypeContext.java b/vespajlib/src/main/java/com/yahoo/tensor/evaluation/TypeContext.java
new file mode 100644
index 00000000000..9b2e81f0b6d
--- /dev/null
+++ b/vespajlib/src/main/java/com/yahoo/tensor/evaluation/TypeContext.java
@@ -0,0 +1,18 @@
+package com.yahoo.tensor.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;
+
+/**
+ * @author bratseth
+ */
+public interface TypeContext {
+
+ /**
+ * Returns tye type of the tensor with this name.
+ *
+ * @return returns the type of the tensor which will be returned by calling getTensor(name)
+ * or null if getTensor will return null.
+ */
+ TensorType getType(String name);
+
+}
diff --git a/vespajlib/src/main/java/com/yahoo/tensor/evaluation/VariableTensor.java b/vespajlib/src/main/java/com/yahoo/tensor/evaluation/VariableTensor.java
index 6c149724aca..34beb465d4c 100644
--- a/vespajlib/src/main/java/com/yahoo/tensor/evaluation/VariableTensor.java
+++ b/vespajlib/src/main/java/com/yahoo/tensor/evaluation/VariableTensor.java
@@ -10,6 +10,7 @@ import com.yahoo.tensor.functions.ToStringContext;
import java.util.Collections;
import java.util.List;
+import java.util.Optional;
/**
* A tensor variable name which resolves to a tensor in the context at evaluation time
@@ -20,9 +21,17 @@ import java.util.List;
public class VariableTensor extends PrimitiveTensorFunction {
private final String name;
+ private final Optional<TensorType> requiredType;
public VariableTensor(String name) {
this.name = name;
+ this.requiredType = Optional.empty();
+ }
+
+ /** A variable tensor which must be compatible with the given type */
+ public VariableTensor(String name, TensorType requiredType) {
+ this.name = name;
+ this.requiredType = Optional.of(requiredType);
}
@Override
@@ -35,11 +44,19 @@ public class VariableTensor extends PrimitiveTensorFunction {
public PrimitiveTensorFunction toPrimitive() { return this; }
@Override
- public TensorType type(EvaluationContext context) { return context.getTensorType(name); }
+ public TensorType type(TypeContext context) {
+ TensorType givenType = context.getType(name);
+ if (givenType == null) return null;
+ verifyType(givenType);
+ return givenType;
+ }
@Override
public Tensor evaluate(EvaluationContext context) {
- return context.getTensor(name);
+ Tensor tensor = context.getTensor(name);
+ if (tensor == null) return null;
+ verifyType(tensor.type());
+ return tensor;
}
@Override
@@ -47,4 +64,9 @@ public class VariableTensor extends PrimitiveTensorFunction {
return name;
}
+ private void verifyType(TensorType givenType) {
+ if (requiredType.isPresent() && ! givenType.isAssignableTo(requiredType.get()))
+ throw new IllegalArgumentException("Variable '" + name + "' must be compatible with " +
+ requiredType.get() + " but was " + givenType);
+ }
}
diff --git a/vespajlib/src/main/java/com/yahoo/tensor/functions/CompositeTensorFunction.java b/vespajlib/src/main/java/com/yahoo/tensor/functions/CompositeTensorFunction.java
index 0c43caef05c..2109b730e1a 100644
--- a/vespajlib/src/main/java/com/yahoo/tensor/functions/CompositeTensorFunction.java
+++ b/vespajlib/src/main/java/com/yahoo/tensor/functions/CompositeTensorFunction.java
@@ -5,6 +5,7 @@ import com.google.common.annotations.Beta;
import com.yahoo.tensor.Tensor;
import com.yahoo.tensor.TensorType;
import com.yahoo.tensor.evaluation.EvaluationContext;
+import com.yahoo.tensor.evaluation.TypeContext;
/**
* A composite tensor function is a tensor function which can be expressed (less tersely)
@@ -17,7 +18,7 @@ public abstract class CompositeTensorFunction extends TensorFunction {
/** Finds the type this produces by first converting it to a primitive function */
@Override
- public final TensorType type(EvaluationContext context) { return toPrimitive().type(context); }
+ public final TensorType type(TypeContext context) { return toPrimitive().type(context); }
/** Evaluates this by first converting it to a primitive function */
@Override
diff --git a/vespajlib/src/main/java/com/yahoo/tensor/functions/Concat.java b/vespajlib/src/main/java/com/yahoo/tensor/functions/Concat.java
index cc8067224c7..c77ed1c0526 100644
--- a/vespajlib/src/main/java/com/yahoo/tensor/functions/Concat.java
+++ b/vespajlib/src/main/java/com/yahoo/tensor/functions/Concat.java
@@ -9,8 +9,14 @@ import com.yahoo.tensor.Tensor;
import com.yahoo.tensor.TensorAddress;
import com.yahoo.tensor.TensorType;
import com.yahoo.tensor.evaluation.EvaluationContext;
-
-import java.util.*;
+import com.yahoo.tensor.evaluation.TypeContext;
+
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.Set;
import java.util.stream.Collectors;
/**
@@ -54,7 +60,7 @@ public class Concat extends PrimitiveTensorFunction {
}
@Override
- public TensorType type(EvaluationContext context) {
+ public TensorType type(TypeContext context) {
return type(argumentA.type(context), argumentB.type(context));
}
diff --git a/vespajlib/src/main/java/com/yahoo/tensor/functions/ConstantTensor.java b/vespajlib/src/main/java/com/yahoo/tensor/functions/ConstantTensor.java
index 4a6d656142f..50b479da168 100644
--- a/vespajlib/src/main/java/com/yahoo/tensor/functions/ConstantTensor.java
+++ b/vespajlib/src/main/java/com/yahoo/tensor/functions/ConstantTensor.java
@@ -5,6 +5,7 @@ import com.google.common.annotations.Beta;
import com.yahoo.tensor.Tensor;
import com.yahoo.tensor.TensorType;
import com.yahoo.tensor.evaluation.EvaluationContext;
+import com.yahoo.tensor.evaluation.TypeContext;
import java.util.Collections;
import java.util.List;
@@ -41,7 +42,7 @@ public class ConstantTensor extends PrimitiveTensorFunction {
public PrimitiveTensorFunction toPrimitive() { return this; }
@Override
- public TensorType type(EvaluationContext context) { return constant.type(); }
+ public TensorType type(TypeContext context) { return constant.type(); }
@Override
public Tensor evaluate(EvaluationContext context) { return constant; }
diff --git a/vespajlib/src/main/java/com/yahoo/tensor/functions/Generate.java b/vespajlib/src/main/java/com/yahoo/tensor/functions/Generate.java
index ff9589bd6ae..e70d1de3db7 100644
--- a/vespajlib/src/main/java/com/yahoo/tensor/functions/Generate.java
+++ b/vespajlib/src/main/java/com/yahoo/tensor/functions/Generate.java
@@ -7,6 +7,7 @@ import com.yahoo.tensor.IndexedTensor;
import com.yahoo.tensor.Tensor;
import com.yahoo.tensor.TensorType;
import com.yahoo.tensor.evaluation.EvaluationContext;
+import com.yahoo.tensor.evaluation.TypeContext;
import java.util.Collections;
import java.util.List;
@@ -60,7 +61,7 @@ public class Generate extends PrimitiveTensorFunction {
public PrimitiveTensorFunction toPrimitive() { return this; }
@Override
- public TensorType type(EvaluationContext context) { return type; }
+ public TensorType type(TypeContext context) { return type; }
@Override
public Tensor evaluate(EvaluationContext context) {
diff --git a/vespajlib/src/main/java/com/yahoo/tensor/functions/Join.java b/vespajlib/src/main/java/com/yahoo/tensor/functions/Join.java
index 835a2a82a2c..7812c985091 100644
--- a/vespajlib/src/main/java/com/yahoo/tensor/functions/Join.java
+++ b/vespajlib/src/main/java/com/yahoo/tensor/functions/Join.java
@@ -11,6 +11,7 @@ import com.yahoo.tensor.Tensor;
import com.yahoo.tensor.TensorAddress;
import com.yahoo.tensor.TensorType;
import com.yahoo.tensor.evaluation.EvaluationContext;
+import com.yahoo.tensor.evaluation.TypeContext;
import java.util.ArrayList;
import java.util.Collections;
@@ -94,7 +95,7 @@ public class Join extends PrimitiveTensorFunction {
}
@Override
- public TensorType type(EvaluationContext context) {
+ public TensorType type(TypeContext context) {
return new TensorType.Builder(argumentA.type(context), argumentB.type(context)).build();
}
diff --git a/vespajlib/src/main/java/com/yahoo/tensor/functions/Map.java b/vespajlib/src/main/java/com/yahoo/tensor/functions/Map.java
index e5440b56c54..53504868ff2 100644
--- a/vespajlib/src/main/java/com/yahoo/tensor/functions/Map.java
+++ b/vespajlib/src/main/java/com/yahoo/tensor/functions/Map.java
@@ -6,6 +6,7 @@ import com.yahoo.tensor.Tensor;
import com.yahoo.tensor.TensorAddress;
import com.yahoo.tensor.TensorType;
import com.yahoo.tensor.evaluation.EvaluationContext;
+import com.yahoo.tensor.evaluation.TypeContext;
import java.util.Collections;
import java.util.Iterator;
@@ -52,7 +53,7 @@ public class Map extends PrimitiveTensorFunction {
}
@Override
- public TensorType type(EvaluationContext context) {
+ public TensorType type(TypeContext context) {
return argument.type(context);
}
diff --git a/vespajlib/src/main/java/com/yahoo/tensor/functions/Reduce.java b/vespajlib/src/main/java/com/yahoo/tensor/functions/Reduce.java
index 591a6e4649e..76a938b9fe2 100644
--- a/vespajlib/src/main/java/com/yahoo/tensor/functions/Reduce.java
+++ b/vespajlib/src/main/java/com/yahoo/tensor/functions/Reduce.java
@@ -8,6 +8,7 @@ import com.yahoo.tensor.Tensor;
import com.yahoo.tensor.TensorAddress;
import com.yahoo.tensor.TensorType;
import com.yahoo.tensor.evaluation.EvaluationContext;
+import com.yahoo.tensor.evaluation.TypeContext;
import java.util.Collections;
import java.util.HashMap;
@@ -100,7 +101,7 @@ public class Reduce extends PrimitiveTensorFunction {
}
@Override
- public TensorType type(EvaluationContext context) {
+ public TensorType type(TypeContext context) {
return type(argument.type(context));
}
diff --git a/vespajlib/src/main/java/com/yahoo/tensor/functions/Rename.java b/vespajlib/src/main/java/com/yahoo/tensor/functions/Rename.java
index 6a9b8d68b38..de3d2be265a 100644
--- a/vespajlib/src/main/java/com/yahoo/tensor/functions/Rename.java
+++ b/vespajlib/src/main/java/com/yahoo/tensor/functions/Rename.java
@@ -7,6 +7,7 @@ import com.yahoo.tensor.Tensor;
import com.yahoo.tensor.TensorAddress;
import com.yahoo.tensor.TensorType;
import com.yahoo.tensor.evaluation.EvaluationContext;
+import com.yahoo.tensor.evaluation.TypeContext;
import java.util.Collections;
import java.util.HashMap;
@@ -71,7 +72,7 @@ public class Rename extends PrimitiveTensorFunction {
public PrimitiveTensorFunction toPrimitive() { return this; }
@Override
- public TensorType type(EvaluationContext context) {
+ public TensorType type(TypeContext context) {
return type(argument.type(context));
}
diff --git a/vespajlib/src/main/java/com/yahoo/tensor/functions/TensorFunction.java b/vespajlib/src/main/java/com/yahoo/tensor/functions/TensorFunction.java
index 3f6dfae6222..78ab09c7820 100644
--- a/vespajlib/src/main/java/com/yahoo/tensor/functions/TensorFunction.java
+++ b/vespajlib/src/main/java/com/yahoo/tensor/functions/TensorFunction.java
@@ -6,6 +6,7 @@ import com.yahoo.tensor.Tensor;
import com.yahoo.tensor.TensorType;
import com.yahoo.tensor.evaluation.EvaluationContext;
import com.yahoo.tensor.evaluation.MapEvaluationContext;
+import com.yahoo.tensor.evaluation.TypeContext;
import java.util.List;
@@ -49,7 +50,7 @@ public abstract class TensorFunction {
*
* @param context a context which must be passed to all nexted functions when evaluating
*/
- public abstract TensorType type(EvaluationContext context);
+ public abstract TensorType type(TypeContext context);
/** Evaluate with no context */
public final Tensor evaluate() { return evaluate(new MapEvaluationContext()); }