001: /*
002: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: [See end of file]
004: $Id: QueryMapper.java,v 1.7 2008/01/02 12:07:43 andy_seaborne Exp $
005: */
006:
007: package com.hp.hpl.jena.util;
008:
009: import com.hp.hpl.jena.rdf.model.*;
010: import com.hp.hpl.jena.graph.*;
011: import com.hp.hpl.jena.graph.query.*;
012:
013: /**
014: Utility class for <code>ModelQueryUtil</code> which converts a query represented
015: by a model with jqv: variables into a <code>.graph.query.Query</code> object.
016:
017: @author kers
018: */
019: public class QueryMapper {
020: private Query query;
021: private Node[] variables;
022: private Graph graph;
023:
024: public QueryMapper(Model m, Resource[] variables) {
025: super ();
026: this .graph = toQueryGraph(m);
027: this .query = new Query(this .graph);
028: this .variables = toQueryVariables(variables);
029: }
030:
031: public Node[] getVariables() {
032: return variables;
033: }
034:
035: public Query getQuery() {
036: return query;
037: }
038:
039: public Graph getGraph() {
040: return graph;
041: }
042:
043: public Graph toQueryGraph(Model m) {
044: StmtIterator st = m.listStatements();
045: Graph result = Factory.createDefaultGraph();
046: while (st.hasNext())
047: result.add(toQueryTriple(st.nextStatement()));
048: return result;
049: }
050:
051: public Triple toQueryTriple(Statement s) {
052: return Triple.create(toQueryNode(s.getSubject()), toQueryNode(s
053: .getPredicate()), toQueryNode(s.getObject()));
054: }
055:
056: public Node[] toQueryVariables(Resource[] vars) {
057: Node[] result = new Node[vars.length];
058: for (int i = 0; i < vars.length; i += 1)
059: result[i] = toQueryNode(vars[i]);
060: return result;
061: }
062:
063: static final String varPrefix = "jqv:";
064:
065: public Node toQueryNode(RDFNode rn) {
066: Node n = rn.asNode();
067: return n.isURI() && n.getURI().startsWith(varPrefix) ? Node
068: .createVariable(n.getURI()
069: .substring(varPrefix.length())) : n;
070: }
071:
072: }
073:
074: /*
075: (c) Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
076: All rights reserved.
077:
078: Redistribution and use in source and binary forms, with or without
079: modification, are permitted provided that the following conditions
080: are met:
081:
082: 1. Redistributions of source code must retain the above copyright
083: notice, this list of conditions and the following disclaimer.
084:
085: 2. Redistributions in binary form must reproduce the above copyright
086: notice, this list of conditions and the following disclaimer in the
087: documentation and/or other materials provided with the distribution.
088:
089: 3. The name of the author may not be used to endorse or promote products
090: derived from this software without specific prior written permission.
091:
092: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
093: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
094: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
095: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
096: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
097: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
098: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
099: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
100: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
101: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
102: */
|