Example #1
0
        public void SparqlGroupByAssignmentExpression()
        {
            String query = "SELECT ?s ?sum WHERE { ?s ?p ?o } GROUP BY ?s (1 + 2 AS ?sum)";
            SparqlQueryParser parser = new SparqlQueryParser();
            SparqlQuery q = parser.ParseFromString(query);

            SparqlFormatter formatter = new SparqlFormatter();
            Console.WriteLine(formatter.Format(q));
            Console.WriteLine();

            QueryableGraph g = new QueryableGraph();
            FileLoader.Load(g, "InferenceTest.ttl");

            Object results = g.ExecuteQuery(q);
            if (results is SparqlResultSet)
            {
                SparqlResultSet rset = (SparqlResultSet)results;
                TestTools.ShowResults(rset);

                Assert.IsTrue(rset.All(r => r.HasValue("s") && r.HasValue("sum")), "All Results should have a ?s and a ?sum variable");
            }
            else
            {
                Assert.Fail("Didn't get a Result Set as expected");
            }
        }
        public void SparqlFunctionsNow()
        {
            SparqlQueryParser parser = new SparqlQueryParser();
            SparqlQuery q = parser.ParseFromFile("now01.rq");

            Console.WriteLine("ToString Output:");
            Console.WriteLine(q.ToString());
            Console.WriteLine();

            SparqlFormatter formatter = new SparqlFormatter();
            Console.WriteLine("SparqlFormatter Output:");
            Console.WriteLine(formatter.Format(q));

            TripleStore store = new TripleStore();
            SparqlResultSet results = q.Evaluate(store) as SparqlResultSet;
            if (results != null)
            {
                Assert.IsTrue(results.Result, "Result should be true");
            }
            else
            {
                Assert.Fail("Expected a non-null result");
            }
        }
Example #3
0
        static void DoQuery(Dictionary<String, String> arguments)
        {
            SparqlRemoteEndpoint endpoint;
            bool verbose = arguments.ContainsKey("verbose") || arguments.ContainsKey("v");
            if (verbose) Options.HttpDebugging = true;

            //First get the Server to which we are going to connect
            try
            {
                if (arguments.ContainsKey("server") && !arguments["server"].Equals(String.Empty))
                {
                    endpoint = new SparqlRemoteEndpoint(new Uri(arguments["server"]));
                }
                else if (arguments.ContainsKey("service") && !arguments["service"].Equals(String.Empty))
                {
                    endpoint = new SparqlRemoteEndpoint(new Uri(arguments["service"]));
                }
                else
                {
                    Console.Error.WriteLine("soh: Error: Required --server/--service argument not present");
                    Environment.Exit(-1);
                    return;
                }
            }
            catch (UriFormatException uriEx)
            {
                Console.Error.WriteLine("soh: Error: Malformed SPARQL Endpoint URI");
                Console.Error.WriteLine(uriEx.Message);
                Environment.Exit(-1);
                return;
            }
            if (verbose) Console.Error.WriteLine("soh: SPARQL Endpoint for URI " + endpoint.Uri + " created OK");

            //Then decide where to get the query to execute from
            SparqlQueryParser parser = new SparqlQueryParser();
            SparqlQuery query;
            try
            {
                if (arguments.ContainsKey("query") && !arguments["query"].Equals(String.Empty))
                {
                    query = parser.ParseFromFile(arguments["query"]);
                }
                else if (arguments.ContainsKey("file") && !arguments["file"].Equals(String.Empty))
                {
                    query = parser.ParseFromFile(arguments["file"]);
                }
                else if (arguments.ContainsKey("$1") && !arguments["$1"].Equals(String.Empty))
                {
                    query = parser.ParseFromString(arguments["$1"]);
                }
                else
                {
                    Console.Error.WriteLine("soh: Error: Required SPARQL Query not found - may be specified as --file/--query FILE or as final argument");
                    Environment.Exit(-1);
                    return;
                }
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine("soh: Error: Error Parsing SPARQL Query");
                Console.Error.WriteLine(ex.Message);
                Environment.Exit(-1);
                return;
            }

            if (verbose)
            {
                Console.Error.WriteLine("soh: Parsed Query OK");
                Console.Error.WriteLine("soh: dotNetRDF's interpretation of the Query:");
                SparqlFormatter formatter = new SparqlFormatter();
                Console.Error.WriteLine(formatter.Format(query));
                Console.Error.WriteLine("soh: Submitting Query");
            }

            try
            {
                using (HttpWebResponse response = endpoint.QueryRaw(query.ToString()))
                {
                    MimeTypeDefinition definition = MimeTypesHelper.GetDefinitions(response.ContentType).FirstOrDefault();
                    Encoding enc;
                    if (definition != null)
                    {
                        enc = definition.Encoding;
                    }
                    else if (!response.ContentEncoding.Equals(String.Empty))
                    {
                        enc = Encoding.GetEncoding(response.ContentEncoding);
                    }
                    else if (response.ContentType.Contains("charset="))
                    {
                        enc = Encoding.GetEncoding(response.ContentType.Substring(response.ContentType.IndexOf('=') + 1));
                    }
                    else
                    {
                        enc = Console.OutputEncoding;
                    }

                    if (verbose)
                    {
                        Console.Error.WriteLine("soh: Got Response from SPARQL Endpoint OK");
                        Console.Error.WriteLine("soh: Content-Type: " + response.ContentType);
                        Console.Error.WriteLine("soh: Content-Encoding: " + enc.WebName);
                    }

                    String requestedType = arguments.ContainsKey("accept") ? arguments["accept"] : null;

                    if (requestedType == null || response.ContentType.StartsWith(requestedType, StringComparison.OrdinalIgnoreCase))
                    {
                        //If no --accept (OR matches servers content type) then just return whatever the server has given us
                        using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                        {
                            using (StreamWriter writer = new StreamWriter(Console.OpenStandardOutput(), enc))
                            {
                                while (!reader.EndOfStream)
                                {
                                    writer.WriteLine(reader.ReadLine());
                                }
                                writer.Close();
                            }
                            reader.Close();
                        }
                    }
                    else
                    {
                        if (verbose) Console.Error.WriteLine("soh: Warning: Retrieved Content Type '" + response.ContentType + "' does not match your desired Content Type '" + requestedType + "' so dotNetRDF will not attempt to transcode the response into your desired format");

                        //Requested Type Doesn't match servers returned type so parse then serialize
                        MimeTypeDefinition outputDefinition;
                        try
                        {
                            ISparqlResultsReader sparqlParser = MimeTypesHelper.GetSparqlParser(response.ContentType);
                            SparqlResultSet results = new SparqlResultSet();
                            using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                            {
                                sparqlParser.Load(results, reader);
                                reader.Close();
                            }

                            outputDefinition = MimeTypesHelper.GetDefinitions(requestedType).FirstOrDefault(d => d.CanWriteSparqlResults || d.CanWriteRdf);
                            if (outputDefinition == null) throw new RdfWriterSelectionException("No MIME Type Definition for the MIME Type '" + requestedType + "' was found that can write SPARQL Results/RDF");
                            ISparqlResultsWriter writer = outputDefinition.GetSparqlResultsWriter();
                            Console.OutputEncoding = outputDefinition.Encoding;
                            writer.Save(results, new StreamWriter(Console.OpenStandardOutput(), outputDefinition.Encoding));
                        }
                        catch (RdfParserSelectionException)
                        {
                            try 
                            {
                                IRdfReader rdfParser = MimeTypesHelper.GetParser(response.ContentType);
                                Graph g = new Graph();
                                using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                                {
                                    rdfParser.Load(g, reader);
                                    reader.Close();
                                }

                                outputDefinition = MimeTypesHelper.GetDefinitions(requestedType).FirstOrDefault(d => d.CanWriteRdf);
                                if (outputDefinition == null) throw new RdfWriterSelectionException("No MIME Type Definition for the MIME Type '" + requestedType + "' was found that can write RDF");
                                IRdfWriter writer = outputDefinition.GetRdfWriter();
                                Console.OutputEncoding = outputDefinition.Encoding;
                                writer.Save(g, new StreamWriter(Console.OpenStandardOutput(), outputDefinition.Encoding));

                            }
                            catch (Exception ex)
                            {
                                //For any other exception show a warning
                                Console.Error.WriteLine("soh: Warning: You wanted results in the format '" + requestedType + "' but the server returned '" + response.ContentType + "' and dotNetRDF was unable to translate the response into your desired format.");
                                Console.Error.WriteLine(ex.Message);
                            }
                        }
                        catch (Exception ex)
                        {
                            //For any other exception show a warning
                            Console.Error.WriteLine("soh: Warning: You wanted results in the format '" + requestedType + "' but the server returned '" + response.ContentType + "' and dotNetRDF was unable to translate the response into your desired format.");
                            Console.Error.WriteLine(ex.Message);
                        }
                    }
                    

                    response.Close();
                }

                if (verbose) Console.Error.WriteLine("soh: Query Completed OK");
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine("soh: Error: Error while making the SPARQL Query");
                Console.Error.WriteLine(ex.Message);
                Environment.Exit(-1);
                return;
            }
        }
