Skip to content
mislusnys edited this page Nov 29, 2013 · 3 revisions
import inf.unibz.it.dl.domain.NamedConcept;
import inf.unibz.it.dl.domain.ObjectProperty;
import inf.unibz.it.obda.owlapi.OWLAPIController;
import inf.unibz.it.ucq.domain.BinaryQueryAtom;
import inf.unibz.it.ucq.domain.ConceptQueryAtom;
import inf.unibz.it.ucq.domain.ConjunctiveQuery;
import inf.unibz.it.ucq.domain.QueryResult;
import inf.unibz.it.ucq.domain.UnionOfConjunctiveQueries;
import inf.unibz.it.ucq.domain.VariableTerm;
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.net.URI;
import java.util.Properties;

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

public class example2 {

	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
			
			ConjunctiveQuery cq = new ConjunctiveQuery();
			VariableTerm term1 = new VariableTerm("x");
			VariableTerm term2 = new VariableTerm("y");
			cq.addHeadTerm(term1);
			cq.addHeadTerm(term2);
			
			BinaryQueryAtom atom1 = new BinaryQueryAtom(new ObjectProperty(URI.create("hasParent")), term1, term2);
			cq.addQueryAtom(atom1);
			
			UnionOfConjunctiveQueries ucq = new UnionOfConjunctiveQueries();
			ucq.addQuery(cq);
			
			
			
			// Executing the query
			
			QueryResult result = reasoner.answerUCQ(ucq);
			
			// 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