public static void Main() { System.Net.ServicePointManager.Expect100Continue = false; // don't send HTTP Expect: headers which confuse some servers string endpoint = "http://dbpedia.org/sparql"; string query = "PREFIX owl: <http://www.w3.org/2002/07/owl#>\n" + "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>\n" + "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n" + "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" + "PREFIX foaf: <http://xmlns.com/foaf/0.1/>\n" + "PREFIX dc: <http://purl.org/dc/elements/1.1/>\n" + "PREFIX : <http://dbpedia.org/resource/>\n" + "PREFIX dbpedia2: <http://dbpedia.org/property/>\n" + "PREFIX dbpedia: <http://dbpedia.org/>\n" + "PREFIX skos: <http://www.w3.org/2004/02/skos/core#>\n" + "\n" + "SELECT ?property ?hasValue ?isValueOf\n" + "WHERE {\n" + " { <http://dbpedia.org/resource/Category:First-person_shooters> ?property ?hasValue }\n" + " UNION\n" + " { ?isValueOf ?property <http://dbpedia.org/resource/Category:First-person_shooters> }\n" + "}\n"; SparqlHttpSource source = new SparqlHttpSource(endpoint); Console.WriteLine("RunSparqlQuery(query, Console.Out):"); source.RunSparqlQuery(query, Console.Out); Console.WriteLine(); Console.WriteLine("RunSparqlQuery(query, SparqlXmlQuerySink):"); source.RunSparqlQuery(query, new SparqlXmlQuerySink(Console.Out)); Console.WriteLine(); Console.WriteLine(); }
public static void Main() { System.Net.ServicePointManager.Expect100Continue = false; // don't send HTTP Expect: headers which confuse some servers string endpoint = "http://www.rdfabout.com/sparql"; string ex1 = "PREFIX foaf: <http://xmlns.com/foaf/0.1/>\n" + "SELECT ?name \n" + "WHERE { [] foaf:name ?name . }\n" + "LIMIT 10 \n"; string ex2 = "PREFIX foaf: <http://xmlns.com/foaf/0.1/>\n" + "ASK \n" + "WHERE { [] foaf:name ?name . }\n"; string ex3 = "PREFIX foaf: <http://xmlns.com/foaf/0.1/>\n" + "CONSTRUCT { ?person foaf:name2 ?name } \n" + "WHERE { ?person foaf:name ?name . }\n" + "LIMIT 10 \n"; SparqlHttpSource source = new SparqlHttpSource(endpoint); Console.WriteLine("RunSparqlQuery(ex1, Console.Out):"); source.RunSparqlQuery(ex1, Console.Out); Console.WriteLine(); Console.WriteLine("RunSparqlQuery(ex1, SparqlXmlQuerySink):"); source.RunSparqlQuery(ex1, new SparqlXmlQuerySink(Console.Out)); Console.WriteLine(); Console.WriteLine(); Console.WriteLine("RunSparqlQuery(ex2, bool):"); bool result; source.RunSparqlQuery(ex2, out result); Console.WriteLine(result); Console.WriteLine(); Console.WriteLine("RunSparqlQuery(ex3, N3Writer):"); using (N3Writer writer = new N3Writer(Console.Out)) source.RunSparqlQuery(ex3, writer); Console.WriteLine(); Console.WriteLine("Select(subject,__,__)"); using (N3Writer writer = new N3Writer(Console.Out)) source.Select(new Statement("http://www.rdfabout.com/rdf/usgov/congress/people/M000303", null, null), writer); Console.WriteLine(); Console.WriteLine("Query(...) A"); Variable a = new Variable("a"); QueryOptions qo = new QueryOptions(); qo.Limit = 10; source.Query(new Statement[] { new Statement(a, "http://xmlns.com/foaf/0.1/name", (Literal)"John McCain"), new Statement(a, new Variable("b"), new Variable("c")), }, qo, new SparqlXmlQuerySink(Console.Out)); Console.WriteLine(); Console.WriteLine(); Console.WriteLine("Query(...) B"); QueryResultBuffer qb = new QueryResultBuffer(); source.Query(new Statement[] { new Statement(a, "http://xmlns.com/foaf/0.1/name", (Literal)"John McCain"), new Statement(a, new Variable("b"), new Variable("c")), }, qo, qb); foreach (VariableBindings b in qb) { Console.WriteLine("a => " + b["a"]); Console.WriteLine("b => " + b["b"]); Console.WriteLine(); } Console.WriteLine(); }
private static void ExecuteQuery(Path cache) { var namespaces = new[] { new { Prefix = "foaf", Uri = foaf }, new { Prefix = "dbo", Uri = dbo }, new { Prefix = "dbp", Uri = "http://dbpedia.org/property/" }, new { Prefix = "skos", Uri = "http://www.w3.org/2004/02/skos/core#" }, new { Prefix = "xsd", Uri = "http://www.w3.org/2001/XMLSchema#" }, }; var attributes = new[] { new { Name = "foaf:page", Variable = "page" }, new { Name = "dbo:thumbnail", Variable = "photo" }, new { Name = "foaf:name", Variable = "name" }, new { Name = "dbo:abstract", Variable = "description" }, new { Name = "dbo:birthYear", Variable = "birthYear" }, new { Name = "dbo:birthPlace", Variable = "birthPlace" }, new { Name = "dbo:deathYear", Variable = "deathYear" }, new { Name = "dbo:deathPlace", Variable = "deathPlace " }, new { Name = "dbp:children", Variable = "child " }, new { Name = "dbp:parents", Variable = "parent " }, }; Console.WriteLine("Refreshing DBpedia data..."); var query = new StringBuilder(); foreach (var @namespace in namespaces) { query.AppendLine(string.Format("PREFIX {0}: <{1}>", @namespace.Prefix, @namespace.Uri)); } query.AppendLine("CONSTRUCT {"); foreach (var attribute in attributes) { query.AppendLine(string.Format("?person {0} ?{1} .", attribute.Name, attribute.Variable)); } query.AppendLine("} WHERE {"); query.AppendLine("?person skos:subject <http://dbpedia.org/resource/Category:American_inventors> ."); foreach (var attribute in attributes) { query.AppendLine(string.Format("OPTIONAL {{ ?person {0} ?{1} . }}", attribute.Name, attribute.Variable)); } query.AppendLine("FILTER (bound(?name) && lang(?name) = 'en') ."); query.AppendLine("FILTER (!bound(?description) || lang(?description) = 'en') ."); //query.AppendLine("FILTER (!bound(?birthYear) || datatype(?birthYear) = xsd:gYear) ."); //query.AppendLine("FILTER (!bound(?deathYear) || datatype(?deathYear) = xsd:gYear) ."); query.AppendLine("} ORDER BY ?person"); //query.AppendLine("LIMIT 20"); Console.WriteLine(query); using (var writer = new RdfXmlWriter(cache.FullPath)) { foreach (var @namespace in namespaces) { writer.Namespaces.AddNamespace(@namespace.Uri, @namespace.Prefix); } var source = new SparqlHttpSource("http://dbpedia.org/sparql"); source.RunSparqlQuery(query.ToString(), writer); } }