/* Read content from input file and convert it to automat */
        public List<AutomatonBase> readFile(string fileName)
        {
            try
            {
                System.IO.StreamReader file = new System.IO.StreamReader(fileName); /* Read file */
                int numOfAutomat = Int32.Parse(file.ReadLine().ToString()); /* Number of automata */
                bool isInit = false;
                List<AutomatonBase> models = new List<AutomatonBase>();

                for (int i = 0; i < numOfAutomat; i++)
                {
                    List<StateBase> inputStateList = new List<StateBase>();
                    string initState = file.ReadLine().ToString(); /* Init state */
                    string []listOfState = file.ReadLine().ToString().Split();

                    foreach (string state in listOfState)
                    {
                        if (state.CompareTo(initState) == 0)
                        {
                            isInit = true;
                            inputStateList.Add(new StateBase(state, state, isInit, true));
                            isInit = false;
                        }
                        else
                        {
                            inputStateList.Add(new StateBase(state, state, isInit, true));
                        }
                    }

                    /* Delete white space and ( */
                    string lineOfTrans = RemoveWhitespace(file.ReadLine().ToString());
                    string[] listOfTrans = lineOfTrans.Split(';'); /* Split to each transition */
                    List<StateBase> fromSt = new List<StateBase>();
                    List<StateBase> toSt = new List<StateBase>();

                    foreach (string trans in listOfTrans)
                    {

                        string[] list = trans.Split('.');
                        string from = list[0];
                        string evt = list[1];
                        string to = list[2];
                        StateBase sbFrom = inputStateList[0];
                        StateBase sbTo = inputStateList[0];

                        for (int j = 0; j < inputStateList.Count; j++)
                        {
                            // Add members to FromList state
                            bool flag = false;
                            if (from.CompareTo(inputStateList[j].ToString()) == 0)
                            {
                                sbFrom = inputStateList[j];
                                if(fromSt.Count > 0)
                                {
                                    foreach (StateBase s in fromSt)
                                        if (s.Name.CompareTo(from) == 0)
                                            flag = true;
                                }

                                if(flag == false)
                                    fromSt.Add(sbFrom);
                            }

                            // Add members to ToList state
                            flag = false;
                            if (to.CompareTo(inputStateList[j].ToString()) == 0)
                            {
                                sbTo = inputStateList[j];
                                if(toSt.Count > 0)
                                {
                                    foreach (StateBase s in toSt)
                                        if (s.Name.CompareTo(to) == 0)
                                            flag = true;
                                }

                                if(flag == false)
                                    toSt.Add(sbTo);
                            }
                        }


                        for (int j = 0; j < inputStateList.Count; j++)
                            if (inputStateList[j].ToString().CompareTo(from.ToString()) == 0)
                                inputStateList[j].OutgoingTransitions.Add(new Transition(new Event(evt), sbFrom, sbTo));
                    }
                    AutomatonBase input = new AutomatonBase("Input", null, inputStateList, fromSt, toSt);
                    input.CollectTransitions();
                    input.CollectEvent();
                    models.Add(input);
                }
                return models;
            }
            catch (IOException )
            {
                Console.WriteLine("File not found!");
                return null;
            }
        }
