KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > mem > GraphTripleStore


1 /*
2   (c) Copyright 2004, 2005 Hewlett-Packard Development Company, LP, all rights reserved.
3   [See end of file]
4   $Id: GraphTripleStore.java,v 1.7 2005/02/21 12:03:46 andy_seaborne Exp $
5 */

6 package com.hp.hpl.jena.mem;
7
8 import com.hp.hpl.jena.graph.*;
9 import com.hp.hpl.jena.graph.impl.TripleStore;
10 import com.hp.hpl.jena.util.iterator.ExtendedIterator;
11 import com.hp.hpl.jena.util.iterator.WrappedIterator;
12
13 /**
14     GraphTripleStore - the underlying triple-indexed triple store for GraphMem et al,
15     ripped out from the heart of GraphMem as part of simplifying the reification code.
16     A GraphTripleStore is a searchable repository for triples.
17     
18     @author kers
19  */

20 public class GraphTripleStore implements TripleStore
21     {
22     protected NodeToTriplesMap subjects = new NodeToTriplesMap()
23         { public Node getIndexNode( Triple t ) { return t.getSubject(); } };
24         
25     protected NodeToTriplesMap predicates = new NodeToTriplesMap()
26         { public Node getIndexNode( Triple t ) { return t.getPredicate(); } };
27         
28     protected NodeToTriplesMap objects = new NodeToTriplesMap()
29         { public Node getIndexNode( Triple t ) { return t.getObject(); } };
30         
31     protected Graph parent;
32     
33     public GraphTripleStore( Graph parent )
34         { this.parent = parent; }
35     
36     /**
37          Destroy this triple store - discard the indexes.
38     */

39     public void close()
40         { subjects = predicates = objects = null; }
41     
42     /**
43          Add a triple to this triple store.
44     */

45     public void add( Triple t )
46         {
47         if (subjects.add( t.getSubject(), t ))
48             {
49             predicates.add( t.getPredicate(), t );
50             objects.add( t.getObject(), t );
51             }
52         }
53     
54     /**
55          Remove a triple from this triple store.
56     */

57     public void delete( Triple t )
58         {
59         if (subjects.remove( t.getSubject(), t ))
60             {
61             predicates.remove( t.getPredicate(), t );
62             objects.remove( t.getObject(), t );
63             }
64         }
65     
66     /**
67          Answer the size (number of triples) of this triple store.
68     */

69     public int size()
70         { return subjects.size(); }
71     
72     /**
73          Answer true iff this triple store is empty.
74     */

75     public boolean isEmpty()
76         { return subjects.isEmpty(); }
77     
78     public ExtendedIterator listSubjects()
79         { return WrappedIterator.createNoRemove( subjects.domain() ); }
80
81     public ExtendedIterator listPredicates()
82         { return WrappedIterator.createNoRemove( predicates.domain() ); }
83     
84     public ExtendedIterator listObjects()
85         { return WrappedIterator.createNoRemove( objects.domain() ); }
86     
87     /**
88          Answer true iff this triple store contains the (concrete) triple <code>t</code>.
89     */

90     public boolean contains( Triple t )
91         { return subjects.contains( t ); }
92     
93     /**
94         Answer an ExtendedIterator returning all the triples from this store that
95         match the pattern <code>m = (S, P, O)</code>.
96         
97         <p>Because the node-to-triples maps index on each of subject, predicate,
98         and (non-literal) object, concrete S/P/O patterns can immediately select
99         an appropriate map. Because the match for literals must be by sameValueAs,
100         not equality, the optimisation is not applied for literals. [This is probably a
101         Bad Thing for strings.]
102         
103         <p>Practice suggests doing the predicate test <i>last</i>, because there are
104         "usually" many more statements than predicates, so the predicate doesn't
105         cut down the search space very much. By "practice suggests" I mean that
106         when the order went, accidentally, from S/O/P to S/P/O, performance on
107         (ANY, P, O) searches on largish models with few predicates declined
108         dramatically - specifically on the not-galen.owl ontology.
109     */

110     public ExtendedIterator find( TripleMatch tm )
111         {
112         Triple t = tm.asTriple();
113         Node pm = t.getPredicate();
114         Node om = t.getObject();
115         Node sm = t.getSubject();
116         if (sm.isConcrete())
117             return new StoreTripleIterator( parent, subjects.iterator( sm , t ), predicates, objects );
118         else if (om.isConcrete() && !om.isLiteral())
119             return new StoreTripleIterator( parent, objects.iterator( om, t ), subjects, predicates );
120         else if (pm.isConcrete())
121             return new StoreTripleIterator( parent, predicates.iterator( pm, t ), subjects, objects );
122         else
123             return new StoreTripleIterator( parent, subjects.iterator( t ), predicates, objects );
124         }
125     
126     /**
127          Clear this store, ie remove all triples from it.
128     */

129     public void clear()
130         {
131         subjects.clear();
132         predicates.clear();
133         objects.clear();
134         }
135     
136     }
137
138 /*
139     (c) Copyright 2004, 2005 Hewlett-Packard Development Company, LP
140     All rights reserved.
141     
142     Redistribution and use in source and binary forms, with or without
143     modification, are permitted provided that the following conditions
144     are met:
145     
146     1. Redistributions of source code must retain the above copyright
147        notice, this list of conditions and the following disclaimer.
148     
149     2. Redistributions in binary form must reproduce the above copyright
150        notice, this list of conditions and the following disclaimer in the
151        documentation and/or other materials provided with the distribution.
152     
153     3. The name of the author may not be used to endorse or promote products
154        derived from this software without specific prior written permission.
155     
156     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
157     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
158     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
159     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
160     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
161     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
162     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
163     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
164     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
165     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
166 */
Popular Tags