Example #1
0
        /// <summary>
        /// Creates a new graph representation for the given automata.
        /// </summary>
        /// <param name="automata">The automata instance.</param>
        /// <param name="registerEvents">If true, the automata's events will be registered.</param>
        public AutomataGraph(IAutomata automata, bool registerEvents = false)
        {
            Automata       = automata ?? throw new ArgumentNullException(nameof(automata), "The automata can not be null!");
            RegisterEvents = registerEvents;

            SimulationDrawer = new SimulationDrawer(this);
        }
Example #2
0
        protected void RestartWithNew()
        {
            lock (this)
            {
                Clear();
                rule     = new RandomLangtonRule(numStates, lambda);
                automata = new BasicCellularAutomata(fieldSize, numStates, rule);

                automata.Initialize();
            }
        }
        public NewStateForm(IAutomata automata)
        {
            Automata = automata ?? throw new ArgumentNullException(nameof(automata), "The automata can not be null!");

            InitializeComponent();

            if (Automata.GetStartState() != null)
            {
                StartStateWarningLabel.Visible = true;
            }
        }
Example #4
0
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            visual = new DrawingVisual();

            this.AddVisualChild(visual);

            rule     = new RandomLangtonRule(numStates, lambda);
            automata = new BasicCellularAutomata(fieldSize, numStates, rule);

            automata.Initialize();

            Draw(automata.GetField());

            Task task = new Task(() =>
            {
                while (true)
                {
                    Dispatcher.Invoke(() => {
                        switch (keyPressed)
                        {
                        case Key.Left:
                            RestartOld();
                            break;

                        case Key.Right:
                            RestartWithNew();
                            break;

                        case Key.Space:
                            FastForward();
                            break;

                        default:
                            Draw(automata.GetField());
                            break;
                        }

                        keyPressed = Key.None;
                    });

                    lock (this)
                    {
                        automata.Iterate();
                    }

                    System.Threading.Thread.Sleep(100);
                }
            });

            task.Start();
        }
Example #5
0
        /// <summary>
        /// Creates a new simulation based on the given parameters.
        /// </summary>
        /// <param name="automata">The automata to be simulated.</param>
        /// <param name="input">The input symbols.</param>
        public SimpleSimulation(IAutomata automata, object[] input)
        {
            if (input == null)
            {
                throw new ArgumentNullException(nameof(input), "The input symbols array can not be null!");
            }

            Automata     = automata ?? throw new ArgumentNullException(nameof(automata), "The automata can not be null!");
            CurrentState = Automata.GetStartState() ?? throw new ArgumentException(nameof(automata), "The automata must have a start state!");

            Input = new object[input.Length];

            Array.Copy(input, Input, input.Length);
        }
Example #6
0
        /// <summary>
        /// Creates a new delete state form for the given automata.
        /// </summary>
        /// <param name="automata">The automata.</param>
        public DeleteStateForm(IAutomata automata)
        {
            Automata = automata ?? throw new ArgumentNullException(nameof(automata), "The automata can not be null!");

            InitializeComponent();

            DeleteStateComboBox.Items.Clear();

            foreach (var state in Automata.States)
            {
                DeleteStateComboBox.Items.Add(state.Id);
            }

            if (DeleteStateComboBox.Items.Count > 0)
            {
                DeleteStateComboBox.SelectedIndex = 0;
            }
        }
Example #7
0
        /// <summary>
        /// Creates a new delete transition form for the given automata.
        /// </summary>
        /// <param name="automata"></param>
        public DeleteTransitionForm(IAutomata automata)
        {
            Automata = automata ?? throw new ArgumentNullException(nameof(automata), "The automata can not be null!");

            InitializeComponent();

            foreach (var state in Automata.States)
            {
                SourceStateIdComboBox.Items.Add(state.Id);
            }

            if (SourceStateIdComboBox.Items.Count > 0)
            {
                SourceStateIdComboBox.SelectedIndex = 0;
            }

            SetupTransitionComboBox();
        }
Example #8
0
        public NewTransitionForm(IAutomata automata)
        {
            Automata = automata ?? throw new ArgumentNullException(nameof(automata), "The automata can not be null!");

            InitializeComponent();

            foreach (var state in Automata.States)
            {
                SourceStateIdComboBox.Items.Add(state.Id);
                TargetStateIdComboBox.Items.Add(state.Id);
            }

            if (Automata.States.Count > 0)
            {
                SourceStateIdComboBox.SelectedIndex = 0;
                TargetStateIdComboBox.SelectedIndex = 0;
            }

            AvailableInputSymbolsLabel.Text = Automata.Alphabet.ConstructSymbolText();
        }
Example #9
0
 /// <summary>
 /// Create an automata state wrapper
 /// </summary>
 /// <param name="automata"></param>
 /// <param name="next"></param>
 public AutomataWrapperState(IAutomata <char> automata, int next)
 {
     this.automata = automata;
     this.next     = next;
 }
