
-- Cleanup


SPARQL 
CLEAR GRAPH <urn:school:8890:sql:r2rml:mappings> ;

SPARQL
DROP QUAD MAP <urn:school:8890:sql:r2rml:mappings> ;

SPARQL
CLEAR GRAPH <urn:demo.openlinksw.com:8890:School> ;

-- Load Mapping Descriptions into a Designated Named Graph (or Internal Virtuoso Document) identified by its IRI

SPARQL

prefix rr: <http://www.w3.org/ns/r2rml#> 
prefix School: <http://demo.openlinksw.com:8890/schemas/SchoolDemo#> 
prefix school-stat: <http://demo.openlinksw.com:8890/SchoolDemo/stat#> 
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> 
prefix void: <http://rdfs.org/ns/void#> 
prefix scovo: <http://purl.org/NET/scovo#> 
prefix aowl: <http://bblfish.net/work/atom-owl/2006-06-06/> 
PREFIX foaf: <http://xmlns.com/foaf/0.1/> 

INSERT { GRAPH <urn:school:8890:sql:r2rml:mappings> {


<#TriplesMapCourse> a rr:TriplesMap; rr:logicalTable [ rr:sqlQuery """
																		  SELECT "CourseID", "Title", "Credits", "DepartmentID", "CourseID" 
																		  FROM "School"."DBA"."Course" 
									  							   """ ]; 
rr:subjectMap [ rr:termType rr:IRI  ; rr:template "http://demo.openlinksw.com:8890/SchoolDemo/course/CourseID={CourseID}#this"; rr:class School:Course; rr:graph <http://demo.openlinksw.com:8890/SchoolDemo#> ];
rr:predicateObjectMap [ rr:predicateMap [ rr:constant School:courseid ] ; rr:objectMap [ rr:column "CourseID" ]; ] ;
rr:predicateObjectMap [ rr:predicateMap [ rr:constant School:title ] ; rr:objectMap [ rr:column "Title" ]; ] ;
rr:predicateObjectMap [ rr:predicateMap [ rr:constant School:credits ] ; rr:objectMap [ rr:column "Credits" ]; ] ;
rr:predicateObjectMap [ rr:predicateMap [ rr:constant School:course_has_department ] ; rr:objectMap [ rr:termType rr:IRI ; rr:template "http://demo.openlinksw.com:8890/SchoolDemo/department/DepartmentID={DepartmentID}#this" ]; ] ;
rr:predicateObjectMap [ rr:predicateMap [ rr:constant School:course_of_coursegrade ] ; rr:objectMap [ rr:parentTriplesMap <#TriplesMapCourseGrade>;  rr:joinCondition [ rr:child "CourseID" ; rr:parent "CourseID" ] ; ]; ] ;
rr:predicateObjectMap [ rr:predicateMap [ rr:constant School:course_of_courseinstructor ] ; rr:objectMap [ rr:parentTriplesMap <#TriplesMapCourseInstructor>;  rr:joinCondition [ rr:child "CourseID" ; rr:parent "CourseID" ] ; ]; ] ;
rr:predicateObjectMap [ rr:predicateMap [ rr:constant School:course_of_onlinecourse ] ; rr:objectMap [ rr:parentTriplesMap <#TriplesMapOnlineCourse>;  rr:joinCondition [ rr:child "CourseID" ; rr:parent "CourseID" ] ; ]; ] ;
rr:predicateObjectMap [ rr:predicateMap [ rr:constant School:course_of_onsitecourse ] ; rr:objectMap [ rr:parentTriplesMap <#TriplesMapOnsiteCourse>;  rr:joinCondition [ rr:child "CourseID" ; rr:parent "CourseID" ] ; ]; ] .

<#TriplesMapCourseGrade> a rr:TriplesMap; rr:logicalTable [ rr:sqlQuery """
																		  SELECT "EnrollmentID", "Grade", "CourseID", "StudentID" 
																		  FROM "School"."DBA"."CourseGrade" 
									  							   """ ]; 
rr:subjectMap [ rr:termType rr:IRI  ; rr:template "http://demo.openlinksw.com:8890/SchoolDemo/coursegrade/EnrollmentID={EnrollmentID}#this"; rr:class School:CourseGrade; rr:graph <http://demo.openlinksw.com:8890/SchoolDemo#> ];
rr:predicateObjectMap [ rr:predicateMap [ rr:constant School:enrollmentid ] ; rr:objectMap [ rr:column "EnrollmentID" ]; ] ;
rr:predicateObjectMap [ rr:predicateMap [ rr:constant School:grade ] ; rr:objectMap [ rr:column "Grade" ]; ] ;
rr:predicateObjectMap [ rr:predicateMap [ rr:constant School:coursegrade_has_course ] ; rr:objectMap [ rr:termType rr:IRI ; rr:template "http://demo.openlinksw.com:8890/SchoolDemo/course/CourseID={CourseID}#this" ]; ] ;
rr:predicateObjectMap [ rr:predicateMap [ rr:constant School:coursegrade_has_person ] ; rr:objectMap [ rr:termType rr:IRI ; rr:template "http://demo.openlinksw.com:8890/SchoolDemo/person/StudentID={StudentID}#this" ]; ] .

<#TriplesMapCourseInstructor> a rr:TriplesMap; rr:logicalTable [ rr:sqlQuery """
																		  SELECT "CourseID", "PersonID" 
																		  FROM "School"."DBA"."CourseInstructor" 
									  							   """ ]; 
rr:subjectMap [ rr:termType rr:IRI  ; rr:template "http://demo.openlinksw.com:8890/SchoolDemo/courseinstructor/CourseID={CourseID}/PersonID={PersonID}#this"; rr:class School:CourseInstructor ; rr:graph <http://demo.openlinksw.com:8890/SchoolDemo#> ];
rr:predicateObjectMap [ rr:predicateMap [ rr:constant School:courseinstructor_has_course ] ; rr:objectMap [ rr:termType rr:IRI ; rr:template "http://demo.openlinksw.com:8890/SchoolDemo/course/CourseID={CourseID}#this" ]; ] ;
rr:predicateObjectMap [ rr:predicateMap [ rr:constant School:courseinstructor_has_person ] ; rr:objectMap [ rr:termType rr:IRI ; rr:template "http://demo.openlinksw.com:8890/SchoolDemo/person/PersonID={PersonID}#this" ]; ] .

<#TriplesMapDepartment> a rr:TriplesMap; rr:logicalTable [ rr:sqlQuery """
																		  SELECT "DepartmentID", "Name", "Budget", "StartDate", "Administrator" 
																		  FROM "School"."DBA"."Department" 
									  							   """ ];
rr:subjectMap [ rr:termType rr:IRI  ; rr:template "http://demo.openlinksw.com:8890/SchoolDemo/department/DepartmentID={DepartmentID}#this"; rr:class School:Department; rr:graph <http://demo.openlinksw.com:8890/SchoolDemo#> ];
rr:predicateObjectMap [ rr:predicateMap [ rr:constant School:departmentid ] ; rr:objectMap [ rr:column "DepartmentID" ]; ] ;
rr:predicateObjectMap [ rr:predicateMap [ rr:constant School:name ] ; rr:objectMap [ rr:column "Name" ]; ] ;
rr:predicateObjectMap [ rr:predicateMap [ rr:constant School:budget ] ; rr:objectMap [ rr:column "Budget" ]; ] ;
rr:predicateObjectMap [ rr:predicateMap [ rr:constant School:startdate ] ; rr:objectMap [ rr:column "StartDate" ]; ] ;
rr:predicateObjectMap [ rr:predicateMap [ rr:constant School:administrator ] ; rr:objectMap [ rr:column "Administrator" ]; ] ;
rr:predicateObjectMap [ rr:predicateMap [ rr:constant School:department_of_course ] ; rr:objectMap [ rr:parentTriplesMap <#TriplesMapCourse>;  rr:joinCondition [ rr:child "DepartmentID" ; rr:parent "DepartmentID" ] ; ]; ] .

<#TriplesMapOfficeAssignment> a rr:TriplesMap; rr:logicalTable [ rr:sqlQuery """
																		  SELECT "InstructorID", "Location", "Timestamp"
																		  FROM "School"."DBA"."OfficeAssignment" 
									  							   """ ];
rr:subjectMap [ rr:termType rr:IRI  ; rr:template "http://demo.openlinksw.com:8890/SchoolDemo/officeassignment/InstructorID={InstructorID}#this"; rr:class School:OfficeAssignment; rr:graph <http://demo.openlinksw.com:8890/SchoolDemo#> ];
rr:predicateObjectMap [ rr:predicateMap [ rr:constant School:location ] ; rr:objectMap [ rr:column "Location" ]; ] ;
rr:predicateObjectMap [ rr:predicateMap [ rr:constant School:timestamp ] ; rr:objectMap [ rr:column "Timestamp" ]; ] ;
rr:predicateObjectMap [ rr:predicateMap [ rr:constant School:officeassignment_has_person ] ; rr:objectMap [ rr:termType rr:IRI ; rr:template "http://demo.openlinksw.com:8890/SchoolDemo/person/InstructorID={InstructorID}#this" ]; ] .

<#TriplesMapOnlineCourse> a rr:TriplesMap; rr:logicalTable [ rr:sqlQuery """
																		  SELECT "CourseID", "URL"
																		  FROM "School"."DBA"."OnlineCourse" 
									  							   """ ]; 
rr:subjectMap [ rr:termType rr:IRI  ; rr:template "http://demo.openlinksw.com:8890/SchoolDemo/onlinecourse/CourseID={CourseID}#this"; rr:class School:OnlineCourse; rr:graph <http://demo.openlinksw.com:8890/SchoolDemo#> ];
rr:predicateObjectMap [ rr:predicateMap [ rr:constant School:url ] ; rr:objectMap [ rr:column "URL" ]; ] ;
rr:predicateObjectMap [ rr:predicateMap [ rr:constant School:onlinecourse_has_course ] ; rr:objectMap [ rr:termType rr:IRI ; rr:template "http://demo.openlinksw.com:8890/SchoolDemo/course/CourseID={CourseID}#this" ]; ] .

<#TriplesMapOnsiteCourse> a rr:TriplesMap; rr:logicalTable [ rr:sqlQuery """
																		  SELECT "CourseID", "Location", "Days", "Time"
																		  FROM "School"."DBA"."OnsiteCourse" 
									  							   """ ];
rr:subjectMap [ rr:termType rr:IRI  ; rr:template "http://demo.openlinksw.com:8890/SchoolDemo/onsitecourse/CourseID={CourseID}#this"; rr:class School:OnsiteCourse; rr:graph <http://demo.openlinksw.com:8890/SchoolDemo#> ];
rr:predicateObjectMap [ rr:predicateMap [ rr:constant School:location ] ; rr:objectMap [ rr:column "Location" ]; ] ;
rr:predicateObjectMap [ rr:predicateMap [ rr:constant School:days ] ; rr:objectMap [ rr:column "Days" ]; ] ;
rr:predicateObjectMap [ rr:predicateMap [ rr:constant School:time ] ; rr:objectMap [ rr:column "Time" ]; ] ;
rr:predicateObjectMap [ rr:predicateMap [ rr:constant School:onsitecourse_has_course ] ; rr:objectMap [ rr:termType rr:IRI ; rr:template "http://demo.openlinksw.com:8890/SchoolDemo/course/CourseID={CourseID}#this" ]; ] .

<#TriplesMapPerson> a rr:TriplesMap; rr:logicalTable [ rr:sqlQuery """
																		  SELECT "PersonID", "LastName", "FirstName", CONCAT(CONCAT("FirstName",' ') , "LastName") AS "Name", "HireDate", "EnrollmentDate"
																		  FROM "School"."DBA"."Person" 
									  							   """ ]; 
rr:subjectMap [ rr:termType rr:IRI  ; rr:template "http://demo.openlinksw.com:8890/SchoolDemo/person/PersonID={PersonID}#this"; rr:class School:Person, foaf:Agent, foaf:Person; rr:graph <http://demo.openlinksw.com:8890/SchoolDemo#> ];
rr:predicateObjectMap [ rr:predicateMap [ rr:constant School:personid ] ; rr:objectMap [ rr:column "PersonID" ]; ] ;
rr:predicateObjectMap [ rr:predicateMap [ rr:constant School:lastname ] ; rr:objectMap [ rr:column "LastName" ]; ] ;
rr:predicateObjectMap [ rr:predicateMap [ rr:constant School:firstname ] ; rr:objectMap [ rr:column "FirstName" ]; ] ;
rr:predicateObjectMap [ rr:predicateMap [ rr:constant School:name ] ; rr:objectMap [ rr:column "Name" ]; ] ;
rr:predicateObjectMap [ rr:predicateMap [ rr:constant School:hiredate ] ; rr:objectMap [ rr:column "HireDate" ]; ] ;
rr:predicateObjectMap [ rr:predicateMap [ rr:constant School:enrollmentdate ] ; rr:objectMap [ rr:column "EnrollmentDate" ]; ] ;
rr:predicateObjectMap [ rr:predicateMap [ rr:constant School:person_of_coursegrade ] ; rr:objectMap [ rr:parentTriplesMap <#TriplesMapCourseGrade>;  rr:joinCondition [ rr:child "PersonID" ; rr:parent "StudentID" ] ; ]; ] ;
rr:predicateObjectMap [ rr:predicateMap [ rr:constant School:person_of_courseinstructor ] ; rr:objectMap [ rr:parentTriplesMap <#TriplesMapCourseInstructor>;  rr:joinCondition [ rr:child "PersonID" ; rr:parent "PersonID" ] ; ]; ] ;
rr:predicateObjectMap [ rr:predicateMap [ rr:constant School:person_of_officeassignment ] ; rr:objectMap [ rr:parentTriplesMap <#TriplesMapOfficeAssignment>;  rr:joinCondition [ rr:child "PersonID" ; rr:parent "InstructorID" ] ; ]; ] .

}
} ;


-- Next command is commented out due to fact that it produces urn: unsupported error
-- DB.DBA.R2RML_GENERATE_LINKED_VIEW('urn:School:8890:r2rml', 'http://demo.openlinksw.com:8890/SchoolDemo#', 0);
-- DB.DBA.R2RML_GENERATE_LINKED_VIEW('urn:School:8890:r2rml', 'http://demo.openlinksw.com:8890/SchoolDemo#', 0);
-- THIS WORKS for generating triples for Virtual RDF Views Graph: DB.DBA.R2RML_GENERATE_LINKED_VIEW('http://kingsley.idehen.net/DAV/home/kidehen/Public/R2RML%20Demos/School-8890-r2rml.ttl',
--'http://demo.openlinksw.com:8890/SchoolDemo#', 0);
-- THIS WORKS for generating triples for both Virtual & Physical RDF Views Graph: DB.DBA.R2RML_GENERATE_LINKED_VIEW('http://kingsley.idehen.net/DAV/home/kidehen/Public/R2RML%20Demos/School-8890-r2rml.ttl',
-- 'urn:opllinux5.usnet.private:8890:School', 1);
-- Generate & Execute Virtual RDF Views Script, hence the use of EXEC()

EXEC ('SPARQL ' || DB.DBA.R2RML_MAKE_QM_FROM_G ('urn:school:8890:sql:r2rml:mappings'));



-- Test RDF generated Virtual RDF Views
-- Test 1

SPARQL
SELECT SAMPLE(?s) AS ?sample COUNT(1) AS ?count ?o
FROM <http://demo.openlinksw.com:8890/SchoolDemo#> 
WHERE {?s a ?o}
GROUP BY ?o
ORDER BY DESC(?count)
LIMIT 50 ;

-- Test 2 (workaround for RDF Views created using rr:query)

SPARQL

SELECT ?o IRI(max(str(?s))) AS ?sample COUNT(1) AS ?count
FROM <http://demo.openlinksw.com:8890/SchoolDemo#> 
WHERE {?s a ?o}
GROUP BY ?o
ORDER BY DESC(?count)
LIMIT 50 ;

-- Sync Triples from Virtual RDF Views Graph to Physical RDF Views Graph

RDF_VIEW_SYNC_TO_PHYSICAL ('http://demo.openlinksw.com:8890/SchoolDemo#', 1, 'urn:demo.openlinksw.com:8890:School');

-- Test Phyical RDF Views sync'd from Virtual RDF Views Graph

-- Virtual Graph Triples Count
SPARQL
SELECT count(*)
FROM <http://demo.openlinksw.com:8890/SchoolDemo#>
WHERE {?s ?p ?o} ;

-- Physical Graph Triples Count

SPARQL
SELECT count(*)
FROM <urn:demo.openlinksw.com:8890:School> 
# FROM <http://demo.openlinksw.com:8890/SchoolDemo#>
WHERE {?s ?p ?o} ;

SPARQL
SELECT SAMPLE(?s) AS ?sample COUNT(1) AS ?count ?o
FROM <urn:demo.openlinksw.com:8890:School> 
WHERE {?s a ?o}
GROUP BY ?o
ORDER BY DESC(?count)
LIMIT 50 ;
