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!");
}
}
}
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);
}
Aggregations