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 25-Jul-2003
009: * Filename $RCSfile: DescribeClass.java,v $
010: * Revision $Revision: 1.1 $
011: * Release status $State: Exp $
012: *
013: * Last modified on $Date: 2005/10/06 17:49:06 $
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.describeClass;
019:
020: // Imports
021: ///////////////
022: import java.io.PrintStream;
023: import java.util.*;
024:
025: import com.hp.hpl.jena.ontology.*;
026: import com.hp.hpl.jena.rdf.model.*;
027: import com.hp.hpl.jena.shared.PrefixMapping;
028:
029: /**
030: * <p>
031: * Simple example of describing the basic attributes of a OWL, DAML or RDFS class
032: * using the ontology API. This is not meant as a definitive solution to the problem,
033: * but as an illustration of one approach to solving the problem. This example should
034: * be adapted as necessary to provide a given application with the means to render
035: * a class description in a readable form.
036: * </p>
037: *
038: * @author Ian Dickinson, HP Labs
039: * (<a href="mailto:Ian.Dickinson@hp.com" >email</a>)
040: * @version CVS $Id: DescribeClass.java,v 1.1 2005/10/06 17:49:06 andy_seaborne Exp $
041: */
042: public class DescribeClass {
043: // Constants
044: //////////////////////////////////
045:
046: // Static variables
047: //////////////////////////////////
048:
049: // Instance variables
050: //////////////////////////////////
051:
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: /**
062: * <p>Describe the given ontology class in texttual form. The description
063: * produced has the following form (approximately):
064: * <pre>
065: * Class foo:Bar
066: * is a sub-class of foo:A, ex:B
067: * is a super-class of ex:C
068: * </pre>
069: * </p>
070: *
071: * @param out The print stream to write the description to
072: * @param cls The ontology class to describe
073: */
074: public void describeClass(PrintStream out, OntClass cls) {
075: renderClassDescription(out, cls);
076: out.println();
077:
078: // sub-classes
079: for (Iterator i = cls.listSuperClasses(true); i.hasNext();) {
080: out.print(" is a sub-class of ");
081: renderClassDescription(out, (OntClass) i.next());
082: out.println();
083: }
084:
085: // super-classes
086: for (Iterator i = cls.listSubClasses(true); i.hasNext();) {
087: out.print(" is a super-class of ");
088: renderClassDescription(out, (OntClass) i.next());
089: out.println();
090: }
091: }
092:
093: /**
094: * <p>Render a description of the given class to the given output stream.</p>
095: * @param out A print stream to write to
096: * @param c The class to render
097: */
098: public void renderClassDescription(PrintStream out, OntClass c) {
099: if (c.isUnionClass()) {
100: renderBooleanClass(out, "union", c.asUnionClass());
101: } else if (c.isIntersectionClass()) {
102: renderBooleanClass(out, "intersection", c
103: .asIntersectionClass());
104: } else if (c.isComplementClass()) {
105: renderBooleanClass(out, "complement", c.asComplementClass());
106: } else if (c.isRestriction()) {
107: renderRestriction(out, c.asRestriction());
108: } else {
109: if (!c.isAnon()) {
110: out.print("Class ");
111: renderURI(out, prefixesFor(c), c.getURI());
112: out.print(' ');
113: } else {
114: renderAnonymous(out, c, "class");
115: }
116: }
117: }
118:
119: // Internal implementation methods
120: //////////////////////////////////
121:
122: /**
123: * <p>Handle the case of rendering a restriction.</p>
124: * @param out The print stream to write to
125: * @param r The restriction to render
126: */
127: protected void renderRestriction(PrintStream out, Restriction r) {
128: if (!r.isAnon()) {
129: out.print("Restriction ");
130: renderURI(out, prefixesFor(r), r.getURI());
131: } else {
132: renderAnonymous(out, r, "restriction");
133: }
134:
135: out.println();
136:
137: renderRestrictionElem(out, " on property", r.getOnProperty());
138: out.println();
139:
140: if (r.isAllValuesFromRestriction()) {
141: renderRestrictionElem(out, " all values from", r
142: .asAllValuesFromRestriction().getAllValuesFrom());
143: }
144: if (r.isSomeValuesFromRestriction()) {
145: renderRestrictionElem(out, " some values from", r
146: .asSomeValuesFromRestriction().getSomeValuesFrom());
147: }
148: if (r.isHasValueRestriction()) {
149: renderRestrictionElem(out, " has value", r
150: .asHasValueRestriction().getHasValue());
151: }
152: }
153:
154: protected void renderRestrictionElem(PrintStream out, String desc,
155: RDFNode value) {
156: out.print(desc);
157: out.print(" ");
158: renderValue(out, value);
159: }
160:
161: protected void renderValue(PrintStream out, RDFNode value) {
162: if (value.canAs(OntClass.class)) {
163: renderClassDescription(out, (OntClass) value
164: .as(OntClass.class));
165: } else if (value instanceof Resource) {
166: Resource r = (Resource) value;
167: if (r.isAnon()) {
168: renderAnonymous(out, r, "resource");
169: } else {
170: renderURI(out, r.getModel(), r.getURI());
171: }
172: } else if (value instanceof Literal) {
173: out.print(((Literal) value).getLexicalForm());
174: } else {
175: out.print(value);
176: }
177: }
178:
179: protected void renderURI(PrintStream out, PrefixMapping prefixes,
180: String uri) {
181: out.print(prefixes.shortForm(uri));
182: }
183:
184: protected PrefixMapping prefixesFor(Resource n) {
185: return n.getModel().getGraph().getPrefixMapping();
186: }
187:
188: protected void renderAnonymous(PrintStream out, Resource anon,
189: String name) {
190: String anonID = (String) m_anonIDs.get(anon.getId());
191: if (anonID == null) {
192: anonID = "a-" + m_anonCount++;
193: m_anonIDs.put(anon.getId(), anonID);
194: }
195:
196: out.print("Anonymous ");
197: out.print(name);
198: out.print(" with ID ");
199: out.print(anonID);
200: }
201:
202: protected void renderBooleanClass(PrintStream out, String op,
203: BooleanClassDescription boolClass) {
204: out.print(op);
205: out.println(" of {");
206:
207: for (Iterator i = boolClass.listOperands(); i.hasNext();) {
208: out.print(" ");
209: renderClassDescription(out, (OntClass) i.next());
210: out.println();
211: }
212: out.print(" } ");
213: }
214:
215: //==============================================================================
216: // Inner class definitions
217: //==============================================================================
218:
219: }
220:
221: /*
222: (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
223: All rights reserved.
224:
225: Redistribution and use in source and binary forms, with or without
226: modification, are permitted provided that the following conditions
227: are met:
228:
229: 1. Redistributions of source code must retain the above copyright
230: notice, this list of conditions and the following disclaimer.
231:
232: 2. Redistributions in binary form must reproduce the above copyright
233: notice, this list of conditions and the following disclaimer in the
234: documentation and/or other materials provided with the distribution.
235:
236: 3. The name of the author may not be used to endorse or promote products
237: derived from this software without specific prior written permission.
238:
239: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
240: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
241: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
242: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
243: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
244: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
245: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
246: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
247: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
248: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
249: */
|