Skip to content

Commit

Permalink
WIP: merge action implementation back into state from master
Browse files Browse the repository at this point in the history
  • Loading branch information
madmike200590 committed May 26, 2024
1 parent fac7e67 commit 8191e89
Show file tree
Hide file tree
Showing 24 changed files with 320 additions and 175 deletions.
26 changes: 13 additions & 13 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 @@ -28,18 +28,18 @@ public interface Alpha {
*
* @param cfg and {@link InputConfig} specifying program sources (strings, files) as well as config metadata (e.g. literate program,
* external atoms, etc)
* @return an {@link ASPCore2Program} representing the parsed ASP code from all sources referenced in the given {@link InputConfig}
* @return an {@link InputProgram} 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
*/
InputProgram readProgram(InputConfig cfg) throws IOException;

/**
* Reads and parses an {@link ASPCore2Program} from a list of {@link String}s representing paths.
* Reads and parses an {@link InputProgram} from a list of {@link String}s representing paths.
*
* @param literate flag indicating whether ASP code should be treated as "literate".
* @param externals Custom {@link PredicateInterpretation}s for user-defined external atoms
* @param paths a list of {@link String}s representing paths containing all sources from which ASP code should be read
* @return an {@link ASPCore2Program} representing the parsed ASP code from all given path strings
* @return an {@link InputProgram} 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
*/
InputProgram readProgramFiles(boolean literate, Map<String, PredicateInterpretation> externals, List<String> paths) throws IOException;
Expand All @@ -50,12 +50,12 @@ public interface Alpha {
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
* Parses a given String into an {@link InputProgram}, using a map of custom {@link PredicateInterpretation}s to resolve external atoms
* in ASP code.
*
* @param aspString a string representing a valid ASP-Core2 program
* @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
* @return an {@link InputProgram} representing the parsed ASP code
*/
InputProgram readProgramString(String aspString, Map<String, PredicateInterpretation> externals);

Expand All @@ -75,7 +75,7 @@ public interface Alpha {
InputProgram readProgramStream(InputStream is, Map<String, PredicateInterpretation> externals) throws IOException;

/**
* Prepares a {@link DebugSolvingContext} for the given {@link ASPCore2Program} to debug program preprocessing.
* Prepares a {@link DebugSolvingContext} for the given {@link InputProgram} to debug program preprocessing.
*
* @return a {@link DebugSolvingContext} holding debug information for the given program
*/
Expand All @@ -89,7 +89,7 @@ public interface Alpha {
DebugSolvingContext prepareDebugSolve(final NormalProgram program);

/**
* Prepares a {@link DebugSolvingContext} for the given {@link ASPCore2Program} to debug program preprocessing.
* Prepares a {@link DebugSolvingContext} for the given {@link InputProgram} to debug program preprocessing.
*
* @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
Expand All @@ -105,15 +105,15 @@ public interface Alpha {
DebugSolvingContext prepareDebugSolve(final NormalProgram program, java.util.function.Predicate<Predicate> filter);

/**
* Solves the given {@link ASPCore2Program}.
* Solves the given {@link InputProgram}.
*
* @param program an input program
* @return a {@link Stream} of {@link AnswerSet}s of the given program
*/
Stream<AnswerSet> solve(InputProgram program);

/**
* Solves the given {@link ASPCore2Program}.
* Solves the given {@link InputProgram}.
*
* @param program an input program
* @param filter a {@link java.util.function.Predicate} against which {@link Predicate}s of answer sets are tested.
Expand Down Expand Up @@ -143,13 +143,13 @@ public interface Alpha {
* code.
* See {@link NormalProgram},
*
* @param program An {@link ASPCore2Program} to normalize
* @param program An {@link InputProgram} to normalize
* @return a {@link NormalProgram} that is a semantic equivalent to the given input 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
* Constructs a @{link Solver} pre-loaded with the given {@link InputProgram} from which {@link AnswerSet}s can be obtained via
* {@link Solver#stream()}.
*
* @param program the program to solve
Expand All @@ -174,11 +174,11 @@ public interface Alpha {
* @param program an ASP program to reify
* @return a set of {@link BasicAtom}s encoding the given program
*/
Set<BasicAtom> reify(ASPCore2Program program);
Set<BasicAtom> reify(InputProgram program);

/**
* Runs all test cases of the given program.
*/
TestResult test(ASPCore2Program program);
TestResult test(InputProgram program);

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public interface AnswerSet extends Comparable<AnswerSet> {
boolean isEmpty();

/**
* List {@link Atom}s in this answer set satisfying the given {@link AnswerSetQuery}.
* List {@link Atom}s in this answer set satisfying the given {@link AtomQuery}.
*/
List<Atom> query(AtomQuery query);

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
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;
import at.ac.tuwien.kr.alpha.api.programs.rules.Rule;
import at.ac.tuwien.kr.alpha.api.programs.rules.heads.Head;
import at.ac.tuwien.kr.alpha.api.programs.tests.TestCase;

import java.util.List;

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

/**
* The test cases associated with this program.
*/
List<TestCase> getTestCases();

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

import at.ac.tuwien.kr.alpha.api.grounder.Substitution;
import at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom;
import at.ac.tuwien.kr.alpha.api.programs.rules.heads.InstantiableHead;

public interface RuleInstantiator {

BasicAtom instantiate(InstantiableHead ruleHead, Substitution substitution);

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

import at.ac.tuwien.kr.alpha.api.programs.terms.Term;
import at.ac.tuwien.kr.alpha.api.programs.terms.VariableTerm;

import java.util.List;

public interface ActionHead extends NormalHead {

String getActionName();

List<Term> getActionInputTerms();

VariableTerm getActionOutputTerm();

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

import at.ac.tuwien.kr.alpha.api.grounder.Substitution;
import at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom;
import at.ac.tuwien.kr.alpha.api.programs.rules.RuleInstantiator;

public interface InstantiableHead extends Head {

BasicAtom instantiate(RuleInstantiator instantiator, Substitution substitution);

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

/**
* A term representing the result of an evolog action (i.e. result of action function application).
* Action result terms are function terms with symbol "success" or "error" depending on whether the corresponding action was sucessful.
* There is always one argument which is either some term representing the actual function result or an error message, respectively.
*/
public interface ActionResultTerm<T extends Term> extends FunctionTerm {

public static final String SUCCESS_SYMBOL = "success";
public static final String ERROR_SYMBOL = "error";

/**
* True if the action that generated this result was successful (i.e. executed normally).
*/
boolean isSuccess();

/**
* True if the action that generated this result failed (i.e. threw an error in execution).
*/
boolean isError();

/**
* Gets the actual value wrapped in this result.
* Either a term representing the action return value or a string term representing an error
* message.s
*/
T getValue();

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

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

public interface Assertion {

Expand All @@ -23,7 +23,7 @@ public String toString() {

Mode getMode();

ASPCore2Program getVerifier();
InputProgram getVerifier();


}
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
*/
package at.ac.tuwien.kr.alpha.commons.programs;

import at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program;
import at.ac.tuwien.kr.alpha.api.programs.InlineDirectives;
import at.ac.tuwien.kr.alpha.api.programs.InputProgram;
import at.ac.tuwien.kr.alpha.api.programs.atoms.Atom;
import at.ac.tuwien.kr.alpha.api.programs.rules.Rule;
import at.ac.tuwien.kr.alpha.api.programs.rules.heads.Head;
Expand All @@ -43,7 +43,8 @@
* <p>
* Copyright (c) 2017-2019, the Alpha Team.
*/
class ASPCore2ProgramImpl extends AbstractProgram<Rule<Head>> implements ASPCore2Program{
// TODO rename this to InputProgramImpl or some such
class ASPCore2ProgramImpl extends AbstractProgram<Rule<Head>> implements InputProgram {

static final ASPCore2ProgramImpl EMPTY = new ASPCore2ProgramImpl(Collections.emptyList(), Collections.emptyList(), new InlineDirectivesImpl(), Collections.emptyList());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program;
import at.ac.tuwien.kr.alpha.api.programs.InlineDirectives;
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.atoms.Atom;
import at.ac.tuwien.kr.alpha.api.programs.rules.NormalRule;
Expand All @@ -20,31 +21,33 @@ private Programs() {
throw new AssertionError("Cannot instantiate utility class!");
}

public static ASPCore2Program emptyProgram() {
public static InputProgram emptyProgram() {
return ASPCore2ProgramImpl.EMPTY;
}

public static ASPCore2Program newASPCore2Program(List<Rule<Head>> rules, List<Atom> facts, InlineDirectives inlineDirectives, List<TestCase> testCases) {
// TODO rename method
public static InputProgram newASPCore2Program(List<Rule<Head>> rules, List<Atom> facts, InlineDirectives inlineDirectives, List<TestCase> testCases) {
return new ASPCore2ProgramImpl(rules, facts, inlineDirectives, testCases);
}

public static ASPCore2Program newASPCore2Program(List<Rule<Head>> rules, List<Atom> facts, InlineDirectives inlineDirectives) {
// TODO rename method
public static InputProgram newASPCore2Program(List<Rule<Head>> rules, List<Atom> facts, InlineDirectives inlineDirectives) {
return new ASPCore2ProgramImpl(rules, facts, inlineDirectives, Collections.emptyList());
}

public static ASPCore2ProgramBuilder builder() {
return new ASPCore2ProgramBuilder();
}

public static ASPCore2ProgramBuilder builder(ASPCore2Program program) {
public static ASPCore2ProgramBuilder builder(InputProgram program) {
return new ASPCore2ProgramBuilder(program);
}

public static NormalProgram newNormalProgram(List<NormalRule> rules, List<Atom> facts, InlineDirectives inlineDirectives) {
return new NormalProgramImpl(rules, facts, inlineDirectives);
}

public static NormalProgram toNormalProgram(ASPCore2Program inputProgram) {
public static NormalProgram toNormalProgram(InputProgram inputProgram) {
List<NormalRule> normalRules = new ArrayList<>();
for (Rule<Head> r : inputProgram.getRules()) {
normalRules.add(Rules.toNormalRule(r));
Expand All @@ -57,8 +60,9 @@ public static InlineDirectives newInlineDirectives() {
}

/**
* Builder for more complex program construction scenarios, ensuring that an {@link AspCore2ProgramImpl} is immutable
* Builder for more complex program construction scenarios, ensuring that an {@link ASPCore2ProgramImpl} is immutable
*/
// TODO maybe rename
public static class ASPCore2ProgramBuilder {

private List<Rule<Head>> rules = new ArrayList<>();
Expand All @@ -67,7 +71,7 @@ public static class ASPCore2ProgramBuilder {

private List<TestCase> testCases = new ArrayList<>();

public ASPCore2ProgramBuilder(ASPCore2Program prog) {
public ASPCore2ProgramBuilder(InputProgram prog) {
this.addRules(prog.getRules());
this.addFacts(prog.getFacts());
this.addInlineDirectives(prog.getInlineDirectives());
Expand Down Expand Up @@ -113,11 +117,11 @@ public ASPCore2ProgramBuilder addTestCases(List<TestCase> testCases) {
return this;
}

public ASPCore2ProgramBuilder accumulate(ASPCore2Program prog) {
public ASPCore2ProgramBuilder accumulate(InputProgram prog) {
return this.addRules(prog.getRules()).addFacts(prog.getFacts()).addInlineDirectives(prog.getInlineDirectives()).addTestCases(prog.getTestCases());
}

public ASPCore2Program build() {
public InputProgram build() {
return Programs.newASPCore2Program(this.rules, this.facts, this.inlineDirectives, this.testCases);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package at.ac.tuwien.kr.alpha.commons.programs.rules;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom;
Expand All @@ -23,10 +24,7 @@ public static Rule<Head> newRule(Head head, List<Literal> body) {
}

public static Rule<Head> newRule(Head head, Literal... body) {
List<Literal> bodyLst = new ArrayList<>();
for (Literal lit : body) {
bodyLst.add(lit);
}
List<Literal> bodyLst = new ArrayList<>(Arrays.asList(body));
return new BasicRule(head, bodyLst);
}

Expand All @@ -35,10 +33,7 @@ public static NormalRule newNormalRule(NormalHead head, List<Literal> body) {
}

public static NormalRule newNormalRule(NormalHead head, Literal... body) {
List<Literal> bodyLst = new ArrayList<>();
for (Literal lit : body) {
bodyLst.add(lit);
}
List<Literal> bodyLst = new ArrayList<>(Arrays.asList(body));
return new NormalRuleImpl(head, bodyLst);
}

Expand Down
Loading

0 comments on commit 8191e89

Please sign in to comment.