private int ProcessUpdateEvaluationTest(IGraph manifest, INode testNode)
        {
            try
            {
                IUriNode utData = manifest.CreateUriNode("ut:data");
                IUriNode utGraph = manifest.CreateUriNode("ut:graph");
                IUriNode utGraphData = manifest.CreateUriNode("ut:graphData");
                IUriNode rdfsLabel = manifest.CreateUriNode("rdfs:label");

                //Get the test name and comment
                String name = manifest.GetTriplesWithSubjectPredicate(testNode, manifest.CreateUriNode("mf:name")).Select(t => t.Object).First().ToString();
                String comment = manifest.GetTriplesWithSubjectPredicate(testNode, manifest.CreateUriNode("rdfs:comment")).Select(t => t.Object).First().ToString();

                //Get the test action and file
                INode actionNode = manifest.GetTriplesWithSubjectPredicate(testNode, manifest.CreateUriNode("mf:action")).Select(t => t.Object).First();
                String updateFile = manifest.GetTriplesWithSubjectPredicate(actionNode, manifest.CreateUriNode("ut:request")).Select(t => t.Object).First().ToString();

                Console.WriteLine("# Processing Update Evaluation Test " + updateFile);
                Console.WriteLine(name);
                Console.WriteLine(comment);
                Console.WriteLine();

                if (evaluationTestOverride.Any(x => updateFile.EndsWith(x)))
                {
                    Console.WriteLine();
                    Console.WriteLine("# Test Result = Manually overridden to Pass (Test Passed)");
                    testsPassed++;
                    testsEvaluationPassed++;
                    return 1;
                }

                //Parse the Update
                SparqlUpdateParser parser = new SparqlUpdateParser();
                SparqlUpdateCommandSet cmds;
                try
                {
                    if (updateFile.StartsWith("file:///"))
                    {
                        updateFile = updateFile.Substring(8);
                    }
                    cmds = parser.ParseFromFile(updateFile);

                    Console.WriteLine("Update Commands:");
                    Console.WriteLine(cmds.ToString());
                }
                catch (Exception ex)
                {
                    this.ReportError("Error Parsing Update Commands", ex);

                    Console.WriteLine("# Test Result - Update Command failed to pass (Test Failed)");
                    testsEvaluationFailed++;
                    testsFailed++;
                    return -1;
                }

                //Build the Initial Dataset
                InMemoryDataset dataset = new InMemoryDataset(new TripleStore());
                try
                {
                    foreach (Triple t in manifest.GetTriplesWithSubjectPredicate(actionNode, utData))
                    {
                        Console.WriteLine("Uses Default Graph File " + t.Object.ToString());
                        Graph g = new Graph();
                        UriLoader.Load(g, ((IUriNode)t.Object).Uri);
                        g.BaseUri = null;
                        dataset.AddGraph(g);
                    }
                    foreach (Triple t in manifest.GetTriplesWithSubjectPredicate(actionNode, utGraphData))
                    {
                        Graph g = new Graph();
                        INode dataNode = manifest.GetTriplesWithSubjectPredicate(t.Object, utData).Concat(manifest.GetTriplesWithSubjectPredicate(t.Object, utGraph)).Select(x => x.Object).FirstOrDefault();
                        UriLoader.Load(g, ((IUriNode)dataNode).Uri);
                        INode nameNode = manifest.GetTriplesWithSubjectPredicate(t.Object, rdfsLabel).Select(x => x.Object).FirstOrDefault();
                        g.BaseUri = new Uri(nameNode.ToString());
                        Console.WriteLine("Uses Named Graph File " + dataNode.ToString() + " named as " + nameNode.ToString());
                        dataset.AddGraph(g);
                    }
                }
                catch (Exception ex)
                {
                    this.ReportError("Error Building Initial Dataset", ex);
                    Console.WriteLine("# Test Result - Unable to build Initial Dataset (Test Indeterminate)");
                    testsEvaluationIndeterminate++;
                    testsIndeterminate++;
                    return 0;
                }
                Console.WriteLine();

                //Try running the Update
                try
                {
                    LeviathanUpdateProcessor processor = new LeviathanUpdateProcessor(dataset);

                    //Since all Tests assume that the WHERE is limited to the unnamed default graph unless specified
                    //then must set this to be the Active Graph for the dataset
                    dataset.SetDefaultGraph(dataset[null]);

                    //Try the Update
                    processor.ProcessCommandSet(cmds);

                    dataset.ResetDefaultGraph();
                }
                catch (SparqlUpdateException updateEx)
                {
                    //TODO: Some Update tests might be to test cases where a failure should occur
                    this.ReportError("Unexpected Error while performing Update", updateEx);
                    Console.WriteLine("# Test Result - Update Failed (Test Failed)");
                    testsEvaluationFailed++;
                    testsFailed++;
                    return -1;
                }
                catch (Exception ex)
                {
                    this.ReportError("Unexpected Error while performing Update", ex);
                    Console.WriteLine("# Test Result - Update Failed (Test Failed)");
                    testsEvaluationFailed++;
                    testsFailed++;
                    return -1;
                }

                //Build the Result Dataset
                INode resultNode = manifest.GetTriplesWithSubjectPredicate(testNode, manifest.CreateUriNode("mf:result")).Select(t => t.Object).First();
                InMemoryDataset resultDataset = new InMemoryDataset(new TripleStore());
                try
                {
                    foreach (Triple t in manifest.GetTriplesWithSubjectPredicate(resultNode, utData))
                    {
                        Console.WriteLine("Uses Result Default Graph File " + t.Object.ToString());
                        Graph g = new Graph();
                        UriLoader.Load(g, ((IUriNode)t.Object).Uri);
                        g.BaseUri = null;
                        resultDataset.AddGraph(g);
                    }
                    foreach (Triple t in manifest.GetTriplesWithSubjectPredicate(resultNode, utGraphData))
                    {
                        Graph g = new Graph();
                        INode dataNode = manifest.GetTriplesWithSubjectPredicate(t.Object, utData).Concat(manifest.GetTriplesWithSubjectPredicate(t.Object, utGraph)).Select(x => x.Object).FirstOrDefault();
                        UriLoader.Load(g, ((IUriNode)dataNode).Uri);
                        INode nameNode = manifest.GetTriplesWithSubjectPredicate(t.Object, rdfsLabel).Select(x => x.Object).FirstOrDefault();
                        g.BaseUri = new Uri(nameNode.ToString());
                        Console.WriteLine("Uses Result Named Graph File " + dataNode.ToString() + " named as " + nameNode.ToString());
                        resultDataset.AddGraph(g);
                    }
                    
                    //Do this just to ensure that if the Result Dataset doesn't have a default unnamed graph it will
                    //have one
                    LeviathanUpdateProcessor processor = new LeviathanUpdateProcessor(resultDataset);
                }
                catch (Exception ex)
                {
                    this.ReportError("Error Building Result Dataset", ex);
                    Console.WriteLine("# Test Result - Unable to build Result Dataset (Test Indeterminate)");
                    testsEvaluationIndeterminate++;
                    testsIndeterminate++;
                    return 0;
                }
                Console.WriteLine();

                //Now compare the two datasets to see if the tests passes
                foreach (Uri u in resultDataset.GraphUris)
                {
                    if (dataset.HasGraph(u))
                    {
                        if (!resultDataset[u].Equals(dataset[u]))
                        {
                            this.ShowGraphs(dataset[u], resultDataset[u]);

                            Console.WriteLine("# Test Result - Expected Result Dataset Graph '" + this.ToSafeString(u) + "' is different from the Graph with that name in the Updated Dataset (Test Failed)");
                            testsEvaluationFailed++;
                            testsFailed++;
                            return -1;
                        }
                    }
                    else
                    {
                        Console.WriteLine("# Test Result - Expected Result Dataset has Graph '" + this.ToSafeString(u) + "' which is not present in the Updated Dataset (Test Failed)");
                        testsEvaluationFailed++;
                        testsFailed++;
                        return -1;
                    }
                }
                foreach (Uri u in dataset.GraphUris)
                {
                    if (!resultDataset.HasGraph(u))
                    {
                        Console.WriteLine("# Test Result - Updated Dataset has additional Graph '" + this.ToSafeString(u) + "' which is not present in the Expected Result Dataset (Test Failed)");
                    }
                }

                Console.WriteLine("# Test Result - Updated Dataset matches Expected Result Dataset (Test Passed)");
                testsEvaluationPassed++;
                testsPassed++;
                return 1;
            }
            catch (Exception ex)
            {
                this.ReportError("Unexpected Error", ex);
                Console.WriteLine("# Test Result - Unexpected Error (Test Failed)");
                testsEvaluationFailed++;
                testsFailed++;
                return -1;
            }
        }
Esempio n. 2
0
        static void DoUpdate(Dictionary<String, String> arguments)
        {
            SparqlRemoteUpdateEndpoint 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 SparqlRemoteUpdateEndpoint(new Uri(arguments["server"]));
                }
                else if (arguments.ContainsKey("service") && !arguments["service"].Equals(String.Empty))
                {
                    endpoint = new SparqlRemoteUpdateEndpoint(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 Update Endpoint URI");
                Console.Error.WriteLine(uriEx.Message);
                Environment.Exit(-1);
                return;
            }
            if (verbose) Console.Error.WriteLine("soh: SPARQL Update Endpoint for URI " + endpoint.Uri + " created OK");

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

            if (verbose)
            {
                Console.Error.WriteLine("soh: Parsed Update OK");
                Console.Error.WriteLine("soh: dotNetRDF's interpretation of the Update:");
                Console.Error.WriteLine(cmds.ToString());
                Console.Error.WriteLine("soh: Submitting Update");
            }

            try
            {
                endpoint.Update(cmds.ToString());
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine("soh: Error: Error while making the SPARQL Update");
                Console.Error.WriteLine(ex.Message);
                Environment.Exit(-1);
                return;
            }
        }
        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));
            }
        }