double aboxUpdateTime = 0;
double instanceRetrievalTime = 0;
double approximationTime = 0;
int individualCounter = 1;
org.mindswap.pellet.utils.Timers timers = new Timers();
timers.createTimer("classification");
timers.createTimer("approximation");
OWLReasonerFactory reasonerFactory = null;
switch (selectReasoner){
case 1: reasonerFactory = new PelletReasonerFactory(); break;
case 2: reasonerFactory = new FaCTPlusPlusReasonerFactory(); break;
default: reasonerFactory = new PelletReasonerFactory(); break;
}
/* Initialize manager used for loading the Abox */
OWLOntologyManager manager = null;
manager = OWLManager.createOWLOntologyManager();
/* Rewrite Tbox T -> T' based on the current vocabulary subset */
Timer timerApproximation = timers.startTimer( "approximation" );
ApproximationResult approximationResult = approximation.getNextTBox();
timerApproximation.stop();
if (approximationResult != null) {
System.out.println("Creating Tbox approximation "+runCounter);
OWLOntology abox = null;
OWLOntology approximatedTbox = null;
OWLReasoner reasoner = null;
int newIndividualsToAdd = 0;
/* get the approximated Tbox */
approximatedTbox = approximationResult.approximatedOntology();
if (approximationResult.isOriginal()){
/* Last run. Use original Abox to compute the baseline run */
aboxPhysicalURI = originalAboxURI;
}
/* Load Abox into the manager */
abox = manager.loadOntologyFromPhysicalURI(aboxPhysicalURI);
int aboxIndividualAxiomsCount = abox.getIndividualAxioms().size();
if (measureApproximationTime) {
/* update time with the time it takes to build the Tbox based on the current vocabulary set */
approximationTime = timerApproximation.getTotal();
}
//OWLOntology currentTBox2 = approximationResult.approximatedOntology();
/* Create a new physical URI based on the current run */
URI tboxPhysicalURI = createNewURI(tmpTboxPhysicalURI, runCounter);
if (saveApproximatedTbox){
/* Save approximated Tbox to disk - for debugging purposes only */
saveOntologyToFile(approximatedTbox, tboxPhysicalURI);
}
/* Create the reasoner */
reasoner = reasonerFactory.createReasoner(manager);
logger.info("Using reasoner: "+reasonerFactory.getReasonerName());
Set<OWLOntology> ontologiesToLoad = new HashSet<OWLOntology>();
ontologiesToLoad.add(abox);
ontologiesToLoad.add(approximatedTbox);
/* Load approximated Tbox into the reasoner */
//reasoner.loadOntologies(ontologiesToLoad);
/* Load approximated Tbox into reasoner */
reasoner.loadOntologies(Collections.singleton(approximatedTbox));
/* Print statistics */
printStatistics(runCounter, approximation, approximatedTbox, abox, aboxPhysicalURI);
/* Classify ontology if needed */
double classificationTime = 0;
if ( !(reasoner.isClassified()) ){
logger.info("Classifying KB...");
Timer timerClassify = timers.startTimer( "classification" );
reasoner.classify();
timerClassify.stop();
classificationTime = timerClassify.getTotal();
}
/* Load Abox into reasoner */
reasoner.loadOntologies(Collections.singleton(abox));
/* In each run this variable maps classes to number of instances (direct and indirect) retrieved for that class */
HashMap<OWLClass, Integer> instancesPerClass = new HashMap<OWLClass, Integer>();
/* Maps classes to number of direct instances */
HashMap<OWLClass, Integer> directIndividualsCounts = new HashMap<OWLClass, Integer>();
/* Maps classes to number of indirect instances */
HashMap<OWLClass, Integer> indirectIndividualsCounts = new HashMap<OWLClass, Integer>();
/* Total number of direct instances retrieved (among all classes) in the current run */
int cumulativeDirectInstancesCnt = 0;
/* Total number of indirect instances retrieved (among all classes) in the current run */
int cumulativeIndirectInstancesCnt = 0;
logger.info("Iterating over "+concepts.size()+" atomic concept names (calculated from original tbox)");
/* Retrieve Instances from (T', A) */
for (OWLClass cc : concepts){
/* In each run this variable contains the instances (direct and indirect) retrieved from a given class */
Set<OWLIndividual> individuals = new HashSet<OWLIndividual>();
/* In each run this variable contains the instances (direct) retrieved from a given class */
Set<OWLIndividual> directIndividuals = new HashSet<OWLIndividual>();
/* get all individuals (direct and indirect) of class cc only if the class is defined in the ontology */
if (reasoner.isDefined(cc)){
timers.createTimer("retrieval");
Timer timerRetrieval = timers.startTimer( "retrieval" );
individuals = reasoner.getIndividuals(cc, false);
timerRetrieval.stop();
/* get direct individuals only */
directIndividuals = reasoner.getIndividuals(cc, true);
/* update instance retrieval time */
instanceRetrievalTime += timerRetrieval.getTotal();
if (individuals.size() > 0){
logger.info("");
logger.info("Instance relations found for class "+cc.getURI()+": "+individuals.size()+" (class's retrieval time:"+timerRetrieval.getTotal()+")");
for (OWLIndividual owli : individuals){
logger.info(individualCounter+": "+owli.getURI());
individualCounter++;
}
}
}else{
//System.out.println("Undefined Concept in Tbox: "+cc);
logger.info("Undefined Concept in Tbox: "+cc);
}
/* save the instance relations that were retrieved for the class */
classInstances.put(cc, individuals);
/* Update number of instances (including direct and indirect) of the current class */
instancesPerClass.put(cc, new Integer(individuals.size()));
/* save number of direct and indirect instances of each class */
directIndividualsCounts.put(cc, new Integer(directIndividuals.size()));
indirectIndividualsCounts.put(cc, new Integer(individuals.size()-directIndividuals.size()));
if (isInterruptible){
List<OWLOntologyChange> changesToMake = new Vector<OWLOntologyChange>();
/* Add instances retrieved in the current run to the current Abox */
OWLDataFactory factory = manager.getOWLDataFactory();
timers.createTimer("aboxupdate");
Timer timerAboxUpdate = timers.startTimer("aboxupdate");
for (OWLIndividual ci : individuals){
changesToMake.add(new AddAxiom(abox, factory.getOWLClassAssertionAxiom(ci, cc)));
}
List<OWLOntologyChange> actualChangesMade = manager.applyChanges(changesToMake);
timerAboxUpdate.stop();