Search in sources :

Example 1 with InconsistentInputException

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

the class ReasoningServiceExecutor method executeJenaReasoningService.

/**
 * Execute a JenaReasoningService
 *
 * TODO: Add parameter to decide if the output graph must be deleted if exists
 *
 * @param s
 * @param input
 * @param rules
 * @return
 * @throws ReasoningServiceException
 * @throws UnsupportedTaskException
 */
private ReasoningServiceResult<Model> executeJenaReasoningService(String task, JenaReasoningService s, Model input, List<Rule> rules, String targetGraphID, boolean filtered, Map<String, List<String>> parameters) throws ReasoningServiceException, UnsupportedTaskException {
    // Check task: this is managed directly by the endpoint
    if (task.equals(ReasoningServiceExecutor.TASK_CHECK)) {
        log.debug("Task is '{}'", ReasoningServiceExecutor.TASK_CHECK);
        try {
            boolean is = s.isConsistent(input);
            return new ReasoningServiceResult<Model>(ReasoningServiceExecutor.TASK_CHECK, is);
        } catch (ReasoningServiceException e) {
            log.error("Error thrown: {}", e);
            throw e;
        }
    }
    try {
        Set<Statement> result = s.runTask(task, input, rules, filtered, parameters);
        if (result == null) {
            log.error("Result is null");
            throw new RuntimeException("Result is null.");
        }
        Model outputModel = ModelFactory.createDefaultModel();
        outputModel.add(result.toArray(new Statement[result.size()]));
        // If target is null, then get back results, elsewhere put it in
        // target graph
        log.debug("Prepare output");
        if (targetGraphID == null) {
            log.debug("Returning {} statements", result.size());
            return new ReasoningServiceResult<Model>(task, true, outputModel);
        } else {
            save(outputModel, targetGraphID);
            return new ReasoningServiceResult<Model>(task, true);
        }
    } catch (ReasoningServiceException e) {
        log.error("Error thrown: {}", e);
        throw e;
    } catch (InconsistentInputException e) {
        log.debug("The input is not consistent");
        return new ReasoningServiceResult<Model>(ReasoningServiceExecutor.TASK_CHECK, false);
    } catch (UnsupportedTaskException e) {
        log.error("Error thrown: {}", e);
        throw e;
    } catch (IOException e) {
        throw new ReasoningServiceException(e);
    }
}
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) IOException(java.io.IOException) UnsupportedTaskException(org.apache.stanbol.reasoners.servicesapi.UnsupportedTaskException)

Example 2 with InconsistentInputException

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

the class ReasoningServiceExecutor method executeOWLApiReasoningService.

/**
 * Executes the OWLApiReasoingService
 *
 * @param task
 * @param s
 * @param input
 * @param rules
 * @param targetGraphID
 * @param parameters
 * @return
 * @throws InconsistentInputException
 * @throws ReasoningServiceException
 * @throws UnsupportedTaskException
 */
private ReasoningServiceResult<OWLOntology> executeOWLApiReasoningService(String task, OWLApiReasoningService s, OWLOntology input, List<SWRLRule> rules, String targetGraphID, boolean filtered, Map<String, List<String>> parameters) throws InconsistentInputException, ReasoningServiceException, UnsupportedTaskException {
    // Check task: this is managed directly by the endpoint
    if (task.equals(ReasoningServiceExecutor.TASK_CHECK)) {
        log.debug("Task is '{}'", ReasoningServiceExecutor.TASK_CHECK);
        try {
            boolean is = s.isConsistent(input);
            return new ReasoningServiceResult<OWLOntology>(ReasoningServiceExecutor.TASK_CHECK, is);
        } catch (ReasoningServiceException e) {
            throw e;
        }
    }
    // We get the manager from the input ontology
    // XXX We must be aware of this.
    OWLOntologyManager manager = input.getOWLOntologyManager();
    try {
        OWLOntology output = manager.createOntology();
        Set<OWLAxiom> axioms = s.runTask(task, input, rules, filtered, parameters);
        log.debug("Prepare output: {} axioms", axioms.size());
        manager.addAxioms(output, axioms);
        if (targetGraphID == null) {
            return new ReasoningServiceResult<OWLOntology>(task, true, manager.getOntology(output.getOntologyID()));
        } else {
            save(output, targetGraphID);
            return new ReasoningServiceResult<OWLOntology>(task, true);
        }
    } catch (InconsistentInputException e) {
        log.warn("The input is not consistent");
        return new ReasoningServiceResult<OWLOntology>(ReasoningServiceExecutor.TASK_CHECK, false);
    } catch (ReasoningServiceException e) {
        throw e;
    } catch (OWLOntologyCreationException e) {
        log.error("Error! \n", e);
        throw new ReasoningServiceException(new IOException(e));
    } catch (UnsupportedTaskException e) {
        log.error("Error! \n", e);
        throw e;
    } catch (Throwable t) {
        log.error("Error! \n", t);
        throw new ReasoningServiceException(t);
    }
}
Also used : InconsistentInputException(org.apache.stanbol.reasoners.servicesapi.InconsistentInputException) IOException(java.io.IOException) 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) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager) OWLAxiom(org.semanticweb.owlapi.model.OWLAxiom)

