KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > DescribeClass


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 25-Jul-2003
9  * Filename $RCSfile: DescribeClass.java,v $
10  * Revision $Revision: 1.2 $
11  * Release status $State: Exp $
12  *
13  * Last modified on $Date: 2003/10/14 13:52:52 $
14  * by $Author: ian_dickinson $
15  *
16  *****************************************************************************/

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

21 // Imports
22
///////////////
23
import java.io.PrintStream JavaDoc;
24 import java.util.*;
25 import java.util.Iterator JavaDoc;
26
27 import com.hp.hpl.jena.ontology.*;
28 import com.hp.hpl.jena.rdf.model.*;
29 import com.hp.hpl.jena.shared.PrefixMapping;
30
31
32
33 /**
34  * <p>
35  * Simple example of describing the basic attributes of a OWL, DAML or RDFS class
36  * using the ontology API. This is not meant as a definitive solution to the problem,
37  * but as an illustration of one approach to solving the problem. This example should
38  * be adapted as necessary to provide a given application with the means to render
39  * a class description in a readable form.
40  * </p>
41  *
42  * @author Ian Dickinson, HP Labs
43  * (<a HREF="mailto:Ian.Dickinson@hp.com" >email</a>)
44  * @version CVS $Id: DescribeClass.java,v 1.2 2003/10/14 13:52:52 ian_dickinson Exp $
45  */

46 public class DescribeClass {
47     // Constants
48
//////////////////////////////////
49

50
51
52     // Static variables
53
//////////////////////////////////
54

55     // Instance variables
56
//////////////////////////////////
57

58     private Map m_anonIDs = new HashMap();
59     private int m_anonCount = 0;
60     
61     
62     // Constructors
63
//////////////////////////////////
64

65     // External signature methods
66
//////////////////////////////////
67

68     /**
69      * <p>Describe the given ontology class in texttual form. The description
70      * produced has the following form (approximately):
71      * <pre>
72      * Class foo:Bar
73      * is a sub-class of foo:A, ex:B
74      * is a super-class of ex:C
75      * </pre>
76      * </p>
77      *
78      * @param out The print stream to write the description to
79      * @param cls The ontology class to describe
80      */

81     public void describeClass( PrintStream JavaDoc out, OntClass cls ) {
82         renderClassDescription( out, cls );
83         out.println();
84         
85         // sub-classes
86
for (Iterator JavaDoc i = cls.listSuperClasses( true ); i.hasNext(); ) {
87             out.print( " is a sub-class of " );
88             renderClassDescription( out, (OntClass) i.next() );
89             out.println();
90         }
91
92         // super-classes
93
for (Iterator JavaDoc i = cls.listSubClasses( true ); i.hasNext(); ) {
94             out.print( " is a super-class of " );
95             renderClassDescription( out, (OntClass) i.next() );
96             out.println();
97         }
98     }
99
100     /**
101      * <p>Render a description of the given class to the given output stream.</p>
102      * @param out A print stream to write to
103      * @param c The class to render
104      */

105     public void renderClassDescription( PrintStream JavaDoc out, OntClass c ) {
106         if (c.isUnionClass()) {
107             renderBooleanClass( out, "union", c.asUnionClass() );
108         }
109         else if (c.isIntersectionClass()) {
110             renderBooleanClass( out, "intersection", c.asIntersectionClass() );
111         }
112         else if (c.isComplementClass()) {
113             renderBooleanClass( out, "complement", c.asComplementClass() );
114         }
115         else if (c.isRestriction()) {
116             renderRestriction( out, c.asRestriction() );
117         }
118         else {
119             if (!c.isAnon()) {
120                 out.print( "Class " );
121                 renderURI( out, prefixesFor( c ), c.getURI() );
122                 out.print( ' ' );
123             }
124             else {
125                 renderAnonymous( out, c, "class" );
126             }
127         }
128     }
129
130
131     // Internal implementation methods
132
//////////////////////////////////
133

134     /**
135      * <p>Handle the case of rendering a restriction.</p>
136      * @param out The print stream to write to
137      * @param r The restriction to render
138      */

139     protected void renderRestriction( PrintStream JavaDoc out, Restriction r ) {
140         if (!r.isAnon()) {
141             out.print( "Restriction " );
142             renderURI( out, prefixesFor( r ), r.getURI() );
143         }
144         else {
145             renderAnonymous( out, r, "restriction" );
146         }
147         
148         out.println();
149         
150         renderRestrictionElem( out, " on property", r.getOnProperty() );
151         out.println();
152         
153         if (r.isAllValuesFromRestriction()) {
154             renderRestrictionElem( out, " all values from", r.asAllValuesFromRestriction().getAllValuesFrom() );
155         }
156         if (r.isSomeValuesFromRestriction()) {
157             renderRestrictionElem( out, " some values from", r.asSomeValuesFromRestriction().getSomeValuesFrom() );
158         }
159         if (r.isHasValueRestriction()) {
160             renderRestrictionElem( out, " has value", r.asHasValueRestriction().getHasValue() );
161         }
162     }
163
164     protected void renderRestrictionElem( PrintStream JavaDoc out, String JavaDoc desc, RDFNode value ) {
165         out.print( desc );
166         out.print( " " );
167         renderValue( out, value );
168     }
169
170     protected void renderValue( PrintStream JavaDoc out, RDFNode value ) {
171         if (value.canAs( OntClass.class )) {
172             renderClassDescription( out, (OntClass) value.as( OntClass.class ) );
173         }
174         else if (value instanceof Resource) {
175             Resource r = (Resource) value;
176             if (r.isAnon()) {
177                 renderAnonymous( out, r, "resource" );
178             }
179             else {
180                 renderURI( out, r.getModel(), r.getURI() );
181             }
182         }
183         else if (value instanceof Literal) {
184             out.print( ((Literal) value).getLexicalForm() );
185         }
186         else {
187             out.print( value );
188         }
189     }
190
191     protected void renderURI( PrintStream JavaDoc out, PrefixMapping prefixes, String JavaDoc uri ) {
192         out.print( prefixes.usePrefix( uri ) );
193     }
194     
195     protected PrefixMapping prefixesFor( Resource n ) {
196         return n.getModel().getGraph().getPrefixMapping();
197     }
198     
199     protected void renderAnonymous( PrintStream JavaDoc out, Resource anon, String JavaDoc name ) {
200         String JavaDoc anonID = (String JavaDoc) m_anonIDs.get( anon.getId() );
201         if (anonID == null) {
202             anonID = "a-" + m_anonCount++;
203             m_anonIDs.put( anon.getId(), anonID );
204         }
205         
206         out.print( "Anonymous ");
207         out.print( name );
208         out.print( " with ID " );
209         out.print( anonID );
210     }
211         
212     protected void renderBooleanClass( PrintStream JavaDoc out, String JavaDoc op, BooleanClassDescription boolClass ) {
213         out.print( op );
214         out.println( " of {" );
215         
216         for (Iterator JavaDoc i = boolClass.listOperands(); i.hasNext(); ) {
217             out.print( " " );
218             renderClassDescription( out, (OntClass) i.next() );
219             out.println();
220         }
221         out.print( " } " );
222     }
223     
224     
225     //==============================================================================
226
// Inner class definitions
227
//==============================================================================
228

229 }
230
231
Popular Tags