Search in sources :

Example 1 with JenaSerializerProvider

use of org.apache.clerezza.rdf.jena.serializer.JenaSerializerProvider in project stanbol by apache.

the class TestUtils method logEnhancements.

public static void logEnhancements(ContentItem ci) {
    JenaSerializerProvider serializer = new JenaSerializerProvider();
    ByteArrayOutputStream logOut = new ByteArrayOutputStream();
    serializer.serialize(logOut, ci.getMetadata(), TURTLE);
    log.info("Enhancements: \n{}", new String(logOut.toByteArray(), Charset.forName("UTF-8")));
}
Also used : JenaSerializerProvider(org.apache.clerezza.rdf.jena.serializer.JenaSerializerProvider) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 2 with JenaSerializerProvider

use of org.apache.clerezza.rdf.jena.serializer.JenaSerializerProvider in project stanbol by apache.

the class JenaToClerezzaConverter method clerezzaGraphToJenaModel.

/**
 * Converts a Clerezza {@link Graph} to a Jena {@link Model}.
 *
 * @param mGraph {@link Graph}
 * @return the equivalent Jena {@link Model}.
 */
public static Model clerezzaGraphToJenaModel(org.apache.clerezza.commons.rdf.Graph mGraph) {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    SerializingProvider serializingProvider = new JenaSerializerProvider();
    serializingProvider.serialize(out, mGraph, SupportedFormat.RDF_XML);
    ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
    Model jenaModel = ModelFactory.createDefaultModel();
    jenaModel.read(in, null);
    return jenaModel;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) JenaSerializerProvider(org.apache.clerezza.rdf.jena.serializer.JenaSerializerProvider) Model(com.hp.hpl.jena.rdf.model.Model) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SerializingProvider(org.apache.clerezza.rdf.core.serializedform.SerializingProvider)

Example 3 with JenaSerializerProvider

use of org.apache.clerezza.rdf.jena.serializer.JenaSerializerProvider in project stanbol by apache.

the class OWLAPIToClerezzaConverter method clerezzaGraphToOWLOntology.

public static OWLOntology clerezzaGraphToOWLOntology(org.apache.clerezza.commons.rdf.Graph graph, OWLOntologyManager ontologyManager) {
    /*
         * The root graph can be serialized and de-serialized, but before that we should decide what to do
         * with imports. We can proceed as follows:
         * 
         * for each import statement, - check if the ontology manager has (1) an ontology or (2) a mapping. -
         * if (1), just get it and add it to the merge pool - if (2), do nothing. the ontology manager should
         * handle it when loading the root, - if neither, fetch the graph and call the procideure on it
         * 
         * Alternatively, construct the whole reverse imports stack, then traverse it again, get the
         * OWLOntology version for each (with the phony mapper set) and add it to the merge pool
         * 
         * If it works, just add all the triples to a Graph, but no, we don't want to store that
         * change.
         */
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    SerializingProvider serializingProvider = new JenaSerializerProvider();
    serializingProvider.serialize(out, graph, SupportedFormat.RDF_XML);
    ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
    OWLOntology ontology = null;
    try {
        ontology = ontologyManager.loadOntologyFromOntologyDocument(in);
    } catch (OWLOntologyAlreadyExistsException e) {
        ontology = ontologyManager.getOntology(e.getOntologyID());
    } catch (OWLOntologyCreationException e) {
        log.error("Failed to serialize OWL Ontology " + ontology + "for conversion", e);
    }
    return ontology;
}
Also used : OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) ByteArrayInputStream(java.io.ByteArrayInputStream) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) JenaSerializerProvider(org.apache.clerezza.rdf.jena.serializer.JenaSerializerProvider) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SerializingProvider(org.apache.clerezza.rdf.core.serializedform.SerializingProvider) OWLOntologyAlreadyExistsException(org.semanticweb.owlapi.model.OWLOntologyAlreadyExistsException)

Example 4 with JenaSerializerProvider

use of org.apache.clerezza.rdf.jena.serializer.JenaSerializerProvider in project stanbol by apache.

the class ZemantaEnhancementEngineTest method tesetBioText.

