コード例 #1
0
        private void btnConvertToMealy_Click(object sender, RoutedEventArgs e)
        {
            MealyTransitionDiagram diagTemp = null;

            if (diagram.states.Count != 0 && diagram.transitions.Count != 0)
            {
                diagTemp = ((MooreTransitionDiagram)diagram).convertToMealy();
            }
            else
            {
                MessageBox.Show("Please draw the transition diagram first");
            }

            if (diagTemp != null)//is null on errors
            {
                diagram = diagTemp;
                setInterfaceOnMachineLoad();

                this.machine = machines.Mealy;
                btnConvertToMoore.Visibility = Visibility.Visible;
                btnConvertToMealy.Visibility = Visibility.Collapsed;
                this.ribbonMain.Title        = "Mealy Machine";
                loadedFilePath = "";
            }
        }
コード例 #2
0
        private void btnClear_Click(object sender, RoutedEventArgs e)
        {
            if (canvasMain.Children.Count == 0)
            {
                return;
            }
            else
            {
                if (MessageBox.Show("Are you sure to clear the Page", "Clear?", MessageBoxButton.YesNo) == MessageBoxResult.No)
                {
                    return;
                }
            }

            grpStateOptions.IsEnabled = false;
            grpTransOptions.IsEnabled = false;
            btnInitial.IsChecked      = btnFinal.IsChecked = false;

            TransitionDiagram newDiag = null;

            switch (machine)
            {
            case machines.Dfa:
                newDiag = new DfaTransitionDiagram();
                break;

            case machines.Nfa:
                newDiag = new NfaTransitionDiagram();
                break;

            case machines.Moore:
                newDiag = new MooreTransitionDiagram();
                break;

            case machines.Mealy:
                newDiag = new MealyTransitionDiagram();
                break;

            case machines.Turing:
                newDiag = new TuringTransitionDiagram();
                break;
            }

            if (newDiag != null)
            {
                newDiag.inputs     = diagram.inputs;
                newDiag.noOfInputs = diagram.noOfInputs;
                newDiag.outputs    = diagram.outputs;
                canvasMain.Children.Clear();
                newDiag.setCanvas(canvasMain);
                diagram = newDiag;
            }
        }
コード例 #3
0
        public void newMachine(machines type, TransitionDiagram newDiagram = null)
        {
            if (newDiagram != null) // don't clear the canvas, i.e. we are only updating the interface on conversions etc
            {
                ;
            }
            else
            {
                if (saveOnNewOrExit() == MessageBoxResult.Cancel)
                {
                    return;
                }

                fileStatus.isEdited = false;
                loadedFilePath      = "";

                grammar = null;
                pda     = null;

                mode = modes.Machines;
                GrammarTab.Visibility = pdaTab.Visibility = Visibility.Collapsed;

                transitionDiagramTab.Visibility = Visibility.Visible;
                ExecuteTab.Visibility           = Visibility.Visible;
                ribbonMain.Visibility           = Visibility.Visible;
                transitionDiagramTab.IsSelected = true;

                clearAllTextBoxes();
            }

            //restore original settings

            btnFinal.IsChecked    = btnInitial.IsChecked = false;
            btnAddState.IsChecked = btnAddTrans.IsChecked = btnSelect.IsChecked = false;
            btnNull.IsChecked     = false;

            grpSymbols.IsEnabled   = false;
            grpToolbox.IsEnabled   = false;
            grpSelection.IsEnabled = false;

            btnStatistics.IsEnabled = false;

            grpStateOptions.IsEnabled = false;
            btnFinal.IsEnabled        = true;// disabled in turing, moore, mealy

            grpTransOptions.IsEnabled = false;

            grpStringAcceptance.Visibility = Visibility.Collapsed;
            grpOutputProducer.Visibility   = Visibility.Collapsed;

            btnNull.IsEnabled = false;


            //because they are changed in turing machine interface
            txtInput.Width = txtOutput.Width = 155;

            lblInput.Content  = "Input   "; //changed to input tape in turnig
            lblOutput.Content = "Output";   //changed to output tape in turnig

            grpDfaOptions.Visibility = grpNfaOptions.Visibility = grpMooreMealyOptions.Visibility = Visibility.Collapsed;

            switch (type)
            {
            case machines.Dfa:

                machine = machines.Dfa;
                if (newDiagram == null)
                {
                    newDiagram = new DfaTransitionDiagram();
                }

                diagram = newDiagram;

                spInputSymbols.IsEnabled  = true;
                spOutputSymbols.IsEnabled = false;

                grpDfaOptions.Visibility             = Visibility.Visible;
                grpStringAcceptance.Visibility       = Visibility.Visible;
                Application.Current.MainWindow.Title = "DFA";
                break;

            case machines.Nfa:
                machine = machines.Nfa;

                if (newDiagram == null)
                {
                    newDiagram = new NfaTransitionDiagram();
                }

                diagram = newDiagram;

                btnNullClosure.IsEnabled = false;    // to be enabled on state selection

                spInputSymbols.IsEnabled  = true;
                spOutputSymbols.IsEnabled = false;
                btnNull.IsEnabled         = true;


                grpNfaOptions.Visibility             = Visibility.Visible;
                grpStringAcceptance.Visibility       = Visibility.Visible;
                Application.Current.MainWindow.Title = "NFA";
                break;

            case machines.Moore:
                machine = machines.Moore;

                if (newDiagram == null)
                {
                    newDiagram = new MooreTransitionDiagram();
                }

                diagram = newDiagram;

                spInputSymbols.IsEnabled  = true;
                spOutputSymbols.IsEnabled = true;
                btnFinal.IsEnabled        = false;

                grpMooreMealyOptions.Visibility = Visibility.Visible;
                btnConvertToMoore.Visibility    = Visibility.Collapsed;
                btnConvertToMealy.Visibility    = Visibility.Visible;

                grpMooreMealyOptions.Header = "Moore Options";

                grpOutputProducer.Visibility         = Visibility.Visible;
                Application.Current.MainWindow.Title = "Moore Machine";
                break;

            case machines.Mealy:
                machine = machines.Mealy;

                if (newDiagram == null)
                {
                    newDiagram = new MealyTransitionDiagram();
                }

                diagram = newDiagram;

                spInputSymbols.IsEnabled  = true;
                spOutputSymbols.IsEnabled = true;
                btnFinal.IsEnabled        = false;

                grpMooreMealyOptions.Visibility = Visibility.Visible;
                btnConvertToMoore.Visibility    = Visibility.Visible;
                btnConvertToMealy.Visibility    = Visibility.Collapsed;
                grpMooreMealyOptions.Header     = "Mealy Options";

                grpOutputProducer.Visibility         = Visibility.Visible;
                Application.Current.MainWindow.Title = "Mealy Machine";
                break;

            case machines.Turing:
                machine = machines.Turing;

                if (newDiagram == null)
                {
                    newDiagram = new TuringTransitionDiagram();
                }

                diagram = newDiagram;

                lblInput.Content  = "Input Tape   ";
                lblOutput.Content = "Output Tape";

                spInputSymbols.IsEnabled  = true;
                spOutputSymbols.IsEnabled = false;
                btnFinal.IsEnabled        = false;

                grpOutputProducer.Visibility = Visibility.Visible;
                txtInput.Width = txtOutput.Width = 500;
                Application.Current.MainWindow.Title = "Turing Machine";
                break;

            case machines.None:
                return;     // deselected above
            }



            //enable common options
            grpSymbols.IsEnabled    = true;
            grpToolbox.IsEnabled    = true;
            grpSelection.IsEnabled  = true;
            btnStatistics.IsEnabled = true;
            btnAddTrans.IsEnabled   = false;

            State.resetLabels();

            btnOk.IsEnabled = true;
            diagram.setCanvas(canvasMain);

            btnAddState.IsChecked = true;
            btnAddState_Click(btnAddState, null); // to set the mode as addState
        }
