Exemplo n.º 1
0
        private JudgeTask[] ParseTask(string jsonStr)
        {
            JArray arr = JArray.Parse(jsonStr);

            if (arr.Count == 0)
            {
                return(new JudgeTask[0]);
            }

            JObject obj = arr[0] as JObject;

            if (!CheckTaskJObject(obj))
            {
                return(new JudgeTask[0]);
            }

            SDNUOJJudgeTask t = obj.ToObject <SDNUOJJudgeTask>();

            JudgeTask task = JudgeTaskFactory.Create(
                Int32.Parse(t.SubmitID), Int32.Parse(t.ProblemID), t.DataVersion,
                t.Language.Substring(0, t.Language.Length - 2), t.SourceCode, t.Author,
                Int32.Parse(t.TimeLimit), Int32.Parse(t.MemoryLimit), false);

            return(new JudgeTask[1] {
                task
            });
        }
Exemplo n.º 2
0
        /// <summary>
        /// 评测此JudgeTask
        /// </summary>
        private void Judge(JudgeTask task)
        {
            ITaskSubmitter submitter = FetcherFactory.CreateTaskSubmitter();

            try
            {
                // 检查测试数据是否为最新
                if (!TestDataManager.CheckData(task.ProblemID, task.DataVersion))
                {
                    ITestDataFetcher fetcher = FetcherFactory.CreateTestDataFetcher();
                    TestDataManager.WriteTestData(task.ProblemID, fetcher.Fetch(task.ProblemID));
                }

                JudgeResult result;
                using (MainJudger judger = new MainJudger(task))
                {
                    result = judger.Judge();
                }

                submitter.Submit(result);
            }
            catch (Exception ex)//判题失败
            {
                submitter.Submit(CreateFailedJudgeResult(task, ex.ToString()));
                throw ex;
            }
        }
Exemplo n.º 3
0
        private void Judge(JudgeContext context)
        {
            JudgeTask      task      = context.Task;
            ITaskSubmitter submitter = AdapterFactory.CreateTaskSubmitter();

            try
            {
                UpdateTestData(context);

                using (BaseJudger judger = JudgerFactory.Create(context))
                {
                    LogStartJudgeTask(task.SubmitId);
                    judger.Judge();
                }
            }
            catch (ExpectedJudgeStopException)
            { }
            catch (Exception ex) // 判题失败
            {
                context.Result = CreateFailedJudgeResult(context, ex.ToString());
                throw;
            }
            finally
            {
                LogJudgeResult(context.Result);
                submitter.Submit(context);
            }
        }
Exemplo n.º 4
0
        private void LogAddTask(JudgeContext context)
        {
            JudgeTask task = context.Task;

            LogManager.Info(
                $"New task: SubmitID:{task.SubmitId} Language:{task.Language} " +
                $"CodeLength:{task.SourceCode.Length} ProblemID:{task.ProblemId} Author:{task.Author}");
        }
Exemplo n.º 5
0
        private void LogAddTask(JudgeContext context)
        {
            JudgeTask task = context.Task;

            LogManager.Info(string.Format(
                                "New task: SubmitID:{0} Language:{1} CodeLength:{2} ProblemID:{3} Author:{4}",
                                task.SubmitId, task.Language, task.SourceCode.Length, task.ProblemId, task.Author));
        }
Exemplo n.º 6
0
 public vStatusDetail(JudgeTask judgetask, int index)
 {
     ID          = index;
     MemoryUsage = judgetask.MemoryUsage;
     TimeUsage   = judgetask.TimeUsage;
     Result      = judgetask.ResultAsInt;
     Hint        = Helpers.HtmlFilter.Instance.SanitizeHtml(judgetask.Hint);
 }
Exemplo n.º 7
0
        /// <summary>
        /// 创建Compiler
        /// </summary>
        public static ICompiler Create(JudgeTask task)
        {
            ICompiler compiler = new Compiler(
                task.LangConfig.CompilerPath,
                task.LangConfig.CompilerWorkDirectory);

            compiler.TimeLimit = task.LangConfig.MaxCompileTime;

            return(compiler);
        }
        /// <summary>
        /// 创建JudgeTask实例
        /// </summary>
        /// <param name="submitId">提交ID</param>
        /// <param name="problemId">问题ID</param>
        /// <param name="dataVersion">测试数据版本</param>
        /// <param name="language">语言</param>
        /// <param name="sourceCode">源代码</param>
        /// <param name="author">作者</param>
        /// <param name="timeLimit">时间限制</param>
        /// <param name="memoryLimit">内存限制</param>
        /// <param name="judgeAllCases">是否评测全部样例(即使遇到错误答案)</param>
        /// <param name="judgeType">评测类型</param>
        /// <returns>JudgeTask实例</returns>
        public static JudgeContext Create(int submitId, int problemId, string dataVersion,
                                          string language, string sourceCode, string author = "",
                                          int timeLimit       = 1000, int memoryLimit = 262144, bool judgeAllCases = false,
                                          JudgeType judgeType = JudgeType.ProgramJudge)
        {
            ILangConfig langConfig = ConfigManager.GetLanguageConfig(language);

            if (langConfig == null)
            {
                throw new JudgeException("Unsupported language: " + language);
            }

            // 分配评测临时目录
            string tempDirectory = RandomString.Next(16);

            if (langConfig is ProgramLangConfig)
            {
                ProgramLangConfig langCfg = langConfig as ProgramLangConfig;
                tempDirectory = GetTempDirectory(langCfg.JudgeDirectory);
                UpdatePathInfo(langCfg, tempDirectory);
            }

            double timeCompensation = GetTimeCompensation(langConfig);

            JudgeTask task = new JudgeTask
            {
                SubmitId      = submitId,
                ProblemId     = problemId,
                DataVersion   = dataVersion,
                Language      = language,
                SourceCode    = sourceCode,
                Author        = author,
                TimeLimit     = (int)(timeLimit / timeCompensation),
                MemoryLimit   = memoryLimit,
                JudgeAllCases = judgeAllCases,
                JudgeType     = judgeType
            };

            JudgeResult result = new JudgeResult
            {
                SubmitId    = task.SubmitId,
                ProblemId   = task.ProblemId,
                Author      = task.Author,
                JudgeDetail = "",
                MemoryCost  = Config.MinimumMemoryCost,
                TimeCost    = 0,
                PassRate    = 0,
                ResultCode  = JudgeResultCode.Accepted
            };

            return(new JudgeContext(task, result, tempDirectory, langConfig));
        }
Exemplo n.º 9
0
        /// <summary>
        /// 创建单例Judger
        /// </summary>
        public static ISingleJudger Create(JudgeTask task)
        {
            LanguageConfiguration config = task.LangConfig;

            ISingleJudger judger = new SingleJudger(config.RunnerPath)
            {
                RunnerWorkDirectory = config.RunnerWorkDirectory,
                RunnerArgs          = config.RunnerArgs,
                TimeLimit           = task.TimeLimit,
                MemoryLimit         = task.MemoryLimit,
                OutputLimit         = config.OutputLimit
            };

            return(judger);
        }
Exemplo n.º 10
0
        private void UpdateTestData(JudgeContext context)
        {
            JudgeTask task = context.Task;

            // 检查本地测试数据是否为最新
            if (!TestDataManager.CheckDataVersion(task.ProblemId, task.DataVersion))
            {
                LogInvalidTestData(task.ProblemId);

                ITestDataFetcher fetcher = AdapterFactory.CreateTestDataFetcher();
                TestDataManager.WriteTestData(task.ProblemId, fetcher.Fetch(context));

                LogTestDataFetched(task.ProblemId);
            }
        }
Exemplo n.º 11
0
        /// <summary>
        /// 创建失败的JudgeResult
        /// </summary>
        /// <param name="task">JudgeTask</param>
        /// <param name="message">错误信息</param>
        /// <returns>结果为失败的JudgeResult</returns>
        private JudgeResult CreateFailedJudgeResult(JudgeTask task, string message = "")
        {
            JudgeResult result = new JudgeResult
            {
                Author      = task.Author,
                JudgeDetail = message,
                MemoryCost  = 0,
                TimeCost    = 0,
                PassRate    = 0,
                ProblemID   = task.ProblemID,
                SubmitID    = task.SubmitID,
                ResultCode  = JudgeResultCode.JudgeFailed
            };

            return(result);
        }
