blob: 991cbd304333ca532046984eaaa8d14420a543b4 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.indexinglanguage.expressions;
import com.yahoo.document.DataType;
import com.yahoo.document.datatypes.FieldValue;
import com.yahoo.vespa.indexinglanguage.ExpressionConverter;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
/**
* An expression which returns the value of the first of a list of subexpressions which
* returns a non-null value.
*
* Syntax: sub-expression1 || sub-expression2 || ...
*
* @author bratseth
*/
public class ChoiceExpression extends ExpressionList<Expression> {
public ChoiceExpression() {
this(List.of());
}
public ChoiceExpression(Expression... choices) {
this(Arrays.asList(choices));
}
public ChoiceExpression(Collection<? extends Expression> choices) {
super(choices, resolveInputType(choices));
}
@Override
public ChoiceExpression convertChildren(ExpressionConverter converter) {
return new ChoiceExpression(asList().stream().map(choice -> converter.branch().convert(choice)).toList());
}
@Override
protected void doExecute(ExecutionContext context) {
FieldValue input = context.getValue();
for (Expression expression : this) {
context.setValue(input).execute(expression);
if (context.getValue() != null)
break; // value found
}
}
@Override
protected void doVerify(VerificationContext context) {
DataType input = context.getValueType();
context.setValueType(input);
for (Expression exp : this)
context.setValueType(input).execute(exp);
}
private static DataType resolveInputType(Collection<? extends Expression> list) {
DataType previousInput = null;
DataType previousOutput = null;
for (Expression choice : list) {
DataType thisInput = choice.requiredInputType();
if (previousInput == null)
previousInput = thisInput;
else if (thisInput != null && !previousInput.isAssignableFrom(thisInput))
throw new VerificationException(ScriptExpression.class, "Choice expression require conflicting input types, " +
previousInput.getName() + " vs " + thisInput.getName());
DataType thisOutput = choice.createdOutputType();
if (previousOutput == null)
previousOutput = thisOutput;
else if (thisOutput != null && !previousOutput.isAssignableFrom(thisOutput))
throw new VerificationException(ScriptExpression.class, "Choice expression produce conflicting output types, " +
previousOutput.getName() + " vs " + thisOutput.getName());
}
return previousInput;
}
@Override
public DataType createdOutputType() {
return UnresolvedDataType.INSTANCE;
}
@Override
public String toString() {
return asList().stream().map(Object::toString).collect(Collectors.joining(" || "));
}
@Override
public boolean equals(Object obj) {
return super.equals(obj) && obj instanceof ChoiceExpression;
}
}
|