KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > joseki > util > Closure


1 /*
2  * (c) Copyright 2003, 2004 Hewlett-Packard Development Company, LP
3  * [See end of file]
4  */

5
6 /** Code to calculate the transitive-bNode closures
7  * @author Andy Seaborne
8  * @version $Id: Closure.java,v 1.7 2004/04/30 14:13:13 andy_seaborne Exp $
9  */

10
11 package org.joseki.util;
12 import java.util.* ;
13 import com.hp.hpl.jena.rdf.model.* ;
14 import org.apache.commons.logging.* ;
15
16 public class Closure
17 {
18     private static Log logger = LogFactory.getLog(Closure.class.getName()) ;
19
20     /** Calculate the bNode closure from a statement .
21      * The Statement itself does not automatically get included.
22      * @param stmt
23      * @return A model containing statements
24      */

25
26     public static Model closure(Statement stmt)
27     {
28         return closure(stmt, new ClosureBNode()) ;
29     }
30
31     /** Calculate the bNode closure from a statement .
32       * The Statement itself does not automatically get included.
33       * @param statement Starting point for the closure.
34       * @param test The test object to be applied
35       * @return A model containing statements
36       * @see ClosureTest
37       */

38
39     public static Model closure(Statement statement, ClosureTest test)
40     {
41         return closure(statement, test, ModelFactory.createDefaultModel()) ;
42     }
43
44     /** Calculate the bNode closure from a statement .
45       * The Statement itself does not automatically get included.
46       * @param statement Starting point for the closure.
47       * @param model Add the statements to this model
48       * @return A model containing statements
49       * @see ClosureTest
50       */

51
52     public static Model closure(Statement statement, Model model)
53     {
54         return closure(statement, new ClosureBNode(), model) ;
55     }
56     
57     /** Calculate the bNode closure from a statement .
58       * The Statement itself does not automatically get included.
59       * @param statement Starting point for the closure.
60       * @param test The test object to be applied
61       * @param model Add the statements to this model
62       * @return A model containing statements
63       * @see ClosureTest
64       */

65
66     public static Model closure(Statement statement, ClosureTest test, Model model)
67     {
68         if ( logger.isDebugEnabled() )
69             logger.debug("closure: "+statement) ;
70         //Set visited = new HashSet() ;
71
List visited = new ArrayList() ;
72
73         closure(statement, model, visited, test) ;
74         return model ;
75     }
76
77     /** Calculate the bNode closure from a resource.
78      * The Statement itself does not automatically get included.
79      * @param resource Starting point for the closure.
80      * @param testThisNode Indicate whether to apply the closure test to the Resource argument.
81      * @return A model containing statements
82      */

83
84     public static Model closure(Resource resource, boolean testThisNode)
85     {
86         return closure(resource, new ClosureBNode(), testThisNode) ;
87     }
88
89     /** Calculate the bNode closure from a resource .
90      * The Statement itself does not automatically get included.
91      * @param resource
92      * @param test The test object to be applied
93      * @param testThisNode Indicate whether to apply the closure test to the Resource argument.
94      * @return A model containing statements
95      */

96
97     public static Model closure(Resource resource, ClosureTest test, boolean testThisNode)
98     {
99         return closure(resource, test, testThisNode, ModelFactory.createDefaultModel()) ;
100     }
101
102
103     /** Calculate the bNode closure from a resource .
104      * The Statement itself does not automatically get included.
105      * @param resource
106      * @param testThisNode Indicate whether to apply the closure test to the Resource argument.
107      * @param results Add the statements to this model
108      * @return A model containing statements
109      */

110
111     public static Model closure(Resource resource, boolean testThisNode, Model results)
112     {
113         return closure(resource, new ClosureBNode(), testThisNode, results) ;
114     }
115     
116     
117     /** Calculate the bNode closure from a resource .
118      * The Statement itself does not automatically get included.
119      * @param resource
120      * @param test The test object to be applied
121      * @param testThisNode Indicate whether to apply the closure test to the Resource argument.
122      * @param results Add the statements to this model
123      * @return A model containing statements
124      */

125
126     public static Model closure(Resource resource, ClosureTest test,
127                                 boolean testThisNode, Model results)
128     {
129
130         if ( logger.isDebugEnabled() )
131             logger.debug("closure: "+resource) ;
132         //Set s = new HashSet() ;
133
//Set visited = new HashSet() ;
134
List visited = new ArrayList() ;
135         
136         if ( ! testThisNode )
137             closureNoTest(resource, results, visited, test) ;
138         else
139             closure(resource, results, visited, test) ;
140         return results ;
141     }
142
143
144
145     // --------------------------------------------------------------------------------
146

147     private static void closure(Statement stmt,
148                                 Model closureBlob, Collection visited,
149                                 ClosureTest test)
150     {
151         if ( logger.isDebugEnabled() )
152             logger.trace(stmt.toString()) ;
153         if ( test.includeStmt(stmt) )
154                 closureBlob.add(stmt) ;
155         closure(stmt.getSubject(), closureBlob, visited, test) ;
156         closure(stmt.getObject(), closureBlob, visited, test) ;
157     }
158
159
160     private static void closure(RDFNode n,
161                                 Model closureBlob, Collection visited,
162                                 ClosureTest test)
163     {
164         if ( ! ( n instanceof Resource ) )
165         {
166             if ( logger.isTraceEnabled() )
167                 logger.trace(" Not a resource: "+n) ;
168             return ;
169         }
170
171         Resource r = (Resource)n ;
172
173         if ( visited.contains(r) )
174         {
175             if ( logger.isTraceEnabled() )
176                 logger.trace(" Already visited: "+r) ;
177             return ;
178         }
179
180         if ( ! test.traverse(r) )
181         {
182             if ( logger.isTraceEnabled() )
183                 logger.trace(" End traverse: "+r) ;
184             return ;
185         }
186
187         closureNoTest(r, closureBlob, visited, test) ;
188     }
189     
190      
191     private static void closureNoTest(Resource r,
192                                       Model closureBlob, Collection visited,
193                                       ClosureTest test)
194     {
195         if (logger.isTraceEnabled() )
196             logger.trace("@ "+r+" :: "+closureBlob.size()+" //"+dbg_string(visited));
197
198         visited.add(r) ;
199
200         StmtIterator sIter = r.listProperties() ;
201         for ( ; sIter.hasNext() ; )
202         {
203             Statement stmt = sIter.nextStatement() ;
204             closure(stmt, closureBlob, visited, test) ;
205         }
206     }
207
208
209     private static String JavaDoc dbg_string(Collection s)
210     {
211         String JavaDoc tmp = "" ;
212         for ( Iterator iter = s.iterator() ; iter.hasNext() ; )
213         {
214             tmp = tmp+" "+iter.next().toString() ;
215         }
216         return tmp ;
217     }
218
219     // Defines the bNode closure
220

221     public static class ClosureBNode implements ClosureTest
222     {
223         public boolean traverse(Resource r)
224         {
225             return r.isAnon() ;
226         }
227
228         public boolean includeStmt(Statement s)
229         {
230             return true ;
231         }
232     }
233
234     // Defines the reachable (on forwrd arcs) subgraph.
235

236     public static class ClosureReachable implements ClosureTest
237     {
238         public boolean traverse(Resource r)
239         {
240             return true ;
241         }
242
243         public boolean includeStmt(Statement s)
244         {
245             return true ;
246         }
247     }
248     
249
250 }
251
252
253 /*
254  * (c) Copyright 2003, 2004 Hewlett-Packard Development Company, LP
255  * All rights reserved.
256  *
257  * Redistribution and use in source and binary forms, with or without
258  * modification, are permitted provided that the following conditions
259  * are met:
260  * 1. Redistributions of source code must retain the above copyright
261  * notice, this list of conditions and the following disclaimer.
262  * 2. Redistributions in binary form must reproduce the above copyright
263  * notice, this list of conditions and the following disclaimer in the
264  * documentation and/or other materials provided with the distribution.
265  * 3. The name of the author may not be used to endorse or promote products
266  * derived from this software without specific prior written permission.
267  *
268  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
269  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
270  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
271  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
272  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
273  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
274  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
275  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
276  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
277  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
278  */

279
280
Popular Tags