Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class BaseNode:
- def __init__(self):
- self.execResult = None
- def execute(self):
- raise RuntimeError("Not implemented")
- def result(self):
- if self.execResult == None:
- print("Executed= " + str(self))
- self.execResult = self.execute()
- else:
- print("From cache= " + str(self))
- return self.execResult
- class LeafNode(BaseNode):
- def __init__(self, name, result):
- BaseNode.__init__(self)
- self.name = name
- self.oracleResult = result
- def execute(self):
- return self.oracleResult
- def __str__(self):
- return self.name
- class NotNode(BaseNode):
- def __init__(self, child):
- BaseNode.__init__(self)
- self.child = child
- def execute(self):
- return not self.child.result()
- def __str__(self):
- return "Not: " + str(self.child)
- class AndNode(BaseNode):
- def __init__(self, children):
- BaseNode.__init__(self)
- self.children = children
- def execute(self):
- for node in self.children:
- if node.result() == False:
- return False
- return True
- def __str__(self):
- return "And: ["+','.join(str(e) for e in self.children)+"]"
- class OrNode(BaseNode):
- def __init__(self, children):
- BaseNode.__init__(self)
- self.children = children
- def execute(self):
- for node in self.children:
- if node.result() == True:
- return True
- return False
- def __str__(self):
- return "Or: ["+','.join(str(e) for e in self.children)+"]"
- true_node_1 = LeafNode("true_node_1", True)
- true_node_2 = LeafNode("true_node_2", True)
- false_node_1 = LeafNode("false_node_1", False)
- some_false_and_node = AndNode([NotNode(true_node_2), true_node_1])
- tree = OrNode([false_node_1, some_false_and_node, some_false_and_node, AndNode([true_node_1, true_node_2])])
- print(tree.result())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement