use of at.ac.tuwien.kr.alpha.api.programs.literals.Literal in project Alpha by alpha-asp.
the class AnalyzeUnjustifiedTest method justifySimpleRules.
@Test
public void justifySimpleRules() {
String program = "p(X) :- q(X)." + "q(X) :- p(X)." + "q(5) :- r." + "r :- not nr." + "nr :- not r." + ":- not p(5).";
CompiledProgram internalProgram = parseAndPreprocess.apply(program);
AtomStore atomStore = new AtomStoreImpl();
NaiveGrounder grounder = new NaiveGrounder(internalProgram, atomStore, true);
grounder.getNoGoods(null);
TrailAssignment assignment = new TrailAssignment(atomStore);
int rId = atomStore.get(Atoms.newBasicAtom(Predicates.getPredicate("r", 0)));
int nrId = atomStore.get(Atoms.newBasicAtom(Predicates.getPredicate("nr", 0)));
assignment.growForMaxAtomId();
assignment.assign(rId, ThriceTruth.FALSE);
assignment.assign(nrId, ThriceTruth.TRUE);
BasicAtom p5 = Atoms.newBasicAtom(Predicates.getPredicate("p", 1), Collections.singletonList(Terms.newConstant(5)));
assignment.assign(atomStore.get(p5), ThriceTruth.MBT);
Set<Literal> reasons = grounder.justifyAtom(atomStore.get(p5), assignment);
assertFalse(reasons.isEmpty());
}
use of at.ac.tuwien.kr.alpha.api.programs.literals.Literal in project Alpha by alpha-asp.
the class AnalyzeUnjustifiedTest method justifyMultipleReasons.
@Test
public void justifyMultipleReasons() {
String program = "n(a). n(b). n(c). n(d). n(e)." + "s(a,b). s(b,c). s(c,d). s(d,e)." + "{ q(X) } :- n(X)." + "p(X) :- q(X)." + "p(X) :- p(Y), s(Y,X)." + ":- not p(c).";
CompiledProgram internalProgram = parseAndPreprocess.apply(program);
AtomStore atomStore = new AtomStoreImpl();
NaiveGrounder grounder = new NaiveGrounder(internalProgram, atomStore, true);
grounder.getNoGoods(null);
TrailAssignment assignment = new TrailAssignment(atomStore);
Atom qa = parser.parse("q(a).").getFacts().get(0);
Atom qb = parser.parse("q(b).").getFacts().get(0);
Atom qc = parser.parse("q(c).").getFacts().get(0);
Atom qd = parser.parse("q(d).").getFacts().get(0);
Atom qe = parser.parse("q(e).").getFacts().get(0);
int qaId = atomStore.get(qa);
int qbId = atomStore.get(qb);
int qcId = atomStore.get(qc);
int qdId = atomStore.get(qd);
int qeId = atomStore.get(qe);
assignment.growForMaxAtomId();
assignment.assign(qaId, ThriceTruth.FALSE);
assignment.assign(qbId, ThriceTruth.FALSE);
assignment.assign(qcId, ThriceTruth.FALSE);
assignment.assign(qdId, ThriceTruth.FALSE);
assignment.assign(qeId, ThriceTruth.FALSE);
Predicate nq = Predicates.getPredicate("_nq", 2, true);
Atom nqa = Atoms.newBasicAtom(nq, Arrays.asList(Terms.newConstant("1"), Terms.newSymbolicConstant("a")));
Atom nqb = Atoms.newBasicAtom(nq, Arrays.asList(Terms.newConstant("1"), Terms.newSymbolicConstant("b")));
Atom nqc = Atoms.newBasicAtom(nq, Arrays.asList(Terms.newConstant("1"), Terms.newSymbolicConstant("c")));
Atom nqd = Atoms.newBasicAtom(nq, Arrays.asList(Terms.newConstant("1"), Terms.newSymbolicConstant("d")));
Atom nqe = Atoms.newBasicAtom(nq, Arrays.asList(Terms.newConstant("1"), Terms.newSymbolicConstant("e")));
int nqaId = atomStore.get(nqa);
int nqbId = atomStore.get(nqb);
int nqcId = atomStore.get(nqc);
int nqdId = atomStore.get(nqd);
int nqeId = atomStore.get(nqe);
assignment.growForMaxAtomId();
assignment.assign(nqaId, ThriceTruth.TRUE);
assignment.assign(nqbId, ThriceTruth.TRUE);
assignment.assign(nqcId, ThriceTruth.TRUE);
assignment.assign(nqdId, ThriceTruth.TRUE);
assignment.assign(nqeId, ThriceTruth.TRUE);
Atom pc = parser.parse("p(c).").getFacts().get(0);
Set<Literal> reasons = grounder.justifyAtom(atomStore.get(pc), assignment);
assertFalse(reasons.isEmpty());
}
use of at.ac.tuwien.kr.alpha.api.programs.literals.Literal in project Alpha by alpha-asp.
the class LiteralInstantiationStrategyTest method workingMemoryBasedInstantiationAcceptLiteral.
@Test
public void workingMemoryBasedInstantiationAcceptLiteral() {
Predicate p = Predicates.getPredicate("p", 1);
WorkingMemory workingMemory = new WorkingMemory();
workingMemory.initialize(p);
workingMemory.addInstance(Atoms.newBasicAtom(p, Terms.newSymbolicConstant("a")), true);
LiteralInstantiationStrategy strategy = new WorkingMemoryBasedInstantiationStrategy(workingMemory);
Literal positiveAcceptedLiteral = Literals.fromAtom(Atoms.newBasicAtom(p, Terms.newSymbolicConstant("a")), true);
assertEquals(AssignmentStatus.TRUE, strategy.getTruthForGroundLiteral(positiveAcceptedLiteral));
Literal negativeAcceptedLiteral = Literals.fromAtom(Atoms.newBasicAtom(p, Terms.newSymbolicConstant("b")), false);
assertEquals(AssignmentStatus.TRUE, strategy.getTruthForGroundLiteral(negativeAcceptedLiteral));
}
use of at.ac.tuwien.kr.alpha.api.programs.literals.Literal in project Alpha by alpha-asp.
the class LiteralInstantiationStrategyTest method workingMemoryBasedInstantiationRejectLiteral.
@Test
public void workingMemoryBasedInstantiationRejectLiteral() {
Predicate p = Predicates.getPredicate("p", 1);
WorkingMemory workingMemory = new WorkingMemory();
workingMemory.initialize(p);
workingMemory.addInstance(Atoms.newBasicAtom(p, Terms.newSymbolicConstant("a")), true);
LiteralInstantiationStrategy strategy = new WorkingMemoryBasedInstantiationStrategy(workingMemory);
Literal positiveRejectedLiteral = Literals.fromAtom(Atoms.newBasicAtom(p, Terms.newSymbolicConstant("b")), true);
assertEquals(AssignmentStatus.FALSE, strategy.getTruthForGroundLiteral(positiveRejectedLiteral));
Literal negativeRejectedLiteral = Literals.fromAtom(Atoms.newBasicAtom(p, Terms.newSymbolicConstant("a")), false);
assertEquals(AssignmentStatus.FALSE, strategy.getTruthForGroundLiteral(negativeRejectedLiteral));
}
use of at.ac.tuwien.kr.alpha.api.programs.literals.Literal in project Alpha by alpha-asp.
the class ParserTest method parseChoiceRuleBounded.
@Test
public void parseChoiceRuleBounded() {
ASPCore2Program parsedProgram = parser.parse("dom(1). dom(2). 1 < { a: p(v,w), not r; b } <= 13 :- dom(X). foo.");
ChoiceHead choiceHead = (ChoiceHead) parsedProgram.getRules().get(0).getHead();
assertEquals(2, choiceHead.getChoiceElements().size());
assertTrue(choiceHead.getChoiceElements().get(0).getChoiceAtom().toString().equals("a"));
assertTrue(choiceHead.getChoiceElements().get(1).getChoiceAtom().toString().equals("b"));
List<Literal> conditionalLiterals = choiceHead.getChoiceElements().get(0).getConditionLiterals();
assertEquals(2, conditionalLiterals.size());
assertFalse(conditionalLiterals.get(0).isNegated());
assertTrue(conditionalLiterals.get(1).isNegated());
assertEquals(Terms.newConstant(1), choiceHead.getLowerBound());
assertEquals(ComparisonOperators.LT, choiceHead.getLowerOperator());
assertEquals(Terms.newConstant(13), choiceHead.getUpperBound());
assertEquals(ComparisonOperators.LE, choiceHead.getUpperOperator());
}
Aggregations