private void ExtractJsonLdStructuredData(ref string html, ref Graph graph) { var parser = new JsonLdParser(); var store = new TripleStore(); store.Add(graph); try { var jsonPieces = _htmlPiecesExtractionService.GetJsonLdSections(ref html); foreach (var piece in jsonPieces) { try { parser.Load(store, new StringReader(piece)); } catch (Exception) { parser.Load(store, new StringReader(_utilityService.GetRecognizableJsonLdObject(piece))); } } } catch (Exception) { } }
public void ItAddsListTriplesToTheGraphContainingTheList() { var jsonLdParser = new JsonLdParser(); ITripleStore tripleStore = new TripleStore(); using (var reader = new StringReader(@"{ ""@id"": ""urn:graph:1"", ""@graph"": [ { ""@id"": ""urn:subject:1"", ""urn:predicate:1"": { ""@list"": [ ""foo"", ""bar"" ] } } ]}")) { jsonLdParser.Load(tripleStore, reader); } var defaultGraph = tripleStore.Graphs.FirstOrDefault(g => g.BaseUri == null); Assert.True(defaultGraph == null || defaultGraph.IsEmpty); var contentGraph = tripleStore.Graphs[new Uri("urn:graph:1")]; Assert.NotNull(contentGraph); Assert.Equal(5, contentGraph.Triples.Count); }
public void TestIssue122() { var jsonLdParser = new JsonLdParser(); ITripleStore tStore = new TripleStore(); using (var reader = new System.IO.StringReader(@"{ ""@context"": ""http://json-ld.org/contexts/person.jsonld"", ""@id"": ""http://dbpedia.org/resource/John_Lennon"", ""name"": ""John Lennon"", ""born"": ""1940-10-09"", ""spouse"": ""http://dbpedia.org/resource/Cynthia_Lennon"" }")) { jsonLdParser.Load(tStore, reader); } Assert.Equal(3, tStore.Triples.Count()); Assert.Contains(tStore.Triples, x => x.Subject.As <IUriNode>().Uri.ToString().Equals("http://dbpedia.org/resource/John_Lennon") && x.Predicate.As <IUriNode>().Uri.ToString().Equals("http://xmlns.com/foaf/0.1/name") && x.Object.As <ILiteralNode>().Value.Equals("John Lennon")); Assert.Contains(tStore.Triples, x => x.Subject.As <IUriNode>().Uri.ToString().Equals("http://dbpedia.org/resource/John_Lennon") && x.Predicate.As <IUriNode>().Uri.ToString().Equals("http://schema.org/birthDate") && x.Object.As <ILiteralNode>().Value.Equals("1940-10-09") && x.Object.As <ILiteralNode>().DataType.ToString().Equals("http://www.w3.org/2001/XMLSchema#date")); Assert.Contains(tStore.Triples, x => x.Subject.As <IUriNode>().Uri.ToString().Equals("http://dbpedia.org/resource/John_Lennon") && x.Predicate.As <IUriNode>().Uri.ToString().Equals("http://schema.org/spouse") && x.Object.As <IUriNode>().Uri.ToString().Equals("http://dbpedia.org/resource/Cynthia_Lennon")); }
public void ItTreatsUrnsAsAbsoluteIris() { var jsonLd = @" { '@id': 'urn:uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6', 'http://example.org/p': { '@value': 'o', '@type': 'urn:uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6' } }"; var jsonLdParser = new JsonLdParser(); ITripleStore tStore = new TripleStore(); using (var reader = new StringReader(jsonLd)) { jsonLdParser.Load(tStore, reader); } foreach (var t in tStore.Triples) { _output.WriteLine(t.ToString()); } Assert.Contains(tStore.Triples, t => t.Subject is IUriNode node && node.Uri.ToString().Equals("urn:uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6")); Assert.Contains(tStore.Triples, t => t.Object is ILiteralNode node && node.DataType.ToString().Equals("urn:uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6")); }
public void ItRetainsLocalContextWhenProcessingARemoteContext() { ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; var jsonLd = @" { '@context': [ { '@base': 'http://example.com/' }, 'https://www.w3.org/ns/hydra/core' ], '@id': 'foo', 'rdf:type': 'hydra:Class' }"; var jsonLdParser = new JsonLdParser(); ITripleStore tStore = new TripleStore(); using (var reader = new StringReader(jsonLd)) { jsonLdParser.Load(tStore, reader); } foreach (var t in tStore.Triples) { _output.WriteLine(t.Subject.ToString()); } Assert.Contains(tStore.Triples, t => t.Subject.As <IUriNode>().Uri.ToString().Equals("http://example.com/foo")); }
public void JsonLdParserTests(string inputPath, string contextPath, string expectedOutputPath, string baseIri, string processorMode, string expandContextPath, bool compactArrays) { var processorOptions = MakeProcessorOptions(inputPath, baseIri, processorMode, expandContextPath, compactArrays); var contextJson = contextPath == null ? null : File.ReadAllText(contextPath); var contextElement = contextJson == null ? null : JToken.Parse(contextJson); var nqParser = new NQuadsParser(NQuadsSyntax.Rdf11); var expectedStore = new TripleStore(); nqParser.Load(expectedStore, expectedOutputPath); FixStringLiterals(expectedStore); var jsonldParser = new JsonLdParser(processorOptions); var actualStore = new TripleStore(); jsonldParser.Load(actualStore, inputPath); Assert.True(expectedStore.Graphs.Count.Equals(actualStore.Graphs.Count), $"Test failed for input {Path.GetFileName(inputPath)}.\r\nActual graph count {actualStore.Graphs.Count} does not match expected graph count {expectedStore.Graphs.Count}."); foreach (var expectGraph in expectedStore.Graphs) { Assert.True(actualStore.HasGraph(expectGraph.BaseUri), $"Test failed for input {Path.GetFileName(inputPath)}.\r\nCould not find expected graph {expectGraph.BaseUri}"); var actualGraph = actualStore.Graphs[expectGraph.BaseUri]; var bNodeMapping = new Dictionary <INode, INode>(); var graphsEqual = actualGraph.Equals(expectGraph, out bNodeMapping); if (!graphsEqual) { var ser = new NQuadsWriter(); string expectedLines = MakeNQuadsList(expectedStore); string actualLines = MakeNQuadsList(actualStore); Assert.True(graphsEqual, $"Test failed for input {Path.GetFileName(inputPath)}.\r\nGraph {expectGraph.BaseUri} differs in actual output from expected output.\r\nExpected:\r\n{expectedLines}\r\nActual:\r\n{actualLines}"); } } }
public virtual void JsonLdParserTests(string testId, JsonLdTestType testType, string inputPath, string contextPath, string expectedOutputPath, JsonLdErrorCode expectedErrorCode, string baseIri, string processorMode, string expandContextPath, bool compactArrays, string rdfDirection) { var processorOptions = MakeProcessorOptions(inputPath, baseIri, processorMode, expandContextPath, compactArrays, rdfDirection); var jsonldParser = new JsonLdParser(processorOptions); var actualStore = new TripleStore(); switch (testType) { case JsonLdTestType.PositiveEvaluationTest: var nqParser = new NQuadsParser(NQuadsSyntax.Rdf11); var expectedStore = new TripleStore(); nqParser.Load(expectedStore, expectedOutputPath); FixStringLiterals(expectedStore); jsonldParser.Load(actualStore, inputPath); Assert.True(expectedStore.Graphs.Count.Equals(actualStore.Graphs.Count) || (expectedStore.Graphs.Count == 0 && actualStore.Graphs.Count == 1 && actualStore.Graphs[null].IsEmpty), $"Test failed for input {Path.GetFileName(inputPath)}.\r\nActual graph count {actualStore.Graphs.Count} does not match expected graph count {expectedStore.Graphs.Count}."); AssertStoresEqual(expectedStore, actualStore, Path.GetFileName(inputPath)); break; case JsonLdTestType.NegativeEvaluationTest: var exception = Assert.Throws <JsonLdProcessorException>(() => jsonldParser.Load(actualStore, inputPath)); Assert.Equal(expectedErrorCode, exception.ErrorCode); break; case JsonLdTestType.PositiveSyntaxTest: // Positive syntax test should load input file without raising any exceptions jsonldParser.Load(actualStore, inputPath); break; default: Assert.True(false, $"Test type {testType} is not currently supported for the JSON-LD Parser tests"); break; } }
public static IEnumerable <string> ToRdf(JToken token, JsonLdProcessorOptions options) { var jsonLdParser = new JsonLdParser(options); var store = new TripleStore(); jsonLdParser.Load(store, new StringReader(token.ToString(Newtonsoft.Json.Formatting.None))); var nqWriter = new NQuadsWriter(NQuadsSyntax.Rdf11); using var expectedTextWriter = new System.IO.StringWriter(); nqWriter.Save(store, expectedTextWriter); return(expectedTextWriter.ToString().Split(Environment.NewLine).Where(x => !string.IsNullOrWhiteSpace(x))); }
static void Main(string[] args) { //uncomment if you need to serialize the recipes from json to jsonld //ConvertJsonRecipesToJsonRdf(); try { TripleStore tripleStore = new TripleStore(); JsonLdParser parser = new JsonLdParser(); //Load using Filename parser.Load(tripleStore, @"D:\Finki\Web based systems\Project\output.jsonld"); var graph = tripleStore.Graphs.First(); //CompressingTurtleWriter writer = new CompressingTurtleWriter(); //Save to a File //writer.Save(graph, "Example.ttl"); Console.WriteLine("Please enter an ingridient:"); var input = Console.ReadLine(); var inputIngridients = input.Split(','); string x = "x"; string y = "y"; var queryBuilder = QueryBuilder .Select(new string[] { x }) .Where( (triplePatternBuilder) => { triplePatternBuilder .Subject(x) .PredicateUri(new Uri(@"https://schema.org/#recipeIngredient")) .Object(y); }).Filter((builder) => builder.Regex(builder.Variable("y"), input, "i")); //Console.WriteLine(queryBuilder.BuildQuery().ToString()); //SparqlQuery query = queryBuilder.BuildQuery(); var query = @"SELECT ?x WHERE { ?x <https://schema.org/#recipeIngredient> ?object FILTER (REGEX(STR(?object), '{{input}}', 'i') )}"; query = query.Replace("{{input}}", input); var results = graph.ExecuteQuery(query); //Print out the Results SparqlResultSet g = (SparqlResultSet)results; foreach (SparqlResult t in g) { Console.WriteLine(t.ToString()); } Console.ReadKey(); } catch (RdfParseException parseEx) { //This indicates a parser error e.g unexpected character, premature end of input, invalid syntax etc. Console.WriteLine("Parser Error"); Console.WriteLine(parseEx.Message); } catch (RdfException rdfEx) { //This represents a RDF error e.g. illegal triple for the given syntax, undefined namespace Console.WriteLine("RDF Error"); Console.WriteLine(rdfEx.Message); } }