Example #4
0
        public void RunQuery(String[] args) 
        {
            //If we can't set options then we abort
            if (!this.SetOptions(args))
            {
                return;
            }

            if (this._query == null)
            {
                //Try to read query from Standard In instead
                this._query = Console.In.ReadToEnd();
                if (this._query.Equals(String.Empty))
                {
                    Console.Error.WriteLine("rdfQuery: No Query was specified");
                    return;
                }
            }

            //Parse the Query
            try
            {
                SparqlQuery q = this._parser.ParseFromString(this._query);

                //Set Timeout if necessary
                q.Timeout = this._timeout;
                q.PartialResultsOnTimeout = this._partialResults;

                //Execute the Query unless print was specified
                Object results = null;
                if (!this._print)
                {
                    switch (this._mode)
                    {
                        case RdfQueryMode.Local:
                            if (this._explain)
                            {
                                ExplainQueryProcessor processor = new ExplainQueryProcessor(this._store, this._level);
                                results = processor.ProcessQuery(q);
                            }
                            else
                            {
                                results = this._store.ExecuteQuery(q);
                            }
                            break;
                        case RdfQueryMode.Remote:
                            if (this._explain) Console.Error.WriteLine("rdfQuery: Warning: Cannot explain queries when the query is being sent to a remote endpoint");
                            this._endpoint.Timeout = Convert.ToInt32(this._timeout);
                            switch (q.QueryType)
                            {
                                case SparqlQueryType.Construct:
                                case SparqlQueryType.Describe:
                                case SparqlQueryType.DescribeAll:
                                    results = this._endpoint.QueryWithResultGraph(this._query);
                                    break;
                                default:
                                    results = this._endpoint.QueryWithResultSet(this._query);
                                    break;
                            }
                            break;
                        case RdfQueryMode.Unknown:
                        default:
                            if (this._explain)
                            {
                                ExplainQueryProcessor processor = new ExplainQueryProcessor(new TripleStore(), this._level);
                                processor.ProcessQuery(q);
                            }
                            else
                            {
                                Console.Error.WriteLine("rdfQuery: There were no inputs or a remote endpoint specified in the arguments so no query can be executed");
                            }
                            return;
                    }
                }

                //Select the Output Stream
                StreamWriter output;
                if (this._output == null)
                {
                    output = new StreamWriter(Console.OpenStandardOutput());
                }
                else
                {
                    output = new StreamWriter(this._output);
                }

                if (!this._print)
                {
                    //Output the Results
                    if (results is SparqlResultSet)
                    {
                        this._resultsWriter.Save((SparqlResultSet)results, output);
                    }
                    else if (results is IGraph)
                    {
                        this._graphWriter.Save((IGraph)results, output);
                    }
                    else
                    {
                        Console.Error.WriteLine("rdfQuery: The Query resulted in an unknown result");
                    }
                }
                else
                {
                    //If Printing Print the Query then the Algebra
                    SparqlFormatter formatter = new SparqlFormatter(q.NamespaceMap);
                    output.WriteLine("Query");
                    output.WriteLine(formatter.Format(q));
                    output.WriteLine();
                    output.WriteLine("Algebra");
                    output.WriteLine(q.ToAlgebra().ToString());
                    output.Flush();
                    output.Close();
                }
            }
            catch (RdfQueryTimeoutException timeout)
            {
                Console.Error.WriteLine("rdfQuery: Query Timeout: " + timeout.Message);
                if (this._debug) this.DebugErrors(timeout);
                return;
            }
            catch (RdfQueryException queryEx)
            {
                Console.Error.WriteLine("rdfQuery: Query Error: " + queryEx.Message);
                if (this._debug) this.DebugErrors(queryEx);
                return;
            }
            catch (RdfParseException parseEx)
            {
                Console.Error.WriteLine("rdfQuery: Parser Error: " + parseEx.Message);
                if (this._debug) this.DebugErrors(parseEx);
                return;
            }
            catch (RdfException rdfEx)
            {
                Console.Error.WriteLine("rdfQuery: RDF Error: " + rdfEx.Message);
                if (this._debug) this.DebugErrors(rdfEx);
                return;
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine("rdfQuery: Error: " + ex.Message);
                if (this._debug) this.DebugErrors(ex);
                return;
            }
        }
        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 ProcessSyntaxTest(SparqlQueryParser parser, String queryFile, bool shouldParse)
        {
            if (queryFile.StartsWith("file:///")) queryFile = queryFile.Substring(8);

            bool error = false;
            try
            {
                Console.WriteLine("# Processing Syntax Test " + Path.GetFileName(queryFile));
                Console.Write("# Result Expected = ");
                if (shouldParse)
                {
                    Console.WriteLine("Parses OK");
                }
                else
                {
                    Console.WriteLine("Parsing Fails");
                }

                SparqlQuery q = parser.ParseFromFile(queryFile);

                Console.WriteLine(q.ToString());
                Console.WriteLine();

                Console.WriteLine("Formatted with SparqlFormatter");
                SparqlFormatter formatter = new SparqlFormatter(q.NamespaceMap);
                Console.WriteLine(formatter.Format(q));
                Console.WriteLine();
            }
            catch (RdfParseException parseEx)
            {
                this.ReportError("Parser Error", parseEx);
                error = true;
            }
            catch (Exception ex)
            {
                this.ReportError("Other Error", ex);
                error = true;
            }
            finally
            {
                Console.Write("# Result = ");
                if (error)
                {
                    if (shouldParse)
                    {
                        Console.WriteLine(" Parsing Failed when should have parsed (Test Failed)");
                        testsFailed++;
                        testsSyntaxFailed++;
                    }
                    else
                    {
                        Console.WriteLine(" Parsing Failed as expected (Test Passed)");
                        testsPassed++;
                        testsSyntaxPassed++;
                    }
                }
                else
                {
                    if (shouldParse)
                    {
                        Console.WriteLine(" Parsed OK as expected (Test Passed)");
                        testsPassed++;
                        testsSyntaxPassed++;
                    }
                    else
                    {
                        Console.WriteLine(" Parsed OK when should have failed (Test Failed)");
                        testsFailed++;
                        testsSyntaxFailed++;
                    }
                }
                Console.WriteLine(new String('-', 150));
            }
        }
        private void ProcessSyntaxTest(SparqlQueryParser queryParser, SparqlUpdateParser updateParser, String inputFile, bool shouldParse)
        {
            if (inputFile.StartsWith("file:///")) inputFile = inputFile.Substring(8);

            bool error = false;
            bool skipFinally = false;
            try
            {
                Console.WriteLine("# Processing Syntax Test " + Path.GetFileName(inputFile));
                Console.Write("# Result Expected = ");
                if (shouldParse)
                {
                    Console.WriteLine("Parses OK");
                }
                else
                {
                    Console.WriteLine("Parsing Fails");
                }

                if (evaluationTestOverride.Any(x => inputFile.EndsWith(x)))
                {
                    Console.WriteLine();
                    Console.WriteLine("# Test Result = Manually overridden to Pass (Test Passed)");
                    skipFinally = true;
                    testsPassed++;
                    testsSyntaxPassed++;
                    return;
                }

                if (inputFile.EndsWith(".rq"))
                {
                    SparqlQuery q = queryParser.ParseFromFile(inputFile);

                    Console.WriteLine("Formatted with SparqlFormatter");
                    SparqlFormatter formatter = new SparqlFormatter(q.NamespaceMap);
                    Console.WriteLine(formatter.Format(q));
                    Console.WriteLine();
                }
                else if (inputFile.EndsWith(".ru"))
                {
                    SparqlUpdateCommandSet cmds = updateParser.ParseFromFile(inputFile);

                    Console.WriteLine(cmds.ToString());
                    Console.WriteLine();
                }
                else
                {
                    Console.WriteLine("# Test Result - Unknown Input File for Syntax Test (Test Indeterminate)");
                    skipFinally = true;
                    testsIndeterminate++;
                    testsSyntaxIndeterminate++;
                    return;
                }
            }
            catch (RdfParseException parseEx)
            {
                this.ReportError("Parser Error", parseEx);
                error = true;
            }
            catch (Exception ex)
            {
                this.ReportError("Other Error", ex);
                error = true;
            }
            finally
            {
                if (!skipFinally)
                {
                    Console.Write("# Test Result = ");
                    if (error)
                    {
                        if (shouldParse)
                        {
                            Console.WriteLine(" Parsing Failed when should have parsed (Test Failed)");
                            testsFailed++;
                            testsSyntaxFailed++;
                        }
                        else
                        {
                            Console.WriteLine(" Parsing Failed as expected (Test Passed)");
                            testsPassed++;
                            testsSyntaxPassed++;
                        }
                    }
                    else
                    {
                        if (shouldParse)
                        {
                            Console.WriteLine(" Parsed OK as expected (Test Passed)");
                            testsPassed++;
                            testsSyntaxPassed++;
                        }
                        else
                        {
                            Console.WriteLine(" Parsed OK when should have failed (Test Failed)");
                            testsFailed++;
                            testsSyntaxFailed++;
                        }
                    }
                }
                Console.WriteLine(new String('-', 150));
            }
        }
        private void TestProductTimeout(IGraph data, String query, bool useGlobal, int expectedResults)
        {
            Console.WriteLine("Maximum Expected Results: " + expectedResults);
            Console.WriteLine("Initial Global Timeout: " + Options.QueryExecutionTimeout);
            Console.WriteLine();

            long globalOrig = Options.QueryExecutionTimeout;
            try
            {
                if (useGlobal)
                {
                    Console.WriteLine("Global Timeout setting in use");
                }
                else
                {
                    Console.WriteLine("Per Query Timeout setting in use");
                }
                Console.WriteLine();

                TripleStore store = new TripleStore();
                store.Add(data);

                SparqlQuery q = this._parser.ParseFromString(query);
                LeviathanQueryProcessor processor = new LeviathanQueryProcessor(store);

                SparqlFormatter formatter = new SparqlFormatter();
                Console.WriteLine("Query:");
                Console.WriteLine(formatter.Format(q));

                //Evaluate for each Timeout
                foreach (long t in this._timeouts)
                {
                    //Set the Timeout and ask for Partial Results
                    if (useGlobal)
                    {
                        Options.QueryExecutionTimeout = t;
                    }
                    else
                    {
                        q.Timeout = t;
                    }
                    q.PartialResultsOnTimeout = true;

                    //Check that the reported Timeout matches the expected
                    SparqlEvaluationContext context = new SparqlEvaluationContext(q, null);
                    long expected;
                    if (useGlobal)
                    {
                        expected = t;
                    }
                    else
                    {
                        if (Options.QueryExecutionTimeout > 0 && t <= Options.QueryExecutionTimeout)
                        {
                            expected = t;
                        }
                        else if (Options.QueryExecutionTimeout == 0)
                        {
                            expected = t;
                        }
                        else
                        {
                            expected = Options.QueryExecutionTimeout;
                        }
                    }
                    Assert.AreEqual(expected, context.QueryTimeout, "Reported Timeout not as expected");

                    //Run the Query
                    Object results = processor.ProcessQuery(q);
                    if (results is SparqlResultSet)
                    {
                        SparqlResultSet rset = (SparqlResultSet)results;

                        Console.WriteLine("Requested Timeout: " + t + " - Actual Timeout: " + expected + "ms - Results: " + rset.Count + " - Query Time: " + q.QueryTime + "ms");
                        Assert.IsTrue(rset.Count <= expectedResults, "Results should be <= expected");
                    }
                    else
                    {
                        Assert.Fail("Did not get a Result Set as expected");
                    }
                }
            }
            finally
            {
                Options.QueryExecutionTimeout = globalOrig;
            }
        }
        public void SparqlParsingConstructShortForm()
        {
            List<String> valid = new List<string>()
            {
                "CONSTRUCT WHERE {?s ?p ?o }",
                "CONSTRUCT WHERE {?s a ?type }",
            };

            List<String> invalid = new List<string>()
            {
                "CONSTRUCT {?s ?p ?o}",
                "CONSTRUCT WHERE { ?s ?p ?o . FILTER(ISLITERAL(?o)) }",
                "CONSTRUCT WHERE { GRAPH ?g { ?s ?p ?o } }",
                "CONSTRUCT WHERE { ?s ?p ?o . OPTIONAL {?s a ?type}}",
                "CONSTRUCT WHERE { ?s a ?type . BIND (<http://example.org> AS ?thing) }",
                "CONSTRUCT WHERE { {SELECT * WHERE { ?s ?p ?o } } }"
            };

            SparqlQueryParser parser = new SparqlQueryParser();
            SparqlFormatter formatter = new SparqlFormatter();
            foreach (String v in valid)
            {
                Console.WriteLine("Valid Input: " + v);
                try
                {
                    SparqlQuery q = parser.ParseFromString(v);
                    Console.WriteLine(formatter.Format(q));
                }
                catch (RdfParseException parseEx)
                {
                    Console.WriteLine("Failed to parse valid Query");
                    TestTools.ReportError("Parsing Error", parseEx, true);
                }
                Console.WriteLine();
            }

            foreach (String iv in invalid)
            {
                Console.WriteLine("Invalid Input: " + iv);
                try
                {
                    SparqlQuery q = parser.ParseFromString(iv);
                    Assert.Fail("Should have thrown a Parsing Error");
                }
                catch (RdfParseException parseEx)
                {
                    Console.WriteLine("Errored as expected");
                    TestTools.ReportError("Parsing Error", parseEx, false);
                }
                Console.WriteLine();
            }
        }
        public void SparqlParsingDescribeHangingWhere()
        {
            List<String> valid = new List<string>()
            {
                "DESCRIBE ?s WHERE { ?s a ?type }",
                "DESCRIBE <http://example.org/>",
                "PREFIX ex: <http://example.org/> DESCRIBE ex:"
            };

            List<String> invalid = new List<string>()
            {
                "DESCRIBE ?s WHERE"
            };

            SparqlQueryParser parser = new SparqlQueryParser();
            SparqlFormatter formatter = new SparqlFormatter();
            foreach (String v in valid)
            {
                try
                {
                    SparqlQuery q = parser.ParseFromString(v);
                    Console.WriteLine(formatter.Format(q));
                    Console.WriteLine();
                }
                catch (RdfParseException parseEx)
                {
                    Console.WriteLine("Failed to parse valid Query");
                    TestTools.ReportError("Parsing Error", parseEx, true);
                }
            }

            foreach (String iv in invalid)
            {
                try
                {
                    SparqlQuery q = parser.ParseFromString(iv);
                    Assert.Fail("Should have thrown a Parsing Error");
                }
                catch (RdfParseException parseEx)
                {
                    Console.WriteLine("Errored as expected");
                    TestTools.ReportError("Parsing Error", parseEx, false);
                }
            }
        }
        public void SparqlEvaluationGraphUnion()
        {
            SparqlQueryParser parser = new SparqlQueryParser();
            SparqlQuery q = parser.ParseFromFile("graph-11.rq");
            q.BaseUri = new Uri("file:///" + Environment.CurrentDirectory.Replace('\\', '/') + "/");

            TripleStore store = new TripleStore();
            Graph g = new Graph();

            FileLoader.Load(g, "data-g1.ttl");
            store.Add(g);
            Graph h = new Graph();
            FileLoader.Load(h, "data-g2.ttl");
            store.Add(h);
            Graph i = new Graph();
            FileLoader.Load(i, "data-g3.ttl");
            store.Add(i);
            Graph j = new Graph();
            FileLoader.Load(j, "data-g4.ttl");
            store.Add(j);

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

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

            //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, "graph-11.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 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");
            }
        }
