コード例 #1
0
        public static void Main(string[] args)
        {
            StreamWriter output = new StreamWriter("RemoteSPARQLTestSuite.txt",false,Encoding.UTF8);
            String[] skipTests = { };

            Console.SetOut(output);

            output.WriteLine("### Running Remote SPARQL Endpoint Tests");
            output.WriteLine();
            output.WriteLine("Accept Header for SPARQL SELECT and ASK: " + MimeTypesHelper.HttpSparqlAcceptHeader);
            output.WriteLine("Accept Header for SPARQL DESCRIBE and CONSTRUCT: " + MimeTypesHelper.HttpAcceptHeader);
            output.WriteLine();

            //Options.HttpDebugging = true;
            try
            {
                SparqlRemoteEndpoint dbpedia = new SparqlRemoteEndpoint(new Uri("http://dbpedia.org/sparql"), "http://dbpedia.org");
                SparqlResultSet results = new SparqlResultSet();

                String[] queries = new String[3];
                queries[0] = "select distinct ?Concept where {[] a ?Concept} limit 10";
                queries[1] = "select distinct ?Concept where {[] a ?Concept} limit 10 offset 5";
                queries[2] = "prefix skos: <http://www.w3.org/2004/02/skos/core#> select distinct ?City where {?City skos:subject <http://dbpedia.org/resource/Category:Cities_in_England>}";

                foreach (String query in queries)
                {
                    output.WriteLine("## Making a SPARQL SELECT Query");
                    output.WriteLine("# Query");
                    output.WriteLine();
                    output.WriteLine(query);
                    output.WriteLine();
                    output.WriteLine("# Results");

                    results = dbpedia.QueryWithResultSet(query);
                    foreach (SparqlResult result in results)
                    {
                        output.WriteLine(result.ToString());
                    }
                    output.WriteLine();
                }

                //Options.HttpFullDebugging = true;
                String gquery = "DESCRIBE <http://dbpedia.org/resource/Southampton>";
                output.WriteLine("## Making a SPARQL DESCRIBE Query");
                output.WriteLine("# Query");
                output.WriteLine();
                output.WriteLine(gquery);
                output.WriteLine();
                output.WriteLine("# Results");
                IGraph g = dbpedia.QueryWithResultGraph(gquery);

                foreach (Triple t in g.Triples)
                {
                    output.WriteLine(t.ToString());
                }
            }
            catch (XmlException xmlEx)
            {
                reportError(output, "XML Exception", xmlEx);
            }
            catch (IOException ioEx)
            {
                reportError(output, "IO Exception", ioEx);
            }
            catch (RdfParseException parseEx)
            {
                reportError(output, "Parsing Exception", parseEx);
            }
            catch (RdfException rdfEx)
            {
                reportError(output, "RDF Exception", rdfEx);
            }
            catch (Exception ex)
            {
                reportError(output, "Other Exception", ex);
            }
            //Options.HttpDebugging = false;

            output.WriteLine();
            output.WriteLine("### Running Federated SPARQL Test");

            try
            {
                SparqlRemoteEndpoint dbpedia = new SparqlRemoteEndpoint(new Uri("http://dbpedia.org/sparql"), "http://dbpedia.org");
                SparqlRemoteEndpoint bbcProgs = new SparqlRemoteEndpoint(new Uri("http://dbpedia.org/sparql"), "http://www.bbc.co.uk/programmes");
                SparqlRemoteEndpoint books = new SparqlRemoteEndpoint(new Uri("http://sparql.org/books"));

                String fedQuery = "SELECT * WHERE {?s a ?type} LIMIT 10";

                FederatedSparqlRemoteEndpoint fedEndpoint = new FederatedSparqlRemoteEndpoint(new SparqlRemoteEndpoint[] { dbpedia, bbcProgs/*, books*/ });
                fedEndpoint.MaxSimultaneousRequests = 1;
                SparqlResultSet results = fedEndpoint.QueryWithResultSet(fedQuery);
                foreach (SparqlResult result in results)
                {
                    output.WriteLine(result.ToString());
                }
                output.WriteLine();
            }
            catch (XmlException xmlEx)
            {
                reportError(output, "XML Exception", xmlEx);
            }
            catch (IOException ioEx)
            {
                reportError(output, "IO Exception", ioEx);
            }
            catch (RdfParseException parseEx)
            {
                reportError(output, "Parsing Exception", parseEx);
            }
            catch (RdfException rdfEx)
            {
                reportError(output, "RDF Exception", rdfEx);
            }
            catch (Exception ex)
            {
                reportError(output, "Other Exception", ex);
            }

            output.WriteLine();
            output.WriteLine("### Running Result Set Parser Tests");

            try
            {
                int testsPassed = 0;
                int testsFailed = 0;
                String[] files = Directory.GetFiles("sparql_tests");
                bool passed, passDesired;
                SparqlResultSet results = new SparqlResultSet();
                SparqlXmlParser parser = new SparqlXmlParser();

                foreach (String file in files)
                {
                    if (skipTests.Contains(Path.GetFileName(file)))
                    {
                        output.WriteLine("## Skipping Test of File " + Path.GetFileName(file));
                        output.WriteLine();
                        continue;
                    }

                    if (Path.GetExtension(file) != ".srx")
                    {
                        continue;
                    }

                    Debug.WriteLine("Testing File " + Path.GetFileName(file));
                    output.WriteLine("## Testing File " + Path.GetFileName(file));
                    output.WriteLine("# Test Started at " + DateTime.Now.ToString(TestSuite.TestSuiteTimeFormat));

                    passed = false;
                    passDesired = true;

                    try
                    {
                        if (Path.GetFileNameWithoutExtension(file).StartsWith("bad"))
                        {
                            passDesired = false;
                            output.WriteLine("# Desired Result = Parsing Failed");
                        }
                        else
                        {
                            output.WriteLine("# Desired Result = Parses OK");
                        }

                        results = new SparqlResultSet();
                        parser.Load(results, file);

                        passed = true;
                        output.WriteLine("Parsed OK");
                    }
                    catch (XmlException xmlEx)
                    {
                        reportError(output, "XML Exception", xmlEx);
                    }
                    catch (IOException ioEx)
                    {
                        reportError(output, "IO Exception", ioEx);
                    }
                    catch (RdfParseException parseEx)
                    {
                        reportError(output, "Parsing Exception", parseEx);
                    }
                    catch (RdfException rdfEx)
                    {
                        reportError(output, "RDF Exception", rdfEx);
                    }
                    catch (Exception ex)
                    {
                        reportError(output, "Other Exception", ex);
                    }
                    finally
                    {
                        if (passed && passDesired)
                        {
                            //Passed and we wanted to Pass
                            testsPassed++;
                            output.WriteLine("# Result = Test Passed");
                        }
                        else if (!passed && passDesired)
                        {
                            //Failed when we should have Passed
                            testsFailed++;
                            output.WriteLine("# Result = Test Failed");
                        }
                        else if (passed && !passDesired)
                        {
                            //Passed when we should have Failed
                            testsFailed++;
                            output.WriteLine("# Result = Test Failed");
                        }
                        else
                        {
                            //Failed and we wanted to Fail
                            testsPassed++;
                            output.WriteLine("# Result = Test Passed");
                        }

                        output.WriteLine("# Results Generated = " + results.Count);
                        output.WriteLine("# Query Result was " + results.Result);
                        output.WriteLine("# Test Ended at " + DateTime.Now.ToString(TestSuite.TestSuiteTimeFormat));
                    }

                    output.WriteLine();
                }

                output.WriteLine(testsPassed + " Tests Passed");
                output.WriteLine(testsFailed + " Tests Failed");

            }
            catch (Exception ex)
            {
                reportError(output, "Other Exception", ex);
            }

            Console.SetOut(Console.Out);
            Console.WriteLine("Done");
            Debug.WriteLine("Finished");

            output.Close();

        }
