|
| 1 | +package net.finmath.util.config; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | +import java.util.Map; |
| 5 | +import java.util.stream.Collectors; |
| 6 | + |
| 7 | +import net.finmath.util.config.nodes.ConfigNode; |
| 8 | +import net.finmath.util.config.nodes.Node; |
| 9 | +import net.finmath.util.config.nodes.SpecialNodes; |
| 10 | +import net.finmath.util.config.nodes.ValueNode; |
| 11 | + |
| 12 | +/** |
| 13 | + * Config Tree: A tree based representation of configurations that can be selected with a key-value map describing the selector. |
| 14 | + * |
| 15 | + * A configuration is a value (Object) assigned to a selector (Map<String, Object>). |
| 16 | + * A selector is a key-value map where certain properties (String) have certain values (Object). |
| 17 | + * Properties of the selector are checked in a fixed order, |
| 18 | + * such that a tree is formed, where each property corresponds to a level (depth) in the tree. |
| 19 | + * Each level has a special branch for DEFAULT VALUES, if the selector value does not match any value of other nodes. |
| 20 | + * |
| 21 | + * @author Christian Fries |
| 22 | + */ |
| 23 | +public class ConfigTree { |
| 24 | + |
| 25 | + private final Node root; |
| 26 | + |
| 27 | + /** |
| 28 | + * Construct the tree. |
| 29 | + * |
| 30 | + * @param keyOrder Order in which the string keys of a selector define the levels of the tree. |
| 31 | + * @param configs A list, where each element is map of keys to object. The key "value" is interpreted as the configuration value. All other keys are interpreted as configuration properties. |
| 32 | + */ |
| 33 | + public ConfigTree(List<String> keyOrder, List<Map<String, Object>> configs) { |
| 34 | + this.root = group(keyOrder, configs); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Get the configuration for a given specification of the properties (selector). |
| 39 | + * |
| 40 | + * @param selector Maps the name (String) of a property to its value (Object). |
| 41 | + * @return The configuration value for the given selector. |
| 42 | + */ |
| 43 | + public Object getConfig(Map<String, Object> selector) { |
| 44 | + |
| 45 | + Node node = this.root; |
| 46 | + |
| 47 | + while(node instanceof ConfigNode) { |
| 48 | + ConfigNode configNode = (ConfigNode)node; |
| 49 | + if(selector.containsKey(configNode.getKey()) && configNode.getValueToConfig().keySet().contains(selector.get(configNode.getKey()))) { |
| 50 | + node = configNode.getValueToConfig().get(selector.get(configNode.getKey())); |
| 51 | + } |
| 52 | + else { |
| 53 | + node = configNode.getValueToConfig().get(SpecialNodes.DEFAULT_VALUE); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + if(node instanceof ValueNode) { |
| 58 | + ValueNode valueNode = (ValueNode)node; |
| 59 | + return valueNode.getValue(); |
| 60 | + } |
| 61 | + else { |
| 62 | + throw new IllegalArgumentException("Unable to resolve configuration from the given properties."); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Helper for the constructor. Recursive contruction of the tree. |
| 68 | + * |
| 69 | + * @param keyOrder Given key order. |
| 70 | + * @param configs List of configs. |
| 71 | + * @return Node of the (sub-)tree for the given config key. |
| 72 | + */ |
| 73 | + private Node group(List<String> keyOrder, List<Map<String, Object>> configs) { |
| 74 | + if(keyOrder.size() == 0) { |
| 75 | + if(configs.size() == 1) { |
| 76 | + Map<String, Object> config = configs.get(0); |
| 77 | + Object value = config.get("value"); |
| 78 | + return new ValueNode( value); |
| 79 | + } |
| 80 | + else { |
| 81 | + throw new IllegalArgumentException("Multiple configs for same values."); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + // Group all elements by the first key.... |
| 86 | + String key = keyOrder.get(0); |
| 87 | + Map<Object, List<Map<String, Object>>> grouped = configs.stream().collect(Collectors.groupingBy(map -> map.get(key))); |
| 88 | + |
| 89 | + // ...call group (recursive) for all values below this key... |
| 90 | + List<String> keyOrderRemain = keyOrder.subList(1, keyOrder.size()); |
| 91 | + Map<Object, Node> valueToConfig = grouped.entrySet().stream().collect(Collectors.toMap( |
| 92 | + Map.Entry::getKey, entry -> group(keyOrderRemain, entry.getValue()))); |
| 93 | + |
| 94 | + // ...create a ConfigNode for this key. |
| 95 | + return new ConfigNode(key, valueToConfig); |
| 96 | + } |
| 97 | +} |
0 commit comments