def __init__(self, show_all=False):
        super().__init__(show_all=show_all)
        self.name = '>='

        r1 = Replacer(['all'], ['red', 'blue'])
        r2 = Replacer(['all'], ['red', 'blue'])
        s1 = Sorter([('red', ), ('blue', )])
        s2 = Sorter([('red', ), ('blue', )])
        greater = GreaterThan(show_all=show_all)
        equal = Equals(show_all=show_all)
        or_block = Or(show_all=show_all)

        self.inputs = [(r1, 0), (r2, 0)]
        self.outputs = [(or_block, 0)]

        self.connections = [
            Connection(r1, s1),
            Connection(r2, s2),
            Connection(s1, greater, comp_from_output=0, comp_to_input=0),
            Connection(s1, equal, comp_from_output=1, comp_to_input=0),
            Connection(s2, greater, comp_from_output=0, comp_to_input=1),
            Connection(s2, equal, comp_from_output=1, comp_to_input=1),
            Connection(greater, or_block),
            Connection(equal, or_block, comp_to_input=1),
        ]

        self.shared_layers = [SharedLayer(r1, r2), SharedLayer(s1, s2)]
    def __init__(self, show_all=False):
        super().__init__(show_all=show_all)
        self.name = '= 2 or 3'

        in2 = IO(balls=['blue'] * 2)
        in3 = IO(balls=['blue'] * 3)

        r1 = Replacer(['all'], ['red', 'blue'])
        s1 = Sorter([('red', ), ('blue', )])
        eq2 = Equals(show_all=show_all)
        eq3 = Equals(show_all=show_all)
        or_block = Or(show_all=show_all)

        self.inputs = [(r1, 0)]
        self.outputs = [(or_block, 0)]

        self.connections = [
            Connection(r1, s1),
            Connection(in2, eq2),
            Connection(s1, eq2, comp_to_input=1),
            Connection(s1, eq3, comp_from_output=1),
            Connection(in3, eq3, comp_to_input=1),
            Connection(eq2, or_block),
            Connection(eq3, or_block, comp_to_input=1),
        ]

        self.shared_layers = [SharedLayer(in2, s1), SharedLayer(s1, in3)]
    def __init__(self, show_all=False):
        super().__init__(show_all=show_all)
        self.name = 'Sub If >='

        r1 = Replacer(['all'], ['red', 'blue'])
        r2 = Replacer(['all'], ['red', 'blue'])
        r3 = Replacer(['red'], ['red', 'blue'])
        r4 = Replacer(['all'], ['red', 'blue'])
        r5 = Replacer(['all'], ['red'])
        r6 = Replacer(['all'], ['red'])
        s1 = Sorter([('red', ), ('blue', )])
        s2 = Sorter([('red', ), ('blue', )])
        s3 = Sorter([('red', ), ('blue', )])
        s4 = Sorter([('red', ), ('blue', )])
        s5 = Sorter([('red', )])
        greater = GreaterEqualThan(show_all=show_all)
        ifelse = IfElse(show_all=show_all)
        hold1 = Hold(show_all=show_all)
        hold2 = Hold(show_all=show_all)
        sub = Subtract(show_all=show_all)

        self.inputs = [(r1, 0), (r2, 0)]
        self.outputs = [(s4, 1), (s5, 0)]

        self.connections = [
            Connection(r1, s1),
            Connection(r2, s2),
            Connection(s1, r3),
            Connection(s1, greater, comp_from_output=1, comp_to_input=0),
            Connection(s2, greater, comp_from_output=0, comp_to_input=1),
            Connection(r3, s3),
            Connection(s3, hold1),
            Connection(greater, ifelse),
            Connection(ifelse, hold1, comp_to_input=1),
            Connection(ifelse, r4, comp_from_output=1),
            Connection(r4, s4),
            Connection(s4, hold2, comp_to_input=1),
            Connection(s3, hold2, comp_from_output=1),
            Connection(hold2, sub),
            Connection(s2, sub, comp_from_output=1, comp_to_input=1),
            Connection(hold1, r5),
            Connection(sub, r6),
            Connection(r5, s5),
            Connection(r6, s5),
        ]

        self.shared_layers = [SharedLayer(r1, r2), SharedLayer(s1, s2)]
