use of mb.statix.generator.SearchStrategy in project nabl by metaborg.
the class StatixGenerate method run.
public void run(String file) throws MetaborgException, InterruptedException {
final FileObject resource = STX.S.resolve(file);
TermFormatter tf = ITerm::toString;
try {
final ILanguageImpl lang = STX.cli.loadLanguage(STX.project.location());
final IContext context = STX.S.contextService.get(resource, STX.project, lang);
tf = StatixGenerator.pretty(STX.S, context, "pp-generated");
} catch (MetaborgException e) {
// ignore
}
final TermFormatter _tf = tf;
final Function1<SearchState, String> pretty = (s) -> _tf.format(project(VAR, s));
final DescriptiveStatistics hitStats = new DescriptiveStatistics();
final DescriptiveStatistics missStats = new DescriptiveStatistics();
final StreamProgressPrinter progress = new StreamProgressPrinter(System.err, 80, out -> {
long hits = hitStats.getN();
long all = hits + missStats.getN();
out.println(" " + hits + "/" + all + " " + summary(hitStats));
});
final SearchLogger<SearchState, SearchState> searchLog = new SearchLogger<SearchState, SearchState>() {
@Override
public void init(long seed, SearchStrategy<SearchState, SearchState> strategy, Iterable<IConstraint> constraints) {
log.info("seed {}", seed);
log.info("strategy {}", strategy);
log.info("constraints {}", constraints);
}
@Override
public void success(SearchNode<SearchState> n) {
progress.step('+');
addSize(n.output(), hitStats);
logSuccess(log, Level.Debug, n, pretty);
}
@Override
public void failure(SearchNodes<?> nodes) {
progress.step('.');
SearchNode<?> parentNode = nodes.parent();
if (parentNode != null && parentNode.output() instanceof SearchState) {
addSize((SearchState) parentNode.output(), missStats);
}
logFailure(log, Level.Debug, nodes, pretty);
}
private void addSize(SearchState s, DescriptiveStatistics stats) {
s.state().unifier().size(project(VAR, s)).ifFinite(size -> {
stats.addValue(size.doubleValue());
});
}
};
final StatixGenerator statixGen = new StatixGenerator(STX.S, STX.context, resource);
// Paret.addFragments(statixGen.spec());
final Spec spec = statixGen.spec();
final RandomTermGenerator rtg = new RandomTermGenerator(spec, statixGen.constraint(), new Paret(spec).search(), searchLog);
final Stream<SearchState> resultStream = rtg.apply().nodes().map(sn -> {
searchLog.success(sn);
return sn.output();
});
log.info("Generating random terms.");
final List<SearchState> results = Lists.newArrayList(resultStream.limit(COUNT).iterator());
progress.done();
results.forEach(s -> {
System.out.println(pretty.apply(s));
});
log.info("Generated {} random terms.", results.size());
logStatsInfo("hits", hitStats);
logStatsInfo("misses", missStats);
}
use of mb.statix.generator.SearchStrategy in project nabl by metaborg.
the class Seq method doApply.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public SearchNodes<O> doApply(SearchContext ctx, SearchNode<I> node) {
Stream<SearchNode> nodes = Stream.of(node);
Deque<Function0<String>> descs = Queues.newArrayDeque();
for (SearchStrategy s : ss) {
nodes = flatMap(nodes, n -> {
final SearchNodes<?> sn = s.apply(ctx, n);
descs.push(sn::desc);
return sn.nodes();
});
}
final Function0<String> desc = () -> descs.stream().map(Function0::apply).collect(Collectors.joining(" . ", "(", ")"));
return SearchNodes.of(node, desc, (Stream) nodes);
}
Aggregations