Parcourir la source

添加代码沙箱接口定义

rongrunxiang il y a 1 an
Parent
commit
7b1b2a44de
23 fichiers modifiés avec 435 ajouts et 2 suppressions
  1. 30 0
      src/main/java/com/example/onlinejudge/judge/JudgeManager.java
  2. 10 0
      src/main/java/com/example/onlinejudge/judge/JudgeService.java
  3. 21 0
      src/main/java/com/example/onlinejudge/judge/JudgeServiceImpl.java
  4. 8 0
      src/main/java/com/example/onlinejudge/judge/codesandbox/CodeSandbox.java
  5. 4 0
      src/main/java/com/example/onlinejudge/judge/codesandbox/CodeSandboxFactory.java
  6. 24 0
      src/main/java/com/example/onlinejudge/judge/codesandbox/CodeSandboxProxy.java
  7. 28 0
      src/main/java/com/example/onlinejudge/judge/codesandbox/impl/ExampleCodeSandbox.java
  8. 13 0
      src/main/java/com/example/onlinejudge/judge/codesandbox/impl/RemoteCodeSandbox.java
  9. 14 0
      src/main/java/com/example/onlinejudge/judge/codesandbox/impl/ThirdPartyCodeSandbox.java
  10. 21 0
      src/main/java/com/example/onlinejudge/judge/codesandbox/model/ExecuteCodeRequest.java
  11. 23 0
      src/main/java/com/example/onlinejudge/judge/codesandbox/model/ExecuteCodeResponse.java
  12. 22 0
      src/main/java/com/example/onlinejudge/judge/codesandbox/model/JudgeInfo.java
  13. 10 0
      src/main/java/com/example/onlinejudge/judge/strategy/DefaultJudgeStrategy.java
  14. 10 0
      src/main/java/com/example/onlinejudge/judge/strategy/JavaLanguageJudgeStrategy.java
  15. 25 0
      src/main/java/com/example/onlinejudge/judge/strategy/JudgeContext.java
  16. 10 0
      src/main/java/com/example/onlinejudge/judge/strategy/JudgeStrategy.java
  17. 39 0
      src/main/java/com/example/onlinejudge/model/dto/question/QuestionAddRequest.java
  18. 39 0
      src/main/java/com/example/onlinejudge/model/dto/question/QuestionEditRequest.java
  19. 28 0
      src/main/java/com/example/onlinejudge/model/dto/question/QuestionQueryRequest.java
  20. 44 0
      src/main/java/com/example/onlinejudge/model/dto/question/QuestionUpdateRequest.java
  21. 0 2
      src/main/java/com/example/onlinejudge/model/entity/SubmitCode.java
  22. 4 0
      src/main/java/com/example/onlinejudge/service/QuestionSubmitService.java
  23. 8 0
      src/main/java/com/example/onlinejudge/service/impl/QuestionSubmitServiceImpl.java

+ 30 - 0
src/main/java/com/example/onlinejudge/judge/JudgeManager.java

@@ -0,0 +1,30 @@
+package com.example.onlinejudge.judge;
+
+import com.example.onlinejudge.judge.strategy.DefaultJudgeStrategy;
+import com.example.onlinejudge.judge.strategy.JavaLanguageJudgeStrategy;
+import com.example.onlinejudge.judge.strategy.JudgeContext;
+import com.example.onlinejudge.judge.strategy.JudgeStrategy;
+import com.example.onlinejudge.model.dto.questionSubmit.JudgeInfo;
+import com.example.onlinejudge.model.entity.QuestionSubmit;
+
+/**
+ * 判题管理(简化调用)
+ */
+public class JudgeManager {
+
+        /**
+        * 执行判题
+        *
+        * @param judgeContext
+        * @return
+        */
+        JudgeInfo doJudge(JudgeContext judgeContext) {
+            QuestionSubmit questionSubmit = judgeContext.getQuestionSubmit();
+            String language = questionSubmit.getLanguage();
+            JudgeStrategy judgeStrategy = new DefaultJudgeStrategy();
+            if ("java".equals(language)) {
+                judgeStrategy = new JavaLanguageJudgeStrategy();
+            }
+            return judgeStrategy.doJudge(judgeContext);
+        }
+}

+ 10 - 0
src/main/java/com/example/onlinejudge/judge/JudgeService.java

@@ -0,0 +1,10 @@
+package com.example.onlinejudge.judge;
+
+import com.example.onlinejudge.model.entity.QuestionSubmit;
+
+/**
+ * 判题服务
+ */
+public interface JudgeService {
+    QuestionSubmit doJudge(long questionSubmitId);
+}

+ 21 - 0
src/main/java/com/example/onlinejudge/judge/JudgeServiceImpl.java

