Search in sources :

Example 46 with Literal

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());
}
Also used : NaiveGrounder(at.ac.tuwien.kr.alpha.core.grounder.NaiveGrounder) AtomStore(at.ac.tuwien.kr.alpha.core.common.AtomStore) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) AtomStoreImpl(at.ac.tuwien.kr.alpha.core.common.AtomStoreImpl) CompiledProgram(at.ac.tuwien.kr.alpha.core.programs.CompiledProgram) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) TrailAssignment(at.ac.tuwien.kr.alpha.core.solver.TrailAssignment) Test(org.junit.jupiter.api.Test)

Example 47 with Literal

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());
}
Also used : NaiveGrounder(at.ac.tuwien.kr.alpha.core.grounder.NaiveGrounder) AtomStore(at.ac.tuwien.kr.alpha.core.common.AtomStore) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) AtomStoreImpl(at.ac.tuwien.kr.alpha.core.common.AtomStoreImpl) CompiledProgram(at.ac.tuwien.kr.alpha.core.programs.CompiledProgram) TrailAssignment(at.ac.tuwien.kr.alpha.core.solver.TrailAssignment) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) 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 48 with Literal

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));
}
Also used : WorkingMemory(at.ac.tuwien.kr.alpha.core.grounder.WorkingMemory) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate) Test(org.junit.jupiter.api.Test)

Example 49 with Literal

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));
}
Also used : WorkingMemory(at.ac.tuwien.kr.alpha.core.grounder.WorkingMemory) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate) Test(org.junit.jupiter.api.Test)

Example 50 with Literal

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());
}
Also used : ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) ChoiceHead(at.ac.tuwien.kr.alpha.api.rules.heads.ChoiceHead) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) AggregateLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.AggregateLiteral) Test(org.junit.jupiter.api.Test)

Aggregations

Literal (at.ac.tuwien.kr.alpha.api.programs.literals.Literal)82 Test (org.junit.jupiter.api.Test)42 VariableTerm (at.ac.tuwien.kr.alpha.api.terms.VariableTerm)20 ArrayList (java.util.ArrayList)20 AggregateLiteral (at.ac.tuwien.kr.alpha.api.programs.literals.AggregateLiteral)17 Atom (at.ac.tuwien.kr.alpha.api.programs.atoms.Atom)16 Substitution (at.ac.tuwien.kr.alpha.api.grounder.Substitution)14 Predicate (at.ac.tuwien.kr.alpha.api.programs.Predicate)14 BasicAtom (at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom)14 BasicSubstitution (at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution)13 Map (java.util.Map)12 Head (at.ac.tuwien.kr.alpha.api.rules.heads.Head)11 CompiledProgram (at.ac.tuwien.kr.alpha.core.programs.CompiledProgram)11 CompiledRule (at.ac.tuwien.kr.alpha.core.rules.CompiledRule)10 HashSet (java.util.HashSet)10 ComparisonLiteral (at.ac.tuwien.kr.alpha.api.programs.literals.ComparisonLiteral)9 Instance (at.ac.tuwien.kr.alpha.commons.substitutions.Instance)9 LinkedHashSet (java.util.LinkedHashSet)8 List (java.util.List)8 Term (at.ac.tuwien.kr.alpha.api.terms.Term)7