view src/de/mpiwg/itgroup/nimanager/owl/TripleStoreHandler.java @ 0:1384a0d382fa

first input
author dwinter
date Thu, 30 Jun 2011 11:44:24 +0200
parents
children e3ecb88314a5
line wrap: on
line source

package de.mpiwg.itgroup.nimanager.owl;

import info.aduna.iteration.Iteration;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;
import org.openrdf.model.Resource;
import org.openrdf.model.Statement;
import org.openrdf.model.URI;
import org.openrdf.model.Value;
import org.openrdf.model.impl.ContextStatementImpl;
import org.openrdf.model.impl.LiteralImpl;
import org.openrdf.model.impl.StatementImpl;
import org.openrdf.model.impl.URIImpl;
import org.openrdf.query.MalformedQueryException;
import org.openrdf.query.QueryEvaluationException;
import org.openrdf.query.QueryLanguage;
import org.openrdf.query.TupleQuery;
import org.openrdf.query.TupleQueryResult;
import org.openrdf.query.impl.DatasetImpl;
import org.openrdf.repository.Repository;
import org.openrdf.repository.RepositoryConnection;
import org.openrdf.repository.RepositoryException;
import org.openrdf.repository.RepositoryResult;
import org.openrdf.repository.http.HTTPRepository;
import org.openrdf.rio.RDFHandlerException;
import org.openrdf.rio.rdfxml.RDFXMLWriter;

import virtuoso.sesame2.driver.VirtuosoRepository;

import com.hp.hpl.jena.vocabulary.OWL;

import de.mpiwg.itgroup.nimanager.exceptions.TripleStoreHandlerException;


import edu.stanford.smi.protege.widget.StringListWidget;
import edu.stanford.smi.protegex.owl.model.OWLIndividual;
import edu.stanford.smi.protegex.owl.model.OWLModel;
import edu.stanford.smi.protegex.owl.model.OWLNamedClass;
import edu.stanford.smi.protegex.owl.model.RDFProperty;
import edu.stanford.smi.protegex.owl.model.RDFResource;
import edu.stanford.smi.protegex.owl.model.RDFSLiteral;
import edu.stanford.smi.protegex.owl.model.impl.AbstractRDFSClass;
import edu.stanford.smi.protegex.owl.model.impl.DefaultRDFProperty;



public class TripleStoreHandler {

	public static String ONTOLOGY_NS="http://ontologies.mpiwg-berlin.mpg.de/authorities/namedIdentities/";
	private boolean save=true; // wenn false dann schreibt write nicht ins repositorium.
	
	private Logger logger = Logger.getRootLogger();
	
	static  Map<String,TripleStoreHandler> instance = new HashMap<String, TripleStoreHandler>();
	private Repository repository;