Example 3 with InconsistentInputException

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

the class JenaReasoningServiceTest method testClassifyWithRule.

/**
 * Tests the classify(Model data, List<Rule> rules) method
 */
private void testClassifyWithRule(JenaReasoningService service) {
    log.info("Testing {}", service.getClass());
    // Prepare the rule set
    String source = "" + "\n@prefix rdf: <" + RDF.getURI() + ">." + "\n@prefix foaf: <" + TestData.FOAF_NS + ">." + "\n@prefix ex: <" + TestData.TEST_NS + ">." + "\n[rule: (?a foaf:workplaceHomepage ?w) (?w rdf:type ex:SWResearchLab) -> (?a rdf:type ex:SWResearcher)] ";
    // log.info("This is the ruleset: \n {}", source);
    List<Rule> rules = TestUtils.parseRuleStringAsFile(source);
    log.info("Loaded {} rules", rules.size());
    // Clean data
    TestData.alexdma.removeProperties();
    TestData.enridaga.removeProperties();
    Resource wphomepage = TestData.model.createResource("http://stlab.istc.cnr.it");
    Resource swResearchLab = TestData.model.createResource(TestData.TEST_NS + "SWResearchLab");
    // Prepare data
    TestData.alexdma.addProperty(TestData.foaf_workplaceHomepage, wphomepage);
    TestData.enridaga.addProperty(TestData.foaf_workplaceHomepage, wphomepage);
    wphomepage.addProperty(RDF.type, swResearchLab);
    // Setup input for the reasoner
    Model input = ModelFactory.createUnion(TestData.enridaga.getModel(), TestData.alexdma.getModel());
    input = ModelFactory.createUnion(input, wphomepage.getModel());
    input = ModelFactory.createUnion(input, TestData.foaf);
    try {
        // Run the method
        Set<Statement> inferred = service.runTask(ReasoningService.Tasks.CLASSIFY, input, rules, false, null);
        // Expected statements
        Resource swResearcher = TestData.model.createResource(TestData.TEST_NS + "SWResearcher");
        Set<Statement> expected = new HashSet<Statement>();
        expected.add(TestData.model.createStatement(TestData.alexdma, RDF.type, swResearcher));
        expected.add(TestData.model.createStatement(TestData.enridaga, RDF.type, swResearcher));
        log.info("All the expected statements must be in the inferred output");
        Set<Statement> notInOutput = TestUtils.expectedStatementsCheck(inferred, expected);
        log.info("Are all expected statements in the inferred set (true)? {}", notInOutput.isEmpty());
        if (!notInOutput.isEmpty()) {
            for (Statement bad : notInOutput) {
                log.error("The following statement is not included in the reasoner output: {}", bad);
            }
        }
        assertTrue(notInOutput.isEmpty());
        // There must be only rdf:type output
        boolean onlyRdf = true;
        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);
            }
            if (!stat.getPredicate().equals(RDF.type)) {
                onlyRdf = false;
            }
        }
        log.info("Check for statements to be rdf:type only (true): {}", onlyRdf);
        assertTrue(onlyRdf);
    } 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();
    TestData.enridaga.removeProperties();
}
Also used : ReasoningServiceException(org.apache.stanbol.reasoners.servicesapi.ReasoningServiceException) Statement(com.hp.hpl.jena.rdf.model.Statement) Resource(com.hp.hpl.jena.rdf.model.Resource) Model(com.hp.hpl.jena.rdf.model.Model) Rule(com.hp.hpl.jena.reasoner.rulesys.Rule) InconsistentInputException(org.apache.stanbol.reasoners.servicesapi.InconsistentInputException) UnsupportedTaskException(org.apache.stanbol.reasoners.servicesapi.UnsupportedTaskException) HashSet(java.util.HashSet)

Example 4 with InconsistentInputException

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

the class JenaReasoningServiceTest method testEnrich.

/**
 * Tests the enrich(Model data) method
 *
 * @param service
 */
private void testEnrich(JenaReasoningService service) {
    // 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());
    try {
        // Run the method
        Set<Statement> inferred = service.runTask(ReasoningService.Tasks.ENRICH, input);
        // Prepare the input statements to check the output with
        Set<Statement> inputStatements = input.listStatements().toSet();
        boolean onlyInferred = true;
        log.info("Check for statements to be only the inferred ones");
        Set<Statement> badOnes = new HashSet<Statement>();
        for (Statement stat : inferred) {
            // Must not be a statement in input
            if (inputStatements.contains(stat)) {
                onlyInferred = false;
                badOnes.add(stat);
            }
        }
        log.info("Are there only inferred statements (true)? {}", onlyInferred);
        if (!onlyInferred) {
            for (Statement bad : badOnes) {
                log.error("Found a bad statement in output: {}", bad);
            }
        }
        assertTrue(onlyInferred);
    } 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) HashSet(java.util.HashSet)

Example 5 with InconsistentInputException

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

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