001: /*
002: (c) Copyright 2002, 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: [See end of file]
004: $Id: EnhGraph.java,v 1.20 2008/01/02 12:09:12 andy_seaborne Exp $
005: */
006:
007: package com.hp.hpl.jena.enhanced;
008:
009: import com.hp.hpl.jena.graph.*;
010: import com.hp.hpl.jena.util.cache.*;
011:
012: /**
013: A specialisation of Polymorphic that models an extended graph - that is, one that
014: contains{@link EnhNode Enhanced nodes} or one that itself exposes additional
015: capabilities beyond the graph API.
016: <p>
017: <span style="color:red">WARNING</span>. The polymorphic aspects of EnhGraph
018: are <span style="color:red">not supported</span> and are not expected to be
019: supported in this way for the indefinite future.
020:
021: @author <a href="mailto:Jeremy.Carroll@hp.com">Jeremy Carroll</a> (original code)
022: <br><a href="mailto:Chris.Dollin@hp.com">Chris Dollin</a> (original code)
023: <br><a href="mailto:Ian.Dickinson@hp.com">Ian Dickinson</a>
024: (refactoring and commentage)
025: */
026:
027: public class EnhGraph extends Polymorphic {
028: // Instance variables
029: /** The graph that this enhanced graph is wrapping */
030: protected Graph graph;
031:
032: /** Counter that helps to ensure that caches are kept distinct */
033: static private int cnt = 0;
034:
035: /** Cache of enhanced nodes that have been created */
036: protected Cache enhNodes = CacheManager.createCache(
037: CacheManager.ENHNODECACHE, "EnhGraph-" + cnt++, 1000);
038:
039: /** The unique personality that is bound to this polymorphic instace */
040: private Personality personality;
041:
042: public boolean isValid() {
043: return true;
044: }
045:
046: // Constructors
047: /**
048: * Construct an enhanced graph from the given underlying graph, and
049: * a factory for generating enhanced nodes.
050: *
051: * @param g The underlying plain graph, may be null to defer binding to a given
052: * graph until later.
053: * @param p The personality factory, that maps types to realisations
054: */
055: public EnhGraph(Graph g, Personality p) {
056: super ();
057: graph = g;
058: personality = p;
059: }
060:
061: // External methods
062:
063: /**
064: * Answer the normal graph that this enhanced graph is wrapping.
065: * @return A graph
066: */
067: public Graph asGraph() {
068: return graph;
069: }
070:
071: /**
072: * Hashcode for an enhnaced graph is delegated to the underlyin graph.
073: * @return The hashcode as an int
074: */
075: final public int hashCode() {
076: return graph.hashCode();
077: }
078:
079: /**
080: * An enhanced graph is equal to another graph g iff the underlying graphs
081: * are equal.
082: * This is deemed to be a complete and correct interpretation of enhanced
083: * graph equality, which is why this method has been marked final.
084: * <p> Note that this equality test does not look for correspondance between
085: * the structures in the two graphs. To test whether another graph has the
086: * same nodes and edges as this one, use {@link #isIsomorphicWith}.
087: * </p>
088: * @param o An object to test for equality with this node
089: * @return True if o is equal to this node.
090: * @see #isIsomorphicWith
091: */
092: final public boolean equals(Object o) {
093: return this == o || o instanceof EnhGraph
094: && graph.equals(((EnhGraph) o).asGraph());
095: }
096:
097: /**
098: * Answer true if the given enhanced graph contains the same nodes and
099: * edges as this graph. The default implementation delegates this to the
100: * underlying graph objects.
101: *
102: * @param eg A graph to test
103: * @return True if eg is a graph with the same structure as this.
104: */
105: final public boolean isIsomorphicWith(EnhGraph eg) {
106: return graph.isIsomorphicWith(eg.graph);
107: }
108:
109: /**
110: * Answer an enhanced node that wraps the given node and conforms to the given
111: * interface type.
112: *
113: * @param n A node (assumed to be in this graph)
114: * @param interf A type denoting the enhanced facet desired
115: * @return An enhanced node
116: */
117: public EnhNode getNodeAs(Node n, Class interf) {
118: // We use a cache to avoid reconstructing the same Node too many times.
119: EnhNode eh = (EnhNode) enhNodes.get(n);
120: if (eh != null)
121: return eh.viewAs(interf);
122:
123: // not in the cache, so build a new one
124: eh = (EnhNode) ((GraphPersonality) personality)
125: .nodePersonality().newInstance(interf, n, this );
126: enhNodes.put(n, eh);
127: return eh;
128: }
129:
130: /**
131: * Answer the cache controlle for this graph
132: * @return A cache controller object
133: */
134: public CacheControl getNodeCacheControl() {
135: return enhNodes;
136: }
137:
138: /**
139: * Set the cache controller object for this graph
140: * @param cc The cache controller
141: */
142: public void setNodeCache(Cache cc) {
143: enhNodes = cc;
144: }
145:
146: /**
147: * Answer an enhanced graph that presents <i>this</i> in a way which satisfies type
148: * t. This is a stub method that has not yet been implemented.
149: @deprecated
150: @param t A type
151: @return A polymorphic instance, possibly but not necessarily this, that conforms to t.
152: */
153: protected Polymorphic convertTo(Class t) {
154: throw new PersonalityConfigException(
155: "Alternative perspectives on graphs has not been implemented yet");
156: }
157:
158: /**
159: we can't convert to anything.
160: @deprecated
161: */
162: protected boolean canSupport(Class t) {
163: return false;
164: }
165:
166: /**
167: * Answer the personality object bound to this polymorphic instance
168: *
169: * @return The personality object
170: */
171: protected Personality getPersonality() {
172: return personality;
173: }
174:
175: }
176:
177: /*
178: (c) Copyright 2002, 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
179: All rights reserved.
180:
181: Redistribution and use in source and binary forms, with or without
182: modification, are permitted provided that the following conditions
183: are met:
184:
185: 1. Redistributions of source code must retain the above copyright
186: notice, this list of conditions and the following disclaimer.
187:
188: 2. Redistributions in binary form must reproduce the above copyright
189: notice, this list of conditions and the following disclaimer in the
190: documentation and/or other materials provided with the distribution.
191:
192: 3. The name of the author may not be used to endorse or promote products
193: derived from this software without specific prior written permission.
194:
195: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
196: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
197: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
198: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
199: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
200: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
201: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
202: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
203: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
204: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
205: */
|