<commit_before>from grammar.triggerGrammar import TriggerParser
from grammar.triggerExpressions import BExpression
from grammar.consequenceGrammar import ConsequenceParser
from grammar.tokenGrammar import TokenParametersParser
from database import Property, Event, UNDEFINED_PARAMETER, ParameterizedExpression
from lrparsing import LrParsingError


class TokenParseException(Exception):
    def __init__(self, node, tokenText, parseError):
        super(TokenParseException, self).__init__(
            TokenParseException.getMessage(node, tokenText, parseError))

    @staticmethod
    def getMessage(node, tokenText, parseError):
        s1 = 'Error while parsing token of node ' + str(node.num)
        s2 = 'Token text : ' + tokenText
        s3 = str(parseError)
        return s1 + '\n' + s2 + '\n' + s3


class StateMachine:
    def __init__(self):
        self._tokens = set([])
        self._nodes = {}
        self.i = 0
        self.gameWindow = None

    def setGameWindow(self, gw):
        self.gameWindow = gw

    def addTokenByNodeNum(self, nodeNum, args, kwargs):
        node = self.getNodeByNum(nodeNum)
        token = Token(node, args, kwargs)
        self._tokens.add(token)

    def addToken(self, node, tokenStr):
        try:
            token = Token(node, *TokenParametersParser.parse(tokenStr))
        except LrParsingError as e:
            raise TokenParseException(node, tokenStr, e)
        self._tokens.add(token)

    def removeToken(self, token):
        self._tokens.remove(token)

    def clearTokens(self):
        self._tokens.clear()

    def updateTokensNbFrames(self):
        for token in self._tokens:
            token.oneMoreFrame()

    def addNode(self, num, label):
        node = Node(num, label)
        self._nodes[num] = node
        return node

    def getNodeByNum(self, num):
        return self._nodes[num]

    def clearNodes(self):
        self._nodes.clear()

    def init(self):
        self.clearTokens()
        Property.properties.clear()
        Event.events.clear()

    def tick(self):
        print self._tokens, Property.properties, Event.events
        from itertools import chain

        # bug? la variable token est iteree a partir de la liste tokens
        # cependant la variable token dans les valeurs chain.from ... token.node.outputArcs)
        # est toujours egale au premier token alors que la cles est iteree correctement
        # On se retrouve donc avec le dictionnaire suivant :
        # {token1 : chain...(token1), token2 : chain...(token1), token3 : chain...(token1), ...}
        # tokenEvaluations = {token: chain.from_iterable(((evaluation, tr) for evaluation in tr.eval(token))
        #                                               for tr in token.node.outputArcs) for token in tokens}

        # la version suivant fonctionne correctement
        tokenEvaluations = {}
        for token in self._tokens:
            tokenEvaluations[token] = chain.from_iterable(((evaluation, tr) for evaluation in tr.eval(token))
                                                          for tr in token.node.outputArcs)

        locks = {}
        transitionGen = {}

        toCheck = self._tokens
        while len(toCheck) > 0:
            toRecheck = set([])
            for token in toCheck:
                evaluations = tokenEvaluations[token]
                try:
                    evaluation, tr = evaluations.next()
                    locked = False
                    if len(evaluation.locks) > 0:
                        for keys, prio in evaluation.locks.iteritems():
                            try:
                                if locks[keys][0] > prio:
                                    toRecheck |= locks[keys][1]
                                    for token2 in locks[keys][1]:
                                        del transitionGen[token2]
                                    locks[keys] = (prio, set([token]))
                                elif locks[keys][0] == prio:
                                    locks[keys][1].add(token)
                                else:
                                    locked = True
                            except KeyError:
                                locks[keys] = (prio, set([token]))
                    if locked:
                        toRecheck.add(token)
                    else:
                        transitionGen[token] = (tr, evaluation.variables)
                except StopIteration:
                    pass
            toCheck = toRecheck
        Event.events.clear()

        if len(transitionGen) == 0:
            return False

        for token in transitionGen:
            tr, evaluation = transitionGen[token]
            token.moveTo(tr.n2)
            tr.applyConsequences(evaluation, self, token)
            # for consType, cons in consequences:
            #     if cons is None:
            #         continue
            #     if consType == ADD_PROPERTY_CONSEQUENCE:
            #         prop = Property(cons[0], cons[1])
            #         Property.properties.add(prop)
            #     elif consType == REMOVE_PROPERTY_CONSEQUENCE:
            #         Property.removeAll(cons[0], cons[1])
            #     elif consType == EDIT_PROPERTY_CONSEQUENCE:
            #         Property.edit(cons[0], cons[1], cons[2])
            #     elif consType == ADD_EVENT_CONSEQUENCE:
            #         event = Event(cons[0], cons[1])
            #         Event.events.add(event)
            #     elif consType == ADD_SPRITE_CONSEQUENCE:
            #         gameWindow.addSprite(cons[0], cons[1], cons[2], cons[3])
            #     elif consType == REMOVE_SPRITE_CONSEQUENCE:
            #         try:
            #             gameWindow.removeSprite(cons)
            #         except KeyError:
            #             pass
            #     elif consType == MOVE_SPRITE_CONSEQUENCE:
            #         try:
            #             gameWindow.moveSprite(cons[0], cons[1], cons[2])
            #         except KeyError:
            #             pass
            #     elif consType == EDIT_SPRITE_CONSEQUENCE:
            #         try:
            #             gameWindow.editSprite(cons[0], cons[1])
            #         except KeyError:
            #             pass
            #     elif consType == ADD_TOKEN_CONSEQUENCE:
            #         node = self.getNodeByNum(cons[0])
            #         newToken = Token(node, cons[1])
            #         self._tokens.add(newToken)
            #     elif consType == EDIT_TOKEN_CONSEQUENCE:
            #         token.setArgs(cons)
            #     elif consType == REMOVE_TOKEN_CONSEQUENCE:
            #         self.removeToken(token)
        return True

<commit_msg>Remove useless code<commit_after>from grammar.triggerGrammar import TriggerParser
from grammar.triggerExpressions import BExpression
from grammar.consequenceGrammar import ConsequenceParser
from grammar.tokenGrammar import TokenParametersParser
from database import Property, Event, UNDEFINED_PARAMETER, ParameterizedExpression
from lrparsing import LrParsingError


class TokenParseException(Exception):
    def __init__(self, node, tokenText, parseError):
        super(TokenParseException, self).__init__(
            TokenParseException.getMessage(node, tokenText, parseError))

    @staticmethod
    def getMessage(node, tokenText, parseError):
        s1 = 'Error while parsing token of node ' + str(node.num)
        s2 = 'Token text : ' + tokenText
        s3 = str(parseError)
        return s1 + '\n' + s2 + '\n' + s3


class StateMachine:
    def __init__(self):
        self._tokens = set([])
        self._nodes = {}
        self.i = 0
        self.gameWindow = None

    def setGameWindow(self, gw):
        self.gameWindow = gw

    def addTokenByNodeNum(self, nodeNum, args, kwargs):
        node = self.getNodeByNum(nodeNum)
        token = Token(node, args, kwargs)
        self._tokens.add(token)

    def addToken(self, node, tokenStr):
        try:
            token = Token(node, *TokenParametersParser.parse(tokenStr))
        except LrParsingError as e:
            raise TokenParseException(node, tokenStr, e)
        self._tokens.add(token)

    def removeToken(self, token):
        self._tokens.remove(token)

    def clearTokens(self):
        self._tokens.clear()

    def updateTokensNbFrames(self):
        for token in self._tokens:
            token.oneMoreFrame()

    def addNode(self, num, label):
        node = Node(num, label)
        self._nodes[num] = node
        return node

    def getNodeByNum(self, num):
        return self._nodes[num]

    def clearNodes(self):
        self._nodes.clear()

    def init(self):
        self.clearTokens()
        Property.properties.clear()
        Event.events.clear()

    def tick(self):
        print self._tokens, Property.properties, Event.events
        from itertools import chain

        # bug? la variable token est iteree a partir de la liste tokens
        # cependant la variable token dans les valeurs chain.from ... token.node.outputArcs)
        # est toujours egale au premier token alors que la cles est iteree correctement
        # On se retrouve donc avec le dictionnaire suivant :
        # {token1 : chain...(token1), token2 : chain...(token1), token3 : chain...(token1), ...}
        # tokenEvaluations = {token: chain.from_iterable(((evaluation, tr) for evaluation in tr.eval(token))
        #                                               for tr in token.node.outputArcs) for token in tokens}

        # la version suivant fonctionne correctement
        tokenEvaluations = {}
        for token in self._tokens:
            tokenEvaluations[token] = chain.from_iterable(((evaluation, tr) for evaluation in tr.eval(token))
                                                          for tr in token.node.outputArcs)

        locks = {}
        transitionGen = {}

        toCheck = self._tokens
        while len(toCheck) > 0:
            toRecheck = set([])
            for token in toCheck:
                evaluations = tokenEvaluations[token]
                try:
                    evaluation, tr = evaluations.next()
                    locked = False
                    if len(evaluation.locks) > 0:
                        for keys, prio in evaluation.locks.iteritems():
                            try:
                                if locks[keys][0] > prio:
                                    toRecheck |= locks[keys][1]
                                    for token2 in locks[keys][1]:
                                        del transitionGen[token2]
                                    locks[keys] = (prio, set([token]))
                                elif locks[keys][0] == prio:
                                    locks[keys][1].add(token)
                                else:
                                    locked = True
                            except KeyError:
                                locks[keys] = (prio, set([token]))
                    if locked:
                        toRecheck.add(token)
                    else:
                        transitionGen[token] = (tr, evaluation.variables)
                except StopIteration:
                    pass
            toCheck = toRecheck
        Event.events.clear()

        if len(transitionGen) == 0:
            return False

        for token in transitionGen:
            tr, evaluation = transitionGen[token]
            token.moveTo(tr.n2)
            tr.applyConsequences(evaluation, self, token)

        return True


{'fraction_non_alphanumeric': 0.08699784017278618, 'fraction_numerical': 0.0058747300215982725, 'mean_word_length': 1.5576668139637648, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2920444', 'n_tokens_mistral': 3013, 'n_tokens_neox': 2949, 'n_words': 746}