def test_machine(machine, show_all, fname=''):
    root = Tk()

    machine = machine(show_all=show_all)

    num_inputs = len(machine.inputs)
    num_outputs = len(machine.outputs)

    inputs = []
    outputs = []

    for i in range(num_inputs):
        inputs.append(IO(name=f'input_{i}'))

    for i in range(num_outputs):
        outputs.append(IO(name=f'output_{i}'))

    m = Machine(root)
    
    i = 0
    for input in inputs:
        m.add_connection(Connection(input, machine, comp_to_input=i))
        i += 1

    i = 0
    for output in outputs:
        m.add_connection(Connection(machine, output, comp_from_output=i))
        m.specify_output(output)
        i += 1

    for comb in itertools.combinations(inputs, 2):
        m.add_shared_layer(SharedLayer(comb[0], comb[1]))

    for comb in itertools.combinations(outputs, 2):
        m.add_shared_layer(SharedLayer(comb[0], comb[1]))

    m.draw()
    if fname != '':
        m.save(fname)
    root.mainloop()
    def __init__(self, show_all=False):
        super().__init__(show_all=show_all)
        self.name = 'FSM Is Odd'

        io1 = IO(balls=['red'])
        to_bin = Binary4Bit(show_all=show_all)
        br4 = BinaryRelease4(show_all=show_all)
        r0 = Replacer(['all'], ['dark green'])
        s1 = Sorter([('blue', 'purple'), ('red', 'dark green'), ('red', ),
                     ('blue', 'dark green'), ('blue', )])
        s2 = Sorter([('red', ), ('blue', ), ('purple', )])
        r1 = Replacer(['red', 'dark green'], ['blue', 'purple'])
        r2 = Replacer(['red'], ['red', 'purple'])
        r3 = Replacer(['blue', 'dark green'], ['blue', 'purple'])
        r4 = Replacer(['blue'], ['red', 'purple'])
        r5 = Replacer(['all'], ['purple'])

        self.inputs = [(to_bin, 0)]
        self.outputs = [(s1, 0)]

        self.connections = [
            Connection(to_bin, br4, comp_from_output=0, comp_to_input=0),
            Connection(to_bin, br4, comp_from_output=1, comp_to_input=1),
            Connection(to_bin, br4, comp_from_output=2, comp_to_input=2),
            Connection(to_bin, br4, comp_from_output=3, comp_to_input=3),
            Connection(io1, s1),
            Connection(br4, r5, comp_from_output=0),
            Connection(br4, r0, comp_from_output=1),
            Connection(br4, r0, comp_from_output=2),
            Connection(br4, r0, comp_from_output=3),
            Connection(br4, r0, comp_from_output=4),
            Connection(r0, s1),
            Connection(r5, s1),
            Connection(s1, r1, comp_from_output=1),
            Connection(s1, r2, comp_from_output=2),
            Connection(s1, r3, comp_from_output=3),
            Connection(s1, r4, comp_from_output=4),
            Connection(r1, s2),
            Connection(r2, s2),
            Connection(r3, s2),
            Connection(r4, s2),
            Connection(s2, s1, comp_from_output=0, reverse=True),
            Connection(s2, s1, comp_from_output=1, reverse=True),
            Connection(s2,
                       br4,
                       comp_from_output=2,
                       comp_to_input=4,
                       reverse=True),
        ]

        self.shared_layers = [SharedLayer(r0, io1)]
    def __init__(self, show_all=False):
        super().__init__(show_all=show_all)
        self.name = '4-Bit\nRelease'

        rort1 = ReleaseOrThru(show_all=show_all)
        rort2 = ReleaseOrThru(show_all=show_all)
        rort3 = ReleaseOrThru(show_all=show_all)
        rort4 = ReleaseOrThru(show_all=show_all)

        self.inputs = [(rort4, 0), (rort3, 0), (rort2, 0), (rort1, 0),
                       (rort1, 1)]
        self.outputs = [(rort4, 1), (rort4, 0), (rort3, 0), (rort2, 0),
                        (rort1, 0)]

        self.connections = [
            Connection(rort1,
                       rort2,
                       comp_from_output=0,
                       comp_to_input=1,
                       reverse=True),
            Connection(rort2,
                       rort3,
                       comp_from_output=0,
                       comp_to_input=1,
                       reverse=True),
            Connection(rort3,
                       rort4,
                       comp_from_output=0,
                       comp_to_input=1,
                       reverse=True)
        ]

        self.shared_layers = [
            SharedLayer(rort1, rort2),
            SharedLayer(rort2, rort3),
            SharedLayer(rort3, rort4),
        ]
    def __init__(self, show_all=False):
        super().__init__(show_all=show_all)
        self.name = 'Sort 2'

        r1 = Replacer(['all'], ['red', 'blue'])
        r2 = Replacer(['all'], ['red', 'blue'])
        s1 = Sorter([('red', ), ('blue', )])
        s2 = Sorter([('red', ), ('blue', )])
        min_block = Min(show_all=show_all)
        max_block = Max(show_all=show_all)

        self.inputs = [(r1, 0), (r2, 0)]
        self.outputs = [(min_block, 0), (max_block, 0)]

        self.connections = [
            Connection(r1, s1),
            Connection(r2, s2),
            Connection(s1, min_block, comp_from_output=0, comp_to_input=0),
            Connection(s1, max_block, comp_from_output=1, comp_to_input=0),
            Connection(s2, min_block, comp_from_output=0, comp_to_input=1),
            Connection(s2, max_block, comp_from_output=1, comp_to_input=1),
        ]

        self.shared_layers = [SharedLayer(r1, r2), SharedLayer(s1, s2)]
    def __init__(self, show_all=False):
        super().__init__(show_all=show_all)
        self.name = 'Sub'

        r1 = Replacer(['all'], ['red'], name=f'{self.name} r1')
        r2 = Replacer(['all'], ['blue'], name=f'{self.name} r2')
        s1 = Sorter([('red', 'blue'), ('red', ), ('blue', )],
                    name=f'{self.name} s1')

        self.inputs = [(r1, 0), (r2, 0)]
        self.outputs = [(s1, 1)]

        self.connections = [
            Connection(r1, s1),
            Connection(r2, s1, comp_to_input=1)
        ]

        self.shared_layers = [SharedLayer(r1, r2)]
