Search in sources :

Example 6 with Resource

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

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

the class JenaToOwlConvert method ResourceJenaToOwlAxiom.

// //////////////////////////////////////////////////////////////////////////////
/**
     * This function converts every statments relative to a resource in an a set of OWLAxiom objects
     * 
     * @param jenadata
     *            {A resource in the form (S,P,O), it could be any kind of resource (a class, a data property,
     *            an object property and an instance) except a litteral}
     * @param format
     *            {The format of the ontology, i.e. "RDF/XML"}
     * @return {A set of axiom in the form of Set<OWLAxiom>}
     */
public synchronized Set<OWLAxiom> ResourceJenaToOwlAxiom(Resource jenadata, String format) {
    while (available == false) {
        try {
            wait();
        } catch (InterruptedException e) {
            System.err.println("ResourceJenaToOwlAxiom::: " + e);
        }
    }
    available = false;
    try {
        OntModel model = ModelFactory.createOntologyModel();
        StmtIterator prop = jenadata.listProperties();
        while (prop.hasNext()) {
            Statement stat = prop.nextStatement();
            model.add(stat);
            RDFNode obj = stat.getObject();
            if (obj.isResource()) {
                if (!obj.isURIResource()) {
                    StmtIterator aux = ((Resource) obj).listProperties();
                    while (aux.hasNext()) {
                        Statement stataux = aux.nextStatement();
                        model.add(stataux);
                    }
                }
            }
        }
        OWLOntology owlmodel = ModelJenaToOwlConvert(model, format);
        available = true;
        notifyAll();
        return owlmodel.getAxioms();
    } catch (Exception e) {
        System.err.print("ResourceJenaToOwlAxiom::: ");
        e.printStackTrace();
        return null;
    }
}
Also used : StmtIterator(com.hp.hpl.jena.rdf.model.StmtIterator) Statement(com.hp.hpl.jena.rdf.model.Statement) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) Resource(com.hp.hpl.jena.rdf.model.Resource) OntModel(com.hp.hpl.jena.ontology.OntModel) RDFNode(com.hp.hpl.jena.rdf.model.RDFNode) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) OWLOntologyStorageException(org.semanticweb.owlapi.model.OWLOntologyStorageException)

Example 8 with Resource

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

the class ConversionTester method testAxiomOwlToJenaResource.

