use of at.ac.tuwien.kr.alpha.api.programs.Predicate in project Alpha by alpha-asp.
the class StratificationAlgorithmTest method avoidDuplicatesTest1.
@Test
public void avoidDuplicatesTest1() {
StringBuilder bld = new StringBuilder();
bld.append("b :- a.");
bld.append("c :- b.");
bld.append("c :- a.");
ASPCore2Program prog = parser.parse(bld.toString());
NormalProgram normalProg = normalizeTransform.apply(prog);
AnalyzedProgram analyzed = AnalyzedProgram.analyzeNormalProgram(normalProg);
DependencyGraph dg = analyzed.getDependencyGraph();
ComponentGraph cg = ComponentGraphImpl.buildComponentGraph(dg, StronglyConnectedComponentsAlgorithm.findStronglyConnectedComponents(dg));
List<SCComponent> strata = StratificationAlgorithm.calculateStratification(cg);
Predicate a = Predicates.getPredicate("a", 0);
Predicate b = Predicates.getPredicate("b", 0);
Predicate c = Predicates.getPredicate("c", 0);
assertTrue(predicateIsBeforePredicateInOrder(a, b, strata));
assertTrue(predicateIsBeforePredicateInOrder(b, c, strata));
assertTrue(predicateIsBeforePredicateInOrder(a, c, strata));
assertEquals(3, strata.size());
}
use of at.ac.tuwien.kr.alpha.api.programs.Predicate in project Alpha by alpha-asp.
the class StratificationAlgorithmTest method stratifyOneRuleTest.
@Test
public void stratifyOneRuleTest() {
ASPCore2Program prog = parser.parse("a :- b.");
NormalProgram normalProg = normalizeTransform.apply(prog);
AnalyzedProgram analyzed = AnalyzedProgram.analyzeNormalProgram(normalProg);
DependencyGraph dg = analyzed.getDependencyGraph();
ComponentGraph cg = ComponentGraphImpl.buildComponentGraph(dg, StronglyConnectedComponentsAlgorithm.findStronglyConnectedComponents(dg));
List<SCComponent> strata = StratificationAlgorithm.calculateStratification(cg);
Predicate a = Predicates.getPredicate("a", 0);
Predicate b = Predicates.getPredicate("b", 0);
assertEquals(2, strata.size());
assertTrue(predicateIsBeforePredicateInOrder(b, a, strata));
}
use of at.ac.tuwien.kr.alpha.api.programs.Predicate in project Alpha by alpha-asp.
the class DependencyGraphImplTest method edgesEqualTest.
@Test
public void edgesEqualTest() {
Predicate testPredicate = Predicates.getPredicate("test", 2, false, false);
EdgeImpl e1 = new EdgeImpl(new NodeImpl(testPredicate), true);
EdgeImpl e2 = new EdgeImpl(new NodeImpl(testPredicate), true);
assertEquals(e1, e2);
}
use of at.ac.tuwien.kr.alpha.api.programs.Predicate in project Alpha by alpha-asp.
the class LiteralInstantiatorTest method workingMemoryBasedInstantiatePositiveBasicLiteral.
@Test
public void workingMemoryBasedInstantiatePositiveBasicLiteral() {
Predicate p = Predicates.getPredicate("p", 2);
WorkingMemory workingMemory = new WorkingMemory();
workingMemory.initialize(p);
workingMemory.addInstance(Atoms.newBasicAtom(p, Terms.newSymbolicConstant("x"), Terms.newSymbolicConstant("y")), true);
workingMemory.addInstance(Atoms.newBasicAtom(p, Terms.newSymbolicConstant("x"), Terms.newSymbolicConstant("z")), true);
VariableTerm x = Terms.newVariable("X");
VariableTerm y = Terms.newVariable("Y");
Literal lit = Literals.fromAtom(Atoms.newBasicAtom(p, x, y), true);
Substitution substitution = new BasicSubstitution();
substitution.put(x, Terms.newSymbolicConstant("x"));
LiteralInstantiator instantiator = new LiteralInstantiator(new WorkingMemoryBasedInstantiationStrategy(workingMemory));
LiteralInstantiationResult result = instantiator.instantiateLiteral(lit, substitution);
assertEquals(LiteralInstantiationResult.Type.CONTINUE, result.getType());
List<ImmutablePair<Substitution, AssignmentStatus>> substitutions = result.getSubstitutions();
assertEquals(2, substitutions.size());
boolean ySubstituted = false;
boolean zSubstituted = false;
for (ImmutablePair<Substitution, AssignmentStatus> resultSubstitution : substitutions) {
assertTrue(resultSubstitution.left.isVariableSet(y));
assertEquals(AssignmentStatus.TRUE, resultSubstitution.right);
if (resultSubstitution.left.eval(y).equals(Terms.newSymbolicConstant("y"))) {
ySubstituted = true;
} else if (resultSubstitution.left.eval(y).equals(Terms.newSymbolicConstant("z"))) {
zSubstituted = true;
} else {
fail("Invalid substitution for variable Y");
}
}
assertTrue(ySubstituted && zSubstituted);
}
use of at.ac.tuwien.kr.alpha.api.programs.Predicate in project Alpha by alpha-asp.
the class FixedInterpretationLiteralsTest method positiveExternalBindingOutput.
@Test
public void positiveExternalBindingOutput() {
Optional<AnswerSet> answer = this.alpha.solve(this.alpha.readProgramString(TEST_PROG, this.externals)).findFirst();
assertTrue(answer.isPresent());
AnswerSet answerSet = answer.get();
Predicate pred = Predicates.getPredicate("positive_external_binding_output", 2);
assertTrue(answerSet.getPredicates().contains(pred));
Set<Atom> instances = answerSet.getPredicateInstances(pred);
assertEquals(3, instances.size());
assertTrue(instances.contains(Atoms.newBasicAtom(pred, Terms.newConstant("Klagenfurt"), Terms.newConstant("Villach"))));
assertTrue(instances.contains(Atoms.newBasicAtom(pred, Terms.newConstant("Klagenfurt"), Terms.newConstant("Graz"))));
assertTrue(instances.contains(Atoms.newBasicAtom(pred, Terms.newConstant("Villach"), Terms.newConstant("Salzburg"))));
}
Aggregations