Exemplo n.º 12
0
        private JudgeTask GetJudgeTask(int submitID)
        {
            GetSolutionInfo(submitID, out int problemID, out string author, out string lang);
            string sourceCode = GetSolution(submitID);

            GetProblemInfo(problemID, out int timeLimit, out int memoryLimit, out bool spj);

            string dateMD5 = GetTestDataMD5(problemID);

            JudgeTask task = JudgeTaskFactory.Create(
                submitID, problemID, dateMD5, lang, sourceCode,
                author, timeLimit, memoryLimit, spj);

            Authenticator.Singleton.UpdateSolution(submitID, 3, 0, 0, 0.0);

            return(task);
        }
Exemplo n.º 13
0
		public fmTaskJudge(JudgeTask jTask,string userId,string userName)
		{
			//
			// Windows 窗体设计器支持所必需的
			//
			InitializeComponent();
            NowTask=jTask;
			this.UserId=userId;
			this.UserName=userName;
            tbxTaskName.Text = NowTask.TaskName;
            if (NowTask.TaskTypeAndOr == WorkConst.Command_And)
                rbtAnd.Checked=true;
            else
                if (NowTask.TaskTypeAndOr == WorkConst.Command_Or)
                    rbtOr.Checked=true;
			//
			// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
			//
		}
Exemplo n.º 14
0
        /// <summary>
        /// 开始评测JudgeTask
        /// </summary>
        /// <param name="judgeTaskObject">JudgeTask对象</param>
        private void RunJudgeTask(object judgeTaskObject)
        {
            JudgeTask task = judgeTaskObject as JudgeTask;

            try
            {
                Judge(task);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }

            lock (_queueLock)
            {
                RunningCount--;
            }
            CheckTask();//重新检查是否有任务
        }
Exemplo n.º 15
0
        private JudgeTask[] ParseTask(string jsonStr)
        {
            JObject jsonObj = JObject.Parse(jsonStr);

            if (!CheckTaskJObject(jsonObj))
            {
                return(new JudgeTask[0]);
            }

            JudgeTask t = JObject.Parse(jsonStr).ToObject <JudgeTask>();

            JudgeTask task = JudgeTaskFactory.Create(
                t.SubmitID, t.ProblemID, t.DataVersion,
                t.Language, t.SourceCode, t.Author,
                t.TimeLimit, t.MemoryLimit, t.SpecialJudge);

            return(new JudgeTask[1] {
                task
            });
        }
Exemplo n.º 16
0
        /// <summary>
        /// 创建JudgeTask实例
        /// </summary>
        /// <param name="submitID">提交ID</param>
        /// <param name="problemID">问题ID</param>
        /// <param name="dataVersion">测试数据版本</param>
        /// <param name="language">语言</param>
        /// <param name="sourceCode">源代码</param>
        /// <param name="author">作者</param>
        /// <param name="timeLimit">时间限制</param>
        /// <param name="memoryLimit">内存限制</param>
        /// <param name="specialJudge">是否为SpecialJudge</param>
        /// <returns>JudgeTask实例</returns>
        public static JudgeTask Create(int submitID, int problemID, string dataVersion, string language, string sourceCode,
                                       string author = "", int timeLimit = 1000, int memoryLimit = 262144, bool specialJudge = false)
        {
            LanguageConfiguration langConfig = ConfigManager.GetLanguageConfig(language).Clone() as LanguageConfiguration;

            // 分配评测临时目录
            string tempDirectory = GetTempDirectory(langConfig.JudgeDirectory);

            // 替换<tempdir>字段
            langConfig.CompilerPath          = langConfig.CompilerPath.Replace("<tempdir>", tempDirectory);
            langConfig.CompilerWorkDirectory = langConfig.CompilerWorkDirectory.Replace("<tempdir>", tempDirectory);
            langConfig.CompilerArgs          = langConfig.CompilerArgs.Replace("<tempdir>", tempDirectory);
            langConfig.RunnerPath            = langConfig.RunnerPath.Replace("<tempdir>", tempDirectory);
            langConfig.RunnerWorkDirectory   = langConfig.RunnerWorkDirectory.Replace("<tempdir>", tempDirectory);
            langConfig.RunnerArgs            = langConfig.RunnerArgs.Replace("<tempdir>", tempDirectory);

            // 使用绝对路径
            langConfig.CompilerPath          = PathHelper.GetBaseAbsolutePath(langConfig.CompilerPath);
            langConfig.CompilerWorkDirectory = PathHelper.GetBaseAbsolutePath(langConfig.CompilerWorkDirectory);
            langConfig.RunnerWorkDirectory   = PathHelper.GetBaseAbsolutePath(langConfig.RunnerWorkDirectory);
            langConfig.RunnerPath            = PathHelper.GetBaseAbsolutePath(langConfig.RunnerPath);

            JudgeTask task = new JudgeTask
            {
                SubmitID           = submitID,
                ProblemID          = problemID,
                DataVersion        = dataVersion,
                Language           = language,
                SourceCode         = sourceCode,
                Author             = author,
                TimeLimit          = (int)(timeLimit / langConfig.TimeCompensation),
                MemoryLimit        = memoryLimit,
                SpecialJudge       = specialJudge,
                LangConfig         = langConfig,
                TempJudgeDirectory = tempDirectory
            };

            return(task);
        }
