예제 #1
0
 def repl(a):
     """ Replacement function for every non-overlapping occurrence of pattern in re.subn()
     """
     perforations = a.group(1).split(delimiter)
     parent = SpinTree(or_=True)
     placeholder = "__NODE__" + str(random.randint(0, 10000))
     while placeholder in self.placeholders:
         placeholder = "__NODE__" + str(random.randint(0, 10000))
     insert_index = -1
     for perfo in perforations:
         s_perfo = [perfo]
         for ph in self.placeholders:
             for i, chunk in enumerate(s_perfo):
                 if isinstance(chunk, SpinTree):
                     continue
                 sp = chunk.split(ph)
                 if len(sp) == 2:  # Because every placeholder is unique
                     s_perfo = [
                         node
                         for node in s_perfo[:i] + [sp[0], self.placeholders[ph], sp[1]] + s_perfo[i + 1 :]
                         if node != ""
                     ]
                     insert_index = list(self.placeholders.keys()).index(ph)
                     self.placeholders.pop(ph)
                     break
         if len(s_perfo) == 1 and not isinstance(s_perfo[0], SpinTree):
             child = SpinTree(value=perfo)
         elif len(s_perfo) == 1 and isinstance(s_perfo[0], SpinTree):
             child = s_perfo[0]
         else:
             child = SpinTree(and_=True)
             for and_child in s_perfo:
                 if isinstance(and_child, SpinTree):
                     child.add_child(and_child)
                 else:
                     child.add_child(SpinTree(value=and_child))
         parent.add_child(child)
     # Insert the new placeholder at the right position
     self.placeholders = insert_at_position(self.placeholders, insert_index, (placeholder, parent))
     return placeholder
예제 #2
0
 def repl(a):
     """ Replacement function for every non-overlapping occurrence of pattern in re.subn()
     """
     perforations = a.group(1).split(delimiter)
     parent = SpinTree(or_=True)
     placeholder = '__NODE__'+str(random.randint(0, 10000))
     while placeholder in self.placeholders:
         placeholder = '__NODE__'+str(random.randint(0, 10000))
     insert_index = -1
     for perfo in perforations:
         s_perfo = [perfo]
         for ph in self.placeholders:
             for i, chunk in enumerate(s_perfo):
                 if isinstance(chunk, SpinTree):
                     continue
                 sp = chunk.split(ph)
                 if len(sp) == 2: # Because every placeholder is unique
                     s_perfo = [node for node in s_perfo[:i] + [sp[0], self.placeholders[ph], sp[1]] + s_perfo[i+1:] if node != '']
                     insert_index = list(self.placeholders.keys()).index(ph)
                     self.placeholders.pop(ph)
                     break
         if len(s_perfo) == 1 and not isinstance(s_perfo[0], SpinTree):
             child = SpinTree(value=perfo)
         elif len(s_perfo) == 1 and isinstance(s_perfo[0], SpinTree):
             child = s_perfo[0]
         else:
             child = SpinTree(and_=True)
             for and_child in s_perfo:
                 if isinstance(and_child, SpinTree):
                     child.add_child(and_child)
                 else:
                     child.add_child(SpinTree(value=and_child))
         parent.add_child(child)
     # Insert the new placeholder at the right position
     self.placeholders = insert_at_position(self.placeholders, insert_index, (placeholder, parent))
     return placeholder
