示例#1
0
    def minimize_clicked(self):
        if self.validate_input():
            self.bigEditor.clear()

            mts = self.mterm_edit.text().split(' ')

            #remove any duplicates from the list
            #remove any non integers from the list
            mts = list(set(filter(lambda x: self.representsInt(x), mts)))

            dcs = self.dcare_edit.text().split(' ')

            #remove any non integers from the list
            dcs = list(set(filter(lambda x: self.representsInt(x), dcs)))

            vs = self.v_edit.text().split(' ')
            vs = list(filter(lambda x: x, vs))

            qm = QM(mts, dcs, vs)
            pis = qm.pis()
            qm.primary_epis()
            self.bigEditor.append('Prime Implicants')

            print(qm.procedure)
            for pi in pis:
                self.bigEditor.append(qm.procedure)
                self.bigEditor.append('\n')
示例#2
0
 def test_combine_generation_wrong(self):
     #test the combine generation with an empty generation
     minterms = [1, 2, 3, 4, 5, 6, 15]
     qm = QM(minterms)
     #expected pass test case
     self.assertNotEqual(
         qm.combine_generation([['_00_'], ['_0_1'], ['__11']]), [['____']])
示例#3
0
 def test_combine_generation_no_new_generation(self):
     #test the combine generation with a generation that does not result in offspring
     minterms = [1, 2, 3, 4, 5, 6, 15]
     qm = QM(minterms)
     #expected pass test case
     self.assertEqual(qm.combine_generation([['_00_'], ['_0_1'], ['__11']]),
                      [])
示例#4
0
 def test_length_convert_to_binary(self):
     #check if the number of bits in each binary string is the same for
     #all
     minterms = [1, 2, 3, 4, 5, 6, 15]
     qm = QM(minterms)
     conversion = qm.to_binary(minterms)
     for term in conversion:
         self.assertEqual(len(term), 4)
示例#5
0
 def test_can_cover(self):
     minterms = [1, 2, 3, 4, 5, 6, 15]
     qm = QM(minterms)
     #expected pass test case
     self.assertTrue(qm.can_cover('_00_', '0000'))
     self.assertTrue(qm.can_cover('_00_', '1001'))
     self.assertFalse(qm.can_cover('_11_', '0000'))
     self.assertFalse(qm.can_cover('_00_', '0110'))
示例#6
0
    def test_pis(self):
        #test the combine generation with an empty generation
        minterms = [1, 2, 3, 4, 5, 6, 15]
        qm = QM(minterms)
        #expected pass test case

        self.assertEqual(
            qm.pis(), ['1111', '00_1', '0_01', '001_', '0_10', '010_', '01_0'])
示例#7
0
 def test_to_char(self):
     minterms = [1, 2, 3, 4, 5, 6, 9, 11, 15]
     qm = QM(minterms)
     #expected pass test case
     self.assertEqual(qm.to_char('_00_', ['a', 'b', 'c', 'd']), "b'c'")
     self.assertEqual(qm.to_char('_11_', ['a', 'b', 'c', 'd']), "bc")
     self.assertEqual(qm.to_char('____', ['a', 'b', 'c', 'd']), "")
     self.assertEqual(qm.to_char('0000', ['a', 'b', 'c', 'd']), "a'b'c'd'")
     self.assertEqual(qm.to_char('1111', ['a', 'b', 'c', 'd']), "abcd")
示例#8
0
 def test_group_minterms(self):
     #test the combine generation with an empty generation
     minterms = [1, 2, 3, 4, 5, 6, 15]
     qm = QM(minterms)
     #expected pass test case
     self.assertEqual(
         qm.group_minterms(
             ['0000', '0001', '0010', '0100', '0011', '0101', '1111']),
         [['0000'], ['0001', '0010', '0100'], ['0011', '0101'], ['1111']])
示例#9
0
 def test_combine_generation_no_dash(self):
     minterms = [1, 2, 3, 4, 5, 6, 15]
     qm = QM(minterms)
     #expected pass test case
     self.assertEqual(
         qm.combine_generation([['0000'], ['0001', '1000'],
                                ['0011', '1001', '1100'], ['0111', '1011'],
                                ['1111']]),
         [['000_', '_000'], ['00_1', '_001', '100_', '1_00'],
          ['0_11', '_011', '10_1'], ['_111', '1_11']])
