% Since Prova 1.9.
% Demonstrate built-in SPARQL integration in Prova

:- eval(ex059()).

% The most straightforward way of runninq SPARQL queries that indicates the required data right inside the FROM clause
ex059() :-
	QueryString = '
		PREFIX foaf: <http://xmlns.com/foaf/0.1/>
		PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
		SELECT ?contributor ?url ?type
			FROM <http://planetrdf.com/bloggers.rdf>
			WHERE {
				?contributor foaf:name "Bob DuCharme" .
				?contributor foaf:weblog ?url .
				?contributor rdf:type ?type .
			}
	',
	sparql_select(QueryString,url(URL),type(Type)|X),
	println([[url,URL],[type,Type]|X],",").

% Another way of runninq Sparql queries that uses an explicitely constructed Jena Model  
ex059a() :-
	Model = com.hp.hpl.jena.rdf.model.ModelFactory.createDefaultModel(),
	urlstream("http://planetrdf.com/bloggers.rdf",In),
	Model.read(In,""),
%	Model.write(System.out),
	QueryString = '
		PREFIX foaf: <http://xmlns.com/foaf/0.1/>
		SELECT ?url
			WHERE {
				?contributor foaf:name "Bob DuCharme" .
				?contributor foaf:weblog ?url .
			}
	',
	sparql_select(Model,QueryString,url(URL)),
	println([url,"=",URL]).

urlstream(Address,IS) :-
	URL = java.net.URL(Address),
	IS = URL.openStream().


% SPARQL query without rest variable
ex059b() :-
   QueryString = '
      PREFIX foaf: <http://xmlns.com/foaf/0.1/>
      PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
      SELECT ?contributor ?url ?type
         FROM <http://planetrdf.com/bloggers.rdf>
         WHERE {
            ?contributor foaf:name "Bob DuCharme" .
            ?contributor foaf:weblog ?url .
            ?contributor rdf:type ?type .
         }
   ',
   sparql_select(QueryString,url(URL),type(Type),contributor(Contributor)),
   println([[url,URL],[type,Type],[contributor,Contributor]],",").

   
% call a SPARQL web end point / service. 
% The restricted key word "SPARQLQUERY" in the URL is replaced by the 
% query string.
% The SPARQL endpoint must return a result in SPARQL XML format
% <http://www.w3.org/TR/rdf-sparql-XMLres/>
ex059c() :-
    SparqlServiceURL = 'http://hcls.deri.ie/sparql/?query=SPARQLQUERY&format=application/sparql-results+xml', 
	QueryString = '
		PREFIX owl: <http://www.w3.org/2002/07/owl#>
		PREFIX go: <http://purl.org/obo/owl/GO#>
		PREFIX obo: <http://www.geneontology.org/formats/oboInOwl#>
		PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
		
		select  ?name  ?class ?definition
		from <http://purl.org/commons/hcls/20070416>
		where
		{   graph <http://purl.org/commons/hcls/20070416/classrelations>
		        {?class rdfs:subClassOf go:GO_0008150}
		?class rdfs:label ?name.
		?class obo:hasDefinition ?def.
		?def rdfs:label ?definition 
		filter(regex(?name,"[Dd]endrite"))
		}
	',
	sparql_select(SparqlServiceURL,QueryString,name(Name),class(Class),definition(Def)),
	println([[name,Name],[class,Class],[definition,Def]],",").



% call a SPARQL end point with the query encoded directly in the URL
% The SPARQL endpoint must return a result in SPARQL XML format
% <http://www.w3.org/TR/rdf-sparql-XMLres/> 
ex059d() :-
    SparqlServiceURL = 'http://hcls.deri.ie/sparql/?query=PREFIX+owl%3A+%3Chttp%3A%2F%2Fwww.w3.org%2F2002%2F07%2Fowl%23%3E%0D%0APREFIX+go%3A+%3Chttp%3A%2F%2Fpurl.org%2Fobo%2Fowl%2FGO%23%3E%0D%0APREFIX+obo%3A+%3Chttp%3A%2F%2Fwww.geneontology.org%2Fformats%2FoboInOwl%23%3E%0D%0APREFIX+rdfs%3A+%3Chttp%3A%2F%2Fwww.w3.org%2F2000%2F01%2Frdf-schema%23%3E%0D%0A%0D%0Aselect++%3Fname++%3Fclass+%3Fdefinition%0D%0Afrom+%3Chttp%3A%2F%2Fpurl.org%2Fcommons%2Fhcls%2F20070416%3E%0D%0Awhere%0D%0A%7B+++graph+%3Chttp%3A%2F%2Fpurl.org%2Fcommons%2Fhcls%2F20070416%2Fclassrelations%3E%0D%0A++++++++%7B%3Fclass+rdfs%3AsubClassOf+go%3AGO_0008150%7D%0D%0A%3Fclass+rdfs%3Alabel+%3Fname.%0D%0A%3Fclass+obo%3AhasDefinition+%3Fdef.%0D%0A%3Fdef+rdfs%3Alabel+%3Fdefinition+%0D%0Afilter%28regex%28%3Fname%2C%22%5BDd%5Dendrite%22%29%29%0D%0A%7D%0D%0A++++&format=application/sparql-results+xml', 
	sparql_select(SparqlServiceURL,name(Name),class(Class),definition(Def)),
	println([[name,Name],[class,Class],[definition,Def]],",").	