@@ -0,0 +1,21 @@
+package com.example.onlinejudge.judge;
+
+import com.example.onlinejudge.model.entity.QuestionSubmit;
+import com.example.onlinejudge.service.QuestionService;
+import com.example.onlinejudge.service.QuestionSubmitService;
+import org.springframework.stereotype.Service;
+
+import javax.annotation.Resource;
+
+@Service
+public class JudgeServiceImpl implements JudgeService{
+
+    @Resource
+    private QuestionService questionService;
+
+    @Resource
+    QuestionSubmitService questionSubmitService;
+    public QuestionSubmit doJudge(long questionSubmitId) {
+        return null;
+    }
+}

+ 8 - 0
src/main/java/com/example/onlinejudge/judge/codesandbox/CodeSandbox.java

@@ -0,0 +1,8 @@
+package com.example.onlinejudge.judge.codesandbox;
+
+import com.example.onlinejudge.judge.codesandbox.model.ExecuteCodeRequest;
+import com.example.onlinejudge.judge.codesandbox.model.ExecuteCodeResponse;
+
+public interface CodeSandbox {
+    ExecuteCodeResponse executeCode(ExecuteCodeRequest executeCodeRequest);
+}

+ 4 - 0
src/main/java/com/example/onlinejudge/judge/codesandbox/CodeSandboxFactory.java

@@ -0,0 +1,4 @@
+package com.example.onlinejudge.judge.codesandbox;
+
+public class CodeSandboxFactory {
+}

+ 24 - 0
src/main/java/com/example/onlinejudge/judge/codesandbox/CodeSandboxProxy.java

@@ -0,0 +1,24 @@
+package com.example.onlinejudge.judge.codesandbox;
+
+
+import com.example.onlinejudge.judge.codesandbox.model.ExecuteCodeRequest;
+import com.example.onlinejudge.judge.codesandbox.model.ExecuteCodeResponse;
+import lombok.extern.slf4j.Slf4j;
+
+@Slf4j
+public class CodeSandboxProxy implements CodeSandbox{
+
+    private final CodeSandbox codeSandbox;
+
+    public CodeSandboxProxy(CodeSandbox codeSandbox) {
+        this.codeSandbox = codeSandbox;
+    }
+
+    @Override
+    public ExecuteCodeResponse executeCode(ExecuteCodeRequest executeCodeRequest) {
+        log.info("代码沙箱请求信息:" + executeCodeRequest.toString());
+        ExecuteCodeResponse executeCodeResponse = codeSandbox.executeCode(executeCodeRequest);
+        log.info("代码沙箱响应信息:" + executeCodeResponse.toString());
+        return executeCodeResponse;
+    }
+}

+ 28 - 0
src/main/java/com/example/onlinejudge/judge/codesandbox/impl/ExampleCodeSandbox.java

@@ -0,0 +1,28 @@
+package com.example.onlinejudge.judge.codesandbox.impl;
+
+import com.example.onlinejudge.judge.codesandbox.CodeSandbox;
+import com.example.onlinejudge.judge.codesandbox.model.ExecuteCodeRequest;
+import com.example.onlinejudge.judge.codesandbox.model.ExecuteCodeResponse;
+import com.example.onlinejudge.judge.codesandbox.model.JudgeInfo;
+import com.example.onlinejudge.model.enums.JudgeInfoMessageEnum;
+import com.example.onlinejudge.model.enums.QuestionSubmitStatusEnum;
+
+import java.util.List;
+
+public class ExampleCodeSandbox implements CodeSandbox {
+
+    @Override
+    public ExecuteCodeResponse executeCode(ExecuteCodeRequest executeCodeRequest) {
+        List<String> inputList = executeCodeRequest.getInputList();
+        ExecuteCodeResponse executeCodeResponse = new ExecuteCodeResponse();
+        executeCodeResponse.setOutputList(inputList);
+        executeCodeResponse.setMessage("测试执行成功");
+        executeCodeResponse.setStatus(QuestionSubmitStatusEnum.SUCCEED.getValue());
+        JudgeInfo judgeInfo = new JudgeInfo();
+        judgeInfo.setMessage(JudgeInfoMessageEnum.ACCEPTED.getText());
+        judgeInfo.setMemory(100L);
+        judgeInfo.setTime(100L);
+        executeCodeResponse.setJudgeInfo(judgeInfo);
+        return executeCodeResponse;
+    }
+}

+ 13 - 0
src/main/java/com/example/onlinejudge/judge/codesandbox/impl/RemoteCodeSandbox.java

