/// <summary> /// Tries to load a Triple Store 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; ITripleStore store = null; INode subObj; bool async; Object temp; //Get Property Nodes we need INode propSqlManager = ConfigurationLoader.CreateConfigurationNode(g, "dnr:sqlManager"), propGenericManager = ConfigurationLoader.CreateConfigurationNode(g, "dnr:genericManager"), propAsync = ConfigurationLoader.CreateConfigurationNode(g, "dnr:async"); //Instantiate the Store Class switch (targetType.FullName) { case TripleStore: store = new TripleStore(); break; case WebDemandTripleStore: store = new WebDemandTripleStore(); break; #if !NO_DATA && !NO_STORAGE case SqlTripleStore: case ThreadedSqlTripleStore: case OnDemandTripleStore: subObj = ConfigurationLoader.GetConfigurationNode(g, objNode, propSqlManager); if (subObj == null) return false; temp = ConfigurationLoader.LoadObject(g, subObj); if (temp is ISqlIOManager) { if (targetType.FullName.Equals(SqlTripleStore)) { store = new SqlTripleStore((ISqlIOManager)temp); } else if (targetType.FullName.Equals(OnDemandTripleStore)) { store = new OnDemandTripleStore((ISqlIOManager)temp); } else if (temp is IThreadedSqlIOManager) { async = ConfigurationLoader.GetConfigurationBoolean(g, objNode, propAsync, false); store = new ThreadedSqlTripleStore((IThreadedSqlIOManager)temp, async); } else { throw new DotNetRdfConfigurationException("Unable to load a SQL Triple Store identified by the Node '" + objNode.ToString() + "' as the value given for the dnr:sqlManager property points to an Object which could not be loaded as an object which implements either the ISqlIOManager/IThreadedSqlIOManager interface"); } } else { throw new DotNetRdfConfigurationException("Unable to load a SQL Triple Store identified by the Node '" + objNode.ToString() + "' as the value given for the dnr:sqlManager property points to an Object which could not be loaded as an object which implements either the ISqlIOManager interface"); } break; #endif #if !NO_STORAGE case NativeTripleStore: subObj = ConfigurationLoader.GetConfigurationNode(g, objNode, propGenericManager); if (subObj == null) return false; temp = ConfigurationLoader.LoadObject(g, subObj); if (temp is IQueryableGenericIOManager) { store = new NativeTripleStore((IQueryableGenericIOManager)temp); } else { throw new DotNetRdfConfigurationException("Unable to load a Native Triple Store identified by the Node '" + objNode.ToString() + "' as the value given for the dnr:genericManager property points to an Object which could not be loaded as an object which implements the IQueryableGenericIOManager interface"); } break; #endif } //Read in additional data to be added to the Store if (store != null) { IEnumerable<INode> sources = ConfigurationLoader.GetConfigurationData(g, objNode, ConfigurationLoader.CreateConfigurationNode(g, "dnr:usingGraph")); //Read from Graphs foreach (INode source in sources) { temp = ConfigurationLoader.LoadObject(g, source); if (temp is IGraph) { store.Add((IGraph)temp); } else { throw new DotNetRdfConfigurationException("Unable to load data from a Graph for the Triple Store identified by the Node '" + objNode.ToString() + "' as one of the values for the dnr:usingGraph 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(store, ((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"); } } //Read from Files - we assume these files are Dataset Files sources = ConfigurationLoader.GetConfigurationData(g, objNode, ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyFromFile)); foreach (INode source in sources) { if (source.NodeType == NodeType.Literal) { FileLoader.Load(store, ConfigurationLoader.ResolvePath(((ILiteralNode)source).Value)); } else { throw new DotNetRdfConfigurationException("Unable to load data from a file for the Triple Store identified by the Node '" + objNode.ToString() + "' as one of the values for the dnr:fromFile property is not a Literal Node as required"); } } //Finally we'll apply any reasoners if (store is IInferencingTripleStore) { IEnumerable<INode> reasoners = ConfigurationLoader.GetConfigurationData(g, objNode, ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyReasoner)); foreach (INode reasoner in reasoners) { temp = ConfigurationLoader.LoadObject(g, reasoner); if (temp is IInferenceEngine) { ((IInferencingTripleStore)store).AddInferenceEngine((IInferenceEngine)temp); } 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 = store; return (store != null); }
public void SparqlDefaultGraphExists6() { MicrosoftSqlStoreManager db = new MicrosoftSqlStoreManager("localhost", "unit_test", "example", "password"); db.PreserveState = true; SqlTripleStore store = new SqlTripleStore(db); //Create Default Graph only if required if (!store.HasGraph(null)) { Graph g = new Graph(); g.Assert(g.CreateUriNode(new Uri("http://example.org/subject")), g.CreateUriNode(new Uri("http://example.org/predicate")), g.CreateUriNode(new Uri("http://example.org/object"))); store.Add(g); store.Flush(); } Object results = store.ExecuteQuery("ASK WHERE { GRAPH <dotnetrdf:default-graph> { ?s ?p ?o }}"); if (results is SparqlResultSet) { Assert.IsTrue(((SparqlResultSet)results).Result); } else { Assert.Fail("ASK Query did not return a SPARQL Result Set as expected"); } store.Flush(); db.PreserveState = false; store.Dispose(); }
/// <summary> /// Tries to load a Triple Store 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; ITripleStore store = null; INode subObj; bool isAsync; Object temp; //Get Property Nodes we need INode propSqlManager = ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertySqlManager), propGenericManager = ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyGenericManager), propAsync = ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyAsync); //Instantiate the Store Class switch (targetType.FullName) { case TripleStore: store = new TripleStore(); break; #if !SILVERLIGHT case WebDemandTripleStore: store = new WebDemandTripleStore(); break; #endif #if !NO_DATA && !NO_STORAGE case SqlTripleStore: case ThreadedSqlTripleStore: case OnDemandTripleStore: subObj = ConfigurationLoader.GetConfigurationNode(g, objNode, propSqlManager); if (subObj == null) { return(false); } temp = ConfigurationLoader.LoadObject(g, subObj); if (temp is ISqlIOManager) { if (targetType.FullName.Equals(SqlTripleStore)) { store = new SqlTripleStore((ISqlIOManager)temp); } else if (targetType.FullName.Equals(OnDemandTripleStore)) { store = new OnDemandTripleStore((ISqlIOManager)temp); } else if (temp is IThreadedSqlIOManager) { isAsync = ConfigurationLoader.GetConfigurationBoolean(g, objNode, propAsync, false); store = new ThreadedSqlTripleStore((IThreadedSqlIOManager)temp, isAsync); } else { throw new DotNetRdfConfigurationException("Unable to load a SQL Triple Store identified by the Node '" + objNode.ToString() + "' as the value given for the dnr:sqlManager property points to an Object which could not be loaded as an object which implements either the ISqlIOManager/IThreadedSqlIOManager interface"); } } else { throw new DotNetRdfConfigurationException("Unable to load a SQL Triple Store identified by the Node '" + objNode.ToString() + "' as the value given for the dnr:sqlManager property points to an Object which could not be loaded as an object which implements either the ISqlIOManager interface"); } break; #endif #if !NO_STORAGE case NativeTripleStore: subObj = ConfigurationLoader.GetConfigurationNode(g, objNode, propGenericManager); if (subObj == null) { return(false); } temp = ConfigurationLoader.LoadObject(g, subObj); if (temp is IQueryableGenericIOManager) { store = new NativeTripleStore((IQueryableGenericIOManager)temp); } else { throw new DotNetRdfConfigurationException("Unable to load a Native Triple Store identified by the Node '" + objNode.ToString() + "' as the value given for the dnr:genericManager property points to an Object which could not be loaded as an object which implements the IQueryableGenericIOManager interface"); } break; case PersistentTripleStore: subObj = ConfigurationLoader.GetConfigurationNode(g, objNode, propGenericManager); if (subObj == null) { return(false); } temp = ConfigurationLoader.LoadObject(g, subObj); if (temp is IGenericIOManager) { store = new PersistentTripleStore((IGenericIOManager)temp); } else { throw new DotNetRdfConfigurationException("Unable to load a Persistent Triple Store identified by the Node '" + objNode.ToString() + "' as the value given the for dnr:genericManager property points to an Object which could not be loaded as an object which implements the IGenericIOManager interface"); } break; #endif } //Read in additional data to be added to the Store if (store != null) { IEnumerable <INode> sources = ConfigurationLoader.GetConfigurationData(g, objNode, ConfigurationLoader.CreateConfigurationNode(g, "dnr:usingGraph")); //Read from Graphs foreach (INode source in sources) { temp = ConfigurationLoader.LoadObject(g, source); if (temp is IGraph) { store.Add((IGraph)temp); } else { throw new DotNetRdfConfigurationException("Unable to load data from a Graph for the Triple Store identified by the Node '" + objNode.ToString() + "' as one of the values for the dnr:usingGraph 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(store, ((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"); } } //Read from Files - we assume these files are Dataset Files sources = ConfigurationLoader.GetConfigurationData(g, objNode, ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyFromFile)); foreach (INode source in sources) { if (source.NodeType == NodeType.Literal) { FileLoader.Load(store, ConfigurationLoader.ResolvePath(((ILiteralNode)source).Value)); } else { throw new DotNetRdfConfigurationException("Unable to load data from a file for the Triple Store identified by the Node '" + objNode.ToString() + "' as one of the values for the dnr:fromFile property is not a Literal Node as required"); } } //Finally we'll apply any reasoners if (store is IInferencingTripleStore) { IEnumerable <INode> reasoners = ConfigurationLoader.GetConfigurationData(g, objNode, ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyReasoner)); foreach (INode reasoner in reasoners) { temp = ConfigurationLoader.LoadObject(g, reasoner); if (temp is IInferenceEngine) { ((IInferencingTripleStore)store).AddInferenceEngine((IInferenceEngine)temp); } 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"); } } } //And as an absolute final step if the store is transactional we'll flush any changes we've made if (store is ITransactionalStore) { ((ITransactionalStore)store).Flush(); } } obj = store; return(store != null); }