double instanceRetrievalTime = 0;
/* Stores the set of instances of every class */
HashMap<OWLClass, Set<OWLIndividual>> complete = new HashMap<OWLClass, Set<OWLIndividual>>();
OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
OWLOntology tbox = null;
OWLOntology abox = null;
try
{
tbox = manager.loadOntologyFromPhysicalURI(tboxPhysicalURI);
abox = manager.loadOntologyFromPhysicalURI(aboxPhysicalURI);
VocabularyManager vManager = new VocabularyManager(tbox);
Set<OWLClass> classes = vManager.getConceptSet();
Set<OWLProperty<?, ?>> properties = new HashSet<OWLProperty<?, ?>>();
logger.info("Selected vocabulary subset: ");
int cCounter = 1;
for (OWLClass owlClass : classes)
{
logger.info("Concept " + cCounter + ": " + owlClass.getURI());
cCounter++;
}
logger.info("Selected property subset: ");
int pCounter = 1;
for (OWLProperty<?, ?> owlProperty : properties)
{
logger.info("Property " + pCounter + ": "
+ owlProperty.getURI());
pCounter++;
}
logger.info("");
org.mindswap.pellet.utils.Timers timers = new Timers();
timers.createTimer("classification");
/* Create the reaosner */
OWLReasoner reasoner = reasonerFactory.createReasoner(manager);
System.out
.println("Classifying and retrieving instance relations...");
this.logger
.info("Classifying and retrieving instance relations...");
this.logger.info("Using reasoner: "
+ reasonerFactory.getReasonerName());
/* Load Tbox into reasoner */
reasoner.loadOntologies(Collections.singleton(tbox));
/* Load Abox into reasoner */
// reasoner.loadOntologies(Collections.singleton(abox));
/* Classify the ontology if necessary */
if (!reasoner.isClassified())
{
/* Measure time using Benchmarking framework */
Timer timerClassify = timers.startTimer("classification");
reasoner.classify();
timerClassify.stop();
classificationTime = timerClassify.getTotal();
}
/* Load Abox into reasoner */
reasoner.loadOntologies(Collections.singleton(abox));
if (!reasoner.isClassified())
{
reasoner.classify();
}
/*
* Maps each class to the number of instances (including direct and
* indirect)
*/
HashMap<OWLClass, Integer> allIndividualsCounts = new HashMap<OWLClass, Integer>();
/* Maps each class to the number of direct instances */
HashMap<OWLClass, Integer> directIndividualsCounts = new HashMap<OWLClass, Integer>();
/* Maps each class to the number of indirect instances */
HashMap<OWLClass, Integer> indirectIndividualsCounts = new HashMap<OWLClass, Integer>();
int individualCounter = 1;
for (OWLClass cc : classes)
{
allIndividuals = new HashSet<OWLIndividual>();
directIndividuals = new HashSet<OWLIndividual>();
if (reasoner.isDefined(cc))
{
/*
* retrieve all individuals of the class (direct and
* indirect individuals)
*/
timers.createTimer("retrieval");
Timer timerRetrieval = timers.startTimer("retrieval");
allIndividuals = reasoner.getIndividuals(cc, false);
timerRetrieval.stop();
/* update instance retrieval time */
instanceRetrievalTime += timerRetrieval.getTotal();
/* retrieve all direct instances of the class */
directIndividuals = reasoner.getIndividuals(cc, true);
} else
{
logger.info("Undefined Concept in Tbox: " + cc);
}
/* set the number of instances (direct + indirect) of the class */
allIndividualsCounts
.put(cc, new Integer(allIndividuals.size()));
/* set the number of direct instances of the class */
directIndividualsCounts.put(cc, new Integer(directIndividuals
.size()));
/* set the number of indirect instances of the class */
indirectIndividualsCounts.put(cc, new Integer(allIndividuals
.size()
- directIndividuals.size()));
complete.put(cc, allIndividuals);
cumulativeNumberOfInstances += allIndividuals.size();
cumulativeNumberOfDirectInstances += directIndividuals.size();
cumulativeNumberOfIndirectInstances += (allIndividuals.size() - directIndividuals
.size());
/* Log the indirect instances found */
HashSet<OWLIndividual> difference = new HashSet<OWLIndividual>(
allIndividuals);
difference.removeAll(directIndividuals);
if (difference.size() > 0)
{
logger.info("");
logger.info("Indirect instance relations found for class "
+ cc.getURI() + ": " + difference.size());
for (OWLIndividual owli : difference)
{
logger.info(individualCounter + ": " + owli.getURI());
individualCounter++;
}
}
}
time = (classificationTime + instanceRetrievalTime) - loadingTime;
RunResult result = new RunResult(allIndividualsCounts, time,
classes.size(), properties.size());
result.setClassificationTime(classificationTime);
result.setOntologyLoadingTime(loadingTime);
result.setDirectIndividuals(directIndividualsCounts);
result.setIndirectIndividuals(indirectIndividualsCounts);
result.setInstanceRetrievalTime(instanceRetrievalTime);
result.setInstances(complete);
result.setRunNumber(approximationCnt);
result.setRunType(runType);
this.logger.info("");
this.logger.info("#Instance relations: "
+ cumulativeNumberOfInstances);
this.logger.info("#Direct instances relations retrieved: "
+ cumulativeNumberOfDirectInstances);
this.logger.info("#Indirect instances relations retrieved: "
+ cumulativeNumberOfIndirectInstances);
this.logger.info("Runtime (ms): " + time);
this.logger.info("Classification Time (ms): " + classificationTime);
this.logger
.info("Reasoner creation and ontology loading time (ms): "
+ loadingTime);
this.logger
.info("Instance Retrieval Time (ms) (approx.: total time - classification time): "
+ instanceRetrievalTime);
this.logger.info("Tbox URI: " + tboxPhysicalURI);
this.logger.info("Abox URI: " + aboxPhysicalURI);
this.logger
.info("============================================================================================================");
reasoner.clearOntologies();
reasoner.dispose();
manager.removeOntology(abox.getURI());
manager.removeOntology(tbox.getURI());
reasoner = null;
return result;
} catch (OWLOntologyCreationException e)