-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into domspec_heuristics
# Conflicts: # src/main/java/at/ac/tuwien/kr/alpha/Main.java # src/main/java/at/ac/tuwien/kr/alpha/config/CommandLineParser.java # src/test/java/at/ac/tuwien/kr/alpha/api/AlphaTest.java
- Loading branch information
Showing
14 changed files
with
574 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
src/main/java/at/ac/tuwien/kr/alpha/AnswerSetToXlsxWriter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package at.ac.tuwien.kr.alpha; | ||
|
||
import at.ac.tuwien.kr.alpha.api.mapper.AnswerSetToObjectMapper; | ||
import at.ac.tuwien.kr.alpha.api.mapper.impl.AnswerSetToWorkbookMapper; | ||
import at.ac.tuwien.kr.alpha.common.AnswerSet; | ||
import org.apache.poi.ss.usermodel.Cell; | ||
import org.apache.poi.ss.usermodel.Row; | ||
import org.apache.poi.ss.usermodel.Sheet; | ||
import org.apache.poi.ss.usermodel.Workbook; | ||
import org.apache.poi.xssf.usermodel.XSSFWorkbook; | ||
|
||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.nio.file.StandardOpenOption; | ||
import java.util.function.BiConsumer; | ||
|
||
public class AnswerSetToXlsxWriter implements BiConsumer<Integer, AnswerSet> { | ||
|
||
private String targetBasePath; | ||
private AnswerSetToObjectMapper<Workbook> answerSetMapper; | ||
|
||
public AnswerSetToXlsxWriter(String targetBasePath) { | ||
this.targetBasePath = targetBasePath; | ||
this.answerSetMapper = new AnswerSetToWorkbookMapper(); | ||
} | ||
|
||
@Override | ||
public void accept(Integer num, AnswerSet as) { | ||
try { | ||
Path outputPath = Paths.get(this.targetBasePath + "." + num + ".xlsx"); | ||
OutputStream os = Files.newOutputStream(outputPath, StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING); | ||
Workbook wb = this.answerSetMapper.mapFromAnswerSet(as); | ||
wb.write(os); | ||
wb.close(); | ||
os.close(); | ||
System.out.println("Answer set written to file " + outputPath.toString()); | ||
} catch (IOException ex) { | ||
System.err.println("Failed writing answer set as xlsx file! (" + ex.getMessage() + ")"); | ||
} | ||
} | ||
|
||
public static void writeUnsatInfo(Path path) throws IOException { | ||
Workbook workbook = new XSSFWorkbook(); | ||
// first, create a worksheet for 0-arity predicates | ||
Sheet sheet = workbook.createSheet("Unsatisfiable"); | ||
Row row = sheet.createRow(0); | ||
Cell cell = row.createCell(0); | ||
cell.setCellValue("Input is unsatisfiable - No answer sets!"); | ||
sheet.autoSizeColumn(0); | ||
OutputStream os = Files.newOutputStream(path, StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING); | ||
workbook.write(os); | ||
workbook.close(); | ||
os.close(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/main/java/at/ac/tuwien/kr/alpha/api/mapper/AnswerSetToObjectMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
* Copyright (c) 2020, the Alpha Team. | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are met: | ||
* | ||
* 1) Redistributions of source code must retain the above copyright notice, this | ||
* list of conditions and the following disclaimer. | ||
* | ||
* 2) Redistributions in binary form must reproduce the above copyright notice, | ||
* this list of conditions and the following disclaimer in the documentation | ||
* and/or other materials provided with the distribution. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | ||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
package at.ac.tuwien.kr.alpha.api.mapper; | ||
|
||
import at.ac.tuwien.kr.alpha.common.AnswerSet; | ||
|
||
/** | ||
* Copyright (c) 2020, the Alpha Team. | ||
* | ||
* Interface definition for an adapter that maps from an {@link AnswerSet} to an instance of the implementation's generic type T. | ||
* | ||
* @param <T> the type to which to map answer sets | ||
*/ | ||
public interface AnswerSetToObjectMapper<T> { | ||
|
||
T mapFromAnswerSet(AnswerSet answerSet); | ||
|
||
} |
137 changes: 137 additions & 0 deletions
137
src/main/java/at/ac/tuwien/kr/alpha/api/mapper/impl/AnswerSetToWorkbookMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
/** | ||
* Copyright (c) 2020, the Alpha Team. | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are met: | ||
* | ||
* 1) Redistributions of source code must retain the above copyright notice, this | ||
* list of conditions and the following disclaimer. | ||
* | ||
* 2) Redistributions in binary form must reproduce the above copyright notice, | ||
* this list of conditions and the following disclaimer in the documentation | ||
* and/or other materials provided with the distribution. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | ||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
package at.ac.tuwien.kr.alpha.api.mapper.impl; | ||
|
||
import at.ac.tuwien.kr.alpha.api.mapper.AnswerSetToObjectMapper; | ||
import at.ac.tuwien.kr.alpha.common.AnswerSet; | ||
import at.ac.tuwien.kr.alpha.common.Predicate; | ||
import at.ac.tuwien.kr.alpha.common.atoms.Atom; | ||
import at.ac.tuwien.kr.alpha.common.terms.Term; | ||
import org.apache.poi.ss.usermodel.Cell; | ||
import org.apache.poi.ss.usermodel.CellStyle; | ||
import org.apache.poi.ss.usermodel.FillPatternType; | ||
import org.apache.poi.ss.usermodel.Font; | ||
import org.apache.poi.ss.usermodel.IndexedColors; | ||
import org.apache.poi.ss.usermodel.Row; | ||
import org.apache.poi.ss.usermodel.Sheet; | ||
import org.apache.poi.ss.usermodel.Workbook; | ||
import org.apache.poi.xssf.usermodel.XSSFWorkbook; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Implementation of {@link AnswerSetToObjectMapper} that generates an office open xml workbook ("excel file") from a given answer set. | ||
* | ||
* Copyright (c) 2020, the Alpha Team. | ||
*/ | ||
public class AnswerSetToWorkbookMapper implements AnswerSetToObjectMapper<Workbook> { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(AnswerSetToWorkbookMapper.class); | ||
|
||
/** | ||
* 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; | ||
} | ||
|
||
private void writeAtomToSheet(Sheet sheet, Atom atom) { | ||
int rownum = -1; | ||
if (sheet.getLastRowNum() == 0 && sheet.getRow(0) == null) { | ||
// sheet is empty, start at row zero | ||
rownum = 0; | ||
} else { | ||
rownum = sheet.getLastRowNum() + 1; | ||
} | ||
Row atomRow = sheet.createRow(rownum); | ||
List<Term> terms = atom.getTerms(); | ||
Cell currCell; | ||
if (terms.isEmpty()) { | ||
// 0-arity atom | ||
currCell = atomRow.createCell(0); | ||
currCell.setCellValue(atom.getPredicate().getName()); | ||
sheet.autoSizeColumn(0); | ||
} else { | ||
for (int i = 0; i < terms.size(); i++) { | ||
currCell = atomRow.createCell(i); | ||
currCell.setCellValue(terms.get(i).toString()); | ||
sheet.autoSizeColumn(i); | ||
} | ||
} | ||
} | ||
|
||
private CellStyle createHeaderStyle(Workbook workbook) { | ||
CellStyle headerStyle = workbook.createCellStyle(); | ||
Font headerFont = workbook.createFont(); | ||
headerFont.setFontHeightInPoints((short) 11); | ||
headerFont.setBold(true); // (short) 0x74c4f2 | ||
headerStyle.setFont(headerFont); | ||
headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); | ||
headerStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex()); | ||
return headerStyle; | ||
} | ||
|
||
private Sheet createSheetWithHeader(Workbook wb, CellStyle headerStyle, String sheetName, String... headerContent) { | ||
Sheet retVal = wb.createSheet(sheetName); | ||
Row headerRow = retVal.createRow(0); | ||
Cell cell; | ||
for (int i = 0; i < headerContent.length; i++) { | ||
cell = headerRow.createCell(i); | ||
cell.setCellStyle(headerStyle); | ||
cell.setCellValue(headerContent[i]); | ||
} | ||
return retVal; | ||
} | ||
|
||
} |
Oops, something went wrong.