示例#1
0
 def __init__(self, LHS, RHS, max_iterations=INFINITY):
     """
         Applies the transformation on all matches found with roll-back capability.
         @param LHS: The pre-condition pattern (LHS + NACs).
         @param RHS: The post-condition pattern (RHS).
         @param max_iterations: The maximum number of times to match.
     """
     super(XSRule, self).__init__(LHS, RHS, max_iterations)
     # max_iterations=1 because no all matches have been exhausted after first application
     self.B = Rollbacker(condition=LHS, max_iterations=1)
示例#2
0
 def __init__(self, LHS, RHS, max_iterations=INFINITY):
     '''
         Applies the transformation on one match with roll-back capability.
         @param LHS: The pre-condition pattern (LHS + NACs).
         @param RHS: The post-condition pattern (RHS).
     '''
     # external_matches_only=True because further matches of this rule are only processed after a roll-back
     super(XRule, self).__init__(LHS, RHS)
     self.M.max = max_iterations
     self.I.max_iterations = max_iterations
     self.B = Rollbacker(condition=LHS, max_iterations=max_iterations)
示例#3
0
class XSRule(SRule):
    """
        Applies the transformation as long as matches can be found with roll-back capability.
    """

    def __init__(self, LHS, RHS, max_iterations=INFINITY):
        """
            Applies the transformation on all matches found with roll-back capability.
            @param LHS: The pre-condition pattern (LHS + NACs).
            @param RHS: The post-condition pattern (RHS).
            @param max_iterations: The maximum number of times to match.
        """
        super(XSRule, self).__init__(LHS, RHS, max_iterations)
        # max_iterations=1 because no all matches have been exhausted after first application
        self.B = Rollbacker(condition=LHS, max_iterations=1)

    def packet_in(self, packet):
        self.exception = None
        self.is_success = False
        # Checkpoint the original packet
        self.B.packet_in(packet)
        if not self.B.is_success:
            self.exception = self.B.exception
            return packet
        # Match
        packet = self.M.packet_in(packet)
        if not self.M.is_success:
            packet = self.B.restore()
            if self.M.exception:
                self.exception = self.M.exception
            elif self.B.exception:
                self.exception = self.B.exception
            return packet
        # Choose the first match
        packet = self.I.packet_in(packet)
        if not self.I.is_success:
            packet = self.B.restore()
            if self.I.exception:
                self.exception = self.I.exception
            elif self.B.exception:
                self.exception = self.B.exception
            return packet
        while True:
            # Rewrite
            packet = self.W.packet_in(packet)
            if not self.W.is_success:
                packet = self.B.restore()
                if self.W.exception:
                    self.exception = self.W.exception
                elif self.B.exception:
                    self.exception = self.B.exception
                return packet
            # Rule has been applied once, so it's a success anyway
            self.is_success = True
            if self.I.iterations == self.I.max_iterations:
                return packet
            # Re-Match
            packet = self.M.packet_in(packet)
            if not self.M.is_success:
                self.exception = self.M.exception
                return packet
            # Choose another match
            packet = self.I.next_in(packet)
            # No more iterations are left
            if not self.I.is_success:
                if self.I.exception:
                    packet = self.B.restore()
                    if self.B.exception:
                        self.exception = self.B.exception
                    self.exception = self.I.exception
                    self.is_success = False
                return packet

    def next_in(self, packet):
        # Only one roll-back
        self.exception = None
        self.is_success = False
        packet = self.B.next_in(packet)
        if not self.B.is_success:
            self.exception = self.B.exception
        return packet