	private int counter=0;
	public Connection sqlCon;
	public Repository getRepository() {
		return repository;
	}
	public void setRepository(Repository repository) {
		this.repository = repository;
	}
	public TripleStoreHandler(String url,String username, String password) throws TripleStoreHandlerException {
		//String sesameUrl="http://127.0.0.1:8180/openrdf-sesame/repositories/authorityDataPersons";
		//String sesameUrl="http://127.0.0.1:8180/openrdf-sesame/repositories/personsRepository";

		//String sesameUrl="http://127.0.0.1:8180/openrdf-sesame/repositories/swiftowlimTest";
		//
		//repository = new HTTPRepository(sesameUrl);
		
		
		repository = new VirtuosoRepository(url, username, password);
		try {
			Class.forName("virtuoso.jdbc4.Driver");
			String connectString = "jdbc:virtuoso://virtuoso.mpiwg-berlin.mpg.de:1111/charset=UTF-8";
			this.sqlCon = DriverManager.getConnection(connectString, username,
					password);
		} catch (Exception e) {
			throw new TripleStoreHandlerException();
		}
	}
	public Map<String,Object> getJenaRDFValues(Collection props, String identifier, OWLModel model) throws RepositoryException {
		identifier = identifier.replace("<", "");
		identifier = identifier.replace(">", "");
		Map<String,Object> retMap = new HashMap<String, Object>();
		
		URI subj= repository.getValueFactory().createURI(identifier);
		RepositoryConnection con = repository.getConnection();
		Iterator propsIterator = props.iterator();
		while(propsIterator.hasNext()){
		//for(DefaultRDFProperty prop:props){
			DefaultRDFProperty prop=(DefaultRDFProperty) propsIterator.next();
			Object newValue;
			String name=prop.getName();
			//System.out.println(name);
			RepositoryResult<Statement> statements = con.getStatements(subj, repository.getValueFactory().createURI(name), null, true);
			//RepositoryResult<Statement> statements = con.getStatements(subj, null, null, false);
			//System.out.println(statements);
			statements.enableDuplicateFilter();
			while(statements.hasNext()){
				Statement sm = statements.next();
				Value val = sm.getObject();
				if (LiteralImpl.class.isInstance(val)) { // wenn ein
					// string
					// literal,
					// dann
					// uebersetze
					// in jena
					// string
					// literal
					LiteralImpl li = (LiteralImpl) val;

					newValue = model.createRDFSLiteralOrString(li.getLabel(),li.getLanguage());
					} else { // anderfalls erzeuge generische Instance
						
						
						newValue= model.getRDFResource(val.stringValue());
						
						if (newValue==null){
							OWLNamedClass owlclass = (OWLNamedClass) model
						.getOWLNamedClass("http://www.w3.org/2002/07/owl#Thing");
						newValue = (OWLIndividual) owlclass.createInstance(val.stringValue());
						}
					}	
				retMap.put(sm.getPredicate().stringValue(), newValue);
				
			}
		}
		return retMap;
		
	}
	public Map<String,String> getResources(String subj, String pred, String obj, String lang) throws RepositoryException {
		
		Map<String,String> retMap = new HashMap<String, String>();
		RepositoryConnection con = repository.getConnection();
		RepositoryResult<Statement> statements = con.getStatements(repository.getValueFactory().createURI(subj), repository.getValueFactory().createURI(pred),  repository.getValueFactory().createLiteral(obj, lang), true);
		statements.enableDuplicateFilter();
		while(statements.hasNext()){
			Statement sm = statements.next();
			retMap.put(sm.getSubject().stringValue(), sm.getObject().stringValue());
			
		}
	
	return retMap;
	}

	public TupleQueryResult querySPARQL(String queryString) throws MalformedQueryException, QueryEvaluationException, TripleStoreHandlerException{
		 TupleQuery tupleQuery;
		try {
			tupleQuery = repository.getConnection().prepareTupleQuery(QueryLanguage.SPARQL, queryString);
		} catch (RepositoryException e) {
			// possible a time out
			//wait a bit
			try {
				Thread.sleep(10000);
				//try again
				tupleQuery = repository.getConnection().prepareTupleQuery(QueryLanguage.SPARQL, queryString);
			} catch (InterruptedException e1) {
				e1.printStackTrace();
				throw new TripleStoreHandlerException();
			} catch (RepositoryException e2) {
				try {
					//ok try ones more
					Thread.sleep(10000);
					tupleQuery = repository.getConnection().prepareTupleQuery(QueryLanguage.SPARQL, queryString);
					} catch (InterruptedException e1) {
					e1.printStackTrace();
					throw new TripleStoreHandlerException();
					} catch (RepositoryException e3) {
						e.printStackTrace();
						throw new TripleStoreHandlerException();
						
					}
			}
		}
         return tupleQuery.evaluate();
      
	}
	
