aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/search/pagetemplates/engine/resolvers/DeterministicResolver.java
blob: d8c9e86bd60a689e694f17997aa4f27ef8026a27 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.pagetemplates.engine.resolvers;

import com.yahoo.search.Query;
import com.yahoo.search.Result;
import com.yahoo.search.pagetemplates.engine.Resolution;
import com.yahoo.search.pagetemplates.engine.Resolver;
import com.yahoo.search.pagetemplates.model.Choice;
import com.yahoo.search.pagetemplates.model.MapChoice;
import com.yahoo.search.pagetemplates.model.PageElement;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * A resolver which
 * <ul>
 *   <li>Always chooses the <i>last</i> alternative of any Choice
 *   <li>Always maps values to placeholders in the order they are listed in the map definition of any MapChoice
 * </ul>
 * This is useful for testing.
 * <p>
 * The id of this if <code>native.deterministic</code>
 *
 * @author bratseth
 */
public class DeterministicResolver extends Resolver {
    public static final String nativeId = "native.deterministic";

    public DeterministicResolver() {}

    protected DeterministicResolver(String id) {
        super(id);
    }

    /** Chooses the last alternative of any choice */
    @Override
    public void resolve(Choice choice, Query query, Result result, Resolution resolution) {
        resolution.addChoiceResolution(choice,choice.alternatives().size()-1);
    }

    /** Chooses a mapping which is always by the literal order given in the source template */
    @Override
    public void resolve(MapChoice choice,Query query,Result result,Resolution resolution) {
        Map<String, List<PageElement>> mapping=new HashMap<>();
        // Map 1-1 by order
        List<String> placeholderIds=choice.placeholderIds();
        List<List<PageElement>> valueList=choice.values();
        int i=0;
        for (String placeholderId : placeholderIds)
            mapping.put(placeholderId,valueList.get(i++));
        resolution.addMapChoiceResolution(choice,mapping);
    }

}