Example #1
0
        public static void Run()
        {
            var doc           = JObject.Parse(_docJson);
            var remoteContext = JObject.Parse("{'@context':'http://example.org/context.jsonld'}");
            var opts          = new JsonLdOptions {
                documentLoader = new CustomDocumentLoader()
            };
            var compacted = JsonLdProcessor.Compact(doc, remoteContext, opts);

            Console.WriteLine(compacted);

            /*
             *
             * Output:
             * {
             *  "@id": "http://example.org/ld-experts",
             *  "member": {
             *      "@type": "Person",
             *      "image": "http://manu.sporny.org/images/manu.png",
             *      "name": "Manu Sporny",
             *      "homepage": "http://manu.sporny.org/"
             *  },
             *  "name": "LD Experts"
             * }
             *
             */
        }
        public static void Run()
        {
            var doc       = JObject.Parse(_docJson);
            var frame     = JObject.Parse(_frameJson);
            var opts      = new JsonLdOptions();
            var flattened = JsonLdProcessor.Frame(doc, frame, opts);

            Console.WriteLine(flattened);

            /*
             *
             * Output:
             * {
             *  "@context": . . .,
             *  "@graph": [
             *      {
             *          "@id": "_:b0",
             *          "@type": "Person",
             *          "image": "http://manu.sporny.org/images/manu.png",
             *          "name": "Manu Sporny",
             *          "homepage": "http://manu.sporny.org/"
             *      }
             *  ]
             * }
             *
             */
        }
        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 <IEntityQuad> > processTestFunc = () => _processor.ToRdf(inputJson, jsonOptions);

            if (options != null)
            {
                if (options.Property("produceGeneralizedRdf") != null)
                {
                    processTestFunc = () => _processor.ToRdf(inputJson, jsonOptions, produceGeneralizedRdf: (bool)options["produceGeneralizedRdf"]);
                }
            }

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

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

            // then
            Assert.That(actualStore, Is.EquivalentTo(expectedStore));
        }
Example #4
0
    public static void Main(string[] args)
    {
        String sparql = File.ReadAllText("../query.sparql");
        //Console.WriteLine(sparql);
        var parameters = new Dictionary <string, string>();

        parameters["query"]  = sparql;
        parameters["format"] = "text/turtle";

        var                 client      = new HttpClient();
        StringContent       queryString = new StringContent(sparql);
        HttpResponseMessage resp        = client.PostAsync("http://virhp07.libris.kb.se/sparql", new FormUrlEncodedContent(parameters)).Result;

        Console.WriteLine("Hello Mono World" + resp.StatusCode);
        var turtle = resp.Content.ReadAsStringAsync().Result;

        Console.WriteLine(turtle);
        var options = new JsonLdOptions()
        {
            format = "text/turtle"
        };

        options.SetUseNativeTypes(true);
        var expanded    = JsonLdProcessor.FromRDF(turtle, options);
        var context     = File.ReadAllText("../context.jsonld");
        var compOptions = new JsonLdOptions();

        compOptions.SetEmbed(true);
        var compacted = JsonLdProcessor.Frame(expanded, context, compOptions);

        Console.WriteLine(turtle);
        Console.WriteLine(expanded);
        Console.WriteLine(compacted);
    }
        public static void Run()
        {
            var doc       = JObject.Parse(_docJson);
            var context   = JObject.Parse(_contextJson);
            var opts      = new JsonLdOptions();
            var flattened = JsonLdProcessor.Flatten(doc, context, opts);

            Console.WriteLine(flattened);

            /*
             *
             * Output:
             * {
             *  "@context": . . .,
             *  "@graph": [
             *      {
             *          "@id": "_:b0",
             *          "image": "http://manu.sporny.org/images/manu.png",
             *          "name": "Manu Sporny",
             *          "homepage": "http://manu.sporny.org/"
             *      },
             *      {
             *          "@id": "ld-experts",
             *          "member": {
             *              "@id": "_:b0"
             *          },
             *          "name": "LD Experts"
             *      }
             *  ]
             * }
             *
             */
        }
        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);
        }
        public static JObject Run()
        {
            var doc       = JObject.Parse(_docJson);
            var context   = JObject.Parse(_contextJson);
            var opts      = new JsonLdOptions();
            var compacted = JsonLdProcessor.Compact(doc, context, opts);

            Console.WriteLine(compacted);

            /*
             *
             * Output:
             * {
             *  "@id": "ld-experts",
             *  "member": {
             *      "image": "http://manu.sporny.org/images/manu.png",
             *      "name": "Manu Sporny",
             *      "homepage": "http://manu.sporny.org/",
             *  },
             *  "name": "LD Experts",
             *  "@context": . . .
             * }
             *
             */

            return(compacted);
        }
