Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring: Decouple API implementation from config structures #336

Draft
wants to merge 2 commits into
base: refactor-tests
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@
*.gpg filter=lfs diff=lfs merge=lfs binary
*.bin filter=lfs diff=lfs merge=lfs binary

alpha-core/benchmarks/** linguist-vendored
alpha-solver/benchmarks/** linguist-vendored
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,4 @@ gradle-app.setting
/.dbeaver
/Scripts
/.vscode/
*.code-workspace
24 changes: 12 additions & 12 deletions alpha-api/src/main/java/at/ac/tuwien/kr/alpha/api/Alpha.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import at.ac.tuwien.kr.alpha.api.common.fixedinterpretations.PredicateInterpretation;
import at.ac.tuwien.kr.alpha.api.config.InputConfig;
import at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program;
import at.ac.tuwien.kr.alpha.api.programs.InputProgram;
import at.ac.tuwien.kr.alpha.api.programs.NormalProgram;
import at.ac.tuwien.kr.alpha.api.programs.Predicate;

Expand All @@ -27,7 +27,7 @@ public interface Alpha {
* @return an {@link ASPCore2Program} representing the parsed ASP code from all sources referenced in the given {@link InputConfig}
* @throws IOException in case one or more program sources (e.g. files) cannot be read, or parsing fails
*/
ASPCore2Program readProgram(InputConfig cfg) throws IOException;
InputProgram readProgram(InputConfig cfg) throws IOException;

/**
* Reads and parses an {@link ASPCore2Program} from a list of {@link String}s representing paths.
Expand All @@ -38,12 +38,12 @@ public interface Alpha {
* @return an {@link ASPCore2Program} representing the parsed ASP code from all given path strings
* @throws IOException in case one or more program sources cannot be read, or parsing fails
*/
ASPCore2Program readProgramFiles(boolean literate, Map<String, PredicateInterpretation> externals, List<String> paths) throws IOException;
InputProgram readProgramFiles(boolean literate, Map<String, PredicateInterpretation> externals, List<String> paths) throws IOException;

/**
* see {@link Alpha#readProgramFiles(boolean, Map, List)}
*/
ASPCore2Program readProgramFiles(boolean literate, Map<String, PredicateInterpretation> externals, Path... paths) throws IOException;
InputProgram readProgramFiles(boolean literate, Map<String, PredicateInterpretation> externals, Path... paths) throws IOException;

/**
* Parses a given String into an {@link ASPCore2Program}, using a map of custom {@link PredicateInterpretation}s to resolve external atoms
Expand All @@ -53,19 +53,19 @@ public interface Alpha {
* @param externals a map of custom {@link PredicateInterpretation}s against which external atoms in the given code are resolved
* @return an {@link ASPCore2Program} representing the parsed ASP code
*/
ASPCore2Program readProgramString(String aspString, Map<String, PredicateInterpretation> externals);
InputProgram readProgramString(String aspString, Map<String, PredicateInterpretation> externals);

/**
* Convenience method to parse ASP strings not containing any user-defined external atoms, see {@link Alpha#readProgramString(String, Map)}.
*/
ASPCore2Program readProgramString(String aspString);
InputProgram readProgramString(String aspString);

/**
* Prepares a {@link DebugSolvingContext} for the given {@link ASPCore2Program} to debug program preprocessing.
*
* @return a {@link DebugSolvingContext} holding debug information for the given program
*/
DebugSolvingContext prepareDebugSolve(final ASPCore2Program program);
DebugSolvingContext prepareDebugSolve(final InputProgram program);

/**
* Prepares a {@link DebugSolvingContext} for the given {@link NormalProgram} to debug program preprocessing.
Expand All @@ -80,7 +80,7 @@ public interface Alpha {
* @param filter a {@link java.util.function.Predicate} against which {@link Predicate}s of answer sets are tested.
* @return a {@link DebugSolvingContext} holding debug information for the given program
*/
DebugSolvingContext prepareDebugSolve(final ASPCore2Program program, java.util.function.Predicate<Predicate> filter);
DebugSolvingContext prepareDebugSolve(final InputProgram program, java.util.function.Predicate<Predicate> filter);

/**
* Prepares a {@link DebugSolvingContext} for the given {@link NormalProgram} to debug program preprocessing.
Expand All @@ -95,15 +95,15 @@ public interface Alpha {
* @param program an input program
* @return a {@link Stream} of {@link AnswerSet}s of the given program
*/
Stream<AnswerSet> solve(ASPCore2Program program);
Stream<AnswerSet> solve(InputProgram program);

/**
* Solves the given {@link ASPCore2Program}.
* @param program an input program
* @param filter a {@link java.util.function.Predicate} against which {@link Predicate}s of answer sets are tested.
* @return a {@link Stream} of {@link AnswerSet}s of the given program
*/
Stream<AnswerSet> solve(ASPCore2Program program, java.util.function.Predicate<Predicate> filter);
Stream<AnswerSet> solve(InputProgram program, java.util.function.Predicate<Predicate> filter);

/**
* Solves the given {@link NormalProgram}.
Expand All @@ -126,7 +126,7 @@ public interface Alpha {
* @param program An {@link ASPCore2Program} to normalize
* @return a {@link NormalProgram} that is a semantic equivalent to the given input program
*/
NormalProgram normalizeProgram(ASPCore2Program program);
NormalProgram normalizeProgram(InputProgram program);

/**
* Constructs a @{link Solver} pre-loaded with the given {@link ASPCore2Program} from which {@link AnswerSet}s can be obtained via {@link Solver#stream()}.
Expand All @@ -135,7 +135,7 @@ public interface Alpha {
* @param filter a {@link java.util.function.Predicate} against which {@link Predicate}s of answer sets are tested.
* @return a {@link Solver} pre-loaded withthe given program
*/
Solver prepareSolverFor(ASPCore2Program program, java.util.function.Predicate<Predicate> filter);
Solver prepareSolverFor(InputProgram program, java.util.function.Predicate<Predicate> filter);

/**
* Constructs a @{link Solver} pre-loaded with the given {@link NormalProgram} from which {@link AnswerSet}s can be obtained via {@link Solver#stream()}.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package at.ac.tuwien.kr.alpha.api;

import at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program;
import at.ac.tuwien.kr.alpha.api.programs.InputProgram;
import at.ac.tuwien.kr.alpha.api.programs.Predicate;
import at.ac.tuwien.kr.alpha.api.terms.Term;

/**
* A comparison operator that can be used in {@link ASPCore2Program}s.
* A comparison operator that can be used in {@link InputProgram}s.
*
* Copyright (c) 2021, the Alpha Team.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package at.ac.tuwien.kr.alpha.api;

import at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program;
import at.ac.tuwien.kr.alpha.api.programs.InputProgram;
import at.ac.tuwien.kr.alpha.api.programs.NormalProgram;
import at.ac.tuwien.kr.alpha.api.programs.analysis.ComponentGraph;
import at.ac.tuwien.kr.alpha.api.programs.analysis.DependencyGraph;
Expand All @@ -13,13 +13,13 @@
public interface DebugSolvingContext {

/**
* The normalized version of the {@link ASPCore2Program} that is being solved.
* See {@link Alpha#normalizeProgram(ASPCore2Program)}.
* The normalized version of the {@link InputProgram} that is being solved.
* See {@link Alpha#normalizeProgram(InputProgram)}.
*/
NormalProgram getNormalizedProgram();