Exemplo n.º 17
0
 public void JudgeReady() => JudgeTask?.SetResult("");
Exemplo n.º 18
0
 public byte[] Fetch(JudgeTask task)
 {
     return(Fetch(task.ProblemID.ToString()));
 }
Exemplo n.º 19
0
 public MainJudger(JudgeTask task)
 {
     JudgeTask = task;
     _affinity = ProcessorAffinityManager.GetUseage();
 }
Exemplo n.º 20
0
        public static void Judge(JudgeTask jt)
        {
            while (Program.CurrentThreads >= Program.MaxThreads)
            {
                System.Threading.Thread.Sleep(500);
            }

            Program.CurrentThreads++;

            JudgeFeedback jfb = new JudgeFeedback()
            {
                ID          = jt.ID,
                MemoryUsage = 0,
                TimeUsage   = 0,
                Success     = false
            };

            try
            {
                CheckPath(jt.ID);

                if (!FileExisted(jt.DataID))
                {
                    DownloadFile(jt.DataID);
                }

                //
                MakeCodeFile(jt.ID, jt.Code, (int)jt.CodeLanguage, Mode.Main);

                //编译选手程序
                if (!Compile(jt.ID, (int)jt.CodeLanguage, Mode.Main))
                {
                    return;
                }

                MakeCodeFile(jt.ID, jt.SpecialJudgeCode, (int)jt.SpecialJudgeCodeLanguage, Mode.Spj);

                //编译SPJ
                if (!string.IsNullOrEmpty(jt.SpecialJudgeCode))
                {
                    if (!Compile(jt.ID, (int)jt.SpecialJudgeCodeLanguage, Mode.Spj))
                    {
                        return;
                    }
                }
                else
                {
                    File.Copy(Program.LibPath + @"\CodeComb.Validator.exe", Program.TempPath + @"\" + jt.ID + @"\Spj.exe", true);
                }

                //准备输入数据
                File.Copy(Program.DataPath + @"\" + jt.DataID + @"\input.txt", Program.TempPath + @"\" + jt.ID + @"\input.txt", true);

                long ExitCode;

                //运行选手程序
                if (!Run(jt.ID, (int)jt.CodeLanguage, jt.TimeLimit, jt.MemoryLimit, Mode.Main, out ExitCode, ref jfb))
                {
                    return;
                }

                //准备输出数据
                File.Copy(Program.DataPath + @"\" + jt.DataID + @"\output.txt", Program.TempPath + @"\" + jt.ID + @"\output.txt", true);

                if (!string.IsNullOrEmpty(jt.SpecialJudgeCode))
                {
                    if (!Run(jt.ID, (int)jt.SpecialJudgeCodeLanguage, jt.TimeLimit, jt.MemoryLimit, Mode.Spj, out ExitCode, ref jfb))
                    {
                        return;
                    }
                }
                else
                {
                    if (!Run(jt.ID, (int)Entity.Language.Cxx, jt.TimeLimit, jt.MemoryLimit, Mode.Spj, out ExitCode, ref jfb))
                    {
                        return;
                    }
                }

                //校验结果
                Validate(jt.ID, ExitCode, jfb);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                jfb.Hint   = System.Web.HttpUtility.HtmlEncode(ex.ToString());
                jfb.Result = Entity.JudgeResult.SystemError;
                Program.CurrentThreads--;
                Feedback(jfb);
            }
        }
Exemplo n.º 21
0
        public static void Judge(JudgeTask jt)
        {
            while (Program.CurrentThreads >= Program.MaxThreads)
            {
                System.Threading.Thread.Sleep(500);
            }

            Program.CurrentThreads++;

            JudgeFeedback jfb = new JudgeFeedback()
            {
                ID = jt.ID,
                MemoryUsage = 0,
                TimeUsage = 0,
                Success = false
            };

            try
            {
                CheckPath(jt.ID);

                if (!FileExisted(jt.DataID))
                {
                    DownloadFile(jt.DataID);
                }

                //
                MakeCodeFile(jt.ID, jt.Code, (int)jt.CodeLanguage, Mode.Main);

                //编译选手程序
                if (!Compile(jt.ID, (int)jt.CodeLanguage, Mode.Main))
                    return;

                MakeCodeFile(jt.ID, jt.SpecialJudgeCode, (int)jt.SpecialJudgeCodeLanguage, Mode.Spj);

                //编译SPJ
                if (!string.IsNullOrEmpty(jt.SpecialJudgeCode))
                {
                    if (!Compile(jt.ID, (int)jt.SpecialJudgeCodeLanguage, Mode.Spj))
                    {
                        return;
                    }
                }
                else
                {
                    File.Copy(Program.LibPath + @"\CodeComb.Validator.exe", Program.TempPath + @"\" + jt.ID + @"\Spj.exe", true);
                }

                //准备输入数据
                File.Copy(Program.DataPath + @"\" + jt.DataID + @"\input.txt", Program.TempPath + @"\" + jt.ID + @"\input.txt", true);

                long ExitCode;

                //运行选手程序
                if (!Run(jt.ID, (int)jt.CodeLanguage, jt.TimeLimit, jt.MemoryLimit, Mode.Main, out ExitCode, ref jfb))
                    return;

                //准备输出数据
                File.Copy(Program.DataPath + @"\" + jt.DataID + @"\output.txt", Program.TempPath + @"\" + jt.ID + @"\output.txt", true);

                if (!string.IsNullOrEmpty(jt.SpecialJudgeCode))
                {
                    if (!Run(jt.ID, (int)jt.SpecialJudgeCodeLanguage, jt.TimeLimit, jt.MemoryLimit, Mode.Spj, out ExitCode, ref jfb))
                        return;
                }
                else
                {
                    if (!Run(jt.ID, (int)Entity.Language.Cxx, jt.TimeLimit, jt.MemoryLimit, Mode.Spj, out ExitCode, ref jfb))
                        return;
                }

                //校验结果
                Validate(jt.ID, ExitCode, jfb);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                jfb.Hint = System.Web.HttpUtility.HtmlEncode(ex.ToString());
                jfb.Result = Entity.JudgeResult.SystemError;
                Program.CurrentThreads--;
                Feedback(jfb);
            }
        }
Exemplo n.º 22
0
 public void JudgeFail(string str) => JudgeTask?.SetException(new Exception(str));
Exemplo n.º 23
0
 /// <summary>
 /// 添加JudgeTask
 /// </summary>
 /// <param name="task">JudgeTask</param>
 public void AddTask(JudgeTask task)
 {
     _judgeQueue.Enqueue(task);
     CheckTask();
 }
Exemplo n.º 24
0
 public void JudgeResponse(string str) => JudgeTask?.SetResult(str);
Exemplo n.º 25
0
 public byte[] Fetch(JudgeTask task)
 {
     return(Fetch(task.ProblemID));
 }