KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ClassHierarchy


1 /*****************************************************************************
2  * Source code information
3  * -----------------------
4  * Original author Ian Dickinson, HP Labs Bristol
5  * Author email Ian.Dickinson@hp.com
6  * Package Jena 2
7  * Web http://sourceforge.net/projects/jena/
8  * Created 27-Mar-2003
9  * Filename $RCSfile: ClassHierarchy.java,v $
10  * Revision $Revision: 1.2 $
11  * Release status $State: Exp $
12  *
13  * Last modified on $Date: 2003/09/02 14:48:50 $
14  * by $Author: ian_dickinson $
15  *
16  *****************************************************************************/

17
18 // Package
19
///////////////
20

21
22 // Imports
23
///////////////
24
import com.hp.hpl.jena.ontology.*;
25 import com.hp.hpl.jena.rdf.model.*;
26 import com.hp.hpl.jena.shared.PrefixMapping;
27
28 import java.io.PrintStream JavaDoc;
29 import java.util.*;
30
31
32 /**
33  * <p>
34  * Simple demonstration program to show how to list a hierarchy of classes. This
35  * is not a complete solution to the problem (sub-classes of restrictions, for example,
36  * are not shown). It is inteded only to be illustrative of the general approach.
37  * </p>
38  *
39  * @author Ian Dickinson, HP Labs
40  * (<a HREF="mailto:Ian.Dickinson@hp.com" >email</a>)
41  * @version CVS $Id: ClassHierarchy.java,v 1.2 2003/09/02 14:48:50 ian_dickinson Exp $
42  */

43 public class ClassHierarchy {
44     // Constants
45
//////////////////////////////////
46

47     // Static variables
48
//////////////////////////////////
49

50     // Instance variables
51
//////////////////////////////////
52

53     protected OntModel m_model;
54     private Map m_anonIDs = new HashMap();
55     private int m_anonCount = 0;
56     
57
58
59     // Constructors
60
//////////////////////////////////
61

62     // External signature methods
63
//////////////////////////////////
64

65     /** Show the sub-class hierarchy encoded by the given model */
66     public void showHierarchy( PrintStream JavaDoc out, OntModel m ) {
67         for (Iterator i = rootClasses( m ); i.hasNext(); ) {
68             showClass( out, (OntClass) i.next(), new ArrayList(), 0 );
69         }
70     }
71
72
73     // Internal implementation methods
74
//////////////////////////////////
75

76     /** Present a class, then recurse down to the sub-classes.
77      * Use occurs check to prevent getting stuck in a loop
78      */

79     protected void showClass( PrintStream JavaDoc out, OntClass cls, List occurs, int depth ) {
80         renderClassDescription( out, cls, depth );
81         out.println();
82
83         // recurse to the next level down
84
if (cls.canAs( OntClass.class ) && !occurs.contains( cls )) {
85             for (Iterator i = cls.listSubClasses( true ); i.hasNext(); ) {
86                 OntClass sub = (OntClass) i.next();
87
88                 // we push this expression on the occurs list before we recurse
89
occurs.add( cls );
90                 showClass( out, sub, occurs, depth + 1 );
91                 occurs.remove( cls );
92             }
93         }
94     }
95
96
97     /**
98      * <p>Render a description of the given class to the given output stream.</p>
99      * @param out A print stream to write to
100      * @param c The class to render
101      */

102     public void renderClassDescription( PrintStream JavaDoc out, OntClass c, int depth ) {
103         indent( out, depth );
104         
105         if (c.isRestriction()) {
106             renderRestriction( out, (Restriction) c.as( Restriction.class ) );
107         }
108         else {
109             if (!c.isAnon()) {
110                 out.print( "Class " );
111                 renderURI( out, c.getModel(), c.getURI() );
112                 out.print( ' ' );
113             }
114             else {
115                 renderAnonymous( out, c, "class" );
116             }
117         }
118     }
119
120     /**
121      * <p>Handle the case of rendering a restriction.</p>
122      * @param out The print stream to write to
123      * @param r The restriction to render
124      */

125     protected void renderRestriction( PrintStream JavaDoc out, Restriction r ) {
126         if (!r.isAnon()) {
127             out.print( "Restriction " );
128             renderURI( out, r.getModel(), r.getURI() );
129         }
130         else {
131             renderAnonymous( out, r, "restriction" );
132         }
133         
134         out.print( " on property " );
135         renderURI( out, r.getModel(), r.getOnProperty().getURI() );
136     }
137     
138     /** Render a URI */
139     protected void renderURI( PrintStream JavaDoc out, PrefixMapping prefixes, String JavaDoc uri ) {
140         out.print( prefixes.usePrefix( uri ) );
141     }
142     
143     /** Render an anonymous class or restriction */
144     protected void renderAnonymous( PrintStream JavaDoc out, Resource anon, String JavaDoc name ) {
145         String JavaDoc anonID = (String JavaDoc) m_anonIDs.get( anon.getId() );
146         if (anonID == null) {
147             anonID = "a-" + m_anonCount++;
148             m_anonIDs.put( anon.getId(), anonID );
149         }
150         
151         out.print( "Anonymous ");
152         out.print( name );
153         out.print( " with ID " );
154         out.print( anonID );
155     }
156     
157     /** Generate the indentation */
158     protected void indent( PrintStream JavaDoc out, int depth ) {
159         for (int i = 0; i < depth; i++) {
160             out.print( " " );
161         }
162     }
163
164     /**
165      * Answer an iterator over the classes we will use as the roots of the depicted
166      * hierarchy. We use named classes that either have Thing as a direct super-class,
167      * or which have no declared super-classes. The first condition is helpful if
168      * using a reasoner, the second otherwise.
169      * @param m A model
170      * @return An iterator over the named class hierarchy roots in m
171      */

172     protected Iterator rootClasses( OntModel m ) {
173         List roots = new ArrayList();
174         
175         for (Iterator i = m.listClasses(); i.hasNext(); ) {
176             OntClass c = (OntClass) i.next();
177             
178             // too confusing to list all the restrictions as root classes
179
if (c.isAnon()) {
180                 continue;
181             }
182             
183             if (c.hasSuperClass( m.getProfile().THING(), true ) ) {
184                 // this class is directly descended from Thing
185
roots.add( c );
186             }
187             else if (c.getCardinality( m.getProfile().SUB_CLASS_OF() ) == 0 ) {
188                 // this class has no super-classes (can occur if we're not using the reasoner)
189
roots.add( c );
190             }
191         }
192         
193         return roots.iterator();
194     }
195
196     //==============================================================================
197
// Inner class definitions
198
//==============================================================================
199

200 }
201
Popular Tags