Browse Source

feat: judge code

haha 4 years ago
parent
commit
10257f5ec0

+ 28 - 0
seec-oj-server/pom.xml

@@ -43,6 +43,34 @@
                 </exclusion>
             </exclusions>
         </dependency>
+        <dependency>
+            <groupId>org.hibernate</groupId>
+            <artifactId>hibernate-core</artifactId>
+            <version>5.4.21.Final</version>
+        </dependency>
+
+        <dependency>
+            <groupId>cn.seecoder</groupId>
+            <artifactId>seecoder-bok-client</artifactId>
+            <version>0.2.3</version>
+            <exclusions>
+                <exclusion>
+                    <groupId>org.apache.logging.log4j</groupId>
+                    <artifactId>log4j-slf4j-impl</artifactId>
+                </exclusion>
+            </exclusions>
+        </dependency>
+
+        <dependency>
+            <groupId>cn.seecoder</groupId>
+            <artifactId>seecoder-bok-common</artifactId>
+            <version>0.2.3</version>
+        </dependency>
+        <dependency>
+            <groupId>com.nju.edu</groupId>
+            <artifactId>PaasMonitor-client</artifactId>
+            <version>0.1.2</version>
+        </dependency>
 
 
     </dependencies>

+ 23 - 4
seec-oj-server/src/main/java/cn/seecoder/ojserver/controller/JudgeController.java

@@ -1,15 +1,34 @@
 package cn.seecoder.ojserver.controller;
 
+import cn.seecoder.ojserver.model.vo.CodeJudgeVO;
+import cn.seecoder.ojserver.service.JudgeService;
 import cn.seecoder.ojserver.web.Response;
