def createSemiStochasticTransitionButton_clicked_cb(self, event):
        """callback executed when the user requests
        the creation of the transition"""
        currentProject = self.grammarController.getCurrentProject()

        # Remove old transition
        if currentProject is not None:
            automata = currentProject.getGrammar().getAutomata()
            if self.currentTransition is not None:
                automata.removeTransition(self.currentTransition)

        # Retrieve the states and symbols of the current project
        states = []
        symbols = [EmptySymbol(), UnknownSymbol()]
        if currentProject is not None:
            automata = currentProject.getGrammar().getAutomata()
            if automata is not None:
                states.extend(automata.getStates())
            vocabulary = currentProject.getVocabulary()
            if vocabulary is not None:
                symbols.extend(vocabulary.getSymbols())

        # Name of the transition
        nameTransition = self._view.nameSemiStochasticTransitionEntry.get_text(
        )
        if nameTransition is None or len(nameTransition) == 0:
            errorMessage = _("Give a name to the transition.")
            self.displayErrorMessage(errorMessage)
            return

        # Start and End state of the transition
        startState_iter = self._view.startStateSemiStochasticTransitionComboBox.get_active_iter(
        )
        endState_iter = self._view.endStateSemiStochasticTransitionComboBox.get_active_iter(
        )
        if startState_iter is None:
            errorMessage = _("Select a start state.")
            self.displayErrorMessage(errorMessage)
            return
        if endState_iter is None:
            errorMessage = _("Select an end state")
            self.displayErrorMessage(errorMessage)
            return

        idStartState = self._view.startStateSemiStochasticTransitionComboBox.get_model(
        )[startState_iter][0]
        idEndState = self._view.endStateSemiStochasticTransitionComboBox.get_model(
        )[endState_iter][0]
        startState = None
        endState = None
        for state in states:
            if str(state.getID()) == str(idStartState):
                startState = state
            if str(state.getID()) == str(idEndState):
                endState = state
            if startState is not None and endState is not None:
                break
        if startState is None:
            errorMessage = _(
                "An error occurred and prevented to retrieve the provided start state."
            )
            self.displayErrorMessage(errorMessage)
            return
        if endState is None:
            errorMesssage = _(
                "An error occurred and prevented to retrieve the provided end state."
            )
            self.displayErrorMessage(errorMessage)
            return

        # Input Symbol
        inputSymbolIter = self._view.inputSymbolSemiStochasticTransitionComboBox.get_active_iter(
        )
        if inputSymbolIter is None:
            errorMessage = _("Select an input Symbol.")
            self.displayErrorMessage(errorMessage)
            return

        idInputSymbol = self._view.inputSymbolSemiStochasticTransitionComboBox.get_model(
        )[inputSymbolIter][0]
        inputSymbol = None
        for symbol in symbols:
            if str(symbol.getID()) == str(idInputSymbol):
                inputSymbol = symbol
                break
        if inputSymbol is None:
            errorMessage = _(
                "An error occurred and prevented to retrieve the provided input symbol"
            )
            self.displayErrorMessage(errorMessage)
            return

        # Verify the start state doesn't have a transition which has the same input symbol
        found = False
        for startStateTransition in startState.getTransitions():
            if startStateTransition.getType() == SemiStochasticTransition.TYPE:
                if str(startStateTransition.getInputSymbol().getID()) == str(
                        inputSymbol.getID()):
                    found = True
                    break
        if found:
            errorMessage = _(
                "The specified start state already has a transition with the same input symbol."
            )
            self.displayErrorMessage(errorMessage)
            return

        if len(self._view.outputSymbolsListStore) == 0:
            errorMessage = _(
                "Provide at least 1 output symbol (even an EmptySymbol).")
            self.displayErrorMessage(errorMessage)
            return

        # Create the transition
        transition = SemiStochasticTransition(self.idTransition,
                                              nameTransition, startState,
                                              endState, inputSymbol)

        # Now we add all the output symbols
        for row in self._view.outputSymbolsListStore:
            idOutputSymbol = row[0]
            probaOutputSymbol = row[2]
            timeOutputSymbol = row[3]

            outputSymbol = None
            for symbol in symbols:
                if str(symbol.getID()) == str(idOutputSymbol):
                    outputSymbol = symbol
                    break
            if outputSymbol is None:
                errorMessage = _(
                    "An error occurred and prevented to retrieve the output symbols"
                )
                self.displayErrorMessage(errorMessage)
                return

            transition.addOutputSymbol(outputSymbol, probaOutputSymbol,
                                       timeOutputSymbol)

        # Add the transition
        startState.registerTransition(transition)

        # attach the transition to the grammar
        self.grammarController.getCurrentProject().getGrammar().getAutomata(
        ).addTransition(transition)
        self._view.destroy()
        self.grammarController.restart()
