use of org.semanticweb.owlapi.model.OWLAxiomVisitor in project stanbol by apache.
the class RegistryManagerImpl method createModel.
@Override
public Set<Registry> createModel(Set<OWLOntology> registryOntologies) {
Set<Registry> results = new HashSet<Registry>();
// Reset population
population.clear();
// Build the transitive imports closure of the union.
Set<OWLOntology> closure = new HashSet<OWLOntology>();
for (OWLOntology rego : registryOntologies) closure.addAll(rego.getOWLOntologyManager().getImportsClosure(rego));
/*
* For each value in this map, index 0 is the score of the library class, while 1 is the score of the
* ontology class.
*/
final Map<IRI, int[]> candidateTypes = new HashMap<IRI, int[]>();
/*
* Scans class assertions and object property values and tries to determine the type of each
* individual it finds.
*/
OWLAxiomVisitor scanner = new OWLAxiomVisitorAdapter() {
/*
* For a given identifier, returns the array of integers whose value determine the likelihood if
* the corresponding entity being a library or an ontology. If no such array exists, it is
* created.
*/
private int[] checkScores(IRI key) {
int[] scores;
if (candidateTypes.containsKey(key))
scores = candidateTypes.get(key);
else {
scores = new int[] { 0, 0 };
candidateTypes.put(key, scores);
}
return scores;
}
@Override
public void visit(OWLAnnotationAssertionAxiom axiom) {
/*
* Works like object property assertions, in case hasOntology and isOntologyOf are not
* detected to be object properties (e.g. due to a failure to load codolight or the registry
* metamodel).
*/
OWLAnnotationProperty prop = axiom.getProperty();
if (hasOntologyAnn.equals(prop)) {
IRI iri;
// The axiom subject gets a +1 in its library score.
OWLObject ind = axiom.getSubject();
if (ind instanceof IRI) {
iri = (IRI) ind;
checkScores(iri)[0]++;
}
// The axiom object gets a +1 in its ontology score.
ind = axiom.getValue();
if (ind instanceof IRI) {
iri = (IRI) ind;
checkScores(iri)[1]++;
}
} else if (isOntologyOfAnn.equals(prop)) {
IRI iri;
// The axiom subject gets a +1 in its ontology score.
OWLObject ind = axiom.getSubject();
if (ind instanceof IRI) {
iri = (IRI) ind;
checkScores(iri)[1]++;
}
// The axiom object gets a +1 in its library score.
ind = axiom.getValue();
if (ind instanceof IRI) {
iri = (IRI) ind;
checkScores(iri)[0]++;
}
}
}
@Override
public void visit(OWLClassAssertionAxiom axiom) {
OWLIndividual ind = axiom.getIndividual();
// Do not accept anonymous registry items.
if (ind.isAnonymous())
return;
IRI iri = ind.asOWLNamedIndividual().getIRI();
int[] scores = checkScores(iri);
OWLClassExpression type = axiom.getClassExpression();
// If the type is stated to be a library, increase its library score.
if (cRegistryLibrary.equals(type)) {
scores[0]++;
} else // If the type is stated to be an ontology, increase its ontology score.
if (cOntology.equals(type)) {
scores[1]++;
}
}
@Override
public void visit(OWLObjectPropertyAssertionAxiom axiom) {
OWLObjectPropertyExpression prop = axiom.getProperty();
if (hasOntology.equals(prop)) {
IRI iri;
// The axiom subject gets a +1 in its library score.
OWLIndividual ind = axiom.getSubject();
if (!ind.isAnonymous()) {
iri = ind.asOWLNamedIndividual().getIRI();
checkScores(iri)[0]++;
}
// The axiom object gets a +1 in its ontology score.
ind = axiom.getObject();
if (!ind.isAnonymous()) {
iri = ind.asOWLNamedIndividual().getIRI();
checkScores(iri)[1]++;
}
} else if (isOntologyOf.equals(prop)) {
IRI iri;
// The axiom subject gets a +1 in its ontology score.
OWLIndividual ind = axiom.getSubject();
if (!ind.isAnonymous()) {
iri = ind.asOWLNamedIndividual().getIRI();
checkScores(iri)[1]++;
}
// The axiom object gets a +1 in its library score.
ind = axiom.getObject();
if (!ind.isAnonymous()) {
iri = ind.asOWLNamedIndividual().getIRI();
checkScores(iri)[0]++;
}
}
}
};
// First pass to determine the types.
for (OWLOntology o : closure) for (OWLAxiom ax : o.getAxioms()) ax.accept(scanner);
// Then populate on the registry
OWLDataFactory df = OWLManager.getOWLDataFactory();
for (IRI iri : candidateTypes.keySet()) {
int[] scores = candidateTypes.get(iri);
if (scores != null && (scores[0] > 0 || scores[1] > 0)) {
if (scores[0] > 0 && scores[1] == 0)
population.put(iri, riFactory.createLibrary(df.getOWLNamedIndividual(iri)));
else if (scores[0] == 0 && scores[1] > 0)
population.put(iri, riFactory.createRegistryOntology(df.getOWLNamedIndividual(iri)));
}
// else log.warn("Unable to determine type for registry item {}", iri);
}
for (OWLOntology oReg : registryOntologies) {
try {
results.add(populateRegistry(oReg));
} catch (RegistryContentException e) {
log.error("An error occurred while populating an ontology registry.", e);
}
}
return results;
}
use of org.semanticweb.owlapi.model.OWLAxiomVisitor in project stanbol by apache.
the class RegistryUtils method getType.
@Deprecated
public static Type getType(final OWLIndividual ind, Set<OWLOntology> ontologies) {
// 0 is for library, 1 is for ontology (more in the future?)
final int[] pointsFor = new int[] { 0, 0 };
final int[] pointsAgainst = new int[] { 0, 0 };
OWLAxiomVisitor v = new OWLAxiomVisitorAdapter() {
@Override
public void visit(OWLClassAssertionAxiom axiom) {
if (ind.equals(axiom.getIndividual())) {
OWLClassExpression type = axiom.getClassExpression();
if (cRegistryLibrary.equals(type)) {
pointsFor[0]++;
pointsAgainst[1]++;
} else if (cOntology.equals(type)) {
pointsFor[1]++;
pointsAgainst[0]++;
}
}
}
@Override
public void visit(OWLObjectPropertyAssertionAxiom axiom) {
OWLObjectPropertyExpression prop = axiom.getProperty();
if (ind.equals(axiom.getSubject())) {
if (hasOntology.equals(prop)) {
pointsFor[0]++;
pointsAgainst[1]++;
} else if (isOntologyOf.equals(prop)) {
pointsFor[1]++;
pointsAgainst[0]++;
}
} else if (ind.equals(axiom.getObject())) {
if (isOntologyOf.equals(prop)) {
pointsFor[0]++;
pointsAgainst[1]++;
} else if (hasOntology.equals(prop)) {
pointsFor[1]++;
pointsAgainst[0]++;
}
}
}
};
// TODO use this strategy in the single pass algorithm for constructing the model.
for (OWLOntology o : ontologies) for (OWLAxiom ax : o.getAxioms()) ax.accept(v);
if (pointsFor[0] > 0 && pointsAgainst[0] == 0)
return Type.LIBRARY;
if (pointsFor[1] > 0 && pointsAgainst[1] == 0)
return Type.ONTOLOGY;
// Cannot determine registries, since they have no associated individual.
return null;
}
Aggregations