Пример #1
0
 public Automaton(Automaton source)
 {
     this.stateList = new List<State>(source.stateList);
     this.startState = source.startState;
     this.mExpression = source.mExpression;
     this.intermediateStates = new List<State>(source.intermediateStates);
     this.finalStates = new List<State>(source.finalStates);
     this.currentState = new State(source.currentState);
 }
Пример #2
0
 public State(State source)
 {
     uniqueID = source.uniqueID;
     title = source.title;
     category = source.category;
     transition = source.transition;
     name = source.name;
     connect = source.connect;
     connectsWith = source.connectsWith;
     alive = source.alive;
 }
Пример #3
0
 //public:
 //FUNCTIONS
 public Automaton()
 {
     //startState = new State();
     currentState = new State();
     //startState = new State();
     currentState = startState;
     /*intermediateStates = new List<State>();
     finalStates = new List<State>();
     mDictionary = new List<string>();
     stateList = new List<State>();
     startState = new State();*/
 }
Пример #4
0
        private void pbxDraw_MouseUp(object sender, MouseEventArgs e)
        {
            drag = -1;
            if (doubleClick)
            {
                tbxName.Enabled = true;
                tbxID.Enabled = true;
                tbxConnectTo.Enabled = true;
                tbxConnectWith.Enabled = true;

                //radiobtnOpen.Enabled = false;
                //radioButton2.Enabled = false;
                //radioButton3.Enabled = false;
                int m = findStatePoint(new Point(e.X, e.Y));
                if (m < myAutomaton.stateList.Count && m > -1)
                {
                    selectedState = myAutomaton.stateList[m];

                }

                showSelectedStateInfo();
                doubleClick = false;
                setComboBox();
                comboBox1.SelectedText = selectedState.name;
                drawDiagram();

            }
            else if (cbxDraw.Checked)
            {
                int n = findStatePoint(new Point(e.X, e.Y));
                if (n >= 0)
                {
                    if (newState[mdState].connect.Length > 0)
                    {
                        newState[mdState].connect += "_";
                    }
                    newState[mdState].connect += newState[n].name;//n.ToString();

                    tbxDraw.Size = new Size(20, 20);
                    tbxDraw.Visible = true;
                    tbxDraw.Focus();
                    Point p = new Point((statePoints[mdState].X+statePoints[n].X)/2 + pbxDraw.Location.X, (statePoints[mdState].Y + statePoints[n].Y)/2+pbxDraw.Location.Y);
                    tbxDraw.Location = p;
                    selectedState = newState[mdState];

                    if (selectedState.category == "Initial")
                    {
                        radioInitial.Checked = true;
                    }
                    if (selectedState.category == "Intermediate")
                    {
                        radioInter.Checked = true;
                    }
                    if (selectedState.category.Contains("Final"))
                    {
                        cbxFinal.Checked = true;
                    }
                    myAutomaton = new Automaton();
                    myAutomaton.build(newState);

                }
                drawDiagram();
                tbxID.Text = selectedState.uniqueID.ToString();
                tbxConnectTo.Text = selectedState.connect.ToString();
                tbxConnectWith.Text = selectedState.connectsWith;

            }
        }
Пример #5
0
        private void pbxDraw_MouseDown(object sender, MouseEventArgs e)
        {
            if (!cbxDraw.Checked)
            {
                drag = findStatePoint(new Point(e.X, e.Y));

            }
            if (!btnAdd.Enabled)
            {
                tbxID.Enabled = true;
                tbxName.Enabled = true;
                tbxConnectTo.Enabled = true;
                tbxConnectWith.Enabled = true;
                tbxDrawName.Clear();
                tbxDrawName.Focus();
                tbxDrawName.Visible = true;
                tbxDrawName.Location = new Point(e.X+pbxDraw.Location.X, e.Y);

                myAutomaton = new Automaton();
                myAutomaton.intermediateStates = new List<State>();
                myAutomaton.finalStates = new List<State>();
                myAutomaton.stateList = new List<State>();
                myAutomaton.startState = new State();
                myAutomaton.currentState = new State();

                State st = new State();
                //if (myAutomaton.currentState == null) { myAutomaton.currentState = myAutomaton.stateList[0]; }
                if (statePoints == null)
                //if(myAutomaton.stateList.Count == 0)
                {
                    //myAutomaton
                    st.category = "Initial";
                    myAutomaton.startState = st;
                    //myAutomaton.currentState = st;
                    statePoints = new List<Point>();
                }
                else
                {
                    st.category = "Intermediate";
                    //myAutomaton.intermediateStates.Add(st);
                }

                btnAdd.Enabled = true;
                //statePoints.Insert(statePoints.Count,
                statePoints.Add(new Point(e.X - wid / 4, e.Y - wid / 4));
                c = statePoints.Count;
                wid = (pbxDraw.Height-170) / (c);

                if (c > 1)
                {
                    st.category = "Intermediate";
                    radioInitial.Enabled = true;
                    radioInter.Enabled = true;

                }
                st.uniqueID = c - 1;
                st.transition = 1;
                //st.connect = textBox4.Text;
                //st.connectsWith = textBox5.Text;

                //myAutomaton.startState = new State();

                newState.Add(st);

                selectedState = st;
                myAutomaton.build(newState);
                //myAutomaton.currentState = newState[0];
                //setStatePoints();
                drawDiagram();
                tbxID.Text = st.uniqueID.ToString();
                //drawStatePoints();
                tbxDrawName.Focus();
            }
            else
            {
                int n = findStatePoint(new Point(e.X, e.Y));
                if (n >= 0)
                {
                    mdState = n;
                    drawDiagram();
                }
            }
        }
Пример #6
0
        private void Form1_Load(object sender, EventArgs e)
        {
            tbxDrawName.Visible = false;
            radioInitial.Checked = true;
            radioInter.Enabled = true;

            //tbxFile.Text = "../../XMLFile5.xml";

            myParser = new Parser();
            myAutomaton = new Automaton();
            newState = new List<State>();
            g = Graphics.FromImage(bmp);//pbxDraw.CreateGraphics();

            statePoints = new List<Point>();
            selectedState = new State();

            c = statePoints.Count+1;
            wid = (pbxDraw.Width-170) / (c);
        }
Пример #7
0
        private void drawDiagram()
        {
            setComboBox();
            if (!selectedState.alive) { selectedState = myAutomaton.stateList[0]; }
            current = myAutomaton.currentState;
            myAutomaton = new Automaton();
            myAutomaton.build(newState);
            myAutomaton.currentState = current;
            wid = Math.Max(wid, 80);
            wid = Math.Min(wid, 200);
            //using (g = Graphics.FromImage(bmp))
            using (g = pbxDraw.CreateGraphics())
            {
                g.Clear(Color.CornflowerBlue);
                drawConnections();

                drawStatePoints();
                drawCurrentState();
                drawStateIDs();
            }
            //pbxDraw.Image = bmp;
            //bmp = new Bitmap(pbxDraw.Width, pbxDraw.Height);
            //pbxDraw.DrawToBitmap(bmp, pbxDraw.ClientRectangle);
            //pbxDraw.BackgroundImage = bmp;
        }
Пример #8
0
 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
 {
     if (comboBox1.SelectedIndex >= 0 && comboBox1.SelectedIndex < newState.Count)
     {
         int n = findIndex(comboBox1.SelectedItem.ToString());
         if (n >= 0)
         {
             selectedState = newState[n];
         }
     }
     showSelectedStateInfo();
     drawDiagram();
 }
