/*
* Licensed to the Technische Universität Darmstadt under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The Technische Universität Darmstadt
* licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License.
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.dkpro.core.io.rdf;
import java.io.OutputStream;
import org.apache.jena.ontology.OntModel;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.riot.RDFDataMgr;
import org.apache.jena.riot.RDFLanguages;
import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
import org.apache.uima.cas.CASException;
import org.apache.uima.fit.descriptor.ConfigurationParameter;
import org.apache.uima.fit.descriptor.MimeTypeCapability;
import org.apache.uima.jcas.JCas;
import org.dkpro.core.io.rdf.internal.Uima2Rdf;
import de.tudarmstadt.ukp.dkpro.core.api.io.JCasFileWriter_ImplBase;
import de.tudarmstadt.ukp.dkpro.core.api.parameter.ComponentParameters;
import de.tudarmstadt.ukp.dkpro.core.api.parameter.MimeTypes;
/**
* Writes the CAS out as RDF.
*/
@MimeTypeCapability({MimeTypes.APPLICATION_X_UIMA_RDF})
public class RdfWriter
extends JCasFileWriter_ImplBase
{
/**
* Specify the suffix of output files. Default value <code>.ttl</code>. The file format will be
* chosen depending on the file suffice.
*
* @see RDFLanguages
*/
public static final String PARAM_FILENAME_EXTENSION = ComponentParameters.PARAM_FILENAME_EXTENSION;
@ConfigurationParameter(name = PARAM_FILENAME_EXTENSION, mandatory = true, defaultValue = ".ttl")
private String filenameSuffix;
@Override
public void process(JCas aJCas)
throws AnalysisEngineProcessException
{
OntModel model = ModelFactory.createOntologyModel();
try {
Uima2Rdf.convert(aJCas, model);
}
catch (CASException e) {
throw new AnalysisEngineProcessException(e);
}
try (OutputStream docOS = getOutputStream(aJCas, filenameSuffix)) {
RDFDataMgr.write(docOS, model.getBaseModel(),
RDFLanguages.fileExtToLang(filenameSuffix));
}
catch (Exception e) {
throw new AnalysisEngineProcessException(e);
}
}
}