Ejemplo n.º 1
0
        public void ParsingSpeedTurtle100ThousandCountOnly()
        {
            try
            {
                Options.InternUris = false;
                EnsureTestData(100000, "100thou.ttl", new TurtleFormatter());

                CountHandler handler = new CountHandler();
                Stopwatch    watch   = new Stopwatch();
                TurtleParser parser  = new TurtleParser();

                watch.Start();
                parser.Load(handler, "100thou.ttl");
                watch.Stop();

                Console.WriteLine(watch.Elapsed.ToString());
                this.CalculateSpeed(100000, watch);

                Assert.Equal(100000, handler.Count);
            }
            finally
            {
                Options.InternUris = true;
            }
        }
Ejemplo n.º 2
0
 public static IGraph Load(string name)
 {
     TurtleParser parser = new TurtleParser();
     IGraph g = new Graph();
     parser.Load(g, new StreamReader(Utils.GetResourceStream(name)));
     return g;
 }
Ejemplo n.º 3
0
        public void WritingBlankNodeOutput()
        {
            //Create a Graph and add a couple of Triples which when serialized have
            //potentially colliding IDs

            Graph g = new Graph();
            g.NamespaceMap.AddNamespace("ex", new Uri("http://example.org"));
            IUriNode subj = g.CreateUriNode("ex:subject");
            IUriNode pred = g.CreateUriNode("ex:predicate");
            IUriNode name = g.CreateUriNode("ex:name");
            IBlankNode b1 = g.CreateBlankNode("autos1");
            IBlankNode b2 = g.CreateBlankNode("1");

            g.Assert(subj, pred, b1);
            g.Assert(b1, name, g.CreateLiteralNode("First Triple"));
            g.Assert(subj, pred, b2);
            g.Assert(b2, name, g.CreateLiteralNode("Second Triple"));

            TurtleWriter ttlwriter = new TurtleWriter();
            ttlwriter.Save(g, "bnode-output-test.ttl");

            TestTools.ShowGraph(g);

            TurtleParser ttlparser = new TurtleParser();
            Graph h = new Graph();
            ttlparser.Load(h, "bnode-output-test.ttl");

            TestTools.ShowGraph(h);

            Assert.AreEqual(g.Triples.Count, h.Triples.Count, "Expected same number of Triples after serialization and reparsing");

        }
Ejemplo n.º 4
0
        public void RunVocab(String[] args)
        {
            if (args.Length < 2)
            {
                Console.Error.WriteLine("rdfWebDeploy: Error: 2 Arguments are required in order to use the -vocab mode");
                return;
            }

            if (File.Exists(args[1]))
            {
                Console.Error.WriteLine("rdfWebDeploy: Error: Cannot output the configuration vocabulary to " + args[1] + " as a file already exists at that location");
                return;
            }

            TurtleParser ttlparser = new TurtleParser();
            StreamReader reader = new StreamReader(Assembly.GetAssembly(typeof(IGraph)).GetManifestResourceStream("VDS.RDF.Configuration.configuration.ttl"));
            Graph g = new Graph();
            ttlparser.Load(g, reader);

            IRdfWriter writer;
            try
            {
                writer = MimeTypesHelper.GetWriter(MimeTypesHelper.GetMimeType(Path.GetExtension(args[1])));
            }
            catch (RdfWriterSelectionException)
            {
                writer = new CompressingTurtleWriter(WriterCompressionLevel.High);
            }
            writer.Save(g, args[1]);
            Console.WriteLine("rdfWebDeploy: Configuration Vocabulary output to " + args[1]);
        }
Ejemplo n.º 5
0
        public void ParsingTurtleDBPediaMalformedData()
        {
            Graph        g      = new Graph();
            TurtleParser parser = new TurtleParser(TurtleSyntax.Original);

            Assert.Throws <RdfParseException>(() => parser.Load(g, "resources\\dbpedia_malformed.ttl"));
        }
Ejemplo n.º 6
0
        public void ParsingTurtleDBPediaMalformedData()
        {
            Graph        g      = new Graph();
            TurtleParser parser = new TurtleParser(TurtleSyntax.Original);

            parser.Load(g, "dbpedia_malformed.ttl");
            Assert.IsFalse(g.IsEmpty);
        }
Ejemplo n.º 7
0
        public void ParsingDefaultPrefixFallbackTurtle1()
        {
            String     data   = @"@base <http://base/> . :subj :pred :obj .";
            IRdfReader parser = new TurtleParser();
            Graph      g      = new Graph();

            Assert.Throws <RdfParseException>(() => parser.Load(g, new StringReader(data)));
        }
Ejemplo n.º 8
0
 /// <summary>
 /// Loads the Default Profile which is embedded in this assembly
 /// </summary>
 private void LoadDefaultProfile()
 {
     StreamReader reader = new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream("VDS.RDF.LinkedData.Profiles.DefaultExpansionProfile.ttl"));
     TurtleParser ttlparser = new TurtleParser();
     Graph g = new Graph();
     ttlparser.Load(g, reader);
     this.Initialise(g);
 }
Ejemplo n.º 9
0
        public void ParsingRelativeUriNoBaseTurtle()
        {
            //This invocation fails because there is no Base URI to
            //resolve against
            Graph        g      = new Graph();
            TurtleParser parser = new TurtleParser();

            parser.Load(g, new StringReader(TurtleExample));
        }
Ejemplo n.º 10
0
        public void ParsingDefaultPrefixFallbackTurtle2()
        {
            String     data   = @"@prefix : <http://default/ns#> . :subj :pred :obj .";
            IRdfReader parser = new TurtleParser();
            Graph      g      = new Graph();

            parser.Load(g, new StringReader(data));
            Assert.False(g.IsEmpty);
            Assert.Equal(1, g.Triples.Count);
        }
Ejemplo n.º 11
0
        public void WritingHtmlSchemaWriter()
        {
            //Load the Graph from within the Assembly
            Graph g = new Graph();
            TurtleParser parser = new TurtleParser();
            parser.Load(g, new StreamReader(Assembly.GetAssembly(typeof(IGraph)).GetManifestResourceStream("VDS.RDF.Configuration.configuration.ttl"), Encoding.UTF8));

            //Now generate the HTML file
            VDS.RDF.Writing.HtmlSchemaWriter writer = new HtmlSchemaWriter();
            writer.Save(g, "configSchema.html");
        }
Ejemplo n.º 12
0
        public void ParsingTurtleWithAndWithoutBOM()
        {
            TurtleParser parser = new TurtleParser();
            Graph        g      = new Graph();

            FileLoader.Load(g, "ttl-with-bom.ttl");

            Graph h = new Graph();

            FileLoader.Load(h, "ttl-without-bom.ttl");

            Assert.AreEqual(g, h, "Graphs should be equal as presence (or lack thereof) of UTF-8 BOM should make no difference");
        }
Ejemplo n.º 13
0
        public void ParsingTurtleWithAndWithoutBOM()
        {
            TurtleParser parser = new TurtleParser();
            Graph        g      = new Graph();

            FileLoader.Load(g, "resources\\ttl-with-bom.ttl");

            Graph h = new Graph();

            FileLoader.Load(h, "resources\\ttl-without-bom.ttl");

            Assert.Equal(g, h);
        }
        private void ParsingUsingWriteThroughHandler(Type formatterType)
        {
            if (!System.IO.File.Exists("temp.ttl"))
            {
                Graph g = new Graph();
                EmbeddedResourceLoader.Load(g, "VDS.RDF.Configuration.configuration.ttl");
                g.SaveToFile("temp.ttl");
            }

            WriteThroughHandler handler = new WriteThroughHandler(formatterType, Console.Out, false);
            TurtleParser parser = new TurtleParser();
            parser.Load(handler, "temp.ttl");
        }
        public void ParsingStringReaderEncoding()
        {
            String test = "<http://example.org/subject> <http://example.org/predicate> \"" + (char)32769 + "\" . ";

            TurtleParser parser = new TurtleParser();
            Graph g = new Graph();
            parser.Load(g, new StringReader(test));
            g.SaveToFile("encoding.ttl");

            Graph h = new Graph();
            parser.Load(h, "encoding.ttl");

            Assert.AreEqual(g, h);
        }
Ejemplo n.º 16
0
        public void ParsingSpeedTurtle500Thousand()
        {
            EnsureTestData(500000, "500thou.ttl", new TurtleFormatter());

            Graph g = new Graph(new IndexedTripleCollection(1));
            Stopwatch watch = new Stopwatch();
            TurtleParser parser = new TurtleParser();

            watch.Start();
            parser.Load(g, "500thou.ttl");
            watch.Stop();

            Console.WriteLine(watch.Elapsed);
            this.CalculateSpeed(500000, watch);
        }
Ejemplo n.º 17
0
        public void ParsingStringReaderEncoding()
        {
            String test = "<http://example.org/subject> <http://example.org/predicate> \"" + (char)32769 + "\" . ";

            TurtleParser parser = new TurtleParser();
            Graph        g      = new Graph();

            parser.Load(g, new StringReader(test));
            g.SaveToFile("encoding.ttl");

            Graph h = new Graph();

            parser.Load(h, "encoding.ttl");

            Assert.Equal(g, h);
        }
Ejemplo n.º 18
0
        public void ParsingMultiHandlerTwoGraphs()
        {
            EnsureTestData();

            Graph g = new Graph();
            Graph h = new Graph();

            GraphHandler handler1 = new GraphHandler(g);
            GraphHandler handler2 = new GraphHandler(h);

            MultiHandler handler = new MultiHandler(new IRdfHandler[] { handler1, handler2 });

            TurtleParser parser = new TurtleParser();
            parser.Load(handler, "temp.ttl");

            Assert.AreEqual(g.Triples.Count, h.Triples.Count, "Expected same number of Triples");
            Assert.AreEqual(g, h, "Expected Graphs to be equal");
        }
Ejemplo n.º 19
0
        public void ParsingMultiHandlerGraphAndPaging()
        {
            EnsureTestData();

            Graph g = new Graph();
            Graph h = new Graph();

            GraphHandler handler1 = new GraphHandler(g);
            PagingHandler handler2 = new PagingHandler(new GraphHandler(h), 100);

            MultiHandler handler = new MultiHandler(new IRdfHandler[] { handler1, handler2 });

            TurtleParser parser = new TurtleParser();
            parser.Load(handler, "temp.ttl");

            Assert.AreEqual(101, g.Triples.Count, "Triples should have been limited to 101 (1st Graph)");
            Assert.AreEqual(100, h.Triples.Count, "Triples should have been limited to 100 (2nd Graph)");
            Assert.AreNotEqual(g.Triples.Count, h.Triples.Count, "Expected different number of Triples");
            Assert.AreNotEqual(g, h, "Expected Graphs to not be equal");
        }
        private void TestWriteToStoreHandler(IGenericIOManager manager)
        {
            //First ensure that our test file exists
            EnsureTestData();

            //Try to ensure that the target Graph does not exist
            if (manager.DeleteSupported)
            {
                manager.DeleteGraph(TestGraphUri);
            }
            else
            {
                Graph g = new Graph();
                g.BaseUri = TestGraphUri;
                manager.SaveGraph(g);
            }

            Graph temp = new Graph();
            try
            {
                manager.LoadGraph(temp, TestGraphUri);
                Assert.IsTrue(temp.IsEmpty, "Unable to ensure that Target Graph in Store is empty prior to running Test");
            }
            catch
            {
                //An Error Loading the Graph is OK
            }

            WriteToStoreHandler handler = new WriteToStoreHandler(manager, TestGraphUri, 100);
            TurtleParser parser = new TurtleParser();
            parser.Load(handler, "temp.ttl");

            manager.LoadGraph(temp, TestGraphUri);
            Assert.IsFalse(temp.IsEmpty, "Graph should not be empty");

            Graph orig = new Graph();
            orig.LoadFromFile("temp.ttl");

            Assert.AreEqual(orig, temp, "Graphs should be equal");
        }
