Пример #1
0
 def visit(self, node, data):
     if Type(node.left) != ops.NONE and Type(node.right) == ops.NONE:
         #print("prune this 1: ", node)
         batchparser.addAsChildNodeToParent(data, node.left)
     if Type(node.left) == ops.NONE and Type(node.right) != ops.NONE:
         #print("prune this 2: ", node)
         batchparser.addAsChildNodeToParent(data, node.right)
Пример #2
0
 def visit(self, node, data):
     if Type(node.left) != ops.NONE and Type(node.right) == ops.NONE:
         #print("prune this 1: ", node)
         batchparser.addAsChildNodeToParent(data, node.left)            
     if Type(node.left) == ops.NONE and Type(node.right) != ops.NONE:
         #print("prune this 2: ", node)
         batchparser.addAsChildNodeToParent(data, node.right)            
Пример #3
0
    def visit_on(self, node, data):
        #        print("DP finder: ", data.get('visited_pair'))
        if Type(data['parent']) == ops.PAIR or data.get('visited_pair'):
            #self.rule += "False "
            return
        #print("test: right node of prod =>", node.right, ": type =>", node.right.type)
        #print("parent type =>", Type(data['parent']))
#        _type = node.right.type
        if Type(node.right) == ops.MUL:
            # must distribute prod to both children of mul
            r = []
            mul_node = node.right
            self.getMulTokens(mul_node, ops.NONE,
                              [ops.EXP, ops.HASH, ops.PAIR, ops.ATTR], r)
            #for i in r:
            #    print("node =>", i)

            if len(r) == 0:
                pass
            elif len(r) <= 2:
                # in case we're dealing with prod{} on attr1 * attr2
                # no need to simply further, so we can simply return
                if mul_node.left.type == ops.ATTR and mul_node.right.type == ops.ATTR:
                    return

                node.right = None
                prod_node2 = BinaryNode.copy(node)

                # add prod nodes to children of mul_node
                prod_node2.right = mul_node.right
                mul_node.right = prod_node2

                node.right = mul_node.left
                mul_node.left = node
                #self.rule += "True "
                # move mul_node one level up to replace the "on" node.
                batchparser.addAsChildNodeToParent(data, mul_node)
                self.applied = True
            elif len(r) > 2:
                #print("original node =>", node)
                muls = [BinaryNode(ops.MUL) for i in range(len(r) - 1)]
                prod = [BinaryNode.copy(node) for i in r]
                # distribute the products to all nodes in r
                for i in range(len(r)):
                    prod[i].right = r[i]
#                    print("n =>", prod[i])
# combine prod nodes into mul nodes
                for i in range(len(muls)):
                    muls[i].left = prod[i]
                    if i < len(muls) - 1:
                        muls[i].right = muls[i + 1]
                    else:
                        muls[i].right = prod[i + 1]
#                print("final node =>", muls[0])
                batchparser.addAsChildNodeToParent(data, muls[0])
                self.applied = True
                #self.rule += "True "
            else:
                #self.rule += "False "
                return
Пример #4
0
    def visit_on(self, node, data):
#        print("DP finder: ", data.get('visited_pair'))
        if Type(data['parent']) == ops.PAIR or data.get('visited_pair'):
            #self.rule += "False "
            return
        #print("test: right node of prod =>", node.right, ": type =>", node.right.type)
        #print("parent type =>", Type(data['parent']))
#        _type = node.right.type
        if Type(node.right) == ops.MUL:            
            # must distribute prod to both children of mul
            r = []
            mul_node = node.right
            self.getMulTokens(mul_node, ops.NONE, [ops.EXP, ops.HASH, ops.PAIR, ops.ATTR], r)
            #for i in r:
            #    print("node =>", i)
            
            if len(r) == 0:
                pass
            elif len(r) <= 2:
            # in case we're dealing with prod{} on attr1 * attr2 
            # no need to simply further, so we can simply return
                if mul_node.left.type == ops.ATTR and mul_node.right.type == ops.ATTR:
                    return

                node.right = None
                prod_node2 = BinaryNode.copy(node)
            
            # add prod nodes to children of mul_node
                prod_node2.right = mul_node.right
                mul_node.right = prod_node2
            
                node.right = mul_node.left
                mul_node.left = node
                #self.rule += "True "
                # move mul_node one level up to replace the "on" node.
                batchparser.addAsChildNodeToParent(data, mul_node)
                self.applied = True
            elif len(r) > 2:
                #print("original node =>", node)
                muls = [BinaryNode(ops.MUL) for i in range(len(r)-1)]
                prod = [BinaryNode.copy(node) for i in r]
                # distribute the products to all nodes in r
                for i in range(len(r)):
                    prod[i].right = r[i]
#                    print("n =>", prod[i])
                # combine prod nodes into mul nodes                     
                for i in range(len(muls)):
                    muls[i].left = prod[i]
                    if i < len(muls)-1:
                        muls[i].right = muls[i+1]
                    else:
                        muls[i].right = prod[i+1]
#                print("final node =>", muls[0])
                batchparser.addAsChildNodeToParent(data, muls[0])       
                self.applied = True
                #self.rule += "True "
            else:
                #self.rule += "False "
                return                
Пример #5
0
    def visit_of(self, node, data):
        sig = str(node.left.right.attr)

        if sig == self.sig:
            key = BinaryNode(self.getkey('sum'))
            #            if node.right.getAttribute() == 'delta':
            #                self.store(key, node, 'ZR')
            #            else:
            self.store(key, node, 'ZR')  # most likely
            batchparser.addAsChildNodeToParent(data, key)
Пример #6
0
    def visit_of(self, node, data):
        sig = str(node.left.right.attr)

        if sig == self.sig:
            key = BinaryNode(self.getkey('sum'))
#            if node.right.getAttribute() == 'delta':
#                self.store(key, node, 'ZR')
#            else:
            self.store(key, node, 'ZR') # most likely
            batchparser.addAsChildNodeToParent(data, key)
Пример #7
0
    def visit_exp(self, node, data):
        left = node.left
        right = node.right
        if left.type == ops.ATTR:
            if right.type == ops.ATTR:
                key = self.canExpBePrecomputed(left, right)
                if key:
                    # make substitution
                    new_node = BinaryNode(key)
                    batchparser.addAsChildNodeToParent(data, new_node)
                else:
                    pass  # no need to apply any substitutions
            elif right.type == ops.MUL:
                node_1 = right.left
                node_2 = right.right
                if node_1.type == ops.ATTR:
                    key = self.canExpBePrecomputed(left, node_1)
                    if key:
                        # a ^ (b * c) ==> A ^ c (if a^b can be precomputed)
                        new_node1 = BinaryNode(ops.EXP)
                        new_node1.left = BinaryNode(key)
                        new_node1.right = node_2
                        batchparser.addAsChildNodeToParent(data, new_node1)

                if node_2.type == ops.ATTR:
                    key = self.canExpBePrecomputed(left, node_2)
                    if key:
                        # a ^ (b * c) ==> A ^ b (if a^c can be precomputed)
                        new_node2 = BinaryNode(ops.EXP)
                        new_node2.left = BinaryNode(key)
                        new_node2.right = node_1
                        batchparser.addAsChildNodeToParent(data, new_node2)
Пример #8
0
 def visit_exp(self, node, data):
     left = node.left
     right = node.right
     if left.type == ops.ATTR:
         if right.type == ops.ATTR:
             key = self.canExpBePrecomputed(left, right)
             if key:
                 # make substitution
                 new_node = BinaryNode(key)
                 batchparser.addAsChildNodeToParent(data, new_node)
             else:
                 pass # no need to apply any substitutions
         elif right.type == ops.MUL:
             node_1 = right.left
             node_2 = right.right
             if node_1.type == ops.ATTR:
                 key = self.canExpBePrecomputed(left, node_1)
                 if key:
                     # a ^ (b * c) ==> A ^ c (if a^b can be precomputed)
                     new_node1 = BinaryNode(ops.EXP)
                     new_node1.left = BinaryNode(key)
                     new_node1.right = node_2                        
                     batchparser.addAsChildNodeToParent(data, new_node1)
             
             if node_2.type == ops.ATTR:
                 key = self.canExpBePrecomputed(left, node_2)
                 if key:
                     # a ^ (b * c) ==> A ^ b (if a^c can be precomputed)                        
                     new_node2 = BinaryNode(ops.EXP)
                     new_node2.left = BinaryNode(key)
                     new_node2.right = node_1
                     batchparser.addAsChildNodeToParent(data, new_node2)
Пример #9
0
    def visit_on(self, node, data):
        index = str(node.left.right.attr)
        dot_type = self.deriveNodeType(node.right)
        #print("node.right type +=> ", dot_type, node.right)
        #print("index =>", index)

        n = self.searchProd(node.right, node)
        if n:
            (t, p) = n
            #            print("Found it:", t)
            dot_type2 = self.deriveNodeType(t.right)
            # perform substition
            subkey = BinaryNode(self.getkey())
            self.store(subkey, t, dot_type2)
            if p.left == t:
                p.left = subkey
#                print("p =>", p)

        if index == self.sig:
            key = BinaryNode(self.getkey())
            self.store(key, node, dot_type)

            batchparser.addAsChildNodeToParent(data, key)
Пример #10
0
    def visit_on(self, node, data):
        index = str(node.left.right.attr)
        dot_type = self.deriveNodeType(node.right)
        #print("node.right type +=> ", dot_type, node.right)
        #print("index =>", index)

        n = self.searchProd(node.right, node)
        if n:
            (t, p) = n
#            print("Found it:", t)
            dot_type2 = self.deriveNodeType(t.right)
            # perform substition
            subkey = BinaryNode(self.getkey())
            self.store(subkey, t, dot_type2)
            if p.left == t:
                p.left = subkey
#                print("p =>", p)
        
        if index == self.sig:
            key = BinaryNode(self.getkey())
            self.store(key, node, dot_type)
            
            batchparser.addAsChildNodeToParent(data, key)
Пример #11
0
    def visit_exp(self, node, data):
        left = node.left
        right = node.right
        #print("left type =>", Type(left))
        if Type(left) == ops.ATTR:
            if Type(right) == ops.ATTR:
                key = self.canExpBePrecomputed(left, right)
                if key:
                    # make substitution
                    new_node = BinaryNode(key)
                    batchparser.addAsChildNodeToParent(data, new_node)
                else:
                    pass
                    #print("left =>", left)
                    #print("right =>", right)
                    # no need to apply any substitutions
            elif Type(right) == ops.MUL:
                node_1 = right.left
                node_2 = right.right
                #                print("left =>", left)
                #                print("node1 =>", node_1)
                #                print("node2 =>", node_2)
                if Type(node_1) == ops.ATTR:
                    key = self.canExpBePrecomputed(left, node_1)
                    if key:
                        # a ^ (b * c) ==> A ^ c (if a^b can be precomputed)
                        new_node1 = BinaryNode(ops.EXP)
                        new_node1.left = BinaryNode(key)
                        new_node1.right = node_2
                        batchparser.addAsChildNodeToParent(data, new_node1)

                if Type(node_2) == ops.ATTR:
                    key = self.canExpBePrecomputed(left, node_2)
                    if key:
                        # a ^ (b * c) ==> A ^ b (if a^c can be precomputed)
                        new_node2 = BinaryNode(ops.EXP)
                        new_node2.left = BinaryNode(key)
                        new_node2.right = node_1
                        batchparser.addAsChildNodeToParent(data, new_node2)
            elif Type(right) == ops.OF:
                pass
            else:
                print("Substitute: missing some cases: ", Type(right))
Пример #12
0
    def visit_exp(self, node, data):
        left = node.left
        right = node.right
        #print("left type =>", Type(left))
        if Type(left) == ops.ATTR:
            if Type(right) == ops.ATTR:
                key = self.canExpBePrecomputed(left, right)
                if key:
                    # make substitution
                    new_node = BinaryNode(key)
                    batchparser.addAsChildNodeToParent(data, new_node)
                else:
                    pass
                    #print("left =>", left)
                    #print("right =>", right) 
                    # no need to apply any substitutions
            elif Type(right) == ops.MUL:
                node_1 = right.left
                node_2 = right.right
#                print("left =>", left)
#                print("node1 =>", node_1)
#                print("node2 =>", node_2)
                if Type(node_1) == ops.ATTR:
                    key = self.canExpBePrecomputed(left, node_1)
                    if key:
                        # a ^ (b * c) ==> A ^ c (if a^b can be precomputed)
                        new_node1 = BinaryNode(ops.EXP)
                        new_node1.left = BinaryNode(key)
                        new_node1.right = node_2                        
                        batchparser.addAsChildNodeToParent(data, new_node1)
                
                if Type(node_2) == ops.ATTR:
                    key = self.canExpBePrecomputed(left, node_2)
                    if key:
                        # a ^ (b * c) ==> A ^ b (if a^c can be precomputed)                        
                        new_node2 = BinaryNode(ops.EXP)
                        new_node2.left = BinaryNode(key)
                        new_node2.right = node_1
                        batchparser.addAsChildNodeToParent(data, new_node2)
            elif Type(right) == ops.OF:
                pass
            else:
                print("Substitute: missing some cases: ", Type(right))
Пример #13
0
 def visit(self, node, data):
     if self.deleteOtherPair:
         #            print("Type(node) :=", Type(node), node)
         #            print("visit: node.right: ", node.right)
         #            print("visit: node.left: ", node.left)
         if node.left in self.extra_parent:
             if Type(node) != ops.EXP:
                 batchparser.addAsChildNodeToParent(data, node.right)
                 BinaryNode.clearNode(node.left)
             else:
                 # has an exp node therefore treat accordingly
                 #print("warning: found another EXP node: ", node.left, node.right)
                 batchparser.addAsChildNodeToParent(data, node.right)
                 BinaryNode.clearNode(node.left)
                 BinaryNode.clearNode(node.right)
             self.pruneCheck = True
         elif node.right in self.extra_parent:
             batchparser.addAsChildNodeToParent(data, node.left)
             BinaryNode.clearNode(node.right)
             self.pruneCheck = True
         else:
             pass
Пример #14
0
    def visit(self, node, data):
        if self.deleteOtherPair:
#            print("Type(node) :=", Type(node), node)
#            print("visit: node.right: ", node.right)        
#            print("visit: node.left: ", node.left)    
            if node.left in self.extra_parent:
                if Type(node) != ops.EXP:
                    batchparser.addAsChildNodeToParent(data, node.right)
                    BinaryNode.clearNode(node.left)
                else:
                    # has an exp node therefore treat accordingly
                    #print("warning: found another EXP node: ", node.left, node.right)
                    batchparser.addAsChildNodeToParent(data, node.right)
                    BinaryNode.clearNode(node.left)
                    BinaryNode.clearNode(node.right)
                self.pruneCheck = True 
            elif node.right in self.extra_parent:
                batchparser.addAsChildNodeToParent(data, node.left)
                BinaryNode.clearNode(node.right)
                self.pruneCheck = True 
            else:
                pass