コード例 #1
0
    def getNormalForm(self, rows, variables):
        """
        Gets a normal form from a rows.

        Attributes:
            rows      (List): list of rows
            variables (List): list of variables.

        Returns:
            Tree: Normal form tree.
        """
        trees = []
        for row in rows:
            if row[-1] == '1':
                vars = []
                tempTrees = []
                for index, value in enumerate(row[:-1]):
                    if value != '*':
                        if value == '0':
                            vars.append('~' + variables[index])
                        elif value == '1':
                            vars.append(variables[index])
                for var in vars:
                    if '~' in var:
                        tempTree = Tree('~')
                        tempTree.addChild(Tree(var[1:]))
                        tempTrees.append(tempTree)
                    else:
                        tempTrees.append(Tree(var))
                trees.append(self.nodesToTree(tempTrees, '&'))
        return self.nodesToTree(trees, '|')
コード例 #2
0
 def test_parseString(self):
     testedClass = Automota()
     testTree = Tree('&')
     testTree.addChild(Tree('A'))
     testTree.addChild(Tree('~'))
     testTree.right.addChild(Tree('B'))
     testedClass.parseString('&(A,~B)')
     self.assertEqual(testTree, testedClass.tree)
コード例 #3
0
 def test_getValuesForVariables(self):
     testTree = Tree('&')
     testTree.addChild(Tree('A'))
     testTree.addChild(Tree('~'))
     testTree.right.addChild(Tree('B'))
     testedClass = TruthTable(['A', 'B'], testTree)
     expectedValue = [['0', '0'], ['0', '1'], ['1', '0'], ['1', '1']]
     values = testedClass.getValuesForVariables(['A', 'B'])
     self.assertEqual(expectedValue, values)
コード例 #4
0
 def test_findVariables(self):
     testedClass = Automota()
     testTree = Tree('&')
     testTree.addChild(Tree('A'))
     testTree.addChild(Tree('~'))
     testTree.right.addChild(Tree('B'))
     outArray = testedClass.findVariables(testTree)
     outArray.sort()
     self.assertEqual(outArray, ['A', 'B'])
コード例 #5
0
 def test_getRows(self):
     testTree = Tree('&')
     testTree.addChild(Tree('A'))
     testTree.addChild(Tree('~'))
     testTree.right.addChild(Tree('B'))
     testedClass = TruthTable(['A', 'B'], testTree)
     expectedValue = [['0', '0', '0'], ['0', '1', '0'], ['1', '0', '1'],
                      ['1', '1', '0']]
     self.assertEqual(expectedValue, testedClass.rows)
コード例 #6
0
 def test_getNormalForm(self):
     testTree = Tree('>')
     testTree.addChild(Tree('A'))
     testTree.addChild(Tree('B'))
     testedClass = TruthTable(['A', 'B'], testTree)
     values = testedClass.rows
     simplified_table = testedClass.simplify(values)
     normalForm = testedClass.getNormalForm(simplified_table, ['A', 'B'])
     expectedTree = Tree('|')
     expectedTree.addChild(Tree('~'))
     expectedTree.addChild(Tree('B'))
     expectedTree.left.addChild(Tree('A'))
     self.assertEqual(expectedTree, normalForm)
コード例 #7
0
 def test_simplify(self):
     testTree = Tree('|')
     testTree.addChild(Tree('A'))
     testTree.addChild(Tree('|'))
     testTree.right.addChild(Tree('B'))
     testTree.right.addChild(Tree('C'))
     testedClass = TruthTable(['A', 'B', 'C'], testTree)
     expectedValue = [
         ['0', '0', '0', '0'],
         ['*', '*', '1', '1'],
         ['*', '1', '*', '1'],
         ['1', '*', '*', '1'],
     ]
     values = testedClass.rows
     simplified_table = testedClass.simplify(values)
     self.assertEqual(expectedValue, simplified_table)
コード例 #8
0
ファイル: automota.py プロジェクト: dbunin/Logic
    def parseString(self, toParse, parent=None):
        """
        Change to default comparing to the Object.

        Attributes:
            toParse (String): users inputed ASII string
            parent  (Tree): Node of a tree
        """
        komaIndex = toParse.find(',')
        if komaIndex == -1:
            notIndex = toParse.find('~')
            tempTree = None
            if notIndex != -1:
                if notIndex != 0:
                    raise ValueError
                else:
                    toParse = toParse.replace(')', '')
                    toParse = toParse.replace('(', '')
                    tempTree = Tree(toParse[0])
                    tempTree.addChild(Tree(toParse[1:]))
            else:
                toParse = toParse.replace(')', '')
                toParse = toParse.replace('(', '')
                tempTree = Tree(toParse)
            if self.tree is None:
                self.tree = tempTree
            else:
                parent.addChild(tempTree)
            return toParse
        else:
            while toParse[:komaIndex + 1].count('(') != (
                    toParse[:komaIndex].count(',') + 1):
                newIndex = toParse[komaIndex + 1:].find(',') + 1
                komaIndex += newIndex
                if newIndex == -1:
                    raise ValueError()
            sign = toParse[0]
            tree = Tree(sign)
            if self.tree is None:
                self.tree = tree
            else:
                parent.addChild(tree)
            left = self.parseString(toParse[2:komaIndex], tree)
            right = self.parseString(toParse[komaIndex+1:-1], tree)
            if left == -1 or right == -1 or (
                    not tree.checkIfSignIsCorrect(sign)):
                raise ValueError()
コード例 #9
0
 def test_traverseTree(self):
     testedClass = Automota()
     expectedOutput = [
         'node [ fontname = "Arial" ]',
         'node1 [ label = "=" ]',
         'node1 -- node2',
         'node2 [ label = "A" ]',
         'node1 -- node3',
         'node3 [ label = "~" ]',
         'node3 -- node4',
         'node4 [ label = "B" ]']
     inputTree = Tree('=')
     inputTree.addChild(Tree('A'))
     inputTree.addChild(Tree('~'))
     inputTree.right.addChild(Tree('B'))
     result = testedClass.traverseTree(inputTree)
     self.assertEqual(expectedOutput, result)
コード例 #10
0
    def nodesToTree(self, trees, sign):
        """
        Transforms a list of nodes to tree
        and connects the nodes with sign.

        Attributes:
            trees (List): list of rows
            sign  (List): sign to connect nodes with.

        Returns:
            Tree: object of type Tree.
        """
        while len(trees) != 1:
            tree1 = trees[0]
            tree2 = trees[1]
            trees.pop(0)
            trees.pop(0)
            newTree = Tree(sign)
            newTree.addChild(tree1)
            newTree.addChild(tree2)
            trees.append(newTree)
        return trees[0]