1 19 20 package com.hp.hpl.jena.ontology.event; 23 24 25 26 import java.util.*; 29 30 import com.hp.hpl.jena.ontology.*; 31 import com.hp.hpl.jena.rdf.listeners.StatementListener; 32 import com.hp.hpl.jena.rdf.model.*; 33 import com.hp.hpl.jena.vocabulary.*; 34 import com.hp.hpl.jena.vocabulary.OntEventsVocab; 35 36 37 80 public class OntEventManager 81 extends StatementListener 82 { 83 86 89 90 private static Object [][] s_rdfTypeInit = { 91 {OntEventsVocab.classDeclaration, new ProfileAccessor() {public Resource get(Profile p) {return p.CLASS();} } }, 92 {OntEventsVocab.datarangeDeclaration, new ProfileAccessor() {public Resource get(Profile p) {return p.DATARANGE();} } }, 93 {OntEventsVocab.propertyDeclaration, new ProfileAccessor() {public Resource get(Profile p) {return p.PROPERTY();} } }, 94 {OntEventsVocab.objectPropertyDeclaration, new ProfileAccessor() {public Resource get(Profile p) {return p.OBJECT_PROPERTY();} } }, 95 {OntEventsVocab.datatypePropertyDeclaration, new ProfileAccessor() {public Resource get(Profile p) {return p.DATATYPE_PROPERTY();} } }, 96 {OntEventsVocab.transitivePropertyDeclaration, new ProfileAccessor() {public Resource get(Profile p) {return p.TRANSITIVE_PROPERTY();} } }, 97 {OntEventsVocab.symmetricPropertyDeclaration, new ProfileAccessor() {public Resource get(Profile p) {return p.SYMMETRIC_PROPERTY();} } }, 98 {OntEventsVocab.functionalPropertyDeclaration, new ProfileAccessor() {public Resource get(Profile p) {return p.FUNCTIONAL_PROPERTY();} } }, 99 {OntEventsVocab.inverseFunctionalPropertyDeclaration, new ProfileAccessor() {public Resource get(Profile p) {return p.INVERSE_FUNCTIONAL_PROPERTY();} } }, 100 {OntEventsVocab.annotationPropertyDeclaration, new ProfileAccessor() {public Resource get(Profile p) {return p.ANNOTATION_PROPERTY();} } }, 101 {OntEventsVocab.ontologyPropertyDeclaration, new ProfileAccessor() {public Resource get(Profile p) {return p.ONTOLOGY_PROPERTY();} } }, 102 {OntEventsVocab.restrictionDeclaration, new ProfileAccessor() {public Resource get(Profile p) {return p.RESTRICTION();} } }, 103 {OntEventsVocab.allDifferentDeclaration, new ProfileAccessor() {public Resource get(Profile p) {return p.ALL_DIFFERENT();} } }, 104 {OntEventsVocab.ontologyDeclaration, new ProfileAccessor() {public Resource get(Profile p) {return p.ONTOLOGY();} } }, 105 }; 106 107 108 private static Object [][] s_predicateInit = { 109 {OntEventsVocab.intersectionOf, new ProfileAccessor() {public Resource get(Profile p) {return p.INTERSECTION_OF();} } }, 110 {OntEventsVocab.equivalentClass, new ProfileAccessor() {public Resource get(Profile p) {return p.EQUIVALENT_CLASS();} } }, 111 {OntEventsVocab.disjointWith, new ProfileAccessor() {public Resource get(Profile p) {return p.DISJOINT_WITH();} } }, 112 {OntEventsVocab.equivalentProperty, new ProfileAccessor() {public Resource get(Profile p) {return p.EQUIVALENT_PROPERTY();} } }, 113 {OntEventsVocab.sameAs, new ProfileAccessor() {public Resource get(Profile p) {return p.SAME_AS();} } }, 114 {OntEventsVocab.differentFrom, new ProfileAccessor() {public Resource get(Profile p) {return p.DIFFERENT_FROM();} } }, 115 {OntEventsVocab.distinctMembers, new ProfileAccessor() {public Resource get(Profile p) {return p.DISTINCT_MEMBERS();} } }, 116 {OntEventsVocab.unionOf, new ProfileAccessor() {public Resource get(Profile p) {return p.UNION_OF();} } }, 117 {OntEventsVocab.intersectionOf, new ProfileAccessor() {public Resource get(Profile p) {return p.INTERSECTION_OF();} } }, 118 {OntEventsVocab.complementOf, new ProfileAccessor() {public Resource get(Profile p) {return p.COMPLEMENT_OF();} } }, 119 {OntEventsVocab.oneOf, new ProfileAccessor() {public Resource get(Profile p) {return p.ONE_OF();} } }, 120 {OntEventsVocab.onProperty, new ProfileAccessor() {public Resource get(Profile p) {return p.ON_PROPERTY();} } }, 121 {OntEventsVocab.allValuesFrom, new ProfileAccessor() {public Resource get(Profile p) {return p.ALL_VALUES_FROM();} } }, 122 {OntEventsVocab.hasValue, new ProfileAccessor() {public Resource get(Profile p) {return p.HAS_VALUE();} } }, 123 {OntEventsVocab.someValuesFrom, new ProfileAccessor() {public Resource get(Profile p) {return p.SOME_VALUES_FROM();} } }, 124 {OntEventsVocab.minCardinality, new ProfileAccessor() {public Resource get(Profile p) {return p.MIN_CARDINALITY();} } }, 125 {OntEventsVocab.maxCardinality, new ProfileAccessor() {public Resource get(Profile p) {return p.MAX_CARDINALITY();} } }, 126 {OntEventsVocab.cardinalityQ, new ProfileAccessor() {public Resource get(Profile p) {return p.CARDINALITY_Q();} } }, 127 {OntEventsVocab.minCardinalityQ, new ProfileAccessor() {public Resource get(Profile p) {return p.MIN_CARDINALITY_Q();} } }, 128 {OntEventsVocab.maxCardinalityQ, new ProfileAccessor() {public Resource get(Profile p) {return p.MAX_CARDINALITY_Q();} } }, 129 {OntEventsVocab.cardinality, new ProfileAccessor() {public Resource get(Profile p) {return p.CARDINALITY();} } }, 130 {OntEventsVocab.inverseOf, new ProfileAccessor() {public Resource get(Profile p) {return p.INVERSE_OF();} } }, 131 {OntEventsVocab.imports, new ProfileAccessor() {public Resource get(Profile p) {return p.IMPORTS();} } }, 132 {OntEventsVocab.versionInfo, new ProfileAccessor() {public Resource get(Profile p) {return p.VERSION_INFO();} } }, 133 {OntEventsVocab.priorVersion, new ProfileAccessor() {public Resource get(Profile p) {return p.PRIOR_VERSION();} } }, 134 {OntEventsVocab.backwardCompatibleWith, new ProfileAccessor() {public Resource get(Profile p) {return p.BACKWARD_COMPATIBLE_WITH();} } }, 135 {OntEventsVocab.incompatibleWith, new ProfileAccessor() {public Resource get(Profile p) {return p.INCOMPATIBLE_WITH();} } }, 136 }; 137 138 139 142 143 private Map m_handlers = new HashMap(); 144 145 146 private Map m_rdfTypeToEventType = new HashMap(); 147 148 149 private Map m_predicateToEventType = new HashMap(); 150 151 152 private OntEventHandler m_defaultHandler = null; 153 154 155 158 164 public OntEventManager( OntModel m ) { 165 Profile p = m.getProfile(); 166 initialiseTable( m_rdfTypeToEventType, p, s_rdfTypeInit ); 167 initialiseTable( m_predicateToEventType, p, s_predicateInit ); 168 } 169 170 171 174 178 public void addedStatement( Statement s ) { 179 processStatement( s, true ); 180 } 181 182 183 187 public void removedStatement( Statement s ) { 188 processStatement( s, false ); 189 } 190 191 192 201 public void raise( Resource event, boolean added, Model source, RDFNode arg0, RDFNode arg1, RDFNode arg2 ) { 202 OntEventHandler h = getHandler( event ); 203 if (h != null) { 204 h.action( event, added, source, arg0, arg1, arg2 ); 205 } 206 else if (m_defaultHandler != null) { 207 m_defaultHandler.action( event, added, source, arg0, arg1, arg2 ); 209 } 210 } 211 212 213 218 public void addDefaultHandler( OntEventHandler handler ) { 219 m_defaultHandler = handler; 220 } 221 222 223 229 public void addHandler( Resource event, OntEventHandler handler ) { 230 m_handlers.put( event, handler ); 231 } 232 233 234 241 public void addHandlers( Object [][] handlers ) { 242 for (int i = 0; i < handlers.length; ) { 243 Resource r = (Resource) handlers[i][0]; 244 OntEventHandler h = (OntEventHandler) handlers[i][1]; 245 addHandler( r, h ); 246 } 247 } 248 249 254 public OntEventHandler getHandler( Resource event ) { 255 return (OntEventHandler) m_handlers.get( event ); 256 } 257 258 259 263 public OntEventHandler getDefaultHandler() { 264 return m_defaultHandler; 265 } 266 267 268 272 public void removeHandler( Resource event ) { 273 m_handlers.remove( event ); 274 } 275 276 277 282 public boolean hasHandler( Resource event ) { 283 return m_handlers.containsKey( event ); 284 } 285 286 287 292 public Iterator listRegisteredEvents() { 293 return m_handlers.keySet().iterator(); 294 } 295 296 297 300 307 private void initialiseTable( Map map, Profile p, Object [][] source ) { 308 for (int i = 0; i < source.length; i++) { 309 Resource evType = (Resource) source[i][0]; 311 Resource key = ((ProfileAccessor) source[i][1]).get( p ); 312 313 if (key != null) { 314 map.put( key, evType ); 316 } 317 } 318 } 319 320 321 326 private void processStatement( Statement s, boolean added ) { 327 if (s.getPredicate().equals( RDF.type )) { 329 Resource type = s.getResource(); 331 Resource evType = (Resource) m_rdfTypeToEventType.get( type ); 332 333 if (evType != null) { 334 raise( evType, added, s.getModel(), s.getSubject(), null, null ); 336 } 337 else { 338 raise( OntEventsVocab.individualDeclaration, added, s.getModel(), s.getSubject(), type, null ); 340 } 341 } 342 else { 343 Property pred = s.getPredicate(); 345 Resource evType = (Resource) m_predicateToEventType.get( pred ); 346 347 if (evType != null) { 348 raise( evType, added, s.getModel(), s.getSubject(), s.getObject(), null ); 350 } 351 else { 352 raise( OntEventsVocab.userData, added, s.getModel(), s.getSubject(), pred, s.getObject() ); 354 } 355 } 356 } 357 358 359 363 364 private static interface ProfileAccessor { 365 public Resource get( Profile p ); 366 } 367 } 368 369 370 396 | Popular Tags |