Search in sources :

Example 1 with DebugSolvingContext

use of at.ac.tuwien.kr.alpha.api.DebugSolvingContext in project Alpha by alpha-asp.

the class DependencyGraphWriterTest method smokeTest.

@Test
public void smokeTest() {
    // Note: rather than testing correct implementation of dot file format,
    // (which would be a lot of work), just test correct dot code generation
    // for one dependency graph that has all possible "special" node constellations,
    // i.e. positive and negative dependencies, cycle through negation, constraints.
    String asp = "p(X) :- q(X), r(X)." + LS + "s(X) :- p(X), q(X), not r(X)." + LS + "t(X) :- p(X), not u(X)." + LS + "u(X) :- p(X), not t(X)." + LS + ":- p(X), not q(X), not r(X).";
    String expectedGraph = "digraph dependencyGraph" + LS + "{" + LS + "splines=false;" + LS + "ranksep=4.0;" + LS + "n0 [label = \"r/1\"]" + LS + "n1 [label = \"q/1\"]" + LS + "n2 [label = \"t/1\"]" + LS + "n3 [label = \"s/1\"]" + LS + "n4 [label = \"u/1\"]" + LS + "n5 [label = \"[constr_1]/0\"]" + LS + "n6 [label = \"p/1\"]" + LS + "n0 -> n6 [xlabel=\"+\" labeldistance=0.1]" + LS + "n0 -> n3 [xlabel=\"-\" labeldistance=0.1]" + LS + "n0 -> n5 [xlabel=\"-\" labeldistance=0.1]" + LS + "n1 -> n6 [xlabel=\"+\" labeldistance=0.1]" + LS + "n1 -> n3 [xlabel=\"+\" labeldistance=0.1]" + LS + "n1 -> n5 [xlabel=\"-\" labeldistance=0.1]" + LS + "n2 -> n4 [xlabel=\"-\" labeldistance=0.1]" + LS + "n4 -> n2 [xlabel=\"-\" labeldistance=0.1]" + LS + "n5 -> n5 [xlabel=\"-\" labeldistance=0.1]" + LS + "n6 -> n3 [xlabel=\"+\" labeldistance=0.1]" + LS + "n6 -> n2 [xlabel=\"+\" labeldistance=0.1]" + LS + "n6 -> n4 [xlabel=\"+\" labeldistance=0.1]" + LS + "n6 -> n5 [xlabel=\"+\" labeldistance=0.1]" + LS + "}" + LS;
    Alpha alpha = new AlphaImpl();
    DebugSolvingContext dbgResult = alpha.prepareDebugSolve(alpha.readProgramString(asp));
    DependencyGraph depgraph = dbgResult.getDependencyGraph();
    DependencyGraphWriter writer = new DependencyGraphWriter();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    writer.writeAsDot(depgraph, out);
    String actualGraph = out.toString();
    assertEquals(expectedGraph, actualGraph);
}
Also used : AlphaImpl(at.ac.tuwien.kr.alpha.api.impl.AlphaImpl) Alpha(at.ac.tuwien.kr.alpha.api.Alpha) DependencyGraph(at.ac.tuwien.kr.alpha.api.programs.analysis.DependencyGraph) DebugSolvingContext(at.ac.tuwien.kr.alpha.api.DebugSolvingContext) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.jupiter.api.Test)

Example 2 with DebugSolvingContext

use of at.ac.tuwien.kr.alpha.api.DebugSolvingContext 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);
}
Also used : ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) Solver(at.ac.tuwien.kr.alpha.api.Solver) StatisticsReportingSolver(at.ac.tuwien.kr.alpha.api.StatisticsReportingSolver) AlphaImpl(at.ac.tuwien.kr.alpha.api.impl.AlphaImpl) AlphaConfig(at.ac.tuwien.kr.alpha.api.config.AlphaConfig) Alpha(at.ac.tuwien.kr.alpha.api.Alpha) FileNotFoundException(java.io.FileNotFoundException) InputConfig(at.ac.tuwien.kr.alpha.api.config.InputConfig) CommandLineParser(at.ac.tuwien.kr.alpha.app.config.CommandLineParser) ParseException(org.apache.commons.cli.ParseException) IOException(java.io.IOException) DebugSolvingContext(at.ac.tuwien.kr.alpha.api.DebugSolvingContext)

Example 3 with DebugSolvingContext

use of at.ac.tuwien.kr.alpha.api.DebugSolvingContext in project Alpha by alpha-asp.

the class ComponentGraphWriterTest method smokeTest.

