use of at.ac.tuwien.kr.alpha.api.programs.Predicate in project Alpha by alpha-asp.
the class NaiveGrounder method bootstrap.
/**
* Prepares facts of the input program for joining and derives all NoGoods representing ground rules. May only be called once.
*
* @return
*/
protected HashMap<Integer, NoGood> bootstrap() {
final HashMap<Integer, NoGood> groundNogoods = new LinkedHashMap<>();
for (Predicate predicate : factsFromProgram.keySet()) {
// Instead of generating NoGoods, add instance to working memories directly.
workingMemory.addInstances(predicate, true, factsFromProgram.get(predicate));
}
for (CompiledRule nonGroundRule : fixedRules) {
// Generate NoGoods for all rules that have a fixed grounding.
RuleGroundingOrder groundingOrder = nonGroundRule.getGroundingInfo().getFixedGroundingOrder();
BindingResult bindingResult = getGroundInstantiations(nonGroundRule, groundingOrder, new BasicSubstitution(), null);
groundAndRegister(nonGroundRule, bindingResult.getGeneratedSubstitutions(), groundNogoods);
}
fixedRules = null;
return groundNogoods;
}
use of at.ac.tuwien.kr.alpha.api.programs.Predicate 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.api.programs.Predicate 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;
}
use of at.ac.tuwien.kr.alpha.api.programs.Predicate in project Alpha by alpha-asp.
the class SimpleAnswerSetFormatter method format.
@Override
public String format(AnswerSet answerSet) {
List<String> predicateInstanceStrings = new ArrayList<>();
for (Predicate p : answerSet.getPredicates()) {
SortedSet<Atom> instances;
if ((instances = answerSet.getPredicateInstances(p)) == null || instances.isEmpty()) {
predicateInstanceStrings.add(p.getName());
} else {
List<String> atomStrings = instances.stream().map((atom) -> atom.toString()).collect(Collectors.toList());
predicateInstanceStrings.add(String.join(this.atomSeparator, atomStrings));
}
}
return "{ " + String.join(this.atomSeparator, predicateInstanceStrings) + " }";
}
use of at.ac.tuwien.kr.alpha.api.programs.Predicate in project Alpha by alpha-asp.
the class AnswerSetQueryTest method matchXWithFuncTerm.
@Test
public void matchXWithFuncTerm() {
Predicate p = Predicates.getPredicate("p", 2);
Atom a1 = Atoms.newBasicAtom(p, Terms.newSymbolicConstant("x"), Terms.newFunctionTerm("f", Terms.newSymbolicConstant("x")));
Atom a2 = Atoms.newBasicAtom(p, Terms.newSymbolicConstant("y"), Terms.newFunctionTerm("f", Terms.newSymbolicConstant("y")));
Atom a3 = Atoms.newBasicAtom(p, Terms.newSymbolicConstant("y"), Terms.newFunctionTerm("f", Terms.newSymbolicConstant("x")));
Atom a4 = Atoms.newBasicAtom(p, Terms.newSymbolicConstant("x"), Terms.newFunctionTerm("f", Terms.newSymbolicConstant("y")));
Atom a5 = Atoms.newBasicAtom(p, Terms.newSymbolicConstant("x"), Terms.newFunctionTerm("f"));
SortedSet<Predicate> predicates = new TreeSet<>();
predicates.add(p);
Map<Predicate, SortedSet<Atom>> instances = new HashMap<>();
SortedSet<Atom> ps = new TreeSet<>();
ps.add(a1);
ps.add(a2);
ps.add(a3);
ps.add(a4);
ps.add(a5);
instances.put(p, ps);
AnswerSet as = AnswerSets.newAnswerSet(predicates, instances);
AnswerSetQueryImpl query = AnswerSetQueryImpl.forPredicate(Predicates.getPredicate("p", 2)).withConstantEquals(0, "x").withFunctionTerm(1, "f", 1);
List<Atom> queryResult = as.query(query);
assertEquals(2, queryResult.size());
}
Aggregations