Search in sources :

Example 6 with SWRLRule

use of org.semanticweb.owlapi.model.SWRLRule 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)

Example 7 with SWRLRule

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

the class SWRLAdpterTest method test.

@SuppressWarnings("unchecked")
@Test
public void test() {
    try {
        List<SWRLRule> rules = (List<SWRLRule>) ruleAdapter.adaptTo(recipeGood, SWRLRule.class);
        StringBuilder sb = new StringBuilder();
        for (SWRLRule rule : rules) {
            sb.append(rule.toString());
        }
        Assert.assertNotSame(sb.toString(), "");
    } catch (UnavailableRuleObjectException e) {
        Assert.fail(e.getMessage());
    } catch (UnsupportedTypeForExportException e) {
        Assert.fail(e.getMessage());
    } catch (RuleAtomCallExeption e) {
        Assert.fail(e.getMessage());
    }
}
Also used : UnsupportedTypeForExportException(org.apache.stanbol.rules.base.api.UnsupportedTypeForExportException) UnavailableRuleObjectException(org.apache.stanbol.rules.base.api.UnavailableRuleObjectException) SWRLRule(org.semanticweb.owlapi.model.SWRLRule) List(java.util.List) RuleAtomCallExeption(org.apache.stanbol.rules.base.api.RuleAtomCallExeption) Test(org.junit.Test)

Example 8 with SWRLRule

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

the class AbstractOWLApiReasoningService method run.

/**
     * Merges the SWRL rules in the input ontology, then calls run(OWLOntology,List<InferredAxiomGenerator<?
     * extends OWLAxiom>>)
     * 
     * @param ontology
     * @param rules
     * @param generators
     * @return
     */
@Override
public Set<OWLAxiom> run(OWLOntology ontology, List<SWRLRule> rules, List<InferredAxiomGenerator<? extends OWLAxiom>> generators) throws ReasoningServiceException, InconsistentInputException {
    log.debug("Called method run(OWLOntology,List<SWRLRule>,List)");
    OWLOntologyManager manager = ontology.getOWLOntologyManager();
    log.debug("Adding SWRL rules to the input ontology.");
    Set<SWRLRule> ruleSet = new HashSet<SWRLRule>();
    ruleSet.addAll(rules);
    manager.addAxioms(ontology, ruleSet);
    if (log.isDebugEnabled())
        for (OWLAxiom a : ontology.getAxioms()) {
            log.debug("Axiom {}", a);
        }
    log.debug("Calling the run method.");
    return run(ontology, generators);
}
Also used : SWRLRule(org.semanticweb.owlapi.model.SWRLRule) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager) OWLAxiom(org.semanticweb.owlapi.model.OWLAxiom) HashSet(java.util.HashSet)

Aggregations

SWRLRule (org.semanticweb.owlapi.model.SWRLRule)8 HashSet (java.util.HashSet)4 ReasoningServiceException (org.apache.stanbol.reasoners.servicesapi.ReasoningServiceException)4 OWLAxiom (org.semanticweb.owlapi.model.OWLAxiom)4 OWLOntology (org.semanticweb.owlapi.model.OWLOntology)4 OWLOntologyCreationException (org.semanticweb.owlapi.model.OWLOntologyCreationException)4 OWLOntologyManager (org.semanticweb.owlapi.model.OWLOntologyManager)4 UnsupportedTypeForExportException (org.apache.stanbol.rules.base.api.UnsupportedTypeForExportException)3 Rule (com.hp.hpl.jena.reasoner.rulesys.Rule)2 Iterator (java.util.Iterator)2 List (java.util.List)2 InconsistentInputException (org.apache.stanbol.reasoners.servicesapi.InconsistentInputException)2 UnsupportedTaskException (org.apache.stanbol.reasoners.servicesapi.UnsupportedTaskException)2 RuleAtomCallExeption (org.apache.stanbol.rules.base.api.RuleAtomCallExeption)2 UnavailableRuleObjectException (org.apache.stanbol.rules.base.api.UnavailableRuleObjectException)2 AddImport (org.semanticweb.owlapi.model.AddImport)2 OWLImportsDeclaration (org.semanticweb.owlapi.model.OWLImportsDeclaration)2 OWLLogicalAxiom (org.semanticweb.owlapi.model.OWLLogicalAxiom)2 OWLOntologyID (org.semanticweb.owlapi.model.OWLOntologyID)2 Model (com.hp.hpl.jena.rdf.model.Model)1