📄 onteventmanager.java
字号:
/*****************************************************************************
* Source code information
* -----------------------
* Original author Ian Dickinson, HP Labs Bristol
* Author email ian.dickinson@hp.com
* Package Jena 2
* Web http://sourceforge.net/projects/jena/
* Created 10-Sep-2003
* Filename $RCSfile: OntEventManager.java,v $
* Revision $Revision: 1.8 $
* Release status $State: Exp $
*
* Last modified on $Date: 2007/01/02 11:53:05 $
* by $Author: andy_seaborne $
*
* (c) Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2007 Hewlett-Packard Development Company, LP
* [See end of file]
*****************************************************************************/
// Package
///////////////
package com.hp.hpl.jena.ontology.event;
// Imports
///////////////
import java.util.*;
import com.hp.hpl.jena.ontology.*;
import com.hp.hpl.jena.rdf.listeners.StatementListener;
import com.hp.hpl.jena.rdf.model.*;
import com.hp.hpl.jena.vocabulary.*;
import com.hp.hpl.jena.vocabulary.OntEventsVocab;
/**
* <p>
* An adapter that translates RDF model-level changes into higher level changes that
* are appropriate to ontology users. The large number of specific events that the
* ontology model can produce makes the traditional Java style of listener interface
* impractical. Instead, this event manager allows the user to register
* {@linkplain OntEventHandler handlers}, based on command pattern, against specific
* {@linkplain OntEventsVocab event types}.
* </p>
* <p>
* For example, to register a handler for the declaration of an ontology class:
* </p>
* <pre>
* OntModel m = ...
* OntEventManager em = m.getEventManager();
* em.addHandler( OntEvents.CLASS_DECLARATION,
* new OntEventHandler() {
* public void action( Resource ev, boolean added,
* RDFNode arg0, RDFNode arg1 ) {
* OntClass c = (OntClass) arg0;
* if (added) {
* // class c added to model
* }
* else {
* // class c removed from model
* }
* }
* }
* );
* </pre>
* <p>
* This listener acts as an adapter for graph events (i.e. adding and
* removing triples), converting them to higher-level ontology events. This is non-trivial, because
* Jena currently doesn't have a means of batching changes so that only consistent
* graph states are seen. For efficiency in non event-using models, this listener
* is only attached as a statement listener to the underlying graph when the first
* ontology event listener is added.
* </p>
*
* @author Ian Dickinson, HP Labs
* (<a href="mailto:Ian.Dickinson@hp.com" >email</a>)
* @version CVS $Id: OntEventManager.java,v 1.8 2007/01/02 11:53:05 andy_seaborne Exp $
*/
public class OntEventManager
extends StatementListener
{
// Constants
//////////////////////////////////
// Static variables
//////////////////////////////////
/** Initialisation data for the rdf:type to event type table */
private static Object[][] s_rdfTypeInit = {
{OntEventsVocab.classDeclaration, new ProfileAccessor() {public Resource get(Profile p) {return p.CLASS();} } },
{OntEventsVocab.datarangeDeclaration, new ProfileAccessor() {public Resource get(Profile p) {return p.DATARANGE();} } },
{OntEventsVocab.propertyDeclaration, new ProfileAccessor() {public Resource get(Profile p) {return p.PROPERTY();} } },
{OntEventsVocab.objectPropertyDeclaration, new ProfileAccessor() {public Resource get(Profile p) {return p.OBJECT_PROPERTY();} } },
{OntEventsVocab.datatypePropertyDeclaration, new ProfileAccessor() {public Resource get(Profile p) {return p.DATATYPE_PROPERTY();} } },
{OntEventsVocab.transitivePropertyDeclaration, new ProfileAccessor() {public Resource get(Profile p) {return p.TRANSITIVE_PROPERTY();} } },
{OntEventsVocab.symmetricPropertyDeclaration, new ProfileAccessor() {public Resource get(Profile p) {return p.SYMMETRIC_PROPERTY();} } },
{OntEventsVocab.functionalPropertyDeclaration, new ProfileAccessor() {public Resource get(Profile p) {return p.FUNCTIONAL_PROPERTY();} } },
{OntEventsVocab.inverseFunctionalPropertyDeclaration, new ProfileAccessor() {public Resource get(Profile p) {return p.INVERSE_FUNCTIONAL_PROPERTY();} } },
{OntEventsVocab.annotationPropertyDeclaration, new ProfileAccessor() {public Resource get(Profile p) {return p.ANNOTATION_PROPERTY();} } },
{OntEventsVocab.ontologyPropertyDeclaration, new ProfileAccessor() {public Resource get(Profile p) {return p.ONTOLOGY_PROPERTY();} } },
{OntEventsVocab.restrictionDeclaration, new ProfileAccessor() {public Resource get(Profile p) {return p.RESTRICTION();} } },
{OntEventsVocab.allDifferentDeclaration, new ProfileAccessor() {public Resource get(Profile p) {return p.ALL_DIFFERENT();} } },
{OntEventsVocab.ontologyDeclaration, new ProfileAccessor() {public Resource get(Profile p) {return p.ONTOLOGY();} } },
};
/** Initialisation data for the predicate to event type table */
private static Object[][] s_predicateInit = {
{OntEventsVocab.intersectionOf, new ProfileAccessor() {public Resource get(Profile p) {return p.INTERSECTION_OF();} } },
{OntEventsVocab.equivalentClass, new ProfileAccessor() {public Resource get(Profile p) {return p.EQUIVALENT_CLASS();} } },
{OntEventsVocab.disjointWith, new ProfileAccessor() {public Resource get(Profile p) {return p.DISJOINT_WITH();} } },
{OntEventsVocab.equivalentProperty, new ProfileAccessor() {public Resource get(Profile p) {return p.EQUIVALENT_PROPERTY();} } },
{OntEventsVocab.sameAs, new ProfileAccessor() {public Resource get(Profile p) {return p.SAME_AS();} } },
{OntEventsVocab.differentFrom, new ProfileAccessor() {public Resource get(Profile p) {return p.DIFFERENT_FROM();} } },
{OntEventsVocab.distinctMembers, new ProfileAccessor() {public Resource get(Profile p) {return p.DISTINCT_MEMBERS();} } },
{OntEventsVocab.unionOf, new ProfileAccessor() {public Resource get(Profile p) {return p.UNION_OF();} } },
{OntEventsVocab.intersectionOf, new ProfileAccessor() {public Resource get(Profile p) {return p.INTERSECTION_OF();} } },
{OntEventsVocab.complementOf, new ProfileAccessor() {public Resource get(Profile p) {return p.COMPLEMENT_OF();} } },
{OntEventsVocab.oneOf, new ProfileAccessor() {public Resource get(Profile p) {return p.ONE_OF();} } },
{OntEventsVocab.onProperty, new ProfileAccessor() {public Resource get(Profile p) {return p.ON_PROPERTY();} } },
{OntEventsVocab.allValuesFrom, new ProfileAccessor() {public Resource get(Profile p) {return p.ALL_VALUES_FROM();} } },
{OntEventsVocab.hasValue, new ProfileAccessor() {public Resource get(Profile p) {return p.HAS_VALUE();} } },
{OntEventsVocab.someValuesFrom, new ProfileAccessor() {public Resource get(Profile p) {return p.SOME_VALUES_FROM();} } },
{OntEventsVocab.minCardinality, new ProfileAccessor() {public Resource get(Profile p) {return p.MIN_CARDINALITY();} } },
{OntEventsVocab.maxCardinality, new ProfileAccessor() {public Resource get(Profile p) {return p.MAX_CARDINALITY();} } },
{OntEventsVocab.cardinalityQ, new ProfileAccessor() {public Resource get(Profile p) {return p.CARDINALITY_Q();} } },
{OntEventsVocab.minCardinalityQ, new ProfileAccessor() {public Resource get(Profile p) {return p.MIN_CARDINALITY_Q();} } },
{OntEventsVocab.maxCardinalityQ, new ProfileAccessor() {public Resource get(Profile p) {return p.MAX_CARDINALITY_Q();} } },
{OntEventsVocab.cardinality, new ProfileAccessor() {public Resource get(Profile p) {return p.CARDINALITY();} } },
{OntEventsVocab.inverseOf, new ProfileAccessor() {public Resource get(Profile p) {return p.INVERSE_OF();} } },
{OntEventsVocab.imports, new ProfileAccessor() {public Resource get(Profile p) {return p.IMPORTS();} } },
{OntEventsVocab.versionInfo, new ProfileAccessor() {public Resource get(Profile p) {return p.VERSION_INFO();} } },
{OntEventsVocab.priorVersion, new ProfileAccessor() {public Resource get(Profile p) {return p.PRIOR_VERSION();} } },
{OntEventsVocab.backwardCompatibleWith, new ProfileAccessor() {public Resource get(Profile p) {return p.BACKWARD_COMPATIBLE_WITH();} } },
{OntEventsVocab.incompatibleWith, new ProfileAccessor() {public Resource get(Profile p) {return p.INCOMPATIBLE_WITH();} } },
};
// Instance variables
//////////////////////////////////
/** Map from event types to handlers */
private Map m_handlers = new HashMap();
/** Map from rdf:type to event type */
private Map m_rdfTypeToEventType = new HashMap();
/** Map from predicate to event type */
private Map m_predicateToEventType = new HashMap();
/** Default event handler */
private OntEventHandler m_defaultHandler = null;
// Constructors
//////////////////////////////////
/**
* <p>Construct an ontology event manager for the given ontology model.
* This involves registering adapters for the ontology events corresponding
* to the language profile of the given model.</p>
* @param m An ontology model
*/
public OntEventManager( OntModel m ) {
Profile p = m.getProfile();
initialiseTable( m_rdfTypeToEventType, p, s_rdfTypeInit );
initialiseTable( m_predicateToEventType, p, s_predicateInit );
}
// External signature methods
//////////////////////////////////
/**
* <p>Handle the addition of a statement to the model.</p>
* @param s The added statement
*/
public void addedStatement( Statement s ) {
processStatement( s, true );
}
/**
* <p>Handle the removal of a statement to the model</p>
* @param s The removed statement
*/
public void removedStatement( Statement s ) {
processStatement( s, false );
}
/**
* <p>Raise an event to be handled by the attached event handlers.</p>
* @param event The resource representing the event type
* @param added True if this is an addition to the model, false otherwise
* @param source The model that caused the event to be raised
* @param arg0 The first argument to the event
* @param arg1 The second argument to the event, or null
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -