Skip to content
mislusnys edited this page Nov 29, 2013 · 3 revisions
import inf.unibz.it.obda.owlapi.OWLAPIController;
import inf.unibz.it.obda.owlapi.sparql.SPARQLPrefixBuilder;
import inf.unibz.it.ucq.domain.QueryResult;
import inf.unibz.it.ucq.domain.UnionOfConjunctiveQueries;
import inf.unibz.it.ucq.parser.sparql.UCQTranslator;
import it.fub.inf.quonto.owlapi.QuontoOWLReasonerFactory;
import it.fub.inf.quonto.owlapi.QuontoOWLReasonerWrapper;
import it.uniroma1.dis.quonto.QuontoConfiguration;

import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;

import org.semanticweb.owl.apibinding.OWLManager;
import org.semanticweb.owl.model.OWLOntology;
import org.semanticweb.owl.model.OWLOntologyManager;

import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryFactory;

public class example3 {

	public static String	owlfile		= "/Users/mariano/ontologies/example3/example3.owl";
	public static String	configFile	= "config.properties";

	public static void main(String args[]) {
		QuontoOWLReasonerWrapper reasoner = null;
		try {
			
			// Loading the OWL file
			OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
			OWLOntology ontology = manager.loadOntologyFromPhysicalURI((new File(owlfile)).toURI());

			// Loading the OBDA data (note, the obda file must be in the same folder as the owl file
			OWLAPIController controller = new OWLAPIController(manager, ontology);
			controller.loadData(new File(owlfile).toURI());
			
			// Creating a new instance of a quonto reasoner
			QuontoOWLReasonerFactory factory = new QuontoOWLReasonerFactory();
			factory.setOBDAController(controller);
			reasoner = (QuontoOWLReasonerWrapper) factory.createReasoner(manager);
			reasoner.loadOntologies(manager.getOntologies());
			
			// Loading a set of configurations for the reasoner and giving them to quonto
			Properties properties = new Properties();
			properties.load(new FileInputStream(configFile));
			QuontoConfiguration config = new QuontoConfiguration(properties);
			reasoner.setConfiguration(config);
			
			// One time classification call.
			reasoner.classify();
			
			// Now we are ready for querying
			
			// The SPARQL query
			String sparqlstr = "SELECT * WHERE { ?x rdf:type 'Person' }";
			
			// Getting a prefix for the query
			SPARQLPrefixBuilder prefBuilder = new SPARQLPrefixBuilder();
			String prefix = prefBuilder.getDefaultPrefix(manager.getOntologies(), ontology);
			// Getting the ARQ query (already parsed java object from Jena)
			
			Query sparqlQuery = QueryFactory.create(prefix + " " + sparqlstr);
			
			
			// Translating into conjuntive query object
			UCQTranslator translator = new UCQTranslator();
			UnionOfConjunctiveQueries query = translator.getUCQ(controller, sparqlQuery);
			
			// Executing the query
			
			QueryResult result = reasoner.answerUCQ(query);
			
			// Printing the results
			System.out.println("Results:");
			int cols = result.getColumnCount();
			while (result.nextRow()) {
				for (int i = 0; i < cols; i++) {
					System.out.print(result.getConstantFromColumn(i) + " ");
				}
				System.out.println("");
			}
			System.out.println("-------------------");
			
			
			

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				reasoner.dispose();
			} catch (Exception e)  {
				
			}
		}

//		System.exit(0);
		
	}
}








              
Clone this wiki locally