Skip to content
Snippets Groups Projects
Commit 3d4f4799 authored by Jenny Zhang's avatar Jenny Zhang
Browse files

complete validator function for checking if the students apply the rule correctly

parent 01341dab
No related branches found
No related tags found
No related merge requests found
...@@ -25,13 +25,16 @@ ...@@ -25,13 +25,16 @@
</el-form-item> </el-form-item>
</template> </template>
<el-form-item label="Answer"> <el-form-item label="Answer">
<el-input v-model="answer"></el-input> <el-input v-model="currentAnswer"></el-input>
<el-button @click="submitAnswer">Next</el-button>
<el-button @click="submitAnswer">Submit</el-button>
<el-alert v-if="errorMsg" title="Error" type="error" :closable="false">{{ errorMsg }}</el-alert>
<el-alert v-if="msg" type="success" :closable="false">{{ msg }}</el-alert>
</el-form-item> </el-form-item>
</el-form> </el-form>
<template #footer> <template #footer>
<el-button @click="closeDialog">Cancel</el-button> <el-button @click="closeDialog">Cancel</el-button>
<el-button type="primary" @click="submitAnswer">Submit</el-button> <el-button type="primary" @click="validAnswer">Check</el-button>
<el-button @click="nextAnswer">Next</el-button>
</template> </template>
</el-dialog> </el-dialog>
</template> </template>
...@@ -48,6 +51,10 @@ const { negationIntroduction, ...@@ -48,6 +51,10 @@ const { negationIntroduction,
implyIntroduction, implyIntroduction,
implyElimination, implyElimination,
contradiction } = require('../utils/ndRules'); contradiction } = require('../utils/ndRules');
const { parse } = require("../utils/parser.js");
const { validator } = require("../utils/validator.js");
const ast = require('../utils/expAst.js');
export default { export default {
props: { props: {
...@@ -70,10 +77,13 @@ export default { ...@@ -70,10 +77,13 @@ export default {
localVisible: this.visible, localVisible: this.visible,
ruleFunction: null, ruleFunction: null,
selectedRule: '', selectedRule: '',
answer: '', standardAnswer: null,
// 新增的数据属性 expressions: [], // parsed expressions
expressions: [],
currentExpression: '', currentExpression: '',
answers: [], // original user input string
currentAnswer: '',
errorMsg: '',
msg: '',
} }
}, },
...@@ -104,6 +114,7 @@ export default { ...@@ -104,6 +114,7 @@ export default {
console.log(this.ruleFunction); console.log(this.ruleFunction);
console.log(this.conclusionString); console.log(this.conclusionString);
console.log(this.expressions); console.log(this.expressions);
console.log(this.answers);
this.selectedRule = ''; this.selectedRule = '';
this.ruleFunction = null; this.ruleFunction = null;
this.localVisible = false; this.localVisible = false;
...@@ -112,21 +123,80 @@ export default { ...@@ -112,21 +123,80 @@ export default {
// 新增的方法 // 新增的方法
submitExpressions() { submitExpressions() {
if (this.currentExpression) { if (this.currentExpression) {
this.expressions.push(this.currentExpression); this.expressions.push(parse(this.currentExpression));
this.currentExpression = ''; this.currentExpression = '';
} }
}, },
submitAnswer() { submitAnswer() {
// 提交答案逻辑 if (this.currentAnswer) {
this.$emit('submit-answer', { rule: this.selectedRule, answer: this.answer }); this.answers.push(this.currentAnswer);
this.currentAnswer = '';
this.errorMsg = '';
}
}, },
nextAnswer() { validAnswer() {
// 提交并准备下一个答案逻辑
this.$emit('next-answer', { rule: this.selectedRule, answer: this.answer }); if (!this.ruleFunction) {
this.errorMsg = 'Please select a rule';
return;
}
if (this.requiresExpressions && this.expressions.length === 0) {
this.errorMsg = 'Please input expressions';
return;
}
if (this.answers.length === 0) {
this.errorMsg = 'Please input answers';
return;
}
if (this.requiresExpressions) {
try {
this.standardAnswer = this.ruleFunction(parse(this.conclusionString), this.expressions);
} catch (e) {
this.errorMsg = e.message;
return;
}
} else {
try {
this.standardAnswer = this.ruleFunction(parse(this.conclusionString));
} catch (e) {
this.errorMsg = e.message;
return;
}
}
if(this.standardAnswer.length !== this.answers.length) {
this.errorMsg = 'Incorrect answer, the number of your answers is not correct';
return;
}
// list of strings
this.standardAnswer.forEach(answer => { ast.astsToString(answer); });
this.standardAnswer.sort();
this.answers.sort();
for (let i = 0; i < this.standardAnswer.length; i++) {
const output = validator(this.standardAnswer[i], this.answers[i]);
if (!output[0]) {
this.errorMsg = output[1];
return;
}
}
this.msg = 'Correct answer';
return;
}, },
handleRuleChange(value) { handleRuleChange(value) {
switch (value) { switch (value) {
case 'andIntroduction': case 'andIntroduction':
...@@ -175,3 +245,10 @@ export default { ...@@ -175,3 +245,10 @@ export default {
}, },
}; };
</script> </script>
<style>
.custom-alert {
background-color: #d7ecdc;
color: rgb(2, 46, 2);
}
</style>
\ No newline at end of file
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
// top is a list of string, for children nodes // top is a list of string, for children nodes
// bottomLatex is a string, for the current node // bottomLatex is a string, for the current node
// bottomValue is a string, for the original input string of the current node // bottomValue is a string, for the original input string of the current node
function sequentNode(bottomLatex, bottomString, rule, topsLatex) { function sequentNode(bottomLatex, bottomString, rule='', topsLatex=[]) {
this.rule = rule; this.rule = rule;
this.bottomLatex = bottomLatex; this.bottomLatex = bottomLatex;
this.bottomString = bottomString; this.bottomString = bottomString;
......
// src/validator.js // src/validator.js
const ast = require('./expAst.js'); const ast = require('./expAst.js');
const { parse } = require("./parser.js");
/*This function will remove spaces /*This function will remove spaces
...@@ -15,7 +14,7 @@ function removeSpaces(str) { ...@@ -15,7 +14,7 @@ function removeSpaces(str) {
// for expression, standard is an list of list of AST from logic rule, userInput is a string // for expression, standard is an list of list of AST from logic rule, userInput is a string
function validator(standardAst, userString) { function validator(standardAst, userString) {
standardString = ast.astsToString(standardAst); const standardString = ast.astsToString(standardAst);
const minLength = Math.min(standardString.length, userString.length); const minLength = Math.min(standardString.length, userString.length);
......
...@@ -3,27 +3,19 @@ const { sequentNode, linkSequentNode } = require("../../src/utils/sequentAst.js" ...@@ -3,27 +3,19 @@ const { sequentNode, linkSequentNode } = require("../../src/utils/sequentAst.js"
// linkSequentNode test // linkSequentNode test
test('linkSequentNode could work correctly for node', () => { test('linkSequentNode could work correctly for node', () => {
const inputA = new sequentNode("A"); const inputA = new sequentNode("A", "A");
const inputB = new sequentNode("B"); const inputB = new sequentNode("B", "B");
const output = linkSequentNode(inputA, inputB); const output = linkSequentNode(inputA, inputB);
const expected = new sequentNode("A", "", [inputB]); const expected = new sequentNode("A","A", "", [inputB]);
expect(output).toEqual(expected); expect(output).toEqual(expected);
}); });
test('linkSequentNode could work correctly for node', () => { test('linkSequentNode could work correctly for node', () => {
const inputA = new sequentNode("A"); const inputA = new sequentNode("A", "A");
const inputB = new sequentNode("B"); const inputB = new sequentNode("B", "B");
const output = linkSequentNode(inputA, inputB); const output = linkSequentNode(inputA, inputB);
const output1 = linkSequentNode(output, inputB); const output1 = linkSequentNode(output, inputB);
expect(output1).toEqual(new sequentNode("A", "", [inputB, inputB])); expect(output1).toEqual(new sequentNode("A", "A","", [inputB, inputB]));
}); });
test('linkSequentNode could report error when adding nodes on a full node', () => {
const inputA = new sequentNode("A");
const inputB = new sequentNode("B");
const output = linkSequentNode(inputA, inputB);
const output1 = linkSequentNode(output, inputB);
expect(() => linkSequentNode(output1, inputB)).toThrow('Addition failed, this node is already full');
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment