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