예제 #1
0
        private async Task RestartAsync(IDialogContext context, IAwaitable <bool> result)
        {
            if (await result)
            {
                digraph        = StudentGraphGenerator.Sample();
                currentStudent = null;
                newStudent     = null;
                newNeighbors   = null;
                newStudentStep = -1;
                student        = "";
                await context.PostAsync($"The system has been restarted.");
            }
            else
            {
                await context.PostAsync($"Restart was cancelled.");
            }

            context.Wait(MessageReceived);
        }
예제 #2
0
        static void Main(string[] args)
        {
            // BUG: 42 is missing :-(

            Console.Title = "Studiegruppefinderen v. 1.0";
            Console.WriteLine("******************************************************************************");
            Console.WriteLine("*                  S T U D I E G R U P P E F I N D E R E N                   *");
            Console.WriteLine("*                                  v. 2.1                                    *");
            Console.WriteLine("******************************************************************************");

            // Generate student graph
            var digraph = StudentGraphGenerator.Generate(20, 5);

            //var digraph = StudentGraphGenerator.Sample();

            Console.WriteLine("\n=========================== Registrerede studerende ===========================\n");
            Console.WriteLine("(Navn,  studie,  søger studiegruppe?,  [egenskaber],  {studierelevante egenskaber})");
            foreach (Node <Student> node in digraph.Nodes.Values)
            {
                Console.Write(node.Data + ",  " + node.Data.Study + ",  " + (node.Data.SeeksGroup ? "Ja" : "Nej") + ",  ");
                Console.Write("[" + node.Data.Attributes.ToSeparatedString(", ") + "],  ");
                Console.WriteLine("{" + node.Data.StudyAttributes.ToSeparatedString(", ") + "}");
            }

            // TODO: Input validation
            // Strengene må ikke være tomme.
            string         studentName = "";
            List <string>  neighbors   = new List <string>();
            Node <Student> newNode     = new Node <Student>();
            bool           approved    = false;

            while (!approved)
            {
                Console.WriteLine("\n=============================== Ny studerende ================================\n");

                studentName = "";

                while (studentName == "")
                {
                    Console.Write("Indtast navn: ");
                    studentName = Console.ReadLine();
                }

                studentName = studentName.Substring(0, 1).ToUpper() + studentName.Substring(1).ToLower();

                if (digraph.Contains(studentName))
                {
                    studentName = StudentGraphGenerator.GenerateUniqueName(digraph, studentName);
                    Console.WriteLine("Da systemet allerede indeholdt en studerende med dit navn, er dit brugernavn ændret til " + studentName);
                }

                string study = "";

                while (study == "")
                {
                    Console.Write("Indtast studie: ");
                    study = Console.ReadLine();
                }

                study = study.Substring(0, 1).ToUpper() + study.Substring(1).ToLower();
                string seeksGroup = "";

                while (!new string[] { "J", "N" }.Contains(seeksGroup.ToUpper()))
                {
                    Console.Write("Ønsker du at deltage i en studiegruppe? (J/N) ");
                    seeksGroup = Console.ReadLine();
                }

                Console.Write("Indtast dine personlige egenskaber adskilt af mellemrum: ");
                HashSet <string> attributes = new HashSet <string>(Console.ReadLine().ToUpper().Split(' '));
                Console.Write("Indtast dine studierelevante egenskaber adskilt af mellemrum: ");
                HashSet <string> studyAttributes = new HashSet <string>(Console.ReadLine().ToUpper().Split(' '));

                bool validNeighbors = true;

                do
                {
                    Console.Write("Indtast navnene på de studerende, der bor tæt på dig, adskilt af mellemrum: ");
                    string neighborsString = Console.ReadLine();
                    neighbors      = neighborsString == "" ? new List <string>() : neighborsString.Split(' ').ToList();
                    validNeighbors = true;

                    foreach (string neighbor in neighbors)
                    {
                        if (!digraph.Nodes.ContainsKey(neighbor))
                        {
                            validNeighbors = false;
                            Console.WriteLine(neighbor + " blev ikke fundet i systemet. Prøv venligst igen. ");
                            break;
                        }
                    }
                }while (!validNeighbors);

                Student newStudent = new Student(studentName, study);
                newStudent.Attributes      = attributes;
                newStudent.StudyAttributes = studyAttributes;

                if (seeksGroup.ToUpper() == "J")
                {
                    newStudent.SeeksGroup = true;
                }
                else
                {
                    newStudent.SeeksGroup = false;
                }

                newNode = new Node <Student>(newStudent);

                Console.WriteLine("\n============================== Dine oplysninger ==============================\n");
                Console.WriteLine(newStudent + ", " + newStudent.Study + ", " + (newStudent.SeeksGroup ? "søger studiegruppe" : "søger ikke studiegruppe"));
                Console.WriteLine("Personlige egenskaber: " + newStudent.Attributes.ToSeparatedString(", "));
                Console.WriteLine("Studierelevante egenskaber: " + newStudent.StudyAttributes.ToSeparatedString(", "));
                Console.WriteLine("Dine naboer: " + neighbors.ToSeparatedString(", "));
                Console.WriteLine();

                string appr = "";
                while (!new string[] { "J", "N" }.Contains(appr))
                {
                    Console.Write("Kan oplysningerne godkendes? (J/N) ");
                    appr = Console.ReadLine().ToUpper();
                }

                approved = appr == "J";
            }

            neighbors.ForEach(n => newNode.AddNeighbor(digraph[n]));
            digraph.AddNode(newNode);

            Console.WriteLine("\n================================== RESULTAT ==================================\n");

            Console.WriteLine("Nærmeste med samme f*g:");
            Console.WriteLine(digraph.FindPath(newNode, n => newNode.Data.Study == n.Study));

            if (newNode.Data.SeeksGroup)
            {
                Console.WriteLine("\nNærmeste med samme f*g, som søger studiegruppe:");
                Console.WriteLine(digraph.FindPath(newNode, n => n.SeeksGroup && newNode.Data.Study == n.Study));

                Console.WriteLine("\nNærmeste med samme f*g, som søger studiegruppe + har mindst én studierelevant egenskab tilfælles:");
                Console.WriteLine(digraph.FindPath(newNode, n =>
                                                   n.SeeksGroup &&
                                                   newNode.Data.Study == n.Study &&
                                                   (newNode.Data.StudyAttributes.Intersect(n.StudyAttributes)).Count() > 0));
            }

            Console.WriteLine("\nNærmeste med mindst én egenskab tilfælles:");
            Console.WriteLine(digraph.FindPath(newNode, n => (newNode.Data.Attributes.Intersect(n.Attributes)).Count() > 0));

            Console.WriteLine("\nNærmeste med mindst tre egenskaber tilfælles:");
            Console.WriteLine(digraph.FindPath(newNode, n => (newNode.Data.Attributes.Intersect(n.Attributes)).Count() > 2));

            Console.WriteLine("\nNærmeste med egenskaben 'A':");
            Console.WriteLine(digraph.FindPath(newNode, n => n.Attributes.Contains("A")));

            Console.WriteLine("\nKorteste rute til hver af de andre studerende (BFS):");
            foreach (KeyValuePair <string, string> dest in digraph.FindPathsToAll(studentName))
            {
                Console.WriteLine($"{studentName} til {dest.Key}:  " + dest.Value);;
            }

            digraph.PathFinder = new DFS <Student>();
            Console.WriteLine("\nVilkårlig rute til hver af de andre studerende (DFS):");

            // FindPathsToAll iterator saved in variable for inspection. Set breakpoint below to inspect.
            var allPaths = digraph.FindPathsToAll(studentName);
            var nextPath = allPaths.FirstOrDefault();

            foreach (KeyValuePair <string, string> dest in allPaths)
            {
                Console.WriteLine($"{studentName} til {dest.Key}:  " + dest.Value);;
            }

            Console.WriteLine("\nOversigt over naboskaber:");
            Console.WriteLine(digraph);

            Console.WriteLine("\nTryk på en vilkårlig tast for at afslutte ...");
            Console.ReadKey();
        }
 static StudentGraph()
 {
     Students = StudentGraphGenerator.Generate(100, 5);
 }