Example #10
0
        /// <summary>
        /// Loads an automata from the given path.
        /// </summary>
        /// <param name="path">THe file path.</param>
        /// <returns>The new automata's graph representation.</returns>
        public static AutomataGraph Load(string path)
        {
            using (var fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                var settings = new XmlReaderSettings
                {
                    ConformanceLevel = ConformanceLevel.Document
                };

                IAutomata automata = null;

                var stateLookup = new Dictionary <string, IState>();

                using (var reader = XmlReader.Create(fileStream))
                {
                    while (reader.Read())
                    {
                        if (reader.IsStartElement())
                        {
                            switch (reader.Name)
                            {
                            case "Automata":
                                automata = Activator.CreateInstance(Type.GetType(reader.GetAttribute("Type"))) as IAutomata;
                                automata.ReadFromXmlReader(reader);
                                break;

                            case "Alphabet":
                                automata.Alphabet = Activator.CreateInstance(Type.GetType(reader.GetAttribute("Type"))) as IAlphabet;
                                automata.Alphabet.ReadFromXmlReader(reader);
                                break;

                            case "State":
                                var state = Activator.CreateInstance(Type.GetType(reader.GetAttribute("Type")), reader.GetAttribute("Id")) as IState;

                                state.Automata      = automata;
                                state.IsStartState  = reader.GetAttribute("IsStartState") == "True";
                                state.IsAcceptState = reader.GetAttribute("IsAcceptState") == "True";

                                if (stateLookup.ContainsKey(state.Id))
                                {
                                    throw new Exception("Multiple states has the same state id!");
                                }

                                stateLookup[state.Id] = state;

                                state.ReadFromXmlReader(reader);

                                automata.States.Add(state);
                                break;

                            case "Transition":
                                var symbols    = reader.GetAttribute("Symbols").Select(c => c as object).ToArray();
                                var transition = Activator.CreateInstance(Type.GetType(reader.GetAttribute("Type")), stateLookup[reader.GetAttribute("SourceStateId")], stateLookup[reader.GetAttribute("TargetStateId")], symbols) as IStateTransition;

                                transition.Automata = automata;
                                transition.ReadFromXmlReader(reader);

                                automata.Transitions.Add(transition);
                                break;
                            }
                        }
                    }
                }

                return(new AutomataGraph(automata, true));
            }
        }
Example #11
0
        /// <summary>
        /// Saves the automata to the given file.
        /// </summary>
        /// <param name="path">The file path.</param>
        /// <param name="automata">The automata to be saved.</param>
        /// <returns>True, if saving the automata was successful.</returns>
        public static bool Save(string path, IAutomata automata)
        {
            using (var outputFileStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                var settings = new XmlWriterSettings
                {
                    Indent      = true,
                    IndentChars = "    "
                };

                using (var writer = XmlWriter.Create(outputFileStream, settings))
                {
                    writer.WriteStartDocument(true);

                    writer.WriteStartElement("Automata");
                    writer.WriteAttributeString("Type", automata.GetType().AssemblyQualifiedName);

                    automata.WriteToXmlWriter(writer);

                    writer.WriteStartElement("Alphabet");
                    writer.WriteAttributeString("Type", automata.Alphabet.GetType().AssemblyQualifiedName);

                    automata.Alphabet.WriteToXmlWriter(writer);

                    writer.WriteEndElement(); // Alphabet

                    writer.WriteStartElement("States");

                    foreach (var state in automata.States)
                    {
                        writer.WriteStartElement("State");
                        writer.WriteAttributeString("Type", state.GetType().AssemblyQualifiedName);
                        writer.WriteAttributeString("Id", state.Id);
                        writer.WriteAttributeString("IsStartState", state.IsStartState.ToString(CultureInfo.InvariantCulture));
                        writer.WriteAttributeString("IsAcceptState", state.IsAcceptState.ToString(CultureInfo.InvariantCulture));

                        state.WriteToXmlWriter(writer);

                        writer.WriteEndElement();
                    }

                    writer.WriteEndElement(); // States

                    writer.WriteStartElement("Transitions");

                    foreach (var transition in automata.Transitions)
                    {
                        writer.WriteStartElement("Transition");
                        writer.WriteAttributeString("Type", transition.GetType().AssemblyQualifiedName);
                        writer.WriteAttributeString("SourceStateId", transition.SourceState.Id);
                        writer.WriteAttributeString("TargetStateId", transition.TargetState.Id);
                        writer.WriteAttributeString("Symbols", automata.Alphabet.ConstructSymbolText(transition.Symbols, false).Replace(" ", ""));

                        transition.WriteToXmlWriter(writer);

                        writer.WriteEndElement(); // Transition
                    }

                    writer.WriteEndElement(); // Transitions

                    writer.WriteEndElement(); // Automata

                    writer.WriteEndDocument();
                }
            }

            return(true);
        }