KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > graph > query > SimpleQueryHandler


1 /*
2   (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3   [See end of file]
4   $Id: SimpleQueryHandler.java,v 1.22 2005/02/21 11:52:25 andy_seaborne Exp $
5 */

6
7 package com.hp.hpl.jena.graph.query;
8
9 import com.hp.hpl.jena.graph.*;
10 import com.hp.hpl.jena.util.CollectionFactory;
11 import com.hp.hpl.jena.util.iterator.*;
12
13 import java.util.*;
14
15 /**
16     A SimpleQueryHandler is a more-or-less straightforward implementation of QueryHandler
17     suitable for use on graphs with no special query engines.
18     
19     @author kers
20 */

21
22 public class SimpleQueryHandler implements QueryHandler
23     {
24     /** the Graph this handler is working for */
25     protected Graph graph;
26     
27     /** make an instance, remember the graph */
28     public SimpleQueryHandler( Graph graph )
29         { this.graph = graph; }
30
31     public Stage patternStage( Mapping map, ExpressionSet constraints, Triple [] t )
32         { return new PatternStage( graph, map, constraints, t ); }
33                 
34     public BindingQueryPlan prepareBindings( Query q, Node [] variables )
35         { return new SimpleQueryPlan( graph, q, variables ); }
36         
37     public TreeQueryPlan prepareTree( Graph pattern )
38         { return new SimpleTreeQueryPlan( graph, pattern ); }
39         
40     public ExtendedIterator objectsFor( Node s, Node p )
41         { return objectsFor( graph, s, p ); }
42         
43     public ExtendedIterator subjectsFor( Node p, Node o )
44         { return subjectsFor( graph, p, o ); }
45     
46     public ExtendedIterator predicatesFor( Node s, Node o )
47         { return predicatesFor( graph, s, o ); }
48     
49     public static ExtendedIterator objectsFor( Graph g, Node s, Node p )
50         {
51         Set objects = CollectionFactory.createHashedSet();
52         ClosableIterator it = g.find( s, p, Node.ANY );
53         while (it.hasNext()) objects.add( ((Triple) it.next()).getObject() );
54         return WrappedIterator.createNoRemove( objects.iterator() );
55         }
56         
57     public static ExtendedIterator subjectsFor( Graph g, Node p, Node o )
58         {
59         Set objects = CollectionFactory.createHashedSet();
60         ClosableIterator it = g.find( Node.ANY, p, o );
61         while (it.hasNext()) objects.add( ((Triple) it.next()).getSubject() );
62         return WrappedIterator.createNoRemove( objects.iterator() );
63         }
64     
65     public static ExtendedIterator predicatesFor( Graph g, Node s, Node o )
66         {
67         Set predicates = CollectionFactory.createHashedSet();
68         ClosableIterator it = g.find( s, Node.ANY, o );
69         while (it.hasNext()) predicates.add( ((Triple) it.next()).getPredicate() );
70         return WrappedIterator.createNoRemove( predicates.iterator() );
71         }
72         
73     /**
74         this is a simple-minded implementation of containsNode that uses find
75         up to three times to locate the node. Almost certainly particular graphs
76         will be able to offer better query-handlers ...
77     */

78     public boolean containsNode( Node n )
79         {
80         return
81             graph.contains( n, Node.ANY, Node.ANY )
82             || graph.contains( Node.ANY, n, Node.ANY )
83             || graph.contains( Node.ANY, Node.ANY, n )
84             ;
85         }
86     }
87
88 /*
89     (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
90     All rights reserved.
91
92     Redistribution and use in source and binary forms, with or without
93     modification, are permitted provided that the following conditions
94     are met:
95
96     1. Redistributions of source code must retain the above copyright
97        notice, this list of conditions and the following disclaimer.
98
99     2. Redistributions in binary form must reproduce the above copyright
100        notice, this list of conditions and the following disclaimer in the
101        documentation and/or other materials provided with the distribution.
102
103     3. The name of the author may not be used to endorse or promote products
104        derived from this software without specific prior written permission.
105
106     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
107     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
108     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
109     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
110     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
111     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
112     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
113     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
114     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
115     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
116 */

117
Popular Tags