1 17 18 21 22 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 ; 29 import java.util.*; 30 31 32 43 public class ClassHierarchy { 44 47 50 53 protected OntModel m_model; 54 private Map m_anonIDs = new HashMap(); 55 private int m_anonCount = 0; 56 57 58 59 62 65 66 public void showHierarchy( PrintStream 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 76 79 protected void showClass( PrintStream out, OntClass cls, List occurs, int depth ) { 80 renderClassDescription( out, cls, depth ); 81 out.println(); 82 83 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 occurs.add( cls ); 90 showClass( out, sub, occurs, depth + 1 ); 91 occurs.remove( cls ); 92 } 93 } 94 } 95 96 97 102 public void renderClassDescription( PrintStream 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 125 protected void renderRestriction( PrintStream 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 139 protected void renderURI( PrintStream out, PrefixMapping prefixes, String uri ) { 140 out.print( prefixes.usePrefix( uri ) ); 141 } 142 143 144 protected void renderAnonymous( PrintStream out, Resource anon, String name ) { 145 String anonID = (String ) 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 158 protected void indent( PrintStream out, int depth ) { 159 for (int i = 0; i < depth; i++) { 160 out.print( " " ); 161 } 162 } 163 164 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 if (c.isAnon()) { 180 continue; 181 } 182 183 if (c.hasSuperClass( m.getProfile().THING(), true ) ) { 184 roots.add( c ); 186 } 187 else if (c.getCardinality( m.getProfile().SUB_CLASS_OF() ) == 0 ) { 188 roots.add( c ); 190 } 191 } 192 193 return roots.iterator(); 194 } 195 196 200 } 201 | Popular Tags |