Skip to content

Commit

Permalink
Merge pull request #280 from kbss-cvut/279-rdf-core-types
Browse files Browse the repository at this point in the history
Implement CoreDataTypes resolving
  • Loading branch information
blcham authored Sep 23, 2024
2 parents 9a92cae + 611af1f commit b8c8d36
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,14 @@ private String generateFrame() {
.map(n -> {
if (isResourceByColumn(n)) {
return String.format("\"%s\": { \"@id\": \"%s%s\", \"@type\": \"@id\" }", n, S_PIPES, n);
}else{
String type = getPrimitiveTypeForColumn(n);
if(JsonLdCoreLiteralDatatypes.isCorePrimitiveType(type)){
return String.format("\"%s\": { \"@id\": \"%s%s\" }", n, S_PIPES, n);
}
return String.format("\"%s\": { \"@id\": \"%s%s\", \"@type\": \"%s\" }", n, S_PIPES, n, type);
}
return String.format("\"%s\": { \"@id\": \"%s%s\" }", n, S_PIPES, n);

})
.collect(Collectors.joining(",\n ")) + ",";

Expand Down Expand Up @@ -161,13 +167,12 @@ private String getType(RDFNode node) {
}
}


private String getPrimitiveTypeForColumn(String column) {
return evidences.stream()
.findAny()
.filter(e -> !e.get(column).isResource())
.map(e -> getType(e.get(column)))
/* filter xsd:string values as it breaks JSON-LD framing algorithm */
.filter(type -> !type.equals("http://www.w3.org/2001/XMLSchema#string"))
.orElse(null);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package cz.cvut.spipes.rest.handler;

import java.util.List;

/**
* Represents XSD datatypes used in JSON-LD without requiring explicit "@type" declaration.
**/
public class JsonLdCoreLiteralDatatypes {

public static final String XSD_NAMESPACE = "http://www.w3.org/2001/XMLSchema#";

public static final String XSD_STRING = XSD_NAMESPACE + "string";
public static final String XSD_INTEGER = XSD_NAMESPACE + "integer";
public static final String XSD_BOOLEAN = XSD_NAMESPACE + "boolean";
public static final String XSD_DOUBLE = XSD_NAMESPACE + "double";

public static final List<String> CORE_TYPES = List.of(XSD_STRING, XSD_INTEGER, XSD_BOOLEAN, XSD_DOUBLE);

public static boolean isCorePrimitiveType(String type) {
if(type == null) {
return false;
}
return CORE_TYPES.contains(type);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package cz.cvut.spipes.rest.handler;

import org.junit.jupiter.api.Test;

import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

public class JsonLdCoreLiteralDatatypesTest {

@Test
public void testConstants() {
assertEquals("http://www.w3.org/2001/XMLSchema#string", JsonLdCoreLiteralDatatypes.XSD_STRING);
assertEquals("http://www.w3.org/2001/XMLSchema#integer", JsonLdCoreLiteralDatatypes.XSD_INTEGER);
assertEquals("http://www.w3.org/2001/XMLSchema#boolean", JsonLdCoreLiteralDatatypes.XSD_BOOLEAN);
assertEquals("http://www.w3.org/2001/XMLSchema#double", JsonLdCoreLiteralDatatypes.XSD_DOUBLE);
}

@Test
public void testCoreTypesList() {
List<String> expectedCoreTypes = List.of(
"http://www.w3.org/2001/XMLSchema#string",
"http://www.w3.org/2001/XMLSchema#integer",
"http://www.w3.org/2001/XMLSchema#boolean",
"http://www.w3.org/2001/XMLSchema#double"
);
assertEquals(expectedCoreTypes, JsonLdCoreLiteralDatatypes.CORE_TYPES);
}

@Test
public void testIsCorePrimitiveTypeWithValidTypes() {
assertTrue(JsonLdCoreLiteralDatatypes.isCorePrimitiveType(JsonLdCoreLiteralDatatypes.XSD_STRING));
assertTrue(JsonLdCoreLiteralDatatypes.isCorePrimitiveType(JsonLdCoreLiteralDatatypes.XSD_INTEGER));
assertTrue(JsonLdCoreLiteralDatatypes.isCorePrimitiveType(JsonLdCoreLiteralDatatypes.XSD_BOOLEAN));
assertTrue(JsonLdCoreLiteralDatatypes.isCorePrimitiveType(JsonLdCoreLiteralDatatypes.XSD_DOUBLE));
}

@Test
public void testIsCorePrimitiveTypeWithInvalidTypes() {
assertFalse(JsonLdCoreLiteralDatatypes.isCorePrimitiveType("http://www.w3.org/2001/XMLSchema#nonexistent"));
assertFalse(JsonLdCoreLiteralDatatypes.isCorePrimitiveType("http://www.w3.org/2001/XMLSchema#date"));
assertFalse(JsonLdCoreLiteralDatatypes.isCorePrimitiveType(""));
assertFalse(JsonLdCoreLiteralDatatypes.isCorePrimitiveType(null));
}

@Test
public void testIsCorePrimitiveTypeWithEdgeCases() {
assertFalse(JsonLdCoreLiteralDatatypes.isCorePrimitiveType(""));
assertFalse(JsonLdCoreLiteralDatatypes.isCorePrimitiveType(null));
}
}

0 comments on commit b8c8d36

Please sign in to comment.