Search in sources :

Example 6 with OWLClassAssertionAxiom

use of org.semanticweb.owlapi.model.OWLClassAssertionAxiom in project stanbol by apache.

the class ConversionTester method testEntityOwlToJenaResource.

public void testEntityOwlToJenaResource() {
    JenaToOwlConvert j2o = new JenaToOwlConvert();
    OWLOntologyManager mgr = OWLManager.createOWLOntologyManager();
    OWLOntology ont = null;
    StmtIterator resource = null;
    try {
        ont = mgr.createOntology();
    } catch (OWLOntologyCreationException e) {
        e.printStackTrace();
        fail("Could not load ontology");
    }
    OWLDataFactory factory = mgr.getOWLDataFactory();
    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, sub, // 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<OWLIndividualAxiom> ind = ont.getAxioms(sub);
    try {
        resource = j2o.EntityOwlToJenaResource(daxiomsub.getEntity(), ont, RDFXML);
        if (resource == null) {
            fail("Some errors accour");
        } else {
            int cont = 0;
            while (resource.hasNext()) {
                Statement stm = resource.nextStatement();
                IRI subres = IRI.create(stm.getSubject().getURI());
                if (("<" + subres + ">").equals(daxiomsub.getEntity().toString()))
                    cont++;
            }
            assertEquals(ind.size(), (cont - 1));
        }
    } catch (Exception e) {
        e.printStackTrace();
        fail("Exception caugth");
    } finally {
        assertNotNull(resource);
    }
}
Also used : IRI(org.semanticweb.owlapi.model.IRI) OWLDeclarationAxiom(org.semanticweb.owlapi.model.OWLDeclarationAxiom) OWLDatatype(org.semanticweb.owlapi.model.OWLDatatype) OWLAnnotationProperty(org.semanticweb.owlapi.model.OWLAnnotationProperty) OWLDataPropertyAssertionAxiom(org.semanticweb.owlapi.model.OWLDataPropertyAssertionAxiom) OWLDataProperty(org.semanticweb.owlapi.model.OWLDataProperty) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) OWLNamedIndividual(org.semanticweb.owlapi.model.OWLNamedIndividual) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager) OWLClassAssertionAxiom(org.semanticweb.owlapi.model.OWLClassAssertionAxiom) StmtIterator(com.hp.hpl.jena.rdf.model.StmtIterator) OWLAnnotationAssertionAxiom(org.semanticweb.owlapi.model.OWLAnnotationAssertionAxiom) OWLAnnotation(org.semanticweb.owlapi.model.OWLAnnotation) Statement(com.hp.hpl.jena.rdf.model.Statement) OWLIndividualAxiom(org.semanticweb.owlapi.model.OWLIndividualAxiom) OWLObjectProperty(org.semanticweb.owlapi.model.OWLObjectProperty) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) OWLLiteral(org.semanticweb.owlapi.model.OWLLiteral) OWLObjectPropertyAssertionAxiom(org.semanticweb.owlapi.model.OWLObjectPropertyAssertionAxiom) OWLClass(org.semanticweb.owlapi.model.OWLClass) OWLDataFactory(org.semanticweb.owlapi.model.OWLDataFactory)

Example 7 with OWLClassAssertionAxiom

use of org.semanticweb.owlapi.model.OWLClassAssertionAxiom 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;
}
Also used : OWLAxiomVisitor(org.semanticweb.owlapi.model.OWLAxiomVisitor) OWLObjectPropertyExpression(org.semanticweb.owlapi.model.OWLObjectPropertyExpression) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) OWLObjectPropertyAssertionAxiom(org.semanticweb.owlapi.model.OWLObjectPropertyAssertionAxiom) OWLClassExpression(org.semanticweb.owlapi.model.OWLClassExpression) OWLAxiom(org.semanticweb.owlapi.model.OWLAxiom) OWLClassAssertionAxiom(org.semanticweb.owlapi.model.OWLClassAssertionAxiom) OWLAxiomVisitorAdapter(org.semanticweb.owlapi.util.OWLAxiomVisitorAdapter)

Example 8 with OWLClassAssertionAxiom

use of org.semanticweb.owlapi.model.OWLClassAssertionAxiom in project stanbol by apache.

the class HermitReasoningServiceTest method testClassifyWithRules.

