01: /*
02: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
03: [See end of file]
04: $Id: ModelQueryUtil.java,v 1.10 2008/01/02 12:07:43 andy_seaborne Exp $
05: */
06:
07: package com.hp.hpl.jena.util;
08:
09: import com.hp.hpl.jena.rdf.model.*;
10: import com.hp.hpl.jena.graph.*;
11: import com.hp.hpl.jena.util.iterator.*;
12:
13: import java.util.*;
14:
15: /**
16: A utility for using the graph query interface from a Model. Queries may be represented
17: as models, where each statement in the model corresponds to a search for matching
18: statements in the model being queried. Variables are represented as resources
19: with URIs using the ficticious "jqv" protocol.
20: <p>
21: See also <code>QueryMapper</code>.
22:
23: @author kers
24: */
25: public class ModelQueryUtil {
26: private ModelQueryUtil() {
27: }
28:
29: public static ExtendedIterator queryBindingsWith(final Model model,
30: Model query, Resource[] variables) {
31: Map1 mm = new Map1() {
32: public Object map1(Object x) {
33: return mappy(model, x);
34: }
35: };
36: QueryMapper qm = new QueryMapper(query, variables);
37: return qm.getQuery().executeBindings(model.getGraph(),
38: qm.getVariables()).mapWith(mm);
39: }
40:
41: public static RDFNode asRDF(Model m, Node n) {
42: return m.asRDFNode(n);
43: }
44:
45: public static List mappy(Model m, Object x) {
46: List L = (List) x;
47: ArrayList result = new ArrayList(L.size());
48: for (int i = 0; i < L.size(); i += 1)
49: result.add(asRDF(m, (Node) L.get(i)));
50: return result;
51: }
52:
53: }
54:
55: /*
56: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
57: All rights reserved.
58:
59: Redistribution and use in source and binary forms, with or without
60: modification, are permitted provided that the following conditions
61: are met:
62:
63: 1. Redistributions of source code must retain the above copyright
64: notice, this list of conditions and the following disclaimer.
65:
66: 2. Redistributions in binary form must reproduce the above copyright
67: notice, this list of conditions and the following disclaimer in the
68: documentation and/or other materials provided with the distribution.
69:
70: 3. The name of the author may not be used to endorse or promote products
71: derived from this software without specific prior written permission.
72:
73: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
74: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
75: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
76: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
77: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
78: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
79: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
80: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
81: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
82: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
83: */
|