1 6 7 package com.hp.hpl.jena.util; 8 9 import com.hp.hpl.jena.rdf.model.*; 10 import com.hp.hpl.jena.graph.*; 11 import com.hp.hpl.jena.graph.query.*; 12 13 19 public class QueryMapper 20 { 21 private Query query; 22 private Node [] variables; 23 private Graph graph; 24 25 public QueryMapper( Model m, Resource [] variables ) 26 { 27 super(); 28 this.graph = toQueryGraph( m ); 29 this.query = new Query( this.graph ); 30 this.variables = toQueryVariables( variables ); 31 } 32 33 public Node [] getVariables() 34 { return variables; } 35 36 public Query getQuery() 37 { return query; } 38 39 public Graph getGraph() 40 { return graph; } 41 42 public Graph toQueryGraph( Model m ) 43 { 44 StmtIterator st = m.listStatements(); 45 Graph result = Factory.createDefaultGraph(); 46 while (st.hasNext()) result.add( toQueryTriple( st.nextStatement() ) ); 47 return result; 48 } 49 50 public Triple toQueryTriple( Statement s ) 51 { 52 return Triple.create 53 ( 54 toQueryNode( s.getSubject() ), 55 toQueryNode( s.getPredicate() ), 56 toQueryNode( s.getObject() ) 57 ); 58 } 59 60 public Node [] toQueryVariables( Resource [] vars ) 61 { 62 Node [] result = new Node[vars.length]; 63 for (int i = 0; i < vars.length; i += 1) result[i] = toQueryNode( vars[i] ); 64 return result; 65 } 66 67 static final String varPrefix = "jqv:"; 68 69 public Node toQueryNode( RDFNode rn ) 70 { 71 Node n = rn.asNode(); 72 return n.isURI() && n.getURI().startsWith( varPrefix ) 73 ? Node.createVariable( n.getURI().substring( varPrefix.length() ) ) 74 : n; 75 } 76 77 } 78 79 80 | Popular Tags |