Skip to content

Commit

Permalink
Add support for JSON and YAML files, closes #20
Browse files Browse the repository at this point in the history
- add dependency on jena-arq
    - used to convert JSON-LD to RDF
    - this will also be used for SPARQL
- add dependency on snakeyaml
- update IOHelper.loadOntology(File, boolean) to
    - detect and convert YAML to JSON
    - detect and convert JSON to RDF
- add simple.json translation of simple.owl
- add simple.yaml translation of simple.owl
- update unit tests
  • Loading branch information
jamesaoverton committed May 12, 2015
1 parent 924a9dd commit cc1666e
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 6 deletions.
10 changes: 10 additions & 0 deletions robot-core/pom.xml
Expand Up @@ -12,10 +12,20 @@
<description>Core library for ROBOT, the OBO tool</description>

<dependencies>
<dependency>
<groupId>org.apache.jena</groupId>
<artifactId>jena-arq</artifactId>
<version>2.13.0</version>
</dependency>
<dependency>
<groupId>com.github.jsonld-java</groupId>
<artifactId>jsonld-java</artifactId>
<version>0.5.1</version>
</dependency>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.15</version>
</dependency>
</dependencies>
</project>
49 changes: 43 additions & 6 deletions robot-core/src/main/java/org/obolibrary/robot/IOHelper.java
Expand Up @@ -2,9 +2,10 @@

import java.io.InputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
Expand All @@ -17,11 +18,14 @@

import com.github.jsonldjava.core.Context;
import com.github.jsonldjava.core.JsonLdApi;
import com.github.jsonldjava.core.JsonLdError;
import com.github.jsonldjava.core.JsonLdOptions;
import com.github.jsonldjava.core.JsonLdProcessor;
import com.github.jsonldjava.utils.JsonUtils;
import org.apache.commons.io.FilenameUtils;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.model.IRI;
Expand All @@ -36,6 +40,7 @@
import org.semanticweb.owlapi.model.PrefixManager;
import org.semanticweb.owlapi.util.DefaultPrefixManager;
import org.semanticweb.owlapi.vocab.PrefixOWLOntologyFormat;
import org.yaml.snakeyaml.Yaml;

/**
* Provides convenience methods for working with ontology and term files.
Expand Down Expand Up @@ -236,17 +241,49 @@ public OWLOntology loadOntology(File ontologyFile, File catalogFile)
logger.debug("Loading ontology {} with catalog file {}",
ontologyFile, catalogFile);

OWLOntology ontology = null;
Object jsonObject = null;

try {
OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
String extension =
FilenameUtils.getExtension(ontologyFile.getName());
extension = extension.trim().toLowerCase();
if (extension.equals("yml") || extension.equals("yaml")) {
logger.debug("Converting from YAML to JSON");
String yamlString = FileUtils.readFileToString(ontologyFile);
jsonObject = new Yaml().load(yamlString);
} else if (extension.equals("js")
|| extension.equals("json")
|| extension.equals("jsonld")) {
String jsonString = FileUtils.readFileToString(ontologyFile);
jsonObject = JsonUtils.fromString(jsonString);
}

// Use Jena to convert a JSON-LD string to RDFXML, then load it
if (jsonObject != null) {
logger.debug("Converting from JSON to RDF");
jsonObject = new JsonLdApi().expand(getContext(), jsonObject);
String jsonString = JsonUtils.toString(jsonObject);
Model model = ModelFactory.createDefaultModel();
model.read(IOUtils.toInputStream(jsonString), null, "JSON-LD");
ByteArrayOutputStream output = new ByteArrayOutputStream();
//model.write(System.out);
model.write(output);
byte[] data = output.toByteArray();
ByteArrayInputStream input = new ByteArrayInputStream(data);
return loadOntology(input);
}

OWLOntologyManager manager =
OWLManager.createOWLOntologyManager();
if (catalogFile != null && catalogFile.isFile()) {
manager.addIRIMapper(new CatalogXmlIRIMapper(catalogFile));
}
ontology = manager.loadOntologyFromOntologyDocument(ontologyFile);
return manager.loadOntologyFromOntologyDocument(ontologyFile);
} catch (JsonLdError e) {
throw new IOException(e);
} catch (OWLOntologyCreationException e) {
throw new IOException(e);
}
return ontology;
}

/**
Expand Down
27 changes: 27 additions & 0 deletions robot-core/src/test/java/org/obolibrary/robot/IOHelperTest.java
@@ -1,5 +1,6 @@
package org.obolibrary.robot;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
Expand All @@ -18,6 +19,32 @@
* Tests for {@link IOHelper}.
*/
public class IOHelperTest extends CoreTest {
/**
* Test loading JSON files.
*
* @throws IOException on file problem
*/
@Test
public void testJSON() throws IOException {
IOHelper ioh = new IOHelper();
String jsonPath = this.getClass().getResource("/simple.json").getFile();
File jsonFile = new File(jsonPath);
assertIdentical("/simple.owl", ioh.loadOntology(jsonFile));
}

/**
* Test loading YAML files.
*
* @throws IOException on file problem
*/
@Test
public void testYAML() throws IOException {
IOHelper ioh = new IOHelper();
String yamlPath = this.getClass().getResource("/simple.yaml").getFile();
File yamlFile = new File(yamlPath);
assertIdentical("/simple.owl", ioh.loadOntology(yamlFile));
}

/**
* Test getting the default context.
*
Expand Down
31 changes: 31 additions & 0 deletions robot-core/src/test/resources/simple.json
@@ -0,0 +1,31 @@
{
"@context": {
"resources": "https://github.com/ontodev/robot/robot-core/src/test/resources/",
"simple": "resources:simple.owl#",
"label": "rdfs:label",
"parent": {
"@id": "rdfs:subClassOf",
"@type": "@id"
}
},

"@graph": [
{
"@id": "resources:simple.owl",
"@type": "owl:Ontology"
},
{
"@id": "simple:test1",
"@type": "owl:Class",
"label": [
{"@value": "test one"},
{"@value": "Test 1"}
]
},
{
"@id": "simple:test2",
"@type": "owl:Class",
"parent": "simple:test1"
}
]
}
20 changes: 20 additions & 0 deletions robot-core/src/test/resources/simple.yaml
@@ -0,0 +1,20 @@
---
"@context":
resources: https://github.com/ontodev/robot/robot-core/src/test/resources/
simple: resources:simple.owl#
label: rdfs:label
parent:
"@id": rdfs:subClassOf
"@type": "@id"

"@graph":
- "@id": resources:simple.owl
"@type": owl:Ontology
- "@id": simple:test1
"@type": owl:Class
label:
- "@value": test one
- "@value": Test 1
- "@id": simple:test2
"@type": owl:Class
parent: simple:test1

0 comments on commit cc1666e

Please sign in to comment.