@Test
public void tesetBioText() throws EngineException, IOException {
    ContentItem ci = wrapAsContentItem(BIO_DOMAIN_TEXT);
    try {
        zemantaEngine.computeEnhancements(ci);
    } catch (EngineException e) {
        RemoteServiceHelper.checkServiceUnavailable(e);
        return;
    }
    JenaSerializerProvider serializer = new JenaSerializerProvider();
    serializer.serialize(System.out, ci.getMetadata(), TURTLE);
    Map<IRI, RDFTerm> expectedValues = new HashMap<IRI, RDFTerm>();
    expectedValues.put(Properties.ENHANCER_EXTRACTED_FROM, ci.getUri());
    expectedValues.put(Properties.DC_CREATOR, LiteralFactory.getInstance().createTypedLiteral(zemantaEngine.getClass().getName()));
    // deactivate require fise:confidence values for fise:TextAnnotations, because
    // the one used to group the TopicAnnotations does not have a confidence value
    int textAnnoNum = validateAllTextAnnotations(ci.getMetadata(), BIO_DOMAIN_TEXT, expectedValues);
    log.info(textAnnoNum + " TextAnnotations found ...");
    // adding null as expected for confidence makes it a required property
    expectedValues.put(Properties.ENHANCER_CONFIDENCE, null);
    int entityAnnoNum = EnhancementStructureHelper.validateAllEntityAnnotations(ci.getMetadata(), expectedValues);
    log.info(entityAnnoNum + " EntityAnnotations found ...");
    int topicAnnoNum = EnhancementStructureHelper.validateAllTopicAnnotations(ci.getMetadata(), expectedValues);
    log.info(topicAnnoNum + " TopicAnnotations found ...");
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) HashMap(java.util.HashMap) EngineException(org.apache.stanbol.enhancer.servicesapi.EngineException) JenaSerializerProvider(org.apache.clerezza.rdf.jena.serializer.JenaSerializerProvider) RDFTerm(org.apache.clerezza.commons.rdf.RDFTerm) ContentItem(org.apache.stanbol.enhancer.servicesapi.ContentItem) Test(org.junit.Test)

Example 5 with JenaSerializerProvider

use of org.apache.clerezza.rdf.jena.serializer.JenaSerializerProvider in project stanbol by apache.

the class OWLOntologyWriter method writeTo.