private void testClassifyWithRules(String testID, String rulesID, String testExpectedID) {
    log.info("Testing the task CLASSIFY with rules");
    OWLOntologyManager manager = TestData.manager;
    // We prepare the input ontology
    try {
        OWLOntology testOntology = manager.createOntology();
        OWLOntologyID testOntologyID = testOntology.getOntologyID();
        log.debug("Created test ontology with ID: {}", testOntologyID);
        OWLImportsDeclaration importTest = TestData.factory.getOWLImportsDeclaration(IRI.create(testID));
        manager.applyChange(new AddImport(testOntology, importTest));
        Set<SWRLRule> rules = manager.getOntology(IRI.create(rulesID)).getAxioms(AxiomType.SWRL_RULE);
        // Maybe we want to see the list of rules
        if (log.isDebugEnabled()) {
            log.debug("List of {} rules: ", rules.size());
            TestUtils.debug(rules, log);
        }
        log.debug("We add the rules to the ontology");
        manager.addAxioms(manager.getOntology(testOntologyID), rules);
        // Maybe we want to see what is in before
        if (log.isDebugEnabled())
            log.debug("Content of the input is:");
        TestUtils.debug(manager.getOntology(testOntologyID), log);
        // Now we test the method
        log.debug("Running HermiT");
        Set<OWLAxiom> inferred = this.theinstance.runTask(ReasoningService.Tasks.CLASSIFY, manager.getOntology(testOntologyID));
        // Maybe we want to see the inferred axiom list
        if (log.isDebugEnabled()) {
            log.debug("{} inferred axioms:", inferred.size());
            TestUtils.debug(inferred, log);
        }
        Set<OWLLogicalAxiom> expectedAxioms = manager.getOntology(IRI.create(testExpectedID)).getLogicalAxioms();
        Set<OWLAxiom> missing = new HashSet<OWLAxiom>();
        for (OWLAxiom expected : expectedAxioms) {
            // We consider here only two kind of axioms
            if (expected instanceof OWLSubClassOfAxiom || expected instanceof OWLClassAssertionAxiom) {
                if (!inferred.contains(expected)) {
                    log.error("missing expected axiom: {}", expected);
                    missing.add(expected);
                }
            }
        }
        assertTrue(missing.isEmpty());
        // We want to remove the ontology from the manager
        manager.removeOntology(testOntology);
    } catch (OWLOntologyCreationException e) {
        log.error("An {} have been thrown while creating the input ontology for test", e.getClass());
        assertTrue(false);
    } catch (ReasoningServiceException e) {
        log.error("An {} have been thrown while executing the reasoning", e.getClass());
        assertTrue(false);
    } catch (InconsistentInputException e) {
        log.error("An {} have been thrown while executing the reasoning", e.getClass());
        assertTrue(false);
    } catch (UnsupportedTaskException e) {
        log.error("An {} have been thrown while executing the reasoning", e.getClass());
        assertTrue(false);
    }
}
Also used : OWLLogicalAxiom(org.semanticweb.owlapi.model.OWLLogicalAxiom) OWLImportsDeclaration(org.semanticweb.owlapi.model.OWLImportsDeclaration) InconsistentInputException(org.apache.stanbol.reasoners.servicesapi.InconsistentInputException) AddImport(org.semanticweb.owlapi.model.AddImport) UnsupportedTaskException(org.apache.stanbol.reasoners.servicesapi.UnsupportedTaskException) ReasoningServiceException(org.apache.stanbol.reasoners.servicesapi.ReasoningServiceException) OWLSubClassOfAxiom(org.semanticweb.owlapi.model.OWLSubClassOfAxiom) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) OWLOntologyID(org.semanticweb.owlapi.model.OWLOntologyID) SWRLRule(org.semanticweb.owlapi.model.SWRLRule) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager) OWLAxiom(org.semanticweb.owlapi.model.OWLAxiom) OWLClassAssertionAxiom(org.semanticweb.owlapi.model.OWLClassAssertionAxiom) HashSet(java.util.HashSet)

Example 9 with OWLClassAssertionAxiom

use of org.semanticweb.owlapi.model.OWLClassAssertionAxiom in project stanbol by apache.

the class HermitReasoningServiceTest method testClassify.

/**
     * We may want to test this method with more then 1 ontology. This is why the implementation is in
     * aprivate method. This method tests if all the logical axioms in testExpectedID ontology are inferences
     * of the testID ontology.
     * 
     * @param testID
     *            // The ID of the ontology to be the input (loaded in the TestData.manager)
     * @param testExpectedID
     *            // The ID of the ontology which contains logical axioms expected in the result
     */
