-
Notifications
You must be signed in to change notification settings - Fork 17
/
GeneratorVisitor.java
242 lines (208 loc) · 7.41 KB
/
GeneratorVisitor.java
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
* © Copyright 2015 - SourceClear Inc
*/
package com.sourceclear.gramtest;
import org.antlr.v4.runtime.RuleContext;
import java.util.*;
/**
*
*/
public class GeneratorVisitor extends bnfBaseVisitor {
///////////////////////////// Class Attributes \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
////////////////////////////// Class Methods \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
//////////////////////////////// Attributes \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
private final Map<String,bnfParser.RhsContext> productionsMap = new HashMap<>();
private int maxNum = 100;
private int maxDepth = 2;
private int maxSize = 4;
private int minSize = 1;
private boolean useMinimalGenerator = true;
private List<String> tests = new LinkedList<>();
private final Stack<String> prodHist = new Stack<>();
/////////////////////////////// Constructors \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
public GeneratorVisitor() {
}
public GeneratorVisitor(int num, int depth, int min, int max, boolean useMinimalGenerator) {
this.maxNum = num;
this.maxDepth = depth;
this.minSize = min;
this.maxSize = max;
this.useMinimalGenerator = useMinimalGenerator;
}
////////////////////////////////// Methods \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
//------------------------ Implements:
//------------------------ Overrides:
@Override
public Object visitId(bnfParser.IdContext ctx) {
return super.visitId(ctx);
}
@Override
public String visitText(bnfParser.TextContext ctx) {
return unquote(ctx.getText());
}
@Override
public String visitCaptext(bnfParser.CaptextContext ctx) {
return ctx.getText();
}
@Override
public Object visitOneormore(bnfParser.OneormoreContext ctx) {
return super.visitOneormore(ctx);
}
@Override
public Object visitZeroormore(bnfParser.ZeroormoreContext ctx) {
return super.visitZeroormore(ctx);
}
@Override
public Object visitOptional(bnfParser.OptionalContext ctx) {
return super.visitOptional(ctx);
}
@Override
public List<String> visitElement(bnfParser.ElementContext ctx) {
List<String> result = new LinkedList<>();
if(ctx.zeroormore() != null) {
result = visitAlternatives(ctx.zeroormore().alternatives()); // one time
List<String> twoLs = combineTwoLists(result, Collections.singletonList("")); // zero time
result.addAll(twoLs);
}
else if(ctx.oneormore() != null) {
result = visitAlternatives(ctx.oneormore().alternatives()); // one time
List<String> twoLs = combineTwoLists(result,result); //two times
result.addAll(twoLs);
}
else if(ctx.optional() != null) {
result = visitAlternatives(ctx.optional().alternatives()); // one time
result.add(""); // zero time
}
else if(ctx.captext() != null) {
result.add(visitCaptext(ctx.captext()));
}
else if(ctx.text() != null) {
result.add(visitText(ctx.text()));
}
else {
String lhs = ctx.id().getText();
prodHist.push(lhs);
bnfParser.RhsContext rhs = productionsMap.get(lhs);
if(rhs.alternatives() == null ||
rhs.alternatives().alternative().stream().allMatch(ac -> isTerminalContext(ac.element())) ||
prodHist.size() < productionsMap.size()) {
return visitRhs(productionsMap.get(lhs));
}
else {
int choices = rhs.alternatives().alternative().size();
int rand = new Random().nextInt(choices);
result = visitAlternative(rhs.alternatives().alternative(rand));
if(!prodHist.empty()) prodHist.pop();
return result;
}
}
return result;
}
@Override
public List<String> visitAlternative(bnfParser.AlternativeContext ctx) {
List<List<String>> comStr = new LinkedList<>();
for(bnfParser.ElementContext ec1 : ctx.element()) {
if(prodHist.size() < maxDepth) {
List<String> slist = visitElement(ec1);
if (!slist.isEmpty()) comStr.add(slist);
}
}
List<String> emptyStr = new LinkedList<>();
emptyStr.add("");
return generateAllStrings(comStr, emptyStr);
}
@Override
public List<String> visitAlternatives(bnfParser.AlternativesContext ctx) {
List<String> altStrs = new LinkedList<>();
List<bnfParser.AlternativeContext> acList = ctx.alternative();
Collections.shuffle(acList);
for(bnfParser.AlternativeContext ac : acList) {
altStrs.addAll(visitAlternative(ac));
}
return altStrs;
}
@Override
public List<String> visitRhs(bnfParser.RhsContext ctx) {
List<String> tmp = visitAlternatives(ctx.alternatives());
if(!prodHist.empty()) prodHist.pop();
return tmp;
}
@Override
public Object visitLhs(bnfParser.LhsContext ctx) {
return super.visitLhs(ctx);
}
@Override
public List<String> visitRule_(bnfParser.Rule_Context ctx) {
return visitRhs(ctx.rhs());
}
@Override
public Object visitRulelist(bnfParser.RulelistContext ctx) {
List<String> sentences = new LinkedList<>();
for(bnfParser.Rule_Context rc : ctx.rule_()) {
productionsMap.put(rc.lhs().id().getText(), rc.rhs());
}
maxDepth = maxDepth + productionsMap.size();
// Should not visit all the rules, but start from the top one
/*
for(bnfParser.Rule_Context rc : ctx.rule_()) {
sentences.addAll(visitRule_(rc));
}
*/
sentences.addAll(visitRule_(ctx.rule_(0)));
//sentences.removeIf(s -> s.length() < minSize);
if(sentences.size() > maxNum)
tests = sentences.subList(0, maxNum); // return only top maxNum test cases
else tests = sentences;
return super.visitRulelist(ctx);
}
//---------------------------- Abstract Methods -----------------------------
//---------------------------- Utility Methods ------------------------------
private String unquote(String s) {
if (s != null && ((s.startsWith("\"") && s.endsWith("\"")) || (s.startsWith("'") && s.endsWith("'")))) {
s = s.substring(1, s.length() - 1);
}
return s;
}
private List<String> generateAllStrings(List<List<String>> strList, List<String> result) {
if(strList.size() > 0) {
List<String> newResult = combineTwoLists(result,strList.remove(0));
if(newResult.isEmpty()) return result;
else return generateAllStrings(strList, newResult);
}
return result;
}
private List<String> combineTwoLists(List<String> preList, List<String> postList) {
List<String> combList = new LinkedList<>();
if(useMinimalGenerator) {
Stack<String> preStack = new Stack<>();
preStack.addAll(preList);
Stack<String> postStack = new Stack<>();
postStack.addAll(postList);
while(!preStack.empty() && !postStack.empty()) {
String s1 = preStack.pop();
String s2 = postStack.pop();
if(combList.size() < maxNum && (s1.length() + s2.length()) > minSize
&& !(s1.isEmpty() && s2.isEmpty()))
combList.add(s1+s2);
}
}
for(String s1 : preList) {
for(String s2 : postList) {
if(combList.size() < maxNum && (s1.length() + s2.length()) < maxSize
&& !(s1.isEmpty() && s2.isEmpty()))
combList.add(s1+s2);
}
}
return combList;
}
private Boolean isTerminalContext(List<bnfParser.ElementContext> eclist) {
for(bnfParser.ElementContext ec : eclist) {
if (ec.text() == null && ec.captext() == null) return false;
}
return true;
}
//---------------------------- Property Methods -----------------------------
public List<String> getTests() {
return tests;
}
}