Ejemplo n.º 21
0
        static void Test0()
        {
            Console.WriteLine("JsonLDIntegrationTests.Test0");

            IGraph g = new Graph();

            TurtleParser parser = new TurtleParser();
            parser.Load(g, "datatypes.test.ttl");

            System.IO.StringWriter stringWriter = new System.IO.StringWriter();

            JToken frame;
            using (JsonReader reader = new JsonTextReader(new StreamReader("datatypes.context.json")))
            {
                frame = JToken.Load(reader);
            }

            JsonLdWriter jsonLdWriter = new JsonLdWriter();
            jsonLdWriter.Save(g, stringWriter);

            JToken flattened = JToken.Parse(stringWriter.ToString());

            JObject framed = JsonLdProcessor.Frame(flattened, frame, new JsonLdOptions());
            JObject compacted = JsonLdProcessor.Compact(framed, framed["@context"], new JsonLdOptions());

            Console.WriteLine(compacted);

            JToken flattened2 = JsonLdProcessor.Flatten(compacted, new JsonLdOptions());

            IGraph g2 = new Graph();
            JsonLdReader jsonLdReader = new JsonLdReader();
            jsonLdReader.Load(g2, new StringReader(flattened2.ToString()));

            CompressingTurtleWriter turtleWriter = new CompressingTurtleWriter();

            turtleWriter.DefaultNamespaces.AddNamespace("ns", new Uri("http://tempuri.org/schema#"));

            turtleWriter.Save(g2, Console.Out);
        }
Ejemplo n.º 22
0
        public void ParsingSpeedTurtle100Thousand()
        {
            try
            {
                Options.InternUris = false;
                EnsureTestData(100000, "100thou.ttl", new TurtleFormatter());

                Graph        g      = new Graph();
                Stopwatch    watch  = new Stopwatch();
                TurtleParser parser = new TurtleParser();

                watch.Start();
                parser.Load(g, "100thou.ttl");
                watch.Stop();

                Console.WriteLine(watch.Elapsed.ToString());
                this.CalculateSpeed(100000, watch);
            }
            finally
            {
                Options.InternUris = true;
            }
        }
Ejemplo n.º 23
0
        public void ParsingRelativeUriAppBaseTurtle()
        {
            //This invocation succeeds because we define a Base URI
            //resolve against
            Graph g = new Graph();

            g.BaseUri = new Uri("http://example.org");
            TurtleParser parser = new TurtleParser();

            parser.Load(g, new StringReader(TurtleExample));

            //Expect a non-empty grpah with a single triple
            Assert.IsFalse(g.IsEmpty);
            Assert.AreEqual(1, g.Triples.Count);
            Triple t = g.Triples.First();

            //Predicate should get it's relative URI resolved into
            //the correct HTTP URI
            Uri obj = ((IUriNode)t.Object).Uri;

            Assert.AreEqual("http", obj.Scheme);
            Assert.AreEqual("example.org", obj.Host);
            Assert.AreEqual("relative", obj.Segments[1]);
        }
        public void WritingCollectionCompressionNamedListNodes3()
        {
            Graph g = new Graph();
            INode data1 = g.CreateBlankNode();
            g.Assert(data1, g.CreateUriNode(new Uri("http://property")), g.CreateLiteralNode("test1"));
            INode data2 = g.CreateBlankNode();
            g.Assert(data2, g.CreateUriNode(new Uri("http://property")), g.CreateLiteralNode("test2"));

            INode listEntry1 = g.CreateUriNode(new Uri("http://test/1"));
            INode rdfFirst = g.CreateUriNode(new Uri(RdfSpecsHelper.RdfListFirst));
            INode rdfRest = g.CreateUriNode(new Uri(RdfSpecsHelper.RdfListRest));
            INode rdfNil = g.CreateUriNode(new Uri(RdfSpecsHelper.RdfListNil));
            g.Assert(listEntry1, rdfFirst, data1);
            g.Assert(listEntry1, rdfRest, rdfNil);

            INode listEntry2 = g.CreateUriNode(new Uri("http://test/2"));
            g.Assert(listEntry2, rdfFirst, data2);
            g.Assert(listEntry2, rdfRest, listEntry1);

            INode root = g.CreateUriNode(new Uri("http://root"));
            g.Assert(root, g.CreateUriNode(new Uri("http://list")), listEntry2);

            NTriplesFormatter formatter = new NTriplesFormatter();
            Console.WriteLine("Original Graph");
            foreach (Triple t in g.Triples)
            {
                Console.WriteLine(t.ToString(formatter));
            }
            Console.WriteLine();

            CompressingTurtleWriterContext context = new CompressingTurtleWriterContext(g, Console.Out);
            WriterHelper.FindCollections(context);
            Console.WriteLine(context.Collections.Count + " Collections Found");
            Console.WriteLine();

            System.IO.StringWriter strWriter = new System.IO.StringWriter();
            CompressingTurtleWriter writer = new CompressingTurtleWriter();
            writer.CompressionLevel = WriterCompressionLevel.High;
            writer.Save(g, strWriter);

            Console.WriteLine("Compressed Turtle");
            Console.WriteLine(strWriter.ToString());
            Console.WriteLine();

            Graph h = new Graph();
            TurtleParser parser = new TurtleParser();
            StringParser.Parse(h, strWriter.ToString());
            Console.WriteLine("Graph after Round Trip to Compressed Turtle");
            foreach (Triple t in h.Triples)
            {
                Console.WriteLine(t.ToString(formatter));
            }

            Assert.AreEqual(g, h, "Graphs should be equal");
        }
Ejemplo n.º 25
0
        public void StorageNativeGraph()
        {
            try
            {
                //Load in our Test Graph
                TurtleParser ttlparser = new TurtleParser();
                Graph g = new Graph();
                ttlparser.Load(g, "Turtle.ttl");

                Console.WriteLine("Loaded Test Graph OK");
                Console.WriteLine("Test Graph contains:");

                Assert.IsFalse(g.IsEmpty, "Test Graph should be non-empty");

                foreach (Triple t in g.Triples)
                {
                    Console.WriteLine(t.ToString());
                }
                Console.WriteLine();

                //Create our Native Managers
                List<IGenericIOManager> managers = new List<IGenericIOManager>() {
                    new MicrosoftSqlStoreManager("localhost", "dotnetrdf_experimental","example","password"),
                    new VirtuosoManager("localhost", 1111, "DB", VirtuosoTest.VirtuosoTestUsername, VirtuosoTest.VirtuosoTestPassword)
                };

                //Save the Graph to each Manager
                foreach (IGenericIOManager manager in managers)
                {
                    Console.WriteLine("Saving using '" + manager.GetType().ToString() + "'");
                    manager.SaveGraph(g);
                    Console.WriteLine("Saved OK");
                    Console.WriteLine();
                }

                //Load Back from each Manager
                foreach (IGenericIOManager manager in managers)
                {
                    Console.WriteLine("Loading using '" + manager.GetType().ToString() + "' with a NativeGraph");
                    StoreGraphPersistenceWrapper native = new StoreGraphPersistenceWrapper(manager, g.BaseUri);
                    Console.WriteLine("Loaded OK");

                    Assert.IsFalse(native.IsEmpty, "Retrieved Graph should contain Triples");
                    Assert.AreEqual(g.Triples.Count, native.Triples.Count, "Retrieved Graph should contain same number of Triples as original Graph");

                    Console.WriteLine("Loaded Graph contains:");
                    foreach (Triple t in native.Triples)
                    {
                        Console.WriteLine(t.ToString());
                    }
                    Console.WriteLine();

                    native.Dispose();
                }
            }
            catch (RdfStorageException storeEx)
            {
                TestTools.ReportError("Storage Error", storeEx, true);
            }
            catch (Exception ex)
            {
                TestTools.ReportError("Other Error", ex, true);
            }
        }
Ejemplo n.º 26
0
        public static void Main(string[] args)
        {
            StreamWriter output = new StreamWriter("MergeTest.txt");
            try
            {
                //Set Output
                Console.SetOut(output);

                Console.WriteLine("## Merge Test Suite");

                //Load the Test RDF
                TurtleParser ttlparser = new TurtleParser();
                Graph g = new Graph();
                Graph h = new Graph();
                ttlparser.Load(g, "MergePart1.ttl");
                ttlparser.Load(h, "MergePart2.ttl");

                Console.WriteLine("Merge Test Data Loaded OK");
                Console.WriteLine();

                Console.WriteLine("Graph 1 Contains");
                foreach (Triple t in g.Triples)
                {
                    Console.WriteLine(t.ToString());
                }

                Console.WriteLine();
                Console.WriteLine("Graph 2 Contains");
                foreach (Triple t in h.Triples)
                {
                    Console.WriteLine(t.ToString());
                }

                Console.WriteLine();

                Console.WriteLine("Attempting Graph Merge");
                g.Merge(h);
                Console.WriteLine();

                foreach (Triple t in g.Triples)
                {
                    Console.WriteLine(t.ToString());
                }

                //Use a GraphViz Generator to picture this
                Console.WriteLine();
                Console.WriteLine("Visualizing Merged Graph as SVG with GraphViz");
                GraphVizGenerator gvzgen = new GraphVizGenerator("svg");
                gvzgen.Generate(g, "MergeTest.svg", false);
                Console.WriteLine("Visualisation created as MergeTest.svg");

                //Same merge into an Empty Graph
                Console.WriteLine();
                Console.WriteLine("Combining the two Graphs with two Merge operations into an Empty Graph");
                Graph i = new Graph();

                //Need to reload g from disk
                g = new Graph();
                ttlparser.Load(g, "MergePart1.ttl");

                //Do the actual merge
                i.Merge(g);
                i.Merge(h);
                Console.WriteLine();

                foreach (Triple t in i.Triples)
                {
                    Console.WriteLine(t.ToString());
                }


            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
            }

            output.Close();
        }
Ejemplo n.º 27
0
        public void StorageVirtuosoSaveGraph()
        {
            NTriplesFormatter formatter = new NTriplesFormatter();
            try
            {
                VirtuosoManager manager = new VirtuosoManager("DB", VirtuosoTestUsername, VirtuosoTestPassword);
                Assert.IsNotNull(manager);

                Console.WriteLine("Got the Virtuoso Manager OK");

                //Load in our Test Graph
                TurtleParser ttlparser = new TurtleParser();
                Graph g = new Graph();
                ttlparser.Load(g, "Turtle.ttl");

                Console.WriteLine();
                Console.WriteLine("Loaded Test Graph OK");
                Console.WriteLine("Test Graph contains:");

                Assert.IsFalse(g.IsEmpty, "Test Graph should be non-empty");

                foreach (Triple t in g.Triples)
                {
                    Console.WriteLine(t.ToString(formatter));
                }
                Console.WriteLine();

                //Try to save to Virtuoso
                manager.SaveGraph(g);
                Console.WriteLine("Saved OK");
                Console.WriteLine();

                //Try to retrieve
                Graph h = new Graph();
                manager.LoadGraph(h, "http://example.org");

                Assert.IsFalse(h.IsEmpty, "Retrieved Graph should be non-empty");

                Console.WriteLine("Retrieved the Graph from Virtuoso OK");
                Console.WriteLine("Retrieved Graph contains:");
                foreach (Triple t in h.Triples)
                {
                    Console.WriteLine(t.ToString(formatter));
                }

                Assert.AreEqual(g.Triples.Count, h.Triples.Count, "Graph should have same number of Triples before and after saving");

                GraphDiffReport diff = h.Difference(g);

                Console.WriteLine();
                if (!diff.AreEqual)
                {
                    Console.WriteLine("Some Differences in Graphs detected (should only be due to Virtuoso not running xsd:boolean as true/false");
                    Console.WriteLine();

                    TestTools.ShowDifferences(diff);

                    IUriNode allowedDiffSubject = g.CreateUriNode(":four");
                    IUriNode allowedDiffSubject2 = g.CreateUriNode(":six");
                    Assert.IsTrue(diff.RemovedTriples.All(t => t.Subject.Equals(allowedDiffSubject2) || (t.Subject.Equals(allowedDiffSubject) && (t.Object.ToString().Equals("true^^" + XmlSpecsHelper.XmlSchemaDataTypeBoolean) || t.Object.ToString().Equals("false^^" + XmlSpecsHelper.XmlSchemaDataTypeBoolean)))), "Removed Triples should only be those with subject :four and boolean object");
                    Assert.IsTrue(diff.AddedTriples.All(t => t.Subject.Equals(allowedDiffSubject2) || (t.Subject.Equals(allowedDiffSubject) && (t.Object.ToString().Equals("1") || t.Object.ToString().Equals("0")))), "Added Triples should only be those with subject :four and 1/0 in place of boolean object");
                }
                else
                {
                    Console.WriteLine("Graphs are equal");
                }
            }
            catch (VirtuosoException virtEx)
            {
                TestTools.ReportError("Virtuoso Error", virtEx, true);
            }
            catch (Exception ex)
            {
                TestTools.ReportError("Other Error", ex, true);
            }
        }
Ejemplo n.º 28
0
        public void WritingNTriplesCharEscaping()
        {
            TurtleParser parser = new TurtleParser();
            NTriplesParser ntparser = new NTriplesParser();
            NTriplesWriter ntwriter = new NTriplesWriter();

            foreach (String sample in samples)
            {
                Graph g = new Graph();
                Console.WriteLine("Original RDF Fragment");
                Console.WriteLine(prefix + sample);
                StringParser.Parse(g, prefix + sample, parser);
                Console.WriteLine();
                Console.WriteLine("Triples in Original");
                foreach (Triple t in g.Triples)
                {
                    Console.WriteLine(t.ToString());
                }
                Console.WriteLine();

                String serialized = StringWriter.Write(g, ntwriter);
                Console.WriteLine("Serialized RDF Fragment");
                Console.WriteLine(serialized);

                Graph h = new Graph();
                StringParser.Parse(h, serialized, ntparser);

                Console.WriteLine();
                Console.WriteLine("Triples in Serialized");
                foreach (Triple t in g.Triples)
                {
                    Console.WriteLine(t.ToString());
                }
                Console.WriteLine();

                Assert.AreEqual(g, h, "Graphs should have been equal");

                Console.WriteLine("Graphs were equal as expected");
                Console.WriteLine();
            }
        }
Ejemplo n.º 29
0
        public void ParsingSpeedTurtle10ThousandCountOnly()
        {
            EnsureTestData(10000, "10thou.ttl", new TurtleFormatter());

            CountHandler handler = new CountHandler();
            Stopwatch watch = new Stopwatch();
            TurtleParser parser = new TurtleParser();

            watch.Start();
            parser.Load(handler, "10thou.ttl");
            watch.Stop();

            Console.WriteLine(watch.Elapsed);
            this.CalculateSpeed(10000, watch);

            Assert.AreEqual(10000, handler.Count);
        }
        private int ProcessEvaluationTest(SparqlQueryParser parser, Triple commentDef, String queryFile, String dataFile, List<String> dataFiles, String resultFile)
        {
            Console.WriteLine("# Processing Evaluation Test " + Path.GetFileName(queryFile));

            if (commentDef != null)
            {
                Console.WriteLine(commentDef.Object.ToString());
                Console.WriteLine();
            }

            if (dataFiles.Contains(dataFile)) dataFiles.Remove(dataFile);
            if (queryFile.StartsWith("file:///")) queryFile = queryFile.Substring(8);
            if (dataFile != null && dataFile.StartsWith("file:///")) dataFile = dataFile.Substring(8);
            if (resultFile.StartsWith("file:///")) resultFile = resultFile.Substring(8);

            Console.WriteLine("Query File is " + queryFile);
            if (evaluationTestOverride.Any(x => queryFile.EndsWith(x)))
            {
                Console.WriteLine();
                Console.WriteLine("# Test Result = Manually overridden to Pass (Test Passed)");
                testsPassed++;
                testsEvaluationPassed++;
                return 1;
            }
            if (dataFile != null) Console.WriteLine("Default Graph File is " + dataFile);
            foreach (String file in dataFiles)
            {
                Console.WriteLine("Uses Named Graph File " + file);
            }
            Console.WriteLine("Expected Result File is " + resultFile);
            Console.WriteLine();

            SparqlQuery query;
            try
            {
                query = parser.ParseFromFile(queryFile);

                Console.WriteLine(query.ToString());
                Console.WriteLine();
                Console.WriteLine("Formatted with SparqlFormatter");
                SparqlFormatter formatter = new SparqlFormatter(query.NamespaceMap);
                Console.WriteLine(formatter.Format(query));
                Console.WriteLine();

                try
                {
                    Console.WriteLine(query.ToAlgebra().ToString());
                    Console.WriteLine();
                }
                catch
                {
                    //Do Nothing
                }
            }
            catch (RdfParseException parseEx)
            {
                this.ReportError("Query Parser Error", parseEx);
                testsFailed++;
                testsEvaluationFailed++;
                Console.WriteLine("# Test Result = Unable to parse query (Test Failed)");
                return -1;
            }

            IInMemoryQueryableStore store;
            if (dataFile != null)
            {
                store = new TripleStore();
            }
            else
            {
                store = new WebDemandTripleStore();
            }

            //Load Default Graph
            Graph defaultGraph = new Graph();
            try
            {
                if (dataFile != null)
                {
                    FileLoader.Load(defaultGraph, dataFile);
                }
                store.Add(defaultGraph);
            }
            catch (RdfParseException parseEx)
            {
                this.ReportError("Parser Error", parseEx);
                testsFailed++;
                testsEvaluationFailed++;
                Console.WriteLine("# Test Result = Unable to parse Default Graph (Test Failed)");
                return -1;
            }

            //Load Named Graphs
            try
            {
                foreach (String graphFile in dataFiles)
                {
                    Graph namedGraph = new Graph();
                    if (graphFile.StartsWith("file:///"))
                    {
                        FileLoader.Load(namedGraph, graphFile.Substring(8));
                    }
                    else
                    {
                        FileLoader.Load(namedGraph, graphFile);
                    }
                    store.Add(namedGraph);
                }
            }
            catch (RdfParseException parseEx)
            {
                this.ReportError("Parser Error", parseEx);
                testsFailed++;
                testsEvaluationFailed++;
                Console.WriteLine("# Test Result - Unable to parse Named Graph (Test Failed)");
                return -1;
            }

            //Create a Dataset and then Set Graphs
            InMemoryDataset dataset = new InMemoryDataset(store);
            if (!query.DefaultGraphs.Any())
            {
                query.AddDefaultGraph(defaultGraph.BaseUri);
                //dataset.SetActiveGraph(defaultGraph.BaseUri);
            }
            if (!query.NamedGraphs.Any())
            {
                foreach (String namedGraphUri in dataFiles)
                {
                    query.AddNamedGraph(new Uri(namedGraphUri));
                }
            }
            
            //Try and get the result
            Object results = null;
            try
            {
                results = query.Evaluate(dataset);
            }
            catch (RdfQueryException queryEx)
            {
                this.ReportError("Query Error", queryEx);
                testsFailed++;
                testsEvaluationFailed++;
                Console.WriteLine("# Test Result - Query execution failed (Test Failed)");
                return -1;
            }
            catch (Exception ex)
            {
                this.ReportError("Other Error", ex);
                testsFailed++;
                testsEvaluationFailed++;
                Console.WriteLine("# Test Result - Query failed (Test Failed)");
                return -1;
            }

            if (results == null)
            {
                testsFailed++;
                testsEvaluationFailed++;
                Console.WriteLine("# Test Result - No result was returned from the Query (Test Failed)");
                return -1;
            }

            //Load in the expected results
            if (results is SparqlResultSet)
            {
                //Save our Results so we can manually compare as needed
                SparqlResultSet ourResults = (SparqlResultSet)results;
                SparqlXmlWriter writer = new SparqlXmlWriter();
                writer.Save(ourResults, resultFile + ".out");
                SparqlResultSet expectedResults = new SparqlResultSet();

                if (resultFile.EndsWith(".srx"))
                {
                    try
                    {
                        SparqlXmlParser resultSetParser = new SparqlXmlParser();
                        resultSetParser.Load(expectedResults, resultFile);
                    }
                    catch (RdfParseException parseEx)
                    {
                        this.ReportError("Result Set Parser Error", parseEx);
                        testsIndeterminate++;
                        testsEvaluationIndeterminate++;
                        Console.WriteLine("# Test Result - Error loading expected Result Set (Test Indeterminate)");
                        return 0;
                    }
                }
                else if (resultFile.EndsWith(".ttl") || resultFile.EndsWith(".rdf"))
                {
                    try
                    {
                        SparqlRdfParser resultSetParser = new SparqlRdfParser();
                        resultSetParser.Load(expectedResults, resultFile);
                    }
                    catch (RdfParseException parseEx)
                    {
                        this.ReportError("Result Set Parser Error", parseEx);
                        testsIndeterminate++;
                        testsEvaluationIndeterminate++;
                        Console.WriteLine("# Test Result - Error loading expected Result Set (Test Indeterminate)");
                        return 0;
                    }
                }
                else
                {
                    testsIndeterminate++;
                    testsEvaluationIndeterminate++;
                    Console.WriteLine("# Test Result - Unable to load the expected Result Set (Test Indeterminate)");
                    return 0;
                }

                try
                {
                    ourResults.Trim();
                    expectedResults.Trim();
                    if (ourResults.Equals(expectedResults))
                    {
                        testsPassed++;
                        testsEvaluationPassed++;
                        Console.WriteLine("# Test Result - Result Set as expected (Test Passed)");
                        return 1;
                    }
                    else
                    {
                        Console.WriteLine("Final Query");
                        Console.WriteLine(query.ToString());
                        Console.WriteLine();
                        this.ShowTestData(store);
                        this.ShowResultSets(ourResults, expectedResults);
                        testsFailed++;
                        testsEvaluationFailed++;
                        Console.WriteLine("# Test Result - Result Set not as expected (Test Failed)");
                        return -1;
                    }
                }
                catch (NotImplementedException)
                {
                    this.ShowResultSets(ourResults, expectedResults);
                    testsIndeterminate++;
                    testsEvaluationIndeterminate++;
                    Console.WriteLine("# Test Result - Unable to establish if Result Set was as expected (Test Indeterminate)");
                    return 0;
                }
            }
            else if (results is Graph)
            {
                if (resultFile.EndsWith(".ttl"))
                {
                    //Save our Results so we can manually compare as needed
                    Graph ourResults = (Graph)results;
                    CompressingTurtleWriter writer = new CompressingTurtleWriter();
                    writer.Save(ourResults, resultFile + ".out");

                    try
                    {
                        Graph expectedResults = new Graph();
                        TurtleParser ttlparser = new TurtleParser();
                        ttlparser.Load(expectedResults, resultFile);

                        try
                        {
                            if (ourResults.Equals(expectedResults))
                            {
                                testsPassed++;
                                testsEvaluationPassed++;
                                Console.WriteLine("# Test Result - Graph as expected (Test Passed)");
                                return 1;
                            }
                            else
                            {
                                this.ShowTestData(store);
                                this.ShowGraphs(ourResults, expectedResults);
                                testsFailed++;
                                testsEvaluationFailed++;
                                Console.WriteLine("# Test Result - Graph not as expected (Test Failed)");
                                return -1;
                            }
                        }
                        catch (NotImplementedException)
                        {
                            this.ShowGraphs(ourResults, expectedResults);
                            testsIndeterminate++;
                            testsEvaluationIndeterminate++;
                            Console.WriteLine("# Test Result - Unable to establish if Graph was as expected (Test Indeterminate)");
                            return 0;
                        }
                    }
                    catch (RdfParseException parseEx)
                    {
                        this.ReportError("Graph Parser Error", parseEx);
                        testsIndeterminate++;
                        testsEvaluationIndeterminate++;
                        Console.WriteLine("# Test Result - Error loading expected Graph (Test Indeterminate)");
                        return 0;
                    }
                }
                else
                {
                    testsIndeterminate++;
                    testsEvaluationIndeterminate++;
                    Console.WriteLine("# Test Result - Unable to load expected Graph (Test Indeterminate)");
                    return 0;
                }
            }
            else
            {
                testsFailed++;
                testsEvaluationFailed++;
                Console.WriteLine("# Test Result - Didn't produce a Graph as expected (Test Failed)");
                return -1;
            }

        }