	public TupleQueryResult querySPARQL(String queryString,String context) throws RepositoryException, MalformedQueryException, QueryEvaluationException{
		 TupleQuery tupleQuery = repository.getConnection().prepareTupleQuery(QueryLanguage.SPARQL, queryString);
		 DatasetImpl ds = new DatasetImpl();
		 ds.addNamedGraph(createUri(repository,context));
		 tupleQuery.setDataset(ds);
        return tupleQuery.evaluate();
     
	}
	public RepositoryResult<Statement> getStatements(String subj,String pred, String obj) throws RepositoryException {
		RepositoryConnection con = repository.getConnection();
		
		RepositoryResult<Statement> statements = con.getStatements(createUri(repository,subj), createUri(repository,pred),  createUri(repository,obj), true);
		
	
	return statements;
	}
	
	public RepositoryResult<Statement> getStatements(String subj,String pred, String obj, String ctx) throws RepositoryException {
		RepositoryConnection con = repository.getConnection();
	
		RepositoryResult<Statement> statements = con.getStatements(createUri(repository,subj), createUri(repository,pred),  createUri(repository,obj), true, createUri(repository, ctx));
		
	
	return statements;
	}
	
	public RepositoryResult<Statement> getStatements(Resource subj,URI pred, Value obj, String ctx) throws RepositoryException {
		RepositoryConnection con = repository.getConnection();
		
		RepositoryResult<Statement> statements = con.getStatements(subj, pred, obj, true, createUri(repository, ctx));
		
	
	return statements;
	}
	
	public URI createUri(String subj){
		return createUri(repository, subj);
	}
	
	public Value createLiteral(String subj){
		return repository.getValueFactory().createLiteral(subj);
	}
	
	public Value createLiteral(String subj, String lang){
		return repository.getValueFactory().createLiteral(subj,lang);
	}
	
	
	public static URI createUri(Repository repository2, String subj) {
		if (subj==null)
			return null;
		return repository2.getValueFactory().createURI(subj);
		
	}
	public static Statement getFirstStatement(
			RepositoryResult<Statement> namesStatements) throws RepositoryException {
		while(namesStatements.hasNext()){
			return namesStatements.next();
		}
		
		return null;
	}
	public void write(OWLIndividual ent,String ctx) throws RepositoryException, TripleStoreHandlerException {
		
		
		String ident = ent.getURI();
//		Iterator<Object> types = ent.getRDFTypes().iterator();
//		
//		while(types.hasNext()){
//			
//			Object typeObj=types.next();
//			AbstractRDFSClass type = (AbstractRDFSClass)typeObj;
//			
//			
//			write(ident,"http://www.w3.org/1999/02/22-rdf-syntax-ns#type",type.getURI());
//		}
		
		Iterator<RDFProperty> props=ent.getPossibleRDFProperties().iterator();
		while(props.hasNext()){
			RDFProperty prop=props.next();
			Object val = ent.getPropertyValue(prop);
			if (val==null){ // nicht belegter wert
				continue;
			}
			if (RDFSLiteral.class.isInstance(val)){
				RDFSLiteral lit = (RDFSLiteral)val;
				String lang = lit.getLanguage();
				String litobj = lit.getString();
				Value newLit=null;
				if(lang!=null){
					newLit=repository.getValueFactory().createLiteral((String)lit.getString(), lang);
				} else {
						newLit=repository.getValueFactory().createLiteral((String)litobj);
					
				
				} 
				write(ident, prop.getURI(), newLit, ctx);
			} else if(OWLIndividual.class.isInstance(val)){
				OWLIndividual ind = (OWLIndividual)val;
				write(ident, prop.getURI(), repository.getValueFactory().createURI(ind.getURI()),ctx);
			} else if (AbstractRDFSClass.class.isInstance(val)){
				AbstractRDFSClass ind = (AbstractRDFSClass)val;
				write(ident, prop.getURI(), repository.getValueFactory().createURI(ind.getURI()),ctx);
			} else if (String.class.isInstance(val)){
				write(ident, prop.getURI(), repository.getValueFactory().createLiteral((String)val),ctx);
			} else {
				//don't now this type
				throw new TripleStoreHandlerException();
			}
			
		}
		
	}
	public OWLIndividual getProvider(MetaDataHandler mh, String providerUri) throws RepositoryException {
		RepositoryConnection con = repository.getConnection();
		URI createURIProvider = repository.getValueFactory().createURI(ONTOLOGY_NS+"NamedEntityProvider");
		
		RepositoryResult<Statement> provider = con.getStatements(repository.getValueFactory().createURI(providerUri), repository.getValueFactory().createURI("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"), createURIProvider, true);
		Statement providerStatement = getFirstStatement(provider);
		if(providerStatement==null) // provider does not exist
			return null;
		
		
		Map<String, Object> values = getJenaRDFValues(mh.getAssociatedPropsForClass(createURIProvider.stringValue()),providerUri,mh.getOwlModel());
		OWLIndividual providerEnt = mh.generateEntity(createURIProvider.stringValue(), providerUri,values);
		
		return providerEnt;
	}
	