コード例 #2
0
        /// <summary>
        /// Tries to load a SPARQL Endpoint based on information from the Configuration Graph
        /// </summary>
        /// <param name="g">Configuration Graph</param>
        /// <param name="objNode">Object Node</param>
        /// <param name="targetType">Target Type</param>
        /// <param name="obj">Output Object</param>
        /// <returns></returns>
        public bool TryLoadObject(IGraph g, INode objNode, Type targetType, out object obj)
        {
            BaseEndpoint endpoint = null;
            obj = null;

            switch (targetType.FullName)
            {
                case Endpoint:
                    String endpointUri = ConfigurationLoader.GetConfigurationValue(g, objNode, ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyEndpointUri));
                    if (endpointUri == null) return false;

                    //Get Default/Named Graphs if specified
                    IEnumerable<String> defaultGraphs = from n in ConfigurationLoader.GetConfigurationData(g, objNode, ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyDefaultGraphUri))
                                                        select n.ToString();
                    IEnumerable<String> namedGraphs = from n in ConfigurationLoader.GetConfigurationData(g, objNode, ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyNamedGraphUri))
                                                      select n.ToString();
                    endpoint = new SparqlRemoteEndpoint(new Uri(endpointUri), defaultGraphs, namedGraphs);

                    break;

#if !SILVERLIGHT
                case FederatedEndpoint:
                    IEnumerable<INode> endpoints = ConfigurationLoader.GetConfigurationData(g, objNode, ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyEndpoint));
                    foreach (INode e in endpoints)
                    {
                        Object temp = ConfigurationLoader.LoadObject(g, e);
                        if (temp is SparqlRemoteEndpoint)
                        {
                            if (endpoint == null)
                            {
                                endpoint = new FederatedSparqlRemoteEndpoint((SparqlRemoteEndpoint)temp);
                            }
                            else
                            {
                                ((FederatedSparqlRemoteEndpoint)endpoint).AddEndpoint((SparqlRemoteEndpoint)temp);
                            }
                        }
                        else
                        {
                            throw new DotNetRdfConfigurationException("Unable to load the SPARQL Endpoint identified by the Node '" + e.ToString() + "' as one of the values for the dnr:endpoint property points to an Object which cannot be loaded as an object which is a SparqlRemoteEndpoint");
                        }
                    }
                    break;
