use of at.ac.tuwien.kr.alpha.api.config.InputConfig in project Alpha by alpha-asp.
the class AlphaImplTest method withExternalInvocationCounted2.
@Test
@Disabled("External atom has state, which is not allowed. Caching of calls makes the number of invocations wrong.")
public void withExternalInvocationCounted2() throws Exception {
Alpha system = new AlphaImpl();
InputConfig cfg = InputConfig.forString("a. b :- &isOne[1], &isOne[2].");
cfg.addPredicateMethod("isOne", Externals.processPredicateMethod(this.getClass().getMethod("isOne", int.class)));
ASPCore2Program prog = system.readProgram(cfg);
int before = invocations;
Set<AnswerSet> actual = system.solve(prog).collect(Collectors.toSet());
int after = invocations;
assertEquals(2, after - before);
Set<AnswerSet> expected = new HashSet<>(singletonList(new AnswerSetBuilder().predicate("a").build()));
assertEquals(expected, actual);
}
use of at.ac.tuwien.kr.alpha.api.config.InputConfig in project Alpha by alpha-asp.
the class AlphaImplTest method withNativeExternal.
@Test
public void withNativeExternal() throws Exception {
Alpha system = new AlphaImpl();
InputConfig cfg = InputConfig.forString("a :- &isTwo[2].");
cfg.addPredicateMethod("isTwo", Externals.processPredicate((Integer t) -> t == 2));
ASPCore2Program prog = system.readProgram(cfg);
Set<AnswerSet> actual = system.solve(prog).collect(Collectors.toSet());
Set<AnswerSet> expected = new HashSet<>(singletonList(new AnswerSetBuilder().predicate("a").build()));
assertEquals(expected, actual);
}
use of at.ac.tuwien.kr.alpha.api.config.InputConfig 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.api.config.InputConfig in project Alpha by alpha-asp.
the class Main method main.
public static void main(String[] args) {
CommandLineParser commandLineParser = new CommandLineParser(Main.ALPHA_CALL_SYNTAX, (msg) -> Main.exitWithMessage(msg, 0));
AlphaConfig cfg = null;
try {
cfg = commandLineParser.parseCommandLine(args);
} catch (ParseException ex) {
System.err.println("Invalid usage: " + ex.getMessage());
Main.exitWithMessage(commandLineParser.getUsageMessage(), 1);
}
Alpha alpha = new AlphaImpl(cfg.getSystemConfig());
ASPCore2Program program = null;
try {
program = alpha.readProgram(cfg.getInputConfig());
} catch (FileNotFoundException e) {
Main.bailOut(e.getMessage());
} catch (IOException e) {
Main.bailOut("Failed to parse program.", e);
}
InputConfig inputCfg = cfg.getInputConfig();
Solver solver;
if (inputCfg.isDebugPreprocessing()) {
DebugSolvingContext dbgCtx = alpha.prepareDebugSolve(program);
Main.writeNormalProgram(dbgCtx.getNormalizedProgram(), inputCfg.getNormalizedPath());
Main.writeNormalProgram(dbgCtx.getPreprocessedProgram(), inputCfg.getPreprocessedPath());
Main.writeDependencyGraph(dbgCtx.getDependencyGraph(), inputCfg.getDepgraphPath());
Main.writeComponentGraph(dbgCtx.getComponentGraph(), inputCfg.getCompgraphPath());
solver = dbgCtx.getSolver();
} else {
solver = alpha.prepareSolverFor(program, inputCfg.getFilter());
}
Main.computeAndConsumeAnswerSets(solver, cfg);
}
use of at.ac.tuwien.kr.alpha.api.config.InputConfig in project Alpha by alpha-asp.
the class CommandLineParser method parseCommandLine.
public AlphaConfig parseCommandLine(String[] args) throws ParseException {
CommandLine commandLine = new DefaultParser().parse(CommandLineParser.CLI_OPTS, args);
if (commandLine.getArgs().length > 0) {
throw new ParseException("Positional arguments { " + StringUtils.join(args, ' ') + " } are invalid!");
}
AlphaConfig retVal = new AlphaConfig();
SystemConfig sysConf = new SystemConfig();
InputConfig inputConf = new InputConfig();
if (commandLine.hasOption(CommandLineParser.OPT_HELP.getOpt())) {
LOGGER.debug("Found help option!");
this.handleHelp();
} else {
this.validate(commandLine);
}
for (Option opt : commandLine.getOptions()) {
this.handleOption(opt, sysConf, inputConf);
}
retVal.setSystemConfig(sysConf);
retVal.setInputConfig(inputConf);
return retVal;
}
Aggregations