Skip to content

Commit fb874e9

Browse files
committed
Count rule violations per fields #172
1 parent 815b62b commit fb874e9

File tree

4 files changed

+106
-8
lines changed

4 files changed

+106
-8
lines changed

src/main/java/de/gwdg/metadataqa/api/rule/BaseRuleChecker.java

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ public abstract class BaseRuleChecker implements RuleChecker {
1515
protected String header;
1616
protected Boolean hidden = false;
1717
private Boolean debug = false;
18+
/**
19+
* A flag to denote if the RuleChecker should count the number instances and failures
20+
*/
21+
private Boolean countInstances = false;
1822

1923
@Override
2024
public String getId() {
@@ -88,11 +92,32 @@ else if (outputType.equals(RuleCheckingOutputType.SCORE))
8892
}
8993

9094
protected void addOutput(FieldCounter<RuleCheckerOutput> results, boolean isNA, boolean allPassed, RuleCheckingOutputType outputType) {
95+
addOutput(results, isNA, allPassed, outputType, null, null);
96+
}
97+
98+
protected void addOutput(FieldCounter<RuleCheckerOutput> results,
99+
boolean isNA,
100+
boolean allPassed,
101+
RuleCheckingOutputType outputType,
102+
Integer instanceCount,
103+
Integer failureCount) {
104+
105+
RuleCheckerOutput output = new RuleCheckerOutput(this, isNA, allPassed);
106+
if (instanceCount != null)
107+
output.setInstanceCount(instanceCount);
108+
if (failureCount != null)
109+
output.setFailureCount(failureCount);
110+
91111
if (outputType.equals(RuleCheckingOutputType.STATUS) || outputType.equals(RuleCheckingOutputType.SCORE)) {
92-
results.put(getHeader(), new RuleCheckerOutput(this, isNA, allPassed).setOutputType(outputType));
112+
results.put(getHeader(), output.setOutputType(outputType));
93113
} else {
94-
results.put(getHeader(RuleCheckingOutputType.STATUS), new RuleCheckerOutput(this, isNA, allPassed).setOutputType(RuleCheckingOutputType.STATUS));
95-
results.put(getHeader(RuleCheckingOutputType.SCORE), new RuleCheckerOutput(this, isNA, allPassed).setOutputType(RuleCheckingOutputType.SCORE));
114+
try {
115+
RuleCheckerOutput output2 = (RuleCheckerOutput) output.clone();
116+
results.put(getHeader(RuleCheckingOutputType.STATUS), output.setOutputType(RuleCheckingOutputType.STATUS));
117+
results.put(getHeader(RuleCheckingOutputType.SCORE), output2.setOutputType(RuleCheckingOutputType.SCORE));
118+
} catch (CloneNotSupportedException e) {
119+
e.printStackTrace(System.err);
120+
}
96121
}
97122
}
98123

@@ -111,4 +136,12 @@ public void setDebug() {
111136
public boolean isDebug() {
112137
return debug;
113138
}
139+
140+
public Boolean countInstances() {
141+
return countInstances;
142+
}
143+
144+
public void setCountInstances(Boolean countInstances) {
145+
this.countInstances = countInstances;
146+
}
114147
}

src/main/java/de/gwdg/metadataqa/api/rule/RuleCheckerOutput.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ public class RuleCheckerOutput {
44
private final RuleCheckingOutputStatus status;
55
private RuleCheckingOutputType outputType;
66
private Integer score = 0;
7+
private Integer instanceCount;
8+
private Integer failureCount;
79

810
public RuleCheckerOutput(RuleChecker ruleChecker, boolean isNA, boolean passed) {
911
this(ruleChecker, RuleCheckingOutputStatus.create(isNA, passed));
@@ -32,13 +34,38 @@ public Integer getScore() {
3234
return score;
3335
}
3436

37+
public Integer getInstanceCount() {
38+
return instanceCount;
39+
}
40+
41+
public RuleCheckerOutput setInstanceCount(Integer instanceCount) {
42+
this.instanceCount = instanceCount;
43+
return this;
44+
}
45+
46+
public Integer getFailureCount() {
47+
return failureCount;
48+
}
49+
50+
public RuleCheckerOutput setFailureCount(Integer failureCount) {
51+
this.failureCount = failureCount;
52+
return this;
53+
}
54+
3555
public String toString() {
36-
return outputType.equals(RuleCheckingOutputType.STATUS)
56+
return outputType != null && outputType.equals(RuleCheckingOutputType.STATUS)
3757
? status.asString()
3858
: score == null
3959
? "0" : score.toString();
4060
}
4161

62+
@Override
63+
protected Object clone() throws CloneNotSupportedException {
64+
return new RuleCheckerOutput(status, score)
65+
.setInstanceCount(instanceCount)
66+
.setFailureCount(failureCount);
67+
}
68+
4269
public RuleCheckerOutput setOutputType(RuleCheckingOutputType outputType) {
4370
this.outputType = outputType;
4471
return this;

src/main/java/de/gwdg/metadataqa/api/rule/singlefieldchecker/ContentTypeChecker.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,28 +36,36 @@ public void update(Selector cache, FieldCounter<RuleCheckerOutput> results, Rule
3636

3737
var allPassed = true;
3838
var isNA = true;
39+
int instanceCount = 0;
40+
int failureCount = 0;
3941
List<XmlFieldInstance> instances = cache.get(field.getPath());
4042
if (instances != null && !instances.isEmpty()) {
4143
for (XmlFieldInstance instance : instances) {
4244
if (instance.hasValue()) {
45+
if (countInstances())
46+
instanceCount++;
4347
isNA = false;
4448
try {
4549
String contentType = ContentTypeExtractor.getContentType(instance.getValue());
4650
if (isDebug())
4751
LOGGER.info(String.format("value: '%s' -> '%s'", instance.getValue(), contentType));
4852
if (contentType == null || !fixedValues.contains(contentType)) {
4953
allPassed = false;
54+
if (countInstances())
55+
failureCount++;
5056
}
5157
} catch (IOException e) {
5258
allPassed = false;
59+
if (countInstances())
60+
failureCount++;
5361
}
54-
if (!allPassed)
62+
if (!countInstances() && !allPassed)
5563
break;
5664
}
5765
}
5866
}
5967

60-
addOutput(results, isNA, allPassed, outputType);
68+
addOutput(results, isNA, allPassed, outputType, instanceCount, failureCount);
6169
if (isDebug())
6270
LOGGER.info(this.getClass().getSimpleName() + " " + this.id + ") result: " + RuleCheckingOutputStatus.create(isNA, allPassed));
6371
}

src/test/java/de/gwdg/metadataqa/api/rule/singlefieldchecker/ContentTypeCheckerTest.java

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.List;
2828

2929
import static org.junit.Assert.assertEquals;
30+
import static org.junit.Assert.assertFalse;
3031

3132
public class ContentTypeCheckerTest extends CheckerTestBase {
3233

@@ -41,15 +42,44 @@ public void success() {
4142
"\"https://iiif.deutsche-digitale-bibliothek.de/image/2/ec863e48-7e20-4e9c-95fd-babd708b6eaf/full/full/0/default.jpg\"");
4243
cache.setCsvReader(new CsvReader().setHeader( ((CsvAwareSchema) schema).getHeader() ));
4344

44-
ContentTypeChecker checker = new ContentTypeChecker(schema.getPathByLabel("name"),
45-
Arrays.asList("image/jpeg", "image/png", "image/tiff", "image/tiff-fx", "image/gif", "image/svg+xml", "application/pdf"));
45+
ContentTypeChecker checker = new ContentTypeChecker(
46+
schema.getPathByLabel("name"),
47+
Arrays.asList("image/jpeg", "image/png", "image/tiff", "image/tiff-fx", "image/gif", "image/svg+xml", "application/pdf")
48+
);
49+
assertFalse(checker.countInstances());
50+
51+
FieldCounter<RuleCheckerOutput> fieldCounter = new FieldCounter<>();
52+
checker.update(cache, fieldCounter, RuleCheckingOutputType.BOTH);
53+
54+
assertEquals(2, fieldCounter.size());
55+
assertEquals("name:contentType", checker.getHeaderWithoutId());
56+
Assert.assertEquals(RuleCheckingOutputStatus.PASSED, fieldCounter.get(checker.getHeader(RuleCheckingOutputType.STATUS)).getStatus());
57+
Assert.assertNull(fieldCounter.get(checker.getHeader(RuleCheckingOutputType.SCORE)).getScore());
58+
Assert.assertEquals(0, (int) fieldCounter.get(checker.getHeader(RuleCheckingOutputType.SCORE)).getInstanceCount());
59+
Assert.assertEquals(0, (int) fieldCounter.get(checker.getHeader(RuleCheckingOutputType.SCORE)).getFailureCount());
60+
}
61+
62+
@Test
63+
public void success_withCountInstance() {
64+
cache = (CsvSelector) SelectorFactory.getInstance(schema.getFormat(),
65+
"\"https://iiif.deutsche-digitale-bibliothek.de/image/2/ec863e48-7e20-4e9c-95fd-babd708b6eaf/full/full/0/default.jpg\"");
66+
cache.setCsvReader(new CsvReader().setHeader( ((CsvAwareSchema) schema).getHeader() ));
67+
68+
ContentTypeChecker checker = new ContentTypeChecker(
69+
schema.getPathByLabel("name"),
70+
Arrays.asList("image/jpeg", "image/png", "image/tiff", "image/tiff-fx", "image/gif", "image/svg+xml", "application/pdf")
71+
);
72+
checker.setCountInstances(true);
4673

4774
FieldCounter<RuleCheckerOutput> fieldCounter = new FieldCounter<>();
4875
checker.update(cache, fieldCounter, RuleCheckingOutputType.BOTH);
4976

5077
assertEquals(2, fieldCounter.size());
5178
assertEquals("name:contentType", checker.getHeaderWithoutId());
5279
Assert.assertEquals(RuleCheckingOutputStatus.PASSED, fieldCounter.get(checker.getHeader(RuleCheckingOutputType.STATUS)).getStatus());
80+
Assert.assertNull(fieldCounter.get(checker.getHeader(RuleCheckingOutputType.SCORE)).getScore());
81+
Assert.assertEquals(1, (int) fieldCounter.get(checker.getHeader(RuleCheckingOutputType.SCORE)).getInstanceCount());
82+
Assert.assertEquals(0, (int) fieldCounter.get(checker.getHeader(RuleCheckingOutputType.SCORE)).getFailureCount());
5383
}
5484

5585
@Test

0 commit comments

Comments
 (0)