Search in sources :

Example 36 with Problem

use of cn.edu.zju.acm.onlinejudge.bean.Problem in project zoj by licheng.

the class ProblemPersistenceImpl method populateProblem.

/**
     * Populates a Problem with given ResultSet.
     * 
     * @param rs
     * @return an Problem instance
     * @throws SQLException
     */
private Problem populateProblem(ResultSet rs) throws SQLException {
    Problem problem = new Problem();
    problem.setId(rs.getLong(DatabaseConstants.PROBLEM_PROBLEM_ID));
    problem.setTitle(rs.getString(DatabaseConstants.PROBLEM_TITLE));
    problem.setContestId(rs.getLong(DatabaseConstants.PROBLEM_CONTEST_ID));
    problem.setCode(rs.getString(DatabaseConstants.PROBLEM_CODE));
    problem.setAuthor(rs.getString(DatabaseConstants.PROBLEM_AUTHOR));
    problem.setSource(rs.getString(DatabaseConstants.PROBLEM_SOURCE));
    problem.setContest(rs.getString(DatabaseConstants.PROBLEM_CONTEST));
    problem.setChecker(rs.getBoolean(DatabaseConstants.PROBLEM_CHECKER));
    problem.setRevision(rs.getInt(DatabaseConstants.PROBLEM_REVISION));
    problem.setColor(rs.getString(DatabaseConstants.PROBLEM_COLOR));
    problem.setScore(rs.getInt(DatabaseConstants.PROBLEM_SCORE));
    return problem;
}
Also used : Problem(cn.edu.zju.acm.onlinejudge.bean.Problem)

Example 37 with Problem

use of cn.edu.zju.acm.onlinejudge.bean.Problem in project zoj by licheng.

the class JudgeClientJudgeThread method judge.

private void judge(Submission submission) throws JudgeServerErrorException, IOException, PersistenceException, JudgeClientErrorException, ProblemDataErrorException {
    Problem problem = this.problemDAO.getProblem(submission.getProblemId());
    int reply = this.sendJudgeCommand(problem.getId(), problem.getRevision(), submission.getId());
    if (reply == JudgeReply.NO_SUCH_PROBLEM.getId()) {
        reply = this.sendDataCommand(problem);
    }
    if (reply == JudgeReply.COMPILATION_ERROR.getId()) {
        int length = this.in.readInt();
        byte[] bytes = new byte[length];
        this.in.read(bytes);
        throw new ProblemDataErrorException("Special judge compilation failure for problem " + problem.getId() + ": " + new String(bytes));
    }
    if (reply != JudgeReply.READY.getId()) {
        throw new JudgeClientErrorException();
    }
    String content = submission.getContent();
    if (content == null) {
        content = this.submissionDAO.getSubmissionSource(submission.getId());
    }
    reply = this.sendCompileCommand(submission.getId(), submission.getLanguage(), content);
    if (reply != JudgeReply.COMPILING.getId()) {
        throw new JudgeClientErrorException();
    }
    submission.setJudgeReply(JudgeReply.COMPILING);
    reply = this.readJudgeReply();
    if (reply == JudgeReply.COMPILATION_ERROR.getId()) {
        submission.setJudgeReply(JudgeReply.COMPILATION_ERROR);
        int length = this.in.readInt();
        byte[] bytes = new byte[length];
        this.in.read(bytes);
        submission.setJudgeComment(new String(bytes));
        return;
    } else if (reply != JudgeReply.READY.getId()) {
        throw new JudgeClientErrorException();
    }
    Limit limit = problem.getLimit();
    reply = this.sendTestcaseCommand(1, limit.getTimeLimit(), limit.getMemoryLimit(), limit.getOutputLimit());
    submission.setJudgeReply(JudgeReply.RUNNING);
    submission.setTimeConsumption(0);
    submission.setMemoryConsumption(0);
    while (reply == JudgeReply.RUNNING.getId()) {
        int timeConsumption = this.in.readInt();
        int memoryConsumption = this.in.readInt();
        submission.setTimeConsumption(timeConsumption);
        submission.setMemoryConsumption(memoryConsumption);
        this.logger.info("Running " + timeConsumption + " " + memoryConsumption);
        reply = this.readJudgeReply();
    }
    while (reply == JudgeReply.JUDGING.getId()) {
        submission.setJudgeReply(JudgeReply.JUDGING);
        reply = this.readJudgeReply();
    }
    if (reply == JudgeReply.JUDGE_INTERNAL_ERROR.getId()) {
        throw new JudgeClientErrorException();
    }
    submission.setJudgeReply(JudgeReply.findById(reply));
    if (submission.getJudgeReply() == null || submission.getJudgeReply() != JudgeReply.TIME_LIMIT_EXCEEDED && submission.getJudgeReply() != JudgeReply.MEMORY_LIMIT_EXCEEDED && submission.getJudgeReply() != JudgeReply.OUTPUT_LIMIT_EXCEEDED && submission.getJudgeReply() != JudgeReply.FLOATING_POINT_ERROR && submission.getJudgeReply() != JudgeReply.SEGMENTATION_FAULT && submission.getJudgeReply() != JudgeReply.RUNTIME_ERROR && submission.getJudgeReply() != JudgeReply.NON_ZERO_EXIT_CODE && submission.getJudgeReply() != JudgeReply.ACCEPTED && submission.getJudgeReply() != JudgeReply.WRONG_ANSWER && submission.getJudgeReply() != JudgeReply.PRESENTATION_ERROR) {
        throw new JudgeClientErrorException();
    }
}
Also used : Problem(cn.edu.zju.acm.onlinejudge.bean.Problem) Limit(cn.edu.zju.acm.onlinejudge.bean.Limit)

Example 38 with Problem

use of cn.edu.zju.acm.onlinejudge.bean.Problem in project zoj by licheng.

the class ProblemForm method toProblem.

public Problem toProblem() throws ParseException, NumberFormatException, PersistenceException {
    Problem problem = new Problem();
    if (this.problemId != null) {
        problem.setId(Long.parseLong(this.problemId));
    }
    if (this.contestId != null) {
        problem.setContestId(Long.parseLong(this.contestId));
    }
    problem.setTitle(this.name);
    problem.setCode(this.code);
    problem.setAuthor(this.author);
    problem.setSource(this.source);
    problem.setContest(this.contest);
    Limit limit = new Limit();
    if (!this.useContestDefault) {
        limit.setTimeLimit(Integer.parseInt(this.timeLimit));
        limit.setMemoryLimit(Integer.parseInt(this.memoryLimit));
        limit.setSubmissionLimit(Integer.parseInt(this.submissionLimit));
        limit.setOutputLimit(Integer.parseInt(this.outputLimit));
    }
    problem.setLimit(limit);
    problem.setChecker(this.specialJudge);
    problem.setColor(this.color);
    problem.setScore(this.score);
    return problem;
}
Also used : Problem(cn.edu.zju.acm.onlinejudge.bean.Problem) Limit(cn.edu.zju.acm.onlinejudge.bean.Limit)

Example 39 with Problem

use of cn.edu.zju.acm.onlinejudge.bean.Problem in project zoj by licheng.

the class ProblemManager method parse.

private static ProblemPackage parse(Map<String, byte[]> files, ActionMessages messages) {
    Map<String, ProblemEntry> entryMap = new TreeMap<String, ProblemEntry>();
    byte[] csv = files.get(ProblemManager.PROBLEM_CSV_FILE);
    BufferedReader reader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(csv)));
    int index = 0;
    try {
        for (; ; ) {
            index++;
            String messageKey = "Line " + index;
            String line = reader.readLine();
            if (line == null) {
                break;
            }
            if (line.trim().length() == 0) {
                continue;
            }
            // CSV format code,title,checker,tl,ml,ol,sl,author,source,contest
            String[] values = ProblemManager.split(line);
            Problem problem = new Problem();
            if (values.length < 2) {
                messages.add(messageKey, new ActionMessage("onlinejudge.importProblem.invalidline"));
                continue;
            }
            if (values[0].length() > 8 || values[0].length() == 0) {
                messages.add(messageKey, new ActionMessage("onlinejudge.importProblem.invalidcode"));
            }
            problem.setCode(values[0]);
            if (values[1].length() > 128 || values[1].length() == 0) {
                messages.add(messageKey, new ActionMessage("onlinejudge.importProblem.invalidtitle"));
            }
            problem.setTitle(values[1]);
            if (values.length > 2 && Boolean.valueOf(values[2]).booleanValue()) {
                problem.setChecker(true);
            }
            Limit limit = new Limit();
            Integer tl = ProblemManager.retrieveInt(values, 3);
            if (tl != null) {
                if (tl.intValue() < 0) {
                    messages.add(messageKey, new ActionMessage("onlinejudge.importProblem.invalidtl"));
                } else {
                    limit.setTimeLimit(tl.intValue());
                }
            }
            Integer ml = ProblemManager.retrieveInt(values, 4);
            if (ml != null) {
                if (ml.intValue() < 0) {
                    messages.add(messageKey, new ActionMessage("onlinejudge.importProblem.invalidml"));
                } else {
                    limit.setMemoryLimit(ml.intValue());
                }
            }
            Integer ol = ProblemManager.retrieveInt(values, 5);
            if (ol != null) {
                if (ol.intValue() < 0) {
                    messages.add(messageKey, new ActionMessage("onlinejudge.importProblem.invalidol"));
                } else {
                    limit.setOutputLimit(ol.intValue());
                }
            }
            Integer sl = ProblemManager.retrieveInt(values, 6);
            if (sl != null) {
                if (sl.intValue() < 0) {
                    messages.add(messageKey, new ActionMessage("onlinejudge.importProblem.invalidsl"));
                } else {
                    limit.setSubmissionLimit(sl.intValue());
                }
            }
            if (tl != null || ml != null || ol != null || sl != null) {
                if (tl != null && ml != null && ol != null && sl != null) {
                    problem.setLimit(limit);
                } else {
                    messages.add(messageKey, new ActionMessage("onlinejudge.importProblem.missinglimit"));
                }
            }
            if (values.length > 7 && values[7].length() > 0) {
                if (values[7].length() > 32) {
                    messages.add(messageKey, new ActionMessage("onlinejudge.importProblem.invalidauthor"));
                } else {
                    problem.setAuthor(values[7]);
                }
            }
            if (values.length > 8 && values[8].length() > 0) {
                if (values[8].length() > 128) {
                    messages.add(messageKey, new ActionMessage("onlinejudge.importProblem.invalidsource"));
                } else {
                    problem.setSource(values[8]);
                }
            }
            if (values.length > 9 && values[9].length() > 0) {
                if (values[9].length() > 128) {
                    messages.add(messageKey, new ActionMessage("onlinejudge.importProblem.invalidcontest"));
                } else {
                    problem.setContest(values[9]);
                }
            }
            ProblemEntry entry = new ProblemEntry();
            entry.setProblem(problem);
            if (entryMap.containsKey(problem.getCode())) {
                messages.add(messageKey, new ActionMessage("onlinejudge.importProblem.reduplicatecode"));
            }
            entryMap.put(problem.getCode(), entry);
        }
    } catch (IOException e) {
        messages.add("error", new ActionMessage("onlinejudge.importProblem.invalidproblemscsv"));
    }
    if (messages.size() > 0) {
        return null;
    }
    if (entryMap.size() == 0) {
        messages.add("error", new ActionMessage("onlinejudge.importProblem.emptyproblemscsv"));
    }
    ProblemPackage problemPackage = new ProblemPackage();
    problemPackage.setProblemEntries(new ProblemEntry[entryMap.size()]);
    // retrieve checker, input, output
    index = 0;
    for (String string : entryMap.keySet()) {
        ProblemEntry entry = entryMap.get(string);
        String code = entry.getProblem().getCode();
        byte[] checker = files.get(code + "/" + ProblemManager.CHECKER_FILE);
        byte[] input = files.get(code + "/" + ProblemManager.INPUT_FILE);
        byte[] output = files.get(code + "/" + ProblemManager.OUTPUT_FILE);
        byte[] text = files.get(code + "/" + ProblemManager.PROBLEM_TEXT_FILE);
        byte[] solution = null;
        byte[] checkerSource = null;
        String checkerSourceType = ProblemManager.getFileType(code, ProblemManager.CHECKER_SOURCE_FILE, files);
        if (checkerSourceType != null) {
            checkerSource = files.get(code + "/" + ProblemManager.CHECKER_SOURCE_FILE + "." + checkerSourceType);
        }
        String solutionType = ProblemManager.getFileType(code, ProblemManager.JUDGE_SOLUTION_FILE, files);
        if (solutionType != null) {
            solution = files.get(code + "/" + ProblemManager.JUDGE_SOLUTION_FILE + "." + solutionType);
        }
        if ("cpp".equals(checkerSourceType)) {
            checkerSourceType = "cc";
        }
        if ("cpp".equals(solutionType)) {
            solutionType = "cc";
        }
        entry.setChecker(checker);
        entry.setInput(input);
        entry.setOutput(output);
        entry.setText(text);
        entry.setSolution(solution);
        entry.setSolutionType(solutionType);
        entry.setCheckerSource(checkerSource);
        entry.setCheckerSourceType(checkerSourceType);
        problemPackage.getProblemEntries()[index] = entry;
        index++;
    }
    // retrieve images
    Map<String, byte[]> imageMap = new HashMap<String, byte[]>();
    Map<String, String> usedImages = new HashMap<String, String>();
    Map<String, String> duplicateImages = new HashMap<String, String>();
    for (String string : files.keySet()) {
        String path = string;
        if (ProblemManager.isImageFile(path)) {
            String imageName = new File(path).getName();
            if (imageMap.containsKey(imageName)) {
                String s = duplicateImages.get(imageName);
                s = (s == null ? "" : s) + " " + path;
                duplicateImages.put(imageName, s);
            }
            if (ProblemManager.isUsed(imageName)) {
                String s = usedImages.get(imageName);
                s = (s == null ? "" : s) + " " + path;
                usedImages.put(imageName, s);
            }
            imageMap.put(imageName, files.get(path));
        }
    }
    problemPackage.setImages(imageMap);
    problemPackage.setUsedImages(usedImages);
    problemPackage.setDuplicateImages(duplicateImages);
    return problemPackage;
}
Also used : InputStreamReader(java.io.InputStreamReader) HashMap(java.util.HashMap) IOException(java.io.IOException) TreeMap(java.util.TreeMap) ByteArrayInputStream(java.io.ByteArrayInputStream) BufferedReader(java.io.BufferedReader) ActionMessage(org.apache.struts.action.ActionMessage) Problem(cn.edu.zju.acm.onlinejudge.bean.Problem) Limit(cn.edu.zju.acm.onlinejudge.bean.Limit) File(java.io.File)