public void testAxiomOwlToJenaResource() {
    JenaToOwlConvert j2o = new JenaToOwlConvert();
    OWLOntologyManager mgr = OWLManager.createOWLOntologyManager();
    OWLOntology ont = null;
    try {
        ont = mgr.createOntology();
    } catch (OWLOntologyCreationException e) {
        e.printStackTrace();
        fail("Can not create ontology");
    }
    OWLDataFactory factory = mgr.getOWLDataFactory();
    StmtIterator resource = null;
    OWLClass cls = factory.getOWLClass(IRI.create(CLAZZ));
    OWLDataProperty dp = factory.getOWLDataProperty(IRI.create(DP));
    OWLObjectProperty op = factory.getOWLObjectProperty(IRI.create(OP));
    OWLAnnotationProperty oa = factory.getOWLAnnotationProperty(IRI.create(label));
    OWLAnnotation oav = factory.getOWLAnnotation(oa, factory.getOWLStringLiteral(clazzlabel, "en"));
    OWLDatatype dt = factory.getOWLDatatype(IRI.create(DATATYPE));
    OWLNamedIndividual sub = factory.getOWLNamedIndividual(IRI.create(SUBJECT));
    OWLNamedIndividual obj = factory.getOWLNamedIndividual(IRI.create(OBJECT));
    OWLLiteral literal1 = factory.getOWLTypedLiteral(VALUE, dt);
    // Classe
    OWLDeclarationAxiom daxiomcls = factory.getOWLDeclarationAxiom(cls);
    // obj prop
    OWLDeclarationAxiom daxiomop = factory.getOWLDeclarationAxiom(op);
    // data prop
    OWLDeclarationAxiom daxiomdp = factory.getOWLDeclarationAxiom(dp);
    // subject
    OWLDeclarationAxiom daxiomsub = factory.getOWLDeclarationAxiom(sub);
    // object
    OWLDeclarationAxiom daxiomobj = factory.getOWLDeclarationAxiom(obj);
    // Istanza
    OWLClassAssertionAxiom axiomsub = factory.getOWLClassAssertionAxiom(cls, sub);
    // Istanza
    OWLClassAssertionAxiom axiomobj = factory.getOWLClassAssertionAxiom(cls, obj);
    // Obj
    OWLObjectPropertyAssertionAxiom axiomop = factory.getOWLObjectPropertyAssertionAxiom(op, sub, obj);
    // prop
    // tra
    // individui
    OWLDataPropertyAssertionAxiom axiomvalue = factory.getOWLDataPropertyAssertionAxiom(dp, obj, // Dataprop all'istanza;
    literal1);
    // Annotazione
    OWLAnnotationAssertionAxiom axioman = factory.getOWLAnnotationAssertionAxiom(cls.getIRI(), oav);
    mgr.addAxiom(ont, daxiomcls);
    mgr.addAxiom(ont, daxiomop);
    mgr.addAxiom(ont, daxiomdp);
    mgr.addAxiom(ont, daxiomsub);
    mgr.addAxiom(ont, daxiomobj);
    mgr.addAxiom(ont, axiomsub);
    mgr.addAxiom(ont, axiomobj);
    mgr.addAxiom(ont, axiomop);
    mgr.addAxiom(ont, axiomvalue);
    mgr.addAxiom(ont, axioman);
    Set<OWLAxiom> setaxiom = ont.getAxioms();
    try {
        resource = j2o.AxiomOwlToJenaResource(setaxiom, RDFXML);
        if (resource == null) {
            fail("Some errors occur");
        } else {
            String statment = "[http://www.w3.org/2000/01/rdf-schema#label, http://www.w3.org/2000/01/rdf-schema#range, http://www.w3.org/2000/01/rdf-schema#Literal] " + "[http://example.org/dummy#hasAge, http://www.w3.org/1999/02/22-rdf-syntax-ns#type, http://www.w3.org/2002/07/owl#DatatypeProperty] " + "[http://example.org/dummy#Linus, http://example.org/dummy#hasAge, \"8\"^^http://www.w3.org/2001/XMLSchema#int] " + "[http://example.org/dummy#Linus, http://www.w3.org/1999/02/22-rdf-syntax-ns#type, http://example.org/dummy#Peanut] " + "[http://example.org/dummy#hasSibling, http://www.w3.org/1999/02/22-rdf-syntax-ns#type, http://www.w3.org/2002/07/owl#ObjectProperty] " + "[http://example.org/dummy#Lucy, http://example.org/dummy#hasSibling, http://example.org/dummy#Linus] " + "[http://example.org/dummy#Lucy, http://www.w3.org/1999/02/22-rdf-syntax-ns#type, http://example.org/dummy#Peanut] " + "[http://www.w3.org/2000/01/rdf-schema#label, http://www.w3.org/1999/02/22-rdf-syntax-ns#type, http://www.w3.org/2002/07/owl#AnnotationProperty] " + "[http://example.org/dummy#Peanut, http://www.w3.org/2000/01/rdf-schema#label, \"Peanut\"@en] " + "[http://example.org/dummy#Peanut, http://www.w3.org/1999/02/22-rdf-syntax-ns#type, http://www.w3.org/2002/07/owl#Class]";
            int size = setaxiom.size();
            int count = 0;
            while (resource.hasNext()) {
                Statement stm = resource.nextStatement();
                Resource jsubj = stm.getSubject();
                if (jsubj.getURI().equals(OP.toString()) || jsubj.getURI().equals(DP.toString()) || jsubj.getURI().equals(CLAZZ.toString()) || jsubj.getURI().equals(OBJECT.toString()) || jsubj.getURI().equals(SUBJECT.toString()) || jsubj.getURI().equals(label.toString()))
                    if (statment.contains(stm.toString()))
                        count++;
            }
            assertEquals(size, count);
        }
    } catch (Exception e) {
        e.printStackTrace();
        fail("Exception caugth");
    } finally {
        assertNotNull(resource);
    }
}
Also used : OWLDeclarationAxiom(org.semanticweb.owlapi.model.OWLDeclarationAxiom) OWLDatatype(org.semanticweb.owlapi.model.OWLDatatype) OWLAnnotationProperty(org.semanticweb.owlapi.model.OWLAnnotationProperty) OWLDataPropertyAssertionAxiom(org.semanticweb.owlapi.model.OWLDataPropertyAssertionAxiom) OWLDataProperty(org.semanticweb.owlapi.model.OWLDataProperty) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) OWLNamedIndividual(org.semanticweb.owlapi.model.OWLNamedIndividual) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager) OWLAxiom(org.semanticweb.owlapi.model.OWLAxiom) OWLClassAssertionAxiom(org.semanticweb.owlapi.model.OWLClassAssertionAxiom) StmtIterator(com.hp.hpl.jena.rdf.model.StmtIterator) OWLAnnotationAssertionAxiom(org.semanticweb.owlapi.model.OWLAnnotationAssertionAxiom) OWLAnnotation(org.semanticweb.owlapi.model.OWLAnnotation) Statement(com.hp.hpl.jena.rdf.model.Statement) Resource(com.hp.hpl.jena.rdf.model.Resource) OWLObjectProperty(org.semanticweb.owlapi.model.OWLObjectProperty) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) OWLLiteral(org.semanticweb.owlapi.model.OWLLiteral) OWLObjectPropertyAssertionAxiom(org.semanticweb.owlapi.model.OWLObjectPropertyAssertionAxiom) OWLClass(org.semanticweb.owlapi.model.OWLClass) OWLDataFactory(org.semanticweb.owlapi.model.OWLDataFactory)