@@ -0,0 +1,13 @@
+package com.example.onlinejudge.judge.codesandbox.impl;
+
+import com.example.onlinejudge.judge.codesandbox.CodeSandbox;
+import com.example.onlinejudge.judge.codesandbox.model.ExecuteCodeRequest;
+import com.example.onlinejudge.judge.codesandbox.model.ExecuteCodeResponse;
+
+public class RemoteCodeSandbox implements CodeSandbox {
+    @Override
+    public ExecuteCodeResponse executeCode(ExecuteCodeRequest executeCodeRequest) {
+        System.out.println("远程代码沙箱");
+        return null;
+    }
+}

+ 14 - 0
src/main/java/com/example/onlinejudge/judge/codesandbox/impl/ThirdPartyCodeSandbox.java

@@ -0,0 +1,14 @@
+package com.example.onlinejudge.judge.codesandbox.impl;
+
+import com.example.onlinejudge.judge.codesandbox.CodeSandbox;
+import com.example.onlinejudge.judge.codesandbox.model.ExecuteCodeRequest;
+import com.example.onlinejudge.judge.codesandbox.model.ExecuteCodeResponse;
+
+public class ThirdPartyCodeSandbox implements CodeSandbox {
+
+    @Override
+    public ExecuteCodeResponse executeCode(ExecuteCodeRequest executeCodeRequest) {
+        System.out.println("第三方代码沙箱");
+        return null;
+    }
+}

+ 21 - 0
src/main/java/com/example/onlinejudge/judge/codesandbox/model/ExecuteCodeRequest.java

@@ -0,0 +1,21 @@
+package com.example.onlinejudge.judge.codesandbox.model;
+
+import lombok.AllArgsConstructor;
+import lombok.Builder;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+import java.util.List;
+
+@Data
+@Builder
+@NoArgsConstructor
+@AllArgsConstructor
+public class ExecuteCodeRequest {
+
+    private List<String> inputList;
+
+    private String code;
+
+    private String language;
+}

+ 23 - 0
src/main/java/com/example/onlinejudge/judge/codesandbox/model/ExecuteCodeResponse.java

@@ -0,0 +1,23 @@
+package com.example.onlinejudge.judge.codesandbox.model;
+
+import lombok.AllArgsConstructor;
+import lombok.Builder;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+import java.util.List;
+
+@Data
+@Builder
+@NoArgsConstructor
+@AllArgsConstructor
+public class ExecuteCodeResponse {
+
+    private List<String> outputList;
+
+    private String message;
+
+    private Integer status;
+
+    private JudgeInfo judgeInfo;
+}

+ 22 - 0
src/main/java/com/example/onlinejudge/judge/codesandbox/model/JudgeInfo.java

@@ -0,0 +1,22 @@
+package com.example.onlinejudge.judge.codesandbox.model;
+
+import lombok.Data;
+
+@Data
+public class JudgeInfo {
+
+    /**
+     * 程序执行信息
+     */
+    private String message;
+
+    /**
+     * 消耗内存(KB)
+     */
+    private Long memory;
+
+    /**
+     * 消耗时间
+     */
+    private Long time;
+}

+ 10 - 0
src/main/java/com/example/onlinejudge/judge/strategy/DefaultJudgeStrategy.java

@@ -0,0 +1,10 @@
+package com.example.onlinejudge.judge.strategy;
+
+import com.example.onlinejudge.model.dto.questionSubmit.JudgeInfo;
+
+public class DefaultJudgeStrategy implements  JudgeStrategy{
+    @Override
+    public JudgeInfo doJudge(JudgeContext judgeContext) {
+        return null;
+    }
+}

+ 10 - 0
src/main/java/com/example/onlinejudge/judge/strategy/JavaLanguageJudgeStrategy.java

@@ -0,0 +1,10 @@
+package com.example.onlinejudge.judge.strategy;
+
+import com.example.onlinejudge.model.dto.questionSubmit.JudgeInfo;
+
+public class JavaLanguageJudgeStrategy implements JudgeStrategy{
+    @Override
+    public JudgeInfo doJudge(JudgeContext judgeContext) {
+        return null;
+    }
+}

+ 25 - 0
src/main/java/com/example/onlinejudge/judge/strategy/JudgeContext.java

@@ -0,0 +1,25 @@
+package com.example.onlinejudge.judge.strategy;
+
+import com.example.onlinejudge.model.dto.question.JudgeCase;
+import com.example.onlinejudge.model.dto.questionSubmit.JudgeInfo;
+import com.example.onlinejudge.model.entity.Question;
+import com.example.onlinejudge.model.entity.QuestionSubmit;
+import lombok.Data;
+import java.util.List;
+@Data
+public class JudgeContext {
+
+    private JudgeInfo judgeInfo;
+
+    private List<String> inputList;
+
+    private List<String> outputList;
+
+    private List<JudgeCase> judgeCaseList;
+
+    private Question question;
+
+    private QuestionSubmit questionSubmit;
+
+
+}

