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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.searchlib.rankingexpression.evaluation;
import com.yahoo.searchlib.rankingexpression.Reference;
import com.yahoo.tensor.TensorType;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/**
* A context backed by a Map
*
* @author bratseth
*/
public class MapContext extends Context {
private Map<String, Value> bindings = new HashMap<>(); // TODO: Change String to Reference
private boolean frozen = false;
public MapContext() {
this(defaultMissingValue);
}
public MapContext(Value missingValue) {
this.missingValue = missingValue.freeze();
}
public MapContext(Map<String,Value> bindings) {
this(bindings, defaultMissingValue);
}
/**
* Creates a map context from a map.
* All the Values of the map will be frozen.
*/
public MapContext(Map<String,Value> bindings, Value missingValue) {
this.missingValue = missingValue.freeze();
bindings.forEach((k, v) -> this.bindings.put(k, v.freeze()));
}
/**
* Freezes this.
* Returns this for convenience.
*/
public MapContext freeze() {
if ( ! frozen)
bindings = Collections.unmodifiableMap(bindings);
return this;
}
/** Returns the type of the given value key, or null if it is not bound. */
@Override
public TensorType getType(Reference key) {
Value value = bindings.get(key.toString());
if (value == null) return null;
return value.type();
}
/** Returns the value of a key. 0 is returned if the given key is not bound in this. */
@Override
public Value get(String key) {
return bindings.getOrDefault(key, missingValue);
}
/**
* Sets the value of a key. The value is frozen by this.
*/
@Override
public void put(String key, Value value) {
bindings.put(key, value.freeze());
}
/** Returns an immutable view of the bindings of this. */
public Map<String, Value> bindings() {
if (frozen) return bindings;
return Collections.unmodifiableMap(bindings);
}
/** Returns a new, modifiable context containing all the bindings of this */
public MapContext thawedCopy() { return new MapContext(new HashMap<>(bindings)); }
/** Returns an unmodifiable map of the names of this */
@Override
public Set<String> names() {
if (frozen) return bindings.keySet();
return Collections.unmodifiableMap(bindings).keySet();
}
@Override
public String toString() {
return "a map context [" + bindings.size() + " bindings]";
}
/**
* A convenience constructor which returns a map context from a string on the form
* <code>name1:value1, name2:value2 ...</code>.
* Extra spaces are allowed anywhere. Any other deviation from the syntax causes an exception to be thrown.
*/
public static MapContext fromString(String contextString) {
MapContext mapContext = new MapContext();
for (String keyValueString : contextString.split(",")) {
String[] strings = keyValueString.trim().split(":");
mapContext.put(strings[0].trim(), Value.parse(strings[1].trim()));
}
return mapContext;
}
}
|