Example #8
0
        private JsonLdOptions PrepareJsonLdOptions()
        {
            JsonLdOptions options = new JsonLdOptions
            {
                format = "application/nquads"
            };

            return(options);
        }
        internal static void Run()
        {
            var serialized = Sample6_ToRDF.Run();
            var opts       = new JsonLdOptions();
            var jsonld     = JsonLdProcessor.FromRDF(serialized, opts);

            Console.WriteLine(jsonld);

            /*
             *
             * Output:
             * [
             *  {
             *      "@id": "_:b0",
             *      "http://schema.org/image": [
             *          {
             *                  "@id": "http://manu.sporny.org/images/manu.png"
             *          }
             *      ],
             *      "http://schema.org/name": [
             *          {
             *              "@value": "Manu Sporny"
             *          }
             *      ],
             *      "http://schema.org/url": [
             *          {
             *              "@id": "http://manu.sporny.org/"
             *          }
             *      ],
             *      "@type": [
             *          "http://schema.org/Person"
             *      ]
             *  },
             *  {
             *      "@id": "http://example.org/ld-experts",
             *      "http://schema.org/member": [
             *          {
             *              "@id": "_:b0"
             *          }
             *      ],
             *      "http://schema.org/name": [
             *          {
             *              "@value": "LD Experts"
             *          }
             *      ]
             *  }
             * ]
             */
        }
Example #10
0
        static void Main(string[] args)
        {
            var input    = GetJson("TestCases\\event-input.jsonld");
            var expected = File.ReadAllText("TestCases\\event-output.nq").Replace("\\", "");

            var options = new JsonLdOptions("http://json-ld.org/test-suite/tests/startup");

            options.format = "application/nquads";
            var result = new JValue(((string)JsonLdProcessor.ToRDF(input, options)).Replace("\n", "\r\n"));

            Console.WriteLine(JSONUtils.ToPrettyString(result));
            Console.WriteLine(JSONUtils.ToPrettyString(result) == JSONUtils.ToPrettyString(expected));
            bool isSame = JsonLdUtils.DeepCompare(result, expected);

            Console.WriteLine(JsonLdUtils.DeepCompare(result, expected));
        }