Example 9 with Resource

use of com.hp.hpl.jena.rdf.model.Resource in project lucene-skos by behas.

the class SKOSEngineImpl method indexSKOSModel.

/**
     * Creates the synonym index
     *
     * @throws IOException
     */
private void indexSKOSModel() throws IOException {
    IndexWriterConfig cfg = new IndexWriterConfig(analyzer);
    IndexWriter writer = new IndexWriter(indexDir, cfg);
    writer.getConfig().setRAMBufferSizeMB(48);
    /* iterate SKOS concepts, create Lucene docs and add them to the index */
    ResIterator concept_iter = skosModel.listResourcesWithProperty(RDF.type, SKOS.Concept);
    while (concept_iter.hasNext()) {
        Resource skos_concept = concept_iter.next();
        Document concept_doc = createDocumentsFromConcept(skos_concept);
        writer.addDocument(concept_doc);
    }
    writer.close();
}
Also used : ResIterator(com.hp.hpl.jena.rdf.model.ResIterator) Resource(com.hp.hpl.jena.rdf.model.Resource) Document(org.apache.lucene.document.Document)

Example 10 with Resource

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

Aggregations

Resource (com.hp.hpl.jena.rdf.model.Resource)14 Statement (com.hp.hpl.jena.rdf.model.Statement)6 Model (com.hp.hpl.jena.rdf.model.Model)5 StmtIterator (com.hp.hpl.jena.rdf.model.StmtIterator)5 Rule (com.hp.hpl.jena.reasoner.rulesys.Rule)3 OWLOntologyCreationException (org.semanticweb.owlapi.model.OWLOntologyCreationException)3 OntModel (com.hp.hpl.jena.ontology.OntModel)2 Property (com.hp.hpl.jena.rdf.model.Property)2 RDFNode (com.hp.hpl.jena.rdf.model.RDFNode)2 URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2 HashSet (java.util.HashSet)2 IRI (org.apache.clerezza.commons.rdf.IRI)2 ReasoningServiceException (org.apache.stanbol.reasoners.servicesapi.ReasoningServiceException)2 OWLOntology (org.semanticweb.owlapi.model.OWLOntology)2 AnnotationProperty (com.hp.hpl.jena.ontology.AnnotationProperty)1 DatatypeProperty (com.hp.hpl.jena.ontology.DatatypeProperty)1 Individual (com.hp.hpl.jena.ontology.Individual)1 ObjectProperty (com.hp.hpl.jena.ontology.ObjectProperty)1 OntClass (com.hp.hpl.jena.ontology.OntClass)1