@Override
public void writeTo(OWLOntology ontology, Class<?> arg1, Type arg2, Annotation[] arg3, MediaType mediaType, MultivaluedMap<String, Object> arg5, OutputStream out) throws IOException, WebApplicationException {
    Logger log = LoggerFactory.getLogger(getClass());
    OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
    log.debug("Rendering ontology " + ontology.getOntologyID() + " to knowledge representation format " + mediaType);
    // Native formats first
    if (RDF_XML_TYPE.equals(mediaType) || OWL_XML_TYPE.equals(mediaType) || MANCHESTER_OWL_TYPE.equals(mediaType) || FUNCTIONAL_OWL_TYPE.equals(mediaType) || TURTLE_TYPE.equals(mediaType) || X_TURTLE_TYPE.equals(mediaType)) {
        OWLOntologyFormat format = null;
        if (RDF_XML_TYPE.equals(mediaType))
            format = new RDFXMLOntologyFormat();
        else if (OWL_XML_TYPE.equals(mediaType))
            format = new OWLXMLOntologyFormat();
        else if (MANCHESTER_OWL_TYPE.equals(mediaType))
            format = new ManchesterOWLSyntaxOntologyFormat();
        else if (FUNCTIONAL_OWL_TYPE.equals(mediaType))
            format = new OWLFunctionalSyntaxOntologyFormat();
        else if (TURTLE_TYPE.equals(mediaType) || X_TURTLE_TYPE.equals(mediaType))
            format = new TurtleOntologyFormat();
        if (format != null)
            try {
                manager.saveOntology(ontology, format, out);
            } catch (OWLOntologyStorageException e) {
                log.error("Failed to store ontology for rendering.", e);
                throw new WebApplicationException(e, Status.INTERNAL_SERVER_ERROR);
            }
        else
            throw new IOException();
    } else {
        // Non-native formats that require a conversion to Clerezza
        if (RDF_JSON_TYPE.equals(mediaType) || N3_TYPE.equals(mediaType) || TEXT_PLAIN.equals(mediaType.toString()) || N_TRIPLE_TYPE.equals(mediaType)) {
            Graph mGraph = OWLAPIToClerezzaConverter.owlOntologyToClerezzaGraph(ontology);
            SerializingProvider serializer = null;
            if (RDF_JSON_TYPE.equals(mediaType))
                serializer = new RdfJsonSerializingProvider();
            else if (N3_TYPE.equals(mediaType) || N_TRIPLE_TYPE.equals(mediaType) || TEXT_PLAIN.equals(mediaType.toString()))
                serializer = new JenaSerializerProvider();
            // text/plain is interpreted as N3.
            if (serializer != null)
                serializer.serialize(out, mGraph, TEXT_PLAIN.equals(mediaType.toString()) ? N3 : mediaType.toString());
        }
    }
    // JSON_LD not supported until both parser and serializer are stable.
    out.flush();
}
Also used : ManchesterOWLSyntaxOntologyFormat(org.coode.owlapi.manchesterowlsyntax.ManchesterOWLSyntaxOntologyFormat) TurtleOntologyFormat(org.coode.owlapi.turtle.TurtleOntologyFormat) WebApplicationException(javax.ws.rs.WebApplicationException) OWLFunctionalSyntaxOntologyFormat(org.semanticweb.owlapi.io.OWLFunctionalSyntaxOntologyFormat) JenaSerializerProvider(org.apache.clerezza.rdf.jena.serializer.JenaSerializerProvider) RDFXMLOntologyFormat(org.semanticweb.owlapi.io.RDFXMLOntologyFormat) IOException(java.io.IOException) Logger(org.slf4j.Logger) RdfJsonSerializingProvider(org.apache.clerezza.rdf.rdfjson.serializer.RdfJsonSerializingProvider) SerializingProvider(org.apache.clerezza.rdf.core.serializedform.SerializingProvider) Graph(org.apache.clerezza.commons.rdf.Graph) RdfJsonSerializingProvider(org.apache.clerezza.rdf.rdfjson.serializer.RdfJsonSerializingProvider) OWLXMLOntologyFormat(org.semanticweb.owlapi.io.OWLXMLOntologyFormat) OWLOntologyFormat(org.semanticweb.owlapi.model.OWLOntologyFormat) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager) OWLOntologyStorageException(org.semanticweb.owlapi.model.OWLOntologyStorageException)

Aggregations

JenaSerializerProvider (org.apache.clerezza.rdf.jena.serializer.JenaSerializerProvider)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 SerializingProvider (org.apache.clerezza.rdf.core.serializedform.SerializingProvider)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 Model (com.hp.hpl.jena.rdf.model.Model)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 Graph (org.apache.clerezza.commons.rdf.Graph)1 IRI (org.apache.clerezza.commons.rdf.IRI)1 RDFTerm (org.apache.clerezza.commons.rdf.RDFTerm)1 RdfJsonSerializingProvider (org.apache.clerezza.rdf.rdfjson.serializer.RdfJsonSerializingProvider)1 ContentItem (org.apache.stanbol.enhancer.servicesapi.ContentItem)1 EngineException (org.apache.stanbol.enhancer.servicesapi.EngineException)1 ManchesterOWLSyntaxOntologyFormat (org.coode.owlapi.manchesterowlsyntax.ManchesterOWLSyntaxOntologyFormat)1 TurtleOntologyFormat (org.coode.owlapi.turtle.TurtleOntologyFormat)1 Test (org.junit.Test)1 OWLFunctionalSyntaxOntologyFormat (org.semanticweb.owlapi.io.OWLFunctionalSyntaxOntologyFormat)1 OWLXMLOntologyFormat (org.semanticweb.owlapi.io.OWLXMLOntologyFormat)1 RDFXMLOntologyFormat (org.semanticweb.owlapi.io.RDFXMLOntologyFormat)1