Search in sources :

Example 1 with OntModel

use of org.apache.jena.ontology.OntModel in project ORCID-Source by ORCID.

the class RDFMessageBodyWriter method writeTo.

/**
     * Write a type to an HTTP response. The response header map is mutable but
     * any changes must be made before writing to the output stream since the
     * headers will be flushed prior to writing the response body.
     * 
     * @param message
     *            the instance to write.
     * @param type
     *            the class of object that is to be written.
     * @param genericType
     *            the type of object to be written, obtained either by
     *            reflection of a resource method return type or by inspection
     *            of the returned instance.
     *            {@link javax.ws.rs.core.GenericEntity} provides a way to
     *            specify this information at runtime.
     * @param annotations
     *            an array of the annotations on the resource method that
     *            returns the object.
     * @param mediaType
     *            the media type of the HTTP entity.
     * @param httpHeaders
     *            a mutable map of the HTTP response headers.
     * @param entityStream
     *            the {@link java.io.OutputStream} for the HTTP entity. The
     *            implementation should not close the output stream.
     * @throws java.io.IOException
     *             if an IO error arises
     * @throws javax.ws.rs.WebApplicationException
     *             if a specific HTTP error response needs to be produced. Only
     *             effective if thrown prior to the response being committed.
     */
@Override
public void writeTo(OrcidMessage xml, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
    OntModel m = getOntModel();
    if (xml.getErrorDesc() != null) {
        describeError(xml.getErrorDesc(), m);
    }
    OrcidProfile orcidProfile = xml.getOrcidProfile();
    // System.out.println(httpHeaders);
    Individual profileDoc = null;
    if (orcidProfile != null) {
        Individual person = describePerson(orcidProfile, m);
        if (person != null) {
            profileDoc = describeAccount(orcidProfile, m, person);
        }
    }
    MediaType jsonLd = new MediaType("application", "ld+json");
    MediaType nTriples = new MediaType("application", "n-triples");
    MediaType rdfXml = new MediaType("application", "rdf+xml");
    String base = null;
    if (getUriInfo() != null) {
        getUriInfo().getAbsolutePath().toASCIIString();
    }
    if (mediaType.isCompatible(nTriples)) {
        // NOTE: N-Triples requires absolute URIs
        m.write(entityStream, "N-TRIPLES");
    } else if (mediaType.isCompatible(jsonLd)) {
        m.write(entityStream, "JSON-LD", base);
    } else if (mediaType.isCompatible(rdfXml)) {
        m.write(entityStream, "RDF/XML", base);
    } else {
        // Turtle is the safest default        	
        m.write(entityStream, "TURTLE", base);
    }
}
Also used : OrcidProfile(org.orcid.jaxb.model.message.OrcidProfile) Individual(org.apache.jena.ontology.Individual) OntModel(org.apache.jena.ontology.OntModel) MediaType(javax.ws.rs.core.MediaType)

Example 2 with OntModel

use of org.apache.jena.ontology.OntModel in project jena by apache.

the class JenaOSGITest method testJenaCore.

@Test
public void testJenaCore() throws Exception {
    Model model = makeModel();
    Writer writer = new StringWriter();
    model.write(writer, "N-Triples");
    assertEquals("<http://example.com/alice> <http://xmlns.com/foaf/0.1/knows> <http://example.com/bob> .", writer.toString().trim());
    OntModel ontModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM_RULE_INF);
    ObjectProperty knowsObjProp = ontModel.createObjectProperty(knows.getURI());
    ObjectProperty hasFriend = ontModel.createObjectProperty("http://example.com/has_friend");
    hasFriend.addSuperProperty(knowsObjProp);
    Individual aliceIndividual = ontModel.createIndividual(alice);
    Individual bobIndividiual = ontModel.createIndividual(bob);
    ontModel.add(aliceIndividual, hasFriend, bobIndividiual);
    assertTrue(aliceIndividual.hasProperty(knowsObjProp, bobIndividiual));
}
Also used : ObjectProperty(org.apache.jena.ontology.ObjectProperty) StringWriter(java.io.StringWriter) Individual(org.apache.jena.ontology.Individual) OntModel(org.apache.jena.ontology.OntModel) Model(org.apache.jena.rdf.model.Model) OntModel(org.apache.jena.ontology.OntModel) StringWriter(java.io.StringWriter) Writer(java.io.Writer) Test(org.junit.Test)

