-- Raw Data -- Cleanup SPARQL CLEAR GRAPH ; SPARQL PREFIX rel: PREFIX wdrs: WITH INSERT { ## Custom Entity Relationship Types (Relations) ## <#hasUncle> a rdf:Property ; rdfs:label "hasUncle" ; rdfs:comment """Relationship Type that's derived from a SPARQL-based Custom Inference Rule that associates a Person with a Male Person that's a sibling of one of their Parents. """ ; rdfs:domain <#RoyalPerson> ; rdfs:range <#MalePerson> ; wdrs:describedby <#> . <#hasAuntie> a rdf:Property ; rdfs:label "hasAuntie" ; rdfs:comment """Relationship Type that's derived from a SPARQL-based Custom Inference Rule that associates a Person with a Female Person that's a sibling of one of their Parents. """ ; rdfs:domain <#RoyalPerson> ; rdfs:range <#FemalePerson> ; wdrs:describedby <#> . <#hasCousin> a rdf:Property ; rdfs:label "hasCousin" ; rdfs:comment """Relationship Type that's derived from a SPARQL-based Custom Inference Rule that associates a Person with another Person; that's a child of the sibling of one of their Parents. """ ; rdfs:domain <#RoyalPerson> ; rdfs:range <#FemalePerson> ; wdrs:describedby <#> . ## Custom Entity Types (Classes) ## <#RoyalPerson> rdfs:subClassOf foaf:Person, schema:Person ; rdfs:label "Person Class" ; rdfs:comment """ A person that inherits properties of both the foaf:Person and schema:Person classes from their respective vocabularies. The properties of this class are defined using custom inference rules using SPARQL via .""" ; wdrs:describedby <#> . <#FemalePerson> rdfs:subClassOf <#RoyalPerson> ; rdfs:label "Female Person Class" ; rdfs:comment """ A female person that inherits properties of the Person class identified by relative URI: <#RoyalPerson>. The properties of this class are defined using custom inference rules using SPARQL via . """ ; wdrs:describedby <#> . <#MalePerson> rdfs:subClassOf <#RoyalPerson> ; rdfs:label "Male Person Class" ; rdfs:comment """ A male person that inherits properties of the Person class identified by relative URI: <#RoyalPerson>. The properties of this class are defined using custom inference rules using SPARQL via . """ ; wdrs:describedby <#> . ## Entity Relationships ## a <#RoyalPerson>, <#MalePerson> ; schema:name "Prince William" ; rel:siblingOf . a <#RoyalPerson>, <#FemalePerson> ; schema:name "Queen Mother" ; rel:parentOf . a <#RoyalPerson>, <#FemalePerson> ; schema:name "Queen Elizabeth II" ; rel:parentOf , , , ; rel:siblingOf . a <#RoyalPerson>, <#MalePerson> ; schema:name "Prince Charles" ; rel:parentOf , . a <#RoyalPerson>, <#FemalePerson> ; schema:name "Princess Margaret" . a <#RoyalPerson>, <#FemalePerson> ; schema:name "Princess Anne" ; rel:parentOf , . a <#RoyalPerson>, <#FemalePerson> ; schema:name "Princess Zara Phillips" . a <#RoyalPerson>, <#FemalePerson> ; schema:name "Princess Beatrice" . a <#RoyalPerson>, <#FemalePerson> ; schema:name "Princess Eugenie" . a <#RoyalPerson>, <#MalePerson> ; schema:name "Prince Andrew" ; rel:parentOf , . a <#RoyalPerson>, <#MalePerson> ; schema:name "Prince Edward" . a <#RoyalPerson>, <#MalePerson> ; schema:name "Prince Harry" . a <#RoyalPerson>, <#MalePerson> ; schema:name "Prince Peter Phillips" . } ; -- Load Relationship Vocabulary Data SPARQL CLEAR GRAPH ; SPARQL DEFINE get:soft "no-sponge" SELECT * FROM WHERE { ?s ?p ?o }; -- Custom Inference Rules using SPIN -- For newer release of SPIN Engine -- SPARQL DROP SPIN LIBRARY ; SPARQL DROP MACRO LIBRARY ; SPARQL PREFIX rel: CREATE SPIN LIBRARY { CLASS <#RoyalPerson> { comment """ Class instance generated from a Macro that customizes relations such as rel:siblingOf while introducing new relations such as <#hasAuntie>, <#hasUncle>, and <#hasCousin> . """ defmacro rel:ancestorOf { ?this rel:ancestorOf ?n } {{ { ?this rel:parentOf+ ?n . } UNION { ?this rel:siblingOf ?n . } FILTER ( NOT EXISTS {?this rel:siblingOf|^rel:siblingOf ?n} ) }} defmacro rel:descendantOf { ?this rel:descendantOf ?n } {{ { ?this ^rel:parentOf+ ?n . } }} defmacro rel:sibingOf { ?this rel:siblingOf ?n } {{ [] rel:parentOf ?this, ?n. FILTER (?n != ?this) . }} defmacro <#hasAuntie> { ?this <#hasAuntie> ?n } {{ [] rel:parentOf ?this ; # rel:siblingOf | ^rel:sinblingOf ?n . rel:siblingOf ?n . ?n a <#FemalePerson> . }} defmacro <#hasUncle> { ?this <#hasUncle> ?n } {{ [] rel:parentOf ?this ; # rel:siblingOf | ^rel:sinblingOf ?n . rel:siblingOf ?n . ?n a <#MalePerson> . }} defmacro <#hasCousin> { ?this <#hasCousin> ?n } {{ [] rel:parentOf ?n ; rel:siblingOf [ rel:parentOf ?this ] . }} } } ASK FROM virtrdf: WHERE { ?s ?o } ; -- Test 1 SPARQL DEFINE input:macro-lib PREFIX rel: WITH SELECT ?person rel:ancestorOf as ?relation ?descendant WHERE { ?person a <#RoyalPerson> ; rel:ancestorOf ?descendant } ; -- Test 2 SPARQL DEFINE input:macro-lib PREFIX rel: WITH SELECT DISTINCT ?person rel:siblingOf as ?relation ?siblingOf WHERE { ?person a <#RoyalPerson> ; rel:siblingOf ?siblingOf } ; -- Test 3 SPARQL DEFINE input:macro-lib PREFIX rel: WITH SELECT ?person <#hasAuntie> as ?relation ?hasAuntie WHERE { ?person a <#RoyalPerson> ; <#hasAuntie> ?hasAuntie } ; -- Test 4 SPARQL DEFINE input:macro-lib PREFIX rel: WITH SELECT ?person <#hasUncle> as ?relation ?hasUncle WHERE { ?person a <#RoyalPerson> ; <#hasUncle> ?hasUncle } ; -- Test 5 SPARQL DEFINE input:macro-lib PREFIX rel: WITH SELECT ?person <#hasCousin> as ?relation ?hasCousin WHERE { ?person a <#RoyalPerson> ; <#hasCousin> ?hasCousin } ; -- Test 6 SPARQL DEFINE input:macro-lib PREFIX rel: WITH SELECT ?s as ?ancestor ?descendant WHERE { ?s a <#RoyalPerson> ; rel:ancestorOf ?descendant }; -- Test 7 SPARQL DEFINE input:macro-lib PREFIX rel: WITH SELECT DISTINCT * WHERE { { ?parent a <#RoyalPerson> ; rel:parentOf ?parentOf .} UNION { ?person a <#RoyalPerson>; <#hasAuntie> ?hasAuntie .} UNION { ?person a <#RoyalPerson>; <#hasUncle> ?hasUncle .} UNION { ?person a <#RoyalPerson>; <#hasCousin> ?hasCousin .} UNION { ?person a <#RoyalPerson>; rel:siblingOf ?hasSibling . } UNION { ?person a <#RoyalPerson>; rel:ancestorOf ?hasDescendant . } } ; COMMIT WORK ;