/// <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 SparqlViewNativeAllegroGraph() { try { AllegroGraphConnector agraph = new AllegroGraphConnector("http://localhost:9875", "test", "unit-test"); NativeTripleStore store = new NativeTripleStore(agraph); //Load a Graph into the Store to ensure there is some data for the view to retrieve Graph g = new Graph(); FileLoader.Load(g, "InferenceTest.ttl"); agraph.SaveGraph(g); //Create the SPARQL View NativeSparqlView view = new NativeSparqlView("CONSTRUCT { ?s ?p ?o } WHERE { ?s ?p ?o . FILTER(IsLiteral(?o)) }", store); Console.WriteLine("SPARQL View Populated"); TestTools.ShowGraph(view); Console.WriteLine(); } catch (RdfQueryException queryEx) { TestTools.ReportError("Query Error", queryEx, true); } catch (RdfException ex) { TestTools.ReportError("Error", ex, true); } }