Skip to content

Feature/issue-40 #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Jun 25, 2020
19 changes: 14 additions & 5 deletions src/json-ld.net/Core/JsonLdApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -430,8 +430,7 @@ public virtual JToken Compact(Context activeCtx, string activeProperty, JToken e
/// <returns></returns>
/// <exception cref="JsonLdError">JsonLdError</exception>
/// <exception cref="JsonLD.Core.JsonLdError"></exception>
public virtual JToken Expand(Context activeCtx, string activeProperty, JToken element
)
public virtual JToken Expand(Context activeCtx, string activeProperty, JToken element)
{
// 1)
if (element.IsNull())
Expand Down Expand Up @@ -1405,7 +1404,7 @@ public virtual JArray Frame(JToken input, JArray frame)
{
state.omitDefault = this.opts.GetOmitDefault().Value;
}
// use tree map so keys are sotred by default
// use tree map so keys are sorted by default
// XXX BUG BUG BUG XXX (sblom) Figure out where this needs to be sorted and use extension methods to return sorted enumerators or something!
JObject nodes = new JObject();
GenerateNodeMap(input, nodes);
Expand Down Expand Up @@ -2116,7 +2115,12 @@ public virtual JArray FromRDF(RDFDataset dataset)
JArray result = new JArray();
// 6)
JArray ids = new JArray(defaultGraph.GetKeys());
ids.SortInPlace();

if (opts.GetSortGraphsFromRdf())
{
ids.SortInPlace();
}

foreach (string subject_1 in ids)
{
JsonLdApi.NodeMapNode node = (NodeMapNode)defaultGraph[subject_1];
Expand All @@ -2127,7 +2131,12 @@ public virtual JArray FromRDF(RDFDataset dataset)
node["@graph"] = new JArray();
// 6.1.2)
JArray keys = new JArray(graphMap[subject_1].GetKeys());
keys.SortInPlace();

if (opts.GetSortGraphNodesFromRdf())
{
keys.SortInPlace();
}

foreach (string s in keys)
{
JsonLdApi.NodeMapNode n = (NodeMapNode)graphMap[subject_1][s];
Expand Down
23 changes: 22 additions & 1 deletion src/json-ld.net/Core/JsonLdOptions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using JsonLD.Core;
using Newtonsoft.Json.Linq;

namespace JsonLD.Core
Expand Down Expand Up @@ -43,6 +42,9 @@ public virtual JsonLD.Core.JsonLdOptions Clone()

private bool produceGeneralizedRdf = false;

private bool sortGraphsFromRdf = true;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since I've only been able to control the order with the FromRDF method (which luckily is all that's needed to get the desired behavior), I made the option pretty narrow in scope.


private bool sortGraphNodesFromRdf = true;
// base options
// frame options
// rdf conversion options
Expand Down Expand Up @@ -147,6 +149,25 @@ public virtual void SetProduceGeneralizedRdf(bool produceGeneralizedRdf)
this.produceGeneralizedRdf = produceGeneralizedRdf;
}

public virtual bool GetSortGraphsFromRdf()
{
return sortGraphsFromRdf;
}

public virtual void SetSortGraphsFromRdf(bool sortGraphs)
{
this.sortGraphsFromRdf = sortGraphs;
}

public virtual bool GetSortGraphNodesFromRdf()
{
return sortGraphNodesFromRdf;
}

public virtual void SetSortGraphNodesFromRdf(bool sortGraphNodes)
{
this.sortGraphNodesFromRdf = sortGraphNodes;
}
public string format = null;

public bool useNamespaces = false;
Expand Down
3 changes: 1 addition & 2 deletions src/json-ld.net/Core/JsonLdProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ namespace JsonLD.Core
public class JsonLdProcessor
{
/// <exception cref="JsonLD.Core.JsonLdError"></exception>
public static JObject Compact(JToken input, JToken context, JsonLdOptions
opts)
public static JObject Compact(JToken input, JToken context, JsonLdOptions opts)
{
// 1)
// TODO: look into java futures/promises
Expand Down
1 change: 1 addition & 0 deletions src/json-ld.net/Core/NormalizeUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ public virtual object HashBlankNodes(IEnumerable<string> unnamed_)
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();
// handle output format
Expand Down
2 changes: 1 addition & 1 deletion src/json-ld.net/json-ld.net.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<Description>JSON-LD processor for .NET

Implements the W3C JSON-LD 1.0 standard.</Description>
<VersionPrefix>1.0.6</VersionPrefix>
<VersionPrefix>1.0.7</VersionPrefix>
<Authors>NuGet;linked-data-dotnet</Authors>
<TargetFrameworks>netstandard1.3;netstandard2.0;netcoreapp2.1</TargetFrameworks>
<AssemblyName>json-ld.net</AssemblyName>
Expand Down
47 changes: 12 additions & 35 deletions test/json-ld.net.tests/ConformanceTests.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Newtonsoft.Json.Linq;
using Xunit;
using Xunit.Extensions;
using System.IO;
using Newtonsoft.Json;
using JsonLD.Core;
using JsonLD.Util;

Expand All @@ -16,7 +13,7 @@ namespace JsonLD.Test
public class ConformanceTests
{
[Theory, ClassData(typeof(ConformanceCases))]
public void ConformanceTestPasses(string id, string testname, ConformanceCase conformanceCase)
public void ConformanceTestPasses(string id, ConformanceCase conformanceCase)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The testname variable was never used, so removing it had no impact

{
JToken result = conformanceCase.run();
if (conformanceCase.error != null)
Expand Down Expand Up @@ -77,20 +74,21 @@ public ConformanceCases()

public IEnumerator<object[]> GetEnumerator()
{
var jsonFetcher = new JsonFetcher();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I created the JsonFetcher class so I could reuse some of this logic in my own test class

var rootDirectory = "W3C";

foreach (string manifest in manifests)
{
JToken manifestJson;

manifestJson = GetJson(manifest);
JToken manifestJson = jsonFetcher.GetJson(manifest, rootDirectory);

foreach (JObject testcase in manifestJson["sequence"])
{
Func<JToken> run;
ConformanceCase newCase = new ConformanceCase();

newCase.input = GetJson(testcase["input"]);
newCase.context = GetJson(testcase["context"]);
newCase.frame = GetJson(testcase["frame"]);
newCase.input = jsonFetcher.GetJson(testcase["input"], rootDirectory);
newCase.context = jsonFetcher.GetJson(testcase["context"], rootDirectory);
newCase.frame = jsonFetcher.GetJson(testcase["frame"], rootDirectory);

var options = new JsonLdOptions("http://json-ld.org/test-suite/tests/" + (string)testcase["input"]);

Expand All @@ -109,11 +107,11 @@ public IEnumerator<object[]> GetEnumerator()
else if (testType.Any((s) => (string)s == "jld:FromRDFTest"))
{
newCase.input = File.ReadAllText(Path.Combine("W3C", (string)testcase["input"]));
newCase.output = GetJson(testcase["expect"]);
newCase.output = jsonFetcher.GetJson(testcase["expect"], rootDirectory);
}
else
{
newCase.output = GetJson(testcase["expect"]);
newCase.output = jsonFetcher.GetJson(testcase["expect"], rootDirectory);
}
}
else
Expand All @@ -138,7 +136,7 @@ public IEnumerator<object[]> GetEnumerator()
}
if (optionDescription.TryGetValue("expandContext", out value))
{
newCase.context = GetJson(testcase["option"]["expandContext"]);
newCase.context = jsonFetcher.GetJson(testcase["option"]["expandContext"], rootDirectory);
options.SetExpandContext((JObject)newCase.context);
}
if (optionDescription.TryGetValue("produceGeneralizedRdf", out value))
Expand Down Expand Up @@ -227,32 +225,11 @@ public IEnumerator<object[]> GetEnumerator()

newCase.run = run;

yield return new object[] { manifest + (string)testcase["@id"], (string)testcase["name"], newCase };
yield return new object[] { manifest + (string)testcase["@id"], newCase };
}
}
}

private JToken GetJson(JToken j)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved to the JsonFetcher class

{
try
{
if (j == null || j.Type == JTokenType.Null) return null;
using (Stream manifestStream = File.OpenRead(Path.Combine("W3C", (string)j)))
using (TextReader reader = new StreamReader(manifestStream))
using (JsonReader jreader = new Newtonsoft.Json.JsonTextReader(reader)
{
DateParseHandling = DateParseHandling.None
})
{
return JToken.ReadFrom(jreader);
}
}
catch (Exception e)
{ // TODO: this should not be here, figure out why this is needed or catch specific exception.
return null;
}
}

System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
throw new Exception("auggh");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"quads": [
{
"graph": "http://example.org/node/3",
"subject": "http://example.org/object/3",
"predicate": "http://example.org/value",
"value": "n3-o3-value"
},
{
"graph": "http://example.org/node/3",
"subject": "http://example.org/object/1",
"predicate": "http://example.org/value",
"value": "n3-o1-value"
},
{
"graph": "http://example.org/node/3",
"subject": "http://example.org/object/2",
"predicate": "http://example.org/value",
"value": "n3-o2-value"
},
{
"graph": "http://example.org/node/1",
"subject": "http://example.org/object/3",
"predicate": "http://example.org/value",
"value": "n1-o3-value"
},
{
"graph": "http://example.org/node/1",
"subject": "http://example.org/object/1",
"predicate": "http://example.org/value",
"value": "n1-o1-value"
},
{
"graph": "http://example.org/node/1",
"subject": "http://example.org/object/2",
"predicate": "http://example.org/value",
"value": "n1-o2-value"
},
{
"graph": "http://example.org/node/2",
"subject": "http://example.org/object/3",
"predicate": "http://example.org/value",
"value": "n2-o3-value"
},
{
"graph": "http://example.org/node/2",
"subject": "http://example.org/object/1",
"predicate": "http://example.org/value",
"value": "n2-o1-value"
},
{
"graph": "http://example.org/node/2",
"subject": "http://example.org/object/2",
"predicate": "http://example.org/value",
"value": "n2-o2-value"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"@type": "mf:Manifest",
"name": "From RDF",
"description": "JSON-LD sorting graphs and nodes when running FromRDF",
"input": "fromRdf-in.json",
"sequence": [
{
"@id": "#t0001",
"sort-type": "jld:GraphsAndNodes",
"test-type": "jld:FromRDF",
"name": "sort graphs and nodes",
"purpose": "graphs and nodes sorted when running FromRDF",
"expect": "fromRdf-out-sort-graphs-and-nodes.jsonld"
},
{
"@id": "#t0002",
"sort-type": "jld:Graphs",
"test-type": "jld:FromRDF",
"name": "sort graphs only",
"purpose": "graphs sorted when running FromRDF",
"expect": "fromRdf-out-sort-graphs.jsonld"
},
{
"@id": "#t0003",
"sort-type": "jld:Nodes",
"test-type": "jld:FromRDF",
"name": "sort graph nodes only",
"purpose": "graph nodes sorted when running FromRDF",
"expect": "fromRdf-out-sort-graph-nodes.jsonld"
},
{
"@id": "#t0004",
"sort-type": "jld:None",
"test-type": "jld:FromRDF",
"name": "sort nothing",
"purpose": "sort nothing running FromRDF",
"expect": "fromRdf-out-no-sorting.jsonld"
}
]
}
Loading