示例#10
0
 def test_combine_generation_dash(self):
     #test the combine generation with a generation that has dashes
     minterms = [1, 2, 3, 4, 5, 6, 15]
     qm = QM(minterms)
     #expected pass test case
     self.assertEqual(
         qm.combine_generation([['000_', '_000'],
                                ['00_1', '_001', '100_', '1_00'],
                                ['0_11', '_011', '10_1'], ['_111',
                                                           '1_11']]),
         [['_00_'], ['_0_1'], ['__11']])
示例#11
0
    def test_convert_binary(self):

        #test if the strings are the actual binary representations
        #for each of the strings
        minterms = [1, 2, 3, 4, 5, 6, 15]
        qm = QM(minterms)
        conversion = qm.to_binary(minterms)
        expected_conversion = [
            '0001', '0010', '0011', '0100', '0101', '0110', '1111'
        ]
        self.assertEqual(conversion, expected_conversion)
示例#12
0
    def test_combine_length(self):
        #test for values that differ in length
        #valueerror exception should be thrown
        minterms = [1, 2, 3, 4, 5, 6, 15]
        qm = QM(minterms)
        with self.assertRaises(ValueError):
            qm.combine('00000', '0001'), ValueError

        #test for values that are the same
        #None should be returned
        self.assertEqual(qm.combine('0000', '0000'), None)
示例#13
0
 def test_to_binary(self):
     
     #test if the strings are the actual binary representations
     #for each of the strings
     minterms = [1,2,3,4,5,6,15]
     qm =  QM(minterms)
     expected_conversion = ['0001','0010','0011','0100','0101','0110','1111']
     conversion = qm.to_binary(minterms)
     self.assertEqual(conversion,expected_conversion)
     
     #check if the number of bits in each binary string is the same for 
     #all
     for term in conversion:
         self.assertEqual(len(term),4)
示例#14
0
    def test_combine(self):
        minterms = [1, 2, 3, 4, 5, 6, 15]
        qm = QM(minterms)
        x = qm.minimize()
        y = []
        for t in x:
            y += t.split('+')

        y = [t.strip() for t in y]

        self.assertEqual(
            sorted(y),
            sorted([
                '1111', '0_10', '00_1', '010_', '1111', '0_01', '001_', '01_0'
            ]))
示例#15
0
    def test_combine_single_values(self):
        #test for two terms that are not supposed to be combined
        #expected return value should be None
        minterms = [1, 2, 3, 4, 5, 6, 15]
        qm = QM(minterms)

        self.assertEqual(qm.combine('0000', '1001'), None)
        #test for values(without _ ) that differ by exactly one position
        #expected return value should have a new value with a _ in the positiion of difference

        self.assertEqual(qm.combine('0000', '0001'), '000_')
        #test for values(without _ ) that differ by exactly one position
        #expected return value should have a new value with a _ in the positiion of difference

        self.assertEqual(qm.combine('000_', '100_'), '_00_')
示例#16
0
 def test_combine_groups(self):
     #test combination for some random groups
     
     minterms = [1,2,3,4,5,6,15]
     qm =  QM(minterms)
     self.assertEqual(qm.combine_groups(['0001','1000'],['0011','1001','1100']),['00_1','_001','100_','1_00'])
     self.assertEqual(qm.combine_groups(['0000'],['0001','1000']),['000_','_000'])
     self.assertEqual(qm.combine_groups([],['0001','1000']),[])
     self.assertEqual(qm.combine_groups([],[]),[])
示例#17
0
    def test_combine(self):
        #test for two terms that are not supposed to be combined
        #expected return value should be None
        minterms = [1,2,3,4,5,6,15]
        qm =  QM(minterms)

        self.assertEqual(qm.combine('0000','1001'),None)
        #test for values(without _ ) that differ by exactly one position 
        #expected return value should have a new value with a _ in the positiion of difference

        self.assertEqual(qm.combine('0000','0001'),'000_')
        #test for values(without _ ) that differ by exactly one position 
        #expected return value should have a new value with a _ in the positiion of difference

        self.assertEqual(qm.combine('000_','100_'),'_00_')

        #test for values that differ in length 
        #valueerror exception should be thrown
        with self.assertRaises(ValueError):
            qm.combine('00000','0001'),ValueError

        #test for values that are the same 
        #None should be returned
        self.assertEqual(qm.combine('0000','0000'),None)
