use of org.semanticweb.owlapi.model.OWLAxiom 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.OWLAxiom in project stanbol by apache.
the class ConversionTester method testAxiomOwlToJenaResource.
public void testAxiomOwlToJenaResource() {
JenaToOwlConvert j2o = new JenaToOwlConvert();
OWLOntologyManager mgr = OWLManager.createOWLOntologyManager();
OWLOntology ont = null;
try {
ont = mgr.createOntology();
} catch (OWLOntologyCreationException e) {
e.printStackTrace();
fail("Can not create ontology");
}
OWLDataFactory factory = mgr.getOWLDataFactory();
StmtIterator resource = null;
OWLClass cls = factory.getOWLClass(IRI.create(CLAZZ));
OWLDataProperty dp = factory.getOWLDataProperty(IRI.create(DP));
OWLObjectProperty op = factory.getOWLObjectProperty(IRI.create(OP));
OWLAnnotationProperty oa = factory.getOWLAnnotationProperty(IRI.create(label));
OWLAnnotation oav = factory.getOWLAnnotation(oa, factory.getOWLStringLiteral(clazzlabel, "en"));
OWLDatatype dt = factory.getOWLDatatype(IRI.create(DATATYPE));
OWLNamedIndividual sub = factory.getOWLNamedIndividual(IRI.create(SUBJECT));
OWLNamedIndividual obj = factory.getOWLNamedIndividual(IRI.create(OBJECT));
OWLLiteral literal1 = factory.getOWLTypedLiteral(VALUE, dt);
// Classe
OWLDeclarationAxiom daxiomcls = factory.getOWLDeclarationAxiom(cls);
// obj prop
OWLDeclarationAxiom daxiomop = factory.getOWLDeclarationAxiom(op);
// data prop
OWLDeclarationAxiom daxiomdp = factory.getOWLDeclarationAxiom(dp);
// subject
OWLDeclarationAxiom daxiomsub = factory.getOWLDeclarationAxiom(sub);
// object
OWLDeclarationAxiom daxiomobj = factory.getOWLDeclarationAxiom(obj);
// Istanza
OWLClassAssertionAxiom axiomsub = factory.getOWLClassAssertionAxiom(cls, sub);
// Istanza
OWLClassAssertionAxiom axiomobj = factory.getOWLClassAssertionAxiom(cls, obj);
// Obj
OWLObjectPropertyAssertionAxiom axiomop = factory.getOWLObjectPropertyAssertionAxiom(op, sub, obj);
// prop
// tra
// individui
OWLDataPropertyAssertionAxiom axiomvalue = factory.getOWLDataPropertyAssertionAxiom(dp, obj, // Dataprop all'istanza;
literal1);
// Annotazione
OWLAnnotationAssertionAxiom axioman = factory.getOWLAnnotationAssertionAxiom(cls.getIRI(), oav);
mgr.addAxiom(ont, daxiomcls);
mgr.addAxiom(ont, daxiomop);
mgr.addAxiom(ont, daxiomdp);
mgr.addAxiom(ont, daxiomsub);
mgr.addAxiom(ont, daxiomobj);
mgr.addAxiom(ont, axiomsub);
mgr.addAxiom(ont, axiomobj);
mgr.addAxiom(ont, axiomop);
mgr.addAxiom(ont, axiomvalue);
mgr.addAxiom(ont, axioman);
Set<OWLAxiom> setaxiom = ont.getAxioms();
try {
resource = j2o.AxiomOwlToJenaResource(setaxiom, RDFXML);
if (resource == null) {
fail("Some errors occur");
} else {
String statment = "[http://www.w3.org/2000/01/rdf-schema#label, http://www.w3.org/2000/01/rdf-schema#range, http://www.w3.org/2000/01/rdf-schema#Literal] " + "[http://example.org/dummy#hasAge, http://www.w3.org/1999/02/22-rdf-syntax-ns#type, http://www.w3.org/2002/07/owl#DatatypeProperty] " + "[http://example.org/dummy#Linus, http://example.org/dummy#hasAge, \"8\"^^http://www.w3.org/2001/XMLSchema#int] " + "[http://example.org/dummy#Linus, http://www.w3.org/1999/02/22-rdf-syntax-ns#type, http://example.org/dummy#Peanut] " + "[http://example.org/dummy#hasSibling, http://www.w3.org/1999/02/22-rdf-syntax-ns#type, http://www.w3.org/2002/07/owl#ObjectProperty] " + "[http://example.org/dummy#Lucy, http://example.org/dummy#hasSibling, http://example.org/dummy#Linus] " + "[http://example.org/dummy#Lucy, http://www.w3.org/1999/02/22-rdf-syntax-ns#type, http://example.org/dummy#Peanut] " + "[http://www.w3.org/2000/01/rdf-schema#label, http://www.w3.org/1999/02/22-rdf-syntax-ns#type, http://www.w3.org/2002/07/owl#AnnotationProperty] " + "[http://example.org/dummy#Peanut, http://www.w3.org/2000/01/rdf-schema#label, \"Peanut\"@en] " + "[http://example.org/dummy#Peanut, http://www.w3.org/1999/02/22-rdf-syntax-ns#type, http://www.w3.org/2002/07/owl#Class]";
int size = setaxiom.size();
int count = 0;
while (resource.hasNext()) {
Statement stm = resource.nextStatement();
Resource jsubj = stm.getSubject();
if (jsubj.getURI().equals(OP.toString()) || jsubj.getURI().equals(DP.toString()) || jsubj.getURI().equals(CLAZZ.toString()) || jsubj.getURI().equals(OBJECT.toString()) || jsubj.getURI().equals(SUBJECT.toString()) || jsubj.getURI().equals(label.toString()))
if (statment.contains(stm.toString()))
count++;
}
assertEquals(size, count);
}
} catch (Exception e) {
e.printStackTrace();
fail("Exception caugth");
} finally {
assertNotNull(resource);
}
}
use of org.semanticweb.owlapi.model.OWLAxiom in project stanbol by apache.
the class ConversionTester method testResourceJenaToOwlAxiom.
public void testResourceJenaToOwlAxiom() {
JenaToOwlConvert j2o = new JenaToOwlConvert();
OntModel model = ModelFactory.createOntologyModel();
OntClass jenaclass = model.createClass(CLAZZ.toString());
ObjectProperty jenaobprop = model.createObjectProperty(OP.toString());
DatatypeProperty jenadataprop = model.createDatatypeProperty(DP.toString());
Individual jenasub = model.createIndividual(SUBJECT.toString(), jenaclass);
Individual jenaobj = model.createIndividual(OBJECT.toString(), jenaclass);
AnnotationProperty jenaanno = model.createAnnotationProperty(label.toString());
Literal value = model.createTypedLiteral(VALUE, DATATYPE.toString());
model.add(jenasub, jenaobprop, jenaobj);
model.add(jenasub, jenadataprop, value);
model.add(jenasub, jenaanno, "Lucy", "en");
Set<OWLAxiom> owlaxiom = null;
try {
owlaxiom = j2o.ResourceJenaToOwlAxiom(jenasub, RDFXML);
if (owlaxiom == null) {
fail("Some errors occur");
} else {
StmtIterator str = model.listStatements();
int count = 0;
while (str.hasNext()) {
Statement stm = str.next();
Resource subject = stm.getSubject();
if (SUBJECT.toString().equals(subject.getURI()))
count++;
}
if (count == owlaxiom.size()) {
assertEquals(count, owlaxiom.size());
} else {
fail("The number of axioms don't match the number of statement");
}
}
} catch (Exception e) {
e.printStackTrace();
fail("Exception caugth");
} finally {
assertNotNull(owlaxiom);
}
}
use of org.semanticweb.owlapi.model.OWLAxiom in project stanbol by apache.
the class OWLAPIToClerezzaConverterTest method testGraphToOWLOntology.
@Test
public void testGraphToOWLOntology() {
/*
* Transform the Clerezza Graph to an OWLOntology.
*/
OWLOntology ontology = OWLAPIToClerezzaConverter.clerezzaGraphToOWLOntology(mGraph);
/*
* Print the number of axioms contained in the OWLOntology.
*/
int axiomCount = ontology.getAxiomCount();
log.info("The ontology contatins " + axiomCount + " axioms: ");
/*
* Print the axioms contained in the OWLOntology.
*/
Set<OWLAxiom> axioms = ontology.getAxioms();
for (OWLAxiom axiom : axioms) {
log.info(" " + axiom.toString());
}
}
use of org.semanticweb.owlapi.model.OWLAxiom in project stanbol by apache.
the class OWLAPIToClerezzaConverterTest method setupClass.
@BeforeClass
public static void setupClass() {
/*
* Set-up the OWL ontology for the test. Simply add the axioms: AndreaNuzzolese isA Person -> class
* assertion axiom EnricoDaga isA Person -> class assertion axiom AndreaNuzzolese knows EnricoDaga ->
* object property assertion axiom
*/
OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
OWLDataFactory factory = manager.getOWLDataFactory();
try {
ontology = manager.createOntology(org.semanticweb.owlapi.model.IRI.create(ns + "testOntology"));
} catch (OWLOntologyCreationException e) {
log.error(e.getMessage());
}
if (ontology != null) {
OWLClass personClass = factory.getOWLClass(org.semanticweb.owlapi.model.IRI.create(foaf + "Person"));
OWLNamedIndividual andreaNuzzoleseOWL = factory.getOWLNamedIndividual(org.semanticweb.owlapi.model.IRI.create(ns + "AndreaNuzzolese"));
OWLNamedIndividual enricoDagaOWL = factory.getOWLNamedIndividual(org.semanticweb.owlapi.model.IRI.create(ns + "EnricoDaga"));
OWLObjectProperty knowsOWL = factory.getOWLObjectProperty(org.semanticweb.owlapi.model.IRI.create(foaf + "knows"));
OWLAxiom axiom = factory.getOWLClassAssertionAxiom(personClass, andreaNuzzoleseOWL);
manager.addAxiom(ontology, axiom);
axiom = factory.getOWLClassAssertionAxiom(personClass, enricoDagaOWL);
manager.addAxiom(ontology, axiom);
axiom = factory.getOWLObjectPropertyAssertionAxiom(knowsOWL, andreaNuzzoleseOWL, enricoDagaOWL);
manager.addAxiom(ontology, axiom);
}
/*
* Set-up the Clerezza model for the test. As before simply add the triples: AndreaNuzzolese isA
* Person EnricoDaga isA Person AndreaNuzzolese knows EnricoDaga
*/
mGraph = new SimpleGraph();
IRI knowsInClerezza = new IRI(ns + "knows");
IRI rdfType = new IRI(RDF.getURI() + "type");
IRI foafPersonInClerezza = new IRI(foaf + "Person");
BlankNodeOrIRI andreaNuzzoleseInClerezza = new IRI(ns + "AndreaNuzzolese");
BlankNodeOrIRI enricoDagaInClerezza = new IRI(ns + "EnricoDaga");
Triple triple = new TripleImpl(andreaNuzzoleseInClerezza, rdfType, foafPersonInClerezza);
mGraph.add(triple);
triple = new TripleImpl(enricoDagaInClerezza, rdfType, foafPersonInClerezza);
mGraph.add(triple);
triple = new TripleImpl(andreaNuzzoleseInClerezza, knowsInClerezza, enricoDagaInClerezza);
mGraph.add(triple);
}
Aggregations