有关:
编辑:其实你可以让它工作!唯一要解决的是强加一些域特定的约束来排除组合爆炸,如a.
如果你添加到Xeger类这样的东西:
public void enumerate() {
System.out.println("enumerate: \"" + regex + "\"");
int level = 0;
String accumulated = "";
enumerate(level, accumulated, automaton.getInitialState());
}
private void enumerate(int level, String accumulated, State state) {
List transitions = state.getSortedTransitions(true);
if (state.isAccept()) {
System.out.println(accumulated);
return;
}
if (transitions.size() == 0) {
assert state.isAccept();
return;
}
int nroptions = state.isAccept() ? transitions.size() : transitions.size() - 1;
for (int option = 0; option <= nroptions; option++) {
// Moving on to next transition
Transition transition = transitions.get(option - (state.isAccept() ? 1 : 0));
for (char choice = transition.getMin(); choice <= transition.getMax(); choice++) {
enumerate(level + 1, accumulated + choice, transition.getDest());
}
}
}
…和XegerTest这样的东西
@Test
public void enumerateAllVariants() {
//String regex = "[ab]{4,6}c";
String regex = "34\\.25\\.14\\.(227|228|229|230|243|244|245|246)";
Xeger generator = new Xeger(regex);
generator.enumerate();
}
你会得到这个:
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running nl.flotsam.xeger.XegerTest
enumerate: "34\.25\.14\.(227|228|229|230|243|244|245|246)"
34.25.14.227
34.25.14.228
34.25.14.229
34.25.14.243
34.25.14.244
34.25.14.245
34.25.14.246
34.25.14.230
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.114 sec
… 你猜怎么着.对于“[ab] {4,6} c”,它正确地产生了112个变体.
这真的是一个快速而肮脏的实验,但它似乎工作;).