예제 #3
0
 def build_tree(self, delimiter='|'):
     """ Build a tree representation of the masterspin
     e.g.:
         masterpin: {a|{b|c}}{d|e}
         tree:
             (AND)
             +--- (OR)
             |    +--- a
             |    +--- (OR)
             |         +--- b
             |         +--- c
             +--- (OR)
                  +--- d
                  +--- e
     """
     def repl(a):
         """ Replacement function for every non-overlapping occurrence of pattern in re.subn()
         """
         perforations = a.group(1).split(delimiter)
         parent = SpinTree(or_=True)
         placeholder = '__NODE__'+str(random.randint(0, 10000))
         while placeholder in self.placeholders:
             placeholder = '__NODE__'+str(random.randint(0, 10000))
         insert_index = -1
         for perfo in perforations:
             s_perfo = [perfo]
             for ph in self.placeholders:
                 for i, chunk in enumerate(s_perfo):
                     if isinstance(chunk, SpinTree):
                         continue
                     sp = chunk.split(ph)
                     if len(sp) == 2: # Because every placeholder is unique
                         s_perfo = [node for node in s_perfo[:i] + [sp[0], self.placeholders[ph], sp[1]] + s_perfo[i+1:] if node != '']
                         insert_index = list(self.placeholders.keys()).index(ph)
                         self.placeholders.pop(ph)
                         break
             if len(s_perfo) == 1 and not isinstance(s_perfo[0], SpinTree):
                 child = SpinTree(value=perfo)
             elif len(s_perfo) == 1 and isinstance(s_perfo[0], SpinTree):
                 child = s_perfo[0]
             else:
                 child = SpinTree(and_=True)
                 for and_child in s_perfo:
                     if isinstance(and_child, SpinTree):
                         child.add_child(and_child)
                     else:
                         child.add_child(SpinTree(value=and_child))
             parent.add_child(child)
         # Insert the new placeholder at the right position
         self.placeholders = insert_at_position(self.placeholders, insert_index, (placeholder, parent))
         return placeholder
     
     masterspin = self.masterspin
     while True:
         masterspin, n = re.subn('{([^{}]*)}',
             repl,
             masterspin)
         if n == 0:
             s_masterspin = [masterspin]
             for ph in self.placeholders:
                 for i, chunk in enumerate(s_masterspin):
                     if isinstance(chunk, SpinTree):
                         continue
                     sp = chunk.split(ph)
                     if len(sp) == 2: # Because every placeholder is unique
                         s_masterspin = [node for node in s_masterspin[:i] + [sp[0], self.placeholders[ph], sp[1]] + s_masterspin[i+1:] if node != '']
                         self.placeholders.pop(ph)
                         break
             if len(s_masterspin) == 1 and not isinstance(s_masterspin[0], SpinTree):
                 return SpinTree(value=masterspin)
             elif len(s_masterspin) == 1 and isinstance(s_masterspin[0], SpinTree):
                 return s_masterspin[0]
             else:
                 final_tree = SpinTree(and_=True)
                 for and_child in s_masterspin:
                     if isinstance(and_child, SpinTree):
                         final_tree.add_child(and_child)
                     else:
                         final_tree.add_child(SpinTree(value=and_child))
                 return final_tree
예제 #4
0
    def build_tree(self, delimiter="|"):
        """ Build a tree representation of the masterspin
        e.g.:
            masterpin: {a|{b|c}}{d|e}
            tree:
                (AND)
                +--- (OR)
                |    +--- a
                |    +--- (OR)
                |         +--- b
                |         +--- c
                +--- (OR)
                     +--- d
                     +--- e
        """

        def repl(a):
            """ Replacement function for every non-overlapping occurrence of pattern in re.subn()
            """
            perforations = a.group(1).split(delimiter)
            parent = SpinTree(or_=True)
            placeholder = "__NODE__" + str(random.randint(0, 10000))
            while placeholder in self.placeholders:
                placeholder = "__NODE__" + str(random.randint(0, 10000))
            insert_index = -1
            for perfo in perforations:
                s_perfo = [perfo]
                for ph in self.placeholders:
                    for i, chunk in enumerate(s_perfo):
                        if isinstance(chunk, SpinTree):
                            continue
                        sp = chunk.split(ph)
                        if len(sp) == 2:  # Because every placeholder is unique
                            s_perfo = [
                                node
                                for node in s_perfo[:i] + [sp[0], self.placeholders[ph], sp[1]] + s_perfo[i + 1 :]
                                if node != ""
                            ]
                            insert_index = list(self.placeholders.keys()).index(ph)
                            self.placeholders.pop(ph)
                            break
                if len(s_perfo) == 1 and not isinstance(s_perfo[0], SpinTree):
                    child = SpinTree(value=perfo)
                elif len(s_perfo) == 1 and isinstance(s_perfo[0], SpinTree):
                    child = s_perfo[0]
                else:
                    child = SpinTree(and_=True)
                    for and_child in s_perfo:
                        if isinstance(and_child, SpinTree):
                            child.add_child(and_child)
                        else:
                            child.add_child(SpinTree(value=and_child))
                parent.add_child(child)
            # Insert the new placeholder at the right position
            self.placeholders = insert_at_position(self.placeholders, insert_index, (placeholder, parent))
            return placeholder

        masterspin = self.masterspin
        while True:
            masterspin, n = re.subn("{([^{}]*)}", repl, masterspin)
            if n == 0:
                s_masterspin = [masterspin]
                for ph in self.placeholders:
                    for i, chunk in enumerate(s_masterspin):
                        if isinstance(chunk, SpinTree):
                            continue
                        sp = chunk.split(ph)
                        if len(sp) == 2:  # Because every placeholder is unique
                            s_masterspin = [
                                node
                                for node in s_masterspin[:i]
                                + [sp[0], self.placeholders[ph], sp[1]]
                                + s_masterspin[i + 1 :]
                                if node != ""
                            ]
                            self.placeholders.pop(ph)
                            break
                if len(s_masterspin) == 1 and not isinstance(s_masterspin[0], SpinTree):
                    return SpinTree(value=masterspin)
                elif len(s_masterspin) == 1 and isinstance(s_masterspin[0], SpinTree):
                    return s_masterspin[0]
                else:
                    final_tree = SpinTree(and_=True)
                    for and_child in s_masterspin:
                        if isinstance(and_child, SpinTree):
                            final_tree.add_child(and_child)
                        else:
                            final_tree.add_child(SpinTree(value=and_child))
                    return final_tree