@Test
public void smokeTest() {
    // Note: rather than testing correct implementation of dot file format,
    // (which would be a lot of work), just test correct dot code generation
    // for one component graph that has all possible "special" node constellations,
    // i.e. positive and negative dependencies, cycle through negation, constraints.
    String asp = "p(X) :- q(X), r(X)." + LS + "s(X) :- p(X), q(X), not r(X)." + LS + "t(X) :- p(X), not u(X)." + LS + "u(X) :- p(X), not t(X)." + LS + ":- p(X), not q(X), not r(X).";
    String expectedGraph = "digraph componentGraph" + LS + "{" + LS + "splines=false;" + LS + "ranksep=4.0;" + LS + "label = <" + LS + "	<table border = '1' cellborder = '0'>" + LS + "		<tr><td>Component Id</td><td>0</td><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td></tr>" + LS + "		<tr><td>Predicates</td><td>q/1<br/></td><td>r/1<br/></td><td>p/1<br/></td><td>[constr_1]/0<br/></td><td>t/1<br/>u/1<br/></td><td>s/1<br/></td></tr>" + LS + "	</table>" + LS + ">" + LS + "" + LS + "n0 [label = C0]" + LS + "n1 [label = C1]" + LS + "n2 [label = C2]" + LS + "n0 -> n2 [xlabel=\"+\" labeldistance=0.1]" + LS + "n1 -> n2 [xlabel=\"+\" labeldistance=0.1]" + LS + "n3 [label = C3]" + LS + "n0 -> n3 [xlabel=\"-\" labeldistance=0.1]" + LS + "n1 -> n3 [xlabel=\"-\" labeldistance=0.1]" + LS + "n2 -> n3 [xlabel=\"+\" labeldistance=0.1]" + LS + "n4 [label = C4]" + LS + "n2 -> n4 [xlabel=\"+\" labeldistance=0.1]" + LS + "n5 [label = C5]" + LS + "n0 -> n5 [xlabel=\"+\" labeldistance=0.1]" + LS + "n1 -> n5 [xlabel=\"-\" labeldistance=0.1]" + LS + "n2 -> n5 [xlabel=\"+\" labeldistance=0.1]" + LS + "}" + LS;
    Alpha alpha = new AlphaImpl();
    DebugSolvingContext dbgResult = alpha.prepareDebugSolve(alpha.readProgramString(asp));
    ComponentGraph compgraph = dbgResult.getComponentGraph();
    ComponentGraphWriter writer = new ComponentGraphWriter();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    writer.writeAsDot(compgraph, out);
    String actualGraph = out.toString();
    System.out.println(actualGraph);
    assertEquals(expectedGraph, actualGraph);
}
Also used : ComponentGraph(at.ac.tuwien.kr.alpha.api.programs.analysis.ComponentGraph) AlphaImpl(at.ac.tuwien.kr.alpha.api.impl.AlphaImpl) Alpha(at.ac.tuwien.kr.alpha.api.Alpha) DebugSolvingContext(at.ac.tuwien.kr.alpha.api.DebugSolvingContext) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.jupiter.api.Test)

Example 4 with DebugSolvingContext

use of at.ac.tuwien.kr.alpha.api.DebugSolvingContext in project Alpha by alpha-asp.

the class AlphaImpl method prepareDebugSolve.

@Override
public DebugSolvingContext prepareDebugSolve(final NormalProgram program, java.util.function.Predicate<Predicate> filter) {
    final DependencyGraph depGraph;
    final ComponentGraph compGraph;
    final AnalyzedProgram analyzed = AnalyzedProgram.analyzeNormalProgram(program);
    final NormalProgram preprocessed;
    if (this.config.isEvaluateStratifiedPart()) {
        preprocessed = new StratifiedEvaluation().apply(analyzed).toNormalProgram();
    } else {
        preprocessed = program;
    }
    depGraph = analyzed.getDependencyGraph();
    compGraph = analyzed.getComponentGraph();
    final Solver solver = prepareSolverFor(analyzed, filter);
    return new DebugSolvingContext() {

        @Override
        public Solver getSolver() {
            return solver;
        }

        @Override
        public NormalProgram getPreprocessedProgram() {
            return preprocessed;
        }

        @Override
        public NormalProgram getNormalizedProgram() {
            return program;
        }

        @Override
        public DependencyGraph getDependencyGraph() {
            return depGraph;
        }

        @Override
        public ComponentGraph getComponentGraph() {
            return compGraph;
        }
    };
}
Also used : ComponentGraph(at.ac.tuwien.kr.alpha.api.programs.analysis.ComponentGraph) Solver(at.ac.tuwien.kr.alpha.api.Solver) AnalyzedProgram(at.ac.tuwien.kr.alpha.core.programs.AnalyzedProgram) NormalProgram(at.ac.tuwien.kr.alpha.api.programs.NormalProgram) DependencyGraph(at.ac.tuwien.kr.alpha.api.programs.analysis.DependencyGraph) DebugSolvingContext(at.ac.tuwien.kr.alpha.api.DebugSolvingContext) StratifiedEvaluation(at.ac.tuwien.kr.alpha.core.programs.transformation.StratifiedEvaluation)

Aggregations

DebugSolvingContext (at.ac.tuwien.kr.alpha.api.DebugSolvingContext)4 Alpha (at.ac.tuwien.kr.alpha.api.Alpha)3 AlphaImpl (at.ac.tuwien.kr.alpha.api.impl.AlphaImpl)3 Solver (at.ac.tuwien.kr.alpha.api.Solver)2 ComponentGraph (at.ac.tuwien.kr.alpha.api.programs.analysis.ComponentGraph)2 DependencyGraph (at.ac.tuwien.kr.alpha.api.programs.analysis.DependencyGraph)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 Test (org.junit.jupiter.api.Test)2 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 ASPCore2Program (at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program)1 NormalProgram (at.ac.tuwien.kr.alpha.api.programs.NormalProgram)1 CommandLineParser (at.ac.tuwien.kr.alpha.app.config.CommandLineParser)1 AnalyzedProgram (at.ac.tuwien.kr.alpha.core.programs.AnalyzedProgram)1 StratifiedEvaluation (at.ac.tuwien.kr.alpha.core.programs.transformation.StratifiedEvaluation)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 ParseException (org.apache.commons.cli.ParseException)1