KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > graph > Triple


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

6
7 package com.hp.hpl.jena.graph;
8
9 import com.hp.hpl.jena.shared.*;
10 import java.util.*;
11
12 /**
13     Triples are the basis for RDF statements; they have a subject, predicate, and
14     object field (all nodes) and express the notion that the relationship named
15     by the predicate holds between the subject and the object.
16     
17     @author Jeremy Carroll
18  */

19 public class Triple implements TripleMatch {
20     private final Node subj, pred, obj;
21     
22     public Triple( Node s, Node p, Node o )
23         {
24         if (s == null) throw new UnsupportedOperationException JavaDoc( "subject cannot be null" );
25         if (p == null) throw new UnsupportedOperationException JavaDoc( "predicate cannot be null" );
26         if (o == null) throw new UnsupportedOperationException JavaDoc( "object cannot be null" );
27         subj = s;
28         pred = p;
29         obj = o;
30         }
31     
32     /**
33         return a human-readable string "subject @predicate object" describing the triple
34     */

35     public String JavaDoc toString()
36         { return toString( PrefixMapping.Standard ); }
37     
38     public String JavaDoc toString( PrefixMapping pm )
39        {
40        return subj.toString( pm, true )
41             + " @" + pred.toString( pm, true )
42             + " " + obj.toString( pm, true );
43        }
44     
45     /**
46         @return the subject of the triple
47     */

48     public Node getSubject() {
49         return subj;
50     }
51     
52     /**
53         @return the predicate of the triple
54     */

55     public Node getPredicate() {
56         return pred;
57     }
58     
59     /**
60         @return the object of the triple
61     */

62     public Node getObject() {
63         return obj;
64     }
65
66     public Node getMatchSubject()
67         { return anyToNull( subj ); }
68         
69     public Node getMatchPredicate()
70         { return anyToNull( pred ); }
71         
72     public Node getMatchObject()
73         { return anyToNull( obj ); }
74         
75     private static Node anyToNull( Node n )
76         { return Node.ANY.equals( n ) ? null : n; }
77
78     private static Node nullToAny( Node n )
79         { return n == null ? Node.ANY : n; }
80         
81     public Triple asTriple()
82         { return this; }
83         
84     public boolean isConcrete()
85         { return subj.isConcrete() && pred.isConcrete() && obj.isConcrete(); }
86         
87     /**
88          Answer true if <code>o</code> is a Triple with the same subject, predicate,
89          and object as this triple.
90     */

91     public boolean equals(Object JavaDoc o)
92         { return o instanceof Triple && ((Triple) o).sameAs( subj, pred, obj ); }
93     
94     /**
95         Answer true iff this triple has subject s, predicate p, and object o.
96     */

97     public boolean sameAs( Node s, Node p, Node o )
98         { return subj.equals( s ) && pred.equals( p ) && obj.equals( o ); }
99         
100     public boolean matches( Triple other )
101         { return other.matchedBy( subj, pred, obj ); }
102         
103     public boolean matches( Node s, Node p, Node o )
104         { return subj.matches( s ) && pred.matches( p ) && obj.matches( o ); }
105         
106     private boolean matchedBy( Node s, Node p, Node o )
107         { return s.matches( subj ) && p.matches( pred ) && o.matches( obj ); }
108         
109     public boolean subjectMatches( Node s )
110         { return subj.matches( s ); }
111         
112     public boolean predicateMatches( Node p )
113         { return pred.matches( p ); }
114         
115     public boolean objectMatches( Node o )
116         { return obj.matches( o ); }
117         
118     /**
119         The hash-code of a triple is the hash-codes of its components munged
120         together: see hashCode(S, P, O).
121     */

122     public int hashCode()
123         { return hashCode( subj, pred, obj ); }
124     
125     /**
126         Return the munged hashCodes of the specified nodes, an exclusive-or of
127         the slightly-shifted component hashcodes; this means (almost) all of the bits
128         count, and the order matters, so (S @P O) has a different hash from
129         (O @P S), etc.
130     */

131     public static int hashCode( Node s, Node p, Node o )
132         { return (s.hashCode() >> 1) ^ p.hashCode() ^ (o.hashCode() << 1); }
133     
134     /**
135         Factory method for creating triples, allows caching opportunities. Attempts
136         to use triples from the cache, if any suitable ones exist.
137         
138         @return a triple with subject=s, predicate=p, object=o
139     */

140     public static Triple create( Node s, Node p, Node o )
141         {
142         Triple already = cache.get( s, p, o );
143         return already == null ? cache.put( new Triple( s, p, o ) ) : already;
144         }
145     
146     /**
147         The cache of already-created triples.
148     */

149     protected static TripleCache cache = new TripleCache();
150         
151     public static Triple createMatch( Node s, Node p, Node o )
152         { return Triple.create( nullToAny( s ), nullToAny( p ), nullToAny( o ) ); }
153         
154     /**
155         Utility factory method for creating a triple based on the content of an
156         "S P O" string. The S, P, O are processed by Node.create, see which for
157         details of the supported syntax. This method exists to support test code.
158         Nodes are interpreted using the Standard prefix mapping.
159     */

160     
161     public static Triple create( String JavaDoc fact )
162         { return create( PrefixMapping.Standard, fact ); }
163         
164     /**
165         Utility factory as for create(String), but allowing the PrefixMapping to
166         be specified explicitly.
167     */

168     public static Triple create( PrefixMapping pm, String JavaDoc fact )
169         {
170         StringTokenizer st = new StringTokenizer( fact );
171         Node sub = Node.create( pm, st.nextToken() );
172         Node pred = Node.create( pm, st.nextToken() );
173         Node obj = Node.create( pm, st.nextToken() );
174         return Triple.create( sub, pred, obj );
175         }
176   
177     public static final Triple ANY = Triple.create( Node.ANY, Node.ANY, Node.ANY );
178     }
179
180 /*
181     (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
182     All rights reserved.
183
184     Redistribution and use in source and binary forms, with or without
185     modification, are permitted provided that the following conditions
186     are met:
187
188     1. Redistributions of source code must retain the above copyright
189        notice, this list of conditions and the following disclaimer.
190
191     2. Redistributions in binary form must reproduce the above copyright
192        notice, this list of conditions and the following disclaimer in the
193        documentation and/or other materials provided with the distribution.
194
195     3. The name of the author may not be used to endorse or promote products
196        derived from this software without specific prior written permission.
197
198     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
199     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
200     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
201     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
202     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
203     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
204     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
205     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
206     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
207     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
208 */

209
Popular Tags