Skip to content

Commit

Permalink
Merge pull request #68 from aodn/fix-more-bugs-ardc-process
Browse files Browse the repository at this point in the history
Fix ardc bug and update tests
  • Loading branch information
utas-raymondng authored Apr 29, 2024
2 parents f912355 + f6a3e9e commit 11fb960
Show file tree
Hide file tree
Showing 5 changed files with 811 additions and 295 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,34 @@ protected Map<String, List<CategoryVocabModel>> getLeafNodeOfParameterCategory(S
return result;
}

protected CategoryVocabModel buildCategoryVocabModel(JsonNode currentNode, JsonNode outerNode) {
if (currentNode instanceof ObjectNode objectNode) {
if (objectNode.has("prefLabel") && objectNode.has("_about")) {
return CategoryVocabModel.builder()
.about(about.apply(currentNode))
.label(label.apply(currentNode))
.build();
}
} else if (currentNode instanceof TextNode textNode) {
if (textNode.asText().contains("parameter_classes")) {
return CategoryVocabModel.builder()
.about(textNode.asText())
.label(this.findLabelByAbout(outerNode, textNode.asText()))
.build();
}
}
return null;
}

protected String findLabelByAbout(JsonNode node, String c) {
for (JsonNode item : node.get("items")) {
if (about.apply(item).contains(c)) {
return label.apply(item);
}
}
return null;
}

public List<CategoryVocabModel> getParameterCategory(String vocabApiBase) {
Map<String, List<CategoryVocabModel>> leaves = getLeafNodeOfParameterCategory(vocabApiBase);
List<CategoryVocabModel> result = new ArrayList<>();
Expand All @@ -123,46 +151,25 @@ public List<CategoryVocabModel> getParameterCategory(String vocabApiBase) {
List<CategoryVocabModel> broader = new ArrayList<>();
List<CategoryVocabModel> narrower = new ArrayList<>();


log.debug("Processing label {}", label.apply(j));

if (j.has("broader")) {
for (JsonNode b : j.get("broader")) {
CategoryVocabModel c = null;
if (b instanceof ObjectNode objectNode) {
if (objectNode.has("prefLabel") && objectNode.has("_about")) {
c = CategoryVocabModel
.builder()
.about(about.apply(b))
.label(label.apply(b))
.build();
}
}
if (b instanceof TextNode textNode && textNode.asText().contains("parameter_classes")) {
c = CategoryVocabModel.builder()
.about(textNode.asText())
.build();
}
broader.add(c);
broader.add(this.buildCategoryVocabModel(b, node));
}
}

if (j.has("narrower")) {
for (JsonNode b : j.get("narrower")) {
if (b.has("prefLabel") && b.has("_about")) {
CategoryVocabModel c = CategoryVocabModel
.builder()
.about(about.apply(b))
.label(label.apply(b))
.build();

narrower.add(c);

// The record comes from ardc have two levels only, so the second level for sure
// is empty, but the third level info comes form another link (aka the leaves)
// and therefore we can attach it to the second level to for the third.
if(leaves.containsKey(about.apply(b))) {
c.setNarrower(leaves.get(about.apply(b)));
}
CategoryVocabModel c = this.buildCategoryVocabModel(b, node);
// The record comes from ardc have two levels only, so the second level for sure
// is empty, but the third level info comes form another link (aka the leaves)
// and therefore we can attach it to the second level to for the third.
if(leaves.containsKey(c.getAbout())) {
c.setNarrower(leaves.get(c.getAbout()));
}
narrower.add(c);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,26 +56,60 @@ public void verifyGetParameterCategory() throws IOException {
List<CategoryVocabModel> categoryVocabModelList = ardcVocabsService.getParameterCategory("");
assertEquals("Total equals", 33, categoryVocabModelList.size());

Optional<CategoryVocabModel> m = categoryVocabModelList

Optional<CategoryVocabModel> c = categoryVocabModelList
.stream()
.filter(p -> p.getBroader().isEmpty() && !p.getNarrower().isEmpty() && p.getLabel().equals("Chemical"))
.findFirst();

assertTrue("Find target Chemical", c.isPresent());
assertEquals("Have narrower equals", 5, c.get().getNarrower().size());


Optional<CategoryVocabModel> b = categoryVocabModelList
.stream()
.filter(p -> !p.getNarrower().isEmpty() && p.getLabel().equals("Physical-Atmosphere"))
.filter(p -> p.getBroader().isEmpty() && !p.getNarrower().isEmpty() && p.getLabel().equals("Biological"))
.findFirst();

assertTrue("Find target Physical-Atmosphere", m.isPresent());
assertEquals("Have narrower equals", 6, m.get().getNarrower().size());
assertTrue("Find target Biological", b.isPresent());
assertEquals("Have narrower equals", 5, b.get().getNarrower().size());


Optional<CategoryVocabModel> visibility = m.get().getNarrower()
Optional<CategoryVocabModel> pa = categoryVocabModelList
.stream()
.filter(p -> p.getBroader().isEmpty() && !p.getNarrower().isEmpty() && p.getLabel().equals("Physical-Atmosphere"))
.findFirst();

assertTrue("Find target Physical-Atmosphere", pa.isPresent());
assertEquals("Have narrower equals", 8, pa.get().getNarrower().size());

Optional<CategoryVocabModel> airTemperature = pa.get().getNarrower()
.stream()
.filter(p -> p.getLabel().equals("Air temperature"))
.findFirst();
assertTrue("Find target Air temperature", airTemperature.isPresent());

Optional<CategoryVocabModel> visibility = pa.get().getNarrower()
.stream()
.filter(p -> p.getLabel().equals("Visibility"))
.findFirst();

assertTrue("Find target Visibility", visibility.isPresent());

Optional<CategoryVocabModel> airSeaLevel = visibility.get().getNarrower()
Optional<CategoryVocabModel> horizontalVisibilityInTheAtmosphere = visibility.get().getNarrower()
.stream()
.filter(p -> p.getLabel().equals("Horizontal visibility in the atmosphere"))
.findFirst();

assertTrue("Horizontal visibility in the atmosphere found", airSeaLevel.isPresent());
assertTrue("Horizontal visibility in the atmosphere found", horizontalVisibilityInTheAtmosphere.isPresent());

Optional<CategoryVocabModel> pw = categoryVocabModelList
.stream()
.filter(p -> p.getBroader().isEmpty() && !p.getNarrower().isEmpty() && p.getLabel().equals("Physical-Water"))
.findFirst();

assertTrue("Find target Physical-Water", pw.isPresent());
assertEquals("Have narrower equals", 14, pw.get().getNarrower().size());

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ void testGetAodnDiscoveryCategories() throws IOException {
assertTrue(categories.contains("alkalinity"));
assertTrue(categories.contains("temperature"));
assertTrue(categories.contains("salinity"));
assertEquals(3, categories.size());
assertTrue(categories.contains("carbon"));
assertEquals(4, categories.size());
}
}
Loading

0 comments on commit 11fb960

Please sign in to comment.