Пример #9
0
 private void tbxDraw_KeyDown(object sender, KeyEventArgs e)
 {
     if (e.KeyCode == Keys.Enter)
     {
         if (tbxDraw.Text != "")
         {
             if (newState[mdState].connectsWith.Length > 0)
             {
                 newState[mdState].connectsWith += "_";
             }
             newState[mdState].connectsWith += tbxDraw.Text;
             selectedState = newState[mdState];
             myAutomaton = new Automaton();
             myAutomaton.build(newState);
             tbxDraw.Clear();
             tbxDraw.Visible = false;
             drawDiagram();
             showSelectedStateInfo();
         }
     }
 }
Пример #10
0
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            tbxDraw.Visible = false;
            tbxDrawName.Visible = false;
            drawDiagram();

            if (tbxName.Text != "")
            { selectedState.name = tbxName.Text; }
            if(tbxID.Text != "")
            {selectedState.uniqueID = System.Convert.ToInt32(tbxID.Text);}
            if(tbxConnectTo.Text != "")
            {selectedState.connect = tbxConnectTo.Text;}
            if(tbxConnectWith.Text != "")
            {selectedState.connectsWith = tbxConnectWith.Text;}

            if (radioInter.Checked)
            { selectedState.category = "Intermediate"; }
            if (cbxFinal.Checked)
            { selectedState.category += "_Final"; }
            if(selectedState.uniqueID < newState.Count)
            {newState[selectedState.uniqueID] = selectedState;}

            if (tbxDraw.Text != "")
            {

                if (newState[mdState].connectsWith.Length > 0)
                {
                    newState[mdState].connectsWith += "_";
                }
                newState[mdState].connectsWith += tbxDraw.Text;
                selectedState = newState[mdState];
                myAutomaton = new Automaton();
                myAutomaton.build(newState);
                tbxDraw.Clear();
            }
            //myAutomaton = new Automaton();
            //myAutomaton.build(newState);
            /*if (selectedState.uniqueID == 0)
            {
                myAutomaton.startState = selectedState;
            }
            else
            {
                myAutomaton.intermediateStates[selectedState.uniqueID - 1] = selectedState;
            }*/
            tbxID.Text = selectedState.uniqueID.ToString();
            tbxConnectTo.Text = selectedState.connect.ToString();
            tbxConnectWith.Text = selectedState.connectsWith;

            //myAutomaton.currentState = newState[0];
            drawDiagram();
        }
Пример #11
0
        public bool build(System.Collections.Generic.List<Automaton_GUI.State> input)
        {
            //***************************************************************************************************************
            // Name: public bool build( List<XML>Parser.State> input )
            // Description: This function takes a list of States to build an Automaton. The list of states must
            // have only one starting state, at least one final state, and may have final states. This function will
            // sort the states into the according categories. It will also create the dictionary based on the State's IDs.
            //***************************************************************************************************************
            //if (intermediateStates == null)
            {
                intermediateStates = new List<Automaton_GUI.State>();
            }
            //if(finalStates == null)
            { finalStates = new List<Automaton_GUI.State>(); }
            if (mDictionary == null)
            { mDictionary = new List<string>(); }
            if (stateList == null)
            { stateList = new List<State>(); }

            foreach (State s in input)
            {
                stateList.Add(s);
                if (s.category.Contains("Initial"))
                {
                    if (startState == null)
                    {
                        startState = s;
                        currentState = s;
                        Console.WriteLine("State: " + s.uniqueID + " set as Initial");
                    }

                    else
                    {
                        Console.WriteLine("ERROR: Start State already set!");
                        return false;
                    }
                }//initial state

                else if (s.category.Contains("Intermediate"))
                {
                    intermediateStates.Add(s);
                    Console.WriteLine("State: " + s.uniqueID + " set as Intermediate");
                }//intermediate state

                else if (s.category.Contains("Final"))
                {
                    finalStates.Add(s);
                    Console.WriteLine("State: " + s.uniqueID + " set as Final");
                }//final state

                else
                {
                    Console.WriteLine("ERROR: Your category for State" + s.uniqueID + " is all sorts of messed up");
                    return false;
                }//category error

                //update dictionary
                mDictionary.Add(s.name);
            }
            if (stateList.Count == 0) { stateList.Add(new State()); }
            startState = stateList[0];
            currentState = startState;
            if (finalStates.Count == 0)
            {
                Console.WriteLine("ERROR: No Final States!");
                return false;
            }
            else
                return true;
        }