+ 10 - 0
src/main/java/com/example/onlinejudge/judge/strategy/JudgeStrategy.java

@@ -0,0 +1,10 @@
+package com.example.onlinejudge.judge.strategy;
+
+import com.example.onlinejudge.model.dto.questionSubmit.JudgeInfo;
+
+/**
+ * 判题策略
+ */
+public interface JudgeStrategy {
+    JudgeInfo doJudge(JudgeContext judgeContext);
+}

+ 39 - 0
src/main/java/com/example/onlinejudge/model/dto/question/QuestionAddRequest.java

@@ -0,0 +1,39 @@
+package com.example.onlinejudge.model.dto.question;
+
+import java.io.Serializable;
+import java.util.List;
+
+public class QuestionAddRequest implements Serializable {
+
+        /**
+        * 标题
+        */
+        private String title;
+
+        /**
+        * 内容
+        */
+        private String content;
+
+        /**
+        * 标签列表
+        */
+        private List<String> tags;
+
+        /**
+        * 题目答案
+        */
+        private String answer;
+
+        /**
+        * 判题用例
+        */
+        private List<JudgeCase> judgeCase;
+
+        /**
+        * 判题配置
+        */
+        private JudgeConfig judgeConfig;
+
+        private static final long serialVersionUID = 1L;
+}

+ 39 - 0
src/main/java/com/example/onlinejudge/model/dto/question/QuestionEditRequest.java

@@ -0,0 +1,39 @@
+package com.example.onlinejudge.model.dto.question;
+
+import java.io.Serializable;
+import java.util.List;
+
+public class QuestionEditRequest implements Serializable{
+
+        /**
+        * 标题
+        */
+        private String title;
+
+        /**
+        * 内容
+        */
+        private String content;
+
+        /**
+        * 标签列表
+        */
+        private List<String> tags;
+
+        /**
+        * 题目答案
+        */
+        private String answer;
+
+        /**
+        * 判题用例
+        */
+        private List<JudgeCase> judgeCase;
+
+        /**
+        * 判题配置
+        */
+        private JudgeConfig judgeConfig;
+
+        private static final long serialVersionUID = 1L;
+}

+ 28 - 0
src/main/java/com/example/onlinejudge/model/dto/question/QuestionQueryRequest.java

@@ -0,0 +1,28 @@
+package com.example.onlinejudge.model.dto.question;
+
+import java.io.Serializable;
+
+public class QuestionQueryRequest implements Serializable {
+
+    /**
+     * 标题
+     */
+    private String title;
+
+    /**
+     * 标签
+     */
+    private String tag;
+
+    /**
+     * 页码
+     */
+    private Integer page;
+
+    /**
+     * 每页数量
+     */
+    private Integer pageSize;
+
+    private static final long serialVersionUID = 1L;
+}

+ 44 - 0
src/main/java/com/example/onlinejudge/model/dto/question/QuestionUpdateRequest.java

@@ -0,0 +1,44 @@
+package com.example.onlinejudge.model.dto.question;
+
+import java.io.Serializable;
+import java.util.List;
+
+public class QuestionUpdateRequest implements Serializable {
+
+    /**
+     * id
+     */
+    private Long id;
+
+    /**
+     * 标题
+     */
+    private String title;
+
+    /**
+     * 内容
+     */
+    private String content;
+
+    /**
+     * 标签列表
+     */
+    private List<String> tags;
+
+    /**
+     * 题目答案
+     */
+    private String answer;
+
+    /**
+     * 判题用例
+     */
+    private List<JudgeCase> judgeCase;
+
+    /**
+     * 判题配置
+     */
+    private JudgeConfig judgeConfig;
+
+    private static final long serialVersionUID = 1L;
+}

+ 0 - 2
src/main/java/com/example/onlinejudge/model/entity/SubmitCode.java

@@ -57,6 +57,4 @@ public class SubmitCode {
 
     @ApiModelProperty("状态")
     String state;
-
-
 }

+ 4 - 0
src/main/java/com/example/onlinejudge/service/QuestionSubmitService.java

@@ -0,0 +1,4 @@
+package com.example.onlinejudge.service;
+
+public interface QuestionSubmitService {
+}

+ 8 - 0
src/main/java/com/example/onlinejudge/service/impl/QuestionSubmitServiceImpl.java

@@ -0,0 +1,8 @@
+package com.example.onlinejudge.service.impl;
+
+import com.example.onlinejudge.service.QuestionSubmitService;
+import org.springframework.stereotype.Service;
+
+@Service
+public class QuestionSubmitServiceImpl implements QuestionSubmitService {
+}