public void ParsingResultSetHandlerImplicitSparqlRdfNTriples()
        {
            this.EnsureTestData("test.sparql.nt", new SparqlRdfWriter(new NTriplesWriter()));

            SparqlRdfParser parser = new SparqlRdfParser(new NTriplesParser());
            SparqlResultSet results = new SparqlResultSet();
            parser.Load(results, "test.sparql.nt");

            NTriplesFormatter formatter = new NTriplesFormatter();
            foreach (SparqlResult r in results)
            {
                Console.WriteLine(r.ToString(formatter));
            }

            Assert.IsFalse(results.IsEmpty, "Result Set should not be empty");
            Assert.AreEqual(SparqlResultsType.VariableBindings, results.ResultsType, "Results Type should be VariableBindings");
        }
 private void CompareResultGraphs(string results, string expectedResultsPath, bool reduced)
 {
     var rdfParser = new SparqlRdfParser();
     var xmlParser = new SparqlXmlParser();
     var actualResultSet = new SparqlResultSet();
     var expectedResultSet = new SparqlResultSet();
     using(var tr = new StringReader(results))
     {
         xmlParser.Load(actualResultSet, tr);
     }
     rdfParser.Load(expectedResultSet, expectedResultsPath);
     CompareSparqlResults(actualResultSet, expectedResultSet, reduced);
 }