private void testClassify(String testID, String testExpectedID) {
    log.info("Testing the CLASSIFY task");
    OWLOntologyManager manager = TestData.manager;
    // We prepare the input ontology
    try {
        OWLOntology testOntology = manager.createOntology();
        OWLOntologyID testOntologyID = testOntology.getOntologyID();
        log.debug("Created test ontology with ID: {}", testOntologyID);
        manager.applyChange(new AddImport(testOntology, TestData.factory.getOWLImportsDeclaration(IRI.create(testID))));
        // Maybe we want to see what is in before
        if (log.isDebugEnabled())
            TestUtils.debug(manager.getOntology(testOntologyID), log);
        // Now we test the method
        log.debug("Running HermiT");
        Set<OWLAxiom> inferred = this.theinstance.runTask(ReasoningService.Tasks.CLASSIFY, manager.getOntology(testOntologyID));
        // Maybe we want to see the inferred axiom list
        if (log.isDebugEnabled()) {
            TestUtils.debug(inferred, log);
        }
        Set<OWLLogicalAxiom> expectedAxioms = manager.getOntology(IRI.create(testExpectedID)).getLogicalAxioms();
        Set<OWLAxiom> missing = new HashSet<OWLAxiom>();
        for (OWLAxiom expected : expectedAxioms) {
            if (!inferred.contains(expected)) {
                log.error("missing expected axiom: {}", expected);
                missing.add(expected);
            }
        }
        assertTrue(missing.isEmpty());
        // We want only Class related axioms in the result set
        for (OWLAxiom a : inferred) {
            assertTrue(a instanceof OWLClassAssertionAxiom || a instanceof OWLSubClassOfAxiom || a instanceof OWLEquivalentClassesAxiom || a instanceof OWLDisjointClassesAxiom);
        }
        // We want to remove the ontology from the manager
        manager.removeOntology(testOntology);
    } catch (OWLOntologyCreationException e) {
        log.error("An {} have been thrown while creating the input ontology for test", e.getClass());
        assertTrue(false);
    } catch (ReasoningServiceException e) {
        log.error("An {} have been thrown while executing the reasoning", e.getClass());
        assertTrue(false);
    } catch (InconsistentInputException e) {
        log.error("An {} have been thrown while executing the reasoning", e.getClass());
        assertTrue(false);
    } catch (UnsupportedTaskException e) {
        log.error("An {} have been thrown while executing the reasoning", e.getClass());
        assertTrue(false);
    }
}
Also used : OWLLogicalAxiom(org.semanticweb.owlapi.model.OWLLogicalAxiom) OWLDisjointClassesAxiom(org.semanticweb.owlapi.model.OWLDisjointClassesAxiom) InconsistentInputException(org.apache.stanbol.reasoners.servicesapi.InconsistentInputException) AddImport(org.semanticweb.owlapi.model.AddImport) UnsupportedTaskException(org.apache.stanbol.reasoners.servicesapi.UnsupportedTaskException) ReasoningServiceException(org.apache.stanbol.reasoners.servicesapi.ReasoningServiceException) OWLSubClassOfAxiom(org.semanticweb.owlapi.model.OWLSubClassOfAxiom) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) OWLOntologyID(org.semanticweb.owlapi.model.OWLOntologyID) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager) OWLAxiom(org.semanticweb.owlapi.model.OWLAxiom) OWLClassAssertionAxiom(org.semanticweb.owlapi.model.OWLClassAssertionAxiom) OWLEquivalentClassesAxiom(org.semanticweb.owlapi.model.OWLEquivalentClassesAxiom) HashSet(java.util.HashSet)

Aggregations

OWLClassAssertionAxiom (org.semanticweb.owlapi.model.OWLClassAssertionAxiom)9 OWLObjectPropertyAssertionAxiom (org.semanticweb.owlapi.model.OWLObjectPropertyAssertionAxiom)7 OWLAnnotationAssertionAxiom (org.semanticweb.owlapi.model.OWLAnnotationAssertionAxiom)6 OWLAnnotationProperty (org.semanticweb.owlapi.model.OWLAnnotationProperty)6 OWLOntology (org.semanticweb.owlapi.model.OWLOntology)6 OWLAxiom (org.semanticweb.owlapi.model.OWLAxiom)5 OWLClass (org.semanticweb.owlapi.model.OWLClass)5 OWLDataProperty (org.semanticweb.owlapi.model.OWLDataProperty)5 OWLDataPropertyAssertionAxiom (org.semanticweb.owlapi.model.OWLDataPropertyAssertionAxiom)5 OWLLiteral (org.semanticweb.owlapi.model.OWLLiteral)5 OWLNamedIndividual (org.semanticweb.owlapi.model.OWLNamedIndividual)5 OWLObjectProperty (org.semanticweb.owlapi.model.OWLObjectProperty)5 IRI (org.semanticweb.owlapi.model.IRI)4 OWLOntologyCreationException (org.semanticweb.owlapi.model.OWLOntologyCreationException)4 OWLOntologyManager (org.semanticweb.owlapi.model.OWLOntologyManager)4 HashSet (java.util.HashSet)3 AddAxiom (org.semanticweb.owlapi.model.AddAxiom)3 OWLDataFactory (org.semanticweb.owlapi.model.OWLDataFactory)3 Statement (com.hp.hpl.jena.rdf.model.Statement)2 StmtIterator (com.hp.hpl.jena.rdf.model.StmtIterator)2