예제 #4
0
        private async Task AddStudentAsync(IDialogContext context, IAwaitable <object> result)
        {
            var activity = await result as Activity;

            if (newStudentStep == 0)
            {
                // Name
                string newName = activity.Text.Substring(0, 1).ToUpper() + activity.Text.Substring(1).ToLower();

                if (digraph.Contains(newName))
                {
                    newName = StudentGraphGenerator.GenerateUniqueName(digraph, newName);
                    await context.PostAsync($"Because the system already contains a student with the given name, it has been changed to { newName }.");
                }

                newStudent     = new Student(newName);
                newStudentStep = 1;
                await context.PostAsync($"What subject does { newStudent } study?");

                context.Wait(AddStudentAsync);
            }
            else if (newStudentStep == 1)
            {
                // Study
                newStudent.Study = activity.Text.Substring(0, 1).ToUpper() + activity.Text.Substring(1);
                newStudentStep   = 2;
                PromptDialog.Confirm(context, StudyGroupAsync, $"Does { newStudent } seek a study group?");
            }
            else if (newStudentStep == 2)
            {
                // SeeksGroup (has already been handled in separate method - this code should never be reached)
                newStudentStep = 3;
                context.Wait(AddStudentAsync);
            }
            else if (newStudentStep == 3)
            {
                // Attributes
                if (activity.Text == "-")
                {
                    activity.Text = "";
                }
                newStudent.Attributes = new HashSet <string>(activity.Text.ToUpper().Split(' '));
                newStudentStep        = 4;
                await context.PostAsync($"Enter { newStudent }'s study attributes separated by spaces. Type '-' if there are none.");

                context.Wait(AddStudentAsync);
            }
            else if (newStudentStep == 4)
            {
                // StudyAttributes
                if (activity.Text == "-")
                {
                    activity.Text = "";
                }
                newStudent.StudyAttributes = new HashSet <string>(activity.Text.ToUpper().Split(' '));
                newStudentStep             = 5;
                await context.PostAsync($"Enter { newStudent }'s neighbors separated by spaces. Type '-' if there are none.");

                context.Wait(AddStudentAsync);
            }
            else if (newStudentStep == 5)
            {
                // Neighbors
                string[] rawNeighbors;

                if (activity.Text == "-")
                {
                    rawNeighbors = new string[] { };
                }
                else
                {
                    rawNeighbors = activity.Text.Split(' ');
                }

                newNeighbors = new string[rawNeighbors.Length];

                for (int i = 0; i < rawNeighbors.Length; i++)
                {
                    newNeighbors[i] = rawNeighbors[i].Substring(0, 1).ToUpper() + rawNeighbors[i].Substring(1).ToLower();
                }

                newStudentStep = 0;
                await context.PostAsync($"The new student's details are: { newStudent.GetDetails() }, Neighbors: [{ newNeighbors.ToSeparatedString() }]");

                PromptDialog.Confirm(context, NewStudentAsync, $"Can you confirm the details?");
            }
            else
            {
                // Error
                await context.PostAsync($"I am a bit confused, sorry. Please try again.");

                context.Wait(MessageReceived);
            }
        }