Search in sources :

Example 16 with Statement

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

the class ConversionTester method testResourceJenaToOwlAxiom.

public void testResourceJenaToOwlAxiom() {
    JenaToOwlConvert j2o = new JenaToOwlConvert();
    OntModel model = ModelFactory.createOntologyModel();
    OntClass jenaclass = model.createClass(CLAZZ.toString());
    ObjectProperty jenaobprop = model.createObjectProperty(OP.toString());
    DatatypeProperty jenadataprop = model.createDatatypeProperty(DP.toString());
    Individual jenasub = model.createIndividual(SUBJECT.toString(), jenaclass);
    Individual jenaobj = model.createIndividual(OBJECT.toString(), jenaclass);
    AnnotationProperty jenaanno = model.createAnnotationProperty(label.toString());
    Literal value = model.createTypedLiteral(VALUE, DATATYPE.toString());
    model.add(jenasub, jenaobprop, jenaobj);
    model.add(jenasub, jenadataprop, value);
    model.add(jenasub, jenaanno, "Lucy", "en");
    Set<OWLAxiom> owlaxiom = null;
    try {
        owlaxiom = j2o.ResourceJenaToOwlAxiom(jenasub, RDFXML);
        if (owlaxiom == null) {
            fail("Some errors occur");
        } else {
            StmtIterator str = model.listStatements();
            int count = 0;
            while (str.hasNext()) {
                Statement stm = str.next();
                Resource subject = stm.getSubject();
                if (SUBJECT.toString().equals(subject.getURI()))
                    count++;
            }
            if (count == owlaxiom.size()) {
                assertEquals(count, owlaxiom.size());
            } else {
                fail("The number of axioms don't match the number of statement");
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        fail("Exception caugth");
    } finally {
        assertNotNull(owlaxiom);
    }
}
Also used : OWLObjectProperty(org.semanticweb.owlapi.model.OWLObjectProperty) ObjectProperty(com.hp.hpl.jena.ontology.ObjectProperty) StmtIterator(com.hp.hpl.jena.rdf.model.StmtIterator) Statement(com.hp.hpl.jena.rdf.model.Statement) Resource(com.hp.hpl.jena.rdf.model.Resource) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) AnnotationProperty(com.hp.hpl.jena.ontology.AnnotationProperty) OWLAnnotationProperty(org.semanticweb.owlapi.model.OWLAnnotationProperty) Individual(com.hp.hpl.jena.ontology.Individual) OWLNamedIndividual(org.semanticweb.owlapi.model.OWLNamedIndividual) OWLLiteral(org.semanticweb.owlapi.model.OWLLiteral) Literal(com.hp.hpl.jena.rdf.model.Literal) OntModel(com.hp.hpl.jena.ontology.OntModel) OWLAxiom(org.semanticweb.owlapi.model.OWLAxiom) OntClass(com.hp.hpl.jena.ontology.OntClass) DatatypeProperty(com.hp.hpl.jena.ontology.DatatypeProperty)

Example 17 with Statement

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

the class JenaToClerezzaConverterTest method testGraphToJenaModel.

@Test
public void testGraphToJenaModel() {
    /*
		 * Convert the Graph to a Jena Model.
		 */
    Model model = JenaToClerezzaConverter.clerezzaGraphToJenaModel(mGraph);
    /*
		 * Print all the triples contained in the Jena Model.
		 */
    StmtIterator stmtIt = model.listStatements();
    while (stmtIt.hasNext()) {
        Statement statement = stmtIt.next();
        log.info(statement.toString());
    }
}
Also used : StmtIterator(com.hp.hpl.jena.rdf.model.StmtIterator) Statement(com.hp.hpl.jena.rdf.model.Statement) Model(com.hp.hpl.jena.rdf.model.Model) Test(org.junit.Test)

Example 18 with Statement

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

the class AbstractJenaReasoningService method prune.

/**
     * Removes the statements in the first set from the second set
     * 
     * @param input
     * @param statements
     * @return
     */
protected final Set<Statement> prune(Set<Statement> first, Set<Statement> second) {
    log.debug(" prune(Set<Statement> first[{}], Set<Statement> second[{}])", first.size(), second.size());
    Set<Statement> remove = new HashSet<Statement>();
    for (Statement s : second) {
        if (first.contains(s)) {
            remove.add(s);
        }
    }
    log.debug(" ---- removing {} statements from {}", first.size(), second.size());
    second.removeAll(remove);
    return second;
}
Also used : Statement(com.hp.hpl.jena.rdf.model.Statement) HashSet(java.util.HashSet)

Example 19 with Statement

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

Example 20 with Statement

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

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