Skip to content

Commit

Permalink
Conformance cases pass for ToRDF, FromRDF, and mostly pass for Normal…
Browse files Browse the repository at this point in the history
…ize.
  • Loading branch information
sblom committed Mar 15, 2014
1 parent 74446a3 commit 7dc9bec
Show file tree
Hide file tree
Showing 18 changed files with 220 additions and 132 deletions.
6 changes: 6 additions & 0 deletions JsonLD.sln
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{BADA6F
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JsonLD.Test", "tests\JsonLD.Test\JsonLD.Test.csproj", "{D41C622A-4470-4BE1-AB20-D9862B628840}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JsonLD.Portable", "JsonLD.Portable\JsonLD.Portable.csproj", "{F119722F-5E6A-4479-A4CF-0CE00FF29737}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -28,6 +30,10 @@ Global
{D41C622A-4470-4BE1-AB20-D9862B628840}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D41C622A-4470-4BE1-AB20-D9862B628840}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D41C622A-4470-4BE1-AB20-D9862B628840}.Release|Any CPU.Build.0 = Release|Any CPU
{F119722F-5E6A-4479-A4CF-0CE00FF29737}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F119722F-5E6A-4479-A4CF-0CE00FF29737}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F119722F-5E6A-4479-A4CF-0CE00FF29737}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F119722F-5E6A-4479-A4CF-0CE00FF29737}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
5 changes: 4 additions & 1 deletion src/JsonLD/Core/Context.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ namespace JsonLD.Core
/// </summary>
/// <author>tristan</author>
//[System.Serializable]
public class Context : JObject, ICloneable
public class Context : JObject
#if !PORTABLE
, ICloneable
#endif
{
private JsonLdOptions options;

Expand Down
4 changes: 4 additions & 0 deletions src/JsonLD/Core/DocumentLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class DocumentLoader
/// <exception cref="JsonLDNet.Core.JsonLdError"></exception>
public virtual RemoteDocument LoadDocument(string url)
{
#if !PORTABLE
RemoteDocument doc = new RemoteDocument(url, null);
try
{
Expand Down Expand Up @@ -58,6 +59,9 @@ public virtual RemoteDocument LoadDocument(string url)
throw new JsonLdError(JsonLdError.Error.LoadingDocumentFailed, url);
}
return doc;
#else
throw new NotImplementedException();
#endif
}

/// <summary>An HTTP Accept header that prefers JSONLD.</summary>
Expand Down
2 changes: 1 addition & 1 deletion src/JsonLD/Core/IJSONLDTripleCallback.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ public interface IJSONLDTripleCallback
/// used).
/// </param>
/// <returns>the resulting RDF object in the desired format</returns>
JToken Call(RDFDataset dataset);
object Call(RDFDataset dataset);
}
}
41 changes: 16 additions & 25 deletions src/JsonLD/Core/JsonLdApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1944,7 +1944,7 @@ public virtual JArray FromRDF(RDFDataset dataset)
// 3/3.1)
foreach (string name in dataset.GraphNames())
{
JArray graph = dataset.GetQuads(name);
IList<RDFDataset.Quad> graph = dataset.GetQuads(name);
// 3.2+3.4)
JObject nodeMap;
if (!graphMap.ContainsKey(name))
Expand Down Expand Up @@ -2139,40 +2139,41 @@ public virtual RDFDataset ToRDF()
return dataset;
}

