-- Name Mapping Rules for RDF Views
-- Cleanup re. physical graphs

SPARQL CLEAR GRAPH <http://demo.openlinksw.com:8890/Progress9#> ;
SPARQL CLEAR GRAPH <http://demo.openlinksw.com:8890/Progress10#> ;
SPARQL CLEAR GRAPH <http://demo.openlinksw.com:8890/ingres#> ;
SPARQL CLEAR GRAPH <http://demo.openlinksw.com:8890/prestoDB#> ;
SPARQL CLEAR GRAPH <http://demo.openlinksw.com:8890/SQLServer#> ;
SPARQL CLEAR GRAPH <http://demo.openlinksw.com:8890/Demo#> ;
SPARQL CLEAR GRAPH <http://demo.openlinksw.com:8890/sybase12#> ;
SPARQL CLEAR GRAPH <http://demo.openlinksw.com:8890/OracleHR#> ;
SPARQL CLEAR GRAPH <http://demo.openlinksw.com:8890/informix#> ;


-- Control Test 1: RDF View confirmation Test
SPARQL

SELECT count(*) AS ?count 
       sample(?s) AS ?sample 
	   ?o 
FROM <http://demo.openlinksw.com:8890/Progress9#>
FROM <http://demo.openlinksw.com:8890/Progress10#>
FROM <http://demo.openlinksw.com:8890/ingres#>
# FROM <http://demo.openlinksw.com:8890/PrestoDB#>
FROM <http://demo.openlinksw.com:8890/sqlserver#>
FROM <http://demo.openlinksw.com:8890/Demo#>
FROM <http://demo.openlinksw.com:8890/sybase12#>
FROM <http://demo.openlinksw.com:8890/informix#>
FROM <http://demo.openlinksw.com:8890/OracleHR#>
FROM <http://demo.openlinksw.com:8890/csv#>

WHERE {?s a ?o} 
GROUP BY ?o ;

-- Control Test 2

SPARQL
SELECT DISTINCT ?this foaf:name ?name  
			    WHERE {

					      {  GRAPH <http://demo.openlinksw.com:8890/ingres#>
					         {
					             ?this   <http://demo.openlinksw.com:8890/schemas/ingres/name>   ?name   .
					         }
					      }

						UNION

						   {  GRAPH <http://demo.openlinksw.com:8890/OracleHR#>
						      {
						          ?this  <http://demo.openlinksw.com:8890/schemas/OracleHR/name>   ?name   .
						      }
						   }
   
					   UNION

					      {  GRAPH <http://demo.openlinksw.com:8890/Demo#>
					         {
					             ?this   <http://demo.openlinksw.com:8890/schemas/Demo/companyname>   ?name   .
					         }
					      }
						  
					   UNION

					      {  GRAPH <http://demo.openlinksw.com:8890/SchoolDemo#>
					         {
					             ?this   <http://demo.openlinksw.com:8890/schemas/SchoolDemo#Name>   ?name   .
					         }
					      }

					   UNION

					      {  GRAPH <http://demo.openlinksw.com:8890/SQLServer#>
					         {
					             ?this   <http://demo.openlinksw.com:8890/schemas/SQLServer/companyname>   ?name   .
					         }
					      }

					   UNION

					      {  GRAPH <http://demo.openlinksw.com:8890/progress9#>
					         {
					             ?this   <http://demo.openlinksw.com:8890/schemas/progress9/name>   ?name   .
					         }
					      }
						  
  						   UNION

  						      {  GRAPH <http://demo.openlinksw.com:8890/progress10#>
  						         {
  						             ?this   <http://demo.openlinksw.com:8890/schemas/progress9/name>   ?name   .
  						         }
  						      }

					   UNION

					      {  GRAPH <http://demo.openlinksw.com:8890/sybase12#>
					         {
					            ?this   <http://demo.openlinksw.com:8890/schemas/sybase12/pub_name>   ?name   .
					         }
					      }

					   # UNION

					   #   {  GRAPH <http://demo.openlinksw.com:8890/prestoDB#>
					   #      {
					   #         ?this   <http://demo.openlinksw.com:8890/schemas/PrestoDB/c_name>   ?name   .
					   #      }
					   #   }
					} ;
					
-- Cleanup old Rules 							 
								 
SPARQL CLEAR GRAPH <urn:data:virtualization:demo:labels:spin:rules:1>   ;

SPARQL DROP SPIN  LIBRARY <urn:data:virtualization:demo:labels:spin:rules:1> ; 