Ejemplo n.º 31
0
        /// <summary>
        /// Internal Method which performs multi-threaded reading of data
        /// </summary>
        private void LoadGraphs(FolderStoreParserContext context)
        {
            //Create the relevant Parser
            IRdfReader parser;

            switch (context.Format)
            {
            case FolderStoreFormat.Turtle:
                parser = new TurtleParser();
                break;

            case FolderStoreFormat.Notation3:
                parser = new Notation3Parser();
                break;

            case FolderStoreFormat.RdfXml:
                parser = new RdfXmlParser();
                break;

            default:
                parser = new TurtleParser();
                break;
            }

            try
            {
                String file = context.GetNextFilename();
                while (file != null)
                {
                    //Read from Disk
                    Graph  g          = new Graph();
                    String sourceFile = Path.Combine(context.Folder, file);
                    parser.Load(g, sourceFile);

                    //Add to Graph Collection
                    foreach (Triple t in g.Triples)
                    {
                        if (context.Terminated)
                        {
                            break;
                        }
                        if (!context.Handler.HandleTriple(t))
                        {
                            ParserHelper.Stop();
                        }
                    }

                    if (context.Terminated)
                    {
                        break;
                    }

                    //Get the Next Filename
                    file = context.GetNextFilename();
                }
            }
            catch (ThreadAbortException)
            {
                //We've been terminated, don't do anything
#if !SILVERLIGHT
                Thread.ResetAbort();
#endif
            }
            catch (RdfParsingTerminatedException)
            {
                context.Terminated = true;
                context.ClearFilenames();
            }
            catch (Exception ex)
            {
                throw new RdfStorageException("Error in Threaded Reader in Thread ID " + Thread.CurrentThread.ManagedThreadId, ex);
            }
        }
Ejemplo n.º 32
0
        /// <summary>
        /// Internal Method which performs multi-threaded reading of data
        /// </summary>
        private void LoadGraphs(FolderStoreParserContext context)
        {
            //Create the relevant Parser
            IRdfReader parser;
            switch (context.Format)
            {
                case FolderStoreFormat.Turtle:
                    parser = new TurtleParser();
                    break;
                case FolderStoreFormat.Notation3:
                    parser = new Notation3Parser();
                    break;
                case FolderStoreFormat.RdfXml:
                    parser = new RdfXmlParser();
                    break;
                default:
                    parser = new TurtleParser();
                    break;
            }

            try
            {
                String file = context.GetNextFilename();
                while (file != null)
                {
                    //Read from Disk
                    Graph g = new Graph();
                    String sourceFile = Path.Combine(context.Folder, file);
                    parser.Load(g, sourceFile);

                    //Add to Graph Collection
                    foreach (Triple t in g.Triples)
                    {
                        if (context.Terminated) break;
                        if (!context.Handler.HandleTriple(t)) ParserHelper.Stop();
                    }

                    if (context.Terminated) break;

                    //Get the Next Filename
                    file = context.GetNextFilename();
                }
            }
            catch (ThreadAbortException)
            {
                //We've been terminated, don't do anything
#if !SILVERLIGHT
                Thread.ResetAbort();
#endif
            }
            catch (RdfParsingTerminatedException)
            {
                context.Terminated = true;
                context.ClearFilenames();
            }
            catch (Exception ex)
            {
                throw new RdfStorageException("Error in Threaded Reader in Thread ID " + Thread.CurrentThread.ManagedThreadId, ex);
            }
        }