#if !PORTABLE
/// <summary>Performs RDF normalization on the given JSON-LD input.</summary>
/// <remarks>Performs RDF normalization on the given JSON-LD input.</remarks>
/// <param name="input">the expanded JSON-LD object to normalize.</param>
/// <param name="options">the normalization options.</param>
/// <param name="callback">(err, normalized) called once the operation completes.</param>
/// <exception cref="JSONLDProcessingError">JSONLDProcessingError</exception>
/// <exception cref="JsonLD.Core.JsonLdError"></exception>
public virtual JToken Normalize(JObject dataset)
public virtual object Normalize(RDFDataset dataset)
{
// create quads and map bnodes to their associated quads
JArray quads = new JArray();
JObject bnodes = new JObject();
foreach (string graphName in dataset.GetKeys())
IList<RDFDataset.Quad> quads = new List<RDFDataset.Quad>();
IDictionary<string,IDictionary<string,object>> bnodes = new Dictionary<string,IDictionary<string,object>>();
foreach (string graphName in dataset.Keys)
{
var eachGraphName = graphName;
JArray triples = (JArray)dataset[eachGraphName];
IList<RDFDataset.Quad> triples = (IList<RDFDataset.Quad>)dataset[eachGraphName];
if ("@default".Equals(eachGraphName))
{
eachGraphName = null;
}
foreach (JObject quad in triples)
foreach (RDFDataset.Quad quad in triples)
{
if (eachGraphName != null)
{
if (eachGraphName.IndexOf("_:") == 0)
{
JObject tmp = new JObject();
IDictionary<string, object> tmp = new Dictionary<string, object>();
tmp["type"] = "blank node";
tmp["value"] = eachGraphName;
quad["name"] = tmp;
}
else
{
JObject tmp = new JObject();
IDictionary<string, object> tmp = new Dictionary<string, object>();
tmp["type"] = "IRI";
tmp["value"] = eachGraphName;
quad["name"] = tmp;
Expand All @@ -2182,33 +2183,23 @@ public virtual JToken Normalize(JObject dataset)
string[] attrs = new string[] { "subject", "object", "name" };
foreach (string attr in attrs)
{
if (quad.ContainsKey(attr) && ((JObject)quad
[attr])["type"].SafeCompare("blank node"))
if (quad.ContainsKey(attr) && (string)((IDictionary<string,object>)quad[attr])["type"] == "blank node")
{
string id = (string)((JObject)quad[attr])["value"];
string id = (string)((IDictionary<string,object>)quad[attr])["value"];
if (!bnodes.ContainsKey(id))
{
bnodes[id] = new _Dictionary_1910();
bnodes[id] = new Dictionary<string,object> { {"quads", new List<RDFDataset.Quad>()} };
}
((JArray)((JObject)bnodes[id])["quads"]).Add(quad);
((IList<RDFDataset.Quad>)bnodes[id]["quads"]).Add(quad);
}
}
}
}
// mapping complete, start canonical naming
NormalizeUtils normalizeUtils = new NormalizeUtils(quads, bnodes, new UniqueNamer
("_:c14n"), opts);
return normalizeUtils.HashBlankNodes(bnodes.GetKeys());
}

private sealed class _Dictionary_1910 : JObject
{
public _Dictionary_1910()
{
{
this["quads"] = new JArray();
}
}
return normalizeUtils.HashBlankNodes(bnodes.Keys);
}
#endif
}
}
15 changes: 9 additions & 6 deletions src/JsonLD/Core/JsonLdProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ public static JToken FromRDF(JToken input, IRDFParser parser)
/// <?></?>
/// <param name="callback">(err, dataset) called once the operation completes.</param>
/// <exception cref="JsonLDNet.Core.JsonLdError"></exception>
public static JToken ToRDF(JToken input, IJSONLDTripleCallback callback, JsonLdOptions
public static object ToRDF(JToken input, IJSONLDTripleCallback callback, JsonLdOptions
options)
{
JToken expandedInput = Expand(input, options);
Expand Down Expand Up @@ -438,23 +438,24 @@ public static JToken FromRDF(JToken input, IRDFParser parser)
}

/// <exception cref="JsonLD.Core.JsonLdError"></exception>
public static JToken ToRDF(JToken input, JsonLdOptions options)
public static object ToRDF(JToken input, JsonLdOptions options)
{
return ToRDF(input, null, options);
}

/// <exception cref="JsonLD.Core.JsonLdError"></exception>
public static JToken ToRDF(JToken input, IJSONLDTripleCallback callback)
public static object ToRDF(JToken input, IJSONLDTripleCallback callback)
{
return ToRDF(input, callback, new JsonLdOptions(string.Empty));
}

/// <exception cref="JsonLD.Core.JsonLdError"></exception>
public static JToken ToRDF(JToken input)
public static object ToRDF(JToken input)
{
return ToRDF(input, new JsonLdOptions(string.Empty));
}

#if !PORTABLE
/// <summary>Performs RDF dataset normalization on the given JSON-LD input.</summary>
/// <remarks>
/// Performs RDF dataset normalization on the given JSON-LD input. The output
Expand All @@ -465,7 +466,7 @@ public static JToken ToRDF(JToken input)
/// <param name="callback">(err, normalized) called once the operation completes.</param>
/// <exception cref="JSONLDProcessingError">JSONLDProcessingError</exception>
/// <exception cref="JsonLDNet.Core.JsonLdError"></exception>
public static JToken Normalize(JToken input, JsonLdOptions options)
public static object Normalize(JToken input, JsonLdOptions options)
{
JsonLdOptions opts = options.Clone();
opts.format = null;
Expand All @@ -474,9 +475,11 @@ public static JToken Normalize(JToken input, JsonLdOptions options)
}

/// <exception cref="JsonLD.Core.JsonLdError"></exception>
public static JToken Normalize(JToken input)
public static object Normalize(JToken input)
{
return Normalize(input, new JsonLdOptions(string.Empty));
}

#endif
}
}
59 changes: 29 additions & 30 deletions src/JsonLD/Core/NormalizeUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@

namespace JsonLD.Core
{
#if !PORTABLE
internal class NormalizeUtils
{
private readonly UniqueNamer namer;

private readonly JObject bnodes;
private readonly IDictionary<string,IDictionary<string,object>> bnodes;

private readonly JArray quads;
private readonly IList<RDFDataset.Quad> quads;

private readonly JsonLdOptions options;

public NormalizeUtils(JArray quads, JObject bnodes, UniqueNamer
public NormalizeUtils(IList<RDFDataset.Quad> quads, IDictionary<string, IDictionary<string, object>> bnodes, UniqueNamer
namer, JsonLdOptions options)
{
this.options = options;
Expand All @@ -27,7 +28,7 @@ internal class NormalizeUtils

// generates unique and duplicate hashes for bnodes
/// <exception cref="JsonLD.Core.JsonLdError"></exception>
public virtual JToken HashBlankNodes(IEnumerable<string> unnamed_)
public virtual object HashBlankNodes(IEnumerable<string> unnamed_)
{
IList<string> unnamed = new List<string>(unnamed_);
IList<string> nextUnnamed = new List<string>();
Expand Down Expand Up @@ -91,22 +92,21 @@ public virtual JToken HashBlankNodes(IEnumerable<string> unnamed_)
// update bnode names in each quad and serialize
for (int cai = 0; cai < quads.Count; ++cai)
{
JObject quad = (JObject)quads[cai];
RDFDataset.Quad quad = quads[cai];
foreach (string attr in new string[] { "subject", "object", "name" })
{
if (quad.ContainsKey(attr))
{
JObject qa = (JObject)quad[attr];
if (qa != null && qa["type"].SafeCompare("blank node") && ((string)qa["value"]).IndexOf
IDictionary<string,object> qa = (IDictionary<string,object>)quad[attr];
if (qa != null && (string)qa["type"] == "blank node" && ((string)qa["value"]).IndexOf
("_:c14n") != 0)
{
qa["value"] = namer.GetName((string)qa[("value")]);
qa["value"] = namer.GetName((string)qa["value"]);
}
}
}
normalized.Add(RDFDatasetUtils.ToNQuad((RDFDataset.Quad)quad, quad.ContainsKey("name"
) && !quad["name"].IsNull() ? (string)((JObject)quad["name"])[
"value"] : null));
normalized.Add(RDFDatasetUtils.ToNQuad(quad, quad.ContainsKey("name"
) && !(quad["name"] == null) ? (string)((IDictionary<string,object>)((IDictionary<string,object>)quad)["name"])["value"] : null));
}
// sort normalized output
normalized.SortInPlace();
Expand Down Expand Up @@ -239,22 +239,22 @@ private class HashResult
/// <param name="namer">the canonical bnode namer.</param>
/// <param name="pathNamer">the namer used to assign names to adjacent bnodes.</param>
/// <param name="callback">(err, result) called once the operation completes.</param>
private static NormalizeUtils.HashResult HashPaths(string id, JObject bnodes, UniqueNamer namer, UniqueNamer pathNamer)
private static NormalizeUtils.HashResult HashPaths(string id, IDictionary<string, IDictionary<string, object>> bnodes, UniqueNamer namer, UniqueNamer pathNamer)
{
try
{
// create SHA-1 digest
MessageDigest md = MessageDigest.GetInstance("SHA-1");
JObject groups = new JObject();
IList<string> groupHashes;
JArray quads = (JArray)((JObject)bnodes[id])["quads"];
IList<RDFDataset.Quad> quads = (IList<RDFDataset.Quad>)bnodes[id]["quads"];
for (int hpi = 0; ; hpi++)
{
if (hpi == quads.Count)
{
// done , hash groups
groupHashes = new List<string>(groups.GetKeys());
groupHashes.SortInPlace();
((List<string>)groupHashes).Sort(StringComparer.CurrentCultureIgnoreCase);
for (int hgi = 0; ; hgi++)
{
if (hgi == groupHashes.Count)
Expand Down Expand Up @@ -381,8 +381,8 @@ private static NormalizeUtils.HashResult HashPaths(string id, JObject bnodes, Un
}
}
// get adjacent bnode
JObject quad = (JObject)quads[hpi];
string bnode_2 = GetAdjacentBlankNodeName((JObject)quad["subject"
IDictionary<string,object> quad = (IDictionary<string,object>)quads[hpi];
string bnode_2 = GetAdjacentBlankNodeName((IDictionary<string, object>)quad["subject"
], id);
string direction = null;
if (bnode_2 != null)
Expand All @@ -392,7 +392,7 @@ private static NormalizeUtils.HashResult HashPaths(string id, JObject bnodes, Un
}
else
{
bnode_2 = GetAdjacentBlankNodeName((JObject)quad["object"], id
bnode_2 = GetAdjacentBlankNodeName((IDictionary<string, object>)quad["object"], id
);
if (bnode_2 != null)
{
Expand Down Expand Up @@ -424,7 +424,7 @@ private static NormalizeUtils.HashResult HashPaths(string id, JObject bnodes, Un
// String toHash = direction + (String) ((Map<String,
// Object>) quad.get("predicate")).get("value") + name;
md1.Update(JsonLD.JavaCompat.GetBytesForString(direction, "UTF-8"));
md1.Update(JsonLD.JavaCompat.GetBytesForString(((string)((JObject)quad["predicate"])["value"]), "UTF-8"));
md1.Update(JsonLD.JavaCompat.GetBytesForString(((string)((IDictionary<string,object>)quad["predicate"])["value"]), "UTF-8"));
md1.Update(JsonLD.JavaCompat.GetBytesForString(name, "UTF-8"));
string groupHash = EncodeHex(md1.Digest());
if (groups.ContainsKey(groupHash))
Expand Down Expand Up @@ -455,28 +455,27 @@ private static NormalizeUtils.HashResult HashPaths(string id, JObject bnodes, Un
/// <param name="bnodes">the mapping of bnodes to quads.</param>
/// <param name="namer">the canonical bnode namer.</param>
/// <returns>the new hash.</returns>
private static string HashQuads(string id, JObject bnodes, UniqueNamer
private static string HashQuads(string id, IDictionary<string, IDictionary<string, object>> bnodes, UniqueNamer
namer)
{
// return cached hash
if (((JObject)bnodes[id]).ContainsKey("hash"))
if (bnodes[id].ContainsKey("hash"))
{
return (string)((JObject)bnodes[id])["hash"];
return (string)bnodes[id]["hash"];
}
// serialize all of bnode's quads
JArray quads = (JArray)((JObject)bnodes[id])["quads"];
IList<RDFDataset.Quad> quads = (IList<RDFDataset.Quad>)bnodes[id]["quads"];
IList<string> nquads = new List<string>();
for (int i = 0; i < quads.Count; ++i)
{
nquads.Add(RDFDatasetUtils.ToNQuad((RDFDataset.Quad)quads[i], quads[i]["name"] !=
null ? (string)((JObject)quads[i]["name"])["value"] : null,
id));
object name;
nquads.Add(RDFDatasetUtils.ToNQuad((RDFDataset.Quad)quads[i], quads[i].TryGetValue("name", out name) ? (string)((IDictionary<string,object>)name)["value"] : null, id));
}
// sort serialized quads
nquads.SortInPlace();
// return hashed quads
string hash = Sha1hash(nquads);
((JObject)bnodes[id])["hash"] = hash;
((IDictionary<string,object>)bnodes[id])["hash"] = hash;
return hash;
}

Expand Down Expand Up @@ -511,7 +510,7 @@ private static string EncodeHex(byte[] data)
string rval = string.Empty;
foreach (byte b in data)
{
rval += string.Format("%02x", b);
rval += b.ToString("x2");
}
return rval;
}
Expand All @@ -528,10 +527,9 @@ private static string EncodeHex(byte[] data)
/// <param name="node">the RDF quad node.</param>
/// <param name="id">the ID of the blank node to look next to.</param>
/// <returns>the adjacent blank node name or null if none was found.</returns>
private static string GetAdjacentBlankNodeName(JObject node,
string id)
private static string GetAdjacentBlankNodeName(IDictionary<string,object> node, string id)
{
return node["type"].SafeCompare("blank node") && (!node.ContainsKey("value") || !node["value"].SafeCompare(id)) ? (string)node["value"] : null;
return (string)node["type"] == "blank node" && (!node.ContainsKey("value") || (string)node["value"] == id) ? (string)node["value"] : null;
}

private class Permutator
Expand Down Expand Up @@ -614,4 +612,5 @@ public virtual JArray Next()
}
}
}
#endif
}

0 comments on commit 7dc9bec

Please sign in to comment.