#endif
            }

            if (endpoint != null)
            {
                //Are there any credentials specified?
                String user, pwd;
                ConfigurationLoader.GetUsernameAndPassword(g, objNode, true, out user, out pwd);
                if (user != null && pwd != null)
                {
                    endpoint.SetCredentials(user, pwd);
                }

#if !NO_PROXY
                //Is there a Proxy Server specified
                INode proxyNode = ConfigurationLoader.GetConfigurationNode(g, objNode, ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyProxy));
                if (proxyNode != null)
                {
                    Object proxy = ConfigurationLoader.LoadObject(g, proxyNode);
                    if (proxy is WebProxy)
                    {
                        endpoint.Proxy = (WebProxy)proxy;

                        //Are we supposed to use the same credentials for the proxy as for the endpoint?
                        bool useCredentialsForProxy = ConfigurationLoader.GetConfigurationBoolean(g, objNode, ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyUseCredentialsForProxy), false);
                        if (useCredentialsForProxy)
                        {
                            endpoint.UseCredentialsForProxy = true;
                        }
                      }
                    else
                    {
                        throw new DotNetRdfConfigurationException("Unable to load SPARQL Endpoint identified by the Node '" + objNode.ToString() + "' as the value for the dnr:proxy property points to an Object which cannot be loaded as an object of type WebProxy");
                    }
                }
#endif
            }

            obj = endpoint;
            return (endpoint != null);
        }