-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(FeedbackRule): Add support for using parentheses in expression (#…
…946)
- Loading branch information
1 parent
cd823dc
commit 3b71c76
Showing
7 changed files
with
196 additions
and
82 deletions.
There are no files selected for viewing
21 changes: 0 additions & 21 deletions
21
src/assets/wise5/components/common/feedbackRule/FeedbackRule.spec.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/assets/wise5/components/common/feedbackRule/FeedbackRuleExpression.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { FeedbackRuleExpression } from './FeedbackRuleExpression'; | ||
|
||
describe('FeedbackRuleExpression', () => { | ||
getPostfix(); | ||
}); | ||
|
||
function getPostfix() { | ||
describe('getPostfix()', () => { | ||
it('should convert text to array in postfix order', () => { | ||
expectPostfixExpression('1 && 2 && 3', ['1', '2', '&&', '3', '&&']); | ||
expectPostfixExpression('1 || 2 && 3', ['1', '2', '||', '3', '&&']); | ||
expectPostfixExpression('!1 && !2', ['1', '!', '2', '!', '&&']); | ||
expectPostfixExpression('1 && isSubmitNumber(2)', ['1', 'isSubmitNumber(2)', '&&']); | ||
expectPostfixExpression('(1 && 2) || (3 && 4)', ['1', '2', '&&', '3', '4', '&&', '||']); | ||
expectPostfixExpression('!(1 || 2) && 3', ['1', '2', '||', '!', '3', '&&']); | ||
expectPostfixExpression('(!1 || (2 && 3)) || 4', ['1', '!', '2', '3', '&&', '||', '4', '||']); | ||
}); | ||
}); | ||
} | ||
|
||
function expectPostfixExpression(text: string, expectedResult: string[]) { | ||
const expression = new FeedbackRuleExpression(text); | ||
expect(expression.getPostfix()).toEqual(expectedResult); | ||
} |
87 changes: 87 additions & 0 deletions
87
src/assets/wise5/components/common/feedbackRule/FeedbackRuleExpression.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
export class FeedbackRuleExpression { | ||
static operatorPrecedences = { '!': 2, '&&': 1, '||': 1 }; | ||
|
||
constructor(private text: string) {} | ||
|
||
// uses shunting-yard algorithm to get expression in postfix (reverse-polish) notation | ||
getPostfix(): string[] { | ||
const result = []; | ||
const operatorStack = []; | ||
for (const symbol of this.getExpressionAsArray()) { | ||
if (FeedbackRuleExpression.isOperator(symbol)) { | ||
this.processOperator(symbol, operatorStack, result); | ||
} else if (FeedbackRuleExpression.isLeftParenthesis(symbol)) { | ||
operatorStack.push(symbol); | ||
} else if (FeedbackRuleExpression.isRightParenthesis(symbol)) { | ||
this.processRightParenthesis(operatorStack, result); | ||
} else { | ||
result.push(symbol); | ||
} | ||
} | ||
while (operatorStack.length > 0) { | ||
result.push(operatorStack.pop()); | ||
} | ||
return result; | ||
} | ||
|
||
private getExpressionAsArray(): string[] { | ||
return this.text | ||
.replace(/ /g, '') | ||
.split( | ||
/(hasKIScore\(\d\)|ideaCountEquals\(\d\)|ideaCountLessThan\(\d\)|ideaCountMoreThan\(\d\)|isSubmitNumber\(\d+\)|&&|\|\||!|\(|\))/g | ||
) | ||
.filter((el) => el !== ''); | ||
} | ||
|
||
private processOperator(symbol: string, operatorStack: string[], result: string[]): void { | ||
while (operatorStack.length > 0) { | ||
const topOperatorOnStack = operatorStack[operatorStack.length - 1]; | ||
if ( | ||
FeedbackRuleExpression.hasGreaterPrecedence(topOperatorOnStack, symbol) || | ||
FeedbackRuleExpression.hasSamePrecedence(topOperatorOnStack, symbol) | ||
) { | ||
result.push(operatorStack.pop()); | ||
} else { | ||
break; | ||
} | ||
} | ||
operatorStack.push(symbol); | ||
} | ||
|
||
private processRightParenthesis(operatorStack: string[], result: string[]): void { | ||
let topOperatorOnStack = operatorStack[operatorStack.length - 1]; | ||
while (!FeedbackRuleExpression.isLeftParenthesis(topOperatorOnStack)) { | ||
result.push(operatorStack.pop()); | ||
topOperatorOnStack = operatorStack[operatorStack.length - 1]; | ||
} | ||
operatorStack.pop(); // discard left parenthesis | ||
} | ||
|
||
static isLeftParenthesis(symbol: string): boolean { | ||
return symbol === '('; | ||
} | ||
|
||
static isRightParenthesis(symbol: string): boolean { | ||
return symbol === ')'; | ||
} | ||
|
||
static isOperator(symbol: string): boolean { | ||
return ['&&', '||', '!'].includes(symbol); | ||
} | ||
|
||
static isOperand(symbol: string): boolean { | ||
return !this.isOperator(symbol); | ||
} | ||
|
||
static hasGreaterPrecedence(symbol1: string, symbol2: string): boolean { | ||
return this.getPrecedence(symbol1) > this.getPrecedence(symbol2); | ||
} | ||
|
||
static hasSamePrecedence(symbol1: string, symbol2: string): boolean { | ||
return this.getPrecedence(symbol1) === this.getPrecedence(symbol2); | ||
} | ||
|
||
static getPrecedence(symbol: string): number { | ||
return this.operatorPrecedences[symbol] ?? 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters