001: /*
002: (c) Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: [See end of file]
004: $Id: SimpleQueryHandler.java,v 1.25 2008/01/02 12:07:57 andy_seaborne Exp $
005: */
006:
007: package com.hp.hpl.jena.graph.query;
008:
009: import com.hp.hpl.jena.graph.*;
010: import com.hp.hpl.jena.util.CollectionFactory;
011: import com.hp.hpl.jena.util.iterator.*;
012:
013: import java.util.*;
014:
015: /**
016: A SimpleQueryHandler is a more-or-less straightforward implementation of QueryHandler
017: suitable for use on graphs with no special query engines.
018:
019: @author kers
020: */
021:
022: public class SimpleQueryHandler implements QueryHandler {
023: /** the Graph this handler is working for */
024: protected Graph graph;
025:
026: /** make an instance, remember the graph */
027: public SimpleQueryHandler(Graph graph) {
028: this .graph = graph;
029: }
030:
031: public Stage patternStage(Mapping map, ExpressionSet constraints,
032: Triple[] t) {
033: return new PatternStage(graph, map, constraints, t);
034: }
035:
036: public BindingQueryPlan prepareBindings(Query q, Node[] variables) {
037: return new SimpleQueryPlan(graph, q, variables);
038: }
039:
040: public TreeQueryPlan prepareTree(Graph pattern) {
041: return new SimpleTreeQueryPlan(graph, pattern);
042: }
043:
044: public ExtendedIterator objectsFor(Node s, Node p) {
045: return objectsFor(graph, s, p);
046: }
047:
048: public ExtendedIterator subjectsFor(Node p, Node o) {
049: return subjectsFor(graph, p, o);
050: }
051:
052: public ExtendedIterator predicatesFor(Node s, Node o) {
053: return predicatesFor(graph, s, o);
054: }
055:
056: public static ExtendedIterator objectsFor(Graph g, Node s, Node p) {
057: Set objects = CollectionFactory.createHashedSet();
058: ClosableIterator it = g.find(s, p, Node.ANY);
059: while (it.hasNext())
060: objects.add(((Triple) it.next()).getObject());
061: return WrappedIterator.createNoRemove(objects.iterator());
062: }
063:
064: public static ExtendedIterator subjectsFor(Graph g, Node p, Node o) {
065: Set objects = CollectionFactory.createHashedSet();
066: ClosableIterator it = g.find(Node.ANY, p, o);
067: while (it.hasNext())
068: objects.add(((Triple) it.next()).getSubject());
069: return WrappedIterator.createNoRemove(objects.iterator());
070: }
071:
072: public static ExtendedIterator predicatesFor(Graph g, Node s, Node o) {
073: Set predicates = CollectionFactory.createHashedSet();
074: ClosableIterator it = g.find(s, Node.ANY, o);
075: while (it.hasNext())
076: predicates.add(((Triple) it.next()).getPredicate());
077: return WrappedIterator.createNoRemove(predicates.iterator());
078: }
079:
080: /**
081: this is a simple-minded implementation of containsNode that uses find
082: up to three times to locate the node. Almost certainly particular graphs
083: will be able to offer better query-handlers ...
084: */
085: public boolean containsNode(Node n) {
086: return graph.contains(n, Node.ANY, Node.ANY)
087: || graph.contains(Node.ANY, n, Node.ANY)
088: || graph.contains(Node.ANY, Node.ANY, n);
089: }
090: }
091:
092: /*
093: (c) Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
094: All rights reserved.
095:
096: Redistribution and use in source and binary forms, with or without
097: modification, are permitted provided that the following conditions
098: are met:
099:
100: 1. Redistributions of source code must retain the above copyright
101: notice, this list of conditions and the following disclaimer.
102:
103: 2. Redistributions in binary form must reproduce the above copyright
104: notice, this list of conditions and the following disclaimer in the
105: documentation and/or other materials provided with the distribution.
106:
107: 3. The name of the author may not be used to endorse or promote products
108: derived from this software without specific prior written permission.
109:
110: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
111: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
112: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
113: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
114: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
115: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
116: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
117: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
118: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
119: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
120: */
|