Add URDNA2015 normalization algorithm implementation by dinhwe2612 · Pull Request #344 · jsonld-java/jsonld-java · GitHub
Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 57 additions & 10 deletions core/src/main/java/com/github/jsonldjava/core/JsonLdApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -2185,15 +2185,50 @@ public RDFDataset toRDF() throws JsonLdError {
* If there was an error while normalizing.
*/
public Object normalize(Map<String, Object> dataset) throws JsonLdError {
return normalize(dataset, opts);
}

/**
* Performs RDF normalization on the given JSON-LD input.
*
* @param dataset
* the expanded JSON-LD object to normalize.
* @param options
* the JSON-LD options containing the algorithm to use.
* @return The normalized JSON-LD object
* @throws JsonLdError
* If there was an error while normalizing.
*/
public Object normalize(Map<String, Object> dataset, JsonLdOptions options) throws JsonLdError {
if (options.getAlgorithm().equals(JsonLdOptions.URGNA2012)) {
return normalizeURGN2012(dataset);
} else if (options.getAlgorithm().equals(JsonLdOptions.URDNA2015)) {
return normalizeURDNA2015(dataset, options);
}

return null;
}

/**
* Normalizes using the URGNA2012 algorithm.
*
* @param dataset
* the expanded JSON-LD object to normalize.
* @return The normalized JSON-LD object
* @throws JsonLdError
* If there was an error while normalizing.
*/
public Object normalizeURGN2012(Map<String, Object> dataset) throws JsonLdError {
// create quads and map bnodes to their associated quads
final List<Object> quads = new ArrayList<Object>();
final Map<String, Object> bnodes = newMap();

for (String graphName : dataset.keySet()) {
final List<Map<String, Object>> triples = (List<Map<String, Object>>) dataset
.get(graphName);
final List<Map<String, Object>> triples = (List<Map<String, Object>>) dataset.get(graphName);
if (JsonLdConsts.DEFAULT.equals(graphName)) {
graphName = null;
}

for (final Map<String, Object> quad : triples) {
if (graphName != null) {
if (graphName.indexOf("_:") == 0) {
Expand All @@ -2212,28 +2247,40 @@ public Object normalize(Map<String, Object> dataset) throws JsonLdError {

final String[] attrs = new String[] { "subject", "object", "name" };
for (final String attr : attrs) {
if (quad.containsKey(attr) && "blank node"
.equals(((Map<String, Object>) quad.get(attr)).get("type"))) {
final String id = (String) ((Map<String, Object>) quad.get(attr))
.get("value");
if (quad.containsKey(attr)
&& "blank node".equals(((Map<String, Object>) quad.get(attr)).get("type"))) {
final String id = (String) ((Map<String, Object>) quad.get(attr)).get("value");
if (!bnodes.containsKey(id)) {
bnodes.put(id, new LinkedHashMap<String, List<Object>>() {
{
put("quads", new ArrayList<Object>());
}
});
}
((List<Object>) ((Map<String, Object>) bnodes.get(id)).get("quads"))
.add(quad);
((List<Object>) ((Map<String, Object>) bnodes.get(id)).get("quads")).add(quad);
}
}
}
}

// mapping complete, start canonical naming
final NormalizeUtils normalizeUtils = new NormalizeUtils(quads, bnodes,
new UniqueNamer("_:c14n"), opts);
final NormalizeUtils normalizeUtils = new NormalizeUtils(quads, bnodes, new UniqueNamer("_:c14n"), opts);
return normalizeUtils.hashBlankNodes(bnodes.keySet());
}

/**
* Normalizes using the URDNA2015 algorithm.
*
* @param dataset
* the expanded JSON-LD object to normalize.
* @param options
* the JSON-LD options to use.
* @return The normalized JSON-LD object
* @throws JsonLdError
* If there was an error while normalizing.
*/
public Object normalizeURDNA2015(Map<String, Object> dataset, JsonLdOptions options) throws JsonLdError {
return new Urdna2015(dataset, options).normalize();
}

}
16 changes: 16 additions & 0 deletions core/src/main/java/com/github/jsonldjava/core/JsonLdOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ public class JsonLdOptions {

public static final boolean DEFAULT_COMPACT_ARRAYS = true;

// Normalization algorithm constants
public static final String URGNA2012 = "URGNA2012";
public static final String URDNA2015 = "URDNA2015";

/**
* Constructs an instance of JsonLdOptions using an empty base.
*/
Expand Down Expand Up @@ -61,6 +65,7 @@ public JsonLdOptions copy() {
copy.setUseRdfType(useRdfType);
copy.setUseNativeTypes(useNativeTypes);
copy.setProduceGeneralizedRdf(produceGeneralizedRdf);
copy.setAlgorithm(algorithm);
copy.format = format;
copy.useNamespaces = useNamespaces;
copy.outputForm = outputForm;
Expand Down Expand Up @@ -110,6 +115,9 @@ public JsonLdOptions copy() {
Boolean useNativeTypes = false;
private boolean produceGeneralizedRdf = false;

// Normalization algorithm option
private String algorithm = URGNA2012;

public String getEmbed() {
switch (this.embed) {
case ALWAYS:
Expand Down Expand Up @@ -294,6 +302,14 @@ public void setDocumentLoader(DocumentLoader documentLoader) {
this.documentLoader = documentLoader;
}

public String getAlgorithm() {
return algorithm;
}

public void setAlgorithm(String algorithm) {
this.algorithm = algorithm;
}

// TODO: THE FOLLOWING ONLY EXIST SO I DON'T HAVE TO DELETE A LOT OF CODE,
// REMOVE IT WHEN DONE
public String format = null;
Expand Down
Loading