Exemple #1
0
        public ActionResult CompareResults(string result)
        {
            ResolveInformation inf = new JavaScriptSerializer().Deserialize <ResolveInformation>(result);

            inf.DllFilePath = Server.MapPath("~/Scripts/TestsFolder/" + inf.DllFilePath);
            ITransform    transform      = ModuleResolver.GetTransformDll(inf.DllFilePath, inf.ResolveDllType);
            string        studentFile    = transform.TransformFileFromClient(result);
            string        studentDirPath = Server.MapPath("~/ProjectsExpressions");
            DirectoryInfo projectExpr    = new DirectoryInfo(studentDirPath);
            DirectoryInfo studentName    = new DirectoryInfo(studentDirPath + "/" + User.Identity.Name);
            DirectoryInfo testDir        = new DirectoryInfo(studentDirPath + "/" + User.Identity.Name + "/" + inf.Description);

            if (!projectExpr.GetDirectories().Contains(studentName))
            {
                studentName.Create();
            }
            if (studentName.GetDirectories().Contains(testDir))
            {
                testDir.Delete();
            }
            testDir.Create();

            FileInfo studentFileInfo = new FileInfo(testDir.FullName + "/" + "Input.rgl");

            using (StreamWriter writer = new StreamWriter(studentFileInfo.Create()))
            {
                writer.Write(studentFile);
            }

            ITestEndpoints testModule  = ModuleResolver.GetAppDll(inf.DllFilePath, inf.ResolveDllType);
            string         gradeResult = testModule.Grade(testDir.FullName);
            TestEntity     test        = testService.GetTestById(inf.Id);

            if (gradeResult == "0")
            {
                string        automataFile   = transform.TransformFileFromClient2(result);
                StringBuilder automataFileSb = new StringBuilder(automataFile);
                automataFileSb.AppendLine("Private Partition");
                automataFileSb.AppendLine();
                FileInfo partitionFile = new FileInfo(testDir + "/" + inf.Description + "/" + inf.Description + "_Partition.txt");
                string   partition     = "";
                using (StreamReader reader = partitionFile.OpenText())
                {
                    partition = reader.ReadToEnd();
                }
                partition = partition.Replace("\n", "");
                string[] lines = partition.Split(Environment.NewLine.ToCharArray()).Skip(3).ToArray();
                int      k     = 0;
                while (k != lines.Length && lines[k] != "*****")
                {
                    automataFileSb.AppendLine(lines[k++]);
                }
                automataFileSb.AppendLine("*****");

                DirectoryInfo compareDir    = testDir.CreateSubdirectory("Automata");
                FileInfo      studentAnswer = new FileInfo(compareDir.FullName + "/student_file.txt");
                using (StreamWriter studentWriter = new StreamWriter(studentAnswer.Create()))
                {
                    studentWriter.Write(automataFileSb.ToString());
                }

                FileInfo   rightAnswer = new FileInfo(compareDir.FullName + "/answer_file.txt");
                FileInfo[] files       = this.GetTestFiles(inf.Id);
                using (FileStream fs = files[inf.TestFileNumber].OpenRead())
                {
                    using (StreamReader sr = new StreamReader(fs))
                    {
                        using (FileStream rightAnswerFs = rightAnswer.Create())
                        {
                            using (StreamWriter answerWriter = new StreamWriter(rightAnswerFs))
                            {
                                answerWriter.Write(sr.ReadToEnd());
                            }
                        }
                    }
                }

                gradeResult = testModule.Grade(studentAnswer.FullName, rightAnswer.FullName);
                if (gradeResult == "True")
                {
                    gradeResult = "0";
                }
            }

            if (gradeResult == "0")
            {
                gradeResult = "10";
            }
            else
            {
                gradeResult = "0";
            }
            AnswerEntity answerEnt = new AnswerEntity()
            {
                Content     = System.Text.Encoding.Default.GetBytes(result),
                Mark        = Double.Parse(gradeResult),
                TestId      = test.Id,
                UserId      = userService.GetUserByEmail(User.Identity.Name).Id,
                TestEndTime = DateTime.Now
            };

            testService.CreateAnswer(answerEnt);

            return(Json(new { Mark = gradeResult }));
        }