Пример #12
0
        public bool solve(State current, string mExp)
        {
            Automaton tmp = new Automaton(this);
            tmp.currentState = new State(current);
            tmp.mExpression = mExp;
            int count = 0;
            string acc = "";
            do{
                count++;
                acc = tmp.move(false);
                if (acc == "reject")
                {
                    return false;
                }

            } while ((acc != "accept" && tmp.mExpression.Length > 0)||(tmp.currentState.connectsWith.Contains("&"))) ;
            return acc == "accept";
        }
Пример #13
0
        public bool runInputExpression()
        {
            //mExpression = 1_2_3_4_
            State currentState = startState;
            string nextID;

            mExpression.Remove(0, 2); //delete starting state

            while (mExpression.Length != 0)
            {
                nextID = "";
                while (mExpression[0] != '_')
                    nextID += mExpression[0];

                if (currentState.canMove(nextID))
                    currentState = move(nextID);
            }

            return false;
        }
Пример #14
0
        public string move(bool pass)
        {
            /*
             * This functions takes the user input expression (mExpression) and moves the currentState to a connected state
             * if the initial input string (ex[0]) matches one of the accepted transition strings (cs[i])
             * The initial user input string (ex[0]) is dropped from mExpression at the end
             * currentState will move at most once each function call
             * Returns true if you've reached a final state, false otherwise
             */
            ///ex - user input string as an array ("a_b_c" -> [a, b, c])
            //string[] ex = System.Text.RegularExpressions.Regex.Split(mExpression, "_");
            //cs - list of acceptable strings for corresponding transitions

            string[] cs = System.Text.RegularExpressions.Regex.Split(this.currentState.connectsWith, "_");
            //next - list of connected states
            string[] next = System.Text.RegularExpressions.Regex.Split(this.currentState.connect, "_");

            if (cs.Length == next.Length)
            {
                for (int i = 0; i < cs.Length; i++)
                {
                    if (mExpression.Length > 0)
                    {

                        string[] transitions = System.Text.RegularExpressions.Regex.Split(cs[i], ",");
                        for (int j = 0; j < transitions.Length; j++)
                        {

                            if (this.mExpression[0].ToString() == transitions[j])
                            {
                                int n = findIndex(next[i]);//System.Convert.ToInt32(next[i]);
                                if(n >= 0)
                                {
                                if (stateList[n].alive)
                                {
                                    if (solve(stateList[n], this.mExpression.Substring(1)) || pass)
                                    {
                                        this.currentState = this.stateList[n];
                                        this.mExpression = this.mExpression.Substring(1);
                                        if (this.currentState.category.Contains("Final") && this.mExpression.Length == 0)
                                        {
                                            return "accept";
                                        }
                                        return "false";
                                    }
                                }
                                }
                            }
                        }
                    }
                    if (cs[i] == "&")
                    {
                        int n = findIndex(next[i]);
                        if (n >= 0 && n != this.currentState.uniqueID)
                        {
                            if ((stateList[n].alive && solve(stateList[n], this.mExpression))|| pass)
                            {
                                this.currentState = this.stateList[n];
                                if (this.currentState.category.Contains("Final") && this.mExpression.Length == 0)
                                {
                                    return "accept";
                                }
                                return "false";
                            }
                        }
                    }
                }

            }

            if (this.currentState.category.Contains("Final") && this.mExpression.Length == 0)
            {
                return "accept";
            }
            if (this.mExpression.Length > 0)
            {
                this.mExpression = this.mExpression.Substring(1);
            }
            return "reject";
        }