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()
def createSemiStochasticTransition(self, transitionID, transitionName, startState, stopState): symbols = self.getVocabulary().getSymbols() dialog = gtk.MessageDialog(None, gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_QUESTION, gtk.BUTTONS_OK, None) dialog.set_markup(_("Definition of a SemiStochastic Transition")) mainTable = gtk.Table(rows=9, columns=2, homogeneous=False) inputSymbolTitle = gtk.Label(_("Define input symbol")) inputSymbolTitle.show() mainTable.attach(inputSymbolTitle, 0, 2, 0, 1, xoptions=gtk.FILL, yoptions=0, xpadding=5, ypadding=5) inputSymbolLabel = gtk.Label(_("Dictionary entry:")) inputSymbolLabel.show() inputSymbolCombo = gtk.ComboBox() inputSymbolCombo.set_model(gtk.ListStore(str, str)) # entry name, entry id inputSymbolComboCell = gtk.CellRendererText() inputSymbolCombo.pack_start(inputSymbolComboCell, True) inputSymbolCombo.add_attribute(inputSymbolComboCell, 'text', 0) for symbol in symbols: inputSymbolCombo.get_model().append([symbol.getName(), str(symbol.getID())]) inputSymbolCombo.get_model().append(["EmptySymbol", EmptySymbol.TYPE]) inputSymbolCombo.get_model().append(["UnknownSymbol", UnknownSymbol.TYPE]) inputSymbolCombo.show() mainTable.attach(inputSymbolLabel, 0, 1, 1, 2, xoptions=gtk.FILL, yoptions=0, xpadding=5, ypadding=5) mainTable.attach(inputSymbolCombo, 1, 2, 1, 2, xoptions=gtk.FILL, yoptions=0, xpadding=5, ypadding=5) outputSymbolTitle = gtk.Label(_("Define output symbols")) outputSymbolTitle.show() mainTable.attach(outputSymbolTitle, 0, 2, 2, 3, xoptions=gtk.FILL, yoptions=0, xpadding=5, ypadding=5) outputSymbolLabel = gtk.Label(_("Dictionary entry:")) outputSymbolLabel.show() outputSymbolCombo = gtk.ComboBox() outputSymbolCombo.set_model(gtk.ListStore(str, str, str)) # symbol type, entry name, entry id outputSymbolComboCell = gtk.CellRendererText() outputSymbolCombo.pack_start(outputSymbolComboCell, True) outputSymbolCombo.add_attribute(outputSymbolComboCell, 'text', 1) for symbol in symbols: outputSymbolCombo.get_model().append([symbol.getType(), symbol.getName(), str(symbol.getID())]) outputSymbolCombo.show() outputSymbolCombo.get_model().append([EmptySymbol.TYPE, "EmptySymbol", ""]) outputSymbolCombo.get_model().append([UnknownSymbol.TYPE, "UnknownSymbol", ""]) mainTable.attach(outputSymbolLabel, 0, 1, 3, 4, xoptions=gtk.FILL, yoptions=0, xpadding=5, ypadding=5) mainTable.attach(outputSymbolCombo, 1, 2, 3, 4, xoptions=gtk.FILL, yoptions=0, xpadding=5, ypadding=5) outputTimeLabel = gtk.Label(_("Time:")) outputTimeLabel.show() outputTimeEntry = gtk.Entry() outputTimeEntry.show() mainTable.attach(outputTimeLabel, 0, 1, 5, 6, xoptions=gtk.FILL, yoptions=0, xpadding=5, ypadding=5) mainTable.attach(outputTimeEntry, 1, 2, 5, 6, xoptions=gtk.FILL, yoptions=0, xpadding=5, ypadding=5) outputProbabilityLabel = gtk.Label(_("Probability ([0;100]):")) outputProbabilityLabel.show() outputProbabilityEntry = gtk.Entry() outputProbabilityEntry.show() mainTable.attach(outputProbabilityLabel, 0, 1, 6, 7, xoptions=gtk.FILL, yoptions=0, xpadding=5, ypadding=5) mainTable.attach(outputProbabilityEntry, 1, 2, 6, 7, xoptions=gtk.FILL, yoptions=0, xpadding=5, ypadding=5) removeOutputSymbolButton = gtk.Button(_("Remove")) # removeOutputSymbolButton.connect("clicked", None) removeOutputSymbolButton.show() removeOutputSymbolButton.set_sensitive(False) outputSymbolsTreeStore = gtk.TreeStore(str, str, str, str, str) # type, id, name, time, proba addOutputSymbolButton = gtk.Button(_("Add")) addOutputSymbolButton.connect("clicked", self.addSymbolToTheList, symbols, outputSymbolsTreeStore, outputSymbolCombo, outputTimeEntry, outputProbabilityEntry) addOutputSymbolButton.show() addOutputSymbolButton.set_sensitive(True) mainTable.attach(removeOutputSymbolButton, 0, 1, 7, 8, xoptions=gtk.FILL, yoptions=0, xpadding=5, ypadding=5) mainTable.attach(addOutputSymbolButton, 1, 2, 7, 8, xoptions=gtk.FILL, yoptions=0, xpadding=5, ypadding=5) outputSymbolsScroll = gtk.ScrolledWindow() outputSymbolsTreeView = gtk.TreeView(outputSymbolsTreeStore) outputSymbolsTreeView.get_selection().set_mode(gtk.SELECTION_SINGLE) outputSymbolsTreeView.set_size_request(-1, 250) # outputSymbolsTreeView.connect("cursor-changed", self.actorDetails) outputSymbolsTreeViewCell = gtk.CellRendererText() # col : name of the symbol outputSymbolsTreeViewCol_name = gtk.TreeViewColumn('Symbol name') outputSymbolsTreeViewCol_name.pack_start(outputSymbolsTreeViewCell, True) outputSymbolsTreeViewCol_name.set_attributes(outputSymbolsTreeViewCell, text=2) outputSymbolsTreeView.append_column(outputSymbolsTreeViewCol_name) # col : time of the symbol outputSymbolsTreeViewCol_time = gtk.TreeViewColumn('time') outputSymbolsTreeViewCol_time.pack_start(outputSymbolsTreeViewCell, True) outputSymbolsTreeViewCol_time.set_attributes(outputSymbolsTreeViewCell, text=3) outputSymbolsTreeView.append_column(outputSymbolsTreeViewCol_time) # col : proba of the symbo2 outputSymbolsTreeViewCol_proba = gtk.TreeViewColumn('proba') outputSymbolsTreeViewCol_proba.pack_start(outputSymbolsTreeViewCell, True) outputSymbolsTreeViewCol_proba.set_attributes(outputSymbolsTreeViewCell, text=4) outputSymbolsTreeView.append_column(outputSymbolsTreeViewCol_proba) outputSymbolsTreeView.show() outputSymbolsScroll.add(outputSymbolsTreeView) outputSymbolsScroll.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) outputSymbolsScroll.show() mainTable.attach(outputSymbolsScroll, 0, 2, 8, 9, xoptions=gtk.FILL, yoptions=0, xpadding=5, ypadding=5) dialog.vbox.pack_end(mainTable, True, True, 0) dialog.show_all() result = dialog.run() if result != gtk.RESPONSE_OK: dialog.destroy() return None inputEntryID = inputSymbolCombo.get_model()[inputSymbolCombo.get_active()][1] if inputEntryID == EmptySymbol.TYPE: inputSymbol = EmptySymbol() elif inputEntryID == UnknownSymbol.TYPE: inputSymbol = UnknownSymbol() else: inputEntry = None for symbol in symbols: if str(symbol.getID()) == inputEntryID: inputEntry = symbol if inputEntry == None: self.log.warn(_("Impossible to retrieve the selected input dictionary entry")) dialog.destroy() return inputSymbol = DictionarySymbol(inputEntry) # retrieve the output symbols outputSymbols = [] # [[symbol, proba, time], ...] for outputData in outputSymbolsTreeStore: outputSymbolType = outputData[0] if outputSymbolType == EmptySymbol.TYPE: outputSymbolTime = outputData[3] outputSymbolProba = outputData[4] outputSymbols.append([EmptySymbol(), int(outputSymbolProba), int(outputSymbolTime)]) elif outputSymbolType == UnknownSymbol.TYPE: outputSymbolTime = outputData[3] outputSymbolProba = outputData[4] outputSymbols.append([UnknownSymbol(), int(outputSymbolProba), int(outputSymbolTime)]) else: outputSymbolID = outputData[1] outputSymbolName = outputData[2] outputSymbolTime = outputData[3] outputSymbolProba = outputData[4] outputEntry = None for symbol in symbols: if str(symbol.getID()) == outputSymbolID: outputEntry = symbol if outputEntry == None: self.log.warn(_("Impossible to retrieve the selected output dictionary entry")) dialog.destroy() return outputSymbols.append([DictionarySymbol(outputEntry), int(outputSymbolProba), int(outputSymbolTime)]) # Now we create the transition transition = SemiStochasticTransition(transitionID, transitionName, startState, stopState, inputSymbol) for data in outputSymbols: transition.addOutputSymbol(data[0], data[1], data[2]) startState.registerTransition(transition) dialog.destroy() return transition
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()
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())