def __init__(self, LHS, RHS): """ Applies the transformation on one match. @param LHS: The pre-condition pattern (LHS + NACs). @param RHS: The post-condition pattern (RHS). """ super(ARule, self).__init__() self.M = Matcher(condition=LHS, max=1) self.I = Iterator(max_iterations=1) self.W = Rewriter(condition=RHS)
def __init__(self, LHS, RHS, inner_rule, outer_first, max_iterations=INFINITY): ''' Applies an inner rule for each application of the outer rule as long as matches can be found. @param LHS: The pre-condition pattern (LHS + NACs). @param RHS: The post-condition pattern (RHS). @param inner_rule: The rule to apply in the loop. @param outer_first: Whether the outer rule should be applied before the inner rule. @param max_iterations: The maximum number of matches of the LHS. ''' super(LSRule, self).__init__(LHS, inner_rule, max_iterations) self.W = Rewriter(condition=RHS) self.outer_first = outer_first
class ARule(Composer): """ Applies the transformation on one match. """ def __init__(self, LHS, RHS): """ Applies the transformation on one match. @param LHS: The pre-condition pattern (LHS + NACs). @param RHS: The post-condition pattern (RHS). """ super(ARule, self).__init__() self.M = Matcher(condition=LHS, max=1) self.I = Iterator(max_iterations=1) self.W = Rewriter(condition=RHS) def packet_in(self, packet): self.exception = None self.is_success = False # Match packet = self.M.packet_in(packet) if not self.M.is_success: self.exception = self.M.exception return packet # Choose the only match packet = self.I.packet_in(packet) if not self.I.is_success: self.exception = self.I.exception return packet # Rewrite packet = self.W.packet_in(packet) if not self.W.is_success: self.exception = self.W.exception return packet # Output success packet self.is_success = True return packet
class LFRule(LRule): """ Applies an inner rule for each application of the outer rule. """ def __init__(self, LHS, RHS, inner_rule, outer_first, max_iterations=INFINITY): """ Applies an inner rule for each application of the outer rule. @param LHS: The pre-condition pattern (LHS + NACs). @param RHS: The post-condition pattern (RHS). @param inner_rule: The rule to apply in the loop. @param outer_first: Whether the outer rule should be applied before the inner rule. @param max_iterations: The maximum number of matches of the LHS. """ super(LFRule, self).__init__(LHS, inner_rule, max_iterations) self.W = Rewriter(condition=RHS) self.outer_first = outer_first self.inner_rule = inner_rule def packet_in(self, packet): self.exception = None self.is_success = False # Match packet = self.M.packet_in(packet) if not self.M.is_success: self.exception = self.M.exception return packet # Choose the first match packet = self.I.packet_in(packet) if not self.I.is_success: self.exception = self.I.exception return packet while True: if self.outer_first: # Rewrite packet = self.W.packet_in(packet) if not self.W.is_success: self.exception = self.W.exception return packet if not self.ignore_resolver: # Resolve any conflicts if necessary packet = self.R.packet_in(packet) if not self.R.is_success: self.exception = self.R.exception return packet # Apply the inner rule packet = self.inner_rule.packet_in(packet) if not self.inner_rule.is_success: self.exception = self.inner_rule.exception return packet if not self.outer_first: # Rewrite packet = self.W.packet_in(packet) if not self.W.is_success: self.exception = self.W.exception return packet if not self.ignore_resolver: # Resolve any conflicts if necessary packet = self.R.packet_in(packet) if not self.R.is_success: self.exception = self.R.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: self.exception = self.I.exception else: # Output success packet self.is_success = True return packet
class LSRule(LRule): ''' Applies an inner rule for each application of the outer rule as long as matches can be found. ''' def __init__(self, LHS, RHS, inner_rule, outer_first, max_iterations=INFINITY): ''' Applies an inner rule for each application of the outer rule as long as matches can be found. @param LHS: The pre-condition pattern (LHS + NACs). @param RHS: The post-condition pattern (RHS). @param inner_rule: The rule to apply in the loop. @param outer_first: Whether the outer rule should be applied before the inner rule. @param max_iterations: The maximum number of matches of the LHS. ''' super(LSRule, self).__init__(LHS, inner_rule, max_iterations) self.W = Rewriter(condition=RHS) self.outer_first = outer_first def packet_in(self, packet): self.exception = None self.is_success = False # Match packet = self.M.packet_in(packet) if not self.M.is_success: self.exception = self.M.exception return packet # Choose the first match packet = self.I.packet_in(packet) if not self.I.is_success: self.exception = self.I.exception return packet while True: if self.outer_first: # Rewrite packet = self.W.packet_in(packet) if not self.W.is_success: self.exception = self.W.exception return packet # Apply the inner rule packet = self.inner_rule.packet_in(packet) if not self.inner_rule.is_success: self.exception = self.inner_rule.exception return packet if not self.outer_first: # Rewrite packet = self.W.packet_in(packet) if not self.W.is_success: self.exception = self.W.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: self.exception = self.I.exception return packet