private void LoadInternal() { //Load Graph SqlReader reader = new SqlReader(this._manager); Stopwatch timer = new Stopwatch(); timer.Start(); this._g = reader.Load(this._graphUri); timer.Stop(); //Show Load Statistics this.lblLoadInfo.Text = this._g.Triples.Count + " Triples Loaded in "; if (timer.ElapsedMilliseconds > 1000) { this.lblLoadInfo.Text += (((double)timer.ElapsedMilliseconds) / 1000d).ToString("F2") + " Seconds"; } else { this.lblLoadInfo.Text += timer.ElapsedMilliseconds + " Milliseconds"; } this.lblLoadInfo.Text += " at a Speed of "; double speed = ((double)this._g.Triples.Count) / (((double)timer.ElapsedMilliseconds) / 1000d); this.lblLoadInfo.Text += speed.ToString("N0") + " Triples/Second"; //Show Graph Uri this.lnkGraphURI.Text = this._graphUri.ToString(); //Show Triples this.lvwTriples.BeginUpdate(); this.lvwTriples.Items.Clear(); ListViewItem item; String[] triple; foreach (Triple t in this._g.Triples) { triple = new String[] { t.Subject.ToString(), t.Predicate.ToString(), t.Object.ToString() }; item = new ListViewItem(triple); this.lvwTriples.Items.Add(item); } this.lvwTriples.EndUpdate(); }
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(); }
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(); } }
/// <summary> /// Tries to load a Graph based on information from the Configuration Graph /// </summary> /// <param name="g">Configuration Graph</param> /// <param name="objNode">Object Node</param> /// <param name="targetType">Target Type</param> /// <param name="obj">Output Object</param> /// <returns></returns> public bool TryLoadObject(IGraph g, INode objNode, Type targetType, out object obj) { obj = null; IGraph output; try { output = (IGraph)Activator.CreateInstance(targetType); } catch { //Any error means this loader can't load this type return false; } //Now we want to find out where the data for the Graph is coming from //Data Source loading order is Graphs, Files, Strings, Databases, Stores, URIs IEnumerable<INode> sources; //Load from Graphs sources = ConfigurationLoader.GetConfigurationData(g, objNode, ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyFromGraph)); foreach (INode source in sources) { ConfigurationLoader.CheckCircularReference(objNode, source, "dnr:fromGraph"); Object graph = ConfigurationLoader.LoadObject(g, source); if (graph is IGraph) { output.Merge((IGraph)graph); } else { throw new DotNetRdfConfigurationException("Unable to load data from another Graph for the Graph identified by the Node '" + objNode.ToString() + "' as one of the values for the dnr:fromGraph property points to an Object that cannot be loaded as an object which implements the IGraph interface"); } } //Load from Embedded Resources sources = ConfigurationLoader.GetConfigurationData(g, objNode, ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyFromEmbedded)); foreach (INode source in sources) { if (source.NodeType == NodeType.Literal) { EmbeddedResourceLoader.Load(output, ((ILiteralNode)source).Value); } else { throw new DotNetRdfConfigurationException("Unable to load data from an Embedded Resource for the Graph identified by the Node '" + objNode.ToString() + "' as one of the values for the dnr:fromEmbedded property is not a Literal Node as required"); } } //Load from Files sources = ConfigurationLoader.GetConfigurationData(g, objNode, ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyFromFile)); foreach (INode source in sources) { if (source.NodeType == NodeType.Literal) { FileLoader.Load(output, ConfigurationLoader.ResolvePath(((ILiteralNode)source).Value)); } else { throw new DotNetRdfConfigurationException("Unable to load data from a file for the Graph identified by the Node '" + objNode.ToString() + "' as one of the values for the dnr:fromFile property is not a Literal Node as required"); } } //Load from Strings sources = ConfigurationLoader.GetConfigurationData(g, objNode, ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyFromString)); foreach (INode source in sources) { if (source.NodeType == NodeType.Literal) { StringParser.Parse(output, ((ILiteralNode)source).Value); } else { throw new DotNetRdfConfigurationException("Unable to load data from a string for the Graph identified by the Node '" + objNode.ToString() + "' as one of the values for the dnr:fromString property is not a Literal Node as required"); } } IEnumerable<Object> connections; #if !NO_DATA && !NO_STORAGE //Load from Databases IEnumerable<INode> dbs = ConfigurationLoader.GetConfigurationData(g, objNode, ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyFromDatabase)); dbs.All(db => !ConfigurationLoader.CheckCircularReference(objNode, db, "dnr:fromDatabase")); connections = dbs.Select(db => ConfigurationLoader.LoadObject(g, db)); sources = ConfigurationLoader.GetConfigurationData(g, objNode, ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyWithUri)); foreach (Object db in connections) { if (db is ISqlIOManager) { SqlReader reader = new SqlReader((ISqlIOManager)db); foreach (INode source in sources) { if (source.NodeType == NodeType.Uri || source.NodeType == NodeType.Literal) { output.Merge(reader.Load(source.ToString())); } else { throw new DotNetRdfConfigurationException("Unable to load data from a database for the Graph identified by the Node '" + objNode.ToString() + "' as one of the values for the dnr:withUri property is not a URI/Literal Node as required"); } } } else { throw new DotNetRdfConfigurationException("Unable to load data from a database for the Graph identified by the Node '" + objNode.ToString() + "' as one of the values of the dnr:fromDatabase property points to an Object which cannot be loaded as an object which implements the ISqlIOManager interface"); } } #endif #if !NO_STORAGE //Load from Stores IEnumerable<INode> stores = ConfigurationLoader.GetConfigurationData(g, objNode, ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyFromStore)); stores.All(s => !ConfigurationLoader.CheckCircularReference(objNode, s, "dnr:fromStore")); connections = stores.Select(s => ConfigurationLoader.LoadObject(g, s)); sources = ConfigurationLoader.GetConfigurationData(g, objNode, ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyWithUri)); foreach (Object store in connections) { if (store is IGenericIOManager) { GenericReader reader = new GenericReader((IGenericIOManager)store); foreach (INode source in sources) { if (source.NodeType == NodeType.Uri || source.NodeType == NodeType.Literal) { reader.Load(output, source.ToString()); } else { throw new DotNetRdfConfigurationException("Unable to load data from a Generic Store for the Graph identified by the Node '" + objNode.ToString() + "' as one of the values for the dnr:withUri property is not a URI/Literal Node as required"); } } } else if (store is ITripleStore) { foreach (INode source in sources) { if (source.NodeType == NodeType.Uri) { output.Merge(((ITripleStore)store).Graph(((IUriNode)source).Uri)); } else if (source.NodeType == NodeType.Literal) { output.Merge(((ITripleStore)store).Graph(new Uri(((ILiteralNode)source).Value))); } else { throw new DotNetRdfConfigurationException("Unable to load data from a Store for the Graph identified by the Node '" + objNode.ToString() + "' as one of the values for the dnr:withUri property is not a URI/Literal Node as required"); } } } else { throw new DotNetRdfConfigurationException("Unable to load data from a Store for the Graph identified by the Node '" + objNode.ToString() + "' as one of the values of the dnr:fromStore property points to an Object which cannot be loaded as an object which implements either the IGenericIOManager/ITripleStore interface"); } } #endif //Finally load from Remote URIs sources = ConfigurationLoader.GetConfigurationData(g, objNode, ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyFromUri)); foreach (INode source in sources) { if (source.NodeType == NodeType.Uri) { UriLoader.Load(output, ((IUriNode)source).Uri); } else if (source.NodeType == NodeType.Literal) { UriLoader.Load(output, new Uri(((ILiteralNode)source).Value)); } else { throw new DotNetRdfConfigurationException("Unable to load data from a URI for the Graph identified by the Node '" + objNode.ToString() + "' as one of the values for the dnr:fromUri property is not a URI/Literal Node as required"); } } //Then are we assigning a Base URI to this Graph which overrides any existing Base URI? INode baseUri = ConfigurationLoader.GetConfigurationNode(g, objNode, ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyAssignUri)); if (baseUri != null) { if (baseUri.NodeType == NodeType.Uri) { output.BaseUri = ((IUriNode)baseUri).Uri; } else if (baseUri.NodeType == NodeType.Literal) { output.BaseUri = new Uri(((ILiteralNode)baseUri).Value); } else { throw new DotNetRdfConfigurationException("Unable to assign a new Base URI for the Graph identified by the Node '" + objNode.ToString() + "' as the value for the dnr:assignUri property is not a URI/Literal Node as required"); } } //Finally we'll apply any reasoners IEnumerable<INode> reasoners = ConfigurationLoader.GetConfigurationData(g, objNode, ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyReasoner)); foreach (INode reasoner in reasoners) { Object temp = ConfigurationLoader.LoadObject(g, reasoner); if (temp is IInferenceEngine) { ((IInferenceEngine)temp).Apply(output); } else { throw new DotNetRdfConfigurationException("Unable to apply a reasoner for the Graph identified by the Node '" + objNode.ToString() + "' as one of the values for the dnr:reasoner property points to an Object which cannot be loaded as an object which implements the IInferenceEngine interface"); } } obj = output; return true; }