Example 40 with Problem

use of cn.edu.zju.acm.onlinejudge.bean.Problem in project zoj by licheng.

the class JudgeClientUnitTest method init.

@BeforeClass
public static void init() throws Exception {
    ReflectionUtil.setFieldValue(DAOFactory.class, "languageDAO", new MockLanguageDAO());
    ReflectionUtil.setFieldValue(DAOFactory.class, "problemDAO", new MockProblemDAO());
    ReflectionUtil.setFieldValue(DAOFactory.class, "submissionDAO", new MockSubmissionDAO());
    ReflectionUtil.setFieldValue(DAOFactory.class, "referenceDAO", new MockReferenceDAO());
    Problem problem = new Problem();
    problem.setId(0);
    problem.setRevision(0);
    Limit limit = new Limit();
    limit.setTimeLimit(1);
    limit.setMemoryLimit(1024);
    limit.setOutputLimit(1);
    problem.setLimit(limit);
    Reference reference = new Reference();
    reference.setReferenceType(ReferenceType.INPUT);
    reference.setContent("0 0\n1 2\n2 3\n".getBytes("ASCII"));
    DAOFactory.getReferenceDAO().save(reference, 0);
    DAOFactory.getReferenceDAO().save(reference, 1);
    reference = new Reference();
    reference.setReferenceType(ReferenceType.OUTPUT);
    reference.setContent("0\n3\n5\n".getBytes("ASCII"));
    DAOFactory.getReferenceDAO().save(reference, 0);
    DAOFactory.getProblemDAO().update(problem);
}
Also used : Reference(cn.edu.zju.acm.onlinejudge.bean.Reference) Problem(cn.edu.zju.acm.onlinejudge.bean.Problem) Limit(cn.edu.zju.acm.onlinejudge.bean.Limit) BeforeClass(org.junit.BeforeClass)

Aggregations

Problem (cn.edu.zju.acm.onlinejudge.bean.Problem)44 ActionForward (org.apache.struts.action.ActionForward)11 AbstractContest (cn.edu.zju.acm.onlinejudge.bean.AbstractContest)10 ArrayList (java.util.ArrayList)9 Limit (cn.edu.zju.acm.onlinejudge.bean.Limit)8 ActionMessage (org.apache.struts.action.ActionMessage)7 ProblemPersistence (cn.edu.zju.acm.onlinejudge.persistence.ProblemPersistence)6 ActionMessages (org.apache.struts.action.ActionMessages)6 Reference (cn.edu.zju.acm.onlinejudge.bean.Reference)5 PersistenceException (cn.edu.zju.acm.onlinejudge.persistence.PersistenceException)5 Connection (java.sql.Connection)5 PreparedStatement (java.sql.PreparedStatement)5 ResultSet (java.sql.ResultSet)5 SQLException (java.sql.SQLException)5 UserProfile (cn.edu.zju.acm.onlinejudge.bean.UserProfile)4 Submission (cn.edu.zju.acm.onlinejudge.bean.Submission)3 RankListEntry (cn.edu.zju.acm.onlinejudge.util.RankListEntry)3 List (java.util.List)3 Language (cn.edu.zju.acm.onlinejudge.bean.enumeration.Language)2 ProblemForm (cn.edu.zju.acm.onlinejudge.form.ProblemForm)2