示例#18
0
 def test_other_pis(self):
     minterms = [1, 2, 3, 4, 5, 6, 9, 11, 15]
     qm = QM(minterms)
     #expected pass test case
     self.assertEqual(qm.secondary_epis(), [])
示例#19
0
def maid(argssop='',argsdont_cares='',argsvariables='',minterms=[]):
    
    if argssop:
        sop = argssop.split('+')
        
        varSet = set()
        for _ in sop:
            for __ in _:
                if __ != "'":varSet.add(__)
        argsvariables = ','.join(varSet)

        variables = argsvariables.split(',')

        number_list = {}
        for num in range(len(variables) ** 2 - 1):
            bin_num = str(bin(num)[2:])
            while len(bin_num) < len(variables):
                bin_num = '0' + bin_num
            number_list[bin_num] = num

        dic = {}
        list_dic = []
        for product in sop:

            for variable in product:

                if ord(variable) in range(65, 123) and product.index(variable) + 1 < len(product) and product[
                    product.index(variable) + 1] == "'":
                    dic[variable] = 0

                elif ord(variable) in range(65, 123):
                    dic[variable] = 1

            list_dic.append(dic)
            dic = {}
        result = []
        for mt in list_dic:
            for num in number_list:
                count = 0
                for i in range(len(variables)):
                    if variables[i] in mt and int(num[i]) == mt[variables[i]]:
                        count += 1
                if len(mt) == count:
                    result.append(number_list[num])
        minterms = list(set(result))

    # make sure all the values in the values entered for minterms are valid integers
    if not minterms:
        return 'Error: sum of product values expected for minterms'

    for mt in minterms:
        # if it is not a whitespace and it is not an integer
        if (mt and not representsInt(mt)) or ((mt and representsInt(mt)) and int(mt) < 0):
            return 'Error: Integer values expected for minterms'

    # make sure all the values in the values entered for dont cares are valid integers
    if argsdont_cares:
        dcares = argsdont_cares.split(',')
        # make sure the don't cares are all integer values
        for dc in dcares:
            if (dc and not representsInt(dc)) or ((dc and representsInt(dc)) and int(dc) < 0):
                return 'Error : Integer values expected for don\'t cares'

            # a term cannot be a don't care and a minterm at the same time
            if dc in minterms:
                return 'Error: A term cannot be a minterm and a don\'t care at the same time'

    else:
        dcares = []

    ##################################add validation for variables here ####################
    if argsvariables:
        variables = argsvariables.split(',')

        # filter out duplicates in the variables entered

        # if there were duplicate terms then let the user know
        if len(variables) != len(list(set(variables))):
            return "Error: Duplicate terms not allowed for variables"
        # make sure the variables entered are enough to represent the expression
        # else raise a value error exception and close the program

        # check the number of variables needed
        # take into consideration the minter's as well

        mterms = map(lambda x: int(x), minterms)
        dcs = map(lambda x: int(x), dcares)
        max_minterm = max(list(mterms) + list(dcs))

        max_minterm = bin(max_minterm)[2:]

        if len(variables) != len(max_minterm):
            return "Error: Number of variables entered is not enough to represent expression"
    else:
        variables = []

    qm = QM(minterms, dcares, variables)
    sols = qm.minimize()
    outputStr='\n'.join(sols)

    return outputStr
示例#20
0
    max_minterm = bin(max_minterm)[2:]

    if len(variables) != len(max_minterm):
        sys.exit(
            "Error: Number of variables entered is not enough to represent expression"
        )
else:
    variables = []

# make sure show steps is either a yes or a no
if args.show_steps.lower() != 'yes' and args.show_steps.lower() != 'no':
    sys.exit('show_steps expects yes or no')
print(minterms)
# simply expression and print solution if necessary
qm = QM(minterms, dcares, variables)
# pis = qm.pis()
# epis = qm.primary_epis()
# sepis =  qm.secondary_epis()

sols = qm.minimize()
if args.show_steps == 'yes':
    print(qm.procedure)

else:
    print('Solution')
    print(sols[0])

    if len(sols) > 1:
        for i in range(1, len(sols)):
            print(sols[i])
示例#21
0
 def test_combine_generation_empty_generation(self):
     #test the combine generation with an empty generation
     minterms = [1, 2, 3, 4, 5, 6, 15]
     qm = QM(minterms)
     #expected pass test case
     self.assertEqual(qm.combine_generation([]), [])