示例#4
0
class XFRule(FRule):
    '''
        Applies the transformation on all matches found with roll-back capability.
    '''
    def __init__(self, LHS, RHS, max_iterations=INFINITY):
        '''
            Applies the transformation on all matches found with roll-back capability.
            @param LHS: The pre-condition pattern (LHS + NACs).
            @param RHS: The post-condition pattern (RHS).
            @param max_iterations: The maximum number of times to apply the rule.
        '''
        super(XFRule, self).__init__(LHS, RHS, max_iterations)
        # max_iterations=1 because no all matches have been exhausted after first application
        self.B = Rollbacker(condition=LHS, max_iterations=1)
    
    def packet_in(self, packet):
        self.exception = None
        self.is_success = False
        # Checkpoint the original packet
        self.B.packet_in(packet)
        if not self.B.is_success:
            self.exception = self.B.exception
            return packet
        # Match
        packet = self.M.packet_in(packet)
        if not self.M.is_success:
            packet = self.B.restore(packet)
            if self.M.exception:
                self.exception = self.M.exception
            elif self.B.exception:
                self.exception = self.B.exception
            return packet
        # Choose the first match
        packet = self.I.packet_in(packet)
        if not self.I.is_success:
            packet = self.B.restore(packet)
            if self.I.exception:
                self.exception = self.I.exception
            elif self.B.exception:
                self.exception = self.B.exception
            return packet
        while True:
            # Rewrite
            packet = self.W.packet_in(packet)
            if not self.W.is_success:
                packet = self.B.restore(packet)
                if self.W.exception:
                    self.exception = self.W.exception
                elif self.B.exception:
                    self.exception = self.B.exception
                return packet
            # Choose another match
            packet = self.I.next_in(packet)
            # No more iterations are left
            if not self.I.is_success:
                if self.I.exception:
                    packet = self.B.restore(packet)
                    if self.B.exception:
                        self.exception = self.B.exception
                    self.exception = self.I.exception
                    self.is_success = False
                else:
                    # Output success packet
                    self.is_success = True
                return packet
    
    def next_in(self, packet):
        # Only one roll-back
        self.exception = None
        self.is_success = False
        packet = self.B.next_in(packet)
        if not self.B.is_success:
            self.exception = self.B.exception
        return packet
示例#5
0
class XRule(ARule):
    '''
        Applies the transformation on one match with roll-back capability.
    '''
    def __init__(self, LHS, RHS, max_iterations=INFINITY):
        '''
            Applies the transformation on one match with roll-back capability.
            @param LHS: The pre-condition pattern (LHS + NACs).
            @param RHS: The post-condition pattern (RHS).
        '''
        # external_matches_only=True because further matches of this rule are only processed after a roll-back
        super(XRule, self).__init__(LHS, RHS)
        self.M.max = max_iterations
        self.I.max_iterations = max_iterations
        self.B = Rollbacker(condition=LHS, max_iterations=max_iterations)
    
    def packet_in(self, packet):
        self.exception = None
        self.is_success = False
        # Checkpoint the original packet
        self.B.packet_in(packet)
        if not self.B.is_success:
            self.exception = self.B.exception
            return packet
        # Match
        packet = self.M.packet_in(packet)
        if not self.M.is_success:
            packet = self.B.restore(packet)
            if self.M.exception:
                self.exception = self.M.exception
            elif self.B.exception:
                self.exception = self.B.exception
            return packet
        # Choose one match
        packet = self.I.packet_in(packet)
        if not self.I.is_success:
            packet = self.B.restore(packet)
            if self.I.exception:
                self.exception = self.I.exception
            elif self.B.exception:
                self.exception = self.B.exception
            return packet
        # Rewrite
        packet = self.W.packet_in(packet)
        if not self.W.is_success:
            packet = self.B.restore(packet)
            if self.W.exception:
                self.exception = self.W.exception
            elif self.B.exception:
                self.exception = self.B.exception
            return packet
        self.is_success = True
        return packet
    
    def next_in(self, packet):
        self.exception = None
        self.is_success = False
        packet = self.B.next_in(packet)
        if not self.B.is_success:
            self.exception = self.B.exception
            return packet
        # Choose the next match
        packet = self.I.packet_in(packet)
        if not self.I.is_success:
            packet = self.B.next_in(packet)
            if self.I.exception:
                self.exception = self.I.exception
            elif self.B.exception:
                self.exception = self.B.exception
            return packet
        # Rewrite
        packet = self.W.packet_in(packet)
        if not self.W.is_success:
            packet = self.B.next_in(packet)
            if self.W.exception:
                self.exception = self.W.exception
            elif self.B.exception:
                self.exception = self.B.exception
            return packet
        # Output success packet
        self.is_success = True
        return packet