	public void write(String subj, String pred, String obj,String ctx) throws RepositoryException {
		Value ob;
		RepositoryConnection con = repository.getConnection();
		ob=repository.getValueFactory().createURI(obj);
		Statement statement = new ContextStatementImpl(repository.getValueFactory().createURI(subj), repository.getValueFactory().createURI(pred), ob,repository.getValueFactory().createURI(ctx));
		logger.debug("Write to triple store:"+subj+":"+pred+":"+obj);
		logger.debug("Wrote statement:");
		counter+=1;
		logger.debug(counter);
		if (save){
		con.add(statement);
		con.commit();
		con.close();
		}
	}
	
	public void write(String subj, String pred, Value obj, String ctx) throws RepositoryException {
		
		RepositoryConnection con = repository.getConnection();
		Statement statement = new ContextStatementImpl(repository.getValueFactory().createURI(subj), repository.getValueFactory().createURI(pred), obj,repository.getValueFactory().createURI(ctx));
		if(LiteralImpl.class.isInstance(obj)){
			logger.debug("Write to triple store:"+subj+":"+pred+":"+((LiteralImpl)obj).stringValue());
		} else
			logger.debug("Write to triple store:"+subj+":"+pred+":"+obj.toString());
		
		logger.debug("Wrote statement:");
		counter+=1;
		logger.debug(counter);
		
		if (save){
		con.add(statement);
		con.commit();
		con.close();
		}
	}

	public void exportCtx(String ctx, File file) throws RepositoryException, RDFHandlerException, IOException{
		
		RepositoryConnection conn = repository.getConnection();
		OutputStream output = (file != null) ? new FileOutputStream(file) : System.out;
        
		RDFXMLWriter rdfxmlfWriter = new RDFXMLWriter(output);
        //conn.export(rdfxmlfWriter,  repository.getValueFactory().createURI(ctx));
		conn.exportStatements(null, null, null, true, rdfxmlfWriter, repository.getValueFactory().createURI(ctx));
        output.write('\n');
        conn.close();
	}
	
	public static void main(String args[]) throws RepositoryException, RDFHandlerException, IOException, TripleStoreHandlerException{
		TripleStoreHandler th = new TripleStoreHandler("jdbc:virtuoso://virtuoso.mpiwg-berlin.mpg.de:1111", "dba", "dba");
		//th.exportCtx("http://import", new File("/tmp/out.rdf"));
		//
		th.exportCtx("file://out.owl", new File("/tmp/out2.rdf"));
		
	}
	
	
	public static TripleStoreHandler getInstance(String url, String user, String password) throws TripleStoreHandlerException{
		
		String identString=url+"_"+user+"_"+password;
		
		if (instance.get(identString)==null){
			instance.put(identString,  new TripleStoreHandler(url,user,password));
		}
		TripleStoreHandler th= instance.get(identString);
		return th;
	}
}