Ejemplo n.º 33
0
        public static void Main(string[] args)
        {
            //Read in Parameters
            if (args.Length < 3) args = new string[] { "both","30","true" };
            String mode = args[0].ToLower();
            if (mode != "read" && mode != "write" && mode != "both") mode = "both";
            int runs = Int32.Parse(args[1]);
            if (runs < 1) runs = 1;
            bool allowHiSpeed = Boolean.Parse(args[2]);

            StreamWriter output = new StreamWriter("DiskIOSpeedTests.txt");
            Console.SetOut(output);

            Stopwatch timer = new Stopwatch();

            if (mode.Equals("read") || mode.Equals("both"))
            {
                Console.WriteLine("## Read Speed Tests");
                Console.WriteLine("# Compares the Speed of reading the same Graph n times");

                try
                {
                    List<IRdfReader> readers = new List<IRdfReader>() { 

                        new NTriplesParser(),
                        new TurtleParser(),
                        new Notation3Parser(), 
                        new RdfXmlParser(RdfXmlParserMode.DOM),
                        new RdfXmlParser(RdfXmlParserMode.Streaming),
                        new RdfJsonParser()/*,
                        new RdfAParser()*/
                    };
                    List<String> files = new List<string>() { 
                        "test.nt", 
                        "test.ttl", 
                        "test.n3", 
                        "test.rdf",
                        "test.rdf",
                        "test.json",
                        "test.nt.json"/*,
                        "test.html"*/
                    };
                    IRdfReader reader;

                    for (int i = 0; i < readers.Count; i++)
                    {
                        reader = readers[i];

                        long totalTime = 0;
                        int totalTriples = 0;
                        int triples;
                        DateTime start, finish;
                        double diff;
                        Console.WriteLine("# Profiling Reader '" + reader.GetType().ToString() + "' - " + reader.ToString());
                        Debug.WriteLine("# Profiling Reader '" + reader.GetType().ToString() + "' - " + reader.ToString());
                        Console.WriteLine("Test File is " + files[i]);
                        Console.WriteLine();

                        for (int j = 1; j <= runs; j++)
                        {
                            Console.WriteLine("Run #" + j);
                            Debug.WriteLine("Run #" + j);

                            timer.Reset();

                            //Start Time
                            start = DateTime.Now;
                            Console.WriteLine("Start @ " + start.ToString(TestSuite.TestSuiteTimeFormat));
                            Debug.WriteLine("Start @ " + start.ToString(TestSuite.TestSuiteTimeFormat));

                            //Load
                            try
                            {
                                Graph g = new Graph();
                                timer.Start();
                                reader.Load(g, "diskio_tests/" + files[i]);
                                timer.Stop();
                                triples = g.Triples.Count;
                            }
                            catch (Exception ex)
                            {
                                triples = 0;
                                reportError(output, "Exception while Reading", ex);
                            }

                            //End Time
                            finish = DateTime.Now;
                            Console.WriteLine("End @ " + finish.ToString(TestSuite.TestSuiteTimeFormat));
                            Debug.WriteLine("End @ " + finish.ToString(TestSuite.TestSuiteTimeFormat));

                            //Compute Load Rate
                            diff = (double)timer.ElapsedMilliseconds;
                            Console.WriteLine("Read Time was " + diff / 1000d + " seconds");
                            Console.WriteLine("Read Rate was " + (triples / diff) * 1000 + " triples/second");
                            Debug.WriteLine("Read Rate was " + (triples / diff) * 1000 + " triples/second");

                            //Running Totals
                            totalTriples += triples;
                            totalTime += timer.ElapsedMilliseconds;

                            Console.WriteLine();
                        }

                        //Compute Averages
                        double dtime = (double)totalTime;
                        Console.WriteLine("Average Read Time was " + totalTime / 1000 / runs + " seconds");
                        Console.WriteLine("Average Read Rate was " + totalTriples / (dtime / 1000d) + " triples/second");
                        Debug.WriteLine("Average Read Rate was " + totalTriples / (dtime / 1000d) + " triples/second");
                        Console.WriteLine();
                    }
                }
                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();
            }

            if (mode.Equals("write") || mode.Equals("both"))
            {
                Console.WriteLine("## Write Speed Tests");
                Console.WriteLine("# Compares the Speed of serializing the same Graph n times");

                try
                {
                    //Load up the Test Graph
                    Console.WriteLine();
                    Graph g = new Graph();
                    g.BaseUri = new Uri(TestSuiteBaseUri);
                    TurtleParser ttlparser = new TurtleParser();
                    ttlparser.Load(g, "diskio_tests/test.ttl");
                    Console.WriteLine("# Test Graph Loaded");
                    int triples = g.Triples.Count;
                    Console.WriteLine("# Test Graph contains " + triples + " Triples");
                    Console.WriteLine();
                    Debug.WriteLine("Test Graph Loaded");

                    List<IRdfWriter> writers = new List<IRdfWriter>()
                    {
                        new RdfXmlWriter(),
                        new FastRdfXmlWriter(),
                        new PrettyRdfXmlWriter(),
                        new NTriplesWriter(),
                        new TurtleWriter(),
                        new CompressingTurtleWriter(),
                        new Notation3Writer(),
                        new RdfJsonWriter(),
                        new HtmlWriter()
                    };

                    foreach (IRdfWriter writer in writers)
                    {
                        long totalTime = 0;
                        int totalTriples = 0;
                        DateTime start, finish;
                        long diff;
                        Console.WriteLine("# Profiling Writer '" + writer.GetType().ToString() + "' - " + writer.ToString());
                        Debug.WriteLine("# Profiling Writer '" + writer.GetType().ToString() + "' - " + writer.ToString());
                        if (writer is ICompressingWriter)
                        {
                            Console.WriteLine("Compression Level is " + ((ICompressingWriter)writer).CompressionLevel);
                        }
                        Console.WriteLine();

                        if (writer is IHighSpeedWriter)
                        {
                            ((IHighSpeedWriter)writer).HighSpeedModePermitted = allowHiSpeed;
                            if (allowHiSpeed)
                            {
                                Console.WriteLine("Using High Speed Mode");
                            }
                        }

                        //Disable Pretty Printing
                        if (writer is IPrettyPrintingWriter)
                        {
                            ((IPrettyPrintingWriter)writer).PrettyPrintMode = false;
                        }

                        for (int i = 1; i <= runs; i++)
                        {
                            Console.WriteLine("Run #" + i);
                            Debug.WriteLine("Run #" + i);

                            //Start Time
                            start = DateTime.Now;
                            Console.WriteLine("Start @ " + start.ToString(TestSuite.TestSuiteTimeFormat));
                            Debug.WriteLine("Start @ " + start.ToString(TestSuite.TestSuiteTimeFormat));

                            //Load
                            try
                            {
                                writer.Save(g, "diskio_tests/" + writer.GetType().ToString() + ".out");
                            }
                            catch (Exception ex)
                            {
                                reportError(output, "Exception while Writing", ex);
                            }

                            //End Time
                            finish = DateTime.Now;
                            Console.WriteLine("End @ " + finish.ToString(TestSuite.TestSuiteTimeFormat));
                            Debug.WriteLine("End @ " + finish.ToString(TestSuite.TestSuiteTimeFormat));

                            //Compute Load Rate
                            diff = Microsoft.VisualBasic.DateAndTime.DateDiff(Microsoft.VisualBasic.DateInterval.Second, start, finish, Microsoft.VisualBasic.FirstDayOfWeek.Monday, Microsoft.VisualBasic.FirstWeekOfYear.System);
                            if (diff == 0)
                            {
                                diff = finish.Millisecond - start.Millisecond;
                                if (diff < 0) diff += 1000;
                                double smalldiff = (double)diff;
                                Console.WriteLine("Write Time was " + smalldiff / 1000d + " seconds");
                                Console.WriteLine("Write Rate was " + (triples / smalldiff) * 1000 + " triples/second");
                                Debug.WriteLine("Write Rate was " + (triples / smalldiff) * 1000 + " triples/second");
                            }
                            else
                            {
                                Console.WriteLine("Write Time was " + diff + " seconds");
                                Console.WriteLine("Write Rate was " + triples / diff + " triples/second");
                                Debug.WriteLine("Write Rate was " + triples / diff + " triples/second");
                                diff = diff * 1000;
                            }

                            //Running Totals
                            totalTriples += triples;
                            totalTime += diff;

                            Console.WriteLine();
                        }

                        //Compute Averages
                        double dtime = (double)totalTime;
                        Console.WriteLine("Average Write Time was " + totalTime / 1000 / runs + " seconds");
                        Console.WriteLine("Average Write Rate was " + totalTriples / (dtime / 1000d) + " triples/second");
                        Debug.WriteLine("Average Write Rate was " + totalTriples / (dtime / 1000d) + " triples/second");
                        Console.WriteLine();
                    }
                }
                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);
                }
            }

            output.Close();
        }
Ejemplo n.º 34
0
        public static void Main(string[] args)
        {
            StreamWriter output = new StreamWriter("InferenceTest.txt");
            try
            {
                //Set Output
                Console.SetOut(output);

                Console.WriteLine("## Inference Test Suite");

                //Load the Test RDF
                TurtleParser ttlparser = new TurtleParser();
                Graph g = new Graph();
                //ttlparser.TraceParsing = true;
                ttlparser.Load(g, "InferenceTest.ttl");

                Console.WriteLine("Inference Test Data Loaded OK");
                Console.WriteLine();

                Console.WriteLine("Graph contains the following Triples:");
                foreach (Triple t in g.Triples)
                {
                    Console.WriteLine(t.ToString());
                }
                Console.WriteLine();

                //Select everything that is exactly a Car
                Console.WriteLine("# Selecting things whose class is exactly Car");
                ExactClassSelector carsel = new ExactClassSelector(g, g.CreateUriNode("eg:Car"));
                Test(g, carsel);
                Console.WriteLine();

                //Select everything that is any sort of Car
                Console.WriteLine("# Selecting things whose class is Car or a subclass thereof");
                ClassSelector allcarsel = new ClassSelector(g, g.CreateUriNode("eg:Car"));
                Test(g, allcarsel);
                Console.WriteLine();

                //Select everything that is a SportsCar or superclass thereof
                Console.WriteLine("# Selecting things whose class is SportsCar or superclass thereof");
                Console.WriteLine("# Thus things which are Cars are selected but not PeopleCarriers since they are a sibling class and not a superclass");
                WideningClassSelector wcarsel = new WideningClassSelector(g, g.CreateUriNode("eg:SportsCar"));
                Test(g, wcarsel);
                Console.WriteLine();

                //Select everything that is exactly a Jumbo Jet
                Console.WriteLine("# Selecting things whose class is exactly JumboJet");
                ExactClassSelector jjsel = new ExactClassSelector(g, g.CreateUriNode("eg:JumboJet"));
                Test(g, jjsel);
                Console.WriteLine();

                //Select everything that is an Air Vehicle
                Console.WriteLine("# Selecting things which are Air Vehicles");
                ClassSelector avsel = new ClassSelector(g, g.CreateUriNode("eg:AirVehicle"));
                Test(g, avsel);
                Console.WriteLine();

                //Select everything that has a defined Speed
                Console.WriteLine("# Selecting things which have exactly Speed defined");
                HasExactPropertySelector spsel = new HasExactPropertySelector(g.CreateUriNode("eg:Speed"));
                Test(g, spsel);
                Console.WriteLine();

                //Select things with a limited Speed
                Console.WriteLine("# Selecting things which have a Limited Speed defined");
                HasExactPropertySelector lspsel = new HasExactPropertySelector(g.CreateUriNode("eg:LimitedSpeed"));
                Test(g, lspsel);
                Console.WriteLine();

                //Select things with any kinds of Speed
                Console.WriteLine("# Selecting things which have any kinds of Speed defined");
                HasPropertySelector allspsel = new HasPropertySelector(g, g.CreateUriNode("eg:Speed"));
                Test(g, allspsel);
                Console.WriteLine();

                //Now stick the Graph in a Triple Store with a Reasoner attached
                Console.WriteLine("# Using a Triple Store with an RDFS Reasoner attached");
                TripleStore store = new TripleStore();
                store.AddInferenceEngine(new RdfsReasoner());
                g.BaseUri = new Uri("http://example.org/Inference");
                //Add a couple of additional Triples, their types must be inferred
                g.Assert(new Triple(g.CreateUriNode("eg:AudiA8"), g.CreateUriNode("eg:Speed"), (180).ToLiteral(g)));
                g.Assert(new Triple(g.CreateUriNode("eg:SpaceShuttle"), g.CreateUriNode("eg:AirSpeed"), (17500).ToLiteral(g)));
                //Add the Graph to the store, this is when inference occurs
                store.Add(g);
                Console.WriteLine("Triple Store contains the following Triples");
                foreach (Triple t in store.Triples)
                {
                    Console.WriteLine(t.ToString());
                }
                Console.WriteLine();

                //Select things which are a Car
                Console.WriteLine("# Selecting things which are a Car (or subclass thereof)");
                HasPropertyValueSelector carsel2 = new HasPropertyValueSelector(g.CreateUriNode("rdf:type"), g.CreateUriNode("eg:Car"));
                Test(store, carsel2);
                Console.WriteLine();

                //Select things which are Air Vehicles
                Console.WriteLine("# Selecting things which are Air Vehicles");
                HasPropertyValueSelector avsel2 = new HasPropertyValueSelector(g.CreateUriNode("rdf:type"), g.CreateUriNode("eg:AirVehicle"));
                Test(store, avsel2);
                Console.WriteLine();

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
            }

            output.Close();
        }
