Permalink
Browse files

Added reader and writer for the CDKOWL/N3 format

  • Loading branch information...
1 parent 0fbe0e0 commit 0e6795165f14292a4819d645a763de3588343004 @egonw egonw committed Oct 5, 2009
View
@@ -21,5 +21,11 @@
<classpathentry kind="lib" path="develjar/ojdcheck.jar"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry exported="true" kind="lib" path="jar/signatures-1.0.jar"/>
+ <classpathentry kind="lib" path="jar/jena/jena.jar"/>
+ <classpathentry kind="lib" path="jar/jena/slf4j-simple-1.5.0.jar"/>
+ <classpathentry kind="lib" path="jar/jena/commons-logging-1.1.1.jar"/>
+ <classpathentry kind="lib" path="jar/jena/slf4j-api-1.5.6.jar"/>
+ <classpathentry kind="lib" path="jar/jena/iri.jar"/>
+ <classpathentry kind="lib" path="jar/jena/icu4j_3_4.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
@@ -0,0 +1,111 @@
+/* Copyright (C) 2009 Egon Willighagen <egonw@users.sf.net>
+ *
+ * Contact: cdk-devel@lists.sourceforge.net
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 2.1
+ * of the License, or (at your option) any later version.
+ * All we ask is that proper credit is given for our work, which includes
+ * - but is not limited to - adding the above copyright notice to the beginning
+ * of your source code files, and to any copyright notice that you may distribute
+ * with programs based on this work.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+package org.openscience.cdk.io.rdf;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
+
+import org.openscience.cdk.annotations.TestClass;
+import org.openscience.cdk.annotations.TestMethod;
+import org.openscience.cdk.exception.CDKException;
+import org.openscience.cdk.interfaces.IChemObject;
+import org.openscience.cdk.interfaces.IMolecule;
+import org.openscience.cdk.io.DefaultChemObjectReader;
+import org.openscience.cdk.io.formats.CDKOWLFormat;
+import org.openscience.cdk.io.formats.IResourceFormat;
+
+/**
+ * Reads content from a CDK OWL serialization.
+ *
+ * @cdk.module iordf
+ * @cdk.githash
+ * @cdk.keyword file format, OWL
+ */
+@TestClass("org.openscience.cdk.io.rdf.CDKOWLReaderTest")
+public class CDKOWLReader extends DefaultChemObjectReader {
+
+ private Reader input;
+
+ public CDKOWLReader(Reader input) {
+ this.input = input;
+ }
+
+ public CDKOWLReader() {
+ this.input = null;
+ }
+
+ @TestMethod("testGetFormat")
+ public IResourceFormat getFormat() {
+ return CDKOWLFormat.getInstance();
+ }
+
+ /**
+ * This method must not be used; XML reading requires the use of an
+ * {@link InputStream}. Use {@link #setReader(InputStream)} instead.
+ */
+ @TestMethod("testSetReader_Reader")
+ public void setReader(Reader reader) throws CDKException {
+ this.input = reader;
+ }
+
+ @TestMethod("testSetReader_InputStream")
+ public void setReader(InputStream input) throws CDKException {
+ this.input = new InputStreamReader(input);
+ }
+
+ @TestMethod("testAccepts")
+ public boolean accepts(Class classObject) {
+ Class[] interfaces = classObject.getInterfaces();
+ for (int i=0; i<interfaces.length; i++) {
+ if (IMolecule.class.equals(interfaces[i])) return true;
+ }
+ Class superClass = classObject.getSuperclass();
+ if (superClass != null) return this.accepts(superClass);
+ return false;
+ }
+
+ public IChemObject read(IChemObject object) throws CDKException {
+ if (object instanceof IMolecule) {
+ return readMolecule((IMolecule)object);
+ } else {
+ throw new CDKException(
+ "Only supported is reading of IMolecule objects."
+ );
+ }
+ }
+
+ // private functions
+
+ private IMolecule readMolecule(IMolecule mol) throws CDKException {
+ return mol;
+ }
+
+ @TestMethod("testClose")
+ public void close() throws IOException {
+ input.close();
+ }
+
+}
+
@@ -0,0 +1,103 @@
+/* Copyright (C) 2009 Egon Willighagen <egonw@users.sf.net>
+ *
+ * Contact: cdk-devel@lists.sourceforge.net
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+package org.openscience.cdk.io.rdf;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.Writer;
+
+import org.openscience.cdk.annotations.TestMethod;
+import org.openscience.cdk.exception.CDKException;
+import org.openscience.cdk.interfaces.IChemObject;
+import org.openscience.cdk.interfaces.IMolecule;
+import org.openscience.cdk.io.DefaultChemObjectWriter;
+import org.openscience.cdk.io.formats.CDKOWLFormat;
+import org.openscience.cdk.io.formats.IResourceFormat;
+import org.openscience.cdk.libio.jena.Convertor;
+
+import com.hp.hpl.jena.rdf.model.Model;
+
+/**
+ * Serializes the data model into CDK OWL.
+ *
+ * @cdk.module iordf
+ * @cdk.githash
+ */
+public class CDKOWLWriter extends DefaultChemObjectWriter {
+
+ private Writer output;
+
+ public CDKOWLWriter(Writer output) {
+ this.output = output;
+ }
+
+ public CDKOWLWriter() {
+ this.output = null;
+ }
+
+ public IResourceFormat getFormat() {
+ return CDKOWLFormat.getInstance();
+ }
+
+ public void setWriter(Writer out) throws CDKException {
+ this.output = out;
+ }
+
+ public void setWriter(OutputStream output) throws CDKException {
+ this.output = new OutputStreamWriter(output);
+ }
+
+ public void close() throws IOException {
+ if (output != null) output.close();
+ }
+
+ @TestMethod("testAccepts")
+ public boolean accepts(Class classObject) {
+ Class[] interfaces = classObject.getInterfaces();
+ for (int i = 0; i < interfaces.length; i++) {
+ if (IMolecule.class.equals(interfaces[i])) return true;
+ }
+ Class superClass = classObject.getSuperclass();
+ if (superClass != null) return this.accepts(superClass);
+ return false;
+ }
+
+ public void write(IChemObject object) throws CDKException {
+ if (object instanceof IMolecule) {
+ try {
+ writeMolecule((IMolecule)object);
+ } catch (Exception ex) {
+ throw new CDKException(
+ "Error while writing HIN file: " + ex.getMessage(), ex
+ );
+ }
+ } else {
+ throw new CDKException(
+ "CDKOWLWriter only supports output of IMolecule classes."
+ );
+ }
+ }
+
+ private void writeMolecule(IMolecule mol) {
+ Model model = Convertor.molecule2Model(mol);
+ model.write(output, "N3");
+ }
+
+}
@@ -0,0 +1,9 @@
+@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
+@prefix cdk: <http://cdk.sourceforge.net/model.owl#> .
+@prefix owl: <http://www.w3.org/2002/07/owl#> .
+@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
+@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
+
+<http://cdk.sf.net/model/molecule/1>
+ a <cdk:Molecule> .
+
@@ -0,0 +1,66 @@
+/* Copyright (C) 2009 The Chemistry Development Kit (CDK) project
+ *
+ * Contact: cdk-devel@slists.sourceforge.net
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 2.1
+ * of the License, or (at your option) any later version.
+ * All we ask is that proper credit is given for our work, which includes
+ * - but is not limited to - adding the above copyright notice to the beginning
+ * of your source code files, and to any copyright notice that you may distribute
+ * with programs based on this work.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+package org.openscience.cdk.io.rdf;
+
+import java.io.InputStream;
+import java.io.InputStreamReader;
+
+import org.junit.Assert;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.openscience.cdk.Molecule;
+import org.openscience.cdk.interfaces.IMolecule;
+import org.openscience.cdk.io.SimpleChemObjectReaderTest;
+import org.openscience.cdk.nonotify.NNMolecule;
+import org.openscience.cdk.tools.ILoggingTool;
+import org.openscience.cdk.tools.LoggingToolFactory;
+
+/**
+ * @cdk.module test-iordf
+ */
+public class CDKOWLReaderTest extends SimpleChemObjectReaderTest {
+
+ private ILoggingTool logger =
+ LoggingToolFactory.createLoggingTool(CDKOWLReaderTest.class);
+
+ @BeforeClass public static void setup() {
+ setSimpleChemObjectReader(new CDKOWLReader(), "data/owl/molecule.n3");
+ }
+
+ @Test public void testAccepts() {
+ Assert.assertTrue(chemObjectIO.accepts(Molecule.class));
+ }
+
+ @Test public void testMolecule() throws Exception {
+ String filename = "data/owl/molecule.n3";
+ logger.info("Testing: " + filename);
+ InputStream ins = this.getClass().getClassLoader().getResourceAsStream(filename);
+ CDKOWLReader reader = new CDKOWLReader(
+ new InputStreamReader(ins)
+ );
+ IMolecule mol = (IMolecule)reader.read(new NNMolecule());
+
+ Assert.assertNotNull(mol);
+ }
+
+}
@@ -0,0 +1,57 @@
+/* Copyright (C) 2009 Egon Willighagen <egonw@users.sf.net>
+ *
+ * Contact: cdk-devel@slists.sourceforge.net
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 2.1
+ * of the License, or (at your option) any later version.
+ * All we ask is that proper credit is given for our work, which includes
+ * - but is not limited to - adding the above copyright notice to the beginning
+ * of your source code files, and to any copyright notice that you may distribute
+ * with programs based on this work.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+package org.openscience.cdk.io.rdf;
+
+import java.io.StringWriter;
+
+import junit.framework.Assert;
+
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.openscience.cdk.interfaces.IMolecule;
+import org.openscience.cdk.io.ChemObjectWriterTest;
+import org.openscience.cdk.nonotify.NNMolecule;
+
+/**
+ * TestCase for the {@link CDKOWLWriter}.
+ *
+ * @cdk.module test-iordf
+ */
+public class CDKOWLWriterTest extends ChemObjectWriterTest {
+
+ @BeforeClass public static void setup() {
+ setChemObjectWriter(new CDKOWLWriter());
+ }
+
+ @Test public void testWriteMolecule() throws Exception {
+ StringWriter output = new StringWriter();
+ CDKOWLWriter writer = new CDKOWLWriter(output);
+
+ IMolecule mol = new NNMolecule();
+ writer.write(mol);
+ String outputString = output.toString();
+ Assert.assertTrue(outputString.contains(
+ "http://cdk.sourceforge.net/model.owl#"
+ ));
+ }
+}
Oops, something went wrong.

0 comments on commit 0e67951

Please sign in to comment.