Search in sources :

Example 6 with ReasoningServiceException

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

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

the class AbstractOWLApiReasoningService method run.

/**
     * Generic method for running the reasoner
     * 
     * @param input
     * @param generators
     * @return
     */
@Override
public Set<OWLAxiom> run(OWLOntology input, List<InferredAxiomGenerator<? extends OWLAxiom>> generators) throws ReasoningServiceException, InconsistentInputException {
    log.debug("run(OWLOntology input, List<InferredAxiomGenerator<? extends OWLAxiom>> generators)");
    try {
        // Get the manager
        OWLOntologyManager manager = createOWLOntologyManager();
        // Get the reasoner
        OWLReasoner reasoner = getReasoner(input);
        log.info("Running {} reasoner on {} ", reasoner.getClass(), input.getOntologyID());
        // To generate inferred axioms
        InferredOntologyGenerator inferred = new InferredOntologyGenerator(reasoner, generators);
        // We fill an anonymous ontology with the result, the return the
        // axiom set
        Set<OWLAxiom> axioms = new HashSet<OWLAxiom>();
        try {
            OWLOntology output = manager.createOntology();
            log.debug("Created output ontology: {}", output);
            try {
                inferred.fillOntology(manager, output);
            } catch (InconsistentOntologyException i) {
                throw i;
            } catch (Throwable t) {
                log.error("Some problem occurred:\n {}", t.getStackTrace());
                throw new ReasoningServiceException();
            }
            log.debug("Filled ontology: {}", output);
            log.debug("Temporary ID is {}", output.getOntologyID());
            axioms = manager.getOntology(output.getOntologyID()).getAxioms();
            // IMPORTANT We remove the ontology from the manager
            manager.removeOntology(output);
        } catch (OWLOntologyCreationException e) {
            log.error("An exception have been thrown when instantiating the ontology");
            throw new ReasoningServiceException();
        }
        return axioms;
    } catch (InconsistentOntologyException inconsistent) {
        /**
             * TODO Add report. Why it is inconsistent?
             */
        throw new InconsistentInputException();
    } catch (Exception exception) {
        log.error("An exception have been thrown while executing method run()", exception);
        throw new ReasoningServiceException();
    }
}
Also used : OWLReasoner(org.semanticweb.owlapi.reasoner.OWLReasoner) InconsistentInputException(org.apache.stanbol.reasoners.servicesapi.InconsistentInputException) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) ReasoningServiceException(org.apache.stanbol.reasoners.servicesapi.ReasoningServiceException) InconsistentInputException(org.apache.stanbol.reasoners.servicesapi.InconsistentInputException) InconsistentOntologyException(org.semanticweb.owlapi.reasoner.InconsistentOntologyException) UnsupportedTaskException(org.apache.stanbol.reasoners.servicesapi.UnsupportedTaskException) ReasoningServiceException(org.apache.stanbol.reasoners.servicesapi.ReasoningServiceException) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) InconsistentOntologyException(org.semanticweb.owlapi.reasoner.InconsistentOntologyException) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager) OWLAxiom(org.semanticweb.owlapi.model.OWLAxiom) InferredOntologyGenerator(org.semanticweb.owlapi.util.InferredOntologyGenerator) HashSet(java.util.HashSet)

Example 8 with ReasoningServiceException

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

the class JenaReasoningServiceTest method testIsConsistentWithRules.

/**
     * Tests the isConsistent(Model,List<Rule>) method of a service
     * 
     * @param service
     */
private void testIsConsistentWithRules(JenaReasoningService service) {
    log.info("Testing reasoner of type {}", service.getClass());
    // Prepare the rule set
    String source = "" + "\n@prefix foaf: <" + TestData.FOAF_NS + ">." + "\n@prefix ex: <" + TestData.TEST_NS + ">." + "\n[rule1: (?a foaf:knows ?b) (?a foaf:workplaceHomepage ?w) (?b foaf:workplaceHomepage ?w) -> (?a ex:collegueOf ?b)] " + "\n[rule2: (?b foaf:knows ?a) -> (?a foaf:knows ?b)] " + "\n[rule3: (?a ex:collegueOf ?b) -> (?b ex:collegueOf ?a)] ";
    // 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");
    // Prepare data
    TestData.alexdma.addProperty(TestData.foaf_knows, TestData.enridaga);
    TestData.alexdma.addProperty(TestData.foaf_workplaceHomepage, wphomepage);
    TestData.enridaga.addProperty(TestData.foaf_workplaceHomepage, wphomepage);
    // Setup input for the reasoner
    Model input = ModelFactory.createUnion(TestData.enridaga.getModel(), TestData.alexdma.getModel());
    input = ModelFactory.createUnion(input, TestData.foaf);
    try {
        // Run the method
        boolean isConsistent = service.isConsistent(input, rules);
        // Assert true
        log.info("Is consistent (true)? {}", isConsistent);
        assertTrue(isConsistent);
    } catch (ReasoningServiceException 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) Resource(com.hp.hpl.jena.rdf.model.Resource) Model(com.hp.hpl.jena.rdf.model.Model) Rule(com.hp.hpl.jena.reasoner.rulesys.Rule)

Example 9 with ReasoningServiceException

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

the class JenaReasoningServiceTest method testIsConsistent.

/**
     * 
     * @param service
     */
private void testIsConsistent(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
        assertTrue(service.isConsistent(input));
    } catch (ReasoningServiceException e) {
        log.error("Error thrown: {}", e);
        assertTrue(false);
    }
    // Clean data
    TestData.alexdma.removeProperties();
}
Also used : ReasoningServiceException(org.apache.stanbol.reasoners.servicesapi.ReasoningServiceException) Model(com.hp.hpl.jena.rdf.model.Model)

Example 10 with ReasoningServiceException

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

the class JenaReasoningServiceTest method testEnrich2.

/**
     * Tests the enrich(Model data,boolean filtered) method
     * 
     * @param service
     */
private void testEnrich2(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, null, false, null);
        // Prepare the input statements to check the output with
        Set<Statement> inputStatements = input.listStatements().toSet();
        log.info("All the input statements must be in the inferred output");
        Set<Statement> notInOutput = new HashSet<Statement>();
        for (Statement stat : inputStatements) {
            if (!inferred.contains(stat)) {
                notInOutput.add(stat);
            }
        }
        log.info("Are all input statements in the inferred set (true)? {}", notInOutput.isEmpty());
        if (!notInOutput.isEmpty()) {
            for (Statement bad : notInOutput) {
                log.error("Found a statement not included in output: {}", bad);
            }
        }
        assertTrue(notInOutput.isEmpty());
    } 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)

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