Exemplo n.º 2
0
    def computeAutomata(self):
        wordAndStates = []
        startState = None
        idState = 0
        idTransition = 0
        states = []

        self.log.info("Compute the automata...")

        # Create the states of the automata
        uniqueRowsInS = self.getUniqueRowsInS()
        for (w, r) in uniqueRowsInS:
            self.log.info("The row with word {0} is unique !".format(str(w)))
            # We create a State for each unique row
            nameState = self.appendValuesInRow(r)
            self.log.info("Create state: {0}".format(nameState))
            currentState = NormalState(idState, nameState)
            states.append(currentState)
            wordAndStates.append((w, currentState))
            # Is it the starting state (wordS = [EmptySymbol])
            if startState is None and w == MembershipQuery([EmptySymbol()]):
                startState = currentState
                self.log.info("Its the starting state")

            idState = idState + 1

        self.log.debug("Create the transition of the automata")
        # Create the transitions of the automata
        for (word, state) in wordAndStates:
            self.log.debug("Working on state: {0}".format(str(state.getName())))

            for symbol in self.initialD:
                # retrieve the value:
                dicValue = self.observationTable[symbol]
                value = dicValue[word]
                # search for the output state
                mq = word.getMQSuffixedWithMQ(symbol)
                self.log.debug("> What happen when we send " + str(symbol) + " after " + str(word))
                self.log.debug(">> " + str(mq))

                for wordSandSA in self.getSandSAWords():
                    self.log.info("IS " + str(wordSandSA) + " eq " + str(mq))
                    if wordSandSA == mq:
                        self.log.info("YES its equal")
                        rowOutputState = self.getRowOfObservationTable(wordSandSA)
                        outputStateName = self.appendValuesInRow(rowOutputState)
                        self.log.debug("rowOutputState = " + str(rowOutputState))
                        self.log.debug("outputStateName = " + str(outputStateName))

                        # search for the state having this name:
                        outputState = None
                        self.log.info("Search for the output state: {0}".format(outputStateName))
                        for (w2, s2) in wordAndStates:
                            if s2.getName() == outputStateName:
                                outputState = s2
                                self.log.info("  == " + str(s2.getName()))
                            else:
                                self.log.info("   != " + str(s2.getName()))

                        if outputState is not None:
                            inputSymbol = symbol.getSymbolsWhichAreNotEmpty()[0]

                            self.log.info("We create a transition from " + str(state.getName()) + "=>" + str(outputState.getName()))
                            self.log.info(" input: {0}".format(str(inputSymbol)))
                            self.log.info(" output: {0}".format(str(value)))

                            transition = SemiStochasticTransition(idTransition, "Transition " + str(idTransition), state, outputState, inputSymbol)
                            transition.addOutputSymbol(value, 100, 1000)
                            state.registerTransition(transition)

                            idTransition = idTransition + 1

                        else:
                            self.log.error("<!!> Impossible to retrieve the output state named " + str(s2.getName()))

        if startState is not None:
            self.log.info("An infered automata has been computed.")

            self.inferedAutomata = MMSTD(startState, self.dictionary)
            for state in states:
                self.inferedAutomata.addState(state)
            self.log.info(self.inferedAutomata.getDotCode())