<commit_before>
from copy import deepcopy
from util.infinity import INFINITY
from core.match_algo import HimesisMatcher as OldHimesisMatcher
from core.new_match_algo import NewHimesisMatcher
from core.himesis import HConstants as HC
from .rule_primitive import RulePrimitive
from .messages import MatchSet, Match, TransformationException

from core.himesis_utils import print_graph, print_GUIDs, get_preds_and_succs
import traceback

from profiler import *

from util.decompose_graph import decompose_graph

class Matcher(RulePrimitive):
    '''
        Binds the source graph according to the pre-condition pattern.
    '''

    def __init__(self, condition, disambig_matching = False, max=INFINITY):
        '''
            Binds the source graph according to the pre-condition pattern.
            @param condition: The pre-condition pattern.
            @param max: The maximum number of matches.
        '''
        super(Matcher, self).__init__()
        self.max = max
        self.condition = condition

        self.disambig_matching = disambig_matching

        try:
            self.superclasses_dict = condition["superclasses_dict"]
        except KeyError:
            self.superclasses_dict = {}

        if disambig_matching:
            self.HimesisMatcher = NewHimesisMatcher
            self.condition_pred = decompose_graph(condition, get_isolated_match_elements = True)
            self.condition_succ = {}

        else:
            self.HimesisMatcher = OldHimesisMatcher

            #igraph.IN = 2, igraph.OUT = 1
            self.condition_pred, self.condition_succ = get_preds_and_succs(condition)
            #self.condition_pred = [(len(tmp), tmp) for tmp in condition.get_adjlist(mode=2)]
            #self.condition_succ = [(len(tmp), tmp) for tmp in condition.get_adjlist(mode=1)]


        self.NAC_preds = {}
        self.NAC_succs = {}
        for NAC in self.condition.NACs:

            if disambig_matching:
                self.NAC_preds[NAC.name] = decompose_graph(NAC, get_isolated_match_elements = True)
                self.NAC_succs[NAC.name] = {}
            else:
                self.NAC_preds[NAC.name], self.NAC_succs[NAC.name] = get_preds_and_succs(NAC)
            #self.NAC_preds[NAC.name] = [(len(tmp), tmp) for tmp in NAC.get_adjlist(mode=2)]
            #self.NAC_succs[NAC.name] = [(len(tmp), tmp) for tmp in NAC.get_adjlist(mode=1)]


        #0 = ignore errors, 1 = give warnings, 2 = give errors
        self.warning_level = 2

    def print_matcher(self):
        print("Matcher:")
        for attrib in sorted(self.condition.attributes()):
            if attrib == "GUID__":
                continue
            print (str(attrib) + " = " + str(self.condition[attrib]))
        print("Number of nodes: " + str(len(self.condition.vs)))
        #print("MM: " + str(self.condition["mm__"]))
        #print(str(self.condition))

    def __str__(self):
        s = super(Matcher, self).__str__()
        s = s.split(' ')
        #print("Condition: " + str(self.condition))
        s.insert(1, '[%s]' % self.condition.name)
        s.append("\n" + str(self.condition) + "\n")
        return reduce(lambda x, y: '%s %s' % (x,y), s)

    #@do_cprofile
    #@profile
    def packet_in(self, packet, verbosity = 0, preds=[], succs=[]):
        self.exception = None
        self.is_success = False

        #cache the packet graph's neighbours
        #igraph.IN = 2, igraph.OUT = 1


        if self.disambig_matching:
            self.graph_pred = decompose_graph(packet.graph)
            self.graph_succ = {}
        else:

            if preds or succs:
                self.graph_pred = preds
                self.graph_succ = succs
            else:
                self.graph_pred, self.graph_succ = get_preds_and_succs(packet.graph)
        #self.graph_pred = [(len(tmp), tmp) for tmp in packet.graph.get_adjlist(mode=2)]
        #self.graph_succ = [(len(tmp), tmp) for tmp in packet.graph.get_adjlist(mode=1)]

        if self.condition[HC.GUID] in packet.match_sets:
            matchSet = packet.match_sets[self.condition[HC.GUID]]
        else:<commit_msg>Add recording of decomposition time.<commit_after>
from copy import deepcopy
from util.infinity import INFINITY
from core.match_algo import HimesisMatcher as OldHimesisMatcher
from core.new_match_algo import NewHimesisMatcher
from core.himesis import HConstants as HC
from .rule_primitive import RulePrimitive
from .messages import MatchSet, Match, TransformationException
import time