TTLP ('
@prefix owl:     <http://www.w3.org/2002/07/owl#> .
@prefix sp:    <http://spinrdf.org/sp#> .
@prefix spl:    <http://spinrdf.org/spl#> .
@prefix spin:    <http://spinrdf.org/spin#> .
@prefix rdf:     <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs:    <http://www.w3.org/2000/01/rdf-schema#> .
@prefix xsd:     <http://www.w3.org/2001/XMLSchema#> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix  rel:  <http://purl.org/vocab/relationship/> .
@prefix wdrs: <http://www.w3.org/2007/05/powder-s#> .
@prefix schema: <http://schema.org/> .
@prefix ecrm: <http://www.openlinksw.com/ontology/ecrm#> . 
@prefix Oracle: <http://demo.openlinksw.com:8890/schemas/OracleHR#> .



foaf:Agent a rdfs:Class ;

rdfs:label "Agents (Person, Organization, or Bot)"^^xsd:string ;
spin:rule
          [ 
		  	a sp:Construct, spin:Rule ;
            rdfs:label "Label Aesthetics Rule" ;
            rdfs:comment """Dynamically generated relations that inform Faceted Browser about Labels for URIS aesthetics""" ;
            sp:text """
			CONSTRUCT 
			   {  
			            ?this foaf:name ?name .
			   }
			WHERE
			   {
			      { SELECT DISTINCT ?this ?name  
				    WHERE {

						      {  GRAPH <http://demo.openlinksw.com:8890/ingres#>
						         {
						             ?this   <http://demo.openlinksw.com:8890/schemas/ingres/name>   ?name   .
						         }
						      }

							UNION

							   {  GRAPH <http://demo.openlinksw.com:8890/OracleHR#>
							      {
							          ?this  <http://demo.openlinksw.com:8890/schemas/OracleHR#name>   ?name   .
							      }
							   }

						   UNION

						      {  GRAPH <http://demo.openlinksw.com:8890/NorthwindDemo#>
						         {
						             ?this   <http://demo.openlinksw.com:8890/schemas/Demo/companyname>   ?name   .
						         }
						      }
							  
   						   UNION

   						      {  GRAPH <http://demo.openlinksw.com:8890/SchoolQA2#>
   						         {
   						             ?this   <http://demo.openlinksw.com:8890/schemas/SchoolQA#Name>   ?name   .
   						         }
   						      }

						   UNION

						      {  GRAPH <http://demo.openlinksw.com:8890/SQLServer#>
						         {
						             ?this   <http://demo.openlinksw.com:8890/schemas/SQLServer/companyname>   ?name   .
						         }
						      }

						   UNION

						      {  GRAPH <http://demo.openlinksw.com:8890/progress9#>
						         {
						             ?this   <http://demo.openlinksw.com:8890/schemas/progress9/name>   ?name   .
						         }
						      }
				  
							   UNION

							      {  GRAPH <http://demo.openlinksw.com:8890/progress10#>
							         {
							             ?this   <http://demo.openlinksw.com:8890/schemas/progress9/name>   ?name   .
							         }
							      }

						   UNION

						      {  GRAPH <http://demo.openlinksw.com:8890/sybase12#>
						         {
						            ?this   <http://demo.openlinksw.com:8890/schemas/sybase12/pub_name>   ?name   .
						         }
						      }

						   # UNION

						   #   {  GRAPH <http://demo.openlinksw.com:8890/prestoDB#>
						   #      {
						   #         ?this   <http://demo.openlinksw.com:8890/schemas/PrestoDB/c_name>   ?name   .
						   #      }
						   #   }
						}
					 }
			   }
					"""
		]. ','', 'urn:data:virtualization:demo:labels:spin:rules:1', 4096 ) ;
 
 -- Generate

 EXEC ('SPARQL ' || SPARQL_SPIN_GRAPH_TO_DEFSPIN('urn:data:virtualization:demo:labels:spin:rules:1'));
 
 -- Test Query 1
 -- Graph scoping is used to negate fact that by default virtual graphs aren't in scope of /sparql queries

 SPARQL 

 DEFINE input:macro-lib <urn:data:virtualization:demo:labels:spin:rules:1>  
 
 SELECT DISTINCT ?s ?name  
 WHERE { 
           GRAPH ?g {?s a foaf:Agent }
           GRAPH ?g2 { ?s foaf:name ?name . }   
       } ;
	  
-- Test Query 2

SPARQL

DEFINE input:macro-lib <urn:data:virtualization:demo:labels:spin:rules:1>  

SELECT DISTINCT ?s ?name  
WHERE { 
          GRAPH ?g {?s a foaf:Agent; foaf:name ?name . }   
      } ;
	 
-- Test Query 3
-- This query include both inferred and existing triples that satisfy condition
-- Only works with Named Graphs associated with Physical Triples

SPARQL

DEFINE input:macro-lib <urn:data:virtualization:demo:labels:spin:rules:1>  

SELECT DISTINCT SAMPLE(?s) count(1) as ?count ?o 
WHERE { 
      graph ?g { ?s a foaf:Agent; a ?o  }
    } 
GROUP BY ?o ;

-- Test Query 3 (workaround that works with Named Graphs associated with Virtual or Physical Triples)

DEFINE input:macro-lib <urn:data:virtualization:demo:labels:spin:rules:1>  

SELECT DISTINCT IRI(max(str(?s))) count(1) as ?count ?o 
WHERE { 
      graph ?g { ?s a foaf:Agent; a ?o  }
    } 
GROUP BY ?o ;
	 
	  