KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > reasoner > FinderUtil


1 /******************************************************************
2  * File: FinderUtil.java
3  * Created by: Dave Reynolds
4  * Created on: 18-Jan-03
5  *
6  * (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
7  * [See end of file]
8  * $Id: FinderUtil.java,v 1.8 2005/02/21 12:16:14 andy_seaborne Exp $
9  *****************************************************************/

10 package com.hp.hpl.jena.reasoner;
11
12 import com.hp.hpl.jena.util.iterator.*;
13
14 /**
15  * Some simple helper methods used when working with Finders,
16  * particularly to compose them into cascade sequences.
17  * The cascades are designed to cope with null Finders as well.
18  *
19  * @author <a HREF="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
20  * @version $Revision: 1.8 $ on $Date: 2005/02/21 12:16:14 $
21  */

22 public class FinderUtil {
23     
24     /**
25      * Create a continuation object which is a cascade of two
26      * continuation objects.
27      * @param first the first Graph/Finder to try
28      * @param second the second Graph/Finder to try
29      */

30     public static Finder cascade(Finder first, Finder second) {
31         if (first == null || (first instanceof FGraph && ((FGraph)first).getGraph() == null)) return second;
32         if (second == null || (second instanceof FGraph && ((FGraph)second).getGraph() == null)) return first;
33         return new Cascade(first, second);
34     }
35     
36     /**
37      * Create a continuation object which is a cascade of three
38      * continuation objects.
39      * @param first the first Graph/Finder to try
40      * @param second the second Graph/Finder to try
41      * @param third the third Graph/Finder to try
42      */

43     public static Finder cascade(Finder first, Finder second, Finder third) {
44         return new Cascade(first, cascade(second, third));
45     }
46     
47     /**
48      * Create a continuation object which is a cascade of four
49      * continuation objects.
50      * @param first the first Graph/Finder to try
51      * @param second the second Graph/Finder to try
52      * @param third the third Graph/Finder to try
53      * @param fourth the third Graph/Finder to try
54      */

55     public static Finder cascade(Finder first, Finder second, Finder third, Finder fourth) {
56         return new Cascade(first, cascade(second, cascade(third, fourth)));
57     }
58     
59     /**
60      * Inner class used to implement cascades of two continuation objects
61      */

62     private static class Cascade implements Finder {
63         /** the first Graph/Finder to try */
64         Finder first;
65         
66         /** the second Graph/Finder to try */
67         Finder second;
68         
69         /**
70          * Constructor
71          */

72         Cascade(Finder first, Finder second) {
73             this.first = first;
74             this.second = second;
75         }
76         
77         /**
78          * Basic pattern lookup interface.
79          * @param pattern a TriplePattern to be matched against the data
80          * @return a ClosableIterator over all Triples in the data set
81          * that match the pattern
82          */

83         public ExtendedIterator find(TriplePattern pattern) {
84             if (second == null) {
85                 return first.find(pattern);
86             } else if (first == null) {
87                 return second.find(pattern);
88             } else {
89                 return first.findWithContinuation(pattern, second);
90             }
91         }
92         
93         /**
94          * Extended find interface used in situations where the implementator
95          * may or may not be able to answer the complete query. It will
96          * attempt to answer the pattern but if its answers are not known
97          * to be complete then it will also pass the request on to the nested
98          * Finder to append more results.
99          * @param pattern a TriplePattern to be matched against the data
100          * @param continuation either a Finder or a normal Graph which
101          * will be asked for additional match results if the implementor
102          * may not have completely satisfied the query.
103          */

104         public ExtendedIterator findWithContinuation(TriplePattern pattern, Finder continuation) {
105             return (FinderUtil.cascade(first, second, continuation)).find(pattern);
106         }
107
108         /**
109          * Return true if the given pattern occurs somewhere in the find sequence.
110          */

111         public boolean contains(TriplePattern pattern) {
112             ClosableIterator it = find(pattern);
113             boolean result = it.hasNext();
114             it.close();
115             return result;
116         }
117
118     }
119 }
120
121 /*
122     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
123     All rights reserved.
124
125     Redistribution and use in source and binary forms, with or without
126     modification, are permitted provided that the following conditions
127     are met:
128
129     1. Redistributions of source code must retain the above copyright
130        notice, this list of conditions and the following disclaimer.
131
132     2. Redistributions in binary form must reproduce the above copyright
133        notice, this list of conditions and the following disclaimer in the
134        documentation and/or other materials provided with the distribution.
135
136     3. The name of the author may not be used to endorse or promote products
137        derived from this software without specific prior written permission.
138
139     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
140     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
141     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
142     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
143     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
144     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
145     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
146     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
147     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
148     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
149 */

150
151
Popular Tags