Example #11
0
        public JObject GetMoviesJObject(List <MovieResponse> movies)
        {
            var docJson = @"
            {
                '@id': 'http://example.org/movies',
                'http://schema.org/name': 'Movies',
                '@type': 'http://schema.org/ItemList',
                'http://schema.org/itemListElement': [";

            docJson = AddMovieArray(movies, docJson);

            docJson += "]}";

            var doc       = JObject.Parse(docJson.UnEscapeString());
            var context   = JObject.Parse(_moviesContextJson);
            var opts      = new JsonLdOptions();
            var compacted = JsonLdProcessor.Compact(doc, context, opts);

            return(compacted);
        }
Example #12
0
        public JObject GetMovieDetailObject(MovieDetailResponse movie)
        {
            var rating = GetRating(movie);

            var docJson = @"
            {
                '@id': 'http://example.org/movies',
                'http://schema.org/name': '" + movie.Title.EscapeString() + @"',
                '@type': 'http://schema.org/Movie',
                'http://schema.org/image': {'@id': '" + movie.Poster + @"'},
                'http://schema.org/dateCreated' : '" + movie.Year.ValidateYear() + @"',
                'http://schema.org/director': '" + movie.Director.EscapeString() + @"',
                'http://schema.org/review': {
                '@type': 'http://schema.org/Review',
                'http://schema.org/reviewRating': {
                    '@type': 'http://schema.org/Rating',
                    'http://schema.org/ratingValue': '" + rating.Value + @"'
                  },
                  'http://schema.org/author': {
                    '@type': 'http://schema.org/Person',
                    'http://schema.org/name': '" + rating.Source + @"'
                  },
                  'http://schema.org/reviewBody': '" + rating.Source + @"'
                },
                'http://schema.org/actor': [";

            docJson = AddActorsArray(movie, docJson);

            docJson += "]}";

            var doc       = JObject.Parse(docJson.UnEscapeString());
            var context   = JObject.Parse(_movieContextJson);
            var opts      = new JsonLdOptions();
            var compacted = JsonLdProcessor.Compact(doc, context, opts);

            return(compacted);
        }
        internal static string Run()
        {
            var doc  = JObject.Parse(_docJson);
            var opts = new JsonLdOptions();
            var rdf  = (RDFDataset)JsonLdProcessor.ToRDF(doc, opts);

            var serialized = RDFDatasetUtils.ToNQuads(rdf); // serialize RDF to string

            Console.WriteLine(serialized);

            /*
             *
             * Output:
             * <http://example.org/ld-experts> <http://schema.org/member> _:b0 .
             * <http://example.org/ld-experts> <http://schema.org/name> "LD Experts" .
             * _:b0 <http://schema.org/image> <http://manu.sporny.org/images/manu.png> .
             * _:b0 <http://schema.org/name> "Manu Sporny" .
             * _:b0 <http://schema.org/url> <http://manu.sporny.org/> .
             * _:b0 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://schema.org/Person> .
             *
             */

            return(serialized);
        }
Example #14
0
        internal static void Run()
        {
            var doc        = JObject.Parse(_docJson);
            var opts       = new JsonLdOptions();
            var normalized = (RDFDataset)JsonLdProcessor.Normalize(doc, opts);

            Console.WriteLine(normalized.Dump());

            /*
             *
             * Output:
             * @default
             *  subject
             *          type      blank node
             *          value     _:c14n0
             *  predicate
             *          type      IRI
             *          value     http://schema.org/image
             *  object
             *          type      IRI
             *          value     http://manu.sporny.org/images/manu.png
             *  ---
             *  subject
             *          type      blank node
             *          value     _:c14n0
             *  predicate
             *          type      IRI
             *          value     http://schema.org/name
             *  object
             *          type      literal
             *          value     Manu Sporny
             *          datatype  http://www.w3.org/2001/XMLSchema#string
             *  ---
             *  subject
             *          type      blank node
             *          value     _:c14n0
             *  predicate
             *          type      IRI
             *          value     http://schema.org/url
             *  object
             *          type      IRI
             *          value     http://manu.sporny.org/
             *  ---
             *  subject
             *          type      blank node
             *          value     _:c14n0
             *  predicate
             *          type      IRI
             *          value     http://www.w3.org/1999/02/22-rdf-syntax-ns#type
             *  object
             *          type      IRI
             *          value     http://schema.org/Person
             *  ---
             *  subject
             *          type      IRI
             *          value     http://example.org/ld-experts
             *  predicate
             *          type      IRI
             *          value     http://schema.org/member
             *  object
             *          type      blank node
             *          value     _:c14n0
             *  ---
             *  subject
             *          type      IRI
             *          value     http://example.org/ld-experts
             *  predicate
             *          type      IRI
             *          value     http://schema.org/name
             *  object
             *          type      literal
             *          value     LD Experts
             *          datatype  http://www.w3.org/2001/XMLSchema#string
             *  ---
             *
             */
        }
Example #15
0
        public IEnumerator <object[]> GetEnumerator()
        {
            foreach (string manifest in manifests)
            {
                JToken manifestJson;

                manifestJson = GetJson(manifest);

                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"]);

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

                    var testType = (JArray)testcase["@type"];

                    if (testType.Any((s) => (string)s == "jld:NegativeEvaluationTest"))
                    {
                        newCase.error = testcase["expect"];
                    }
                    else if (testType.Any((s) => (string)s == "jld:PositiveEvaluationTest"))
                    {
                        if (testType.Any((s) => new List <string> {
                            "jld:ToRDFTest", "jld:NormalizeTest"
                        }.Contains((string)s)))
                        {
                            newCase.output = File.ReadAllText(Path.Combine("W3C", (string)testcase["expect"]));
                        }
                        else if (testType.Any((s) => (string)s == "jld:FromRDFTest"))
                        {
                            newCase.input  = File.ReadAllText(Path.Combine("W3C", (string)testcase["input"]));
                            newCase.output = GetJson(testcase["expect"]);
                        }
                        else
                        {
                            newCase.output = GetJson(testcase["expect"]);
                        }
                    }
                    else
                    {
                        throw new Exception("Expecting either positive or negative evaluation test.");
                    }

                    JToken optionToken;
                    JToken value;

                    if (testcase.TryGetValue("option", out optionToken))
                    {
                        JObject optionDescription = (JObject)optionToken;

                        if (optionDescription.TryGetValue("compactArrays", out value))
                        {
                            options.SetCompactArrays((bool)value);
                        }
                        if (optionDescription.TryGetValue("base", out value))
                        {
                            options.SetBase((string)value);
                        }
                        if (optionDescription.TryGetValue("expandContext", out value))
                        {
                            newCase.context = GetJson(testcase["option"]["expandContext"]);
                            options.SetExpandContext((JObject)newCase.context);
                        }
                        if (optionDescription.TryGetValue("produceGeneralizedRdf", out value))
                        {
                            options.SetProduceGeneralizedRdf((bool)value);
                        }
                        if (optionDescription.TryGetValue("useNativeTypes", out value))
                        {
                            options.SetUseNativeTypes((bool)value);
                        }
                        if (optionDescription.TryGetValue("useRdfType", out value))
                        {
                            options.SetUseRdfType((bool)value);
                        }
                    }

                    if (testType.Any((s) => (string)s == "jld:CompactTest"))
                    {
                        run = () => JsonLdProcessor.Compact(newCase.input, newCase.context, options);
                    }
                    else if (testType.Any((s) => (string)s == "jld:ExpandTest"))
                    {
                        run = () => JsonLdProcessor.Expand(newCase.input, options);
                    }
                    else if (testType.Any((s) => (string)s == "jld:FlattenTest"))
                    {
                        run = () => JsonLdProcessor.Flatten(newCase.input, newCase.context, options);
                    }
                    else if (testType.Any((s) => (string)s == "jld:FrameTest"))
                    {
                        run = () => JsonLdProcessor.Frame(newCase.input, newCase.frame, options);
                    }
                    else if (testType.Any((s) => (string)s == "jld:NormalizeTest"))
                    {
                        run = () => new JValue(
                            RDFDatasetUtils.ToNQuads((RDFDataset)JsonLdProcessor.Normalize(newCase.input, options)).Replace("\n", "\r\n")
                            );
                    }
                    else if (testType.Any((s) => (string)s == "jld:ToRDFTest"))
                    {
                        options.format = "application/nquads";
                        run            = () => new JValue(
                            ((string)JsonLdProcessor.ToRDF(newCase.input, options)).Replace("\n", "\r\n")
                            );
                    }
                    else if (testType.Any((s) => (string)s == "jld:FromRDFTest"))
                    {
                        options.format = "application/nquads";
                        run            = () => JsonLdProcessor.FromRDF(newCase.input, options);
                    }
                    else
                    {
                        run = () => { throw new Exception("Couldn't find a test type, apparently."); };
                    }

                    if ((string)manifestJson["name"] == "Remote document")
                    {
                        Func <JToken> innerRun = run;
                        run = () =>
                        {
                            var remoteDoc = options.documentLoader.LoadDocument("https://json-ld.org/test-suite/tests/" + (string)testcase["input"]);
                            newCase.input = remoteDoc.Document;
                            options.SetBase(remoteDoc.DocumentUrl);
                            options.SetExpandContext((JObject)remoteDoc.Context);
                            return(innerRun());
                        };
                    }

                    if (testType.Any((s) => (string)s == "jld:NegativeEvaluationTest"))
                    {
                        Func <JToken> innerRun = run;
                        run = () =>
                        {
                            try
                            {
                                return(innerRun());
                            }
                            catch (JsonLdError err)
                            {
                                JObject result = new JObject();
                                result["error"] = err.Message;
                                return(result);
                            }
                        };
                    }

                    newCase.run = run;

                    yield return(new object[] { manifest + (string)testcase["@id"], (string)testcase["name"], newCase });
                }
            }
        }
 static VocabElementListState()
 {
     options = new JsonLdOptions();
     options.useNamespaces = true;
 }
        private static IEnumerable <object[]> SortingTestCases()
        {
            var jsonFetcher   = new JsonFetcher();
            var rootDirectory = Path.Combine(ManifestRoot, "Sorting");

            foreach (string manifest in SortingManifests)
            {
                JToken manifestJson = jsonFetcher.GetJson(manifest, rootDirectory);

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

                    newCase.input  = jsonFetcher.GetJson(manifestJson["input"], rootDirectory);
                    newCase.output = jsonFetcher.GetJson(testcase["expect"], rootDirectory);

                    var options = new JsonLdOptions();

                    var sortType = (string)testcase["sort-type"];

                    if (sortType == "jld:GraphsAndNodes")
                    {
                        options.SetSortGraphsFromRdf(true);
                        options.SetSortGraphNodesFromRdf(true);
                    }
                    else if (sortType == "jld:Graphs")
                    {
                        options.SetSortGraphsFromRdf(true);
                        options.SetSortGraphNodesFromRdf(false);
                    }
                    else if (sortType == "jld:Nodes")
                    {
                        options.SetSortGraphsFromRdf(false);
                        options.SetSortGraphNodesFromRdf(true);
                    }
                    else if (sortType == "jld:None")
                    {
                        options.SetSortGraphsFromRdf(false);
                        options.SetSortGraphNodesFromRdf(false);
                    }

                    JsonLdApi jsonLdApi = new JsonLdApi(options);

                    var testType = (string)testcase["test-type"];

                    if (testType == "jld:FromRDF")
                    {
                        JToken     quads = newCase.input["quads"];
                        RDFDataset rdf   = new RDFDataset();

                        foreach (JToken quad in quads)
                        {
                            string subject   = (string)quad["subject"];
                            string predicate = (string)quad["predicate"];
                            string value     = (string)quad["value"];
                            string graph     = (string)quad["graph"];

                            rdf.AddQuad(subject, predicate, value, graph);
                        }

                        options.format = "application/nquads";

                        run = () => jsonLdApi.FromRDF(rdf);
                    }
                    else
                    {
                        run = () => { throw new Exception("Couldn't find a test type, apparently."); };
                    }

                    newCase.run = run;

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