use of com.yahoo.searchlib.rankingexpression.rule.ReferenceNode in project vespa by vespa-engine.
the class MapEvaluationTypeContext method tensorFeatureType.
/**
* There are two features which returns the (non-empty) tensor type: tensorFromLabels and tensorFromWeightedSet.
* This returns the type of those features if this is a reference to either of them, or empty otherwise.
*/
private Optional<TensorType> tensorFeatureType(Reference reference) {
if (!reference.name().equals("tensorFromLabels") && !reference.name().equals("tensorFromWeightedSet"))
return Optional.empty();
if (reference.arguments().size() != 1 && reference.arguments().size() != 2)
throw new IllegalArgumentException(reference.name() + " must have one or two arguments");
ExpressionNode arg0 = reference.arguments().expressions().get(0);
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);
String dimension;
if (reference.arguments().size() > 1) {
ExpressionNode arg1 = reference.arguments().expressions().get(1);
if ((!(arg1 instanceof ReferenceNode) || !(((ReferenceNode) arg1).reference().isIdentifier())) && (!(arg1 instanceof NameNode)))
throw new IllegalArgumentException("The second argument of " + reference.name() + " must be a dimension name, not " + arg1);
dimension = reference.arguments().expressions().get(1).toString();
} else {
// default
dimension = ((ReferenceNode) arg0).reference().arguments().expressions().get(0).toString();
}
return Optional.of(new TensorType.Builder().mapped(dimension).build());
}
use of com.yahoo.searchlib.rankingexpression.rule.ReferenceNode in project vespa by vespa-engine.
the class TensorTransformer method replaceMaxAndMinFunction.
private ExpressionNode replaceMaxAndMinFunction(FunctionNode node) {
ExpressionNode arg1 = node.children().get(0);
ExpressionNode arg2 = node.children().get(1);
TensorFunctionNode.TensorFunctionExpressionNode expression = TensorFunctionNode.wrapArgument(arg1);
Reduce.Aggregator aggregator = Reduce.Aggregator.valueOf(node.getFunction().name());
String dimension = ((ReferenceNode) arg2).getName();
return new TensorFunctionNode(new Reduce(expression, aggregator, dimension));
}
use of com.yahoo.searchlib.rankingexpression.rule.ReferenceNode in project vespa by vespa-engine.
the class ReferenceTestCase method testSimple.
@Test
public void testSimple() {
assertTrue(new Reference("foo", new Arguments(new ReferenceNode("arg")), null).isSimple());
assertTrue(new Reference("foo", new Arguments(new ReferenceNode("arg")), "out").isSimple());
assertTrue(new Reference("foo", new Arguments(new NameNode("arg")), "out").isSimple());
assertFalse(new Reference("foo", new Arguments(), null).isSimple());
}
use of com.yahoo.searchlib.rankingexpression.rule.ReferenceNode in project vespa by vespa-engine.
the class Reshape method unrollTensorExpression.
private static ExpressionNode unrollTensorExpression(TensorType type) {
if (type.rank() == 0) {
return new ConstantNode(DoubleValue.zero);
}
List<ExpressionNode> children = new ArrayList<>();
List<ArithmeticOperator> operators = new ArrayList<>();
int size = 1;
for (int i = type.dimensions().size() - 1; i >= 0; --i) {
TensorType.Dimension dimension = type.dimensions().get(i);
children.add(0, new ReferenceNode(dimension.name()));
if (size > 1) {
operators.add(0, ArithmeticOperator.MULTIPLY);
children.add(0, new ConstantNode(new DoubleValue(size)));
}
size *= TensorConverter.dimensionSize(dimension);
if (i > 0) {
operators.add(0, ArithmeticOperator.PLUS);
}
}
return new ArithmeticNode(children, operators);
}
use of com.yahoo.searchlib.rankingexpression.rule.ReferenceNode in project vespa by vespa-engine.
the class RankingExpressionTestCase method testProgrammaticBuilding.
@Test
public void testProgrammaticBuilding() throws ParseException {
ReferenceNode input = new ReferenceNode("input");
ReferenceNode constant = new ReferenceNode("constant");
ArithmeticNode product = new ArithmeticNode(input, ArithmeticOperator.MULTIPLY, constant);
Reduce sum = new Reduce(new TensorFunctionNode.TensorFunctionExpressionNode(product), Reduce.Aggregator.sum);
RankingExpression expression = new RankingExpression(new TensorFunctionNode(sum));
RankingExpression expected = new RankingExpression("sum(input * constant)");
assertEquals(expected.toString(), expression.toString());
}
Aggregations