aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/searchdefinition/OnnxModels.java
blob: c9c1210055244200883fd29510024cb0b6491baf (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.searchdefinition;

import com.yahoo.config.application.api.FileRegistry;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

/**
 * ONNX models tied to a search definition or global.
 *
 * @author lesters
 */
public class OnnxModels {

    private final FileRegistry fileRegistry;

    /** The schema this belongs to, or empty if it is global */
    private final Optional<Schema> owner;

    private final Map<String, OnnxModel> models = new HashMap<>();

    public OnnxModels(FileRegistry fileRegistry, Optional<Schema> owner) {
        this.fileRegistry = fileRegistry;
        this.owner = owner;
    }

    public void add(OnnxModel model) {
        model.validate();
        model.register(fileRegistry);
        String name = model.getName();
        models.put(name, model);
    }

    public void add(Map<String, OnnxModel> models) {
        models.values().forEach(this::add);
    }

    public OnnxModel get(String name) {
        var model = models.get(name);
        if (model != null) return model;
        if (owner.isPresent() && owner.get().inherited().isPresent())
            return owner.get().inherited().get().onnxModels().get(name);
        return null;
    }

    public boolean has(String name) {
        boolean has = models.containsKey(name);
        if (has) return true;
        if (owner.isPresent() && owner.get().inherited().isPresent())
            return owner.get().inherited().get().onnxModels().has(name);
        return false;
    }

    public Map<String, OnnxModel> asMap() {
        // Shortcuts
        if (owner.isEmpty() || owner.get().inherited().isEmpty()) return Collections.unmodifiableMap(models);
        if (models.isEmpty()) return owner.get().inherited().get().onnxModels().asMap();

        var allModels = new HashMap<>(owner.get().inherited().get().onnxModels().asMap());
        allModels.putAll(models);
        return Collections.unmodifiableMap(allModels);
    }

}