-import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
+import com.nju.edu.paasMonitorClient.MonitorClient;
+import com.nju.edu.paasMonitorClient.entity.Call;
+import com.nju.edu.paasMonitorCommon.enums.SeecSystem;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.*;
 
 @RestController
 @RequestMapping(path = "/api/judge")
 public class JudgeController {
+    private JudgeService judgeService;
+
+    public JudgeController(JudgeService judgeService) {
+        this.judgeService = judgeService;
+    }
+
     @GetMapping("")
-    public Response getJudge(){
+    public Response getJudge() {
         return Response.buildSuccess(true);
     }
+
+    @PostMapping("/code")
+    public Response judgeCode(@RequestParam(value = "questionId") String questionId, @RequestBody CodeJudgeVO codeJudgeVO) {
+        Call call = MonitorClient.createCall(SeecSystem.CODER,"ojtest","/api/judge/code");
+        call.start();
+        Response res = Response.buildSuccess(judgeService.judgeCode(questionId, codeJudgeVO));
+        call.end();
+        return res;
+    }
 }

+ 21 - 0
seec-oj-server/src/main/java/cn/seecoder/ojserver/exception/ThirdPartyServiceException.java

@@ -0,0 +1,21 @@
+package cn.seecoder.ojserver.exception;
+
+import lombok.Data;
+import org.hibernate.service.spi.ServiceException;
+
+/**
+ * @author mars
+ */
+@Data
+public class ThirdPartyServiceException extends ServiceException {
+    private String code;
+
+    public ThirdPartyServiceException(String message, Throwable root) {
+        super(message, root);
+    }
+
+    public ThirdPartyServiceException(String message, String code) {
+        super(message);
+        this.code = code;
+    }
+}

+ 32 - 0
seec-oj-server/src/main/java/cn/seecoder/ojserver/manager/JudgeManager.java

@@ -0,0 +1,32 @@
+package cn.seecoder.ojserver.manager;
+
+import cn.seecoder.codejudge.client.JudgeDTO;
+import cn.seecoder.codejudge.client.JudgeResult;
+import cn.seecoder.ojserver.exception.ThirdPartyServiceException;
+import cn.seecoder.ojserver.utils.thirdparty.CodeJudgeThirdPartyService;
+import org.springframework.stereotype.Service;
+
+import java.util.Optional;
+
+/**
+ * 封装了对客观题和代码题的判题
+ * @author mars
+ */
+@Service
+public class JudgeManager {
+    private final CodeJudgeThirdPartyService codeJudgeThirdPartyService;
+    public JudgeManager(CodeJudgeThirdPartyService codeJudgeThirdPartyService) {
+        this.codeJudgeThirdPartyService = codeJudgeThirdPartyService;
+    }
+
+    public JudgeResult judgeCodeQuestionAnswers(JudgeDTO judgeDTO) {
+        Optional<JudgeResult> res = codeJudgeThirdPartyService.judgeCodeQuestionAnswers(judgeDTO);
+        if (res.isPresent()) {
+            return res.get();
+        } else {
+            throw new ThirdPartyServiceException("C0001", "代码题判题异常");
+        }
+    }
+
+
+}

+ 26 - 0
seec-oj-server/src/main/java/cn/seecoder/ojserver/manager/QuestionManager.java

@@ -0,0 +1,26 @@
+package cn.seecoder.ojserver.manager;
+
+import cn.seecoder.ojserver.exception.ThirdPartyServiceException;
+import cn.seecoder.ojserver.model.vo.CompleteQuestionVO;
+import cn.seecoder.ojserver.utils.thirdparty.QuestionThirdPartyService;
+import org.springframework.stereotype.Service;
+
+import java.util.Optional;
+
+@Service
+public class QuestionManager {
+    private final QuestionThirdPartyService questionThirdPartyService;
+
+    public QuestionManager(QuestionThirdPartyService questionThirdPartyService) {
+        this.questionThirdPartyService = questionThirdPartyService;
+    }
+
+    public Optional<CompleteQuestionVO> getQuestionById(String id) {
+        Optional<CompleteQuestionVO> res = questionThirdPartyService.getQuestionById(id);
+        if (!res.isPresent()) {
+            throw new ThirdPartyServiceException("C0001", "没有对应的问题Id");
+        } else {
+            return res;
+        }
+    }
+}

+ 18 - 0
seec-oj-server/src/main/java/cn/seecoder/ojserver/model/vo/CodeJudgeVO.java

@@ -0,0 +1,18 @@
+package cn.seecoder.ojserver.model.vo;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+import java.io.Serializable;
+
+/**
+ * @author mars
+ */
+@Data
+@NoArgsConstructor
+@AllArgsConstructor
+public class CodeJudgeVO implements Serializable {
+    private String code;
+    private String language;
+}

+ 90 - 0
seec-oj-server/src/main/java/cn/seecoder/ojserver/model/vo/CompleteQuestionVO.java

@@ -0,0 +1,90 @@
+package cn.seecoder.ojserver.model.vo;
+
+import cn.seecoder.bok.common.QuestionType;
+import com.fasterxml.jackson.annotation.JsonInclude;
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+import java.io.Serializable;
+import java.time.Instant;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * @author mars
+ */
+@AllArgsConstructor
+@NoArgsConstructor
+@Data
+@JsonInclude(JsonInclude.Include.NON_NULL)
+public class CompleteQuestionVO implements Serializable {
+    private String id;
+
+    /**
+     * 题干
+     */
+    private String stem;
+
+    /**
+     * 题目类型
+     */
+    private QuestionType type;
+
+    /**
+     * 选项
+     */
+    private Map<String, String> options;
+
+    /**
+     * 填空题 __1__ __2__
+     */
+//    private Map<String, String> blanks;
+
+    /**
+     * 答案
+     */
+    private String answer;
+
+    /**
+     * 关键点
+     */
+    private String keyPoints;
+
+    /**
+     * 分析
+     */
+    private String analysis;
+
+    /**
+     * 标签
+     */
+    private List<String> tags;
+
+    /**
+     * 知识点 id
+     */
+    private List<String> knowledgeId;
+
+    /**
+     * 输入输出的测试用例
+     */
+    private Map<String, String> inputOutputMapping;
+
+
+    /**
+     * 代码题目时间限制
+     */
+    private Integer millisecondLimit;
+
+    /**
+     * 代码空间限制
+     */
+    private Integer memoryLimit;
+
+
+    private Instant createdTime;
+
+    private Instant lastModifiedTime;
+
+}

+ 8 - 0
seec-oj-server/src/main/java/cn/seecoder/ojserver/service/JudgeService.java

@@ -0,0 +1,8 @@
+package cn.seecoder.ojserver.service;
+
+import cn.seecoder.codejudge.client.JudgeResult;
+import cn.seecoder.ojserver.model.vo.CodeJudgeVO;
+
+public interface JudgeService {
+    public JudgeResult judgeCode(String questionId, CodeJudgeVO codeJudgeVO);
+}

+ 45 - 0
seec-oj-server/src/main/java/cn/seecoder/ojserver/service/impl/JudgeServiceImpl.java

@@ -0,0 +1,45 @@
+package cn.seecoder.ojserver.service.impl;
+
+import cn.seecoder.codejudge.client.JudgeDTO;
+import cn.seecoder.codejudge.client.JudgeResult;
+import cn.seecoder.ojserver.exception.ThirdPartyServiceException;
+import cn.seecoder.ojserver.manager.JudgeManager;
+import cn.seecoder.ojserver.manager.QuestionManager;
+import cn.seecoder.ojserver.model.vo.CodeJudgeVO;
+import cn.seecoder.ojserver.model.vo.CompleteQuestionVO;
+import cn.seecoder.ojserver.service.JudgeService;
+import org.springframework.stereotype.Service;
+
+import java.util.Map;
+import java.util.stream.Collectors;
+
+@Service
+public class JudgeServiceImpl implements JudgeService {
+    private final QuestionManager questionManager;
+    private final JudgeManager judgeManager;
+
+    public JudgeServiceImpl(QuestionManager questionManager, JudgeManager judgeManager) {
+        this.questionManager = questionManager;
+        this.judgeManager = judgeManager;
+    }
+
+    @Override
+    public JudgeResult judgeCode(String questionId, CodeJudgeVO codeJudgeVO) {
+        try {
+            JudgeDTO judgeDTO = new JudgeDTO();
+            judgeDTO.setCode(codeJudgeVO.getCode());
+            judgeDTO.setLanguage(codeJudgeVO.getLanguage());
+
+            CompleteQuestionVO question = questionManager.getQuestionById(questionId).get();
+            judgeDTO.setMemoryLimit(question.getMemoryLimit());
+            judgeDTO.setInputList(question.getInputOutputMapping().entrySet().stream().map(Map.Entry<String, String>::getKey).collect(Collectors.toList()));
+            judgeDTO.setOutputList(question.getInputOutputMapping().entrySet().stream().map(Map.Entry<String, String>::getValue).collect(Collectors.toList()));
+
+            return judgeManager.judgeCodeQuestionAnswers(judgeDTO);
+        } catch (ThirdPartyServiceException exception) {
+            throw exception;
+        }
+
+
+    }
+}

+ 20 - 0
seec-oj-server/src/main/java/cn/seecoder/ojserver/utils/thirdparty/CodeJudgeThirdPartyService.java

@@ -0,0 +1,20 @@
+package cn.seecoder.ojserver.utils.thirdparty;
+
+import cn.seecoder.codejudge.client.JudgeDTO;
+import cn.seecoder.codejudge.client.JudgeResult;
+
+import java.util.Optional;
+
+/**
+ * @author mars
+ * 封装调用的代码题评测的第三方服务
+ */
+public interface CodeJudgeThirdPartyService {
+    /**
+     * 代码题判题
+     * @param judgeDTO 代码题相关信息
+     * @return 判题结果
+     * @see JudgeResult
+     */
+    Optional<JudgeResult> judgeCodeQuestionAnswers(JudgeDTO judgeDTO);
+}

+ 21 - 0
seec-oj-server/src/main/java/cn/seecoder/ojserver/utils/thirdparty/QuestionThirdPartyService.java

@@ -0,0 +1,21 @@
+package cn.seecoder.ojserver.utils.thirdparty;
+
+import cn.seecoder.ojserver.model.vo.CompleteQuestionVO;
+
+import java.util.List;
+import java.util.Optional;
+
+/**
+ * 封装调用的从题库拿题的第三方服务
+ * 参考 http://gogs.seec.seecoder.cn/SEEC-BOK/SEEC-BOK-CLIENT/src/master/seecoder-bok-client/src/main/java/cn/seecoder/bok/client 编写
+ * @author mars
+ */
+public interface QuestionThirdPartyService {
+    /**
+     * 根据id从题库系统获得题目
+     * @param id 题目id
+     * @return QuestionVO
+     * @see CompleteQuestionVO
+     */
+    Optional<CompleteQuestionVO> getQuestionById(String id);
+}

+ 40 - 0
seec-oj-server/src/main/java/cn/seecoder/ojserver/utils/thirdparty/impl/CodeJudgeThirdPartyServiceImpl.java

@@ -0,0 +1,40 @@
+package cn.seecoder.ojserver.utils.thirdparty.impl;
+
+
+import cn.seecoder.codejudge.client.CodeJudgeClient;
+import cn.seecoder.codejudge.client.CodeJudgeSystemException;
+import cn.seecoder.codejudge.client.JudgeDTO;
+import cn.seecoder.codejudge.client.JudgeResult;
+import cn.seecoder.ojserver.utils.thirdparty.CodeJudgeThirdPartyService;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.PostConstruct;
+import java.util.Optional;
+
+/**
+ * @author mars
+ */
+@Component
+public class CodeJudgeThirdPartyServiceImpl implements CodeJudgeThirdPartyService {
+    @Value("${code-judgement-client.host}")
+    private String host;
+
+    private CodeJudgeClient client;
+
+    @PostConstruct
+    public void init() {
+        client = new CodeJudgeClient(host);
+    }
+
+    @Override
+    public Optional<JudgeResult> judgeCodeQuestionAnswers(JudgeDTO judgeDTO) {
+        try {
+            judgeDTO.setTimeLimit(1000);
+            System.out.println(judgeDTO);
+            return Optional.of(client.judge(judgeDTO));
+        } catch (CodeJudgeSystemException judgementException) {
+            return Optional.empty();
+        }
+    }
+}

+ 39 - 0
seec-oj-server/src/main/java/cn/seecoder/ojserver/utils/thirdparty/impl/QuestionThirdPartyServiceImpl.java

@@ -0,0 +1,39 @@
+package cn.seecoder.ojserver.utils.thirdparty.impl;
+
+import cn.seecoder.bok.client.SeecoderBokClient;
+import cn.seecoder.bok.common.vo.QuestionVO;
+import cn.seecoder.ojserver.model.vo.CompleteQuestionVO;
+import cn.seecoder.ojserver.utils.thirdparty.QuestionThirdPartyService;
+import org.springframework.beans.BeanUtils;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.PostConstruct;
+import java.util.Optional;
+
+/**
+ * @author mars
+ * 封装调用的题库的第三方服务
+ */
+@Component
+public class QuestionThirdPartyServiceImpl implements QuestionThirdPartyService {
+    @Value("${seec-bok-client.host}")
+    private String host;
+
+    private SeecoderBokClient client;
+
+
+    @PostConstruct
+    public void init() {
+        client = new SeecoderBokClient(host);
+    }
+
+
+    @Override
+    public Optional<CompleteQuestionVO> getQuestionById(String id) {
+        Optional<QuestionVO> response = client.getQuestionById(id);
+        CompleteQuestionVO res = new CompleteQuestionVO();
+        response.ifPresent(questionVO -> BeanUtils.copyProperties(questionVO, res));
+        return Optional.of(res);
+    }
+}

+ 13 - 0
seec-oj-server/src/main/resources/application-prod.yml

@@ -0,0 +1,13 @@
+server:
+  port: 8080
+
+seec-bok-client:
+  host: https://bok.seec.seecoder.cn
+
+objective-judgement-client:
+  host: https://seecoder-judgement.seec.seecoder.cn
+
+code-judgement-client:
+  host: https://oj.seec.seecoder.cn
+
+

+ 3 - 0
seec-oj-server/src/main/resources/application.yml

@@ -0,0 +1,3 @@
+spring:
+  profiles:
+    active: prod