Skip to content

Commit

Permalink
Improve dependency checker, JSON outout of schema
Browse files Browse the repository at this point in the history
  • Loading branch information
pkiraly committed Dec 11, 2024
1 parent da4fb10 commit 94033ce
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 10 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<groupId>de.gwdg.metadataqa</groupId>
<artifactId>metadata-qa-api</artifactId>
<packaging>jar</packaging>
<version>0.9.6-SNAPSHOT</version>
<version>0.9.7-SNAPSHOT</version>
<name>Metadata Quality Assurance Framework API</name>
<description>
A metadata quality assurance framework. It checks some metrics of
Expand Down
1 change: 1 addition & 0 deletions src/main/java/de/gwdg/metadataqa/api/json/DataElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ public DataElement setChildren(List<DataElement> children) {
return this;
}

@JsonIgnore
public boolean isCollection() {
return collection;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,10 @@ else if (outputType.equals(RuleCheckingOutputType.SCORE))
return header + ":" + getId() + suffix;
}

protected void addOutput(FieldCounter<RuleCheckerOutput> results, boolean isNA, boolean allPassed, RuleCheckingOutputType outputType) {
protected void addOutput(FieldCounter<RuleCheckerOutput> results,
boolean isNA,
boolean allPassed,
RuleCheckingOutputType outputType) {
addOutput(results, isNA, allPassed, outputType, null, null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,12 @@ public void update(Selector cache, FieldCounter<RuleCheckerOutput> results, Rule
MinCountChecker minCountChecker = (MinCountChecker) checker;
if (!minCountChecker.isEmptyInstancesAllowed() || minCountChecker.getMinCount() > 0)
allPassed = false;
} else if (checker instanceof DependencyChecker) {
DependencyChecker dependencyChecker = (DependencyChecker) checker;
allPassed = dependencyChecker.getResult(outputType, results);
}
if (!allPassed)
break;
}
}
addOutput(results, isNA, allPassed, outputType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ public DependencyChecker(DataElement field, List<String> dependencies) {
this(field, field.getLabel(), dependencies, RuleCheckingOutputStatus.FAILED);
}

public DependencyChecker(DataElement field, List<String> dependencies, RuleCheckingOutputStatus failedDepencencyStatus) {
public DependencyChecker(DataElement field,
List<String> dependencies,
RuleCheckingOutputStatus failedDepencencyStatus) {
this(field, field.getLabel(), dependencies, failedDepencencyStatus);
}

Expand All @@ -42,7 +44,9 @@ public void update(Selector cache, FieldCounter<RuleCheckerOutput> results, Rule
update(cache, results, outputType, null);
}

public void update(Selector cache, FieldCounter<RuleCheckerOutput> localResults, RuleCheckingOutputType outputType,
public void update(Selector cache,
FieldCounter<RuleCheckerOutput> localResults,
RuleCheckingOutputType outputType,
FieldCounter<RuleCheckerOutput> globalResults) {
if (isDebug())
LOGGER.info(this.getClass().getSimpleName() + " " + this.id);
Expand Down Expand Up @@ -82,4 +86,31 @@ public void update(Selector cache, FieldCounter<RuleCheckerOutput> localResults,
if (isDebug())
LOGGER.info(this.getClass().getSimpleName() + " " + this.id + ") result: " + RuleCheckingOutputStatus.create(isNA, allPassed));
}

public List<String> getDependencies() {
return dependencies;
}

public boolean getResult(RuleCheckingOutputType outputType,
FieldCounter<RuleCheckerOutput> globalResults) {
boolean allPassed = true;
for (String ruleId : dependencies) {
String keyEnd = outputType.equals(RuleCheckingOutputType.BOTH) ? ruleId + ":status" : ruleId;
boolean found = false;
for (Map.Entry<String, RuleCheckerOutput> entry : globalResults.getMap().entrySet()) {
if (entry.getKey().endsWith(keyEnd)) {
found = true;
if (entry.getValue().getStatus().equals(RuleCheckingOutputStatus.FAILED)) {
allPassed = false;
break;
}
}
}
if (!found) {
allPassed = false;
break;
}
}
return allPassed;
}
}
2 changes: 1 addition & 1 deletion src/test/java/de/gwdg/metadataqa/api/cli/VersionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

public class VersionTest {

private final String EXPECTED_VERSION = "0.9.6-SNAPSHOT";
private final String EXPECTED_VERSION = "0.9.7-SNAPSHOT";

@Test
public void getVersion() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,36 @@ public void failure() {

assertEquals(RuleCheckingOutputStatus.FAILED, fieldCounter.get(andChecker.getHeader(RuleCheckingOutputType.STATUS)).getStatus());
}

@Test
public void withDependency() {
schema.getPathByLabel("name")
.setRule(Arrays.asList(
new Rule().withAnd(Arrays.asList(
new Rule().withMinCount(1),
new Rule().withMaxCount(1)))
.withNaScore(-1)
.withId("Q1"),
new Rule().withAnd(Arrays.asList(
new Rule().withDependencies(List.of("Q1")),
new Rule().withHasValue("a")))
.withId("Q2")
));

cache = (CsvSelector) SelectorFactory.getInstance(schema.getFormat(), ",b,a");
cache.setCsvReader(new CsvReader().setHeader(((CsvAwareSchema) schema).getHeader()));

FieldCounter<RuleCheckerOutput> fieldCounter = new FieldCounter<>();
for (RuleChecker checker : schema.getRuleCheckers()) {
checker.update(cache, fieldCounter, RuleCheckingOutputType.STATUS);
}
System.err.println(fieldCounter);
// FieldCounter{fieldMap={
// name:and:name:minCount:name:maxCount:Q1:status=0,
// name:and:name:minCount:name:maxCount:Q1:score=0,
// name:and:name:dependency:name:hasValue:Q2:status=0,
// name:and:name:dependency:name:hasValue:Q2:score=0
// }}

}
}
10 changes: 5 additions & 5 deletions src/test/java/de/gwdg/metadataqa/api/schema/BaseSchemaTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -924,17 +924,17 @@ public void toJson() {
assertTrue(schemaString.contains("\"name\":\"author\""));
assertTrue(schemaString.contains("\"path\":\"author\""));
assertTrue(schemaString.contains("\"categories\":[]"));
// assertTrue(schemaString.contains("\"children\":[]"));
assertTrue(schemaString.contains("\"collection\":false"));
assertFalse(schemaString.contains("\"children\":[]"));
assertFalse(schemaString.contains("\"collection\":false"));
assertTrue(schemaString.contains("\"hasValue\":\"a\""));
assertTrue(schemaString.contains("\"hidden\":false"));
assertTrue(schemaString.contains("\"skip\":false"));
assertTrue(schemaString.contains("\"debug\":false"));
assertTrue(schemaString.contains("\"allowEmptyInstances\":false"));
// assertTrue(schemaString.contains("\"absolutePath\":\"author\""));
// assertTrue(schemaString.contains("\"active\":true"));
assertFalse(schemaString.contains("\"absolutePath\":\"author\""));
assertFalse(schemaString.contains("\"active\":true"));
assertTrue(schemaString.contains("\"extractable\":false"));
// assertTrue(schemaString.contains("\"mandatory\":false"));
assertFalse(schemaString.contains("\"mandatory\":false"));
assertTrue(schemaString.contains("\"rules\":["));
assertTrue(schemaString.contains("\"groups\":[]"));
assertTrue(schemaString.contains("\"categories\":[]"));
Expand Down

0 comments on commit 94033ce

Please sign in to comment.