Beispiel #3
0
 private void CompareResultGraphs(string results, string expectedResultsPath, bool reduced)
 {
     var expectedResultGraph = new Graph();
     FileLoader.Load(expectedResultGraph, expectedResultsPath);
     var resultSet = expectedResultGraph.GetUriNode(new Uri("http://www.w3.org/2001/sw/DataAccess/tests/result-set#ResultSet"));
     if (resultSet != null)
     {
         var rdfParser = new SparqlRdfParser();
         var xmlParser = new SparqlXmlParser();
         var actualResultSet = new SparqlResultSet();
         var expectedResultSet = new SparqlResultSet();
         using (var tr = new StringReader(results))
         {
             xmlParser.Load(actualResultSet, tr);
         }
         rdfParser.Load(expectedResultSet, expectedResultsPath);
         var bnodeMap = new Dictionary<string, string>();
         CompareSparqlResults(actualResultSet, expectedResultSet, reduced, bnodeMap);
     }
     else
     {
         // This is a constructed graph
         var actualGraph = new Graph();
         actualGraph.LoadFromString(results);
         CompareTripleCollections(actualGraph.Triples, expectedResultGraph.Triples, reduced);
     }
 }
        /// <summary>
        /// Makes a SPARQL Query against the underlying Store processing the results with an appropriate handler from those provided
        /// </summary>
        /// <param name="rdfHandler">RDF Handler</param>
        /// <param name="resultsHandler">Results Handler</param>
        /// <param name="sparqlQuery">SPARQL Query</param>
        /// <returns></returns>
        public virtual void Query(IRdfHandler rdfHandler, ISparqlResultsHandler resultsHandler, String sparqlQuery)
        {
            try
            {
                //Pre-parse the query to determine what the Query Type is
                bool isAsk = false;
                SparqlQuery q = null;
                try
                {
                    q = this._parser.ParseFromString(sparqlQuery);
                    isAsk = q.QueryType == SparqlQueryType.Ask;
                }
                catch
                {
                    //If parsing error fallback to naive detection
                    isAsk = Regex.IsMatch(sparqlQuery, "ASK", RegexOptions.IgnoreCase);
                }

                //Select Accept Header
                String accept;
                if (q != null)
                {
                    accept = (SparqlSpecsHelper.IsSelectQuery(q.QueryType) || q.QueryType == SparqlQueryType.Ask ? MimeTypesHelper.HttpSparqlAcceptHeader : MimeTypesHelper.HttpAcceptHeader);
                }
                else
                {
                    accept = MimeTypesHelper.HttpRdfOrSparqlAcceptHeader;
                }

                HttpWebRequest request;

                //Create the Request
                Dictionary<String, String> queryParams = new Dictionary<string, string>();
                if (sparqlQuery.Length < 2048 && !this._postAllQueries)
                {
                    queryParams.Add("query", EscapeQuery(sparqlQuery));

                    request = this.CreateRequest(this._repositoriesPrefix + this._store + this._queryPath, accept, "GET", queryParams);
                }
                else
                {
                    request = this.CreateRequest(this._repositoriesPrefix + this._store + this._queryPath, accept, "POST", queryParams);

                    //Build the Post Data and add to the Request Body
                    request.ContentType = MimeTypesHelper.WWWFormURLEncoded;
                    StringBuilder postData = new StringBuilder();
                    postData.Append("query=");
                    postData.Append(Uri.EscapeDataString(EscapeQuery(sparqlQuery)));
                    StreamWriter writer = new StreamWriter(request.GetRequestStream());
                    writer.Write(postData);
                    writer.Close();
                }

            #if DEBUG
                if (Options.HttpDebugging)
                {
                    Tools.HttpDebugRequest(request);
                }
            #endif

                //Get the Response and process based on the Content Type
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
            #if DEBUG
                    if (Options.HttpDebugging)
                    {
                        Tools.HttpDebugResponse(response);
                    }
            #endif
                    StreamReader data = new StreamReader(response.GetResponseStream());
                    String ctype = response.ContentType;
                    try
                    {
                        //Is the Content Type referring to a Sparql Result Set format?
                        ISparqlResultsReader resreader = MimeTypesHelper.GetSparqlParser(ctype, isAsk);
                        resreader.Load(resultsHandler, data);
                        response.Close();
                    }
                    catch (RdfParserSelectionException)
                    {
                        //If we get a Parser Selection exception then the Content Type isn't valid for a Sparql Result Set

                        //Is the Content Type referring to a RDF format?
                        IRdfReader rdfreader = MimeTypesHelper.GetParser(ctype);
                        if (q != null && (SparqlSpecsHelper.IsSelectQuery(q.QueryType) || q.QueryType == SparqlQueryType.Ask))
                        {
                            SparqlRdfParser resreader = new SparqlRdfParser(rdfreader);
                            resreader.Load(resultsHandler, data);
                        }
                        else
                        {
                            rdfreader.Load(rdfHandler, data);
                        }
                        response.Close();
                    }
                }
            }
            catch (WebException webEx)
            {
                if (webEx.Response != null)
                {
            #if DEBUG
                    if (Options.HttpDebugging)
                    {
                        Tools.HttpDebugResponse((HttpWebResponse)webEx.Response);
                    }
            #endif
                    if (webEx.Response.ContentLength > 0)
                    {
                        try
                        {
                            String responseText = new StreamReader(webEx.Response.GetResponseStream()).ReadToEnd();
                            throw new RdfQueryException("A HTTP error occured while querying the Store.  Store returned the following error message: " + responseText, webEx);
                        }
                        catch
                        {
                            throw new RdfQueryException("A HTTP error occurred while querying the Store", webEx);
                        }
                    }
                    else
                    {
                        throw new RdfQueryException("A HTTP error occurred while querying the Store", webEx);
                    }
                }
                else
                {
                    throw new RdfQueryException("A HTTP error occurred while querying the Store", webEx);
                }
            }
        }
        private int ProcessEvaluationTest(SparqlQueryParser parser, Triple commentDef, String queryFile, String dataFile, List<String> dataFiles, String resultFile)
        {
            Console.WriteLine("# Processing Evaluation Test " + Path.GetFileName(queryFile));

            if (commentDef != null)
            {
                Console.WriteLine(commentDef.Object.ToString());
                Console.WriteLine();
            }

            if (dataFiles.Contains(dataFile)) dataFiles.Remove(dataFile);
            if (queryFile.StartsWith("file:///")) queryFile = queryFile.Substring(8);
            if (dataFile != null && dataFile.StartsWith("file:///")) dataFile = dataFile.Substring(8);
            if (resultFile.StartsWith("file:///")) resultFile = resultFile.Substring(8);

            Console.WriteLine("Query File is " + queryFile);
            if (evaluationTestOverride.Any(x => queryFile.EndsWith(x)))
            {
                Console.WriteLine();
                Console.WriteLine("# Test Result = Manually overridden to Pass (Test Passed)");
                testsPassed++;
                testsEvaluationPassed++;
                return 1;
            }
            if (dataFile != null) Console.WriteLine("Default Graph File is " + dataFile);
            foreach (String file in dataFiles)
            {
                Console.WriteLine("Uses Named Graph File " + file);
            }
            Console.WriteLine("Expected Result File is " + resultFile);
            Console.WriteLine();

            SparqlQuery query;
            try
            {
                query = parser.ParseFromFile(queryFile);

                Console.WriteLine(query.ToString());
                Console.WriteLine();
                Console.WriteLine("Formatted with SparqlFormatter");
                SparqlFormatter formatter = new SparqlFormatter(query.NamespaceMap);
                Console.WriteLine(formatter.Format(query));
                Console.WriteLine();

                try
                {
                    Console.WriteLine(query.ToAlgebra().ToString());
                    Console.WriteLine();
                }
                catch
                {
                    //Do Nothing
                }
            }
            catch (RdfParseException parseEx)
            {
                this.ReportError("Query Parser Error", parseEx);
                testsFailed++;
                testsEvaluationFailed++;
                Console.WriteLine("# Test Result = Unable to parse query (Test Failed)");
                return -1;
            }

            IInMemoryQueryableStore store;
            if (dataFile != null)
            {
                store = new TripleStore();
            }
            else
            {
                store = new WebDemandTripleStore();
            }

            //Load Default Graph
            Graph defaultGraph = new Graph();
            try
            {
                if (dataFile != null)
                {
                    FileLoader.Load(defaultGraph, dataFile);
                }
                store.Add(defaultGraph);
            }
            catch (RdfParseException parseEx)
            {
                this.ReportError("Parser Error", parseEx);
                testsFailed++;
                testsEvaluationFailed++;
                Console.WriteLine("# Test Result = Unable to parse Default Graph (Test Failed)");
                return -1;
            }

            //Load Named Graphs
            try
            {
                foreach (String graphFile in dataFiles)
                {
                    Graph namedGraph = new Graph();
                    if (graphFile.StartsWith("file:///"))
                    {
                        FileLoader.Load(namedGraph, graphFile.Substring(8));
                    }
                    else
                    {
                        FileLoader.Load(namedGraph, graphFile);
                    }
                    store.Add(namedGraph);
                }
            }
            catch (RdfParseException parseEx)
            {
                this.ReportError("Parser Error", parseEx);
                testsFailed++;
                testsEvaluationFailed++;
                Console.WriteLine("# Test Result - Unable to parse Named Graph (Test Failed)");
                return -1;
            }

            //Create a Dataset and then Set Graphs
            InMemoryDataset dataset = new InMemoryDataset(store);
            if (!query.DefaultGraphs.Any())
            {
                query.AddDefaultGraph(defaultGraph.BaseUri);
                //dataset.SetActiveGraph(defaultGraph.BaseUri);
            }
            if (!query.NamedGraphs.Any())
            {
                foreach (String namedGraphUri in dataFiles)
                {
                    query.AddNamedGraph(new Uri(namedGraphUri));
                }
            }
            
            //Try and get the result
            Object results = null;
            try
            {
                results = query.Evaluate(dataset);
            }
            catch (RdfQueryException queryEx)
            {
                this.ReportError("Query Error", queryEx);
                testsFailed++;
                testsEvaluationFailed++;
                Console.WriteLine("# Test Result - Query execution failed (Test Failed)");
                return -1;
            }
            catch (Exception ex)
            {
                this.ReportError("Other Error", ex);
                testsFailed++;
                testsEvaluationFailed++;
                Console.WriteLine("# Test Result - Query failed (Test Failed)");
                return -1;
            }

            if (results == null)
            {
                testsFailed++;
                testsEvaluationFailed++;
                Console.WriteLine("# Test Result - No result was returned from the Query (Test Failed)");
                return -1;
            }

            //Load in the expected results
            if (results is SparqlResultSet)
            {
                //Save our Results so we can manually compare as needed
                SparqlResultSet ourResults = (SparqlResultSet)results;
                SparqlXmlWriter writer = new SparqlXmlWriter();
                writer.Save(ourResults, resultFile + ".out");
                SparqlResultSet expectedResults = new SparqlResultSet();

                if (resultFile.EndsWith(".srx"))
                {
                    try
                    {
                        SparqlXmlParser resultSetParser = new SparqlXmlParser();
                        resultSetParser.Load(expectedResults, resultFile);
                    }
                    catch (RdfParseException parseEx)
                    {
                        this.ReportError("Result Set Parser Error", parseEx);
                        testsIndeterminate++;
                        testsEvaluationIndeterminate++;
                        Console.WriteLine("# Test Result - Error loading expected Result Set (Test Indeterminate)");
                        return 0;
                    }
                }
                else if (resultFile.EndsWith(".ttl") || resultFile.EndsWith(".rdf"))
                {
                    try
                    {
                        SparqlRdfParser resultSetParser = new SparqlRdfParser();
                        resultSetParser.Load(expectedResults, resultFile);
                    }
                    catch (RdfParseException parseEx)
                    {
                        this.ReportError("Result Set Parser Error", parseEx);
                        testsIndeterminate++;
                        testsEvaluationIndeterminate++;
                        Console.WriteLine("# Test Result - Error loading expected Result Set (Test Indeterminate)");
                        return 0;
                    }
                }
                else
                {
                    testsIndeterminate++;
                    testsEvaluationIndeterminate++;
                    Console.WriteLine("# Test Result - Unable to load the expected Result Set (Test Indeterminate)");
                    return 0;
                }

                try
                {
                    ourResults.Trim();
                    expectedResults.Trim();
                    if (ourResults.Equals(expectedResults))
                    {
                        testsPassed++;
                        testsEvaluationPassed++;
                        Console.WriteLine("# Test Result - Result Set as expected (Test Passed)");
                        return 1;
                    }
                    else
                    {
                        Console.WriteLine("Final Query");
                        Console.WriteLine(query.ToString());
                        Console.WriteLine();
                        this.ShowTestData(store);
                        this.ShowResultSets(ourResults, expectedResults);
                        testsFailed++;
                        testsEvaluationFailed++;
                        Console.WriteLine("# Test Result - Result Set not as expected (Test Failed)");
                        return -1;
                    }
                }
                catch (NotImplementedException)
                {
                    this.ShowResultSets(ourResults, expectedResults);
                    testsIndeterminate++;
                    testsEvaluationIndeterminate++;
                    Console.WriteLine("# Test Result - Unable to establish if Result Set was as expected (Test Indeterminate)");
                    return 0;
                }
            }
            else if (results is Graph)
            {
                if (resultFile.EndsWith(".ttl"))
                {
                    //Save our Results so we can manually compare as needed
                    Graph ourResults = (Graph)results;
                    CompressingTurtleWriter writer = new CompressingTurtleWriter();
                    writer.Save(ourResults, resultFile + ".out");

                    try
                    {
                        Graph expectedResults = new Graph();
                        TurtleParser ttlparser = new TurtleParser();
                        ttlparser.Load(expectedResults, resultFile);

                        try
                        {
                            if (ourResults.Equals(expectedResults))
                            {
                                testsPassed++;
                                testsEvaluationPassed++;
                                Console.WriteLine("# Test Result - Graph as expected (Test Passed)");
                                return 1;
                            }
                            else
                            {
                                this.ShowTestData(store);
                                this.ShowGraphs(ourResults, expectedResults);
                                testsFailed++;
                                testsEvaluationFailed++;
                                Console.WriteLine("# Test Result - Graph not as expected (Test Failed)");
                                return -1;
                            }
                        }
                        catch (NotImplementedException)
                        {
                            this.ShowGraphs(ourResults, expectedResults);
                            testsIndeterminate++;
                            testsEvaluationIndeterminate++;
                            Console.WriteLine("# Test Result - Unable to establish if Graph was as expected (Test Indeterminate)");
                            return 0;
                        }
                    }
                    catch (RdfParseException parseEx)
                    {
                        this.ReportError("Graph Parser Error", parseEx);
                        testsIndeterminate++;
                        testsEvaluationIndeterminate++;
                        Console.WriteLine("# Test Result - Error loading expected Graph (Test Indeterminate)");
                        return 0;
                    }
                }
                else
                {
                    testsIndeterminate++;
                    testsEvaluationIndeterminate++;
                    Console.WriteLine("# Test Result - Unable to load expected Graph (Test Indeterminate)");
                    return 0;
                }
            }
            else
            {
                testsFailed++;
                testsEvaluationFailed++;
                Console.WriteLine("# Test Result - Didn't produce a Graph as expected (Test Failed)");
                return -1;
            }

        }
 private void CompareResultGraphs(string results, string expectedResultsPath, bool reduced)
 {
     var expectedResultGraph = new Graph();
     FileLoader.Load(expectedResultGraph, expectedResultsPath);
     var resultSet = expectedResultGraph.GetUriNode(new Uri("http://www.w3.org/2001/sw/DataAccess/tests/result-set#ResultSet"));
     if (resultSet != null)
     {
         var rdfParser = new SparqlRdfParser();
         var xmlParser = new SparqlXmlParser();
         var actualResultSet = new SparqlResultSet();
         var expectedResultSet = new SparqlResultSet();
         using (var tr = new StringReader(results))
         {
             xmlParser.Load(actualResultSet, tr);
         }
         rdfParser.Load(expectedResultSet, expectedResultsPath);
         var bnodeMap = new Dictionary<string, string>();
         CompareSparqlResults(actualResultSet, expectedResultSet, reduced, bnodeMap);
     }
     else
     {
         // This is a constructed graph
         var actualGraph = new Graph();
         actualGraph.LoadFromString(results);
         var toReplace = actualGraph.Triples.Where(
             t => IsGenid(t.Subject) || IsGenid(t.Predicate) || IsGenid(t.Object)).ToList();
         foreach (var t in toReplace)
         {
             var mapped = MapGenidToBlank(t);
             actualGraph.Retract(t);
             actualGraph.Assert(mapped);
         }
         CompareTripleCollections(actualGraph.Triples, expectedResultGraph.Triples, reduced);
     }
 }
        public void SparqlEvaluationComplexOptionalGraphUnion()
        {
            SparqlQueryParser parser = new SparqlQueryParser();
            SparqlQuery q = parser.ParseFromFile("q-opt-complex-4.rq");

            TripleStore store = new TripleStore();
            Graph g = new Graph();
            FileLoader.Load(g, "complex-data-2.ttl");
            store.Add(g);
            Graph h = new Graph();
            FileLoader.Load(h, "complex-data-1.ttl");
            store.Add(h);

            InMemoryDataset dataset = new InMemoryDataset(store);
            q.AddDefaultGraph(g.BaseUri);
            q.AddNamedGraph(h.BaseUri);

            SparqlFormatter formatter = new SparqlFormatter(q.NamespaceMap);
            Object results;

            //Examine limited parts of the Query to see why it doesn't work properly
            SparqlParameterizedString unionClause = new SparqlParameterizedString();
            unionClause.Namespaces = q.NamespaceMap;
            unionClause.QueryText = "SELECT * WHERE { ?person foaf:name ?name . { ?person ex:healthplan ?plan . } UNION { ?person ex:department ?dept . } }";
            SparqlQuery unionQuery = parser.ParseFromString(unionClause);

            Console.WriteLine("UNION Clause Only");
            Console.WriteLine(formatter.Format(unionQuery));

            results = unionQuery.Evaluate(dataset);
            if (results is SparqlResultSet)
            {
                SparqlResultSet rset = (SparqlResultSet)results;
                TestTools.ShowResults(rset);
            }
            else
            {
                Assert.Fail("Didn't get a Result Set as expected");
            }
            Console.WriteLine();

            //Try the Optional Clause
            SparqlParameterizedString optionalClause = new SparqlParameterizedString();
            optionalClause.Namespaces = q.NamespaceMap;
            optionalClause.QueryText = "SELECT * WHERE { OPTIONAL { ?person a foaf:Person . GRAPH ?g { [] foaf:depiction ?img ; foaf:name ?name } } }";
            SparqlQuery optionalQuery = parser.ParseFromString(optionalClause);

            Console.WriteLine("OPTIONAL Clause Only");
            Console.WriteLine(formatter.Format(optionalQuery));

            results = optionalQuery.Evaluate(dataset);
            if (results is SparqlResultSet)
            {
                SparqlResultSet rset = (SparqlResultSet)results;
                TestTools.ShowResults(rset);
            }
            else
            {
                Assert.Fail("Didn't get a Result Set as expected");
            }
            Console.WriteLine();

            //Try the full Query with a SELECT * to examine all the values
            SparqlParameterizedString fullQuery = new SparqlParameterizedString();
            fullQuery.Namespaces = q.NamespaceMap;
            fullQuery.QueryText = "SELECT * WHERE { ?person foaf:name ?name . { ?person ex:healthplan ?plan . } UNION { ?person ex:department ?dept . } OPTIONAL { ?person a foaf:Person . GRAPH ?g { [] foaf:depiction ?img ; foaf:name ?name } } }";
            SparqlQuery q2 = parser.ParseFromString(fullQuery);

            Console.WriteLine("Full Query as a SELECT *");
            Console.WriteLine(formatter.Format(q2));

            results = q2.Evaluate(dataset);
            if (results is SparqlResultSet)
            {
                SparqlResultSet rset = (SparqlResultSet)results;
                TestTools.ShowResults(rset);
            }
            else
            {
                Assert.Fail("Didn't get a Result Set as expected");
            }
            Console.WriteLine();

            //Try the full Query
            Console.WriteLine("Full Query");
            Console.WriteLine(formatter.Format(q));

            results = q.Evaluate(dataset);
            if (results is SparqlResultSet)
            {
                SparqlResultSet rset = (SparqlResultSet)results;
                TestTools.ShowResults(rset);

                SparqlRdfParser resultsParser = new SparqlRdfParser(new TurtleParser());
                SparqlResultSet expected = new SparqlResultSet();
                resultsParser.Load(expected, "result-opt-complex-4.ttl");

                Console.WriteLine();
                Console.WriteLine("Expected Results");
                TestTools.ShowResults(expected);

                Assert.AreEqual(rset, expected, "Result Sets should be equal");
            } 
            else 
            {
                Assert.Fail("Didn't get a Result Set as expected");
            }
        }
        public void SparqlResultSetEquality()
        {
            SparqlXmlParser parser = new SparqlXmlParser();
            SparqlRdfParser rdfparser = new SparqlRdfParser();
            SparqlResultSet a = new SparqlResultSet();
            SparqlResultSet b = new SparqlResultSet();

            parser.Load(a, "list-3.srx");
            parser.Load(b, "list-3.srx.out");

            a.Trim();
            b.Trim();
            Assert.IsTrue(a.Equals(b));

            a = new SparqlResultSet();
            b = new SparqlResultSet();
            parser.Load(a, "no-distinct-opt.srx");
            parser.Load(b, "no-distinct-opt.srx.out");

            a.Trim();
            b.Trim();
            Assert.IsTrue(a.Equals(b));

            a = new SparqlResultSet();
            b = new SparqlResultSet();
            rdfparser.Load(a, "result-opt-3.ttl");
            parser.Load(b, "result-opt-3.ttl.out");

            a.Trim();
            b.Trim();
            Assert.IsTrue(a.Equals(b));
        }