Exemplo n.º 1
0
        public void SparqlDateTimeGreaterThan()
        {
            IGraph g = new Graph();

            g.LoadFromFile(@"resources\sparql\data-3.ttl");
            Assert.IsFalse(g.IsEmpty);

            SparqlQuery     q      = this._parser.ParseFromFile(@"resources\sparql\date-3.rq");
            SparqlResultSet actual = g.ExecuteQuery(q) as SparqlResultSet;

            Assert.IsNotNull(actual);

            Console.WriteLine("Actual Results:");
            TestTools.ShowResults(actual);

            SparqlResultSet expected = new SparqlResultSet();

            this._resultsParser.Load(expected, @"resources\sparql\date-3-result.srx");

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

            Assert.IsTrue(expected.Equals(actual));
        }
Exemplo n.º 2
0
        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));
        }
Exemplo n.º 3
0
        private void RunTest(String name, String comment, String file, String resultFile, bool shouldParse)
        {
            Console.WriteLine("### Running Test #" + this._count);
            if (name != null)
            {
                Console.WriteLine("Test Name " + name);
            }
            if (comment != null)
            {
                Console.WriteLine(comment);
            }
            Console.WriteLine();
            Console.WriteLine("Input File is " + file);
            if (resultFile != null)
            {
                Console.WriteLine("Expected Output File is " + resultFile);
            }

            //Check File Exists
            if (!File.Exists(file))
            {
                Console.WriteLine("Input File not found");
                this._fail++;
                return;
            }

            try
            {
                SparqlResultSet actual = new SparqlResultSet();
                this._parser.Load(actual, file);

                if (shouldParse)
                {
                    Console.WriteLine("Parsed input in OK");

                    //Validate if necessary
                    if (this.CheckResults)
                    {
                        if (!File.Exists(resultFile))
                        {
                            Console.WriteLine("Expected Output File not found");
                            this._fail++;
                        }
                        else
                        {
                            SparqlResultSet expected = new SparqlResultSet();
                            try
                            {
                                this._resultsParser.Load(expected, resultFile);

                                if (expected.Equals(actual))
                                {
                                    Console.WriteLine("Parsed Results matches Expected Results (Test Passed)");
                                    this._pass++;
                                }
                                else
                                {
                                    Console.WriteLine("Parsed Results did not match Expected Graph (Test Failed)");
                                    this._fail++;
                                    Console.WriteLine("Expected:");
                                    TestTools.ShowResults(expected);
                                    Console.WriteLine();
                                    Console.WriteLine("Actual:");
                                    TestTools.ShowResults(actual);
                                }
                            }
                            catch (RdfParseException)
                            {
                                Console.WriteLine("Expected Output File could not be parsed (Test Indeterminate)");
                                this._indeterminate++;
                            }
                        }
                    }
                    else
                    {
                        Console.WriteLine("No Validation Required (Test Passed)");
                        this._pass++;
                    }
                }
                else
                {
                    Console.WriteLine("Parsed when failure was expected (Test Failed)");
                    this._fail++;
                }
            }
            catch (RdfParseException parseEx)
            {
                if (shouldParse)
                {
                    Console.WriteLine("Failed when was expected to parse (Test Failed)");
                    TestTools.ReportError("Parse Error", parseEx);
                    this._fail++;
                }
                else
                {
                    Console.WriteLine("Failed to parse as expected (Test Passed)");
                    this._pass++;
                }
            }
            Console.WriteLine("### End Test #" + this._count);
            Console.WriteLine();
            this._count++;
        }
        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
            //ISparqlDataset dataset = new InMemoryQuadDataset(store);
            ISparqlDataset dataset = new InMemoryDataset(store);

            if (!query.DefaultGraphs.Any())
            {
                query.AddDefaultGraph(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
            {
                LeviathanQueryProcessor processor = new LeviathanQueryProcessor(dataset);
                results = processor.ProcessQuery(query);
            }
            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);
            }
        }