示例#9
0
    machine.add_connection(Connection(sorter2, tape2, comp_from_output=4, reverse=True))
    machine.add_connection(Connection(sorter2, tape3, comp_from_output=5, reverse=True))
    machine.add_connection(Connection(sorter2, tape4, comp_from_output=6, reverse=True))
    machine.add_connection(Connection(sorter2, tape5, comp_from_output=7, reverse=True))
    machine.add_connection(Connection(sorter2, tape6, comp_from_output=8, reverse=True))
    machine.add_connection(Connection(sorter2, tape1, comp_from_output=9, reverse=True))
    machine.add_connection(Connection(sorter2, tape2, comp_from_output=10, reverse=True))
    machine.add_connection(Connection(sorter2, tape3, comp_from_output=11, reverse=True))
    machine.add_connection(Connection(sorter2, tape4, comp_from_output=12, reverse=True))
    machine.add_connection(Connection(sorter2, tape5, comp_from_output=13, reverse=True))
    machine.add_connection(Connection(sorter2, tape6, comp_from_output=14, reverse=True))

    machine.add_connection(Connection(sorter2, hold1, comp_from_output=16, comp_to_input=1, reverse=True))
    machine.add_connection(Connection(sorter2, hold2, comp_from_output=17, comp_to_input=1, reverse=True))
    machine.add_connection(Connection(sorter2, hold3, comp_from_output=18, comp_to_input=1, reverse=True))
    machine.add_connection(Connection(sorter2, hold4, comp_from_output=19, comp_to_input=1, reverse=True))
    machine.add_connection(Connection(sorter2, hold5, comp_from_output=20, comp_to_input=1, reverse=True))
    machine.add_connection(Connection(sorter2, hold2, comp_from_output=21, comp_to_input=1, reverse=True))
    machine.add_connection(Connection(sorter2, hold3, comp_from_output=22, comp_to_input=1, reverse=True))
    machine.add_connection(Connection(sorter2, hold4, comp_from_output=23, comp_to_input=1, reverse=True))
    machine.add_connection(Connection(sorter2, hold5, comp_from_output=24, comp_to_input=1, reverse=True))
    machine.add_connection(Connection(sorter2, hold6, comp_from_output=25, comp_to_input=1, reverse=True))

    machine.add_shared_layer(SharedLayer(hold6, init_state))

    machine.draw()
    mainloop()

# A:A# B:B# X:X# Y: Yellow
# Left: Green
# Right:right