test

package edu.bupt.recommender.jena;

import java.util.*;

import com.hp.hpl.jena.db.*;
import com.hp.hpl.jena.ontology.*;
import com.hp.hpl.jena.rdf.model.*;

/**
 * 将本体数据存储到数据库中的通用类
 */
public class PersistentOntology {


	/**
	 * 将本体文件中的本体数据加入到本体数据库中
	 * @param maker
	 * @param source
	 * @param filePath
	 * @return
	 */
    public Model loadDBAndAddFromFile( ModelMaker maker, String source ,String filePath ) {
        // use the model maker to get the base model as a persistent model
        // strict=false, so we get an existing model by that name if it exists
        // or create a new one
        Model base = maker.createModel( source, false );

        // now we plug that base model into an ontology model that also uses
        // the given model maker to create storage for imported models
        OntModel m = ModelFactory.createOntologyModel( getModelSpec( maker ), base );
        m.getDocumentManager().addAltEntry(source, "file:"+filePath);

        // now load the source document, which will also load any imports
        m.read( source );
        
        m.commit();
        
        return m;
        
        
    }
    
    
    /**
     * 
     * @param maker
     * @param source
     * @return
     * 这个返回值可以作为参数传给创建本体并将本体保存到数据库的模块
     * 但是假如到数据库中的本体数据很多时,则会将所有数据同时加载到内存中
     */
    public OntModel loadDB(ModelMaker maker,String source){
    	Model base = maker.createModel(source, false);
    	System.out.println("in loadDB base= "+base.toString());
    	OntModel m = ModelFactory.createOntologyModel(getModelSpec(maker),base);
    	System.out.println("in loadDB m= "+m.toString());
    	return m;
    }
    
    /**
     * 将新的本体数据加入到本体数据库中
     * @param maker
     * @param source
     * @param addModel
     * @return
     */
    public Model addDB(ModelMaker maker,String source, Model addModel){
    	Model m = loadDB(maker,source);
    	m.add(addModel);
    	m.commit();
    	return m;
    }

    
    /*
    public void listClasses( ModelMaker maker, String modelID ) {
        // use the model maker to get the base model as a persistent model
        // strict=false, so we get an existing model by that name if it exists
        // or create a new one
        Model base = maker.createModel( modelID, false );

        // create an ontology model using the persistent model as base
        OntModel m = ModelFactory.createOntologyModel( getModelSpec( maker ), base );

        for (Iterator i = m.listClasses(); i.hasNext(); ) {
            OntClass c = (OntClass) i.next();
            System.out.println( "Class " + c.getURI() );
        }
    }
    */


    /**
     * 主要是为来方便将conn能够方便关闭,
     */
    public IDBConnection getDBConnection(String dbURL,String dbUser,String dbPw,String dbType){
    	IDBConnection conn = new DBConnection(dbURL,dbUser,dbPw,dbType);
    	return conn;
    }
    

    public ModelMaker getRDBMaker( IDBConnection conn, boolean cleanDB ) {
        try {
            // Create database connection
        	//使用完来之后如何将其从关闭,如何将此conn传出函数外
            //IDBConnection conn  = new DBConnection( dbURL, dbUser, dbPw, dbType );
            
            // do we need to clean the database?
            if (cleanDB) {
                conn.cleanDB();
            }

//             Create a model maker object
//            Model
//            ModelFactory
            return ModelFactory.createModelRDBMaker( conn );
        }
        catch (Exception e) {
            e.printStackTrace();
            System.exit( 1 );
        }

        return null;
    }
    
    public ModelMaker getRDBMaker( String dbURL,String dbUser,String dbPw,String dbType, boolean cleanDB ) {
        try {
            // Create database connection
        	//使用完来之后如何将其从关闭,如何将此conn传出函数外
            IDBConnection conn  = new DBConnection( dbURL, dbUser, dbPw, dbType );
            
            // do we need to clean the database?
            if (cleanDB) {
                conn.cleanDB();
            }

//             Create a model maker object
//            Model
//            ModelFactory
            return ModelFactory.createModelRDBMaker( conn );
        }
        catch (Exception e) {
            e.printStackTrace();
            System.exit( 1 );
        }

        return null;
    }

    /**
     * 
     * @param maker
     * @return
     */
    private OntModelSpec getModelSpec( ModelMaker maker ) {
        // create a spec for the new ont model that will use no inference over models
        // made by the given maker (which is where we get the persistent models from)
        OntModelSpec spec = new OntModelSpec( OntModelSpec.OWL_MEM );
        spec.setImportModelMaker( maker );

        return spec;
    }
}

 

你可能感兴趣的:(C++,c,C#,HP)