<commit_before># Copyright (C) 2011 Orbeon, Inc.
#
# This program is free software; you can redistribute it and/or modify it under the terms of the
# GNU Lesser General Public License as published by the Free Software Foundation; either version
# 2.1 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
# without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU Lesser General Public License for more details.
#
# The full text of the license is available at http://www.gnu.org/copyleft/lesser.html

elementStateMatches = (states, element) ->                                                                              # Case where state is per element
    state = f$.data 'state', element
    ((_.isUndefined state) and (_.contains states, 'initial')) or (_.contains states, state)
globalState = 'initial'                                                                                                 # Case where we have just one global state

ORBEON.util.FiniteStateMachine =
    create: ({ events, elements, conditions, actions, transitions }) ->
        _.each transitions, (transition) ->
            _.each transition.events, (event) ->
                events[event] (event) ->
                    if transition.elements?                                                                             # State if per-element
                        elementsFromEvent = elements[transition.elements]
                        elts = elements[transition.elements] event                                                      # Get elements from event (e.g. editable inside cell for mouseover)
                        if transition.from?                                                                             # Filter elements that are in the 'from' state
                            elts = _.filter elts, (e) -> elementStateMatches transition.from, $ e
                        if transition.conditions                                                                        # Filter elements that match all the conditions
                            elts = _.filter elts, (e) ->
                                _.all transition.conditions, (c) -> conditions[c] $ e
                        _.each elts, (element) ->
                            f$.data 'state', transition.to, $ element                                                   # Change state before running action, so if action trigger an event, that event runs against the new state
                            _.each transition.actions, (action) -> actions[action] $ element                            # Run all the actions on the elements
                    else                                                                                                # State is global (just one state)
                        if not transition.from? or -1 != _.indexOf transition.from, globalState
                            conditionsMet = true
                            if transition.conditions
                                _.each transition.conditions, (condition) ->
                                    conditionsMet = false if not condition(event)
                            if conditionsMet == true
                                globalState = transition.to
                                _.each transition.actions, (action) -> actions[action] event<commit_msg>Fix: `condition` has to be dereferenced<commit_after># Copyright (C) 2011 Orbeon, Inc.
#
# This program is free software; you can redistribute it and/or modify it under the terms of the
# GNU Lesser General Public License as published by the Free Software Foundation; either version
# 2.1 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
# without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU Lesser General Public License for more details.
#
# The full text of the license is available at http://www.gnu.org/copyleft/lesser.html

elementStateMatches = (states, element) ->                                                                              # Case where state is per element
    state = f$.data 'state', element
    ((_.isUndefined state) and (_.contains states, 'initial')) or (_.contains states, state)
globalState = 'initial'                                                                                                 # Case where we have just one global state

ORBEON.util.FiniteStateMachine =
    create: ({ events, elements, conditions, actions, transitions }) ->
        _.each transitions, (transition) ->
            _.each transition.events, (eventName) ->
                events[eventName] (event) ->
                    if transition.elements?                                                                             # State if per-element
                        elementsFromEvent = elements[transition.elements]
                        elts = elements[transition.elements] event                                                      # Get elements from event (e.g. editable inside cell for mouseover)
                        if transition.from?                                                                             # Filter elements that are in the 'from' state
                            elts = _.filter elts, (e) -> elementStateMatches transition.from, $ e
                        if transition.conditions                                                                        # Filter elements that match all the conditions
                            elts = _.filter elts, (e) ->
                                _.all transition.conditions, (c) -> conditions[c] $ e
                        _.each elts, (element) ->
                            f$.data 'state', transition.to, $ element                                                   # Change state before running action, so if action trigger an event, that event runs against the new state
                            _.each transition.actions, (action) -> actions[action] $ element                            # Run all the actions on the elements
                    else                                                                                                # State is global (just one state)
                        if not transition.from? or -1 != _.indexOf transition.from, globalState
                            conditionsMet = true
                            if transition.conditions
                                _.each transition.conditions, (condition) ->
                                    conditionsMet = false if not conditions[c](event)
                            if conditionsMet == true
                                globalState = transition.to if transition.to?
                                _.each transition.actions, (action) ->
                                    actions[action] event
{'fraction_non_alphanumeric': 0.06289040318001135, 'fraction_numerical': 0.0019875070982396364, 'mean_word_length': 1.1710323574730355, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 55, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7061904', 'n_tokens_mistral': 1308, 'n_tokens_neox': 1265, 'n_words': 554}