Search in sources :

Example 1 with SimpleAnswerSetFormatter

use of at.ac.tuwien.kr.alpha.commons.util.SimpleAnswerSetFormatter in project Alpha by alpha-asp.

the class Main method computeAndConsumeAnswerSets.

private static void computeAndConsumeAnswerSets(Solver solver, AlphaConfig alphaCfg) {
    SystemConfig sysCfg = alphaCfg.getSystemConfig();
    InputConfig inputCfg = alphaCfg.getInputConfig();
    Stream<AnswerSet> stream = solver.stream();
    if (sysCfg.isSortAnswerSets()) {
        stream = stream.sorted();
    }
    int limit = inputCfg.getNumAnswerSets();
    if (limit > 0) {
        stream = stream.limit(limit);
    }
    if (!sysCfg.isQuiet()) {
        AtomicInteger counter = new AtomicInteger(0);
        final BiConsumer<Integer, AnswerSet> answerSetHandler;
        final AnswerSetFormatter<String> fmt = new SimpleAnswerSetFormatter(sysCfg.getAtomSeparator());
        BiConsumer<Integer, AnswerSet> stdoutPrinter = (n, as) -> {
            System.out.println("Answer set " + Integer.toString(n) + ":" + System.lineSeparator() + fmt.format(as));
        };
        if (inputCfg.isWriteAnswerSetsAsXlsx()) {
            BiConsumer<Integer, AnswerSet> xlsxWriter = new AnswerSetToXlsxWriter(inputCfg.getAnswerSetFileOutputPath());
            answerSetHandler = stdoutPrinter.andThen(xlsxWriter);
        } else {
            answerSetHandler = stdoutPrinter;
        }
        stream.forEach(as -> {
            int cnt = counter.incrementAndGet();
            answerSetHandler.accept(cnt, as);
        });
        if (counter.get() == 0) {
            System.out.println("UNSATISFIABLE");
            if (inputCfg.isWriteAnswerSetsAsXlsx()) {
                try {
                    AnswerSetToXlsxWriter.writeUnsatInfo(Paths.get(inputCfg.getAnswerSetFileOutputPath() + ".UNSAT.xlsx"));
                } catch (IOException ex) {
                    System.err.println("Failed writing unsat file!");
                }
            }
        } else {
            System.out.println("SATISFIABLE");
        }
    } else {
        // Note: Even though we are not consuming the result, we will still compute
        // answer sets.
        stream.collect(Collectors.toList());
    }
    if (sysCfg.isPrintStats()) {
        if (solver instanceof StatisticsReportingSolver) {
            ((StatisticsReportingSolver) solver).printStatistics();
        } else {
            // Note: Should not happen with proper validation of commandline args
            System.err.println("Solver of type " + solver.getClass().getSimpleName() + " does not support solving statistics!");
        }
    }
}
Also used : DependencyGraphWriter(at.ac.tuwien.kr.alpha.app.DependencyGraphWriter) DebugSolvingContext(at.ac.tuwien.kr.alpha.api.DebugSolvingContext) AnswerSetFormatter(at.ac.tuwien.kr.alpha.api.util.AnswerSetFormatter) LoggerFactory(org.slf4j.LoggerFactory) InputConfig(at.ac.tuwien.kr.alpha.api.config.InputConfig) SimpleAnswerSetFormatter(at.ac.tuwien.kr.alpha.commons.util.SimpleAnswerSetFormatter) NormalProgram(at.ac.tuwien.kr.alpha.api.programs.NormalProgram) CommandLineParser(at.ac.tuwien.kr.alpha.app.config.CommandLineParser) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Solver(at.ac.tuwien.kr.alpha.api.Solver) AlphaConfig(at.ac.tuwien.kr.alpha.api.config.AlphaConfig) BiConsumer(java.util.function.BiConsumer) AlphaImpl(at.ac.tuwien.kr.alpha.api.impl.AlphaImpl) PrintStream(java.io.PrintStream) Logger(org.slf4j.Logger) DependencyGraph(at.ac.tuwien.kr.alpha.api.programs.analysis.DependencyGraph) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) AnswerSet(at.ac.tuwien.kr.alpha.api.AnswerSet) StatisticsReportingSolver(at.ac.tuwien.kr.alpha.api.StatisticsReportingSolver) Collectors(java.util.stream.Collectors) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) Stream(java.util.stream.Stream) Paths(java.nio.file.Paths) ParseException(org.apache.commons.cli.ParseException) ComponentGraphWriter(at.ac.tuwien.kr.alpha.app.ComponentGraphWriter) SystemConfig(at.ac.tuwien.kr.alpha.api.config.SystemConfig) ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) Alpha(at.ac.tuwien.kr.alpha.api.Alpha) ComponentGraph(at.ac.tuwien.kr.alpha.api.programs.analysis.ComponentGraph) SystemConfig(at.ac.tuwien.kr.alpha.api.config.SystemConfig) AnswerSet(at.ac.tuwien.kr.alpha.api.AnswerSet) StatisticsReportingSolver(at.ac.tuwien.kr.alpha.api.StatisticsReportingSolver) IOException(java.io.IOException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SimpleAnswerSetFormatter(at.ac.tuwien.kr.alpha.commons.util.SimpleAnswerSetFormatter) InputConfig(at.ac.tuwien.kr.alpha.api.config.InputConfig)

Example 2 with SimpleAnswerSetFormatter

use of at.ac.tuwien.kr.alpha.commons.util.SimpleAnswerSetFormatter in project Alpha by alpha-asp.

the class SimpleAnswerSetFormatterTest method basicFormatterWithSeparator.

@Test
public void basicFormatterWithSeparator() {
    AnswerSetFormatter<String> fmt = new SimpleAnswerSetFormatter(" bla ");
    AnswerSet as = new AnswerSetBuilder().predicate("p").instance("a").predicate("q").instance("b").build();
    String formatted = fmt.format(as);
    assertEquals("{ p(\"a\") bla q(\"b\") }", formatted);
}
Also used : AnswerSet(at.ac.tuwien.kr.alpha.api.AnswerSet) SimpleAnswerSetFormatter(at.ac.tuwien.kr.alpha.commons.util.SimpleAnswerSetFormatter) AnswerSetBuilder(at.ac.tuwien.kr.alpha.commons.AnswerSetBuilder) Test(org.junit.jupiter.api.Test)

Aggregations

AnswerSet (at.ac.tuwien.kr.alpha.api.AnswerSet)2 SimpleAnswerSetFormatter (at.ac.tuwien.kr.alpha.commons.util.SimpleAnswerSetFormatter)2 Alpha (at.ac.tuwien.kr.alpha.api.Alpha)1 DebugSolvingContext (at.ac.tuwien.kr.alpha.api.DebugSolvingContext)1 Solver (at.ac.tuwien.kr.alpha.api.Solver)1 StatisticsReportingSolver (at.ac.tuwien.kr.alpha.api.StatisticsReportingSolver)1 AlphaConfig (at.ac.tuwien.kr.alpha.api.config.AlphaConfig)1 InputConfig (at.ac.tuwien.kr.alpha.api.config.InputConfig)1 SystemConfig (at.ac.tuwien.kr.alpha.api.config.SystemConfig)1 AlphaImpl (at.ac.tuwien.kr.alpha.api.impl.AlphaImpl)1 ASPCore2Program (at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program)1 NormalProgram (at.ac.tuwien.kr.alpha.api.programs.NormalProgram)1 ComponentGraph (at.ac.tuwien.kr.alpha.api.programs.analysis.ComponentGraph)1 DependencyGraph (at.ac.tuwien.kr.alpha.api.programs.analysis.DependencyGraph)1 AnswerSetFormatter (at.ac.tuwien.kr.alpha.api.util.AnswerSetFormatter)1 ComponentGraphWriter (at.ac.tuwien.kr.alpha.app.ComponentGraphWriter)1 DependencyGraphWriter (at.ac.tuwien.kr.alpha.app.DependencyGraphWriter)1 CommandLineParser (at.ac.tuwien.kr.alpha.app.config.CommandLineParser)1 AnswerSetBuilder (at.ac.tuwien.kr.alpha.commons.AnswerSetBuilder)1 File (java.io.File)1