Ejemplo n.º 35
0
        public static Object LoadFromReader(Reader r, string baseUri, org.openrdf.rio.RDFFormat rdff)
        {
            Object obj;

            if (rdff == dotSesameFormats.RDFFormat.N3)
            {
                obj = new Graph();
                if (baseUri != null) ((IGraph)obj).BaseUri = new Uri(baseUri);
                Notation3Parser parser = new Notation3Parser();
                parser.Load((IGraph)obj, r.ToDotNetReadableStream());
            }
            else if (rdff == dotSesameFormats.RDFFormat.NTRIPLES)
            {
                obj = new Graph();
                if (baseUri != null) ((IGraph)obj).BaseUri = new Uri(baseUri);
                NTriplesParser parser = new NTriplesParser();
                parser.Load((IGraph)obj, r.ToDotNetReadableStream());
            }
            else if (rdff == dotSesameFormats.RDFFormat.RDFXML)
            {
                obj = new Graph();
                if (baseUri != null) ((IGraph)obj).BaseUri = new Uri(baseUri);
                RdfXmlParser parser = new RdfXmlParser();
                parser.Load((IGraph)obj, r.ToDotNetReadableStream());
            }
            else if (rdff == dotSesameFormats.RDFFormat.TRIG)
            {
                obj = new TripleStore();
                TriGParser trig = new TriGParser();
                trig.Load((ITripleStore)obj, new StreamParams(r.ToDotNetReadableStream().BaseStream));
            }
            else if (rdff == dotSesameFormats.RDFFormat.TRIX)
            {
                obj = new TripleStore();
                TriXParser trix = new TriXParser();
                trix.Load((ITripleStore)obj, new StreamParams(r.ToDotNetReadableStream().BaseStream));
            }
            else if (rdff == dotSesameFormats.RDFFormat.TURTLE)
            {
                obj = new Graph();
                if (baseUri != null) ((IGraph)obj).BaseUri = new Uri(baseUri);
                TurtleParser parser = new TurtleParser();
                parser.Load((IGraph)obj, r.ToDotNetReadableStream());
            }
            else
            {
                throw new RdfParserSelectionException("The given Input Format is not supported by dotNetRDF");
            }

            return obj;
        }
Ejemplo n.º 36
0
        public static void Main(String[] args)
        {
            StreamWriter output = new StreamWriter("TalisTests.txt");
            try
            {
                Console.SetOut(output);
                Console.WriteLine("## Talis Platform Tests");
                Console.WriteLine();

                //Read in our Test Graph
                Graph g = new Graph();
                TurtleParser ttlparser = new TurtleParser();
                ttlparser.Load(g, "TalisTest.ttl");
                Console.WriteLine("Loaded Test Graph OK");
                Console.WriteLine();

                //Get the Talis Store
                TalisPlatformConnector talis = new TalisPlatformConnector("rvesse-dev1", "rvesse", "4kn478wj");

                //Attempt to add a Graph
                Console.WriteLine("# Attempting to add this Graph to the Talis Store");
                talis.Add(g);
                Console.WriteLine("Added the Graph OK");
                Console.WriteLine();

                //Attempt to get this some data from this Graph back again
                Console.WriteLine("# Attempting to retrieve some data from this Graph from the Talis Store");

                Graph h = new Graph();
                talis.Describe(h, TestUri);

                Console.WriteLine("Retrieved OK");
                Console.WriteLine();

                foreach (Triple t in h.Triples)
                {
                    Console.WriteLine(t.ToString());
                }
                Console.WriteLine();

                //Make a Sparql Query for the same stuff
                Console.WriteLine("# Attempting to retrieve the same data from this Graph from the Talis Store using SPARQL");

                Object results = talis.Query("SELECT * {?s ?p ?o . FILTER(?s = <" + TestUri + ">)}");
                Console.WriteLine("Retrieved OK");
                Console.WriteLine();

                //Output the Result
                if (results is SparqlResultSet)
                {
                    foreach (SparqlResult result in (SparqlResultSet)results)
                    {
                        Console.WriteLine(result.ToString());
                    }
                }
                else if (results is Graph)
                {
                    foreach (Triple t in ((Graph)results).Triples)
                    {
                        Console.WriteLine(t.ToString());
                    }
                }
                else
                {
                    Console.WriteLine("Unexpected Results Object '" + results.GetType().ToString() + "'");
                }
                Console.WriteLine();

                //Use a TalisGraph object to do the same thing
                Console.WriteLine("# Attempting to retrieve the same data again using a TalisGraph object");
                TalisGraph i = new TalisGraph(TestUri, talis);
                Console.WriteLine("Retrieved OK");
                Console.WriteLine();

                foreach (Triple t in i.Triples)
                {
                    Console.WriteLine(t.ToString());
                }
                Console.WriteLine();

                //Use the same object to Update the Store
                Console.WriteLine("# Attempting to Update the Talis Store using the TalisGraph object");
                if (!i.NamespaceMap.HasNamespace("eg")) {
                    i.NamespaceMap.AddNamespace("eg", new Uri("http://example.org/vehicles/"));
                }
                IUriNode spaceVehicle = i.CreateUriNode("eg:SpaceVehicle");
                IUriNode subClass = i.CreateUriNode("rdfs:subClassOf");
                IUriNode vehicle = i.CreateUriNode("eg:Vehicle");
                i.Assert(new Triple(spaceVehicle, subClass, vehicle));
                Console.WriteLine("Updated OK");
                Console.WriteLine();

                //Retrieve that Graph again
                Console.WriteLine("# Retrieving the same data again to check that the Update was persisted OK");
                TalisGraph j = new TalisGraph(new Uri("http://example.org/vehicles/SpaceVehicle"), talis);
                Console.WriteLine("Retrieved OK");
                Console.WriteLine();

                foreach (Triple t in j.Triples)
                {
                    Console.WriteLine(t.ToString());
                }
                Console.WriteLine();
            }
            catch (Exception ex)
            {
                HandleError(ex);
            }
            finally
            {
                output.Close();
            }
        }
Ejemplo n.º 37
0
        public void StorageVirtuosoDeleteGraph()
        {
            try
            {
                VirtuosoManager manager = new VirtuosoManager("DB", VirtuosoTestUsername, VirtuosoTestPassword);
                Assert.IsNotNull(manager);

                Console.WriteLine("Got the Virtuoso Manager OK");

                //Load in our Test Graph
                TurtleParser ttlparser = new TurtleParser();
                Graph g = new Graph();
                ttlparser.Load(g, "Turtle.ttl");
                g.BaseUri = new Uri("http://example.org/deleteMe");

                Console.WriteLine();
                Console.WriteLine("Loaded Test Graph OK");
                Console.WriteLine("Test Graph contains:");

                Assert.IsFalse(g.IsEmpty, "Test Graph should be non-empty");

                foreach (Triple t in g.Triples)
                {
                    Console.WriteLine(t.ToString());
                }
                Console.WriteLine();

                //Try to save to Virtuoso
                manager.SaveGraph(g);
                Console.WriteLine("Saved OK");
                Console.WriteLine();

                //Try to retrieve
                Graph h = new Graph();
                manager.LoadGraph(h, "http://example.org/deleteMe");

                Assert.IsFalse(h.IsEmpty, "Retrieved Graph should be non-empty");

                Console.WriteLine("Retrieved the Graph from Virtuoso OK");
                Console.WriteLine("Retrieved Graph contains:");
                foreach (Triple t in h.Triples)
                {
                    Console.WriteLine(t.ToString());
                }

                //Try to delete
                manager.DeleteGraph("http://example.org/deleteMe");
                Graph i = new Graph();
                manager.LoadGraph(i, "http://example.org/deleteMe");

                Assert.IsTrue(i.IsEmpty, "Retrieved Graph should be empty as it should have been deleted from the Store");
            }
            catch (VirtuosoException virtEx)
            {
                TestTools.ReportError("Virtuoso Error", virtEx, true);
            }
            catch (Exception ex)
            {
                TestTools.ReportError("Other Error", ex, true);
            }
        }
Ejemplo n.º 38
0
        public void GraphMerging()
        {
            try
            {
                //Load the Test RDF
                TurtleParser ttlparser = new TurtleParser();
                Graph g = new Graph();
                Graph h = new Graph();
                Assert.IsNotNull(g);
                Assert.IsNotNull(h);
                ttlparser.Load(g, "MergePart1.ttl");
                ttlparser.Load(h, "MergePart2.ttl");

                Console.WriteLine("Merge Test Data Loaded OK");
                Console.WriteLine();

                Console.WriteLine("Graph 1 Contains");
                foreach (Triple t in g.Triples)
                {
                    Console.WriteLine(t.ToString());
                }

                Console.WriteLine();
                Console.WriteLine("Graph 2 Contains");
                foreach (Triple t in h.Triples)
                {
                    Console.WriteLine(t.ToString());
                }

                Console.WriteLine();

                Console.WriteLine("Attempting Graph Merge");
                g.Merge(h);
                Console.WriteLine();

                foreach (Triple t in g.Triples)
                {
                    Console.WriteLine(t.ToString());
                }

                Assert.AreEqual(8, g.Triples.Count, "Expected 8 Triples after the Merge");

                //Use a GraphViz Generator to picture this
                Console.WriteLine();
                Console.WriteLine("Visualizing Merged Graph as SVG with GraphViz");
                GraphVizGenerator gvzgen = new GraphVizGenerator("svg");
                gvzgen.Generate(g, "MergeTest.svg", false);
                Console.WriteLine("Visualisation created as MergeTest.svg");

                //Same merge into an Empty Graph
                Console.WriteLine();
                Console.WriteLine("Combining the two Graphs with two Merge operations into an Empty Graph");
                Graph i = new Graph();

                //Need to reload g from disk
                g = new Graph();
                ttlparser.Load(g, "MergePart1.ttl");

                //Do the actual merge
                i.Merge(g);
                i.Merge(h);
                Console.WriteLine();

                foreach (Triple t in i.Triples)
                {
                    Console.WriteLine(t.ToString());
                }

                Assert.AreEqual(8, i.Triples.Count, "Expected 8 Triples after the Merge");



            }
            catch (Exception ex)
            {
                TestTools.ReportError("Other Exception", ex, true);
            }
        }
Ejemplo n.º 39
0
        public static void Main(String[] args)
        {
            StreamWriter output = new StreamWriter("VirtuosoTest.txt");
            Console.SetOut(output);
            try
            {
                Console.WriteLine("##Virtuoso Test Suite");
                Console.WriteLine();

                //Do some basic operations
                Console.WriteLine("# Basic Read and Write of normal Graphs");

                //Read in a Test Graph from a Turtle File
                Graph g = new Graph();
                g.BaseURI = new Uri("http://www.dotnetrdf.org/Tests/SQLStore/");
                TurtleParser ttlparser = new TurtleParser();
                ttlparser.Load(g, "InferenceTest.ttl");

                Console.WriteLine("Loaded the InferenceTest.ttl file as the Test Graph");
                Console.WriteLine("Attempting to save into the SQL Store");

                //Get the Non Native Virtuoso Manager
                NonNativeVirtuosoManager manager = new NonNativeVirtuosoManager("localhost", 1111, "dotnetrdf_experimental", "dba", "20sQl09");

                //Save to a Store using SqlWriter
                SqlWriter sqlwriter = new SqlWriter(manager);
                sqlwriter.Save(g, false);

                Console.WriteLine("Saved to the SQL Store");

                //Read back from the Store using SqlReader
                Graph h = new Graph();
                Console.WriteLine("Trying to read the Graph back from the SQL Store");
                SqlReader sqlreader = new SqlReader(manager);
                h = sqlreader.Load("http://www.dotnetrdf.org/Tests/SQLStore/");

                Console.WriteLine("Read from SQL Store OK");

                foreach (String prefix in h.NamespaceMap.Prefixes)
                {
                    Console.WriteLine(prefix + ": <" + h.NamespaceMap.GetNamespaceURI(prefix).ToString() + ">");
                }
                Console.WriteLine();
                foreach (Triple t in h.Triples)
                {
                    Console.WriteLine(t.ToString());
                }

                Console.WriteLine("# Test Passed");
                Console.WriteLine();

                //Demonstrate that the SqlGraph persists stuff to the Store
                Console.WriteLine("# Advanced Read and Write with a SQLGraph");

                SqlGraph s = new SqlGraph(new Uri("http://www.dotnetrdf.org/Tests/SQLStore"), manager);
                Console.WriteLine("Opened the SQL Graph OK");

                INode type = s.CreateURINode("rdf:type");

                s.Assert(new Triple(type, type, type));

                Console.WriteLine("Asserted something");

                s.NamespaceMap.AddNamespace("ex", new Uri("http://www.example.org/"));
                Console.WriteLine("Added a Namespace");

                s.NamespaceMap.AddNamespace("ex", new Uri("http://www.example.org/changedNamespace/"));
                Console.WriteLine("Changed a Namespace");

                Console.WriteLine("Reopening to see if stuff gets loaded correctly");
                s = new SqlGraph(new Uri("http://www.dotnetrdf.org/Tests/SQLStore"), "dotnetrdf_experimental", "sa", "20sQl08");

                foreach (String prefix in s.NamespaceMap.Prefixes)
                {
                    Console.WriteLine(prefix + ": <" + s.NamespaceMap.GetNamespaceURI(prefix).ToString() + ">");
                }
                Console.WriteLine();
                foreach (Triple t in s.Triples)
                {
                    Console.WriteLine(t.ToString());
                }
                Console.WriteLine();

                s.Retract(new Triple(type, type, type));
                Console.WriteLine("Retracted something");

                foreach (Triple t in s.Triples)
                {
                    Console.WriteLine(t.ToString());
                }
            }
            catch (OpenLink.Data.Virtuoso.VirtuosoException virtEx)
            {
                reportError(output, "Virtuoso Exception", virtEx);
            }
            catch (System.Data.SqlClient.SqlException sqlEx)
            {
                reportError(output, "SQL Exception", sqlEx);
            }
            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
            {
                output.Close();
            }
        }
Ejemplo n.º 40
0
        public static void Main(String[] args)
        {
            StreamWriter output = new StreamWriter("4StoreTest.txt");
            try
            {
                Console.SetOut(output);
                Console.WriteLine("## 4store Test");
                Console.WriteLine();

                //Load the Graph we want to use as a Test
                Graph g = new Graph();
                TurtleParser ttlparser = new TurtleParser();
                ttlparser.Load(g, "InferenceTest.ttl");
                Console.WriteLine("Test Graph contains the following Triples:");
                foreach (Triple t in g.Triples)
                {
                    Console.WriteLine(t.ToString());
                }
                Console.WriteLine();

                //Create the 4store Connector
                FourStoreConnector fourstore = new FourStoreConnector("http://nottm-virtual.ecs.soton.ac.uk:8080/");

                //Try to save a Graph to the Store
                Console.WriteLine("Attempting to save a Graph to the Store");
                g.BaseUri = new Uri("http://example.org/test");
                fourstore.SaveGraph(g);
                Console.WriteLine("Graph saved OK");
                Console.WriteLine();

                //Try to retrieve the Graph from the Store
                Console.WriteLine("Attempting to load the Graph back from the Store");
                Graph h = new Graph();
                fourstore.LoadGraph(h, g.BaseUri);
                Console.WriteLine("Graph loaded OK");
                Console.WriteLine("Contains the following Triples:");
                foreach (Triple t in h.Triples)
                {
                    Console.WriteLine(t.ToString());
                }
                Console.WriteLine();

                //Make a couple of queries against the store
                Console.WriteLine("Attempting to make an ASK query against the Store");
                Object results;
                results = fourstore.Query("ASK WHERE {<http://example.org/vehicles/FordFiesta> ?p ?o}");
                ShowResults(results);

                Console.WriteLine();
                Console.WriteLine("Attempting to make a SELECT query against the Store");
                results = fourstore.Query("SELECT * WHERE {<http://example.org/vehicles/FordFiesta> ?p ?o}");
                ShowResults(results);

                Console.WriteLine();
                Console.WriteLine("Attempting to make a DESCRIBE query against the Store");
                results = fourstore.Query("DESCRIBE <http://example.org/vehicles/FordFiesta>");
                ShowResults(results);

                Console.WriteLine();
                Console.WriteLine("Attempting to make a CONSTRUCT query against the Store");
                results = fourstore.Query("CONSTRUCT {<http://example.org/vehicles/FordFiesta> ?p ?o} WHERE {<http://example.org/vehicles/FordFiesta> ?p ?o}");
                ShowResults(results);

                //Now delete the data we added
                Console.WriteLine();
                Console.WriteLine("Attempting to delete the Graph from the Store");
                fourstore.DeleteGraph(g.BaseUri);
                Console.WriteLine("Graph deleted Ok");

            }
            catch (IOException ioEx)
            {
                reportError(output, "IO Exception", ioEx);
            }
            catch (RdfParseException parseEx)
            {
                reportError(output, "Parsing Exception", parseEx);
            }
            catch (RdfStorageException storeEx)
            {
                reportError(output, "Storage Exception", storeEx);
            }
            catch (RdfException rdfEx)
            {
                reportError(output, "RDF Exception", rdfEx);
            }
            catch (WebException webEx)
            {
                reportError(output, "HTTP Exception", webEx);
            }
            catch (Exception ex)
            {
                reportError(output, "Other Exception", ex);
            }
            finally
            {
                output.Close();
            }
        }
Ejemplo n.º 41
0
        static void Main(string[] args)
        {
            //Going to create a Graph and assert some stuff into it
            Graph g = new Graph();

            //Try to read from a file
            TurtleParser parser = new TurtleParser();
            parser.TraceTokeniser = true;
            parser.TraceParsing = true;
            try
            {
                StreamReader input = new StreamReader("test.n3");
                parser.Load(g, input);
            }
            catch (RDFException rdfEx)
            {
                reportError("RDF Exception", rdfEx);
            }
            catch (IOException ioEx)
            {
                reportError("IO Exception", ioEx);
            }
            catch (Exception ex)
            {
               reportError("Other Exception", ex);
            }

            Console.WriteLine();
            Console.WriteLine();

            //Show Namespaces
            Console.WriteLine("All Namespaces");
            foreach (String pre in g.NamespaceMap.Prefixes)
            {
                Console.WriteLine(pre + " = " + g.NamespaceMap.GetNamespaceURI(pre));
            }

            Console.WriteLine();

            //Now print all the Statements
            Console.WriteLine("All Statements");
            foreach (Triple t in g.Triples)
            {
                Console.WriteLine(t.ToString());
            }


            System.Threading.Thread.Sleep(60000);
            return;



            g.NamespaceMap.AddNamespace("vds", new Uri("http://www.vdesign-studios.com/dotNetRDF#"));
            g.NamespaceMap.AddNamespace("ecs", new Uri("http://id.ecs.soton.ac.uk/person/"));
            //g.BaseURI = g.NamespaceMap.GetNamespaceURI("vds");

            URINode rav08r, wh, lac, hcd;
            rav08r = g.CreateURINode("ecs:11471");
            wh = g.CreateURINode("ecs:1650");
            hcd = g.CreateURINode("ecs:46");
            lac = g.CreateURINode("ecs:60");

            BlankNode blank = g.CreateBlankNode();
            URINode a, b, c, d, has;
            a = g.CreateURINode("vds:someRel");
            b = g.CreateURINode("vds:someOtherRel");
            c = g.CreateURINode("vds:someObj");
            d = g.CreateURINode("vds:someOtherObj");
            has = g.CreateURINode("vds:has");

            URINode supervises, collaborates, advises;
            supervises = g.CreateURINode("vds:supervises");
            collaborates = g.CreateURINode("vds:collaborates");
            advises = g.CreateURINode("vds:advises");

            LiteralNode singleLine = g.CreateLiteralNode("Some string");
            LiteralNode multiLine = g.CreateLiteralNode("This goes over\n\nseveral\n\nlines");
            LiteralNode french = g.CreateLiteralNode("Bonjour", "fr");

            g.Assert(new Triple(wh, supervises, rav08r));
            g.Assert(new Triple(lac, supervises, rav08r));
            g.Assert(new Triple(hcd, advises, rav08r));
            g.Assert(new Triple(wh, collaborates, lac));
            g.Assert(new Triple(wh, collaborates, hcd));
            g.Assert(new Triple(lac, collaborates, hcd));
            //g.Assert(new Triple(rav08r, blank, c));
            //g.Assert(new Triple(rav08r, blank, d));
            g.Assert(new Triple(rav08r, has, singleLine));
            g.Assert(new Triple(rav08r, has, multiLine));
            g.Assert(new Triple(rav08r, has, french));


            //Now print all the Statements
            Console.WriteLine("All Statements");
            foreach (Triple t in g.Triples)
            {
                Console.WriteLine(t.ToString());
            }

            //Get statements about Rob Vesse
            Console.WriteLine();
            Console.WriteLine("Statements about Rob Vesse");
            foreach (Triple t in g.GetTriples(rav08r))
            {
                Console.WriteLine(t.ToString());
            }

            //Get Statements about Collaboration
            Console.WriteLine();
            Console.WriteLine("Statements about Collaboration");
            foreach (Triple t in g.GetTriples(collaborates))
            {
                Console.WriteLine(t.ToString());
            }

            //Show Namespaces for URINodes
            Console.WriteLine();
            Console.WriteLine("Namespaces for URI Nodes");
            foreach (URINode u in g.Nodes.URINodes)
            {
                Console.WriteLine(u.Namespace + " = " + u.URI);
            }

            //Attempt to output Notation 3 for this Graph
            try
            {
                TurtleWriter n3writer = new TurtleWriter();
                n3writer.Save(g, "test.n3");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
            }

            

            System.Threading.Thread.Sleep(30000);
        }
