def testBinaryNode1(): a = VariableNode(name='a') b = VariableNode(name='b') a_and_b = BinaryNode(fn=lambda x, y: (x and y), name='and', arg1=a, arg2=b) if a_and_b.eval({'a': True, 'b': True}) is True: print('Binary Node Test 1 Passed') else: print('Binary Node Test 1 Failed!')
def testBinaryNode2(): a = VariableNode(name='a') not_a = UnaryNode(fn=lambda x: not (x), name='not', arg1=a) b = VariableNode(name='b') b_and_not_a = BinaryNode(fn=lambda x, y: (x and y), name='and', arg1=b, arg2=not_a) if b_and_not_a.eval({'a': True, 'b': True}) is False: print('Unary Node Test 1 Passed') else: print('Unary Node Test 1 Failed!')
def testUnaryNode1(): a = VariableNode(name='a') not_a = UnaryNode(fn=lambda x: not (x), name='not', arg1=a) if not_a.eval({'a': True}) is False: print('Unary Node Test 1 Passed') else: print('Unary Node Test 1 Failed!')
def testCalcLatency(): a = VariableNode(name='a') b = VariableNode(name='b') a_and_b = BinaryNode(fn=lambda x, y: (x and y), name='and', arg1=a, arg2=b) ans = calcLatency(a_and_b) if (ans != 1): print('calcLatency test 1 failed') else: print('calcLatency test 1 passed') c = UnaryNode(fn=lambda x: x, name='nuthing', arg1=a_and_b) d = BinaryNode(fn=lambda x, y: (x and y), name='and', arg1=c, arg2=a) ans = calcLatency(d) if (ans != 3): print('calcLatency test 2 failed') else: print('calcLatency test 2 passed')
def convertToTreeMultiple(combs): ''' Input: list of list of tuples, where each tuple represents a prime implicant Output: list of head nodes of trees ''' numInputs = len(combs[0]) inputs = set() for comb in combs: for pis in comb: for i, val in enumerate(pis): if (val != '-'): inputs.add(chr(97 + i)) inputNodes = {i: VariableNode(i) for i in inputs} ans = [] done = {} for comb in combs: orQ = deque() for pis in comb: q = deque() for i, val in enumerate(pis): if (val == '1'): q.append(inputNodes[chr(97 + i)]) elif (val == '0'): un = UnaryNode(fn=lambda x: not (x), name='not', arg1=inputNodes[chr(97 + i)]) unStr = repr(un) if unStr not in done: done[unStr] = un q.append(un) else: q.append(done[unStr]) while len(q) > 1: binary = BinaryNode(fn=lambda x, y: (x and y), name='and', arg1=q.popleft(), arg2=q.popleft()) binStr = repr(binary) if binStr not in done: done[binStr] = binary q.append(binary) else: q.append(done[binStr]) orQ.append(q.popleft()) while len(orQ) > 1: binary = BinaryNode(fn=lambda x, y: (x or y), name='or', arg1=orQ.popleft(), arg2=orQ.popleft()) binStr = repr(binary) if binStr not in done: done[binStr] = binary orQ.append(binary) else: orQ.append(done[binStr]) ans.append(orQ.popleft()) return ans
def convertToTree(combs): ''' Input: list of tuples, where each tuple represents a prime implicant Output: head node of tree ''' numInputs = len(combs[0]) inputs = set() for comb in combs: for i, val in enumerate(comb): if (val != '-'): inputs.add(chr(97 + i)) inputNodes = {i: VariableNode(i) for i in inputs} orQ = deque() for comb in combs: q = deque() for i, val in enumerate(comb): if (val == '1'): q.append(inputNodes[chr(97 + i)]) elif (val == '0'): q.append( UnaryNode(fn=lambda x: not (x), name='not', arg1=inputNodes[chr(97 + i)])) while len(q) > 1: q.append( BinaryNode(fn=lambda x, y: (x and y), name='and', arg1=q.popleft(), arg2=q.popleft())) orQ.append(q.popleft()) while len(orQ) > 1: orQ.append( BinaryNode(fn=lambda x, y: (x or y), name='or', arg1=orQ.popleft(), arg2=orQ.popleft())) return orQ.popleft()
def testVariableNode1(): a = VariableNode(name='a') if a.eval({'a': True}) is True: print('Variable Node Test 1 Passed') else: print('Variable Node Test 1 Failed!')
def testVariableNode2(): a = VariableNode(name='a') if a.eval({'a': False}) is False: print('Variable Node Test 2 Passed') else: print('Variable Node Test 2 Failed!')