From: Eric Prud'h. <er...@w3...> - 2010-08-16 18:42:27
|
Someone who feels like enhancing this can record a nice tutorial in the SWObjects wiki. Suppose someone wants to ask a question which spans a bunch of query services, but doesn't (care to) enumerate those resources. The ChainingMapper takes rules (currently written as SPARQL CONSTRUCTs) and maps a query over the consequents of those rules to a query over the antecedents of those rules. This can be used to partition queries into SERVICE graphs. For a terse example, imagine service S12 includes triples with predicates <p1> and <p2>, and S3 includes predicates <p3>. Voila a SPARQL invocation and tranformed query: Query and mapping rules: SPARQL -npq \ -M 'CONSTRUCT { ?s <p1> ?o1 ; <p2> ?o2 } WHERE { SERVICE <S12> { ?s <p1> ?o1 ; <p2> ?o2 } }' \ -M 'CONSTRUCT { ?s <p3> ?o } WHERE { SERVICE <S3> { ?s <p3> ?o } }' \ -e 'SELECT * WHERE { ?x1 <p1> ?n1 ; <p2> ?n2 ; <p3> ?n3 }' Transformed query: SELECT * WHERE { SERVICE <S12> { ?x1 <p1> ?n1 . ?x1 <p2> ?n2 } SERVICE <S3> { ?x1 <p3> ?n3 } } If the system has a list of SERVICE graphs, then the user just has to ask the base question and the systems handles query federation. What if some predicates can be obtained from multiple endpoints? SPARQL -npq \ -M 'CONSTRUCT { ?s <p1> ?o1 ; <p2> ?o2 } WHERE { SERVICE <S12> { ?s <p1> ?o1 ; <p2> ?o2 } }' \ -M 'CONSTRUCT { ?s <p2> ?o2 ; <p3> ?o3 } WHERE { SERVICE <S23> { ?s <p2> ?o2 ; <p3> ?o3 } }' \ -e 'SELECT * WHERE { ?x1 <p1> ?n1 ; <p2> ?n2 ; <p3> ?n3 }' Now we expect <p2> to come from both S12 and S23: SELECT * WHERE { { SERVICE <S12> { ?x1 <p1> ?n1 . ?x1 <p2> ?n2 } SERVICE <S23> { ?x1 <p2> ?_0x8752540_0_o2 . ?x1 <p3> ?n3 } } UNION { SERVICE <S12> { ?x1 <p1> ?n1 . ?x1 <p2> ?_0x874df10_1_o2 } SERVICE <S23> { ?x1 <p2> ?n2 . ?x1 <p3> ?n3 } } } So our query for predicates <p1> <p2> <p3> could be answered by <S12> providing <p1> <p2> and <S23> providing <p3>, or by <p2> coming from <S23> (hence the UNION). What are those odd variables like _0x8752540_0_o2 ? In order to be sound with respect to the rules, the ChainingMapper has to ensure that the complete antecedent of each query is satisfied. Looking at the first side of the UNION, <S23> is expected to match { ?x <p2> ?o2; <p3> ?o3 }, so the generated query must make sure that for any { ?x1 <p3> ?n3 }, ?x1 also has a property <p2> (though the value is unimportant). Since those rules are two course, we can write more, simpler rules: SPARQL -npq \ -M 'CONSTRUCT { ?s <p1> ?o1 } WHERE { SERVICE <S12> { ?s <p1> ?o1 } }' \ -M 'CONSTRUCT { ?s <p2> ?o2 } WHERE { SERVICE <S12> { ?s <p2> ?o2 } }' \ -M 'CONSTRUCT { ?s <p2> ?o2 } WHERE { SERVICE <S23> { ?s <p2> ?o2 } }' \ -M 'CONSTRUCT { ?s <p3> ?o3 } WHERE { SERVICE <S23> { ?s <p3> ?o3 } }' \ -e 'SELECT * WHERE { ?x1 <p1> ?n1 ; <p2> ?n2 ; <p3> ?n3 }' and see a simpler federated query: SELECT * WHERE { { SERVICE <S12> { ?x1 <p1> ?n1 } SERVICE <S12> { ?x1 <p2> ?n2 } SERVICE <S23> { ?x1 <p3> ?n3 } } UNION { SERVICE <S12> { ?x1 <p1> ?n1 } SERVICE <S23> { ?x1 <p3> ?n3 } SERVICE <S23> { ?x1 <p2> ?n2 } } } I got this related Q: > Let's say we have two endpoints - S1 and S2 - registered with the federator. > > Triples in S1: > > ?x rdf:type ?y > > Triples in S2: > > ?a rdf:type foaf:person > ?b foaf:mbox ?c > > Now, I get a query: > > Select ?x1 > Where { > ?x1 rdf:type foaf:person > ?x1 foaf:mbox ?y1 > } > > What does the mapper do in this instance? Is there a way to tell the > mapper to look only at S2 (or only S1 to get bindings for the first > triple in the where clause)? Or, is the federator expected to return > the most complete answer by doing a union across both endpoints? We can test this: Query: SPARQL -npq \ -M 'CONSTRUCT { ?x a ?y } WHERE { SERVICE <S1> { ?x a ?y } }' \ -M 'CONSTRUCT { ?a a <person> . ?b <mbox> ?c } WHERE { SERVICE <S2> { ?a a <person> . ?b <mbox> ?c } }' \ -e 'SELECT ?x1 WHERE { ?x1 a <person> . ?x1 <mbox> ?y1 }' and get the expected UNION because two services can produce rdf:type arcs: Result (query): SELECT ?x1 WHERE { { SERVICE <S1> { ?x1 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <person> . } SERVICE <S2> { ?_0x8580540_0_a <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <person> . ?x1 <mbox> ?y1 . } } UNION SERVICE <S2> { ?x1 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <person> . ?x1 <mbox> ?y1 . } } > -Matt > > -- -ericP |