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();
}
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;
}
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!");
}
}
}
}
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();
}
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);
}
Aggregations