public void GraphWithBNodeEquality() { Console.WriteLine("Testing Graph Equality when the Graphs have Blank Nodes"); Graph g = new Graph(); Graph h = new Graph(); TurtleParser ttlparser = new TurtleParser(); ttlparser.Load(g, "resources\\MergePart1.ttl"); ttlparser.Load(h, "resources\\MergePart1.ttl"); Assert.Equal(g.BaseUri, h.BaseUri); //TestTools.CompareGraphs(g, h, true); Dictionary <INode, INode> mapping; bool equals = g.Equals(h, out mapping); Assert.True(@equals, "Graphs should have been equal"); if (mapping != null) { Console.WriteLine("Blank Node Mapping was:"); foreach (KeyValuePair <INode, INode> pair in mapping) { Console.WriteLine(pair.Key.ToString() + " => " + pair.Value.ToString()); } } }
public void GraphHardMatch2() { IGraph g = new Graph(); IGraph h = new Graph(); int size = 1 << Dimension; Random rnd = new Random(); for (int i = 0; i < Runs; i++) { int a = rnd.Next(size); DiHypercube hc1 = new DiHypercube(Dimension, g); hc1 = hc1.Duplicate(a).Duplicate(a).Duplicate(a); DiHypercube hc2 = new DiHypercube(Dimension, h); hc2 = hc2.Duplicate(a).Duplicate(a).Duplicate(a); if (i == 0) { TestTools.ShowGraph(g); Console.WriteLine(); TestTools.ShowGraph(h); } Assert.IsTrue(g.Equals(h), "Graphs should be equal"); Console.WriteLine("Run #" + (i + 1) + " passed OK"); g = new Graph(); h = new Graph(); } }
public void GraphWithBNodeEquality() { try { Console.WriteLine("Testing Graph Equality when the Graphs have Blank Nodes"); Graph g = new Graph(); Graph h = new Graph(); TurtleParser ttlparser = new TurtleParser(); ttlparser.Load(g, "MergePart1.ttl"); ttlparser.Load(h, "MergePart1.ttl"); Assert.AreEqual(g.BaseUri, h.BaseUri, "The Base URIs of the Graphs should not be affected by the Load and so should be both null"); //TestTools.CompareGraphs(g, h, true); Dictionary <INode, INode> mapping; bool equals = g.Equals(h, out mapping); Assert.IsTrue(equals, "Graphs should have been equal"); if (mapping != null) { Console.WriteLine("Blank Node Mapping was:"); foreach (KeyValuePair <INode, INode> pair in mapping) { Console.WriteLine(pair.Key.ToString() + " => " + pair.Value.ToString()); } } } catch (RdfParseException parseEx) { throw; } catch (Exception ex) { throw; } }
private static void TestGraphMatch(string testGraphName) { Graph a = new Graph(); a.LoadFromFile(string.Format("resources\\diff_cases\\{0}_a.ttl", testGraphName)); Graph b = new Graph(); b.LoadFromFile(string.Format("resources\\diff_cases\\{0}_b.ttl", testGraphName)); Assert.IsTrue(a.Equals(b)); Assert.IsTrue(b.Equals(a)); }
public void GraphMatchNull4() { IGraph g = new Graph(); Assert.IsFalse(g.Equals((IGraph)null)); }
public void GraphEquality() { Skip.IfNot(TestConfigManager.GetSettingAsBoolean(TestConfigManager.UseRemoteParsing), "Test Config marks Remote Parsing as unavailable, test cannot be run"); try { Options.UriLoaderCaching = false; Console.WriteLine("Going to get two copies of a Graph from DBPedia and compare"); Console.WriteLine("Using the DBPedia Graph for Barack Obama"); Graph g = new Graph(); Graph h = new Graph(); Uri target = new Uri("http://dbpedia.org/resource/Barack_Obama"); VDS.RDF.Parsing.UriLoader.Load(g, target); Console.WriteLine("Loaded first copy OK - " + g.Triples.Count + " Triples"); VDS.RDF.Parsing.UriLoader.Load(h, target); Console.WriteLine("Loaded second copy OK - " + h.Triples.Count + " Triples"); //Should have same Base Uri Assert.Equal(g.BaseUri, h.BaseUri); //Do equality check Console.WriteLine("Checking the Equality of the Graphs"); //TestTools.CompareGraphs(g, h, true); Dictionary <INode, INode> mapping; bool equals = g.Equals(h, out mapping); Assert.True(equals, "Graphs should have been equal"); if (mapping != null) { Console.WriteLine("Blank Node Mapping was:"); foreach (KeyValuePair <INode, INode> pair in mapping) { Console.WriteLine(pair.Key.ToString() + " => " + pair.Value.ToString()); } } Console.WriteLine(); //Get a third graph of something different Console.WriteLine("Going to get a third Graph of something different and check it is non-equal"); Uri target2 = new Uri("http://dbpedia.org/resource/Nottingham"); Graph i = new Graph(); VDS.RDF.Parsing.UriLoader.Load(i, target2); //Should have different Base URIs and be non-equal Assert.Equal(g.BaseUri, i.BaseUri); Assert.Equal(h.BaseUri, i.BaseUri); Assert.False(g.Equals(i)); Assert.False(h.Equals(i)); //TestTools.CompareGraphs(g, i, false); //TestTools.CompareGraphs(h, i, false); } catch (WebException webEx) { TestTools.ReportError("Web Exception", webEx); Console.WriteLine(); Console.WriteLine("Unable to retrieve the Graphs from the Web successfully!"); Skip.If(true, "Unable to retrieve the graphs from the web successfully."); } finally { Options.UriLoaderCaching = true; } }
private static void CompareTripleCollections(BaseTripleCollection actualTriples, BaseTripleCollection expectedTriples, bool reduced) { var actualGraph = new Graph(actualTriples); var expectedGraph = new Graph(expectedTriples); Assert.IsTrue(expectedGraph.Equals(actualGraph), "Result graphs do not match.r\nExpected:\r\n{0}\r\nActual:\r\n{1}", String.Join(",", expectedTriples.Select(t => t.ToString())), String.Join(",", actualTriples.Select(t => t.ToString()))); }
public void GraphEquality() { try { Console.WriteLine("Going to get two copies of a Graph from DBPedia and compare"); Console.WriteLine("Using the DBPedia Graph for Barack Obama"); Graph g = new Graph(); Graph h = new Graph(); Uri target = new Uri("http://dbpedia.org/resource/Barack_Obama"); VDS.RDF.Parsing.UriLoader.Load(g, target); Console.WriteLine("Loaded first copy OK - " + g.Triples.Count + " Triples"); VDS.RDF.Parsing.UriLoader.Load(h, target); Console.WriteLine("Loaded second copy OK - " + h.Triples.Count + " Triples"); //Should have same Base Uri Assert.AreEqual(g.BaseUri, h.BaseUri, "Should have the same Base URI after being loaded from the same URI via the URILoader"); //Do equality check Console.WriteLine("Checking the Equality of the Graphs"); //TestTools.CompareGraphs(g, h, true); Dictionary<INode, INode> mapping; bool equals = g.Equals(h, out mapping); Assert.IsTrue(equals, "Graphs should have been equal"); if (mapping != null) { Console.WriteLine("Blank Node Mapping was:"); foreach (KeyValuePair<INode, INode> pair in mapping) { Console.WriteLine(pair.Key.ToString() + " => " + pair.Value.ToString()); } } Console.WriteLine(); //Get a third graph of something different Console.WriteLine("Going to get a third Graph of something different and check it is non-equal"); Uri target2 = new Uri("http://dbpedia.org/resource/Nottingham"); Graph i = new Graph(); VDS.RDF.Parsing.UriLoader.Load(i, target2); //Should have different Base URIs and be non-equal Assert.AreNotEqual(g.BaseUri, i.BaseUri, "Graphs retrieved from different URIs via the URILoader should have different Base URIs"); Assert.AreNotEqual(h.BaseUri, i.BaseUri, "Graphs retrieved from different URIs via the URILoader should have different Base URIs"); Assert.IsFalse(g.Equals(i)); Assert.IsFalse(h.Equals(i)); //TestTools.CompareGraphs(g, i, false); //TestTools.CompareGraphs(h, i, false); } catch (WebException webEx) { TestTools.ReportError("Web Exception", webEx, false); Console.WriteLine(); Console.WriteLine("Unable to retrieve the Graphs from the Web successfully!"); Assert.Inconclusive(); } catch (RdfParseException parseEx) { TestTools.ReportError("Parsing Exception", parseEx, true); } catch (Exception ex) { TestTools.ReportError("Other Error", ex, true); } }
public void GraphWithBNodeEquality() { try { Console.WriteLine("Testing Graph Equality when the Graphs have Blank Nodes"); Graph g = new Graph(); Graph h = new Graph(); TurtleParser ttlparser = new TurtleParser(); ttlparser.Load(g, "MergePart1.ttl"); ttlparser.Load(h, "MergePart1.ttl"); Assert.AreEqual(g.BaseUri, h.BaseUri, "The Base URIs of the Graphs should not be affected by the Load and so should be both null"); //TestTools.CompareGraphs(g, h, true); Dictionary<INode, INode> mapping; bool equals = g.Equals(h, out mapping); Assert.IsTrue(equals, "Graphs should have been equal"); if (mapping != null) { Console.WriteLine("Blank Node Mapping was:"); foreach (KeyValuePair<INode, INode> pair in mapping) { Console.WriteLine(pair.Key.ToString() + " => " + pair.Value.ToString()); } } } catch (RdfParseException parseEx) { TestTools.ReportError("Parsing Exception", parseEx, true); } catch (Exception ex) { TestTools.ReportError("Other Error", ex, true); } }
public void GraphEquality() { try { Console.WriteLine("Going to get two copies of a Graph from DBPedia and compare"); Console.WriteLine("Using the DBPedia Graph for Barack Obama"); Graph g = new Graph(); Graph h = new Graph(); Uri target = new Uri("http://dbpedia.org/resource/Barack_Obama"); VDS.RDF.Parsing.UriLoader.Load(g, target); Console.WriteLine("Loaded first copy OK - " + g.Triples.Count + " Triples"); VDS.RDF.Parsing.UriLoader.Load(h, target); Console.WriteLine("Loaded second copy OK - " + h.Triples.Count + " Triples"); //Should have same Base Uri Assert.AreEqual(g.BaseUri, h.BaseUri, "Should have the same Base URI after being loaded from the same URI via the URILoader"); //Do equality check Console.WriteLine("Checking the Equality of the Graphs"); //TestTools.CompareGraphs(g, h, true); Dictionary <INode, INode> mapping; bool equals = g.Equals(h, out mapping); Assert.IsTrue(equals, "Graphs should have been equal"); if (mapping != null) { Console.WriteLine("Blank Node Mapping was:"); foreach (KeyValuePair <INode, INode> pair in mapping) { Console.WriteLine(pair.Key.ToString() + " => " + pair.Value.ToString()); } } Console.WriteLine(); //Get a third graph of something different Console.WriteLine("Going to get a third Graph of something different and check it is non-equal"); Uri target2 = new Uri("http://dbpedia.org/resource/Nottingham"); Graph i = new Graph(); VDS.RDF.Parsing.UriLoader.Load(i, target2); //Should have different Base URIs and be non-equal Assert.AreNotEqual(g.BaseUri, i.BaseUri, "Graphs retrieved from different URIs via the URILoader should have different Base URIs"); Assert.AreNotEqual(h.BaseUri, i.BaseUri, "Graphs retrieved from different URIs via the URILoader should have different Base URIs"); Assert.IsFalse(g.Equals(i)); Assert.IsFalse(h.Equals(i)); //TestTools.CompareGraphs(g, i, false); //TestTools.CompareGraphs(h, i, false); } catch (WebException webEx) { TestTools.ReportError("Web Exception", webEx); Console.WriteLine(); Console.WriteLine("Unable to retrieve the Graphs from the Web successfully!"); Assert.Inconclusive(); } catch (RdfParseException parseEx) { throw; } catch (Exception ex) { throw; } }
public void ParsingBlankNodeIDs() { List<IRdfReader> parsersToTest = new List<IRdfReader>() { new TurtleParser(), new Notation3Parser() }; String[] samples = new String[] { "@prefix ex: <http://example.org>. [] a ex:bNode. _:autos1 a ex:bNode. _:autos1 a ex:another.", "@prefix ex: <http://example.org>. _:autos1 a ex:bNode. [] a ex:bNode. _:autos1 a ex:another.", "@prefix : <http://example.org/>. [] a :BlankNode ; :firstProperty :a ; :secondProperty :b .", "@prefix : <http://example.org/>. (:first :second) a :Collection .", "@prefix : <http://example.org/>. [a :bNode ; :connectsTo [a :bNode ; :connectsTo []]] a [].", "@prefix : <http://example.org/>. [a :bNode ; :connectsTo [a :bNode ; :connectsTo []]] a []. [] a :another ; a [a :yetAnother] ." }; int[] expectedTriples = new int[] { 3, 3, 3, 5, 5, 8 }; int[] expectedSubjects = new int[] { 2, 2, 1, 2, 2, 4 }; Console.WriteLine("Tests Blank Node ID assignment in Parsing and Serialization as well as Graph Equality"); Console.WriteLine(); List<IRdfWriter> writers = new List<IRdfWriter>() { new NTriplesWriter(), new TurtleWriter(), new CompressingTurtleWriter(), new Notation3Writer(), new RdfXmlWriter(), new FastRdfXmlWriter(), new RdfJsonWriter() }; List<IRdfReader> readers = new List<IRdfReader>() { new NTriplesParser(), new TurtleParser(), new TurtleParser(), new Notation3Parser(), new RdfXmlParser(), new RdfXmlParser(), new RdfJsonParser() }; foreach (IRdfReader parser in parsersToTest) { Console.WriteLine("Testing " + parser.GetType().ToString()); //parser.TraceTokeniser = true; //parser.TraceParsing = true; int s = 0; foreach (String sample in samples) { Console.WriteLine(); Console.WriteLine("Sample:"); Console.WriteLine(sample); Console.WriteLine(); Graph g = new Graph(); VDS.RDF.Parsing.StringParser.Parse(g, sample, parser); Console.WriteLine("Original Graph"); Console.WriteLine(g.Triples.Count + " Triples produced"); foreach (Triple t in g.Triples) { Console.WriteLine(t.ToString()); } Console.WriteLine(); Assert.AreEqual(expectedTriples[s], g.Triples.Count, "Should have produced " + expectedTriples[s] + " Triples"); Assert.AreEqual(expectedSubjects[s], g.Triples.SubjectNodes.Distinct().Count(), "Should have produced " + expectedSubjects[s] + " distinct subjects"); //Try outputting with each of the available writers for (int i = 0; i < writers.Count; i++) { String temp = StringWriter.Write(g, writers[i]); Graph h = new Graph(); VDS.RDF.Parsing.StringParser.Parse(h, temp, readers[i]); Console.WriteLine("Trying " + writers[i].GetType().ToString()); Console.WriteLine("Graph after Serialization and Parsing"); Console.WriteLine(h.Triples.Count + " Triples produced"); foreach (Triple t in h.Triples) { Console.WriteLine(t.ToString()); } Console.WriteLine(); if (expectedTriples[s] != h.Triples.Count || expectedSubjects[s] != h.Triples.SubjectNodes.Distinct().Count()) { Console.WriteLine(writers[i].GetType().ToString() + " failed"); Console.WriteLine(temp); } Assert.AreEqual(expectedTriples[s], h.Triples.Count, "Should have produced " + expectedTriples[s] + " Triples"); Assert.AreEqual(expectedSubjects[s], h.Triples.SubjectNodes.Distinct().Count(), "Should have produced " + expectedSubjects[s] + " distinct subjects"); //Do full equality Test Dictionary<INode, INode> mapping; bool equals = g.Equals(h, out mapping); if (!equals) { Console.WriteLine(writers[i].GetType().ToString() + " failed"); Console.WriteLine(temp); } Assert.IsTrue(equals, "Graphs should be equal"); Console.WriteLine("Node Mapping was:"); foreach (KeyValuePair<INode, INode> pair in mapping) { Console.WriteLine(pair.Key.ToString() + " => " + pair.Value.ToString()); } Console.WriteLine(); } Console.WriteLine("All writers OK"); s++; } Console.WriteLine(); Console.WriteLine(); } }
public void ParsingCollections() { List<IRdfReader> parsersToTest = new List<IRdfReader>() { new TurtleParser(), new Notation3Parser() }; String[] samples = new String[] { "@prefix ex: <http://example.com/>. (\"one\" \"two\") a ex:Collection .", "@prefix ex: <http://example.com/>. (\"one\" \"two\" \"three\") a ex:Collection .", "@prefix ex: <http://example.com/>. (1) ex:someProp \"Value\"." }; int[] expectedTriples = new int[] { 5, 7, 3 }; int[] expectedSubjects = new int[] { 2, 3, 1 }; List<IRdfWriter> writers = new List<IRdfWriter>() { new NTriplesWriter(), new TurtleWriter(), new CompressingTurtleWriter(), new Notation3Writer(), new RdfXmlWriter(), new FastRdfXmlWriter(), new RdfJsonWriter() }; List<IRdfReader> readers = new List<IRdfReader>() { new NTriplesParser(), new TurtleParser(), new TurtleParser(), new Notation3Parser(), new RdfXmlParser(), new RdfXmlParser(), new RdfJsonParser() }; foreach (IRdfReader parser in parsersToTest) { Console.WriteLine("Testing " + parser.GetType().ToString()); //parser.TraceTokeniser = true; //parser.TraceParsing = true; int s = 0; foreach (String sample in samples) { Console.WriteLine(); Console.WriteLine("Sample:"); Console.WriteLine(sample); Console.WriteLine(); Graph g = new Graph(); VDS.RDF.Parsing.StringParser.Parse(g, sample, parser); Console.WriteLine(g.Triples.Count + " Triples produced"); foreach (Triple t in g.Triples) { Console.WriteLine(t.ToString()); } Assert.AreEqual(expectedTriples[s], g.Triples.Count, "Should have produced " + expectedTriples[s] + " Triples"); Assert.AreEqual(expectedSubjects[s], g.Triples.SubjectNodes.Distinct().Count(), "Should have produced " + expectedSubjects[s] + " distinct subjects"); //Try outputting with each of the available writers for (int i = 0; i < writers.Count; i++) { String temp = StringWriter.Write(g, writers[i]); Graph h = new Graph(); VDS.RDF.Parsing.StringParser.Parse(h, temp, readers[i]); Console.WriteLine("Trying " + writers[i].GetType().ToString()); Assert.AreEqual(expectedTriples[s], h.Triples.Count, "Should have produced " + expectedTriples[s] + " Triples"); Assert.AreEqual(expectedSubjects[s], h.Triples.SubjectNodes.Distinct().Count(), "Should have produced " + expectedSubjects[s] + " distinct subjects"); Dictionary<INode, INode> mapping; bool equals = g.Equals(h, out mapping); Assert.IsTrue(equals, "Graphs should have been equal"); Console.WriteLine("Node mapping was:"); foreach (KeyValuePair<INode, INode> pair in mapping) { Console.WriteLine(pair.Key.ToString() + " => " + pair.Value.ToString()); } Console.WriteLine(); } Console.WriteLine("All writers OK"); s++; } Console.WriteLine(); Console.WriteLine(); } }
public static void TestWriter(StreamWriter output, Graph g, IRdfWriter writer, IRdfReader reader, String file) { Stopwatch timer = new Stopwatch(); Console.WriteLine(); Console.WriteLine(new String('-', file.Length)); Console.WriteLine(file); Console.WriteLine("Attempting serialization with " + writer.GetType().ToString()); //Show Compression Level if (writer is ICompressingWriter) { Console.WriteLine("Compression Level is " + ((ICompressingWriter)writer).CompressionLevel); } //Enable Pretty Printing if supported if (writer is IPrettyPrintingWriter) { ((IPrettyPrintingWriter)writer).PrettyPrintMode = true; } try { timer.Start(); writer.Save(g, "writer_tests/" + file + ".out"); Console.WriteLine("Serialization Done"); } catch (IOException ioEx) { reportError(output, "IO Exception", ioEx); } catch (RdfParseException parseEx) { reportError(output, "Parsing Exception", parseEx); } catch (RdfException rdfEx) { reportError(output, "RDF Exception", rdfEx); } catch (Exception ex) { reportError(output, "Other Exception", ex); } finally { timer.Stop(); Console.WriteLine("Writing took " + timer.ElapsedMilliseconds + "ms"); //Get the relevant Reader Graph h = new Graph(); try { //Read back in the serialized output to check it's valid RDF Console.WriteLine("Attempting to read back in from serialized Output using " + reader.GetType().ToString()); reader.Load(h, "writer_tests/" + file + ".out"); Console.WriteLine("Serialized Output was valid RDF"); //Check same number of Triples are present if (g.Triples.Count == h.Triples.Count) { Console.WriteLine("Correct number of Triples loaded"); } else { throw new RdfException("Incorrect number of Triples loaded, got " + h.Triples.Count + " but expected " + g.Triples.Count); } //Check same number of Subjects are present if (g.Triples.SubjectNodes.Distinct().Count() == h.Triples.SubjectNodes.Distinct().Count()) { Console.WriteLine("Correct number of Subjects loaded"); } else { throw new RdfException("Incorrect number of Subjects loaded, got " + h.Triples.SubjectNodes.Distinct().Count() + " but expected " + g.Triples.SubjectNodes.Distinct().Count()); } //Reserialize to NTriples NTriplesWriter ntwriter = new NTriplesWriter(); ntwriter.SortTriples = true; ntwriter.Save(h, "writer_tests/" + file + ".nt"); Console.WriteLine("Serialized Output reserialized to NTriples"); //Check Graphs are Equal if (g.Equals(h)) { Console.WriteLine("Graphs are Equal"); } else { Console.WriteLine("First Graphs triples:"); foreach (Triple t in g.Triples) { Console.WriteLine(t.ToString()); } Console.WriteLine(); Console.WriteLine("Second Graph triples:"); foreach (Triple t in h.Triples) { Console.WriteLine(t.ToString()); } Console.WriteLine(); throw new RdfException("Graphs are non-equal"); } } catch (IOException ioEx) { reportError(output, "IO Exception", ioEx); } catch (RdfParseException parseEx) { reportError(output, "Parsing Exception", parseEx); } catch (RdfException rdfEx) { reportError(output, "RDF Exception", rdfEx); } catch (Exception ex) { reportError(output, "Other Exception", ex); } Console.WriteLine(); } }