Search in sources :

Example 31 with Atom

use of at.ac.tuwien.kr.alpha.api.programs.atoms.Atom in project Alpha by alpha-asp.

the class SubstitutionTestUtil method groundAndPrintRule.

public static String groundAndPrintRule(CompiledRule rule, Substitution substitution) {
    StringBuilder ret = new StringBuilder();
    if (!rule.isConstraint()) {
        Atom groundHead = rule.getHeadAtom().substitute(substitution);
        ret.append(groundHead.toString());
    }
    ret.append(" :- ");
    boolean isFirst = true;
    for (Literal lit : rule.getBody()) {
        ret.append(groundLiteralToString(lit, substitution, isFirst));
        isFirst = false;
    }
    ret.append(".");
    return ret.toString();
}
Also used : Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom)

Example 32 with Atom

use of at.ac.tuwien.kr.alpha.api.programs.atoms.Atom in project Alpha by alpha-asp.

the class AnswerSetToWorkbookMapper method mapFromAnswerSet.

/**
 * Creates an xlsx workbook containing all the atoms from the given {@link AnswerSet} with one sheet per predicate. All predicates with arity 0 are listed
 * in a special sheet called "flags". Caution, potential resource leak: note that the returned workbook needs to be closed by the caller once it has been
 * processed (written to file etc).
 */
@Override
public Workbook mapFromAnswerSet(AnswerSet answerSet) {
    LOGGER.debug("Start mapping answer set to workbook");
    Workbook workbook = new XSSFWorkbook();
    // create cell style for header cells
    CellStyle headerStyle = this.createHeaderStyle(workbook);
    // first, create a worksheet for 0-arity predicates
    Sheet flags = this.createSheetWithHeader(workbook, headerStyle, "Flags", "Flags");
    Sheet currentPredicateSheet;
    String[] headerContent;
    for (Predicate pred : answerSet.getPredicates()) {
        if (pred.getArity() == 0) {
            this.writeAtomToSheet(flags, answerSet.getPredicateInstances(pred).first());
        } else {
            headerContent = new String[pred.getArity()];
            for (int i = 0; i < headerContent.length; i++) {
                headerContent[i] = "Attribute " + Integer.toString(i + 1);
            }
            currentPredicateSheet = this.createSheetWithHeader(workbook, headerStyle, pred.getName() + "_" + pred.getArity(), headerContent);
            for (Atom atom : answerSet.getPredicateInstances(pred)) {
                this.writeAtomToSheet(currentPredicateSheet, atom);
            }
        }
    }
    return workbook;
}
Also used : XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) CellStyle(org.apache.poi.ss.usermodel.CellStyle) Sheet(org.apache.poi.ss.usermodel.Sheet) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate)

Example 33 with Atom

use of at.ac.tuwien.kr.alpha.api.programs.atoms.Atom in project Alpha by alpha-asp.

the class AnswerSetToWorkbookMapperTest method assertWorkbookMatchesAnswerSet.

public static void assertWorkbookMatchesAnswerSet(Workbook wb, AnswerSet as) {
    for (Predicate pred : as.getPredicates()) {
        if (pred.getArity() == 0) {
            boolean flagFound = false;
            Sheet flagsSheet = wb.getSheet("Flags");
            assertNotNull(flagsSheet);
            for (Row row : flagsSheet) {
                if (row.getCell(0).getStringCellValue().equals(pred.getName())) {
                    flagFound = true;
                    break;
                }
            }
            assertTrue(flagFound, "0-arity predicate " + pred.getName() + " not found in workbook!");
        } else {
            Sheet predicateSheet = wb.getSheet(pred.getName() + "_" + pred.getArity());
            for (Atom atom : as.getPredicateInstances(pred)) {
                boolean atomFound = false;
                assertNotNull(predicateSheet);
                for (Row row : predicateSheet) {
                    if (AnswerSetToWorkbookMapperTest.rowMatchesAtom(row, atom)) {
                        atomFound = true;
                        break;
                    }
                }
                assertTrue(atomFound, "Atom " + atom.toString() + " not found in workbook!");
            }
        }
    }
}
Also used : Row(org.apache.poi.ss.usermodel.Row) Sheet(org.apache.poi.ss.usermodel.Sheet) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate)

Example 34 with Atom

use of at.ac.tuwien.kr.alpha.api.programs.atoms.Atom in project Alpha by alpha-asp.

the class BasicAnswerSet method toString.

@Override
public String toString() {
    if (predicates.isEmpty()) {
        return "{}";
    }
    final StringBuilder sb = new StringBuilder("{ ");
    for (Iterator<Predicate> iterator = predicates.iterator(); iterator.hasNext(); ) {
        Predicate predicate = iterator.next();
        Set<Atom> instances = getPredicateInstances(predicate);
        if (instances == null || instances.isEmpty()) {
            sb.append(predicate.getName());
            continue;
        }
        for (Iterator<Atom> instanceIterator = instances.iterator(); instanceIterator.hasNext(); ) {
            sb.append(instanceIterator.next());
            if (instanceIterator.hasNext()) {
                sb.append(", ");
            }
        }
        if (iterator.hasNext()) {
            sb.append(", ");
        }
    }
    sb.append(" }");
    return sb.toString();
}
Also used : Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate)

Example 35 with Atom

use of at.ac.tuwien.kr.alpha.api.programs.atoms.Atom 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);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Instance(at.ac.tuwien.kr.alpha.commons.substitutions.Instance) SortedSet(java.util.SortedSet) ChoiceAtom(at.ac.tuwien.kr.alpha.core.atoms.ChoiceAtom) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) RuleAtom(at.ac.tuwien.kr.alpha.core.atoms.RuleAtom) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate) LinkedHashMap(java.util.LinkedHashMap) TreeSet(java.util.TreeSet) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

Atom (at.ac.tuwien.kr.alpha.api.programs.atoms.Atom)73 Test (org.junit.jupiter.api.Test)38 Predicate (at.ac.tuwien.kr.alpha.api.programs.Predicate)27 BasicAtom (at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom)27 LinkedHashSet (java.util.LinkedHashSet)16 Unifier (at.ac.tuwien.kr.alpha.commons.substitutions.Unifier)15 ArrayList (java.util.ArrayList)14 AnswerSet (at.ac.tuwien.kr.alpha.api.AnswerSet)13 Literal (at.ac.tuwien.kr.alpha.api.programs.literals.Literal)12 AtomStoreImpl (at.ac.tuwien.kr.alpha.core.common.AtomStoreImpl)11 Instance (at.ac.tuwien.kr.alpha.commons.substitutions.Instance)9 AtomStore (at.ac.tuwien.kr.alpha.core.common.AtomStore)9 WorkingMemory (at.ac.tuwien.kr.alpha.core.grounder.WorkingMemory)9 TrailAssignment (at.ac.tuwien.kr.alpha.core.solver.TrailAssignment)9 ASPCore2Program (at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program)8 RuleAtom (at.ac.tuwien.kr.alpha.core.atoms.RuleAtom)8 WritableAssignment (at.ac.tuwien.kr.alpha.core.solver.WritableAssignment)7 Substitution (at.ac.tuwien.kr.alpha.api.grounder.Substitution)6 ExternalAtom (at.ac.tuwien.kr.alpha.api.programs.atoms.ExternalAtom)6 AnswerSetBuilder (at.ac.tuwien.kr.alpha.commons.AnswerSetBuilder)6