Skip to content

Commit

Permalink
[Enhancement #43] Support chaining JSON-LD contexts into hierarchy.
Browse files Browse the repository at this point in the history
  • Loading branch information
ledsoft committed Feb 7, 2023
1 parent 91131d8 commit 37ad993
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ public class MappingJsonLdContext implements JsonLdContext {

private final Map<String, JsonNode> mapping = new HashMap<>();

private final JsonLdContext parentContext;

public MappingJsonLdContext() {
this.parentContext = new DummyJsonLdContext();
}

public MappingJsonLdContext(JsonLdContext parentContext) {
this.parentContext = parentContext;
}

/**
* Registers the specified term mapping in this context.
* <p>
Expand Down Expand Up @@ -67,12 +77,12 @@ Map<String, JsonNode> getMapping() {
}

public Optional<JsonNode> getTermMapping(String term) {
return Optional.ofNullable(mapping.get(term));
return mapping.containsKey(term) ? Optional.of(mapping.get(term)) : parentContext.getTermMapping(term);
}

@Override
public boolean hasTermMapping(String term) {
return mapping.containsKey(term);
return mapping.containsKey(term) || parentContext.hasTermMapping(term);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package cz.cvut.kbss.jsonld.serialization.context;

import cz.cvut.kbss.jopa.vocabulary.DC;
import cz.cvut.kbss.jopa.vocabulary.RDFS;
import cz.cvut.kbss.jopa.vocabulary.XSD;
import cz.cvut.kbss.jsonld.JsonLd;
import cz.cvut.kbss.jsonld.environment.Generator;
import cz.cvut.kbss.jsonld.serialization.JsonNodeFactory;
import cz.cvut.kbss.jsonld.serialization.model.JsonNode;
import cz.cvut.kbss.jsonld.serialization.serializer.SerializerUtils;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -43,4 +46,31 @@ void getMappedTermReturnsEmptyOptionalWhenNoMatchingMappingIsRegistered() {
assertNotNull(result);
assertFalse(result.isPresent());
}

@Test
void getTermMappingRetrievesMappingFromParentContextWhenAvailable() {
sut.registerTermMapping("id", JsonLd.ID);
final MappingJsonLdContext currentSut = new MappingJsonLdContext(sut);
final Optional<JsonNode> result = currentSut.getTermMapping("id");
assertTrue(result.isPresent());
assertEquals(JsonNodeFactory.createStringLiteralNode("id", JsonLd.ID), result.get());
}

@Test
void getTermMappingReturnsMappingFromCurrentContextThatOverridesParentMapping() {
final String term = "name";
sut.registerTermMapping(term, RDFS.LABEL);
final MappingJsonLdContext currentSut = new MappingJsonLdContext(sut);
currentSut.registerTermMapping(term, DC.Terms.TITLE);
final Optional<JsonNode> result = currentSut.getTermMapping(term);
assertTrue(result.isPresent());
assertEquals(JsonNodeFactory.createStringLiteralNode(term, DC.Terms.TITLE), result.get());
}

@Test
void hasTermMappingReturnsTrueWhenTermIsMappedInParentContext() {
sut.registerTermMapping("id", JsonLd.ID);
final MappingJsonLdContext currentSut = new MappingJsonLdContext(sut);
assertTrue(currentSut.hasTermMapping("id"));
}
}

0 comments on commit 37ad993

Please sign in to comment.