Skip to content

Commit

Permalink
Introduce BibliographyConsistencyCheckResultTxtWriter (#10847)
Browse files Browse the repository at this point in the history
* Adds check of consistency of given entries by looking at the present and absent fields.

* Remove obsolete file

* Refactoring

* Refactoring to enable more writers

* WIP

* Use Writer instead of Path

* Add legend to txt writer

* Finish ResultTxtWriter

* Fix OpenRewrite issue

* Fix tests

* Remove obsolete key "Legend"
  • Loading branch information
koppor authored Feb 9, 2024
1 parent de1558a commit 13870d8
Show file tree
Hide file tree
Showing 10 changed files with 560 additions and 250 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import org.jabref.model.entry.field.Field;
import org.jabref.model.entry.types.EntryType;

public class PaperConsistencyCheck {
public class BibliographyConsistencyCheck {

public record Result(Map<EntryType, EntryTypeResult> entryTypeToResultMap) {
}
Expand All @@ -32,7 +32,7 @@ public record EntryTypeResult(Collection<Field> fields, SequencedCollection<BibE
* Computes the fields set in all entries. In case entries of the same type has more fields defined, it is output.
* <p>
* This class <em>does not</em> check whether all required fields are present or if the fields are valid for the entry type.
* That result can a) be retrieved by using the JabRef UI and b) by checking the CSV output of {@link PaperConsistencyCheckResultCsvWriter#writeFindingsAsCsv(Result, Path, BibEntryTypesManager, BibDatabaseMode)}
* That result can a) be retrieved by using the JabRef UI and b) by checking the CSV output of {@link BibliographyConsistencyCheckResultCsvWriter#writeFindingsAsCsv(Result, Path, BibEntryTypesManager, BibDatabaseMode)}
*
* @implNote This class does not implement {@link org.jabref.logic.integrity.DatabaseChecker}, because it returns a list of {@link org.jabref.logic.integrity.IntegrityMessage}, which are too fine grained.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.jabref.logic.quality.consistency;

import java.io.IOException;
import java.io.Writer;
import java.util.List;
import java.util.Set;

import org.jabref.model.database.BibDatabaseMode;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.BibEntryTypesManager;
import org.jabref.model.entry.field.Field;

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;

/**
* Outputs the findings as CSV.
* <p>
* The symbols from {@link BibliographyConsistencyCheckResultWriter} are used.
*/
public class BibliographyConsistencyCheckResultCsvWriter extends BibliographyConsistencyCheckResultWriter {

private CSVPrinter csvPrinter;

public BibliographyConsistencyCheckResultCsvWriter(BibliographyConsistencyCheck.Result result, Writer writer) {
super(result, writer);
}

public BibliographyConsistencyCheckResultCsvWriter(BibliographyConsistencyCheck.Result result, Writer writer, BibEntryTypesManager entryTypesManager, BibDatabaseMode bibDatabaseMode) {
super(result, writer, entryTypesManager, bibDatabaseMode);
}

@Override
public void writeFindings() throws IOException {
csvPrinter = new CSVPrinter(writer, CSVFormat.DEFAULT);
csvPrinter.printRecord(columnNames);
super.writeFindings();
}

@Override
protected void writeBibEntry(BibEntry bibEntry, String entryType, Set<Field> requiredFields, Set<Field> optionalFields) throws IOException {
List<String> theRecord = getFindingsAsList(bibEntry, entryType, requiredFields, optionalFields);
csvPrinter.printRecord(theRecord);
}

@Override
public void close() throws IOException {
csvPrinter.close();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package org.jabref.logic.quality.consistency;

import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.StringJoiner;
import java.util.stream.Collectors;

import org.jabref.logic.l10n.Localization;
import org.jabref.model.database.BibDatabaseMode;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.BibEntryTypesManager;
import org.jabref.model.entry.field.Field;

/**
* Outputs the findings as plain text.
* <p>
* The symbols from {@link BibliographyConsistencyCheckResultWriter} are used.
*/
public class BibliographyConsistencyCheckResultTxtWriter extends BibliographyConsistencyCheckResultWriter {

private List<Integer> columnWidths;

public BibliographyConsistencyCheckResultTxtWriter(BibliographyConsistencyCheck.Result result, Writer writer) {
super(result, writer);
}

public BibliographyConsistencyCheckResultTxtWriter(BibliographyConsistencyCheck.Result result, Writer writer, BibEntryTypesManager entryTypesManager, BibDatabaseMode bibDatabaseMode) {
super(result, writer, entryTypesManager, bibDatabaseMode);
}

public void writeFindings() throws IOException {
writer.write(Localization.lang("Field Presence Consistency Check Result"));
writer.write("\n\n");

if (result.entryTypeToResultMap().isEmpty()) {
writer.write("No errors found.\n");
return;
}

initializeColumnWidths();

outputRow(columnNames);

writer.write(columnWidths.stream().map(width -> "-".repeat(width)).collect(Collectors.joining(" | ", "| ", " |\n")));

super.writeFindings();

writer.write("\n");
writer.write("%s | %s\n".formatted(REQUIRED_FIELD_AT_ENTRY_TYPE_CELL_ENTRY, Localization.lang("required field is present")));
writer.write("%s | %s\n".formatted(OPTIONAL_FIELD_AT_ENTRY_TYPE_CELL_ENTRY, Localization.lang("optional field is present")));
writer.write("%s | %s\n".formatted(UNKNOWN_FIELD_AT_ENTRY_TYPE_CELL_ENTRY, Localization.lang("unknown field is present")));
writer.write("%s | %s\n".formatted(UNSET_FIELD_AT_ENTRY_TYPE_CELL_ENTRY, Localization.lang("field is absent")));
}

private void initializeColumnWidths() {
columnWidths = new ArrayList<>(columnNames.size());

Integer max = getColumnWidthOfEntryTypes();
columnWidths.add(max);

max = getColumnWidthOfCitationKeys(max);
columnWidths.add(max);

columnWidths.addAll(columnNames.stream().skip(2).map(String::length).toList());
}

private Integer getColumnWidthOfEntryTypes() {
Integer max = result.entryTypeToResultMap().keySet()
.stream()
.map(entryType -> entryType.getDisplayName().length())
.max(Integer::compareTo)
.get();
max = Math.max(max, "entry type".length());
return max;
}

private Integer getColumnWidthOfCitationKeys(Integer max) {
result.entryTypeToResultMap().values()
.stream()
.flatMap(entryTypeResult -> entryTypeResult.sortedEntries().stream())
.map(entry -> entry.getCitationKey().orElse("").length())
.max(Integer::compareTo)
.get();
return Math.max(max, "citation key".length());
}

@Override
protected void writeBibEntry(BibEntry bibEntry, String entryType, Set<Field> requiredFields, Set<Field> optionalFields) throws IOException {
List<String> theRecord = getFindingsAsList(bibEntry, entryType, requiredFields, optionalFields);
outputRow(theRecord);
}

private void outputRow(List<String> theRecord) throws IOException {
StringJoiner outputJoiner = new StringJoiner(" | ", "| ", " |\n");
for (int i = 0; i < theRecord.size(); i++) {
String fieldValue = theRecord.get(i);
int columnWidth = columnWidths.get(i);
String formattedField = ("%-" + columnWidth + "s").formatted(fieldValue);
outputJoiner.add(formattedField);
}

writer.write(outputJoiner.toString());
}

@Override
public void close() throws IOException {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package org.jabref.logic.quality.consistency;

import java.io.Closeable;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.SequencedCollection;
import java.util.Set;
import java.util.stream.Collectors;

import org.jabref.model.database.BibDatabaseMode;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.BibEntryType;
import org.jabref.model.entry.BibEntryTypesManager;
import org.jabref.model.entry.field.BibField;
import org.jabref.model.entry.field.Field;
import org.jabref.model.entry.types.EntryType;

import org.jooq.lambda.Unchecked;

/**
* Outputs the findings as CSV.
* <p>
* Following symbols are used (as default):
*
* <ul>
* <li><code>x</code> - required field is present</li>
* <li><code>o</code> - optional field is present</li>
* <li><code>?</code> - unknown field is present</li>
* <li><code>-</code> - field is absent</li>
* </ul>
* <p>
* Note that this classification is based on JabRef's definition and might not match the publisher's definition.
*
* @implNote We could have implemented a <code>PaperConsistencyCheckResultFormatter</code>, but that would have been too much effort.
*/
public abstract class BibliographyConsistencyCheckResultWriter implements Closeable {

protected static final String REQUIRED_FIELD_AT_ENTRY_TYPE_CELL_ENTRY = "x";
protected static final String OPTIONAL_FIELD_AT_ENTRY_TYPE_CELL_ENTRY = "o";
protected static final String UNKNOWN_FIELD_AT_ENTRY_TYPE_CELL_ENTRY = "?";
protected static final String UNSET_FIELD_AT_ENTRY_TYPE_CELL_ENTRY = "-";

protected final BibliographyConsistencyCheck.Result result;
protected final Writer writer;
protected final BibEntryTypesManager entryTypesManager;
protected final BibDatabaseMode bibDatabaseMode;
protected final List<String> columnNames;
protected final int columnCount;

private final List<Field> allReportedFields;

public BibliographyConsistencyCheckResultWriter(BibliographyConsistencyCheck.Result result, Writer writer) {
this(result, writer, new BibEntryTypesManager(), BibDatabaseMode.BIBTEX);
}

public BibliographyConsistencyCheckResultWriter(BibliographyConsistencyCheck.Result result, Writer writer, BibEntryTypesManager entryTypesManager, BibDatabaseMode bibDatabaseMode) {
this.result = result;
this.writer = writer;
this.entryTypesManager = entryTypesManager;
this.bibDatabaseMode = bibDatabaseMode;
this.allReportedFields = result.entryTypeToResultMap().values().stream()
.flatMap(entryTypeResult -> entryTypeResult.fields().stream())
.sorted(Comparator.comparing(Field::getName))
.distinct()
.toList();
this.columnNames = getColumnNames();
this.columnCount = columnNames.size();
}

public void writeFindings() throws IOException {
result.entryTypeToResultMap().entrySet().stream()
.sorted(Comparator.comparing(entry -> entry.getKey().getName()))
.forEach(Unchecked.consumer(mapEntry -> {
writeMapEntry(mapEntry);
}));
}

private List<String> getColumnNames() {
List<String> result = new ArrayList(columnCount + 2);
result.add("entry type");
result.add("citation key");
allReportedFields.forEach(field -> {
result.add(field.getDisplayName());
});
return result;
}

protected List<String> getFindingsAsList(BibEntry bibEntry, String entryType, Set<Field> requiredFields, Set<Field> optionalFields) {
List<String> result = new ArrayList(columnCount + 2);
result.add(entryType);
result.add(bibEntry.getCitationKey().orElse(""));
allReportedFields.forEach(field -> {
result.add(bibEntry.getField(field).map(value -> {
if (requiredFields.contains(field)) {
return REQUIRED_FIELD_AT_ENTRY_TYPE_CELL_ENTRY;
} else if (optionalFields.contains(field)) {
return OPTIONAL_FIELD_AT_ENTRY_TYPE_CELL_ENTRY;
} else {
return UNKNOWN_FIELD_AT_ENTRY_TYPE_CELL_ENTRY;
}
}).orElse(UNSET_FIELD_AT_ENTRY_TYPE_CELL_ENTRY));
});
return result;
}

protected void writeMapEntry(Map.Entry<EntryType, BibliographyConsistencyCheck.EntryTypeResult> mapEntry) {
String entryType = mapEntry.getKey().getDisplayName();

Optional<BibEntryType> bibEntryType = this.entryTypesManager.enrich(mapEntry.getKey(), bibDatabaseMode);
Set<Field> requiredFields = bibEntryType
.map(BibEntryType::getRequiredFields)
.stream()
.flatMap(orFieldsCollection -> orFieldsCollection.stream())
.flatMap(orFields -> orFields.getFields().stream())
.collect(Collectors.toSet());
Set<Field> optionalFields = bibEntryType
.map(BibEntryType::getOptionalFields)
.stream()
.flatMap(bibFieldSet -> bibFieldSet.stream())
.map(BibField::field)
.collect(Collectors.toSet());

BibliographyConsistencyCheck.EntryTypeResult entries = mapEntry.getValue();
SequencedCollection<BibEntry> bibEntries = entries.sortedEntries();

bibEntries.forEach(Unchecked.consumer(bibEntry -> {
writeBibEntry(bibEntry, entryType, requiredFields, optionalFields);
}));
}

protected abstract void writeBibEntry(BibEntry bibEntry, String entryType, Set<Field> requiredFields, Set<Field> optionalFields) throws IOException;
}
Loading

0 comments on commit 13870d8

Please sign in to comment.