コード例 #4
0
        public MealyTransitionDiagram convertToMealy()
        {
            if (isError() && transitions.Count == noOfInputs * states.Count)
            {
                MealyTransitionDiagram newDiagram = new MealyTransitionDiagram();
                newDiagram.inputs     = this.inputs;
                newDiagram.outputs    = this.outputs;
                newDiagram.noOfInputs = this.noOfInputs;
                newDiagram.setCanvas(canvasMain);
                List <int> initialStatesIndexes = new List <int>();
                foreach (State s in this.states)
                {
                    if (initialStates.Contains(s))
                    {
                        initialStatesIndexes.Add(s.index);
                    }
                }

                foreach (State s in this.states)
                {
                    foreach (string i in this.inputs)
                    {
                        if (table[s.index, getIndexOfInput(i)] == null)
                        {
                            MessageBox.Show("For each State you must define transitions for all input symbols");
                            return(null);
                        }
                    }
                }

                canvasMain.Children.Clear();

                List <State> newStates = new List <State>();
                for (int i = 0; i < states.Count; i++)
                {
                    State s = newDiagram.addState(State.getNewStatePosition());
                    if (initialStatesIndexes.Contains(i))
                    {
                        newDiagram.initialStates.Add(s);
                        s.addStartingArrow(canvasMain);
                        s.type = stateType.initial;
                    }

                    newStates.Add(s);
                }

                foreach (State s in newStates)
                {
                    State source = s;
                    foreach (string input in newDiagram.inputs)
                    {
                        State dest   = table[s.index, getIndexOfInput(input)];
                        State newDes = dest;
                        foreach (State ss in newStates)
                        {
                            if (ss.index == dest.index)
                            {
                                newDes = ss;
                                break;
                            }
                        }
                        if (newDes != null)
                        {
                            if (source != null && newDes != null)
                            {
                                string output = "";
                                foreach (State r in states)
                                {
                                    if (newDes.index == r.index)
                                    {
                                        output = ((ComboBox)(((StackPanel)(((Label)r.figure.Tag).Content)).Children[1])).SelectedItem.ToString();
                                        break;
                                    }
                                }

                                Transition tr = newDiagram.addTransition(source, newDes);
                                if (tr != null)
                                {
                                    int index1 = ((ComboBox)((StackPanel)tr.figure.Tag).Children[0]).Items.IndexOf(input);
                                    ((ComboBox)((StackPanel)tr.figure.Tag).Children[0]).SelectedIndex = index1;

                                    int index2 = ((ComboBox)((StackPanel)tr.figure.Tag).Children[2]).Items.IndexOf(output);
                                    ((ComboBox)((StackPanel)tr.figure.Tag).Children[2]).SelectedIndex = index2;
                                }
                            }
                        }
                    }
                }

                return(newDiagram);
            }
            else
            {
                if (transitions.Count != noOfInputs * states.Count)
                {
                    MessageBox.Show("Error:There should be transition for each input symbol.");
                }
                else
                {
                    MessageBox.Show("Error:Set input on each transition/Set output on each state");
                }

                return(null);
            }
        }