private void btnConvertToMoore_Click(object sender, RoutedEventArgs e)
        {
            MooreTransitionDiagram diagTemp = null;

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


            if (diagTemp != null)
            {
                diagram = diagTemp;
                setInterfaceOnMachineLoad();

                this.machine = machines.Moore;

                btnConvertToMealy.Visibility = Visibility.Visible;
                btnConvertToMoore.Visibility = Visibility.Collapsed;
                this.ribbonMain.Title        = "Moore Machine";
                loadedFilePath = "";
            }
        }
        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;
            }
        }
        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
        }
Esempio n. 4
0
        public MooreTransitionDiagram convertToMoore()
        {
            if (IsError() && transitions.Count == noOfInputs * states.Count)
            {
                int flag = 0;

                if (initialStates.Count != 0)
                {
                    MooreTransitionDiagram newDiagram = new MooreTransitionDiagram();
                    newDiagram.inputs     = this.inputs;
                    newDiagram.noOfInputs = this.noOfInputs;
                    newDiagram.outputs    = this.outputs;

                    newDiagram.setCanvas(canvasMain);
                    canvasMain.Children.Clear();


                    List <int> initialStatesIndexes = new List <int>();
                    foreach (State s in this.states)
                    {
                        if (initialStates.Contains(s))
                        {
                            initialStatesIndexes.Add(s.index);
                        }
                    }


                    int newStatesCount = 0;
                    for (int i = 0; i < states.Count; i++)
                    {
                        if (table2[i, 0] == table2[i, 1])
                        {
                            State s = newDiagram.addState(State.getNewStatePosition());

                            ((Label)(((StackPanel)(((Label)s.figure.Tag).Content)).Children[0])).Content = getNameOfState(i) + outputs[0];
                            ((Label)(((StackPanel)(((Label)s.figure.Tag).Content)).Children[0])).ToolTip = getNameOfState(i) + outputs[0] + "," + getNameOfState(i) + outputs[1];
                            s.figure.ToolTip = getNameOfState(i) + outputs[0] + "," + getNameOfState(i) + outputs[1];


                            if (flag == 0)
                            {
                                ((ComboBox)(((StackPanel)(((Label)((Ellipse)s.figure).Tag).Content)).Children[1])).Items.Add("^");
                                int idx = ((ComboBox)(((StackPanel)(((Label)((Ellipse)s.figure).Tag).Content)).Children[1])).Items.IndexOf("^");
                                if (idx >= 0)
                                {
                                    ((ComboBox)(((StackPanel)(((Label)((Ellipse)s.figure).Tag).Content)).Children[1])).SelectedIndex = idx;
                                    flag++;
                                }
                            }
                            else
                            {
                                int index = ((ComboBox)(((StackPanel)(((Label)((Ellipse)s.figure).Tag).Content)).Children[1])).Items.IndexOf(table2[i, 0]);
                                if (index >= 0)
                                {
                                    ((ComboBox)(((StackPanel)(((Label)((Ellipse)s.figure).Tag).Content)).Children[1])).SelectedIndex = index;
                                }
                            }

                            if (initialStatesIndexes.Contains(i))
                            {
                                newDiagram.initialStates.Add(s);
                                s.addStartingArrow(canvasMain);
                                s.type = stateType.initial;
                            }

                            table3[ptr, 0] = s;
                            ptr++;
                            newStates.Add(s);
                            newStatesCount++;
                        }
                        else
                        {
                            State s1 = newDiagram.addState(State.getNewStatePosition());
                            ((Label)(((StackPanel)(((Label)s1.figure.Tag).Content)).Children[0])).Content = getNameOfState(i) + table2[i, 0];
                            ((Label)(((StackPanel)(((Label)s1.figure.Tag).Content)).Children[0])).ToolTip = getNameOfState(i) + table2[i, 0];
                            s1.figure.ToolTip = getNameOfState(i) + table2[i, 0];


                            if (flag == 0)
                            {
                                ((ComboBox)(((StackPanel)(((Label)((Ellipse)s1.figure).Tag).Content)).Children[1])).Items.Add("^");
                                int idx = ((ComboBox)(((StackPanel)(((Label)((Ellipse)s1.figure).Tag).Content)).Children[1])).Items.IndexOf("^");
                                if (idx >= 0)
                                {
                                    ((ComboBox)(((StackPanel)(((Label)((Ellipse)s1.figure).Tag).Content)).Children[1])).SelectedIndex = idx;
                                }
                            }
                            else
                            {
                                int index1 = ((ComboBox)(((StackPanel)(((Label)((Ellipse)s1.figure).Tag).Content)).Children[1])).Items.IndexOf(table2[i, 0]);
                                if (index1 >= 0)
                                {
                                    ((ComboBox)(((StackPanel)(((Label)((Ellipse)s1.figure).Tag).Content)).Children[1])).SelectedIndex = index1;
                                }
                            }

                            if (initialStatesIndexes.Contains(i))
                            {
                                newDiagram.initialStates.Add(s1);
                                s1.addStartingArrow(canvasMain);
                                s1.type = stateType.initial;
                            }

                            table3[ptr, 0] = s1;
                            newStates.Add(s1);
                            ptr++;
                            State s2 = newDiagram.addState(State.getNewStatePosition());
                            ((Label)(((StackPanel)(((Label)s2.figure.Tag).Content)).Children[0])).Content = getNameOfState(i) + table2[i, 1];
                            ((Label)(((StackPanel)(((Label)s2.figure.Tag).Content)).Children[0])).ToolTip = getNameOfState(i) + table2[i, 1];
                            s2.figure.ToolTip = getNameOfState(i) + table2[i, 1];

                            if (flag == 0)
                            {
                                ((ComboBox)(((StackPanel)(((Label)((Ellipse)s2.figure).Tag).Content)).Children[1])).Items.Add("^");
                                int idx = ((ComboBox)(((StackPanel)(((Label)((Ellipse)s1.figure).Tag).Content)).Children[1])).Items.IndexOf("^");
                                if (idx >= 0)
                                {
                                    ((ComboBox)(((StackPanel)(((Label)((Ellipse)s2.figure).Tag).Content)).Children[1])).SelectedIndex = idx;
                                    flag++;
                                }
                            }
                            else
                            {
                                int index2 = ((ComboBox)(((StackPanel)(((Label)((Ellipse)s2.figure).Tag).Content)).Children[1])).Items.IndexOf(table2[i, 1]);
                                if (index2 >= 0)
                                {
                                    ((ComboBox)(((StackPanel)(((Label)((Ellipse)s2.figure).Tag).Content)).Children[1])).SelectedIndex = index2;
                                }
                            }

                            if (initialStatesIndexes.Contains(i))
                            {
                                newDiagram.initialStates.Add(s2);
                                s2.addStartingArrow(canvasMain);
                                s2.type = stateType.initial;
                            }

                            newStates.Add(s2);
                            table3[ptr, 0] = s2;
                            ptr++;
                            newStatesCount += 2;
                        }
                    }

                    ptr = 0;
                    for (int i = 0; i < states.Count; i++)
                    {
                        if (table2[i, 0] == table2[i, 1])
                        {
                            State  s    = table[i, 0];
                            string name = getNameOfState(s.index) + table2[i, 0];
                            State  ss   = getStateByName(name);

                            table3[ptr, 1] = ss;

                            State  s1    = table[i, 1];
                            string name1 = getNameOfState(s1.index) + table2[i, 1];
                            State  ss1   = getStateByName(name1);


                            table3[ptr, 2] = ss1;
                            ptr++;
                        }
                        else
                        {
                            State  s1    = table[i, 0];
                            string name1 = getNameOfState(s1.index) + table2[i, 0];
                            State  ss1   = getStateByName(name1);


                            table3[ptr, 1]     = ss1;
                            table3[ptr + 1, 1] = ss1;

                            State  s2    = table[i, 1];
                            string name2 = getNameOfState(s2.index) + table2[i, 1];
                            State  ss2   = getStateByName(name2);

                            table3[ptr, 2]     = ss2;
                            table3[ptr + 1, 2] = ss2;
                            ptr += 2;
                        }
                    }


                    for (int i = 0; i < newStates.Count; i++)
                    {
                        for (int j = 1; j <= 2; j++)
                        {
                            Transition tr = newDiagram.addTransition(table3[i, 0], table3[i, j]);
                            if (tr != null)
                            {
                                int index2 = ((ComboBox)((StackPanel)tr.figure.Tag).Children[0]).Items.IndexOf(inputs[j - 1]);
                                if (index2 >= 0)
                                {
                                    ((ComboBox)((StackPanel)tr.figure.Tag).Children[0]).SelectedIndex = index2;
                                }
                            }
                        }
                    }
                    return(newDiagram);
                }
                else
                {
                    MessageBox.Show("set staring state");
                    return(null);
                }
            }
            else
            {
                if (transitions.Count == noOfInputs * states.Count)
                {
                    MessageBox.Show("Error:There should be transition for each input symbol.");
                }
                else
                {
                    MessageBox.Show("Error:Set inputs/Outputs on all transitions");
                }

                return(null);
            }
        }