⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ontmodelspecobsolete.java

📁 jena2.5.4推理机系统的一种最基本实现 HP实验室出品
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 	(c) Copyright 2006 Hewlett-Packard Development Company, LP
 	All rights reserved.
 	$Id: OntModelSpecObsolete.java,v 1.3 2007/06/11 13:34:49 chris-dollin Exp $
*/

package com.hp.hpl.jena.ontology;

import java.util.*;

import com.hp.hpl.jena.assembler.AssemblerHelp;
import com.hp.hpl.jena.db.IDBConnection;
import com.hp.hpl.jena.db.impl.DriverMap;
import com.hp.hpl.jena.rdf.model.*;
import com.hp.hpl.jena.rdf.model.impl.*;
import com.hp.hpl.jena.reasoner.*;
import com.hp.hpl.jena.reasoner.rulesys.impl.WrappedReasonerFactory;
import com.hp.hpl.jena.shared.*;
import com.hp.hpl.jena.vocabulary.*;

/**
    This class holds the necessary machinery for OntModelSpec to continue
    to implement construction-from-ModelSpec-RDF until the next release
    of Jena, after which it will be excised. 
    
    @author kers
 */

public abstract class OntModelSpecObsolete extends ModelSpecImpl
    {
    public OntModelSpecObsolete( ModelMaker maker )
        { super( maker ); }

    public abstract Model implementCreateModelOver( String name );

    protected abstract Model doCreateModel();

    /**
        Answer the RDFS property used to attach this ModelSpec to its ModelMaker; used
        by the parent classes when constructing the RDF description for this Spec.

        @return JenaModelSpec.importMaker
    */
    public Property getMakerProperty() 
        {
        return JenaModelSpec.importMaker;
        }
    
    /**
        Answer a ModelMaker described by the <code>makerProperty</code> of
        <code>root</code> in <code>description</code>; if there is no such statement,
        answer a memory-model maker.
     */
    protected static ModelMaker getMaker( Model description, Resource root, Property makerProperty )    
        {
        Statement mStatement = description.getProperty( root, makerProperty );
        return mStatement == null
            ? ModelFactory.createMemModelMaker()
            : createMaker( mStatement.getResource(), description );
        }
    
    /**
        Answer the unique subject with the given rdf:type.
        
        @param description the model in which the typed subject is sought
        @param type the RDF type the subject must have
        @return the unique S such that (S rdf:type type)
        @exception BadDescriptionException if there's not exactly one subject
    */        
    public static Resource findRootByType( Model description, Resource type )
        { 
        Model m = withSpecSchema( description );
        StmtIterator it = m.listStatements( null, RDF.type, type );
        if (!it.hasNext()) throw new BadDescriptionNoRootException( m, type );
        Resource root = it.nextStatement().getSubject();
        if (it.hasNext()) throw new BadDescriptionMultipleRootsException( m, type );
        return root; 
        }
        
    public static ModelMaker createMaker( Resource root, Model d )
        { return createMakerByRoot( root, withSpecSchema( d ) ); }
        
    public static ModelMaker createMakerByRoot( Resource root, Model fullDesc )
        { 
        Resource type = findMakerType( root, fullDesc );
        ModelMakerCreator mmc = findCreator( type );
        if (mmc == null) throw new RuntimeException( "no maker type" );  
        return mmc.create( fullDesc, root ); 
        }

    private static ModelMakerCreator findCreator( Resource type )
        {
        if (type.equals( JenaModelSpec.MakerSpec )) return new MemMakerCreator();
        if (type.equals( JenaModelSpec.FileMakerSpec )) return new FileMakerCreator();
        if (type.equals( JenaModelSpec.MemMakerSpec )) return new MemMakerCreator();
        if (type.equals( JenaModelSpec.RDBMakerSpec )) return new RDBMakerCreator();
        return null;
        }

    private static Resource findMakerType( Resource root, Model fullDesc )
        {
        return AssemblerHelp.findSpecificType( (Resource) root.inModel( fullDesc ), JenaModelSpec.MakerSpec );
        }

    public static Model withSpecSchema( Model m )
        {
        return withSchema( m, JenaModelSpec.getSchema() );
        }

    /**
        answer a limited RDFS closure of <code>m union schema</code>.
    */
    public static Model withSchema( Model m, Model schema )
        {
        Model result = ModelFactory.createDefaultModel();
        result.add( m );
        addJMSSubclassesFrom( result, schema );        
        addDomainTypes( result, m, schema );
        addSupertypesFrom( result, schema );
        addSupertypesFrom( result, m );
        return result;
        }
    protected static boolean notRDF( Resource resource )
        {
        if (resource.isAnon()) return true;
        if (resource.getNameSpace().equals( RDF.getURI() )) return false;
        if (resource.getNameSpace().equals( RDFS.getURI() )) return false;
        return true;
        }

    protected static final RDFNode nullObject = (RDFNode) null;

    protected static void addJMSSubclassesFrom( Model result, Model schema )
        {
        for (StmtIterator it = schema.listStatements( null, RDFS.subClassOf, nullObject ); it.hasNext();)
            { 
            Statement s = it.nextStatement();
            if (notRDF( s.getSubject() ) && notRDF( s.getResource() )) result.add( s ); 
            }
        }

    protected static void addDomainTypes( Model result, Model m, Model schema )
        {
        for (StmtIterator it = schema.listStatements( null, RDFS.domain, nullObject ); it.hasNext();)
            {
            Statement s = it.nextStatement();
            Property property = (Property) s.getSubject().as( Property.class );
            for (StmtIterator x = m.listStatements( null, property, nullObject ); x.hasNext();)
                {
                Statement t = x.nextStatement();
                // System.err.println( ">> adding domain type: subject " + t.getSubject() + ", type " + s.getObject() + " because of property " + property );
                result.add( t.getSubject(), RDF.type, s.getObject() );
                }
            }
        }

    protected static void addSupertypesFrom( Model result, Model source )
        {
        Model temp = ModelFactory.createDefaultModel();
        for (StmtIterator it = result.listStatements( null, RDF.type, nullObject ); it.hasNext();)
            {
            Statement s = it.nextStatement();
            for (StmtIterator subclasses = source.listStatements( s.getResource(), RDFS.subClassOf, nullObject ); subclasses.hasNext();)
                {
                RDFNode type = subclasses.nextStatement().getObject();
                // System.err.println( ">> adding super type: subject " + s.getSubject() + ", type " + type );
                temp.add( s.getSubject(), RDF.type, type );
                }
            }
        result.add( temp );
        }

    /**
         Answer a ReasonerFactory described by the properties of the resource
         <code>R</code> in the model <code>rs</code>. Will throw 
         NoReasonerSuppliedException if no jms:reasoner is supplied, or
         NoSuchReasonerException if the reasoner value isn't known to
         ReasonerRegistry. If any <code>ruleSetURL</code>s are supplied, the
         reasoner factory must be a RuleReasonerFactory, and is wrapped so that
         the supplied rules are specific to this Factory.
    */
    public static ReasonerFactory getReasonerFactory( Resource R, Model rs )
        {
        StmtIterator r = rs.listStatements( R, JenaModelSpec.reasoner, (RDFNode) null );
        if (r.hasNext() == false) throw new NoReasonerSuppliedException();
        Resource rr = r.nextStatement().getResource();
        String rrs = rr.getURI();
        ReasonerFactory rf = ReasonerRegistry.theRegistry().getFactory( rrs );
        if (rf == null) throw new NoSuchReasonerException( rrs );
        return new WrappedReasonerFactory( rf, ((Resource) R.inModel( rs )) );
        }

    /**
        Answer a ReasonerFactory as described by the reasonsWith part of this discription,
        or null if no reasoner specification has been supplied.

        @param description the description of this OntModel
        @param root the root of this OntModel's description
        @return  a ReasonerFactory with URI given by root's reasonsWith's reasoner.
    */
    public static ReasonerFactory getReasonerFactory( Model description, Resource root ) 
        {
        Statement factStatement = description.getProperty( root, JenaModelSpec.reasonsWith );
        if (factStatement == null) return null;
        return OntModelSpec.getReasonerFactory( factStatement.getResource(), description );
        }

    /**
        Answer an OntDocumentManager satisfying the docManager part of this description.
        Currently restricted to one where the object of JenaModelSpec.docManager is registered with
        the value table held in ModelSpecImpl. If there's no such property, or if its bnode
        has no associated value, returns null.

         @param description the description of the OntModel
         @param root the root of the description
         @return the OntDocumentManager of root's JenaModelSpec.docManager
    */
    public static OntDocumentManager getDocumentManager( Model description, Resource root ) 
        {
        Statement docStatement = description.getProperty( root, JenaModelSpec.docManager );
        if (docStatement == null) return null;
        Resource manager = docStatement.getResource();
        Statement policy = description.getProperty( manager, JenaModelSpec.policyPath );
        if (policy == null)
            return (OntDocumentManager) getValue( manager );
        else
            return new OntDocumentManager( policy.getString() );
        }

    /**
     * </p>Answer the ModelMaker to be used to construct models that are used for
     * the imports of an OntModel. The ModelMaker is specified by the properties of
     * the resource which is the object of the root's <code>jms:importMaker</code> property.
     * If no importMaker is specified, a MemModelMaker is returned as a default.</p>
     * @param description the description model for this OntModel
     * @param root the root of the description for the OntModel
     * @return a ModelMaker fitting the given description
     */
    public static ModelMaker getImportMaker( Model description, Resource root ) 
        {
        return getMaker( description, root, JenaModelSpec.importMaker );
        }

    /**
     * </p>Answer the ModelMaker to be used to construct models that are used for
     * the base model of an OntModel. The ModelMaker is specified by the properties of

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -