-
Notifications
You must be signed in to change notification settings - Fork 2
/
store.py
1119 lines (926 loc) · 39.1 KB
/
store.py
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
=====
Store
=====
The file system for storing and updating state variables during an experiment.
"""
import copy
import logging as log
from pprint import pformat
import numpy as np
from pint import Quantity
from vivarium import divider_registry, serializer_registry, updater_registry
from vivarium.core.process import Process
from vivarium.library.dict_utils import deep_merge, MULTI_UPDATE_KEY
from vivarium.library.topology import without, dict_to_paths
from vivarium.core.types import Processes, Topology, State
EMPTY_UPDATES = None, None, None
def generate_state(
processes: Processes,
topology: Topology,
initial_state: State,
) -> 'Store':
store = Store({})
store.generate_paths(processes, topology)
store.apply_subschemas()
store.set_value(initial_state)
store.apply_defaults()
return store
def key_for_value(d, looking):
found = None
for key, value in d.items():
if looking == value:
found = key
break
return found
def hierarchy_depth(hierarchy, path=()):
"""
Create a mapping of every path in the hierarchy to the node living at
that path in the hierarchy.
"""
base = {}
for key, inner in hierarchy.items():
down = tuple(path + (key,))
if isinstance(inner, dict):
base.update(hierarchy_depth(inner, down))
else:
base[down] = inner
return base
def always_true(_):
return True
def identity(y):
return y
class Store:
"""Holds a subset of the overall model state
The total state of the model can be broken down into :term:`stores`,
each of which is represented by an instance of this `Store` class.
The store's state is a set of :term:`variables`, each of which is
defined by a set of :term:`schema key-value pairs`. The valid schema
keys are listed in :py:attr:`schema_keys`, and they are:
* **_default** (Type should match the variable value): The default
value of the variable.
* **_updater** (:py:class:`str`): The name of the :term:`updater` to
use. By default this is ``accumulate``.
* **_divider** (:py:class:`str`): The name of the :term:`divider` to
use. Note that ``_divider`` is not included in the ``schema_keys``
set because it can be applied to any node in the hierarchy, not
just leaves (which represent variables).
* **_value** (Type should match the variable value): The current
value of the variable. This is ``None`` by default.
* **_properties** (:py:class:`dict`): Extra properties of the
variable that don't have a specific schema key. This is an empty
dictionary by default.
* **_emit** (:py:class:`bool`): Whether to emit the variable to the
:term:`emitter`. This is ``False`` by default.
"""
schema_keys = {
'_default',
'_updater',
'_value',
'_properties',
'_emit',
'_serializer',
}
def __init__(self, config, outer=None, source=None):
self.outer = outer
self.inner = {}
self.subschema = {}
self.subtopology = {}
self.properties = {}
self.default = None
self.updater = None
self.value = None
self.units = None
self.divider = None
self.emit = False
self.sources = {}
self.leaf = False
self.serializer = None
self.topology = {}
self.apply_config(config, source)
def check_default(self, new_default):
defaults_equal = False
if self.default is not None:
self_default_comp = self.default
new_default_comp = new_default
if isinstance(self_default_comp, np.ndarray):
self_default_comp = self.default.tolist()
if isinstance(new_default_comp, np.ndarray):
new_default_comp = self.default.tolist()
defaults_equal = self_default_comp == new_default_comp
if defaults_equal:
if (
not isinstance(new_default, np.ndarray)
and not isinstance(self.default, np.ndarray)
and new_default == 0
and self.default != 0
):
log.debug(
'_default schema conflict: %s and %s. selecting %s',
str(self.default), str(new_default), str(self.default))
return self.default
log.debug(
'_default schema conflict: %s and %s. selecting %s',
str(self.default), str(new_default), str(new_default))
return new_default
def check_value(self, new_value):
if self.value is not None and new_value != self.value:
raise Exception(
'_value schema conflict: {} and {}'.format(
new_value, self.value))
return new_value
def merge_subtopology(self, subtopology):
self.subtopology = deep_merge(self.subtopology, subtopology)
def apply_subschema_config(self, subschema):
self.subschema = deep_merge(
self.subschema,
subschema)
def apply_config(self, config, source=None):
"""
Expand the tree by applying additional config.
Special keys for the config are:
* _default - Default value for this node.
* _properties - An arbitrary map of keys to values. This can be used
for any properties which exist outside of the operation of the
tree (like mass or energy).
* _updater - Which updater to use. Default is 'accumulate' which
adds the new value to the existing value, but 'set' is common
as well. You can also provide your own function here instead
of a string key into the updater library.
* _emit - whether or not to emit the values under this point in
the tree.
* _divider - What to do with this node when division happens.
Default behavior is to leave it alone, but you can also pass
'split' here, or a function of your choosing. If you need
other values from the state you need to supply a dictionary
here containing the updater and the topology for where the
other state values are coming from. This has two keys:
* divider - a function that takes the existing value and any
values supplied from the adjoining topology.
* topology - a mapping of keys to paths where the value for
those keys will be found. This will be passed in as the
second argument to the divider function.
* _subschema/* - If this node was declared to house an unbounded set
of related states, the schema for these states is held in this
nodes subschema and applied whenever new subkeys are added
here.
* _subtopology - The subschema is informed by the subtopology to
map the process perspective to the actual tree structure.
"""
if '*' in config:
self.apply_subschema_config(config['*'])
config = without(config, '*')
if '_subschema' in config:
if source:
self.sources[source] = config['_subschema']
self.apply_subschema_config(config['_subschema'])
config = without(config, '_subschema')
if '_subtopology' in config:
self.merge_subtopology(config['_subtopology'])
config = without(config, '_subtopology')
if '_topology' in config:
self.topology = config['_topology']
config = without(config, '_topology')
if '_divider' in config:
self.divider = config['_divider']
if isinstance(self.divider, str):
self.divider = divider_registry.access(self.divider)
if isinstance(self.divider, dict) and isinstance(
self.divider['divider'], str):
self.divider['divider'] = divider_registry.access(
self.divider['divider'])
config = without(config, '_divider')
if self.schema_keys & set(config.keys()):
if self.inner:
raise Exception(
'trying to assign leaf values to a branch at: {}'.format(
self.path_for()))
self.leaf = True
if '_units' in config:
self.units = config['_units']
self.serializer = serializer_registry.access('units')
if '_serializer' in config:
self.serializer = config['_serializer']
if isinstance(self.serializer, str):
self.serializer = serializer_registry.access(
self.serializer)
if '_default' in config:
self.default = self.check_default(config.get('_default'))
if isinstance(self.default, Quantity):
self.units = self.units or self.default.units
self.serializer = (self.serializer or
serializer_registry.access('units'))
elif isinstance(self.default, list) and \
len(self.default) > 0 and \
isinstance(self.default[0], Quantity):
self.units = self.units or self.default[0].units
self.serializer = (self.serializer or
serializer_registry.access('units'))
elif isinstance(self.default, np.ndarray):
self.serializer = (self.serializer or
serializer_registry.access('numpy'))
if '_value' in config:
self.value = self.check_value(config.get('_value'))
if isinstance(self.value, Quantity):
self.units = self.value.units
self.updater = config.get(
'_updater',
self.updater or 'accumulate',
)
self.properties = deep_merge(
self.properties,
config.get('_properties', {}))
self.emit = config.get('_emit', self.emit)
if source:
self.sources[source] = config
else:
if self.leaf and config:
raise Exception(
'trying to assign create inner for leaf node: {}'.format(
self.path_for()))
# self.value = None
for key, child in config.items():
if key not in self.inner:
self.inner[key] = Store(child, outer=self, source=source)
else:
self.inner[key].apply_config(child, source=source)
def get_updater(self, update):
updater = self.updater
if isinstance(update, dict) and '_updater' in update:
updater = update['_updater']
if isinstance(updater, str):
updater = updater_registry.access(updater)
return updater
def get_config(self, sources=False):
"""
Assemble a dictionary representation of the config for this node.
A desired property is that the node can be exactly recreated by
applying the resulting config to an empty node again.
"""
config = {}
if self.properties:
config['_properties'] = self.properties
if self.subschema:
config['_subschema'] = self.subschema
if self.subtopology:
config['_subtopology'] = self.subtopology
if self.divider:
config['_divider'] = self.divider
if sources and self.sources:
config['_sources'] = self.sources
if self.inner:
child_config = {
key: child.get_config(sources)
for key, child in self.inner.items()}
config.update(child_config)
else:
config.update({
'_default': self.default,
'_value': self.value})
if self.updater:
config['_updater'] = self.updater
if self.units:
config['_units'] = self.units
if self.emit:
config['_emit'] = self.emit
return config
def top(self):
"""
Find the top of this tree.
"""
if self.outer:
return self.outer.top()
return self
def path_for(self):
"""
Find the path to this node.
"""
if self.outer:
key = key_for_value(self.outer.inner, self)
above = self.outer.path_for()
return above + (key,)
return tuple()
def get_value(self, condition=None, f=None):
"""
Pull the values out of the tree in a structure symmetrical to the tree.
"""
if self.inner:
if condition is None:
condition = always_true
if f is None:
f = identity
return {
key: f(child.get_value(condition, f))
for key, child in self.inner.items()
if condition(child)}
if self.subschema:
return {}
return self.value
def get_path(self, path):
"""
Get the node at the given path relative to this node.
"""
if path:
step = path[0]
if step == '..':
child = self.outer
else:
child = self.inner.get(step)
if child:
return child.get_path(path[1:])
# TODO: more handling for bad paths?
# TODO: check deleted?
return None
return self
def get_paths(self, paths):
return {
key: self.get_path(path)
for key, path in paths.items()}
def get_values(self, paths):
return {
key: self.get_in(path)
for key, path in paths.items()}
def get_in(self, path):
return self.get_path(path).get_value()
def get_template(self, template):
"""
Pass in a template dict with None for each value you want to
retrieve from the tree!
"""
state = {}
for key, value in template.items():
child = self.inner[key]
if value is None:
state[key] = child.get_value()
else:
state[key] = child.get_template(value)
return state
def emit_data(self):
data = {}
if self.inner:
for key, child in self.inner.items():
child_data = child.emit_data()
if child_data is not None or child_data == 0:
data[key] = child_data
return data
if self.emit:
if self.serializer:
if isinstance(self.value, list) and self.units:
return self.serializer.serialize(
[v.to(self.units) for v in self.value])
if self.units:
return self.serializer.serialize(
self.value.to(self.units))
return self.serializer.serialize(self.value)
if self.units:
return self.value.to(self.units).magnitude
return self.value
return None
def delete_path(self, path):
"""
Delete the subtree at the given path.
"""
if not path:
self.inner = {}
self.value = None
return self
target = self.get_path(path[:-1])
remove = path[-1]
if remove in target.inner:
lost = target.inner[remove]
del target.inner[remove]
return lost
return None
def divide_value(self):
"""
Apply the divider for each node to the value in that node to
assemble two parallel divided states of this subtree.
"""
if self.divider:
# divider is either a function or a dict with topology and/or config
if isinstance(self.divider, dict):
divider = self.divider['divider']
state = {}
if 'topology' in self.divider:
topology = self.divider['topology']
state.update({'state': self.outer.get_values(topology)})
if 'config' in self.divider:
config = self.divider['config']
state.update({'config': config})
return divider(self.get_value(), **state)
return self.divider(self.get_value())
if self.inner:
daughters = [{}, {}]
for key, child in self.inner.items():
division = child.divide_value()
if division:
for daughter, divide in zip(daughters, division):
daughter[key] = divide
return daughters
return None
def reduce(self, reducer, initial=None):
"""
Call the reducer on each node accumulating over the result.
"""
value = initial
for path, node in self.depth():
value = reducer(value, path, node)
return value
def set_value(self, value):
"""
Set the value for the given tree elements directly instead of using
the updaters from their nodes.
"""
if self.inner or self.subschema:
for child, inner_value in value.items():
if child not in self.inner:
if self.subschema:
self.inner[child] = Store(self.subschema, self)
else:
pass
# TODO: continue to ignore extra keys?
if child in self.inner:
self.inner[child].set_value(inner_value)
else:
self.value = value
def apply_defaults(self):
"""
If value is None, set to default.
"""
if self.inner:
for child in self.inner.values():
child.apply_defaults()
else:
if self.value is None:
self.value = self.default
def apply_update(self, update, state=None):
"""
Given an arbitrary update, map all the values in that update
to their positions in the tree where they apply, and update
these values using each node's `_updater`.
Arguments:
update: The update being applied.
state: The state at the start of the time step.
There are five topology update methods, which use the following
special update keys:
* `_add` - Adds states into the subtree, given a list of dicts
containing:
* path - Path to the added state key.
* state - The value of the added state.
* `_move` - Moves a node from a source to a target location in the
tree. This uses an update to an :term:`outer` port, which
contains both the source and target node locations. Can move
multiple nodes according to a list of dicts containing:
* source - the source path from an outer process port
* target - the location where the node will be placed.
* `_generate` - The value has four keys, which are essentially
the arguments to the `generate()` function:
* path - Path into the tree to generate this subtree.
* processes - Tree of processes to generate.
* topology - Connections of all the process's `ports_schema()`.
* initial_state - Initial state for this new subtree.
* `_divide` - Performs cell division by constructing two new
daughter cells and removing the mother. Takes a dict with two keys:
* mother - The id of the mother (for removal)
* daughters - List of two new daughter generate directives, of the
same form as the `_generate` value above.
* `_delete` - The value here is a list of paths (tuples) to delete from
the tree.
Additional special update keys for different update operations:
* `_updater` - Override the default updater with any updater you want.
* `_reduce` - This allows a reduction over the entire subtree from some
point downward. Its three keys are:
* from - What point to start the reduction.
* initial - The initial value of the reduction.
* reducer - A function of three arguments, which is called
on every node from the `from` point in the tree down:
* value - The current accumulated value of the reduction.
* path - The path to this point in the tree
* node - The actual node being visited.
This function returns the next `value` for the reduction.
The result of the reduction will be assigned to this point
in the tree.
"""
if isinstance(update, dict) and MULTI_UPDATE_KEY in update:
# apply multiple updates to same node
multi_update = update[MULTI_UPDATE_KEY]
assert isinstance(multi_update, list)
for update_value in multi_update:
self.apply_update(update_value, state)
return EMPTY_UPDATES
if self.inner or self.subschema:
# Branch update: this node has an inner
process_updates, topology_updates, deletions = [], [], []
update = dict(update) # avoid mutating the caller's dict
add_entries = update.pop('_add', None)
if add_entries is not None:
# add a list of sub-states
for added in add_entries:
path = (added['key'],)
added_state = added['state']
target = self.establish_path(path, {})
self.apply_subschema_path(path)
target.apply_defaults()
target.set_value(added_state)
move_entries = update.pop('_move', None)
if move_entries is not None:
# move nodes from source to target path
for move in move_entries:
# get the source node
source_key = move['source']
source_path = (source_key,)
source_node = self.get_path(source_path)
# move source node to target path
target_port = move['target']
target_topology = state.topology[target_port]
target_node = state.outer.get_path(target_topology)
target = target_node.add_node(source_path, source_node)
target_path = target.path_for() + source_path
# find the paths to all the processes
source_process_paths = source_node.depth(
filter_function=lambda x: isinstance(x.value, Process))
# find the process and topology updates
for path, process in source_process_paths:
process_updates.append((
target_path + path, process.value))
topology_updates.append((
target_path + path, process.topology))
self.delete_path(source_path)
here = self.path_for()
source_absolute = tuple(here + source_path)
deletions.append(source_absolute)
generate_entries = update.pop('_generate', None)
if generate_entries is not None:
# generate a list of new processes
for generate in generate_entries:
key = generate.get('key')
path = (key,) if key else tuple()
here = self.path_for()
self.generate(
path,
generate['processes'],
generate['topology'],
generate['initial_state'])
root = here + path
process_paths = dict_to_paths(root, generate['processes'])
process_updates.extend(process_paths)
topology_paths = [
(root + (key,), topology)
for key, topology in generate['topology'].items()]
topology_updates.extend(topology_paths)
self.apply_subschema_path(path)
self.get_path(path).apply_defaults()
divide = update.pop('_divide', None)
if divide is not None:
# use dividers to find initial states for daughters
mother = divide['mother']
daughters = divide['daughters']
initial_state = self.inner[mother].get_value(
condition=lambda child: not
(isinstance(child.value, Process)),
f=lambda child: copy.deepcopy(child))
daughter_states = self.inner[mother].divide_value()
here = self.path_for()
for daughter, daughter_state in \
zip(daughters, daughter_states):
# use initial state as default, merge in divided values
initial_state = deep_merge(
initial_state,
daughter_state)
daughter_key = daughter['key']
daughter_path = (daughter_key,)
self.generate(
daughter_path,
daughter['processes'],
daughter['topology'],
daughter['initial_state'])
root = here + daughter_path
process_paths = dict_to_paths(root, daughter['processes'])
process_updates.extend(process_paths)
topology_paths = [
(root + (key,), topology)
for key, topology in daughter['topology'].items()]
topology_updates.extend(topology_paths)
self.apply_subschema_path(daughter_path)
target = self.get_path(daughter_path)
target.apply_defaults()
target.set_value(initial_state)
mother_path = (mother,)
self.delete_path(mother_path)
deletions.append(tuple(here + mother_path))
delete_keys = update.pop('_delete', None)
for key, value in update.items():
if key in self.inner:
inner = self.inner[key]
inner_topology, inner_processes, inner_deletions = \
inner.apply_update(value, state)
if inner_topology:
topology_updates.extend(inner_topology)
if inner_processes:
process_updates.extend(inner_processes)
if inner_deletions:
deletions.extend(inner_deletions)
# elif key == '..':
# self.outer.apply_update(value, state)
if delete_keys is not None:
# delete a list of paths
here = self.path_for()
for key in delete_keys:
path = (key,)
self.delete_path(path)
deletions.append(tuple(here + path))
return topology_updates, process_updates, deletions
# Leaf update: this node has no inner
updater = self.get_updater(update)
if isinstance(update, dict) and '_reduce' in update:
reduction = update['_reduce']
top = self.get_path(reduction.get('from'))
update = top.reduce(
reduction['reducer'],
initial=reduction['initial'])
if isinstance(update, dict) and \
self.schema_keys and set(update.keys()):
if '_updater' in update:
update = update.get('_value', self.default)
if updater is None:
raise Exception(
f"updater is absent at path {self.path_for()} "
f"with value {self.value} for {pformat(update)}"
)
self.value = updater(self.value, update)
return EMPTY_UPDATES
def inner_value(self, key):
"""
Get the value of an inner state
"""
if key in self.inner:
return self.inner[key].get_value()
return None
def topology_state(self, topology):
"""
Fill in the structure of the given topology with the values at all
the paths the topology points at. Essentially, anywhere in the topology
that has a tuple path will be filled in with the value at that path.
This is the inverse function of the standalone `inverse_topology`.
"""
state = {}
for key, path in topology.items():
if key == '*':
if isinstance(path, dict):
node, path = self.outer_path(path)
for child, child_node in node.inner.items():
state[child] = child_node.topology_state(path)
else:
node = self.get_path(path)
for child, child_node in node.inner.items():
state[child] = child_node.get_value()
elif isinstance(path, dict):
node, path = self.outer_path(path)
state[key] = node.topology_state(path)
else:
state[key] = self.get_path(path).get_value()
return state
def schema_topology(self, schema, topology):
"""
Fill in the structure of the given schema with the values
located according to the given topology.
"""
state = {}
if self.leaf:
state = self.get_value()
else:
for key, subschema in schema.items():
path = topology.get(key)
if key == '*':
if isinstance(path, dict):
node, path = self.outer_path(path)
for child, child_node in node.inner.items():
state[child] = child_node.schema_topology(
subschema, path)
else:
node = self.get_path(path)
for child, child_node in node.inner.items():
state[child] = child_node.schema_topology(
subschema, {})
elif key == '_divider':
pass
elif isinstance(path, dict):
node, path = self.outer_path(path)
state[key] = node.schema_topology(subschema, path)
else:
if path is None:
path = (key,)
node = self.get_path(path)
if node:
state[key] = node.schema_topology(subschema, {})
else:
# node is None, it was likely deleted
print('{} is None'.format(path))
return state
def state_for(self, path, keys):
"""
Get the value of a state at a given path
"""
state = self.get_path(path)
if state is None:
return {}
if keys and keys[0] == '*':
return state.get_value()
return {
key: state.inner_value(key)
for key in keys}
def depth(self, path=(), filter_function=None):
"""
Create a mapping of every path in the tree to the node living at
that path in the tree. An optional `filter` argument is a function
that can declares the instances that will be returned, for example:
* filter=lambda x: isinstance(x.value, Process)
"""
base = []
if filter_function is None or filter_function(self):
base += [(path, self)]
for key, child in self.inner.items():
down = tuple(path + (key,))
base += child.depth(down, filter_function)
return base
def apply_subschema_path(self, path):
if path:
inner = self.inner[path[0]]
if self.subschema:
subtopology = self.subtopology or {}
inner.topology_ports(
self.subschema,
subtopology,
source=self.path_for() + ('*',))
inner.apply_subschema_path(path[1:])
def apply_subschema(self, subschema=None, subtopology=None):
"""
Apply a subschema to all inner nodes (either provided or from this
node's personal subschema) as governed by the given/personal
subtopology.
"""
if subschema is None:
subschema = self.subschema
if subtopology is None:
subtopology = self.subtopology or {}
inner = list(self.inner.values())
for child in inner:
child.topology_ports(
subschema,
subtopology,
source=self.path_for() + ('*',))
def apply_subschemas(self):
"""
Apply all subschemas from all nodes at this point or lower in the tree.
"""
if self.subschema:
self.apply_subschema()
for child in self.inner.values():
child.apply_subschemas()
def update_subschema(self, path, subschema):
"""
Merge a new subschema into an existing subschema at the given path.
"""
target = self.get_path(path)
if target.subschema is None:
target.subschema = subschema
else:
target.subschema = deep_merge(
target.subschema,
subschema)
return target
def establish_path(self, path, config, source=None):
"""
Create a node at the given path if it does not exist, then
apply a config to it.
Paths can include '..' to go up a level (which raises an exception
if that level does not exist).
"""
if len(path) > 0:
path_step = path[0]
remaining = path[1:]
if path_step == '..':
if not self.outer:
raise Exception(
'outer does not exist for path: {}'.format(path))
return self.outer.establish_path(
remaining,
config,
source=source)
if path_step not in self.inner:
self.inner[path_step] = Store(
{}, outer=self, source=source)
return self.inner[path_step].establish_path(
remaining,
config,
source=source)
self.apply_config(config, source=source)
return self
def add_node(self, path, node):
""" Add a node instance at the provided path """
target = self.establish_path(path[:-1], {})
if target.get_value() and path[-1] in target.get_value():