/
StepUtil.ts
76 lines (60 loc) · 2.91 KB
/
StepUtil.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import * as arrayMove from 'array-move';
import * as deepcopy from 'deepcopy';
import { Step } from '../ast/Step';
import { NLPUtil, Entities } from '../nlp'
import { NodeTypes } from "../req/NodeTypes";
import { isDefined } from "../util/TypeChecking";
import { KeywordDictionary } from "../dict/KeywordDictionary";
import { convertCase } from "../util/CaseConversor";
import { CaseType } from "../app/CaseType";
export class StepUtil {
/**
* Move precondition steps to the beginning of the array, after other precondition steps.
* When there is a GIVEN step followed by an AND step, and the GIVEN needs to be moved,
* the AND step is changed to become a GIVEN step.
*
* @param steps Steps to analyze
*/
movePreconditionStepsToTheBeginning( steps: Step[], keywords: KeywordDictionary ): Step[] {
const nlpUtil = new NLPUtil();
let lastWasGiven: boolean | null = null;
let index = 0, preconditionCount = 0;
const stepCount = steps.length;
let newSteps = deepcopy( steps ); // << important
for ( let step of steps ) {
if ( NodeTypes.STEP_GIVEN === step.nodeType
|| ( NodeTypes.STEP_AND == step.nodeType && true === lastWasGiven ) ) {
const hasPrecondition = isDefined( step.nlpResult )
&& nlpUtil.hasEntityNamed( Entities.STATE, step.nlpResult );
if ( hasPrecondition ) {
// Does not have prior GIVEN ? -> Make it a GIVEN
if ( preconditionCount < 1 ) {
newSteps[ index ].nodeType = NodeTypes.STEP_GIVEN;
}
if ( preconditionCount != index ) {
arrayMove.mut( newSteps, index, preconditionCount );
}
// Is the next step an AND step ?
if ( index + 1 < stepCount && newSteps[ index + 1 ].nodeType === NodeTypes.STEP_AND ) {
let nextStep = newSteps[ index + 1 ];
// Make it a GIVEN step
nextStep.nodeType = NodeTypes.STEP_GIVEN;
// Change the sentence content!
if ( !! nextStep.content ) {
const stepAndKeyword: string = ( keywords.stepAnd || [ 'and' ] )[ 0 ];
const stepGivenKeyword: string = ( keywords.stepGiven || [ 'given' ] )[ 0 ];
const regex = new RegExp( stepAndKeyword, 'i' );
nextStep.content = nextStep.content.replace( regex, convertCase( stepGivenKeyword, CaseType.PASCAL ) ); // Given ...
}
}
++preconditionCount;
}
lastWasGiven = true;
} else {
lastWasGiven = false;
}
++index;
}
return newSteps;
}
}