📄 ontdocumentmanager.java
字号:
StringTokenizer pathElems = new StringTokenizer( configPath, FileManager.PATH_DELIMITER );
while (in == null && pathElems.hasMoreTokens()) {
uri = pathElems.nextToken();
in = fm.openNoMap( uri );
}
if (in != null) {
String syntax = FileUtils.guessLang(uri);
Model model = ModelFactory.createDefaultModel() ;
model.read( in, uri, syntax );
m_policyURL = uri;
return model;
}
}
catch (JenaException e) {
log.warn( "Exception while reading configuration: " + e.getMessage(), e);
}
return null;
}
/**
* <p>
* Load the ontology specification metadata from the model into the local
* mapping tables.
* </p>
*
* @param metadata A model containing metadata about ontologies.
*/
protected void processMetadata( Model metadata ) {
// there may be configuration for the location mapper in the ODM metadata file
getFileManager().getLocationMapper().processConfig( metadata );
// first we process the general policy statements for this document manager
for (ResIterator i = metadata.listSubjectsWithProperty( RDF.type, DOC_MGR_POLICY ); i.hasNext(); ) {
Resource policy = i.nextResource();
// iterate over each policy statement
for (StmtIterator j = policy.listProperties(); j.hasNext(); ) {
Statement s = j.nextStatement();
Property pred = s.getPredicate();
if (pred.equals( CACHE_MODELS )) {
setCacheModels( s.getBoolean() );
}
else if (pred.equals( PROCESS_IMPORTS )) {
setProcessImports( s.getBoolean() );
}
else if (pred.equals( IGNORE_IMPORT )) {
addIgnoreImport( s.getResource().getURI() );
}
else if (pred.equals( USE_DECLARED_NS_PREFIXES )) {
setUseDeclaredPrefixes( s.getBoolean() );
}
}
}
// then we look up individual meta-data for particular ontologies
for (ResIterator i = metadata.listSubjectsWithProperty( RDF.type, ONTOLOGY_SPEC ); i.hasNext(); ) {
Resource root = i.nextResource();
Statement s = root.getProperty( PUBLIC_URI );
if (s != null) {
// this will be the key in the mappings
String publicURI = s.getResource().getURI();
// there may be a cached copy for this ontology
s = root.getProperty( ALT_URL );
if (s != null) addAltEntry( publicURI, s.getResource().getURI() );
// there may be a standard prefix for this ontology
s = root.getProperty( PREFIX );
if (s != null) {
// if the namespace doesn't end with a suitable split point character, add a #
boolean endWithNCNameCh = XMLChar.isNCName( publicURI.charAt( publicURI.length() - 1 ) );
String prefixExpansion = endWithNCNameCh ? (publicURI + ANCHOR) : publicURI;
addPrefixMapping( prefixExpansion, s.getString() );
}
// there may be a language specified for this ontology
s = root.getProperty( LANGUAGE );
if (s != null) addLanguageEntry( publicURI, s.getResource().getURI() );
}
else {
log.warn( "Ontology specification node lists no public URI - node ignored");
}
}
}
/**
* <p>
* Load the document referenced by the given URI into the model. The cache will be
* used if permitted by the policy, and the imports of loaded model will be added to
* the end of the queue.
* </p>
*
* @param model The composite model to load into
* @param importURI The URI of the document to load
* @param readQueue Cumulative read queue for this operation
*/
protected void loadImport( OntModel model, String importURI, List readQueue ) {
if (m_processImports) {
log.debug( "OntDocumentManager loading " + importURI );
// add this model to occurs check list
model.addLoadedImport( importURI );
Model in = fetchPossiblyCachedImportModel( model, importURI );
// we trap the case of importing ourself (which may happen via an indirect imports chain)
if (in != model) {
// queue the imports from the input model on the end of the read queue
queueImports( in, readQueue, model.getProfile() );
// add to the imports union graph, but don't do the rebind yet
model.addSubModel( in, false );
// we also cache the model if we haven't seen it before (and caching is on)
addModel( importURI, in );
}
}
}
/**
if we have a cached version get that, otherwise load from the URI but don't do the imports closure
* @param model
* @param importURI
* @return
*/
private Model fetchPossiblyCachedImportModel( OntModel model, String importURI ) {
Model in = getModel( importURI );
// if not cached, we must load it from source
if (in == null) {
in = fetchLoadedImportModel( model.getSpecification(), importURI );
}
return in;
}
/**
* @param spec
* @param importURI
* @return
*/
private Model fetchLoadedImportModel( OntModelSpec spec, String importURI ) {
// workaround - default model maker can apparently create models that are closed
// TODO: this really suggests a bug in ModelMaker, kers to investigate
ModelMaker maker = spec.getImportModelMaker();
if (maker.hasModel( importURI )) {
Model m = maker.getModel( importURI );
if (!m.isClosed()) {
return m;
}
else {
// we don't want to hang on to closed models
maker.removeModel( importURI );
}
}
// otherwise, we use the model maker to get the model anew
Model m = spec.getImportModelGetter()
.getModel( importURI, new ModelReader() {
public Model readModel( Model toRead, String URL ) {
read( toRead, URL, true );
return toRead;
}
} );
return m;
}
/**
* <p>
* Load into the given model from the given URI, or from a local cache URI if defined.
* </p>
*
* @param model A model to read statements into
* @param uri A URI string
* @param warn If true, warn on RDF exception
* @return True if the uri was read successfully
*/
protected boolean read( Model model, String uri, boolean warn ) {
boolean success = false;
try {
// invoke the pre-read hook
String source = m_readHook.beforeRead( model, uri, this );
if (source == null) {
log.warn( "Read hook returned null source, so assuming old value: " + uri );
source = uri;
}
else {
// do the actual read
getFileManager().readModel( model, source );
}
// now the post-read hook
m_readHook.afterRead( model, source, this );
success = true;
}
catch (Exception e) {
// if there is a read failure handler, invoke it now
if (getReadFailureHandler() != null) {
getReadFailureHandler().handleFailedRead( uri, model, e );
}
else {
// otherwise, log the error
log.warn( "An error occurred while attempting to read from " + uri + ". Msg was '" + e.getMessage() + "'.", e );
}
}
return success;
}
/**
* <p>Set the default option settings.</p>
*/
protected void setDefaults() {
setCacheModels( true );
setUseDeclaredPrefixes( true );
setProcessImports( true );
}
//==============================================================================
// Inner class definitions
//==============================================================================
/**
* Interface defining a handler call-back in the case that the {@link OntDocumentManager}
* fails in an attempt to read the contents of a URL into a model.
*/
public static interface ReadFailureHandler
{
/**
* Behaviour to invoke when the {@link OntDocumentManager} tries and fails
* to read an ontology document from a given URL.
* @param url The URL that the OntDocumentManager was trying to read
* @param model The model that the OntDocumentManager is reading into
* @param e An exception indicating the reason for the failure to read the document
*/
public void handleFailedRead( String url, Model model, Exception e );
}
/**
* Interface denoting a handler class that can intervene in the process of
* reading a source document into a model.
*/
public static interface ReadHook
{
/**
* <p>Behaviour that is invoked <strong>before</strong> the contents of the
* given source (URI or filename) are read into the given model. The return
* value from this method denotes a revised string to use in place of the
* supplied source string. Handlers are permitted to make state changes
* to the model and the ODM, but carefully!</p>
*
* @param model The model that is going to receive the contents of the source
* @param source The identity of the source, as a file name or URI
* @param odm The Ont Document Manager invoking this handler
* @return The revised name of the source (or the same string if no
* change to the source is required). Note that if this method returns
* <code>null</code>, the source <strong>will not be subsequently read.</strong>
*/
public String beforeRead( Model model, String source, OntDocumentManager odm );
/**
* <p>Behaviour that is invoked <strong>just after</strong> the contents of the
* given source (URI or filename) have been read into the given model.
* Handlers are permitted to make state changes
* to the model and the ODM, but carefully!</p>
*
* @param model The model that is going to receive the contents of the source
* @param source The identity of the source, as a file name or URI
* @param odm The Ont Document Manager invoking this handler
*/
public void afterRead( Model model, String source, OntDocumentManager odm );
}
/**
* The default implementation of {@link OntDocumentManager.ReadHook} makes no changes.
*/
public static class DefaultReadHook
implements ReadHook
{
public void afterRead( Model model, String source, OntDocumentManager odm ) {
// do nothing
}
public String beforeRead( Model model, String source, OntDocumentManager odm ) {
return source;
}
}
}
/*
(c) Copyright 2002, 2003, 2004, 2005, 2006, 2007 Hewlett-Packard Development Company, LP
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -