Skip to content

Commit

Permalink
[Enhancement #54] Ensure extended term definition is correctly create…
Browse files Browse the repository at this point in the history
…d for collection-valued object property attributes.
  • Loading branch information
ledsoft committed Mar 31, 2023
1 parent 76982fb commit ebf773a
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 10 deletions.
12 changes: 12 additions & 0 deletions src/main/java/cz/cvut/kbss/jsonld/common/BeanClassProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,18 @@ public static void verifyPropertiesFieldType(Field field) {
}
}

/**
* Checks whether the specified class represents an individual reference and not a complex object.
*
* Individual references are identifiers or enum constants mapped to individuals.
* @param cls Class to check
* @return {@code true} when the type represents an individual, {@code false} otherwise
* @see #isIdentifierType(Class)
*/
public static boolean isIndividualType(Class<?> cls) {
return isIdentifierType(cls) || cls.isEnum();
}

/**
* Checks whether the specified type is a valid identifier type.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
import cz.cvut.kbss.jsonld.ConfigParam;
import cz.cvut.kbss.jsonld.Configuration;
import cz.cvut.kbss.jsonld.JsonLd;
import cz.cvut.kbss.jsonld.common.BeanClassProcessor;
import cz.cvut.kbss.jsonld.serialization.model.JsonNode;
import cz.cvut.kbss.jsonld.serialization.serializer.SerializerUtils;
import cz.cvut.kbss.jsonld.serialization.serializer.compact.ObjectPropertyValueSerializer;
import cz.cvut.kbss.jsonld.serialization.traversal.ObjectGraphTraverser;
import cz.cvut.kbss.jsonld.serialization.traversal.SerializationContext;

import java.util.Collection;

public class ContextBuildingObjectPropertyValueSerializer extends ObjectPropertyValueSerializer {

private boolean serializeUsingExtendedDefinition;
Expand All @@ -26,7 +29,7 @@ public JsonNode serialize(Object value, SerializationContext ctx) {
}

private void registerTermDefinition(SerializationContext<?> ctx) {
if (serializeUsingExtendedDefinition) {
if (serializeUsingExtendedDefinition && isIndividual(ctx)) {
ctx.registerTermMapping(ctx.getFieldName(),
SerializerUtils.createTypedTermDefinition(ctx.getFieldName(), ctx.getTerm(),
JsonLd.ID));
Expand All @@ -35,6 +38,21 @@ private void registerTermDefinition(SerializationContext<?> ctx) {
}
}

private static boolean isIndividual(SerializationContext<?> ctx) {
if (BeanClassProcessor.isIndividualType(ctx.getValue().getClass())) {
return true;
}
if (ctx.getValue() instanceof Collection) {
final Collection<?> c = (Collection<?>) ctx.getValue();
for (Object elem : c) {
if (elem != null) {
return BeanClassProcessor.isIndividualType(elem.getClass());
}
}
}
return false;
}

@Override
public void configure(Configuration config) {
this.serializeUsingExtendedDefinition = config.is(ConfigParam.SERIALIZE_INDIVIDUALS_USING_EXPANDED_DEFINITION);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ void traverseSingular(SerializationContext<?> ctx) {
if (!shouldTraverse) {
return;
}
if (isIndividual(ctx)) {
if (BeanClassProcessor.isIndividualType(ctx.getValue().getClass())) {
visitIndividual(ctx);
return;
}
Expand All @@ -100,10 +100,6 @@ void traverseSingular(SerializationContext<?> ctx) {
closeInstance(ctx);
}

private static boolean isIndividual(SerializationContext<?> ctx) {
return BeanClassProcessor.isIdentifierType(ctx.getValue().getClass()) || ctx.getValue().getClass().isEnum();
}

private void serializeFields(SerializationContext<?> ctx) {
final Object instance = ctx.getValue();
final List<Field> fieldsToSerialize =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.net.URI;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

Expand All @@ -42,6 +47,23 @@ void serializeRegistersTermIriMappingInJsonLdContext() throws Exception {

@Test
void serializeRegistersExtendedTermDefinitionWithIdAndTypeInJsonLdContextWhenConfiguredToUseExtendedDefinition() throws Exception {
final Configuration config = new Configuration();
config.set(ConfigParam.SERIALIZE_INDIVIDUALS_USING_EXPANDED_DEFINITION, Boolean.TRUE.toString());
sut.configure(config);
final JsonLdContext ctx = mock(JsonLdContext.class);
final URI value = Generator.generateUri();
final SerializationContext<URI> serializationCtx =
new SerializationContext<>(Vocabulary.ORIGIN, Organization.class.getDeclaredField("country"), value,
ctx);

sut.serialize(value, serializationCtx);
verify(ctx).registerTermMapping("country",
SerializerUtils.createTypedTermDefinition("country", Vocabulary.ORIGIN,
JsonLd.ID));
}

@Test
void serializeRegistersTermIriMappingInJsonLdContextWhenConfiguredToUseExtendedDefinitionWhenValueIsComplex() throws Exception {
final Configuration config = new Configuration();
config.set(ConfigParam.SERIALIZE_INDIVIDUALS_USING_EXPANDED_DEFINITION, Boolean.TRUE.toString());
sut.configure(config);
Expand All @@ -51,9 +73,23 @@ void serializeRegistersExtendedTermDefinitionWithIdAndTypeInJsonLdContextWhenCon
new SerializationContext<>(Vocabulary.IS_MEMBER_OF, Employee.getEmployerField(), value, ctx);

sut.serialize(value, serializationCtx);
verify(ctx).registerTermMapping(Employee.getEmployerField().getName(),
SerializerUtils.createTypedTermDefinition(Employee.getEmployerField().getName(),
Vocabulary.IS_MEMBER_OF,
JsonLd.ID));
verify(ctx).registerTermMapping(Employee.getEmployerField().getName(), Vocabulary.IS_MEMBER_OF);
}

@Test
void serializeRegistersTermIriMappingInJsonLdContextWhenConfiguredToUseExtendedDefinitionWhenValueIsCollectionOfComplexObjects() throws Exception {
final Configuration config = new Configuration();
config.set(ConfigParam.SERIALIZE_INDIVIDUALS_USING_EXPANDED_DEFINITION, Boolean.TRUE.toString());
sut.configure(config);
final JsonLdContext ctx = mock(JsonLdContext.class);
final Set<URI> value = new HashSet<>(Arrays.asList(Generator.generateUri(), Generator.generateUri()));
final SerializationContext<Set<URI>> serializationCtx =
new SerializationContext<>(Vocabulary.HAS_MEMBER, Organization.getEmployeesField(), value, ctx);

sut.serialize(value, serializationCtx);
verify(ctx).registerTermMapping(Organization.getEmployeesField().getName(),
SerializerUtils.createTypedTermDefinition(
Organization.getEmployeesField().getName(), Vocabulary.HAS_MEMBER,
JsonLd.ID));
}
}

0 comments on commit ebf773a

Please sign in to comment.