KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > enhanced > EnhGraph


1 /*
2   (c) Copyright 2002, 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3   [See end of file]
4   $Id: EnhGraph.java,v 1.16 2005/02/21 12:03:38 andy_seaborne Exp $
5 */

6
7 package com.hp.hpl.jena.enhanced;
8
9 import com.hp.hpl.jena.graph.*;
10 import com.hp.hpl.jena.util.cache.*;
11
12 /**
13     A specialisation of Polymorphic that models an extended graph - that is, one that
14     contains{@link EnhNode Enhanced nodes} or one that itself exposes additional
15     capabilities beyond the graph API.
16  <p>
17     <span style="color:red">WARNING</span>. The polymorphic aspects of EnhGraph
18     are <span style="color:red">not supported</span> and are not expected to be
19     supported in this way for the indefinite future.
20     
21     @author <a HREF="mailto:Jeremy.Carroll@hp.com">Jeremy Carroll</a> (original code)
22     <br><a HREF="mailto:Chris.Dollin@hp.com">Chris Dollin</a> (original code)
23     <br><a HREF="mailto:Ian.Dickinson@hp.com">Ian Dickinson</a>
24     (refactoring and commentage)
25 */

26
27 public class EnhGraph
28     extends Polymorphic
29 {
30     // Instance variables
31
/** The graph that this enhanced graph is wrapping */
32     protected Graph graph;
33     
34     /** Counter that helps to ensure that caches are kept distinct */
35     static private int cnt = 0;
36
37     /** Cache of enhanced nodes that have been created */
38     protected Cache enhNodes = CacheManager.createCache( CacheManager.ENHNODECACHE, "EnhGraph-" + cnt++, 1000 );
39     
40     /** The unique personality that is bound to this polymorphic instace */
41     private Personality personality;
42
43     public boolean isValid()
44         { return true; }
45     
46     // Constructors
47
/**
48      * Construct an enhanced graph from the given underlying graph, and
49      * a factory for generating enhanced nodes.
50      *
51      * @param g The underlying plain graph, may be null to defer binding to a given
52      * graph until later.
53      * @param p The personality factory, that maps types to realisations
54      */

55     public EnhGraph( Graph g, Personality p ) {
56         super();
57         graph = g;
58         personality = p;
59     }
60    
61     // External methods
62

63     /**
64      * Answer the normal graph that this enhanced graph is wrapping.
65      * @return A graph
66      */

67     public Graph asGraph() {
68         return graph;
69     }
70    
71     /**
72      * Hashcode for an enhnaced graph is delegated to the underlyin graph.
73      * @return The hashcode as an int
74      */

75     final public int hashCode() {
76         return graph.hashCode();
77     }
78
79      
80     /**
81      * An enhanced graph is equal to another graph g iff the underlying graphs
82      * are equal.
83      * This is deemed to be a complete and correct interpretation of enhanced
84      * graph equality, which is why this method has been marked final.
85      * <p> Note that this equality test does not look for correspondance between
86      * the structures in the two graphs. To test whether another graph has the
87      * same nodes and edges as this one, use {@link #isIsomorphicWith}.
88      * </p>
89      * @param o An object to test for equality with this node
90      * @return True if o is equal to this node.
91      * @see #isIsomorphicWith
92      */

93     final public boolean equals(Object JavaDoc o) {
94         return
95             this == o
96             || o instanceof EnhGraph && graph.equals(((EnhGraph) o).asGraph());
97     }
98     
99     
100     /**
101      * Answer true if the given enhanced graph contains the same nodes and
102      * edges as this graph. The default implementation delegates this to the
103      * underlying graph objects.
104      *
105      * @param eg A graph to test
106      * @return True if eg is a graph with the same structure as this.
107      */

108     final public boolean isIsomorphicWith(EnhGraph eg){
109         return graph.isIsomorphicWith(eg.graph);
110     }
111
112     /**
113      * Answer an enhanced node that wraps the given node and conforms to the given
114      * interface type.
115      *
116      * @param n A node (assumed to be in this graph)
117      * @param interf A type denoting the enhanced facet desired
118      * @return An enhanced node
119      */

120     public EnhNode getNodeAs(Node n,Class JavaDoc interf) {
121          // We use a cache to avoid reconstructing the same Node too many times.
122
EnhNode eh = (EnhNode)enhNodes.get(n);
123         if ( eh != null )
124             return eh.viewAs(interf);
125             
126         // not in the cache, so build a new one
127
eh = (EnhNode) ((GraphPersonality) personality).nodePersonality()
128             .newInstance( interf, n, this );
129         enhNodes.put( n, eh );
130         return eh;
131     }
132     
133     
134     /**
135      * Answer the cache controlle for this graph
136      * @return A cache controller object
137      */

138     public CacheControl getNodeCacheControl() {
139          return enhNodes;
140     }
141     
142     /**
143      * Set the cache controller object for this graph
144      * @param cc The cache controller
145      */

146     public void setNodeCache(Cache cc) {
147          enhNodes = cc;
148     }
149      
150      
151     /**
152      * Answer an enhanced graph that presents <i>this</i> in a way which satisfies type
153      * t. This is a stub method that has not yet been implemented.
154      @deprecated
155      @param t A type
156      @return A polymorphic instance, possibly but not necessarily this, that conforms to t.
157      */

158     protected Polymorphic convertTo(Class JavaDoc t) {
159         throw new PersonalityConfigException
160             ( "Alternative perspectives on graphs has not been implemented yet" );
161     }
162     
163     /**
164         we can't convert to anything.
165         @deprecated
166     */

167     protected boolean canSupport( Class JavaDoc t )
168         { return false; }
169         
170     /**
171      * Answer the personality object bound to this polymorphic instance
172      *
173      * @return The personality object
174      */

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

211
Popular Tags