/// <summary>Expands given JSON-LD string.</summary>
        /// <param name="json">JSON-LD string to be expanded.</param>
        /// <param name="options">Processing options.</param>
        /// <returns><see cref="System.String" /> with expanded JSON-LD data if the <paramref name="json" /> is not null; otherwise <b>null</b>.</returns>
        public string Expand(string json, JsonLdOptions options)
        {
            JToken result = null;
            if (json != null)
            {
                JToken data = (JToken)JsonConvert.DeserializeObject(json);
                IList<string> remoteContexts = new List<string>();
                Context activeContext = new Context() { BaseIri = options.BaseUri.ToString(), DocumentUri = options.BaseUri };
                if (!System.String.IsNullOrEmpty(options.ExpandContext))
                {
                    ExpandContext(activeContext, (JToken)JsonConvert.DeserializeObject(options.ExpandContext), remoteContexts);
                }

                result = Expand(null, data, activeContext, remoteContexts);
            }

            JObject @object = result as JObject;
            if ((@object != null) && (@object.IsPropertySet(Graph)) && (@object.PropertyCount() == 1))
            {
                result = @object[Graph];
            }

            if (result == null)
            {
                result = new JArray();
            }

            if ((result != null) && (!(result is JArray)))
            {
                result = new JArray(result);
            }

            return JsonConvert.SerializeObject(result);
        }
Beispiel #2
0
        /// <summary>Expands given JSON-LD string.</summary>
        /// <param name="json">JSON-LD string to be expanded.</param>
        /// <param name="options">Processing options.</param>
        /// <returns><see cref="System.String" /> with expanded JSON-LD data if the <paramref name="json" /> is not null; otherwise <b>null</b>.</returns>
        public string Expand(string json, JsonLdOptions options)
        {
            JToken result = null;

            if (json != null)
            {
                JToken         data           = (JToken)JsonConvert.DeserializeObject(json);
                IList <string> remoteContexts = new List <string>();
                Context        activeContext  = new Context()
                {
                    BaseIri = (options.BaseUri != null ? options.BaseUri.ToString() : System.String.Empty), DocumentUri = options.BaseUri
                };
                if (!System.String.IsNullOrEmpty(options.ExpandContext))
                {
                    ExpandContext(activeContext, (JToken)JsonConvert.DeserializeObject(options.ExpandContext), remoteContexts);
                }

                result = Expand(null, data, activeContext, remoteContexts);
            }

            JObject @object = result as JObject;

            if ((@object != null) && (@object.IsPropertySet(Graph)) && (@object.PropertyCount() == 1))
            {
                result = @object[Graph];
            }

            if (result == null)
            {
                result = new JArray();
            }

            if ((result != null) && (!(result is JArray)))
            {
                result = new JArray(result);
            }

            return(JsonConvert.SerializeObject(result));
        }
        public void JSON_to_RDF_test_suite(string input, string expectedPath, JObject options)
        {
            // given
            string inputJson = File.ReadAllText(input);
            var jsonOptions = new JsonLdOptions() { BaseUri = new Uri("http://json-ld.org/test-suite/tests/" + System.IO.Path.GetFileName(input)) };
            Func<IEnumerable<EntityQuad>> processTestFunc = () => _processor.ToRdf(inputJson, jsonOptions);
            if (options != null)
            {
                if (options.Property("produceGeneralizedRdf") != null)
                {
                    processTestFunc = () => _processor.ToRdf(inputJson, jsonOptions, produceGeneralizedRdf: (bool)options["produceGeneralizedRdf"]);
                }
            }

            // given
            IEnumerable<EntityQuad> expectedStore = GetQuads(expectedPath, (options == null) || (options.Property("produceGeneralizedRdf") == null) || !(bool)options["produceGeneralizedRdf"]);

            // when
            IEnumerable<EntityQuad> actualStore = processTestFunc();

            // then
            Assert.That(actualStore, Is.EquivalentTo(expectedStore));
        }
        public void Expand_test_suite(string input, string expectedPath, JObject options)
        {
            // given
            string inputJson = File.ReadAllText(input);
            var jsonOptions = new JsonLdOptions() { BaseUri = new Uri("http://json-ld.org/test-suite/tests/" + System.IO.Path.GetFileName(input)) };
            Func<string> expandTestFunc = () => _processor.Expand(inputJson, jsonOptions);

            if (options != null)
            {
                if (options.Property("base") != null)
                {
                    jsonOptions.BaseUri = new Uri((string)options["base"]);
                }

                if (options.Property("expandContext") != null)
                {
                    jsonOptions.ExpandContext = File.ReadAllText(Path.Combine(_testsRoot, (string)options["expandContext"]));
                }
            }

            // when, then
            ExecuteTest(expandTestFunc, expectedPath);
        }
        // TODO: Confirm that toRdf-0009 test has a typo in the input file ('foo' instead of 'foo:').
        // TODO: Confirm that Object to RDF conversion algorithm's step 8 is unnsecessary.
        // TODO: Confirm that Object to RDF conversion algorithm misses an xsd:dateTime, xsd:date and xsd:time datatypes related steps.
        // TODO: Confirm that Node map generation algorithm should have a 'reference' instead of 'element' in the step 6.6.3.
        // TODO: Confirm that Unit test #044 should have 'http://example.org/set2' instead of second 'http://example.org/set1'.
        public IEnumerable <IEntityQuad> ToRdf(string json, JsonLdOptions options, bool produceGeneralizedRdf = false)
        {
            json = Expand(json, options);
            JObject nodeMap = new JObject();
            int     counter = 0;
            IDictionary <string, string> identifierMap = new Dictionary <string, string>();
            int counterGraphs = 0;
            IDictionary <string, string> graphMap = new Dictionary <string, string>();

            GenerateNodeMap((JToken)JsonConvert.DeserializeObject(json), nodeMap, identifierMap, ref counter);
            List <IEntityQuad> dataset = new List <IEntityQuad>();

            foreach (JProperty _graph in nodeMap.Properties().OrderBy(graph => graph.Name == Default ? 1 : 0).ThenBy(graph => graph.Name))
            {
                string  graphName = _graph.Name;
                JObject graph     = (JObject)_graph.Value;
                if ((graphName != Default) && (IsRelative(graphName)))
                {
                    continue;
                }

                List <ITriple> triples = new List <ITriple>();
                foreach (JProperty _subject in graph.Properties().OrderBy(subject => subject.Name))
                {
                    string  subject = _subject.Name;
                    JObject node    = (JObject)_subject.Value;
                    if ((!Regex.IsMatch(subject, "[a-zA-Z0-9_]+://.+")) && (!subject.StartsWith("_:")))
                    {
                        continue;
                    }

                    foreach (JProperty _property in node.Properties().OrderBy(property => property.Name))
                    {
                        string property = _property.Name;
                        if (property == Type)
                        {
                            foreach (JValue type in (JArray)_property.Value)
                            {
                                triples.Add(new Triple(CreateNode(subject), RdfType, CreateNode(type.ValueAs <string>())));
                            }
                        }
                        else if (IsKeyWord(property))
                        {
                            continue;
                        }
                        else if ((property.StartsWith("_:")) && (!produceGeneralizedRdf))
                        {
                            continue;
                        }
                        else if (IsRelative(property))
                        {
                            continue;
                        }
                        else
                        {
                            JArray values = (JArray)_property.Value;
                            foreach (JToken item in values)
                            {
                                if ((item is JObject) && (((JObject)item).IsPropertySet(List)))
                                {
                                    IList <Triple> listTriples = new List <Triple>();
                                    INode          listHead    = ConvertList((JArray)((JObject)item)[List], listTriples, identifierMap, ref counter, graphName, subject);
                                    triples.Add(new Triple(CreateNode(subject), Node.ForUri(new Uri(property)), listHead));
                                    triples.AddRange(listTriples);
                                }
                                else
                                {
                                    INode result = ConvertObject(item, ref counter, identifierMap);
                                    if (result != null)
                                    {
                                        triples.Add(new Triple(CreateNode(subject, graphName), CreateNode(property), result));
                                    }
                                }
                            }
                        }
                    }
                }

                foreach (var triple in triples)
                {
                    if (graphName == Default)
                    {
                        dataset.Add(new EntityQuad(triple.Subject.ToEntityId(), triple.Subject, triple.Predicate, triple.Object));
                    }
                    else
                    {
                        dataset.Add(new EntityQuad(
                                        new EntityId(triple.Subject.Uri),
                                        triple.Subject,
                                        triple.Predicate,
                                        triple.Object,
                                        CreateNode((IsBlankIri(graphName) ? CreateBlankNodeId(graphName, graphMap, ref counterGraphs) : graphName), graphName)));
                    }
                }
            }

            return(dataset.Distinct());
        }
        // TODO: Confirm that toRdf-0009 test has a typo in the input file ('foo' instead of 'foo:').
        // TODO: Confirm that Object to RDF conversion algorithm's step 8 is unnsecessary.
        // TODO: Confirm that Object to RDF conversion algorithm misses an xsd:dateTime, xsd:date and xsd:time datatypes related steps.
        // TODO: Confirm that Node map generation algorithm should have a 'reference' instead of 'element' in the step 6.6.3.
        // TODO: Confirm that Unit test #044 should have 'http://example.org/set2' instead of second 'http://example.org/set1'.
        public IEnumerable<EntityQuad> ToRdf(string json, JsonLdOptions options, bool produceGeneralizedRdf = false)
        {
            json = Expand(json, options);
            JObject nodeMap = new JObject();
            int counter = 0;
            IDictionary<string, string> identifierMap = new Dictionary<string, string>();
            int counterGraphs = 0;
            IDictionary<string, string> graphMap = new Dictionary<string, string>();
            GenerateNodeMap((JToken)JsonConvert.DeserializeObject(json), nodeMap, identifierMap, ref counter);
            List<EntityQuad> dataset = new List<EntityQuad>();
            foreach (JProperty _graph in nodeMap.Properties().OrderBy(graph => graph.Name == Default ? 1 : 0).ThenBy(graph => graph.Name))
            {
                string graphName = _graph.Name;
                JObject graph = (JObject)_graph.Value;
                if ((graphName != Default) && (IsRelative(graphName)))
                {
                    continue;
                }

                List<Triple> triples = new List<Triple>();
                foreach (JProperty _subject in graph.Properties().OrderBy(subject => subject.Name))
                {
                    string subject = _subject.Name;
                    JObject node = (JObject)_subject.Value;
                    if ((!Regex.IsMatch(subject, "[a-zA-Z0-9_]+://.+")) && (!subject.StartsWith("_:")))
                    {
                        continue;
                    }

                    foreach (JProperty _property in node.Properties().OrderBy(property => property.Name))
                    {
                        string property = _property.Name;
                        if (property == Type)
                        {
                            foreach (JValue type in (JArray)_property.Value)
                            {
                                triples.Add(new Triple(CreateNode(subject), RdfType, CreateNode(type.ValueAs<string>())));
                            }
                        }
                        else if (IsKeyWord(property))
                        {
                            continue;
                        }
                        else if ((property.StartsWith("_:")) && (!produceGeneralizedRdf))
                        {
                            continue;
                        }
                        else if (IsRelative(property))
                        {
                            continue;
                        }
                        else
                        {
                            JArray values = (JArray)_property.Value;
                            foreach (JToken item in values)
                            {
                                if ((item is JObject) && (((JObject)item).IsPropertySet(List)))
                                {
                                    IList<Triple> listTriples = new List<Triple>();
                                    Node listHead = ConvertList((JArray)((JObject)item)[List], listTriples, identifierMap, ref counter, graphName, subject);
                                    triples.Add(new Triple(CreateNode(subject), Node.ForUri(new Uri(property)), listHead));
                                    triples.AddRange(listTriples);
                                }
                                else
                                {
                                    Node result = ConvertObject(item, ref counter, identifierMap);
                                    if (result != null)
                                    {
                                        triples.Add(new Triple(CreateNode(subject, graphName), CreateNode(property), result));
                                    }
                                }
                            }
                        }
                    }
                }

                foreach (Triple triple in triples)
                {
                    if (graphName == Default)
                    {
                        dataset.Add(new EntityQuad(triple.Subject.ToEntityId(), triple.Subject, triple.Predicate, triple.Object));
                    }
                    else
                    {
                        dataset.Add(new EntityQuad(
                            new EntityId(triple.Subject.Uri),
                            triple.Subject,
                            triple.Predicate,
                            triple.Object,
                            CreateNode((IsBlankIri(graphName) ? CreateBlankNodeId(graphName, graphMap, ref counterGraphs) : graphName), graphName)));
                    }
                }
            }

            return dataset.Distinct();
        }