Search in sources :

Example 6 with Statement

use of com.hp.hpl.jena.rdf.model.Statement 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 7 with Statement

use of com.hp.hpl.jena.rdf.model.Statement 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 8 with Statement

use of com.hp.hpl.jena.rdf.model.Statement in project stanbol by apache.

the class AbstractJenaReasoningService method enrich.

/**
     * Enriching: 1) Perform reasoning on a reasoner customized with the given rule set 2) Returns all the
     * statements (filtered = false) or only inferred ones (filtered = true)
     * 
     * This is a default implementation of task {@see ReasoningService.Tasks.ENRICH} when a set of rules is
     * given. Subclasses may want to change it.
     * 
     * @param data
     * @param rules
     * @param filtered
     * @return
     */
protected Set<Statement> enrich(Model data, List<Rule> rules, boolean filtered) {
    log.debug(" enrich(Model data, List<Rule> rules, boolean filtered)");
    // We keep the original list to prune the data after, if necessary
    if (filtered) {
        Set<Statement> original = new HashSet<Statement>();
        original.addAll(data.listStatements().toSet());
        log.debug(" original statements are: {}", original.size());
        InfModel i = run(data, rules);
        Set<Statement> inferred = i.listStatements().toSet();
        log.debug(" inferred statements are: {}", inferred.size());
        return prune(original, inferred);
    } else {
        return run(data, rules).listStatements().toSet();
    }
}
Also used : Statement(com.hp.hpl.jena.rdf.model.Statement) InfModel(com.hp.hpl.jena.rdf.model.InfModel) HashSet(java.util.HashSet)

Example 9 with Statement

use of com.hp.hpl.jena.rdf.model.Statement in project stanbol by apache.

the class AbstractJenaReasoningService method enrich.

/**
     * Enriching: 1) Perform reasoning 2) Returns all the statements (filtered = false) or only inferred ones
     * (filtered = true)
     * 
     * This is a default implementation of task {@see ReasoningService.Tasks.ENRICH}. Subclasses may want to
     * change it.
     * 
     * @param data
     * @param rules
     * @return
     */
protected Set<Statement> enrich(Model data, boolean filtered) {
    log.debug(" enrich(Model data, boolean filtered)");
    // We keep the original list to prune the data after, if necessary
    if (filtered) {
        Set<Statement> original = new HashSet<Statement>();
        original.addAll(data.listStatements().toSet());
        log.debug(" original statements are: {}", original.size());
        InfModel i = run(data);
        Set<Statement> inferred = i.listStatements().toSet();
        log.debug(" inferred statements are: {}", inferred.size());
        return prune(original, inferred);
    } else {
        return run(data).listStatements().toSet();
    }
}
Also used : Statement(com.hp.hpl.jena.rdf.model.Statement) InfModel(com.hp.hpl.jena.rdf.model.InfModel) HashSet(java.util.HashSet)

Example 10 with Statement

use of com.hp.hpl.jena.rdf.model.Statement in project stanbol by apache.

the class JenaOWLReasoningServiceTest method testSubclassOf.

/**
	 * Tests rdfs:subClassOf inference with OWLReasoner
	 */
@Test
public void testSubclassOf() {
    log.info("Testing rdfs:subClassOf inference with OWL reasoner");
    // Prepare data
    TestData.alexdma.addProperty(RDF.type, TestData.foaf_Person);
    // Setup input for the reasoner
    Model input = ModelFactory.createUnion(TestData.foaf, TestData.alexdma.getModel());
    // Am I a foaf:Agent?
    log.info("Instantiating the OWL reasoner");
    InfModel inferred = reasoningService.run(input);
    Statement isAgent = TestData.model.createStatement(TestData.alexdma, RDF.type, TestData.foaf_Agent);
    //		log.info("Statements: {}",
    //				TestUtils.printStatements(inferred, TestData.alexdma, RDF.type));
    log.info("Is any foaf:Person a foaf:Agent...? {}", inferred.contains(isAgent));
    assertTrue(inferred.contains(isAgent));
    // Reset resource to be clean for other tests
    TestData.alexdma.removeProperties();
}
Also used : Statement(com.hp.hpl.jena.rdf.model.Statement) InfModel(com.hp.hpl.jena.rdf.model.InfModel) Model(com.hp.hpl.jena.rdf.model.Model) InfModel(com.hp.hpl.jena.rdf.model.InfModel) Test(org.junit.Test)

Aggregations

Statement (com.hp.hpl.jena.rdf.model.Statement)22 Model (com.hp.hpl.jena.rdf.model.Model)13 InfModel (com.hp.hpl.jena.rdf.model.InfModel)7 HashSet (java.util.HashSet)7 Test (org.junit.Test)7 Resource (com.hp.hpl.jena.rdf.model.Resource)6 StmtIterator (com.hp.hpl.jena.rdf.model.StmtIterator)6 ReasoningServiceException (org.apache.stanbol.reasoners.servicesapi.ReasoningServiceException)6 InconsistentInputException (org.apache.stanbol.reasoners.servicesapi.InconsistentInputException)5 UnsupportedTaskException (org.apache.stanbol.reasoners.servicesapi.UnsupportedTaskException)5 OWLOntologyCreationException (org.semanticweb.owlapi.model.OWLOntologyCreationException)5 OWLOntology (org.semanticweb.owlapi.model.OWLOntology)4 Rule (com.hp.hpl.jena.reasoner.rulesys.Rule)3 OWLAnnotationProperty (org.semanticweb.owlapi.model.OWLAnnotationProperty)3 OWLAxiom (org.semanticweb.owlapi.model.OWLAxiom)3 OWLLiteral (org.semanticweb.owlapi.model.OWLLiteral)3 OWLNamedIndividual (org.semanticweb.owlapi.model.OWLNamedIndividual)3 OWLObjectProperty (org.semanticweb.owlapi.model.OWLObjectProperty)3 OntModel (com.hp.hpl.jena.ontology.OntModel)2 Property (com.hp.hpl.jena.rdf.model.Property)2