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
|
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.schema.derived;
import com.yahoo.collections.Pair;
import com.yahoo.schema.parser.ParseException;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author bratseth
*/
public class GeminiTestCase extends AbstractExportingTestCase {
@Test
public void testRanking2() throws IOException, ParseException {
DerivedConfiguration c = assertCorrectDeriving("gemini2");
RawRankProfile p = c.getRankProfileList().getRankProfiles().get("test");
Map<String, String> ranking = removePartKeySuffixes(asMap(p.configProperties()));
assertEquals("attribute(right)", resolve(lookup("toplevel", ranking), ranking));
}
private Map<String, String> asMap(List<Pair<String, String>> properties) {
Map<String, String> map = new HashMap<>();
for (Pair<String, String> property : properties)
map.put(property.getFirst(), property.getSecond());
return map;
}
private Map<String, String> removePartKeySuffixes(Map<String, String> p) {
Map<String, String> pWithoutSuffixes = new HashMap<>();
for (Map.Entry<String, String> entry : p.entrySet())
pWithoutSuffixes.put(removePartSuffix(entry.getKey()), entry.getValue());
return pWithoutSuffixes;
}
private String removePartSuffix(String s) {
int partIndex = s.indexOf(".part");
if (partIndex <= 0) return s;
return s.substring(0, partIndex);
}
/**
* Recursively resolves references to other ranking expressions - rankingExpression(name) -
* and replaces the reference by the expression
*/
private String resolve(String expression, Map<String, String> ranking) {
int referenceStartIndex;
while ((referenceStartIndex = expression.indexOf("rankingExpression(")) >= 0) {
int referenceEndIndex = expression.indexOf(")", referenceStartIndex);
expression = expression.substring(0, referenceStartIndex) +
resolve(lookup(expression.substring(referenceStartIndex + "rankingExpression(".length(), referenceEndIndex), ranking), ranking) +
expression.substring(referenceEndIndex + 1);
}
return expression;
}
private String lookup(String expressionName, Map<String, String> ranking) {
String value = ranking.get("rankingExpression(" + expressionName + ").rankingScript");
if (value == null) {
return expressionName;
}
return value;
}
}
|