from core.himesis_utils import print_graph, print_GUIDs, get_preds_and_succs
import traceback

from profiler import *

from util.decompose_graph import decompose_graph

from functools import reduce

class Matcher(RulePrimitive):
    '''
        Binds the source graph according to the pre-condition pattern.
    '''

    def __init__(self, condition, disambig_matching = False, max=INFINITY):
        '''
            Binds the source graph according to the pre-condition pattern.
            @param condition: The pre-condition pattern.
            @param max: The maximum number of matches.
        '''
        super(Matcher, self).__init__()
        self.max = max
        self.condition = condition

        self.disambig_matching = disambig_matching

        try:
            self.superclasses_dict = condition["superclasses_dict"]
        except KeyError:
            self.superclasses_dict = {}

        self.decomposing_time = None
        if disambig_matching:
            self.HimesisMatcher = NewHimesisMatcher

            self.decomposing_time = time.time()
            self.condition_pred = decompose_graph(condition, get_isolated_match_elements = True)
            self.decomposing_time = time.time() - self.decomposing_time

            self.condition_succ = {}

        else:
            self.HimesisMatcher = OldHimesisMatcher

            #igraph.IN = 2, igraph.OUT = 1
            self.condition_pred, self.condition_succ = get_preds_and_succs(condition)
            #self.condition_pred = [(len(tmp), tmp) for tmp in condition.get_adjlist(mode=2)]
            #self.condition_succ = [(len(tmp), tmp) for tmp in condition.get_adjlist(mode=1)]


        self.NAC_preds = {}
        self.NAC_succs = {}
        for NAC in self.condition.NACs:

            if disambig_matching:
                self.NAC_preds[NAC.name] = decompose_graph(NAC, get_isolated_match_elements = True)
                self.NAC_succs[NAC.name] = {}
            else:
                self.NAC_preds[NAC.name], self.NAC_succs[NAC.name] = get_preds_and_succs(NAC)
            #self.NAC_preds[NAC.name] = [(len(tmp), tmp) for tmp in NAC.get_adjlist(mode=2)]
            #self.NAC_succs[NAC.name] = [(len(tmp), tmp) for tmp in NAC.get_adjlist(mode=1)]


        #0 = ignore errors, 1 = give warnings, 2 = give errors
        self.warning_level = 2

    def print_matcher(self):
        print("Matcher:")
        for attrib in sorted(self.condition.attributes()):
            if attrib == "GUID__":
                continue
            print (str(attrib) + " = " + str(self.condition[attrib]))
        print("Number of nodes: " + str(len(self.condition.vs)))
        #print("MM: " + str(self.condition["mm__"]))
        #print(str(self.condition))

    def __str__(self):
        s = super(Matcher, self).__str__()
        s = s.split(' ')
        #print("Condition: " + str(self.condition))
        s.insert(1, '[%s]' % self.condition.name)
        s.append("\n" + str(self.condition) + "\n")
        return reduce(lambda x, y: '%s %s' % (x,y), s)

    #@do_cprofile
    #@profile
    def packet_in(self, packet, verbosity = 0, preds=[], succs=[]):
        self.exception = None
        self.is_success = False

        #cache the packet graph's neighbours
        #igraph.IN = 2, igraph.OUT = 1


        if self.disambig_matching:
            self.decomposing_time = time.time()
            self.graph_pred = decompose_graph(packet.graph)
            self.decomposing_time = time.time() - self.decomposing_time
            self.graph_succ = {}
        else:

            if preds or succs:
                self.graph_pred = preds
                self.graph_succ = succs
            else:
                self.graph_pred, self.graph_succ = get_preds_and_succs(packet.graph)
        #self.graph_pred = [(len(tmp), tmp) for tmp in packet.graph.get_adjlist(mode=2)]
        #self.graph_succ = [(len(tmp), tmp) for tmp in packet.graph.get_adjlist(mode=1)]

        if self.condition[HC.GUID] in packet.match_sets:
            matchSet = packet.match_sets[self.condition[HC.GUID]]
        else:
{'fraction_non_alphanumeric': 0.10661850384525752, 'fraction_numerical': 0.0037287345607084597, 'mean_word_length': 2.4373247897476973, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5171318', 'n_tokens_mistral': 2769, 'n_tokens_neox': 2604, 'n_words': 585}