/// <summary> /// Loads a Graph from the Store. /// </summary> /// <param name="handler">RDF Handler.</param> /// <param name="graphUri">Graph URI to load.</param> public override void LoadGraph(IRdfHandler handler, Uri graphUri) { IGraph g = null; if (_dataset.HasGraph(graphUri)) { g = _dataset[graphUri]; } handler.Apply(g); }
/// <summary> /// Creates a new Leviathan Update Processor. /// </summary> /// <param name="data">SPARQL Dataset.</param> public LeviathanUpdateProcessor(ISparqlDataset data) { _dataset = data; if (!_dataset.HasGraph(null)) { // Create the Default unnamed Graph if it doesn't exist and then Flush() the change _dataset.AddGraph(new Graph()); _dataset.Flush(); } }
private void TestCreateGraphRollbackWithoutAutoCommit() { ISparqlDataset dataset = this.GetEmptyDataset(); String updates = "CREATE GRAPH <" + TestGraphUri.ToString() + ">; CREATE GRAPH <" + TestGraphUri.ToString() + ">"; SparqlUpdateCommandSet cmds = this._parser.ParseFromString(updates); LeviathanUpdateProcessor processor = new LeviathanUpdateProcessor(dataset); processor.AutoCommit = false; try { processor.ProcessCommandSet(cmds); Assert.True(false, "Did not throw a SparqlUpdateException as expected"); } catch (SparqlUpdateException upEx) { TestTools.ReportError("Update Exception", upEx); } Assert.True(dataset.HasGraph(TestGraphUri), "Graph should exist as the Transaction has not been committed yet as Auto-Commit is off"); //Try to Flush() which should error try { processor.Flush(); Assert.True(false, "Did not throw a SparqlUpdateException as expected on call to Flush()"); } catch (SparqlUpdateException upEx) { Console.WriteLine("Threw error when attempting to Flush() as expected"); TestTools.ReportError("Update Exception", upEx); } //Now discard processor.Discard(); Assert.False(dataset.HasGraph(TestGraphUri), "Graph should not exist as the Discard() should cause it to be removed from the Dataset"); }
private void TestCreateGraphCommit() { ISparqlDataset dataset = this.GetEmptyDataset(); String updates = "CREATE GRAPH <" + TestGraphUri.ToString() + ">"; SparqlUpdateCommandSet cmds = this._parser.ParseFromString(updates); LeviathanUpdateProcessor processor = new LeviathanUpdateProcessor(dataset); processor.ProcessCommandSet(cmds); Assert.True(dataset.HasGraph(TestGraphUri), "Graph should exist"); }
private void TestInsertDataSequenceCommit() { ISparqlDataset dataset = this.GetEmptyDataset(); String updates = "INSERT DATA { GRAPH <" + TestGraphUri.ToString() + "> { } }; INSERT DATA { GRAPH <" + TestGraphUri.ToString() + "> { <ex:subj> <ex:pred> <ex:obj> } }"; SparqlUpdateCommandSet cmds = this._parser.ParseFromString(updates); LeviathanUpdateProcessor processor = new LeviathanUpdateProcessor(dataset); processor.ProcessCommandSet(cmds); Assert.True(dataset.HasGraph(TestGraphUri), "Graph should exist"); Assert.Equal(1, dataset[TestGraphUri].Triples.Count); }
private void TestInsertDataThenDropCommit() { ISparqlDataset dataset = this.GetEmptyDataset(); String updates = "INSERT DATA { GRAPH <" + TestGraphUri.ToString() + "> { <ex:subject> <ex:predicate> <ex:object> } }; DROP GRAPH <" + TestGraphUri.ToString() + ">"; SparqlUpdateCommandSet cmds = this._parser.ParseFromString(updates); LeviathanUpdateProcessor processor = new LeviathanUpdateProcessor(dataset); processor.ProcessCommandSet(cmds); Assert.False(dataset.HasGraph(TestGraphUri), "Graph should not exist as the Flush() should cause it to be removed from the Dataset"); }
private void TestLoadCommit() { ISparqlDataset dataset = this.GetEmptyDataset(); String updates = "LOAD <http://www.dotnetrdf.org/configuration#> INTO GRAPH <" + TestGraphUri.ToString() + ">"; SparqlUpdateCommandSet cmds = this._parser.ParseFromString(updates); LeviathanUpdateProcessor processor = new LeviathanUpdateProcessor(dataset); processor.ProcessCommandSet(cmds); Assert.True(dataset.HasGraph(TestGraphUri), "Graph should exist"); }
private void TestDropGraphCommit() { ISparqlDataset dataset = this.GetNonEmptyDataset(); String updates = "DROP GRAPH <" + TestGraphUri.ToString() + ">"; SparqlUpdateParser parser = new SparqlUpdateParser(); SparqlUpdateCommandSet cmds = parser.ParseFromString(updates); LeviathanUpdateProcessor processor = new LeviathanUpdateProcessor(dataset); processor.ProcessCommandSet(cmds); Assert.False(dataset.HasGraph(TestGraphUri), "Graph should not exist"); }
private void TestCreateGraphRollback() { ISparqlDataset dataset = this.GetEmptyDataset(); String updates = "CREATE GRAPH <" + TestGraphUri.ToString() + ">; CREATE GRAPH <" + TestGraphUri.ToString() + ">"; SparqlUpdateCommandSet cmds = this._parser.ParseFromString(updates); LeviathanUpdateProcessor processor = new LeviathanUpdateProcessor(dataset); try { processor.ProcessCommandSet(cmds); Assert.True(false, "Did not thrown a SparqlUpdateException as expected"); } catch (SparqlUpdateException upEx) { TestTools.ReportError("Update Exception", upEx); } Assert.False(dataset.HasGraph(TestGraphUri), "Graph should not exist as the Discard() should cause it to be removed from the Dataset"); }
private void TestCreateDropSequenceRollback2() { ISparqlDataset dataset = this.GetNonEmptyDataset(); String updates = "DROP GRAPH <" + TestGraphUri.ToString() + ">; CREATE GRAPH <" + TestGraphUri.ToString() + ">; DROP GRAPH <" + TestGraphUri.ToString() + ">; DROP GRAPH <" + TestGraphUri.ToString() + ">"; SparqlUpdateCommandSet cmds = this._parser.ParseFromString(updates); LeviathanUpdateProcessor processor = new LeviathanUpdateProcessor(dataset); try { processor.ProcessCommandSet(cmds); Assert.True(false, "Expected SPARQL Update Exception was not thrown"); } catch (SparqlUpdateException upEx) { TestTools.ReportError("Update Exception", upEx); } Assert.True(dataset.HasGraph(TestGraphUri), "Graph should not exist"); }
private void TestDropGraphRollback() { ISparqlDataset dataset = this.GetNonEmptyDataset(); String updates = "DROP GRAPH <" + TestGraphUri.ToString() + ">; DROP GRAPH <" + TestGraphUri.ToString() + ">"; SparqlUpdateCommandSet cmds = this._parser.ParseFromString(updates); LeviathanUpdateProcessor processor = new LeviathanUpdateProcessor(dataset); try { processor.ProcessCommandSet(cmds); Assert.True(false, "Did not thrown a SparqlUpdateException as expected"); } catch (SparqlUpdateException upEx) { TestTools.ReportError("Update Exception", upEx); } Assert.True(dataset.HasGraph(TestGraphUri), "Graph should exist as the Discard() should ensure it was still in the Dataset"); }
private void TestDropThenInsertDataRollback() { ISparqlDataset dataset = this.GetNonEmptyDataset(); String updates = "DROP GRAPH <" + TestGraphUri.ToString() + ">; INSERT DATA { GRAPH <" + TestGraphUri.ToString() + "> { <ex:subject> <ex:predicate> <ex:object> } }; CREATE GRAPH <" + TestGraphUri.ToString() + ">"; SparqlUpdateCommandSet cmds = this._parser.ParseFromString(updates); LeviathanUpdateProcessor processor = new LeviathanUpdateProcessor(dataset); try { processor.ProcessCommandSet(cmds); Assert.True(false, "Did not thrown a SparqlUpdateException as expected"); } catch (SparqlUpdateException upEx) { TestTools.ReportError("Update Exception", upEx); } Assert.True(dataset.HasGraph(TestGraphUri), "Graph should not exist as the Discard() should cause it to be removed from the Dataset"); Assert.True(dataset[TestGraphUri].IsEmpty, "Graph should be empty as the Discard() should have reversed the INSERT DATA"); }
/// <summary> /// Gets whether the dataset contains a given Graph. /// </summary> /// <param name="graphUri">Graph URI.</param> /// <returns></returns> public virtual bool HasGraph(Uri graphUri) { return(_dataset.HasGraph(graphUri)); }
/// <summary> /// Gets whether the Collection contains a Graph with the given URI. /// </summary> /// <param name="graphUri">Graph URI.</param> /// <returns></returns> public override bool Contains(Uri graphUri) { return(_dataset.HasGraph(graphUri)); }
/// <summary> /// Processes a SPARQL Query sending the results to a RDF/SPARQL Results handler as appropriate. /// </summary> /// <param name="rdfHandler">RDF Handler.</param> /// <param name="resultsHandler">Results Handler.</param> /// <param name="query">SPARQL Query.</param> public void ProcessQuery(IRdfHandler rdfHandler, ISparqlResultsHandler resultsHandler, SparqlQuery query) { // Do Handler null checks before evaluating the query if (query == null) { throw new ArgumentNullException("query", "Cannot evaluate a null query"); } if (rdfHandler == null && (query.QueryType == SparqlQueryType.Construct || query.QueryType == SparqlQueryType.Describe || query.QueryType == SparqlQueryType.DescribeAll)) { throw new ArgumentNullException("rdfHandler", "Cannot use a null RDF Handler when the Query is a CONSTRUCT/DESCRIBE"); } if (resultsHandler == null && (query.QueryType == SparqlQueryType.Ask || SparqlSpecsHelper.IsSelectQuery(query.QueryType))) { throw new ArgumentNullException("resultsHandler", "Cannot use a null resultsHandler when the Query is an ASK/SELECT"); } // Handle the Thread Safety of the Query Evaluation ReaderWriterLockSlim currLock = (_dataset is IThreadSafeDataset) ? ((IThreadSafeDataset)_dataset).Lock : _lock; try { currLock.EnterReadLock(); // Reset Query Timers query.QueryExecutionTime = null; bool datasetOk = false, defGraphOk = false; try { // Set up the Default and Active Graphs if (query.DefaultGraphs.Any()) { // Call HasGraph() on each Default Graph but ignore the results, we just do this // in case a dataset has any kind of load on demand behaviour foreach (Uri defGraphUri in query.DefaultGraphs) { _dataset.HasGraph(defGraphUri); } _dataset.SetDefaultGraph(query.DefaultGraphs); defGraphOk = true; } else if (query.NamedGraphs.Any()) { // No FROM Clauses but one/more FROM NAMED means the Default Graph is the empty graph _dataset.SetDefaultGraph(Enumerable.Empty <Uri>()); } _dataset.SetActiveGraph(_dataset.DefaultGraphUris); datasetOk = true; // Convert to Algebra and execute the Query SparqlEvaluationContext context = GetContext(query); BaseMultiset result; try { context.StartExecution(); ISparqlAlgebra algebra = query.ToAlgebra(); result = context.Evaluate(algebra); context.EndExecution(); query.QueryExecutionTime = new TimeSpan(context.QueryTimeTicks); } catch (RdfQueryException) { context.EndExecution(); query.QueryExecutionTime = new TimeSpan(context.QueryTimeTicks); throw; } catch { context.EndExecution(); query.QueryExecutionTime = new TimeSpan(context.QueryTimeTicks); throw; } // Return the Results switch (query.QueryType) { case SparqlQueryType.Ask: case SparqlQueryType.Select: case SparqlQueryType.SelectAll: case SparqlQueryType.SelectAllDistinct: case SparqlQueryType.SelectAllReduced: case SparqlQueryType.SelectDistinct: case SparqlQueryType.SelectReduced: // For SELECT and ASK can populate a Result Set directly from the Evaluation Context // return new SparqlResultSet(context); resultsHandler.Apply(context); break; case SparqlQueryType.Construct: // Create a new Empty Graph for the Results try { rdfHandler.StartRdf(); foreach (String prefix in query.NamespaceMap.Prefixes) { if (!rdfHandler.HandleNamespace(prefix, query.NamespaceMap.GetNamespaceUri(prefix))) { ParserHelper.Stop(); } } // Construct the Triples for each Solution if (context.OutputMultiset is IdentityMultiset) { context.OutputMultiset = new SingletonMultiset(); } foreach (ISet s in context.OutputMultiset.Sets) { // List<Triple> constructedTriples = new List<Triple>(); try { ConstructContext constructContext = new ConstructContext(rdfHandler, s, false); foreach (IConstructTriplePattern p in query.ConstructTemplate.TriplePatterns.OfType <IConstructTriplePattern>()) { try { if (!rdfHandler.HandleTriple(p.Construct(constructContext))) { ParserHelper.Stop(); } // constructedTriples.Add(((IConstructTriplePattern)p).Construct(constructContext)); } catch (RdfQueryException) { // If we get an error here then we could not construct a specific triple // so we continue anyway } } } catch (RdfQueryException) { // If we get an error here this means we couldn't construct for this solution so the // entire solution is discarded continue; } // h.Assert(constructedTriples); } rdfHandler.EndRdf(true); } catch (RdfParsingTerminatedException) { rdfHandler.EndRdf(true); } catch { rdfHandler.EndRdf(false); throw; } break; case SparqlQueryType.Describe: case SparqlQueryType.DescribeAll: // For DESCRIBE we retrieve the Describe algorithm and apply it ISparqlDescribe describer = query.Describer; describer.Describe(rdfHandler, context); break; default: throw new RdfQueryException("Unknown query types cannot be processed by Leviathan"); } } finally { if (defGraphOk) { _dataset.ResetDefaultGraph(); } if (datasetOk) { _dataset.ResetActiveGraph(); } } } finally { currLock.ExitReadLock(); } }