コード例 #2
0
        public void Initialize(AutomatonBase model, bool isProperty)
        {
            int stateNumFrom = model.fromState.Count;
            int stateNumTo = model.toState.Count;
            int eventNum = model.EventList.Count;

            // Count number of bool variables need for encoding FROM state
            if (stateNumFrom == 1)
                FromStateBoolVarNeeded = 1;
            else
                FromStateBoolVarNeeded = (int)Math.Ceiling((Math.Log(stateNumFrom, 2)));

            // Count number of bool variables need for encoding TO state
            if (stateNumTo == 1)
                ToStateBoolVarNeeded = 1;
            else
                ToStateBoolVarNeeded = (int)Math.Ceiling((Math.Log(stateNumTo, 2)));

            // Count number of bool variables need to use for encoding event
            if (eventNum == 1)
                EventBoolVarNeeded = 1;
            else
                EventBoolVarNeeded = (int) Math.Ceiling((Math.Log(eventNum, 2)));

            //encoding From States
            InitialStateEncoding = StateEncoding(FromStateBoolVarNeeded, 0);

            foreach (StateBase state in model.fromState)
            {
                List<bool> t = StateEncoding(FromStateBoolVarNeeded, model.fromState.IndexOf(state));
                FromStateMapping.Add(state.ID, t);
                //  StateRevertMapping.Add(t,state.ID);

                //Note:modified to IsAccepted
                if (isProperty && state.IsAccepted)
                {
                    IsProperty = true;
                    PropertyPosition = 0;
                    PropertyStateEncoding = StateEncoding(FromStateBoolVarNeeded, model.fromState.IndexOf(state));
                }
            }

            //encoding To States
            InitialStateEncoding = StateEncoding(ToStateBoolVarNeeded, 0);

            foreach (StateBase state in model.toState)
            {
                List<bool> t = StateEncoding(ToStateBoolVarNeeded, model.toState.IndexOf(state));
                ToStateMapping.Add(state.ID, t);
                //  StateRevertMapping.Add(t,state.ID);

                //Note:modified to IsAccepted
                if (isProperty && state.IsAccepted)
                {
                    IsProperty = true;
                    PropertyPosition = 0;
                    PropertyStateEncoding = StateEncoding(ToStateBoolVarNeeded, model.toState.IndexOf(state));
                }
            }

            //encoding Events
            foreach (string eventName in model.EventList)
            {
                EventMapping.Add(eventName, EventEncoding(EventBoolVarNeeded, model.EventList.IndexOf(eventName)));
                EventRevertMapping.Add(model.EventList.IndexOf(eventName), eventName);
            }

            foreach (Transition tran in model.Transitions)
            {
                var to = new List<int>();
                foreach (bool b in ToStateMapping[tran.ToState.ID])
                {
                    if (b)
                        to.Add(1);
                    else
                    {
                        to.Add(0);
                    }
                }
                var from = new List<int>();
                foreach (bool b in FromStateMapping[tran.FromState.ID])
                {
                    if (b)
                        from.Add(1);
                    else
                    {
                        from.Add(0);
                    }
                }
                var eTran = new EncodedTranstion(tran.Event.EventName, from, to, tran.FromState, tran.ToState);
                Transtions.Add(eTran);
            }
        }
        public AutomatonBase Clone()
        {
            var stateList = new List<StateBase>();
            var newInitState = new StateBase(InitialState.Name, InitialState.ID, InitialState.IsInitial,
                                             InitialState.IsAccepted
                                             );
            stateList.Add(newInitState);
            var clonedAutomaton = new AutomatonBase(Name + "_d_", null, stateList, new List<StateBase>(), new List<StateBase>());

            var stateDic = new Dictionary<StateBase, StateBase>();
            stateDic.Add(InitialState, newInitState);

            foreach (StateBase oState in States)
            {
                if (oState.IsInitial)
                {
                    continue;
                }

                var nState = new StateBase(oState.Name, oState.Name, oState.IsInitial, oState.IsAccepted);
                clonedAutomaton.States.Add(nState);

                stateDic.Add(oState, nState);
            }

            foreach (StateBase state in States)
            {
                foreach (Transition oldOTran in state.OutgoingTransitions)
                {
                    StateBase nSource = null;
                    StateBase nDestination = null;

                    nSource = stateDic[oldOTran.FromState];
                    nDestination = stateDic[oldOTran.ToState];


                    var newOTran = new Transition(oldOTran.Event, nSource, nDestination);

                    nSource.OutgoingTransitions.Add(newOTran);
                }
            }

            foreach (string evt in EventList)
            {
                clonedAutomaton.EventList.Add(evt);
            }

            return clonedAutomaton;
        }