1 17 18 21 import java.io.PrintStream ; 24 import java.util.*; 25 import java.util.Iterator ; 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 46 public class DescribeClass { 47 50 51 52 55 58 private Map m_anonIDs = new HashMap(); 59 private int m_anonCount = 0; 60 61 62 65 68 81 public void describeClass( PrintStream out, OntClass cls ) { 82 renderClassDescription( out, cls ); 83 out.println(); 84 85 for (Iterator 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 for (Iterator 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 105 public void renderClassDescription( PrintStream 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 134 139 protected void renderRestriction( PrintStream 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 out, String desc, RDFNode value ) { 165 out.print( desc ); 166 out.print( " " ); 167 renderValue( out, value ); 168 } 169 170 protected void renderValue( PrintStream 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 out, PrefixMapping prefixes, String 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 out, Resource anon, String name ) { 200 String anonID = (String ) 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 out, String op, BooleanClassDescription boolClass ) { 213 out.print( op ); 214 out.println( " of {" ); 215 216 for (Iterator 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 229 } 230 231 | Popular Tags |