def drawer(text, d): term = text.replace(' ', '') print(term) for char in text: if char in ('(', ')', '&', '+', '!'): text = text.replace(char, ' ') args = sorted(set(text.split())) x = 2 coords = {} pl = term.count('+') for arg in args: drawLines(x, arg, coords, pl, d) x += 1 x += 2 if '+' not in term: drawAnd(term, x, 5, coords, d) else: equat = list(set(term.split('+'))) y = 5 for eq in equat: drawAnd(eq, x, y, coords, d) y += 3 x += 5 d.add(l.orgate(inputs=len(equat)), xy=[x, 2 + (3 * (pl + 2)) / 2])
def drawOr(self, coords): y_coord = self.get_y(coords) nots = self.get_nots(coords) print(nots) gate = self.d.add(l.orgate(inputs=len(coords), inputnots=nots), xy=[self.x, y_coord], d='right') count = 1 dict = {} for elem in coords: dict[elem] = 'gate.in' + str(count) count += 1 self.drawConnect(coords, dict, gate) return gate.out
def drawOr(coords, x, y, d): gate = d.add( l.orgate(inputs=len(coords)), xy=[ x, (list(coords.values())[0][1] + list(coords.values())[-1][1]) / 2 ], d='right') count = 1 dict = {} for elem in coords: dict[elem] = 'gate.in' + str(count) count += 1 for elem in coords: d.add(e.LINE, xy=eval(dict[elem]), tox=coords[elem][0], d='left') d.add(e.DOT) return gate.out
def print_ors(d, inputs, all_gates, labels): max_len = max([len(labels[i]) for i in range(len(labels))]) if max_len > 3: rot = 90 else: rot = 0 all_ors = [] func_idxs = set() for x in inputs.values(): func_idxs.update(x) nr_ors = len(func_idxs) for j in range(nr_ors): neg_indexes = [] out_idx = 1 for i, gate in enumerate(all_gates): if j not in gate.get_output_funcs(): continue if gate.is_neg_gate(): neg_indexes.append(out_idx) out_idx += 1 nr_of_inputs = sum(1 for x in all_gates if j in x.get_output_funcs()) all_gates_in_idxs = [ i for i, gate in enumerate(all_gates) if j in gate.get_output_funcs() ] if nr_of_inputs == 1: if len(neg_indexes) == 1: gate_or = logic.NOT GATE_OR = LineWrapper2( d.add(gate_or, d='up', anchor='out', xy=[ all_gates[0].get_output()[0] + j * 4 * d.unit + 2, (all_gates[0].get_output()[1] + 6 * d.unit) ])) GATE_OR.add_label(labels[j], loc='out', ofst=None, align=None, rotation=rot, lblsize=0.04) else: # Change this gate_or = elm.LINE #logic.orgate(nr_of_inputs,inputnots=neg_indexes) GATE_OR = LineWrapper2( d.add(gate_or, d='up', xy=[ all_gates[0].get_output()[0] + j * 4 * d.unit + 2, (all_gates[0].get_output()[1] + 6 * d.unit) ])) GATE_OR.add_label(labels[j], loc='rgt', ofst=None, align=None, rotation=rot, lblsize=0.04) else: gate_or = logic.orgate(nr_of_inputs, inputnots=neg_indexes) GATE_OR = d.add(gate_or, d='up', anchor='out', xy=[ all_gates[0].get_output()[0] + j * 4 * d.unit + 2, (all_gates[0].get_output()[1] + 6 * d.unit) ]) GATE_OR.add_label(labels[j], loc='out', ofst=0.5, align=None, rotation=rot, lblsize=0.04) all_ors.append(OrWrapper(GATE_OR, all_gates_in_indxs=all_gates_in_idxs)) return all_ors
def or_lines(d, f, all_gates, output_label, multi_value=False): l_implicants = len(f) if (l_implicants > 1): if (multi_value): gate_or = logic.orgate(inputs=l_implicants) else: neg_indexes = [ i + 1 for i in range(len(all_gates)) if all_gates[i].is_neg_gate() ] gate_or = logic.orgate(inputs=l_implicants, inputnots=neg_indexes) len_half = l_implicants // 2 if (l_implicants % 2 == 1): GATE_OR = d.add( gate_or, d='right', anchor='out', xy=[ all_gates[0].get_output()[0] + 1.1 * l_implicants, all_gates[l_implicants // 2].get_output()[1] ]) if (multi_value): set_label_on_orinput(GATE_OR, f) Out_line = d.add(elm.LINE, d='right', l=d.unit / 4) Out_line.add_label(label=output_label, ofst=0.3, align=('left', 'bottom')) #print the mid line firt d.add(elm.LINE, d='right', xy=[ all_gates[len_half].get_output()[0], all_gates[len_half].get_output()[1] ], to=(getattr(GATE_OR, 'in' + '{}'.format(len_half))[0], getattr(GATE_OR, 'in' + '{}'.format(len_half))[1])) else: GATE_OR = d.add( gate_or, d='right', anchor='out', xy=[ all_gates[0].get_output()[0] + l_implicants + 1, (all_gates[l_implicants // 2 - 1].get_output()[1] + all_gates[l_implicants // 2].get_output()[1]) / 2 ]) if (multi_value): set_label_on_orinput(GATE_OR, f) Out_line = d.add(elm.LINE, d='right', l=d.unit / 4) Out_line.add_label(label=output_label, ofst=0.3, align=('left', 'bottom')) j = 0 all_gates_reverse = [x for x in reversed(all_gates)] up = 1 down = l_implicants for i in zip(all_gates[:l_implicants // 2], all_gates_reverse[:l_implicants // 2]): j = j + 1.5 L_left = d.add(elm.LINE, d='right', l=l_implicants - j, xy=[i[0].get_output()[0], i[0].get_output()[1]]) L_down = d.add(elm.LINE, d='down', to=(L_left.end[0], getattr(GATE_OR, 'in' + '{}'.format(up))[1])) L_final = d.add(elm.LINE, d='right', to=getattr(GATE_OR, 'in' + '{}'.format(up))) L_left2 = d.add(elm.LINE, d='right', l=l_implicants - j, xy=[i[1].get_output()[0], i[1].get_output()[1]]) L_up = d.add(elm.LINE, d='up', to=(L_left2.end[0], getattr(GATE_OR, 'in' + '{}'.format(down))[1])) L_final2 = d.add(elm.LINE, d='right', to=getattr(GATE_OR, 'in' + '{}'.format(down))) up = up + 1 down = down - 1