001: /*****************************************************************************
002: * Source code information
003: * -----------------------
004: * Original author Ian Dickinson, HP Labs Bristol
005: * Author email Ian.Dickinson@hp.com
006: * Package Jena 2
007: * Web http://sourceforge.net/projects/jena/
008: * Created 27-Mar-2003
009: * Filename $RCSfile: ClassHierarchy.java,v $
010: * Revision $Revision: 1.1 $
011: * Release status $State: Exp $
012: *
013: * Last modified on $Date: 2005/10/06 17:49:07 $
014: * by $Author: andy_seaborne $
015: *
016: * (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
017: * (see footer for full conditions)
018: *****************************************************************************/package jena.examples.ontology.classHierarchy;
019:
020: // Imports
021: ///////////////
022: import com.hp.hpl.jena.ontology.*;
023: import com.hp.hpl.jena.rdf.model.*;
024: import com.hp.hpl.jena.shared.PrefixMapping;
025: import com.hp.hpl.jena.util.iterator.Filter;
026:
027: import java.io.PrintStream;
028: import java.util.*;
029:
030: /**
031: * <p>
032: * Simple demonstration program to show how to list a hierarchy of classes. This
033: * is not a complete solution to the problem (sub-classes of restrictions, for example,
034: * are not shown). It is inteded only to be illustrative of the general approach.
035: * </p>
036: *
037: * @author Ian Dickinson, HP Labs
038: * (<a href="mailto:Ian.Dickinson@hp.com" >email</a>)
039: * @version CVS $Id: ClassHierarchy.java,v 1.1 2005/10/06 17:49:07 andy_seaborne Exp $
040: */
041: public class ClassHierarchy {
042: // Constants
043: //////////////////////////////////
044:
045: // Static variables
046: //////////////////////////////////
047:
048: // Instance variables
049: //////////////////////////////////
050:
051: protected OntModel m_model;
052: private Map m_anonIDs = new HashMap();
053: private int m_anonCount = 0;
054:
055: // Constructors
056: //////////////////////////////////
057:
058: // External signature methods
059: //////////////////////////////////
060:
061: /** Show the sub-class hierarchy encoded by the given model */
062: public void showHierarchy(PrintStream out, OntModel m) {
063: // create an iterator over the root classes that are not anonymous class expressions
064: Iterator i = m.listHierarchyRootClasses().filterDrop(
065: new Filter() {
066: public boolean accept(Object o) {
067: return ((Resource) o).isAnon();
068: }
069: });
070:
071: while (i.hasNext()) {
072: showClass(out, (OntClass) i.next(), new ArrayList(), 0);
073: }
074: }
075:
076: // Internal implementation methods
077: //////////////////////////////////
078:
079: /** Present a class, then recurse down to the sub-classes.
080: * Use occurs check to prevent getting stuck in a loop
081: */
082: protected void showClass(PrintStream out, OntClass cls,
083: List occurs, int depth) {
084: renderClassDescription(out, cls, depth);
085: out.println();
086:
087: // recurse to the next level down
088: if (cls.canAs(OntClass.class) && !occurs.contains(cls)) {
089: for (Iterator i = cls.listSubClasses(true); i.hasNext();) {
090: OntClass sub = (OntClass) i.next();
091:
092: // we push this expression on the occurs list before we recurse
093: occurs.add(cls);
094: showClass(out, sub, occurs, depth + 1);
095: occurs.remove(cls);
096: }
097: }
098: }
099:
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 out, OntClass c,
106: int depth) {
107: indent(out, depth);
108:
109: if (c.isRestriction()) {
110: renderRestriction(out, (Restriction) c
111: .as(Restriction.class));
112: } else {
113: if (!c.isAnon()) {
114: out.print("Class ");
115: renderURI(out, c.getModel(), c.getURI());
116: out.print(' ');
117: } else {
118: renderAnonymous(out, c, "class");
119: }
120: }
121: }
122:
123: /**
124: * <p>Handle the case of rendering a restriction.</p>
125: * @param out The print stream to write to
126: * @param r The restriction to render
127: */
128: protected void renderRestriction(PrintStream out, Restriction r) {
129: if (!r.isAnon()) {
130: out.print("Restriction ");
131: renderURI(out, r.getModel(), r.getURI());
132: } else {
133: renderAnonymous(out, r, "restriction");
134: }
135:
136: out.print(" on property ");
137: renderURI(out, r.getModel(), r.getOnProperty().getURI());
138: }
139:
140: /** Render a URI */
141: protected void renderURI(PrintStream out, PrefixMapping prefixes,
142: String uri) {
143: out.print(prefixes.shortForm(uri));
144: }
145:
146: /** Render an anonymous class or restriction */
147: protected void renderAnonymous(PrintStream out, Resource anon,
148: String name) {
149: String anonID = (String) m_anonIDs.get(anon.getId());
150: if (anonID == null) {
151: anonID = "a-" + m_anonCount++;
152: m_anonIDs.put(anon.getId(), anonID);
153: }
154:
155: out.print("Anonymous ");
156: out.print(name);
157: out.print(" with ID ");
158: out.print(anonID);
159: }
160:
161: /** Generate the indentation */
162: protected void indent(PrintStream out, int depth) {
163: for (int i = 0; i < depth; i++) {
164: out.print(" ");
165: }
166: }
167:
168: //==============================================================================
169: // Inner class definitions
170: //==============================================================================
171:
172: }
173:
174: /*
175: (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
176: All rights reserved.
177:
178: Redistribution and use in source and binary forms, with or without
179: modification, are permitted provided that the following conditions
180: are met:
181:
182: 1. Redistributions of source code must retain the above copyright
183: notice, this list of conditions and the following disclaimer.
184:
185: 2. Redistributions in binary form must reproduce the above copyright
186: notice, this list of conditions and the following disclaimer in the
187: documentation and/or other materials provided with the distribution.
188:
189: 3. The name of the author may not be used to endorse or promote products
190: derived from this software without specific prior written permission.
191:
192: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
193: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
194: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
195: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
196: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
197: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
198: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
199: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
200: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
201: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
202: */
|