<commit_before>#!/usr/bin/env python

from pypepa.logger import init_log
from pypepa.exceptions import DeadlockError

class StateSpace():
    operators = []
    components = []
    comp_ss = None

    def __init__(self):
        self.max_length = 0
        self.log = init_log()

    def _combine_states(self, states):
        """ When more than one transition leads to the same state,
            the function combines these transitions into one with actions rewritten
        """
        for i in list( range(0, len(states))):
            if states[i] is not None and len(states[i].to_s) == 1:
                continue
            if states[i] is not None:
                for j in list (range(i+1, len(states))):
                    if states[j] is not None:
                        if states[i].to_s == states[j].to_s:
                            states[i].actions = {}
                            states[i].actions[states[i].action] = states[i].rate
                            states[i].combined = True
                            states[i].action += "," + states[j].action
                            states[i].rate =  float(states[i].rate) + float(states[j].rate)
                            states[i].actions[states[j].action] = float(states[j].rate)
                            states[j] = None
        states = [i for i in states if i is not None]
        return states

    def derive(self, alg="BU", dotdir=None):
        if alg == "BU":
            return self.derive_bu(dotdir=dotdir)


    def derive_bu(self,dotdir=None):
        """ Derives the whole state space using Bottom-up algorithm""" 
        initial_state = []
        queue = []
        visited = []
        resulting_states = {}
        actions_to_state = {}
        state_num = 0
        for x in list(range(0,self.max_length+1,1)):
            for op in self.operators:
                if op.length == x:
                    op.update_offset()
        for comp in self.components:
            initial_state.append(comp.name)
        queue.append(initial_state)
        while(queue):
            state = queue.pop(0)
            if self._gs_to_string(state) in visited:
              continue
            state_num = state_num + 1
            self.log.debug("{} STATE {}".format(state_num, state))
            resulting_states[self._gs_to_string(state)] = ([],state_num)
            visited.append(self._gs_to_string(state))
            # update components table (same refs are in operators) -->
            # new state
            for i in list( range(0, len( state ),1)):
                self.components[i].update( state[i] )
            # for every length can be changed
            for x in list(range(0,self.max_length+1,1)):
                for op in self.operators:
                    if op.length == x:
                        # Runs through all operators, if operator is the
                        # top one it composes
                        if self.max_length == op.length:
                            new_states = []
                            new_states = op.compose(self.comp_ss, state, True)
                            if not new_states:
                                self.log.error("DEADLOCK in {}".format(state))
                                raise DeadlockError("Deadlock in state: {}".format(state))
                            new_states = self._combine_states(new_states[:])
                            for news in new_states:
                                # aggregate
                                if len(news.to_s) < self.max_length:
                                    new_state = state[:]
                                    new_state[news.offset] = news.to_s[0]
                                    news.to_s = new_state
                                self.log.debug("{}\trate={} \t-> {}".format(news.action, news.arate, news.to_s))
                                resulting_states[self._gs_to_string(state)][0].append( (news.rate, self._gs_to_string(news.to_s), news.action))
                                #handle combines actions, not very elegant so to be changed
                                if news.combined:
                                    for act in news.actions:
                                        self._add_to_actions_set(act, news.actions[act], actions_to_state, state_num)
                                else:
                                    self._add_to_actions_set(news.action, news.rate, actions_to_state, state_num)

                                if self._gs_to_string(news.to_s) not in visited:
                                    queue.append(news.to_s)
                        else:
                            op.compose(self.comp_ss , state , False)
        return (resulting_states, actions_to_state)

    def _add_to_actions_set(self, action, rate,actions_to_state, state_num):
        if rate == "infty":
            self.log.error("Resulting rate cannot be infty in {} in state {}".format(action, state_num))
            raise ValueError("Resulting rate cannot be infty in {} in state {}".format(action, state_num))
        if (action,state_num) not in actions_to_state:
            actions_to_state[ (action, state_num) ] = float(rate)
        else:
            #check if the same stateand action, so we add rates
            actions_to_state[ (action, state_num) ] += float(rate)

    def _gs_to_string(self, gs_list):
        return ','.join( map( str, gs_list ) )

class Component():
    length = None
    offset = None
    name = None
    ss = None
    data = None


    def update(self, name):
        """
        Update with new name
        """
        self.name
        self.name = name
        self.data = name #TODO: wyjebac
        self.derivatives = []
        arates = {}
        for der in self.ss[self.name].transitions:
            if der.action not in arates:
                if der.rate == "infty":
                    arates[der.action] = float(-1)
                else:
                    arates[der.action] = float(der.rate)
            else:
                arates[der.action] += float(der.rate)
        for der in self.ss[self.name].transitions:
            # print("self.name:%s der.action:%s der.rate:%s de.to:%s" % (self.name, der.action, der.rate, der.to))
            if der.rate == "infty":<commit_msg>Removed a small typo in state space, which was an expression with no side-effect<commit_after>#!/usr/bin/env python

from pypepa.logger import init_log
from pypepa.exceptions import DeadlockError

class StateSpace():
    operators = []
    components = []
    comp_ss = None

    def __init__(self):
        self.max_length = 0
        self.log = init_log()

    def _combine_states(self, states):
        """ When more than one transition leads to the same state,
            the function combines these transitions into one with actions rewritten
        """
        for i in list( range(0, len(states))):
            if states[i] is not None and len(states[i].to_s) == 1:
                continue
            if states[i] is not None:
                for j in list (range(i+1, len(states))):
                    if states[j] is not None:
                        if states[i].to_s == states[j].to_s:
                            states[i].actions = {}
                            states[i].actions[states[i].action] = states[i].rate
                            states[i].combined = True
                            states[i].action += "," + states[j].action
                            states[i].rate =  float(states[i].rate) + float(states[j].rate)
                            states[i].actions[states[j].action] = float(states[j].rate)
                            states[j] = None
        states = [i for i in states if i is not None]
        return states

    def derive(self, alg="BU", dotdir=None):
        if alg == "BU":
            return self.derive_bu(dotdir=dotdir)


    def derive_bu(self,dotdir=None):
        """ Derives the whole state space using Bottom-up algorithm""" 
        initial_state = []
        queue = []
        visited = []
        resulting_states = {}
        actions_to_state = {}
        state_num = 0
        for x in list(range(0,self.max_length+1,1)):
            for op in self.operators:
                if op.length == x:
                    op.update_offset()
        for comp in self.components:
            initial_state.append(comp.name)
        queue.append(initial_state)
        while(queue):
            state = queue.pop(0)
            if self._gs_to_string(state) in visited:
              continue
            state_num = state_num + 1
            self.log.debug("{} STATE {}".format(state_num, state))
            resulting_states[self._gs_to_string(state)] = ([],state_num)
            visited.append(self._gs_to_string(state))
            # update components table (same refs are in operators) -->
            # new state
            for i in list( range(0, len( state ),1)):
                self.components[i].update( state[i] )
            # for every length can be changed
            for x in list(range(0,self.max_length+1,1)):
                for op in self.operators:
                    if op.length == x:
                        # Runs through all operators, if operator is the
                        # top one it composes
                        if self.max_length == op.length:
                            new_states = []
                            new_states = op.compose(self.comp_ss, state, True)
                            if not new_states:
                                self.log.error("DEADLOCK in {}".format(state))
                                raise DeadlockError("Deadlock in state: {}".format(state))
                            new_states = self._combine_states(new_states[:])
                            for news in new_states:
                                # aggregate
                                if len(news.to_s) < self.max_length:
                                    new_state = state[:]
                                    new_state[news.offset] = news.to_s[0]
                                    news.to_s = new_state
                                self.log.debug("{}\trate={} \t-> {}".format(news.action, news.arate, news.to_s))
                                resulting_states[self._gs_to_string(state)][0].append( (news.rate, self._gs_to_string(news.to_s), news.action))
                                #handle combines actions, not very elegant so to be changed
                                if news.combined:
                                    for act in news.actions:
                                        self._add_to_actions_set(act, news.actions[act], actions_to_state, state_num)
                                else:
                                    self._add_to_actions_set(news.action, news.rate, actions_to_state, state_num)

                                if self._gs_to_string(news.to_s) not in visited:
                                    queue.append(news.to_s)
                        else:
                            op.compose(self.comp_ss , state , False)
        return (resulting_states, actions_to_state)

    def _add_to_actions_set(self, action, rate,actions_to_state, state_num):
        if rate == "infty":
            self.log.error("Resulting rate cannot be infty in {} in state {}".format(action, state_num))
            raise ValueError("Resulting rate cannot be infty in {} in state {}".format(action, state_num))
        if (action,state_num) not in actions_to_state:
            actions_to_state[ (action, state_num) ] = float(rate)
        else:
            #check if the same stateand action, so we add rates
            actions_to_state[ (action, state_num) ] += float(rate)

    def _gs_to_string(self, gs_list):
        return ','.join( map( str, gs_list ) )

class Component():
    length = None
    offset = None
    name = None
    ss = None
    data = None


    def update(self, name):
        """
        Update with new name
        """
        self.name = name
        self.data = name #TODO: wyjebac
        self.derivatives = []
        arates = {}
        for der in self.ss[self.name].transitions:
            if der.action not in arates:
                if der.rate == "infty":
                    arates[der.action] = float(-1)
                else:
                    arates[der.action] = float(der.rate)
            else:
                arates[der.action] += float(der.rate)
        for der in self.ss[self.name].transitions:
            # print("self.name:%s der.action:%s der.rate:%s de.to:%s" % (self.name, der.action, der.rate, der.to))
            if der.rate == "infty":
{'fraction_non_alphanumeric': 0.10258641145297794, 'fraction_numerical': 0.002847425452819742, 'mean_word_length': 1.433878729547642, 'pattern_counts': {'":': 8, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 26, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2114658', 'n_tokens_mistral': 3490, 'n_tokens_neox': 3389, 'n_words': 861}