Example #13
0
        public void SparqlGroupByAssignmentExpression3()
        {
            String query = "SELECT ?lang (SAMPLE(?o) AS ?example) WHERE { ?s ?p ?o . FILTER(ISLITERAL(?o)) } GROUP BY (LANG(?o) AS ?lang) HAVING LANGMATCHES(?lang, \"*\")";
            SparqlQueryParser parser = new SparqlQueryParser();
            SparqlQuery q = parser.ParseFromString(query);

            SparqlFormatter formatter = new SparqlFormatter();
            Console.WriteLine(formatter.Format(q));
            Console.WriteLine();

            QueryableGraph g = new QueryableGraph();
            UriLoader.Load(g, new Uri("http://dbpedia.org/resource/Southampton"));

            Object results = g.ExecuteQuery(q);
            if (results is SparqlResultSet)
            {
                SparqlResultSet rset = (SparqlResultSet)results;
                TestTools.ShowResults(rset);

                Assert.IsTrue(rset.All(r => r.HasValue("lang") && r.HasValue("example")), "All Results should have a ?lang and a ?example variable");
            }
            else
            {
                Assert.Fail("Didn't get a Result Set as expected");
            }
        }
Example #14
0
 private void LogStartQuery(SparqlQuery q)
 {
     SparqlFormatter formatter = new SparqlFormatter(q.NamespaceMap);
     this.Log("QUERY START", formatter.Format(q));
 }