KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2   (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3   [See end of file]
4   $Id: SimpleTreeQueryPlan.java,v 1.9 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 import com.hp.hpl.jena.mem.*;
13 import java.util.*;
14
15 /**
16     Incomplete class. Do not use.
17 */

18 public class SimpleTreeQueryPlan implements TreeQueryPlan
19     {
20     private Graph pattern;
21     private Graph target;
22     
23     public SimpleTreeQueryPlan( Graph target, Graph pattern )
24         {
25         this.target = target;
26         this.pattern = pattern;
27         }
28         
29     public Graph executeTree()
30         {
31         Graph result = new GraphMem();
32         Set roots = getRoots( pattern );
33         for (Iterator it = roots.iterator(); it.hasNext(); handleRoot( result, (Node) it.next(), CollectionFactory.createHashedSet())) {}
34         return result;
35         }
36         
37     private Iterator findFromTriple( Graph g, Triple t )
38         {
39         return g.find( asPattern( t.getSubject() ), asPattern( t.getPredicate() ), asPattern( t.getObject() ) );
40         }
41         
42     private Node asPattern( Node x )
43         { return x.isBlank() ? null : x; }
44         
45     private void handleRoot( Graph result, Node root, Set pending )
46         {
47         ClosableIterator it = pattern.find( root, null, null );
48         if (!it.hasNext())
49             {
50             absorb( result, pending );
51             return;
52             }
53         while (it.hasNext())
54             {
55             Triple base = (Triple) it.next();
56             Iterator that = findFromTriple( target, base ); // target.find( base.getSubject(), base.getPredicate(), base.getObject() );
57
while (that.hasNext())
58                 {
59                 Triple x = (Triple) that.next();
60                 pending.add( x );
61                 handleRoot( result, base.getObject(), pending );
62                 }
63             }
64         }
65         
66     private void absorb( Graph result, Set triples )
67         {
68         for (Iterator it = triples.iterator(); it.hasNext(); result.add( (Triple) it.next())) {}
69         triples.clear();
70         }
71         
72     public static Set getRoots( Graph pattern )
73         {
74         Set roots = CollectionFactory.createHashedSet();
75         ClosableIterator sub = GraphUtil.findAll( pattern );
76         while (sub.hasNext()) roots.add( ((Triple) sub.next()).getSubject() );
77         ClosableIterator obj = GraphUtil.findAll( pattern );
78         while (obj.hasNext()) roots.remove( ((Triple) obj.next()).getObject() );
79         return roots;
80         }
81     }
82 /*
83     (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
84     All rights reserved.
85
86     Redistribution and use in source and binary forms, with or without
87     modification, are permitted provided that the following conditions
88     are met:
89
90     1. Redistributions of source code must retain the above copyright
91        notice, this list of conditions and the following disclaimer.
92
93     2. Redistributions in binary form must reproduce the above copyright
94        notice, this list of conditions and the following disclaimer in the
95        documentation and/or other materials provided with the distribution.
96
97     3. The name of the author may not be used to endorse or promote products
98        derived from this software without specific prior written permission.
99
100     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
101     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
102     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
103     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
104     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
105     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
106     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
107     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
108     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
109     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
110 */

111
Popular Tags