Search in sources :

Example 26 with Predicate

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

the class ThreeColouringWheelTest method createColors.

private List<Atom> createColors(String... colours) {
    List<Atom> facts = new ArrayList<>(colours.length);
    Predicate predicate = Predicates.getPredicate("c", 1);
    for (String colour : colours) {
        List<Term> terms = new ArrayList<>(1);
        terms.add(Terms.newConstant(colour));
        facts.add(Atoms.newBasicAtom(predicate, terms));
    }
    return facts;
}
Also used : ArrayList(java.util.ArrayList) Term(at.ac.tuwien.kr.alpha.api.terms.Term) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate)

Example 27 with Predicate

use of at.ac.tuwien.kr.alpha.api.programs.Predicate 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 28 with Predicate

use of at.ac.tuwien.kr.alpha.api.programs.Predicate 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 29 with Predicate

use of at.ac.tuwien.kr.alpha.api.programs.Predicate 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 30 with Predicate

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

the class BasicAnswerSet method compareTo.

@Override
public int compareTo(AnswerSet other) {
    final SortedSet<Predicate> predicates = this.getPredicates();
    int result = Util.compareSortedSets(predicates, other.getPredicates());
    if (result != 0) {
        return result;
    }
    for (Predicate predicate : predicates) {
        result = Util.compareSortedSets(this.getPredicateInstances(predicate), other.getPredicateInstances(predicate));
        if (result != 0) {
            return result;
        }
    }
    return 0;
}
Also used : Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate)

Aggregations

Predicate (at.ac.tuwien.kr.alpha.api.programs.Predicate)69 Test (org.junit.jupiter.api.Test)37 Atom (at.ac.tuwien.kr.alpha.api.programs.atoms.Atom)27 BasicAtom (at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom)19 WorkingMemory (at.ac.tuwien.kr.alpha.core.grounder.WorkingMemory)15 Literal (at.ac.tuwien.kr.alpha.api.programs.literals.Literal)14 AnswerSet (at.ac.tuwien.kr.alpha.api.AnswerSet)13 BasicSubstitution (at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution)13 LinkedHashSet (java.util.LinkedHashSet)13 Substitution (at.ac.tuwien.kr.alpha.api.grounder.Substitution)11 AtomStoreImpl (at.ac.tuwien.kr.alpha.core.common.AtomStoreImpl)10 ArrayList (java.util.ArrayList)10 DependencyGraph (at.ac.tuwien.kr.alpha.api.programs.analysis.DependencyGraph)9 ComponentGraph (at.ac.tuwien.kr.alpha.api.programs.analysis.ComponentGraph)8 Term (at.ac.tuwien.kr.alpha.api.terms.Term)8 AtomStore (at.ac.tuwien.kr.alpha.core.common.AtomStore)8 AnalyzedProgram (at.ac.tuwien.kr.alpha.core.programs.AnalyzedProgram)8 CompiledRule (at.ac.tuwien.kr.alpha.core.rules.CompiledRule)8 TrailAssignment (at.ac.tuwien.kr.alpha.core.solver.TrailAssignment)8 HashMap (java.util.HashMap)8