Search in sources :

Example 11 with ReasoningServiceException

use of org.apache.stanbol.reasoners.servicesapi.ReasoningServiceException in project stanbol by apache.

the class JenaReasoningServiceTest method testClassify.

/**
     * Tests the classify() method
     */
private void testClassify(JenaReasoningService reasoningService) {
    // Clean data
    TestData.alexdma.removeProperties();
    // Prepare data
    TestData.alexdma.addProperty(RDF.type, TestData.foaf_Person);
    // Setup input for the reasoner
    Model input = ModelFactory.createUnion(TestData.foaf, TestData.alexdma.getModel());
    // Run the method
    Set<Statement> inferred;
    try {
        inferred = reasoningService.runTask(ReasoningService.Tasks.CLASSIFY, input);
        boolean foafAgentExists = false;
        log.info("Check for statements to be rdf:type only");
        for (Statement stat : inferred) {
            // Here we want only rdf:type statements
            if (!stat.getPredicate().equals(RDF.type)) {
                log.error("This statement is not rdf:type: {}", stat);
            }
            assertTrue(stat.getPredicate().equals(RDF.type));
            if (stat.getObject().isResource()) {
                if (stat.getObject().asResource().equals(TestData.foaf_Agent)) {
                    foafAgentExists = true;
                }
            }
        }
        log.info("Does the statement: example:me rdf:type foaf:Agent exists (true)? {}", foafAgentExists);
        assertTrue(foafAgentExists);
    } catch (ReasoningServiceException e) {
        log.error("Error thrown: {}", e);
        assertTrue(false);
    } catch (InconsistentInputException e) {
        log.error("Error thrown: {}", e);
        assertTrue(false);
    } catch (UnsupportedTaskException e) {
        log.error("Error thrown: {}", e);
        assertTrue(false);
    }
    // Clean data
    TestData.alexdma.removeProperties();
}
Also used : ReasoningServiceException(org.apache.stanbol.reasoners.servicesapi.ReasoningServiceException) Statement(com.hp.hpl.jena.rdf.model.Statement) Model(com.hp.hpl.jena.rdf.model.Model) InconsistentInputException(org.apache.stanbol.reasoners.servicesapi.InconsistentInputException) UnsupportedTaskException(org.apache.stanbol.reasoners.servicesapi.UnsupportedTaskException)

Example 12 with ReasoningServiceException

use of org.apache.stanbol.reasoners.servicesapi.ReasoningServiceException in project stanbol by apache.

the class JenaOWLReasoningServiceTest method isNotConsistent.

public void isNotConsistent() {
    // Clean data
    TestData.alexdma.removeProperties();
    // Prepare data
    TestData.alexdma.addProperty(RDF.type, TestData.foaf_Person);
    TestData.alexdma.addProperty(RDF.type, TestData.foaf_Organization);
    // Setup input for the reasoner
    Model input = ModelFactory.createUnion(TestData.foaf, TestData.alexdma.getModel());
    try {
        assertFalse(reasoningService.isConsistent(input));
    } catch (ReasoningServiceException e) {
        log.error("Error thrown: {}", e);
    }
    // Clean data
    TestData.alexdma.removeProperties();
}
Also used : ReasoningServiceException(org.apache.stanbol.reasoners.servicesapi.ReasoningServiceException) InfModel(com.hp.hpl.jena.rdf.model.InfModel) Model(com.hp.hpl.jena.rdf.model.Model)

Example 13 with ReasoningServiceException

use of org.apache.stanbol.reasoners.servicesapi.ReasoningServiceException 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 14 with ReasoningServiceException

use of org.apache.stanbol.reasoners.servicesapi.ReasoningServiceException in project stanbol by apache.

the class HermitReasoningServiceTest method testIsConsistent.

/**
     * Check whether the ontology is consistent
     * 
     * @param testID
     *            // The ID of the ontology to test
     * @param expected
     *            // If it is expected to be consistent or not
     */
private void testIsConsistent(String testID, boolean expected) {
    log.info("Testing the isConsistent method");
    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));
        // 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");
        try {
            assertTrue(this.theinstance.isConsistent(testOntology) == expected);
        } catch (ReasoningServiceException e) {
            log.error("Error while testing the isConsistent method. Message was: {}", e.getLocalizedMessage());
            assertTrue(false);
        }
        // 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);
    }
}
Also used : ReasoningServiceException(org.apache.stanbol.reasoners.servicesapi.ReasoningServiceException) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) OWLOntologyID(org.semanticweb.owlapi.model.OWLOntologyID) OWLImportsDeclaration(org.semanticweb.owlapi.model.OWLImportsDeclaration) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager) AddImport(org.semanticweb.owlapi.model.AddImport)

Example 15 with ReasoningServiceException

use of org.apache.stanbol.reasoners.servicesapi.ReasoningServiceException in project stanbol by apache.

the class HermitReasoningServiceTest method testEnrichWithRules.

private void testEnrichWithRules(String testID, String rulesID, String testExpectedID) {
    log.info("Testing the task ENRICH 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.ENRICH, 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 all kind of axioms
            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) 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) HashSet(java.util.HashSet)

Aggregations

ReasoningServiceException (org.apache.stanbol.reasoners.servicesapi.ReasoningServiceException)18 InconsistentInputException (org.apache.stanbol.reasoners.servicesapi.InconsistentInputException)12 UnsupportedTaskException (org.apache.stanbol.reasoners.servicesapi.UnsupportedTaskException)12 OWLOntology (org.semanticweb.owlapi.model.OWLOntology)10 OWLOntologyCreationException (org.semanticweb.owlapi.model.OWLOntologyCreationException)10 Model (com.hp.hpl.jena.rdf.model.Model)9 HashSet (java.util.HashSet)9 OWLOntologyManager (org.semanticweb.owlapi.model.OWLOntologyManager)9 OWLAxiom (org.semanticweb.owlapi.model.OWLAxiom)8 Statement (com.hp.hpl.jena.rdf.model.Statement)6 AddImport (org.semanticweb.owlapi.model.AddImport)6 OWLOntologyID (org.semanticweb.owlapi.model.OWLOntologyID)6 OWLImportsDeclaration (org.semanticweb.owlapi.model.OWLImportsDeclaration)5 OWLLogicalAxiom (org.semanticweb.owlapi.model.OWLLogicalAxiom)4 SWRLRule (org.semanticweb.owlapi.model.SWRLRule)4 Rule (com.hp.hpl.jena.reasoner.rulesys.Rule)3 Resource (com.hp.hpl.jena.rdf.model.Resource)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 OWLClassAssertionAxiom (org.semanticweb.owlapi.model.OWLClassAssertionAxiom)2