/// <summary> /// Attempts to load an Object of the given type identified by the given Node and returned as the Type that this loader generates /// </summary> /// <param name="g">Configuration Graph</param> /// <param name="objNode">Object Node</param> /// <param name="targetType">Target Type</param> /// <param name="obj">Created Object</param> /// <returns>True if the loader succeeded in creating an Object</returns> public bool TryLoadObject(IGraph g, INode objNode, Type targetType, out object obj) { obj = null; String server, port, db, user, pwd; int p = -1; //Create the URI Nodes we're going to use to search for things INode propServer = ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyServer), propDb = ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyDatabase); //Get Server and Database details server = ConfigurationLoader.GetConfigurationString(g, objNode, propServer); if (server == null) { server = "localhost"; } db = ConfigurationLoader.GetConfigurationString(g, objNode, propDb); if (db == null) { db = VirtuosoManager.DefaultDB; } //Get Port port = ConfigurationLoader.GetConfigurationString(g, objNode, ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyPort)); if (!Int32.TryParse(port, out p)) { p = VirtuosoManager.DefaultPort; } //Get user credentials ConfigurationLoader.GetUsernameAndPassword(g, objNode, true, out user, out pwd); if (user == null || pwd == null) { return(false); } //Based on this information create a Manager if possible switch (targetType.FullName) { case NonNativeVirtuosoManager: obj = new NonNativeVirtuosoManager(server, p, db, user, pwd); break; case Virtuoso: //Get Server settings server = ConfigurationLoader.GetConfigurationString(g, objNode, propServer); if (server == null) { return(false); } obj = new VirtuosoManager(server, p, db, user, pwd); break; } return(obj != null); }
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(); } }