Skip to content

Commit 6843834

Browse files
committed
The logic for the statement evaluation is now more robust.
1 parent 4923335 commit 6843834

File tree

2 files changed

+69
-10
lines changed

2 files changed

+69
-10
lines changed

media/js/src/firstOrderLogic/firstOrderLogic.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ export const FirstOrderLogic: React.FC = () => {
8181
}
8282
setCorrectTemplate(newArr);
8383
setSelected(null);
84+
setText('');
8485
}
8586

8687
const mkSelect = (options, action) => <select className='form-select mt-2'

media/js/src/firstOrderLogic/statementInput.tsx

Lines changed: 68 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import { GridStatement } from './utils';
44
interface StatementProps {
55
correctStatement: GridStatement
66
isCorrect: boolean
7-
setIsCorrect: Function
7+
setIsCorrect: React.Dispatch<React.SetStateAction<boolean>>
88
text: string
9-
setText: Function
9+
setText: React.Dispatch<React.SetStateAction<string>>
1010
}
1111

1212
export const StatementInput: React.FC<StatementProps> = ({
@@ -18,12 +18,48 @@ export const StatementInput: React.FC<StatementProps> = ({
1818

1919
const buttonList = ['∀', '→', '∃', '∧', '≥'];
2020

21+
/**
22+
* Build an object of the key-value pairs extracted from a given statement.
23+
*
24+
* e.g: "Key1(x, value1) ∧ Key2(x, value2)" => {key: value, ..., key2: value2}
25+
* @param text
26+
*/
27+
const pullData = (text:string):Object => {
28+
const rules = {};
29+
const found = text.match(/\w+\(\w+.[\w\s]+\)+?/g);
30+
if (found) {
31+
found.forEach((keyValue) => {
32+
const key = keyValue.match(/\w+(?=\(\w+[^\)])/);
33+
const value = keyValue.match(/\w[\w\s]*(?=\))/);
34+
if (key != null && value != null) {
35+
rules[key.toString().toLowerCase()] =
36+
value.toString().toLowerCase();
37+
}
38+
});
39+
}
40+
return rules;
41+
};
42+
43+
/**
44+
* Splits the statement into a left and right object around the if
45+
* statement.
46+
* @param text
47+
*/
48+
const parseStatement = (text:string):Object[] => {
49+
const sides = text.split('→');
50+
if (sides.length == 2) {
51+
return [pullData(sides[0]), pullData(sides[1])]
52+
}
53+
};
54+
2155
const directionalRelationships = ['Top(y,x)', 'TopLeftOf(y,x)',
2256
'TopRightOf(y,x)', 'LeftOf(y,x)', 'RightOf(y,x)', 'Below(y,x)',
2357
'BottomLeftOf(y,x)', 'BottomRightOf(y,x)'];
2458

2559
const objectRelationships = ['Shape(x/y, Circle/Square/Triangle)',
26-
'Color(x/y, Blue/Green/Red))', 'Value(x/y, 0 to 9))',]
60+
'Color(x/y, Blue/Green/Red))', 'Value(x/y, 0 to 9))', 'Prime(Value(x/y))',
61+
'Location(x/y, top/bottom/left/right [number of subsets] rows/columns',
62+
'MultipleOf(Value(x/y))'];
2763

2864
const mkList = (items:string[], uniqueClass='') =>
2965
<ul className={`list-group-flush ps-2 ${uniqueClass}`}>
@@ -52,29 +88,51 @@ export const StatementInput: React.FC<StatementProps> = ({
5288
el.focus();
5389
};
5490

55-
const generalize = (text:string) => {
56-
return text.replace(/\s/g, '').toLowerCase();
91+
const evaluate = (check:Object[], evalObj:Object[]) => {
92+
if (check.length == evalObj.length) {
93+
for (let i in evalObj) {
94+
for (let [key, value] of Object.entries(evalObj[i])) {
95+
if (!check[i][key] || check[i][key] !== value) {
96+
return false;
97+
}
98+
}
99+
}
100+
return true;
101+
}
102+
return false;
57103
};
58104

59105
const handleCheck = (e) => {
60106
setSubmitted(true);
61107
const el =
62108
document.getElementById('statement-text') as HTMLInputElement;
63-
setIsCorrect(generalize(el.value) ===
64-
generalize(correctStatement.formalFOLStatement));
109+
if (el.value.length > 0) {
110+
const check = parseStatement(el.value);
111+
if (el.value.match(/^\∀x\s*\(.*\)\s*$/)) {
112+
setIsCorrect(evaluate(check,
113+
parseStatement(correctStatement.formalFOLStatement))
114+
);
115+
} else {
116+
setIsCorrect(false);
117+
}
118+
}
65119
};
66120

67121
const handleText = (e) => {
68122
setText(e.value);
69123
};
70124

71125
useEffect(() => {
72-
if (isCorrect) setFeedback('Good! XD');
73-
else setFeedback('Ooops <:O');
126+
if (isCorrect) {
127+
setFeedback('Good! XD');
128+
} else {
129+
setFeedback('Ooops <:O');
130+
}
74131
}, [isCorrect])
75132

76133
useEffect(() => {
77134
setSubmitted(false);
135+
console.log(correctStatement.formalFOLStatement);
78136
}, [])
79137

80138
return <section className='col-4'>
@@ -99,4 +157,4 @@ export const StatementInput: React.FC<StatementProps> = ({
99157
</div>
100158
</div>
101159
</section>
102-
}
160+
}

0 commit comments

Comments
 (0)