/**
* The fully preprocessed version of the {@link ASPCore2Program} that is being solved.
* The fully preprocessed version of the {@link InputProgram} that is being solved.
* This differs from the value of {@link DebugSolvingContext#getNormalizedProgram()} in the stratified part of the normalized program may
* already be evaluated depending on the respective configuration of {@link Alpha}.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,11 @@ public class SystemConfig {
// initializing from those values in order to have the values accessible in
// contexts where no AlphaConfig instance exists (e.g. argument parsing from
// command line)
public static final String DEFAULT_GROUNDER_NAME = "naive";
public static final String DEFAULT_SOLVER_NAME = "default";
public static final String DEFAULT_NOGOOD_STORE_NAME = "alphaRoaming";
public static final Heuristic DEFAULT_BRANCHING_HEURISTIC = Heuristic.VSIDS;
public static final BinaryNoGoodPropagationEstimationStrategy DEFAULT_MOMS_STRATEGY = BinaryNoGoodPropagationEstimationStrategy.CountBinaryWatches;
public static final long DEFAULT_SEED = System.nanoTime();
public static final boolean DEFAULT_DETERMINISTIC = false;
public static final boolean DEFAULT_PRINT_STATS = false;
public static final boolean DEFAULT_QUIET = false;
public static final boolean DEFAULT_DISABLE_JUSTIFICATION_SEARCH = false;
Expand All @@ -65,10 +63,8 @@ public class SystemConfig {
public static final String DEFAULT_ATOM_SEPARATOR = ", ";
public static final AggregateRewritingConfig DEFAULT_AGGREGATE_REWRITING_CONFIG = new AggregateRewritingConfig();

private String grounderName = DEFAULT_GROUNDER_NAME;
private String solverName = DEFAULT_SOLVER_NAME;
private String nogoodStoreName = DEFAULT_NOGOOD_STORE_NAME;
private boolean deterministic = DEFAULT_DETERMINISTIC;
private long seed = DEFAULT_SEED;
private boolean debugInternalChecks = DEFAULT_DEBUG_INTERNAL_CHECKS;
private Heuristic branchingHeuristic = DEFAULT_BRANCHING_HEURISTIC;
Expand All @@ -86,17 +82,6 @@ public class SystemConfig {
private String atomSeparator = DEFAULT_ATOM_SEPARATOR;
private AggregateRewritingConfig aggregateRewritingConfig = DEFAULT_AGGREGATE_REWRITING_CONFIG;

public String getGrounderName() {
return this.grounderName;
}

/**
* Sets the name of the grounder implementation to use.
*/
public void setGrounderName(String grounderName) {
this.grounderName = grounderName;
}

public String getSolverName() {
return this.solverName;
}
Expand All @@ -119,18 +104,6 @@ public void setNogoodStoreName(String nogoodStoreName) {
this.nogoodStoreName = nogoodStoreName;
}

public boolean isDeterministic() {
return this.deterministic;
}

/**
* If set, 0 will be used as random seed for solver-internal branching heuristics, resulting in answer sets of the same program being found
* in a fixed sequence.
*/
public void setDeterministic(boolean deterministic) {
this.deterministic = deterministic;
}

