SPARQL 
CLEAR GRAPH <urn:spin:nanotation:demo:kennedy:family> ;


SPARQL
PREFIX  rel:  <http://purl.org/vocab/relationship/> 
PREFIX wdrs: <http://www.w3.org/2007/05/powder-s#> 
PREFIX schema: <http://schema.org/> 
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 ex:    <http://ex#> 
PREFIX kennedys:    <http://topbraid.org/examples/kennedys#> 
PREFIX : <http://qq/> 



WITH <urn:spin:nanotation:demo:kennedy:family>
INSERT {
		  ex:Parent
		      a       rdfs:Class ;
		      rdfs:label "Parent"^^xsd:string ;
		      rdfs:subClassOf ex:Person ;
		      spin:constraint
		              [ a       sp:Ask ;
		                sp:text """
		                    # must be at least 18 years old
							PREFIX  rel:  <http://purl.org/vocab/relationship/> 
							PREFIX wdrs: <http://www.w3.org/2007/05/powder-s#> 
							PREFIX sp: <http://spinrdf.org/sp#> 
							PREFIX spl: <http://spinrdf.org/spl#> 
		                    ASK WHERE {
		                        ?this ex:age ?age .
		                        FILTER (?age < 18) .
		                    }"""
		              ] .

		  owl:Thing
		        spin:constraint
		          [ a       sp:Construct ;
		            sp:text """
					PREFIX  rel:  <http://purl.org/vocab/relationship/> 
					PREFIX wdrs: <http://www.w3.org/2007/05/powder-s#> 
					PREFIX sp: <http://spinrdf.org/sp#> 
					PREFIX spl: <http://spinrdf.org/spl#> 
		                CONSTRUCT {
				                     _:violation a spin:ConstraintViolation ;
			                         spin:violationRoot ?this ;
			                         spin:violationPath kennedys:spouse ;
			                         rdfs:label "Same-sex marriage not permitted (in this model)"
		                		 }
		                WHERE {
		                    ?this kennedys:spouse ?spouse .
		                    ?this kennedys:gender ?gender .
		                    ?spouse kennedys:gender ?spouseGender .
		                    FILTER (?gender = ?spouseGender) .
		                }"""
		          ] .

		ex:Person
		      a       rdfs:Class ;
		      rdfs:label "Person"^^xsd:string ;
		      rdfs:subClassOf owl:Thing ;
		      spin:rule
		              [ a       sp:Construct ;
		                sp:text """
							PREFIX  rel:  <http://purl.org/vocab/relationship/> 
							PREFIX wdrs: <http://www.w3.org/2007/05/powder-s#> 
							PREFIX sp: <http://spinrdf.org/sp#> 
							PREFIX spl: <http://spinrdf.org/spl#> 
		                    CONSTRUCT {
		                        ?this ex:grandParent ?grandParent .
		                    }
		                    WHERE {
		                        ?parent ex:child ?this .
		                        ?grandParent ex:child ?parent .
		                    }"""
		              ] .

		  ex:USCitizen
		      a       rdfs:Class ;
		      rdfs:label "US citizen"^^xsd:string ;
		      rdfs:subClassOf ex:Person ;
		      spin:constructor
		              [ a       sp:Construct ;
		                sp:text """
							PREFIX  rel:  <http://purl.org/vocab/relationship/> 
							PREFIX wdrs: <http://www.w3.org/2007/05/powder-s#> 
							PREFIX sp: <http://spinrdf.org/sp#> 
							PREFIX spl: <http://spinrdf.org/spl#> 
		                    CONSTRUCT {
		                        ?this ex:birthCountry ex:USA .
		                    }
		                    WHERE {
		                    }"""
		              ] .

		ex:Parent
		      a       owl:Class ;
		      rdfs:label "Parent"^^xsd:string ;
		      rdfs:subClassOf owl:Thing ;
		      spin:constraint
		              [ a ex:MinCardinality ;
		                ex:count 1 ;
		                ex:predicate ex:child
		              ] .

		ex:cardinality
		      a       spin:Function ;
		      rdfs:subClassOf   spin:Functions ;
		      rdfs:comment "Gets the number of values of a given property at the current subject (?this)." ;
		      rdfs:label "cardinality"^^xsd:string ;
		      spin:constraint
		              [ a       spl:Argument ;
		                rdfs:comment "The property to get the cardinality of." ;
		                spl:predicate sp:arg1
		              ] ;
		      spin:body
		              [ a       sp:Select ;
		                sp:text """
							PREFIX  rel:  <http://purl.org/vocab/relationship/> 
							PREFIX wdrs: <http://www.w3.org/2007/05/powder-s#> 
							PREFIX sp: <http://spinrdf.org/sp#> 
							PREFIX spl: <http://spinrdf.org/spl#> 
		                    SELECT (COUNT(?object) AS ?result)
		                    WHERE {
		                        ?this ?arg1 ?object .
		                    }"""
		              ] .

		:grandParent
		      a       spin:MagicProperty ;
		      rdfs:label "grand parent"^^xsd:string ;
		      rdfs:subClassOf spin:MagicProperties ;
		      spin:constraint
		              [ a       spl:Argument ;
		                rdfs:comment "The Person to get the grandparent(s) of."^^xsd:string ;
		                spl:predicate sp:arg1 ;
		                spl:valueType kennedys:Person
		              ] ;
		      spin:body
		              [ a       sp:Select ;
		                sp:text """
							PREFIX  rel:  <http://purl.org/vocab/relationship/> 
							PREFIX wdrs: <http://www.w3.org/2007/05/powder-s#> 
							PREFIX sp: <http://spinrdf.org/sp#> 
							PREFIX spl: <http://spinrdf.org/spl#> 
		                    SELECT ?grandParent
		                    WHERE {
		                        ?parent kennedys:child ?arg1 .
		                        ?grandParent kennedys:child ?parent .
		                    }"""
		              ] .
	} ;


-- Then Execute

SELECT DB.DBA.SPARQL_SPIN_GRAPH_TO_DEFSPIN ('urn:spin:nanotation:demo:kennedy:family') as x long varchar ;