Ejemplo n.º 42
0
        public static void Main(string[] args)
        {
            StreamWriter output = new StreamWriter("SQLStoreTest.txt");
            Console.SetOut(output);
            Console.WriteLine("## SQL Store Test");

            //Set default parameters if insufficient supplied
            if (args.Length < 5)
            {
                args = new string[] { "read", "10", "false", "false", "8" };
            }

            try
            {
                //Read in the Parameters
                String testMode = args[0].ToLower();
                if (testMode != "read" && testMode != "write")
                {
                    testMode = "read";
                }
                int runs = Int32.Parse(args[1]);
                if (runs < 1) runs = 1;
                bool reuseManager = Boolean.Parse(args[2]);
                bool useThreadedManager = Boolean.Parse(args[3]);
                int threads = Int32.Parse(args[4]);
                if (threads < 1) threads = 4;

                #region Basic Tests

                //Do some basic operations
                Console.WriteLine("# Basic Read and Write of normal Graphs");

                //Read in a Test Graph from a Turtle File
                Graph g = new Graph();
                g.BaseUri = new Uri("http://www.dotnetrdf.org/Tests/SQLStore/");
                TurtleParser ttlparser = new TurtleParser();
                ttlparser.Load(g, "InferenceTest.ttl");

                Console.WriteLine("Loaded the InferenceTest.ttl file as the Test Graph");
                Console.WriteLine("Attempting to save into the SQL Store");

                //Save to a Store using SqlWriter
                SqlWriter sqlwriter = new SqlWriter("dotnetrdf_experimental", "sa", "20sQl08");
                sqlwriter.Save(g, false);

                Console.WriteLine("Saved to the SQL Store");

                //Read back from the Store using SqlReader
                IGraph h = new Graph();
                Console.WriteLine("Trying to read the Graph back from the SQL Store");
                SqlReader sqlreader = new SqlReader("dotnetrdf_experimental", "sa", "20sQl08");
                h = sqlreader.Load("http://www.dotnetrdf.org/Tests/SQLStore/");

                Console.WriteLine("Read from SQL Store OK");

                foreach (String prefix in h.NamespaceMap.Prefixes)
                {
                    Console.WriteLine(prefix + ": <" + h.NamespaceMap.GetNamespaceUri(prefix).ToString() + ">");
                }
                Console.WriteLine();
                foreach (Triple t in h.Triples)
                {
                    Console.WriteLine(t.ToString());
                }

                Console.WriteLine("# Test Passed");
                Console.WriteLine();

                //Demonstrate that the SqlGraph persists stuff to the Store
                Console.WriteLine("# Advanced Read and Write with a SQLGraph");

                SqlGraph s = new SqlGraph(new Uri("http://www.dotnetrdf.org/Tests/SQLStore"), "dotnetrdf_experimental", "sa", "20sQl08");
                Console.WriteLine("Opened the SQL Graph OK");

                INode type = s.CreateUriNode("rdf:type");

                s.Assert(new Triple(type, type, type));

                Console.WriteLine("Asserted something");

                s.NamespaceMap.AddNamespace("ex", new Uri("http://www.example.org/"));
                Console.WriteLine("Added a Namespace");

                s.NamespaceMap.AddNamespace("ex", new Uri("http://www.example.org/changedNamespace/"));
                Console.WriteLine("Changed a Namespace");

                Console.WriteLine("Reopening to see if stuff gets loaded correctly");
                s = new SqlGraph(new Uri("http://www.dotnetrdf.org/Tests/SQLStore"), "dotnetrdf_experimental", "sa", "20sQl08");

                foreach (String prefix in s.NamespaceMap.Prefixes)
                {
                    Console.WriteLine(prefix + ": <" + s.NamespaceMap.GetNamespaceUri(prefix).ToString() + ">");
                }
                Console.WriteLine();
                foreach (Triple t in s.Triples)
                {
                    Console.WriteLine(t.ToString());
                }
                Console.WriteLine();

                s.Retract(new Triple(type, type, type));
                Console.WriteLine("Retracted something");

                foreach (Triple t in s.Triples)
                {
                    Console.WriteLine(t.ToString());
                }

                #endregion

                #region Benchmarking Tests

                Console.WriteLine();
                Console.WriteLine("# Performance Benchmarking for a Large TripleStore");
                Console.WriteLine("Performing " + runs + " Runs to gauge average performance");

                int totalTriples = 0;
                long totalTime = 0;
                long diff;
                DateTime start, finish;
                int triples;

                if (testMode.Equals("read"))
                {
                    //Read Benchmark
                    Console.WriteLine("Read Benchmarking");

                    if (reuseManager)
                    {
                        Console.WriteLine("Reusing ISQLIOManager which should improve performance");
                    }
                    if (useThreadedManager)
                    {
                        Console.WriteLine("Using IThreadedSQLIOManager which should improve perfomance");
                    }

                    //Create the Manager
                    IThreadedSqlIOManager manager;
                    manager = new MicrosoftSqlStoreManager("localhost", "bbcone", "sa", "20sQl08");
                    if (reuseManager)
                    {
                        manager.PreserveState = true;
                    }

                    //Perform the Runs
                    for (int i = 1; i <= runs; i++)
                    {
                        Console.WriteLine("Run #" + i);
                        Debug.WriteLine("Run #" + i);

                        //Start Profiling
                        start = DateTime.Now;
                        Console.WriteLine("Starting Loading @ " + start.ToString(TestSuite.TestSuiteTimeFormat));
                        Debug.WriteLine("Start @ " + start.ToString(TestSuite.TestSuiteTimeFormat));

                        //Set-up the Manager as required by the Test Options
                        ITripleStore bigstore;
                        if (reuseManager)
                        {
                            if (useThreadedManager)
                            {
                                bigstore = new ThreadedSqlTripleStore((IThreadedSqlIOManager)manager, threads);
                            }
                            else
                            {
                                bigstore = new SqlTripleStore(manager);
                            }
                        }
                        else
                        {
                            if (useThreadedManager)
                            {
                                bigstore = new ThreadedSqlTripleStore(manager, threads);
                            }
                            else
                            {
                                bigstore = new SqlTripleStore(manager);
                            }
                        }

                        //End Profiling
                        finish = DateTime.Now;
                        Console.WriteLine("Finished Loading @ " + finish.ToString(TestSuite.TestSuiteTimeFormat));
                        Debug.WriteLine("Finish @ " + finish.ToString(TestSuite.TestSuiteTimeFormat));

                        //Increment Totals for final average calculations later
                        triples = bigstore.Triples.Count();
                        totalTriples += triples;
                        Console.WriteLine(triples + " Triples loaded");

                        //Compute Load Rate
                        diff = Microsoft.VisualBasic.DateAndTime.DateDiff(Microsoft.VisualBasic.DateInterval.Second, start, finish, Microsoft.VisualBasic.FirstDayOfWeek.Monday, Microsoft.VisualBasic.FirstWeekOfYear.System);
                        totalTime += diff;
                        Console.WriteLine("Load took " + diff + " seconds");
                        Console.WriteLine("Load Rate was " + triples / diff + " Triples/Second");
                        Debug.WriteLine("Load Rate was " + triples / diff + " Triples/Second");
                        Console.WriteLine();

                        bigstore.Dispose();
                    }
                }
                else
                {
                    //Write Benchmark
                    Console.WriteLine("Write Benchmarking");

                    //Load in the Source Data from our Test Store
                    ITripleStore origstore;
                    IThreadedSqlIOManager readManager = new MicrosoftSqlStoreManager("localhost", "dotnetrdf_experimental", "sa", "20sQl08");
                    Console.WriteLine();
                    Console.WriteLine("Obtaining Test Data");

                    start = DateTime.Now;
                    Console.WriteLine("Starting Loading @ " + start.ToString(TestSuite.TestSuiteTimeFormat));
                    Debug.WriteLine("Start @ " + start.ToString(TestSuite.TestSuiteTimeFormat));

                    //Do the Load
                    origstore = new ThreadedSqlTripleStore(readManager,8);
                    finish = DateTime.Now;
                    triples = origstore.Triples.Count();

                    Console.WriteLine("Finished Loading @ " + finish.ToString(TestSuite.TestSuiteTimeFormat));
                    Debug.WriteLine("Finish @ " + finish.ToString(TestSuite.TestSuiteTimeFormat));

                    //Compute Load Rate
                    diff = Microsoft.VisualBasic.DateAndTime.DateDiff(Microsoft.VisualBasic.DateInterval.Second, start, finish, Microsoft.VisualBasic.FirstDayOfWeek.Monday, Microsoft.VisualBasic.FirstWeekOfYear.System);
                    Console.WriteLine("Load took " + diff + " seconds");
                    if (diff > 0)
                    {
                        Console.WriteLine("Load Rate was " + triples / diff + " Triples/Second");
                        Debug.WriteLine("Load Rate was " + triples / diff + " Triples/Second");
                    }
                    Console.WriteLine();

                    IThreadedSqlIOManager writeManager;

                    for (int i = 1; i <= runs; i++) 
                    {
                        Console.WriteLine("Run #" + i);
                        Debug.WriteLine("Run #" + i);

                        //Create a New Manager for every write
                        writeManager = new MicrosoftSqlStoreManager("localhost", "write_test", "sa", "20sQl08");

                        //Create SqlWriter
                        ThreadedSqlStoreWriter writer = new ThreadedSqlStoreWriter();

                        //Start Profiling
                        start = DateTime.Now;
                        Console.WriteLine("Starting Loading @ " + start.ToString(TestSuite.TestSuiteTimeFormat));
                        Debug.WriteLine("Start @ " + start.ToString(TestSuite.TestSuiteTimeFormat));

                        //Write the Store
                        writer.Save(origstore, new ThreadedSqlIOParams(writeManager,threads));
                        
                        //End Profiling
                        finish = DateTime.Now;
                        Console.WriteLine("Finished Loading @ " + finish.ToString(TestSuite.TestSuiteTimeFormat));
                        Debug.WriteLine("Finish @ " + finish.ToString(TestSuite.TestSuiteTimeFormat));

                        //Compute Load Rate
                        diff = Microsoft.VisualBasic.DateAndTime.DateDiff(Microsoft.VisualBasic.DateInterval.Second, start, finish, Microsoft.VisualBasic.FirstDayOfWeek.Monday, Microsoft.VisualBasic.FirstWeekOfYear.System);
                        totalTime += diff;
                        Console.WriteLine("Writing took " + diff + " seconds");
                        if (diff > 0)
                        {
                            Console.WriteLine("Write Rate was " + triples / diff + " Triples/Second");
                            Debug.WriteLine("Write Rate was " + triples / diff + " Triples/Second");
                        }
                        Console.WriteLine();

                        //Now need to clear the Database for the next Test
                        writeManager.Open(true);
                        writeManager.ExecuteNonQuery("DELETE FROM NODES");
                        writeManager.ExecuteNonQuery("DELETE FROM TRIPLES");
                        writeManager.ExecuteNonQuery("DELETE FROM GRAPH_TRIPLES");
                        writeManager.ExecuteNonQuery("DELETE FROM GRAPHS");
                        writeManager.ExecuteNonQuery("DELETE FROM NAMESPACES");
                        writeManager.ExecuteNonQuery("DELETE FROM NS_PREFIXES");
                        writeManager.ExecuteNonQuery("DELETE FROM NS_URIS");
                        writeManager.Close(true);
                    }
                }

                //Final Average Calculations
                Console.WriteLine();
                Console.WriteLine("Average Load Time was " + totalTime / runs + " seconds");
                Console.WriteLine("Average Load Rate was " + totalTriples / totalTime + " Triples/Second");

                #endregion

                Console.WriteLine("# Tests Passed");
                

            }
            catch (System.Data.SqlClient.SqlException sqlEx)
            {
                reportError(output, "SQL Exception", sqlEx);
            }
            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);
            }

            output.Close();
        }