Search in sources :

Example 41 with AnswerSet

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!");
        }
    }
}
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 42 with AnswerSet

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) + " }";
}
Also used : List(java.util.List) SortedSet(java.util.SortedSet) AnswerSetFormatter(at.ac.tuwien.kr.alpha.api.util.AnswerSetFormatter) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) AnswerSet(at.ac.tuwien.kr.alpha.api.AnswerSet) Collectors(java.util.stream.Collectors) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate)

Example 43 with AnswerSet

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());
}
Also used : HashMap(java.util.HashMap) TreeSet(java.util.TreeSet) AnswerSet(at.ac.tuwien.kr.alpha.api.AnswerSet) SortedSet(java.util.SortedSet) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate) Test(org.junit.jupiter.api.Test)

Example 44 with AnswerSet

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);
    }
}
Also used : Term(at.ac.tuwien.kr.alpha.api.terms.Term) SortedSet(java.util.SortedSet) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) AnswerSets(at.ac.tuwien.kr.alpha.commons.AnswerSets) HashMap(java.util.HashMap) AnswerSet(at.ac.tuwien.kr.alpha.api.AnswerSet) Atoms(at.ac.tuwien.kr.alpha.commons.atoms.Atoms) TreeSet(java.util.TreeSet) AnswerSetBuilder(at.ac.tuwien.kr.alpha.commons.AnswerSetBuilder) ConstantTerm(at.ac.tuwien.kr.alpha.api.terms.ConstantTerm) Test(org.junit.jupiter.api.Test) Terms(at.ac.tuwien.kr.alpha.commons.terms.Terms) List(java.util.List) Predicates(at.ac.tuwien.kr.alpha.commons.Predicates) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Map(java.util.Map) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate) AnswerSet(at.ac.tuwien.kr.alpha.api.AnswerSet) ConstantTerm(at.ac.tuwien.kr.alpha.api.terms.ConstantTerm) AnswerSetBuilder(at.ac.tuwien.kr.alpha.commons.AnswerSetBuilder) Term(at.ac.tuwien.kr.alpha.api.terms.Term) ConstantTerm(at.ac.tuwien.kr.alpha.api.terms.ConstantTerm) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) Test(org.junit.jupiter.api.Test)

Example 45 with AnswerSet

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)));
    }
}
Also used : AnswerSet(at.ac.tuwien.kr.alpha.api.AnswerSet) AnswerSetBuilder(at.ac.tuwien.kr.alpha.commons.AnswerSetBuilder) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) Test(org.junit.jupiter.api.Test)

Aggregations

AnswerSet (at.ac.tuwien.kr.alpha.api.AnswerSet)80 Test (org.junit.jupiter.api.Test)64 Alpha (at.ac.tuwien.kr.alpha.api.Alpha)25 ASPCore2Program (at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program)24 AnswerSetBuilder (at.ac.tuwien.kr.alpha.commons.AnswerSetBuilder)20 InputConfig (at.ac.tuwien.kr.alpha.api.config.InputConfig)17 Predicate (at.ac.tuwien.kr.alpha.api.programs.Predicate)14 Atom (at.ac.tuwien.kr.alpha.api.programs.atoms.Atom)14 HashSet (java.util.HashSet)13 CompiledProgram (at.ac.tuwien.kr.alpha.core.programs.CompiledProgram)10 ProgramParserImpl (at.ac.tuwien.kr.alpha.core.parser.ProgramParserImpl)9 Solver (at.ac.tuwien.kr.alpha.api.Solver)8 InputProgram (at.ac.tuwien.kr.alpha.core.programs.InputProgram)7 Disabled (org.junit.jupiter.api.Disabled)7 SystemConfig (at.ac.tuwien.kr.alpha.api.config.SystemConfig)4 ProgramParser (at.ac.tuwien.kr.alpha.api.programs.ProgramParser)4 ConstantTerm (at.ac.tuwien.kr.alpha.api.terms.ConstantTerm)4 TestUtils.assertRegressionTestAnswerSet (at.ac.tuwien.kr.alpha.core.test.util.TestUtils.assertRegressionTestAnswerSet)4 TestUtils.buildSolverForRegressionTest (at.ac.tuwien.kr.alpha.core.test.util.TestUtils.buildSolverForRegressionTest)4 HashMap (java.util.HashMap)4