#!/usr/bin/python
# -*- encoding: utf-8 -*-
class Node:
def __init__(self, name, children=None):
self.children = children or list()
self.name = name
self.system = set()
def dependOn(self, node):
self.children.append(node)
def mark(self, name):
self.system.add(name)
def unmark(self, name):
self.system.remove(name)
def __str__(self):
return self.name
def __repr__(self):
return str(self)
def down(root, callable):
callable(root)
for l in root.children:
down(l, callable)
def up(root, callable):
for l in root.children:
up(l, callable)
callable(root)
""" Система S
G <--- H
A <--- B <--- C <--- D
^ ^----- E
+----- F
"""
tree1=a=Node('A')
b=Node('B')
c=Node('C')
d=Node('D')
e=Node('E')
f=Node('F')
tree2=g=Node('G')
h=Node('H')
a.dependOn(b)
b.dependOn(c)
c.dependOn(d)
c.dependOn(e)
b.dependOn(f)
g.dependOn(h)
S = [ tree2, tree1 ]
for n in S:
up(n, lambda x: x.mark('S'))
""" Система C (возможно вырожденная т.к. (зависящий от B) узел A в ней как-бы не может находиться ?)
G <--- H
A <--- ? <--- ? <--- D
^ ^----- E
+----- F <--- NEW1 <--- NEW2
"""
import copy
C = copy.deepcopy(S)
f = C[1].children[0].children[1]
new1 = Node('NEW1')
f.dependOn(new1)
new2 = Node('NEW2')
new1.dependOn(new2)
for n in C:
up(n, lambda x: x.mark('C'))
c = C[1].children[0].children[0]
c.unmark('C') # unmark B/A ???
b = C[1].children[0]
b.unmark('C')
def messageIf(msg, l):
def message(node):
if l(node.system):
print "%s.%s()" % (node, msg) # node.deactivate() :)
return message
for n in C:
down(n, messageIf('deactivate', lambda s: 'C' not in s))
up(n, messageIf('activate', lambda s: 'C' in s and 'S' not in s))