public long getSeed() {
return this.seed;
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package at.ac.tuwien.kr.alpha.api.programs;

import at.ac.tuwien.kr.alpha.api.rules.Rule;
import at.ac.tuwien.kr.alpha.api.rules.heads.Head;

// TODO javadoc
public interface InputProgram extends Program<Rule<Head>> {

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,40 @@
import at.ac.tuwien.kr.alpha.api.common.fixedinterpretations.PredicateInterpretation;

/**
* A parser for {@link ASPCore2Program}s.
* A parser for {@link InputProgram}s.
*
* Copyright (c) 2021, the Alpha Team.
*/
public interface ProgramParser {

default ASPCore2Program parse(String programString) {
default InputProgram parse(String programString) {
return parse(programString, Collections.emptyMap());
}

default ASPCore2Program parse(InputStream programSource) throws IOException {
default InputProgram parse(InputStream programSource) throws IOException {
return parse(programSource, Collections.emptyMap());
}

default ASPCore2Program parse(Path programPath) throws IOException {
default InputProgram parse(Path programPath) throws IOException {
return parse(programPath, Collections.emptyMap());
}

default ASPCore2Program parse(Path... programSources) throws IOException {
default InputProgram parse(Path... programSources) throws IOException {
return parse(Collections.emptyMap(), programSources);
}

default ASPCore2Program parse(Iterable<Path> programSources) throws IOException {
default InputProgram parse(Iterable<Path> programSources) throws IOException {
return parse(programSources, Collections.emptyMap());
}

ASPCore2Program parse(String programString, Map<String, PredicateInterpretation> externalPredicateDefinitions);
InputProgram parse(String programString, Map<String, PredicateInterpretation> externalPredicateDefinitions);

ASPCore2Program parse(InputStream programSource, Map<String, PredicateInterpretation> externalPredicateDefinitions) throws IOException;
InputProgram parse(InputStream programSource, Map<String, PredicateInterpretation> externalPredicateDefinitions) throws IOException;

ASPCore2Program parse(Path programPath, Map<String, PredicateInterpretation> externalPredicateDefinitions) throws IOException;
InputProgram parse(Path programPath, Map<String, PredicateInterpretation> externalPredicateDefinitions) throws IOException;

ASPCore2Program parse(Map<String, PredicateInterpretation> externalPredicateDefinitions, Path... programSources) throws IOException;
InputProgram parse(Map<String, PredicateInterpretation> externalPredicateDefinitions, Path... programSources) throws IOException;

ASPCore2Program parse(Iterable<Path> programSources, Map<String, PredicateInterpretation> externalPredicateDefinitions) throws IOException;
InputProgram parse(Iterable<Path> programSources, Map<String, PredicateInterpretation> externalPredicateDefinitions) throws IOException;

}
1 change: 1 addition & 0 deletions alpha-cli-app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,6 @@ tasks.create<Jar>("bundledJar") {
}

tasks.test {
dependsOn(":alpha-solver:test")
useJUnitPlatform()
}
8 changes: 4 additions & 4 deletions alpha-cli-app/src/main/java/at/ac/tuwien/kr/alpha/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@
import at.ac.tuwien.kr.alpha.api.config.AlphaConfig;
import at.ac.tuwien.kr.alpha.api.config.InputConfig;
import at.ac.tuwien.kr.alpha.api.config.SystemConfig;
import at.ac.tuwien.kr.alpha.api.impl.AlphaImpl;
import at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program;
import at.ac.tuwien.kr.alpha.api.impl.AlphaFactory;
import at.ac.tuwien.kr.alpha.api.programs.InputProgram;
import at.ac.tuwien.kr.alpha.api.programs.NormalProgram;
import at.ac.tuwien.kr.alpha.api.programs.analysis.ComponentGraph;
import at.ac.tuwien.kr.alpha.api.programs.analysis.DependencyGraph;
Expand Down Expand Up @@ -80,9 +80,9 @@ public static void main(String[] args) {
Main.exitWithMessage(commandLineParser.getUsageMessage(), 1);
}

Alpha alpha = new AlphaImpl(cfg.getSystemConfig());
Alpha alpha = AlphaFactory.newAlpha(cfg.getSystemConfig());

ASPCore2Program program = null;
InputProgram program = null;
try {
program = alpha.readProgram(cfg.getInputConfig());
} catch (FileNotFoundException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,12 @@ public class CommandLineParser {
.desc("Write answer sets to excel files, i.e. xlsx workbooks (one workbook per answer set)").build();

// general system-wide config
private static final Option OPT_GROUNDER = Option.builder("g").longOpt("grounder").hasArg(true).argName("grounder")
.desc("the grounder implementation to use (default: " + SystemConfig.DEFAULT_GROUNDER_NAME + ")").build();
private static final Option OPT_SOLVER = Option.builder("s").longOpt("solver").hasArg(true).argName("solver")
.desc("the solver implementation to use (default: " + SystemConfig.DEFAULT_SOLVER_NAME + ")").build();
private static final Option OPT_NOGOOD_STORE = Option.builder("r").longOpt("store").hasArg(true).argName("store")
.desc("the nogood store to use (default: " + SystemConfig.DEFAULT_NOGOOD_STORE_NAME + ")").build();
private static final Option OPT_SORT = Option.builder("sort").longOpt("sort").hasArg(false)
.desc("sort answer sets (default: " + SystemConfig.DEFAULT_SORT_ANSWER_SETS + ")").build();
private static final Option OPT_DETERMINISTIC = Option.builder("d").longOpt("deterministic").hasArg(false)
.desc("disables randomness (default: " + SystemConfig.DEFAULT_DETERMINISTIC + ")").build();
private static final Option OPT_SEED = Option.builder("e").longOpt("seed").hasArg(true).argName("seed").type(Integer.class)
.desc("set seed (default: System.nanoTime())").build();
private static final Option OPT_DEBUG_INTERNAL_CHECKS = Option.builder("dbgs").longOpt("DebugEnableInternalChecks")
Expand Down Expand Up @@ -167,11 +163,9 @@ public class CommandLineParser {
CommandLineParser.CLI_OPTS.addOption(CommandLineParser.OPT_DEBUG_PREPROCESSING);
CommandLineParser.CLI_OPTS.addOption(CommandLineParser.OPT_WRITE_XSLX);

CommandLineParser.CLI_OPTS.addOption(CommandLineParser.OPT_GROUNDER);
CommandLineParser.CLI_OPTS.addOption(CommandLineParser.OPT_SOLVER);
CommandLineParser.CLI_OPTS.addOption(CommandLineParser.OPT_NOGOOD_STORE);
CommandLineParser.CLI_OPTS.addOption(CommandLineParser.OPT_SORT);
CommandLineParser.CLI_OPTS.addOption(CommandLineParser.OPT_DETERMINISTIC);
CommandLineParser.CLI_OPTS.addOption(CommandLineParser.OPT_SEED);
CommandLineParser.CLI_OPTS.addOption(CommandLineParser.OPT_DEBUG_INTERNAL_CHECKS);
CommandLineParser.CLI_OPTS.addOption(CommandLineParser.OPT_BRANCHING_HEURISTIC);
Expand Down Expand Up @@ -225,11 +219,9 @@ private void initializeGlobalOptionHandlers() {
*/
// help is handled separately, therefore dummy handler
this.globalOptionHandlers.put(CommandLineParser.OPT_HELP.getOpt(), (o, c) -> { });
this.globalOptionHandlers.put(CommandLineParser.OPT_GROUNDER.getOpt(), this::handleGrounder);
this.globalOptionHandlers.put(CommandLineParser.OPT_SOLVER.getOpt(), this::handleSolver);
this.globalOptionHandlers.put(CommandLineParser.OPT_NOGOOD_STORE.getOpt(), this::handleNogoodStore);
this.globalOptionHandlers.put(CommandLineParser.OPT_SORT.getOpt(), this::handleSort);
this.globalOptionHandlers.put(CommandLineParser.OPT_DETERMINISTIC.getOpt(), this::handleDeterministic);
this.globalOptionHandlers.put(CommandLineParser.OPT_SEED.getOpt(), this::handleSeed);
this.globalOptionHandlers.put(CommandLineParser.OPT_DEBUG_INTERNAL_CHECKS.getOpt(), this::handleInternalChecks);
this.globalOptionHandlers.put(CommandLineParser.OPT_BRANCHING_HEURISTIC.getOpt(), this::handleBranchingHeuristic);
Expand Down Expand Up @@ -335,10 +327,6 @@ private void handleInput(Option opt, InputConfig cfg) {
cfg.getFiles().add(optVal);
}

private void handleGrounder(Option opt, SystemConfig cfg) {
cfg.setGrounderName(opt.getValue(SystemConfig.DEFAULT_GROUNDER_NAME));
}

private void handleSolver(Option opt, SystemConfig cfg) {
cfg.setSolverName(opt.getValue(SystemConfig.DEFAULT_SOLVER_NAME));
}
Expand All @@ -362,12 +350,10 @@ private void handleSort(Option opt, SystemConfig cfg) {
}

private void handleDeterministic(Option opt, SystemConfig cfg) {
cfg.setDeterministic(true);
cfg.setSeed(0);
}

private void handleSeed(Option opt, SystemConfig cfg) {
cfg.setDeterministic(false);
String optVal = opt.getValue();
long seed;
if (optVal != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ public class MainTest {

private static Stream<Arguments> provideCommandLineArguments() {
return Stream.of(
Arguments.of((Object) new String[]{"-DebugEnableInternalChecks", "-g", "naive", "-s", "default", "-e", "1119654162577372", "-n", "20", "-str", INPUT}),
Arguments.of((Object) new String[]{"-DebugEnableInternalChecks", "-g", "naive", "-s", "default", "-n", "0", "-str", INPUT}),
Arguments.of((Object) new String[]{"-DebugEnableInternalChecks", "-g", "naive", "-s", "default", "-n", "1", "-str", INPUT}),
Arguments.of((Object) new String[]{"-g", "naive", "-s", "default", "-r", "naive", "-e", "1119654162577372", "--numAS", "1", "-str", INPUT}));
Arguments.of((Object) new String[]{"-DebugEnableInternalChecks", "-s", "default", "-e", "1119654162577372", "-n", "20", "-str", INPUT}),
Arguments.of((Object) new String[]{"-DebugEnableInternalChecks", "-s", "default", "-n", "0", "-str", INPUT}),
Arguments.of((Object) new String[]{"-DebugEnableInternalChecks", "-s", "default", "-n", "1", "-str", INPUT}),
Arguments.of((Object) new String[]{"-s", "default", "-r", "naive", "-e", "1119654162577372", "--numAS", "1", "-str", INPUT}));
}

/**
Expand Down
Loading