use of at.ac.tuwien.kr.alpha.api.AnswerSet 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.AnswerSet in project Alpha by alpha-asp.
the class SimpleAnswerSetFormatter method format.
@Override
public String format(AnswerSet answerSet) {
List<String> predicateInstanceStrings = new ArrayList<>();
for (Predicate p : answerSet.getPredicates()) {
SortedSet<Atom> instances;
if ((instances = answerSet.getPredicateInstances(p)) == null || instances.isEmpty()) {
predicateInstanceStrings.add(p.getName());
} else {
List<String> atomStrings = instances.stream().map((atom) -> atom.toString()).collect(Collectors.toList());
predicateInstanceStrings.add(String.join(this.atomSeparator, atomStrings));
}
}
return "{ " + String.join(this.atomSeparator, predicateInstanceStrings) + " }";
}
use of at.ac.tuwien.kr.alpha.api.AnswerSet in project Alpha by alpha-asp.
the class AnswerSetQueryTest method matchXWithFuncTerm.
@Test
public void matchXWithFuncTerm() {
Predicate p = Predicates.getPredicate("p", 2);
Atom a1 = Atoms.newBasicAtom(p, Terms.newSymbolicConstant("x"), Terms.newFunctionTerm("f", Terms.newSymbolicConstant("x")));
Atom a2 = Atoms.newBasicAtom(p, Terms.newSymbolicConstant("y"), Terms.newFunctionTerm("f", Terms.newSymbolicConstant("y")));
Atom a3 = Atoms.newBasicAtom(p, Terms.newSymbolicConstant("y"), Terms.newFunctionTerm("f", Terms.newSymbolicConstant("x")));
Atom a4 = Atoms.newBasicAtom(p, Terms.newSymbolicConstant("x"), Terms.newFunctionTerm("f", Terms.newSymbolicConstant("y")));
Atom a5 = Atoms.newBasicAtom(p, Terms.newSymbolicConstant("x"), Terms.newFunctionTerm("f"));
SortedSet<Predicate> predicates = new TreeSet<>();
predicates.add(p);
Map<Predicate, SortedSet<Atom>> instances = new HashMap<>();
SortedSet<Atom> ps = new TreeSet<>();
ps.add(a1);
ps.add(a2);
ps.add(a3);
ps.add(a4);
ps.add(a5);
instances.put(p, ps);
AnswerSet as = AnswerSets.newAnswerSet(predicates, instances);
AnswerSetQueryImpl query = AnswerSetQueryImpl.forPredicate(Predicates.getPredicate("p", 2)).withConstantEquals(0, "x").withFunctionTerm(1, "f", 1);
List<Atom> queryResult = as.query(query);
assertEquals(2, queryResult.size());
}
use of at.ac.tuwien.kr.alpha.api.AnswerSet in project Alpha by alpha-asp.
the class AnswerSetQueryTest method matchEvenIntegers.
@Test
public void matchEvenIntegers() {
AnswerSetBuilder bld = new AnswerSetBuilder();
bld.predicate("p").instance(1).instance(2).instance(3).instance(4).instance(5).instance("bla").symbolicInstance("blubb");
AnswerSet as = bld.build();
java.util.function.Predicate<Term> isInteger = (term) -> {
if (!(term instanceof ConstantTerm<?>)) {
return false;
}
String strValue = ((ConstantTerm<?>) term).getObject().toString();
return strValue.matches("[0-9]+");
};
AnswerSetQueryImpl evenIntegers = AnswerSetQueryImpl.forPredicate(Predicates.getPredicate("p", 1)).withFilter(0, isInteger.and((term) -> Integer.valueOf(((ConstantTerm<?>) term).getObject().toString()) % 2 == 0));
List<Atom> queryResult = as.query(evenIntegers);
assertEquals(2, queryResult.size());
for (Atom atom : queryResult) {
ConstantTerm<?> term = (ConstantTerm<?>) atom.getTerms().get(0);
assertTrue(Integer.valueOf(term.getObject().toString()) % 2 == 0);
}
}
use of at.ac.tuwien.kr.alpha.api.AnswerSet in project Alpha by alpha-asp.
the class AnswerSetQueryTest method matchPredicate.
@Test
public void matchPredicate() {
AnswerSetBuilder bld = new AnswerSetBuilder();
// @formatter:off
bld.predicate("p").symbolicInstance("a").symbolicInstance("b").predicate("q").symbolicInstance("x");
// @formatter:on
AnswerSet as = bld.build();
List<Atom> queryResult = as.query(AnswerSetQueryImpl.forPredicate(Predicates.getPredicate("p", 1)));
assertEquals(2, queryResult.size());
for (Atom a : queryResult) {
assertTrue(a.getPredicate().equals(Predicates.getPredicate("p", 1)));
}
}
Aggregations