use of at.ac.tuwien.kr.alpha.commons.substitutions.Instance in project Alpha by alpha-asp.
the class SubstitutionTest method specializeBasicAtom.
@Test
public void specializeBasicAtom() {
Predicate p = Predicates.getPredicate("p", 2);
BasicAtom atom = Atoms.newBasicAtom(p, Arrays.asList(X, Y));
Instance instance = new Instance(A, B);
Substitution substitution = BasicSubstitution.specializeSubstitution(atom, instance, BasicSubstitution.EMPTY_SUBSTITUTION);
BasicAtom substituted = atom.substitute(substitution);
assertEquals(p, substituted.getPredicate());
assertEquals(A, substituted.getTerms().get(0));
assertEquals(B, substituted.getTerms().get(1));
}
use of at.ac.tuwien.kr.alpha.commons.substitutions.Instance in project Alpha by alpha-asp.
the class IndexedInstanceStorageTest method testIndexedInstanceStorage.
@Test
public void testIndexedInstanceStorage() {
IndexedInstanceStorage storage = new IndexedInstanceStorage(Predicates.getPredicate("p", 4), true);
storage.addIndexPosition(0);
storage.addIndexPosition(2);
ConstantTerm<String> t0 = Terms.newConstant("0");
ConstantTerm<String> t1 = Terms.newConstant("1");
ConstantTerm<String> t2 = Terms.newConstant("2");
ConstantTerm<String> t3 = Terms.newConstant("3");
ConstantTerm<String> t4 = Terms.newConstant("4");
ConstantTerm<String> t5 = Terms.newConstant("5");
Instance badInst1 = new Instance(t1, t1, t0);
Instance badInst2 = new Instance(t5, t5, t5, t5, t5);
try {
storage.addInstance(badInst1);
fail();
} catch (Exception e) {
assertTrue(e.getMessage().startsWith("Instance length does not match arity of IndexedInstanceStorage"));
}
try {
storage.addInstance(badInst2);
fail();
} catch (Exception e) {
assertTrue(e.getMessage().startsWith("Instance length does not match arity of IndexedInstanceStorage"));
}
Instance inst1 = new Instance(t1, t1, t1, t1);
Instance inst2 = new Instance(t1, t2, t3, t4);
Instance inst3 = new Instance(t4, t3, t3, t5);
Instance inst4 = new Instance(t1, t2, t1, t1);
Instance inst5 = new Instance(t5, t4, t3, t2);
storage.addInstance(inst1);
storage.addInstance(inst2);
storage.addInstance(inst3);
storage.addInstance(inst4);
storage.addInstance(inst5);
List<Instance> matching3 = storage.getInstancesMatchingAtPosition(t3, 2);
assertEquals(matching3.size(), 3);
assertTrue(matching3.contains(new Instance(t1, t2, t3, t4)));
assertTrue(matching3.contains(new Instance(t4, t3, t3, t5)));
assertTrue(matching3.contains(new Instance(t5, t4, t3, t2)));
assertFalse(matching3.contains(new Instance(t1, t1, t1, t1)));
List<Instance> matching1 = storage.getInstancesMatchingAtPosition(t2, 0);
assertEquals(matching1.size(), 0);
}
use of at.ac.tuwien.kr.alpha.commons.substitutions.Instance in project Alpha by alpha-asp.
the class StratifiedEvaluationTest method testDuplicateFacts.
@Test
public void testDuplicateFacts() {
String aspStr = "p(a). p(b). q(b). q(X) :- p(X).";
CompiledProgram evaluated = parseAndEvaluate.apply(aspStr);
Instance qOfB = new Instance(TestUtils.basicAtomWithSymbolicTerms("q", "b").getTerms());
Set<Instance> facts = evaluated.getFactsByPredicate().get(Predicates.getPredicate("q", 1));
int numQOfB = 0;
for (Instance at : facts) {
if (at.equals(qOfB)) {
numQOfB++;
}
}
assertEquals(1, numQOfB);
}
use of at.ac.tuwien.kr.alpha.commons.substitutions.Instance in project Alpha by alpha-asp.
the class NaiveGrounder method assignmentToAnswerSet.
@Override
public AnswerSet assignmentToAnswerSet(Iterable<Integer> trueAtoms) {
Map<Predicate, SortedSet<Atom>> predicateInstances = new LinkedHashMap<>();
SortedSet<Predicate> knownPredicates = new TreeSet<>();
// Iterate over all true atomIds, computeNextAnswerSet instances from atomStore and add them if not filtered.
for (int trueAtom : trueAtoms) {
final Atom atom = atomStore.get(trueAtom);
Predicate predicate = atom.getPredicate();
// Skip atoms over internal predicates.
if (predicate.isInternal()) {
continue;
}
// Skip filtered predicates.
if (!filter.test(predicate)) {
continue;
}
knownPredicates.add(predicate);
predicateInstances.putIfAbsent(predicate, new TreeSet<>());
Set<Atom> instances = predicateInstances.get(predicate);
instances.add(atom);
}
// Add true atoms from facts.
for (Map.Entry<Predicate, LinkedHashSet<Instance>> facts : factsFromProgram.entrySet()) {
Predicate factPredicate = facts.getKey();
// Skip atoms over internal predicates.
if (factPredicate.isInternal()) {
continue;
}
// Skip filtered predicates.
if (!filter.test(factPredicate)) {
continue;
}
// Skip predicates without any instances.
if (facts.getValue().isEmpty()) {
continue;
}
knownPredicates.add(factPredicate);
predicateInstances.putIfAbsent(factPredicate, new TreeSet<>());
for (Instance factInstance : facts.getValue()) {
SortedSet<Atom> instances = predicateInstances.get(factPredicate);
instances.add(Atoms.newBasicAtom(factPredicate, factInstance.terms));
}
}
if (knownPredicates.isEmpty()) {
return AnswerSets.EMPTY_SET;
}
return AnswerSets.newAnswerSet(knownPredicates, predicateInstances);
}
use of at.ac.tuwien.kr.alpha.commons.substitutions.Instance in project Alpha by alpha-asp.
the class NaiveGrounder method getRulesWithUniqueHead.
private Set<CompiledRule> getRulesWithUniqueHead() {
// FIXME: below optimisation (adding support nogoods if there is only one rule instantiation per unique atom over the interpretation) could
// be done as a transformation (adding a non-ground constraint corresponding to the nogood that is generated by the grounder).
// Record all unique rule heads.
final Set<CompiledRule> uniqueGroundRulePerGroundHead = new HashSet<>();
for (Map.Entry<Predicate, LinkedHashSet<CompiledRule>> headDefiningRules : program.getPredicateDefiningRules().entrySet()) {
if (headDefiningRules.getValue().size() != 1) {
continue;
}
CompiledRule nonGroundRule = headDefiningRules.getValue().iterator().next();
// Check that all variables of the body also occur in the head (otherwise grounding is not unique).
Atom headAtom = nonGroundRule.getHeadAtom();
// Rule is not guaranteed unique if there are facts for it.
HashSet<Instance> potentialFacts = factsFromProgram.get(headAtom.getPredicate());
if (potentialFacts != null && !potentialFacts.isEmpty()) {
continue;
}
// Collect head and body variables.
HashSet<VariableTerm> occurringVariablesHead = new HashSet<>(headAtom.toLiteral().getBindingVariables());
HashSet<VariableTerm> occurringVariablesBody = new HashSet<>();
for (Literal lit : nonGroundRule.getPositiveBody()) {
occurringVariablesBody.addAll(lit.getBindingVariables());
}
occurringVariablesBody.removeAll(occurringVariablesHead);
// Check if ever body variables occurs in the head.
if (occurringVariablesBody.isEmpty()) {
uniqueGroundRulePerGroundHead.add(nonGroundRule);
}
}
return uniqueGroundRulePerGroundHead;
}
Aggregations