Example 3 with OntModel

use of org.apache.jena.ontology.OntModel in project legato by DOREMUS-ANR.

the class ModelManager method loadModel.

/**
 *************************************
 ***Load the RDF model from an RDF file
 **************************************
 */
public static Model loadModel(String inputFile) {
    Model model = ModelFactory.createDefaultModel();
    try {
        InputStream in = new FileInputStream(inputFile);
        String ext = FilenameUtils.getExtension(inputFile);
        if (ext.equals("nt")) {
            model.read(in, null, "N-TRIPLES");
        } else if (ext.equals("ttl")) {
            model.read(in, null, "TTL");
        } else if (ext.equals("rdf")) {
            model.read(in, null, "RDF/XML");
        } else if (ext.equals("owl")) {
            OntModel ontologyModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM, null);
            ontologyModel.read(in, "RDF/XML-ABBREV");
            model = ontologyModel.getBaseModel();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return model;
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OntModel(org.apache.jena.ontology.OntModel) Model(org.apache.jena.rdf.model.Model) OntModel(org.apache.jena.ontology.OntModel) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException)

Example 4 with OntModel

use of org.apache.jena.ontology.OntModel in project jena by apache.

the class TestFileManager method testFileManagerReadOntModel.

@Test
public void testFileManagerReadOntModel() {
    OntModel model = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
    FileManager.getInternal().readModelInternal(model, testingDir + "/data.ttl");
    // Check
    Individual ind = model.getIndividual("http://example.com/individual");
    String t = ind.getOntClass().getURI();
    assertEquals("http://example.com/T", t);
    long c1 = model.size();
    model.loadImports();
    long c2 = model.size();
    assertEquals(c1, c2);
}
Also used : Individual(org.apache.jena.ontology.Individual) OntModel(org.apache.jena.ontology.OntModel) Test(org.junit.Test)

Example 5 with OntModel

use of org.apache.jena.ontology.OntModel in project jena by apache.

the class TestReasoners method testTransitiveCycleBug.

/**
 * Cycle bug in transitive reasoner
 */
public void testTransitiveCycleBug() {
    Model m = FileManager.getInternal().loadModelInternal("file:testing/reasoners/bugs/unbroken.n3");
    OntModel om = ModelFactory.createOntologyModel(OntModelSpec.RDFS_MEM_TRANS_INF, m);
    OntClass rootClass = om.getOntClass(RDFS.Resource.getURI());
    Resource c = m.getResource("c");
    Set<OntClass> direct = rootClass.listSubClasses(true).toSet();
    assertFalse(direct.contains(c));
}
Also used : OntModel(org.apache.jena.ontology.OntModel) InfModel(org.apache.jena.rdf.model.InfModel) Model(org.apache.jena.rdf.model.Model) Resource(org.apache.jena.rdf.model.Resource) OntModel(org.apache.jena.ontology.OntModel) OntClass(org.apache.jena.ontology.OntClass)

Aggregations

OntModel (org.apache.jena.ontology.OntModel)17 Individual (org.apache.jena.ontology.Individual)4 InputStream (java.io.InputStream)3 Model (org.apache.jena.rdf.model.Model)3 MediaType (javax.ws.rs.core.MediaType)2 Resource (org.apache.jena.rdf.model.Resource)2 Test (org.junit.Test)2 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 Writer (java.io.Writer)1 Triple (org.apache.jena.graph.Triple)1 DatatypeProperty (org.apache.jena.ontology.DatatypeProperty)1 ObjectProperty (org.apache.jena.ontology.ObjectProperty)1 OntClass (org.apache.jena.ontology.OntClass)1 OntModelImpl (org.apache.jena.ontology.impl.OntModelImpl)1 InfModel (org.apache.jena.rdf.model.InfModel)1 StmtIterator (org.apache.jena.rdf.model.StmtIterator)1 ValidityReport (org.apache.jena.reasoner.ValidityReport)1 OrcidProfile (org.orcid.jaxb.model.message.OrcidProfile)1