Search in sources :

Example 1 with StatixGenerator

use of org.metaborg.spoofax.core.shell.StatixGenerator 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);
}
Also used : Spec(mb.statix.spec.Spec) Function1(org.metaborg.util.functions.Function1) ITerm(mb.nabl2.terms.ITerm) RandomTermGenerator(mb.statix.generator.RandomTermGenerator) SearchStrategy(mb.statix.generator.SearchStrategy) MetaborgException(org.metaborg.core.MetaborgException) Lists(com.google.common.collect.Lists) SearchLogger(mb.statix.generator.SearchLogger) ITermVar(mb.nabl2.terms.ITermVar) ILogger(org.metaborg.util.log.ILogger) B(mb.nabl2.terms.build.TermBuild.B) Level(org.metaborg.util.log.Level) SearchNode(mb.statix.generator.nodes.SearchNode) TermFormatter(mb.nabl2.util.TermFormatter) SearchNodes(mb.statix.generator.nodes.SearchNodes) FileObject(org.apache.commons.vfs2.FileObject) SearchElement(mb.statix.generator.nodes.SearchElement) List(java.util.List) Stream(java.util.stream.Stream) ILanguageImpl(org.metaborg.core.language.ILanguageImpl) IConstraint(mb.statix.solver.IConstraint) DescriptiveStatistics(org.apache.commons.math3.stat.descriptive.DescriptiveStatistics) IContext(org.metaborg.core.context.IContext) SearchState(mb.statix.generator.SearchState) LoggerUtils(org.metaborg.util.log.LoggerUtils) StatixGenerator(org.metaborg.spoofax.core.shell.StatixGenerator) StreamProgressPrinter(mb.statix.generator.util.StreamProgressPrinter) DescriptiveStatistics(org.apache.commons.math3.stat.descriptive.DescriptiveStatistics) SearchLogger(mb.statix.generator.SearchLogger) TermFormatter(mb.nabl2.util.TermFormatter) IContext(org.metaborg.core.context.IContext) SearchStrategy(mb.statix.generator.SearchStrategy) RandomTermGenerator(mb.statix.generator.RandomTermGenerator) MetaborgException(org.metaborg.core.MetaborgException) SearchNodes(mb.statix.generator.nodes.SearchNodes) FileObject(org.apache.commons.vfs2.FileObject) SearchNode(mb.statix.generator.nodes.SearchNode) StreamProgressPrinter(mb.statix.generator.util.StreamProgressPrinter) ILanguageImpl(org.metaborg.core.language.ILanguageImpl) SearchState(mb.statix.generator.SearchState) Spec(mb.statix.spec.Spec) StatixGenerator(org.metaborg.spoofax.core.shell.StatixGenerator)

Aggregations

Lists (com.google.common.collect.Lists)1 List (java.util.List)1 Stream (java.util.stream.Stream)1 ITerm (mb.nabl2.terms.ITerm)1 ITermVar (mb.nabl2.terms.ITermVar)1 B (mb.nabl2.terms.build.TermBuild.B)1 TermFormatter (mb.nabl2.util.TermFormatter)1 RandomTermGenerator (mb.statix.generator.RandomTermGenerator)1 SearchLogger (mb.statix.generator.SearchLogger)1 SearchState (mb.statix.generator.SearchState)1 SearchStrategy (mb.statix.generator.SearchStrategy)1 SearchElement (mb.statix.generator.nodes.SearchElement)1 SearchNode (mb.statix.generator.nodes.SearchNode)1 SearchNodes (mb.statix.generator.nodes.SearchNodes)1 StreamProgressPrinter (mb.statix.generator.util.StreamProgressPrinter)1 IConstraint (mb.statix.solver.IConstraint)1 Spec (mb.statix.spec.Spec)1 DescriptiveStatistics (org.apache.commons.math3.stat.descriptive.DescriptiveStatistics)1 FileObject (org.apache.commons.vfs2.FileObject)1 MetaborgException (org.metaborg.core.MetaborgException)1