Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add classes context file #116

Merged
merged 2 commits into from
Jul 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion docs/features.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ For each of these paths, OBA generates the SPARQL queries that are necessary to

### Generating JSON-LD contexts

OBA converts the format of the responses from JSON/LD to plain JSON, which is a widely format used by Web developers. In order to achieve this, OBA requires a file with the context of the ontologies used. The context is used to map simple terms to IRIs. OBA generates these context file automatically.
OBA converts the format of the responses from JSON/LD to plain JSON, which is a widely format used by Web developers. In order to achieve this, OBA requires two files with the context of the ontologies used. The context is used to map simple terms to IRIs. OBA generates these context file automatically.

!!! note
Since OBA 3.3.0, OBA uses two files `context.json` and `context_class.json`. The first file contains all the mappings (classes and properties) and the second file contains the classes mapping. OBAsparql uses the `context.json` for `POST` and `PUT` method and uses the `context_class.json` for `GET` method.

!!! info
OBA reuses and extends [owl2jsonld](https://github.com/stain/owl2jsonld), developed by [Stain Soiland-Reyes](https://github.com/stain).
Expand Down
4 changes: 2 additions & 2 deletions examples/modelcatalog/config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
ontologies:
- https://w3id.org/okn/o/sdm/1.5.0
- https://w3id.org/okn/o/sd/1.6.0
- https://w3id.org/okn/o/sdm/1.6.0-nm
- https://w3id.org/okn/o/sd/1.7.0-nm
name: modelcatalog
output_dir: outputs
openapi:
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/edu/isi/oba/Oba.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,18 @@ public static void main(String[] args) throws Exception {
private static void generate_context(YamlConfig config_data, String destination_dir) {
List<String> ontologies = config_data.getOntologies();
JSONObject context_json_object = null;
JSONObject context_json_object_class = null;
try {
context_json_object = ObaUtils.generate_context_file(ontologies.toArray(new String[0]));
context_json_object = ObaUtils.generate_context_file(ontologies.toArray(new String[0]), false);
context_json_object_class = ObaUtils.generate_context_file(ontologies.toArray(new String[0]), true);
} catch (Exception e) {
e.printStackTrace();
}
String file_path = destination_dir + File.separator + "servers" + File.separator + "context.json";
String file_path_class = destination_dir + File.separator + "servers" + File.separator + "context_class.json";

ObaUtils.write_file(file_path, context_json_object.toString(4));
ObaUtils.write_file(file_path_class, context_json_object_class.toString(4));
}

private static void copy_custom_queries(String source, String destination){
Expand Down
20 changes: 15 additions & 5 deletions src/main/java/edu/isi/oba/ObaUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -226,11 +226,11 @@ public static JSONObject mergeJSONObjects(JSONObject json1, JSONObject json2) {
return mergedJSON;
}

public static JSONObject generate_context_file(String[] ontologies) throws Exception {
public static JSONObject generate_context_file(String[] ontologies, Boolean only_classes) throws Exception {
JSONObject[] jsons = new JSONObject[ontologies.length];
for (int i = 0; i < ontologies.length; i++) {
try {
jsons[i] = run_owl_jsonld(ontologies[i]);
jsons[i] = run_owl_jsonld(ontologies[i], only_classes);
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
Expand All @@ -242,18 +242,28 @@ public static JSONObject generate_context_file(String[] ontologies) throws Excep

/**
* Method to execute OWL2JSONLD
* @param ontology_file file path of the ontology to load
* @param ontology_file file path of the ontology to load
* @param only_classes
* @return JSON object with the context of the ontology
* @throws Exception
*/
private static JSONObject run_owl_jsonld(String ontology_file) throws Exception {
private static JSONObject run_owl_jsonld(String ontology_file, Boolean only_classes) throws Exception {
ontology_file = new File(ontology_file).toURI().toString();
String owl2jsonld = "/owl2jsonld-0.3.0-SNAPSHOT-standalone.jar";
InputStream owl2_jar = ObaUtils.class.getResourceAsStream(owl2jsonld);
File tempFile = File.createTempFile("oba", "jar");
copy(owl2_jar,tempFile);
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(new String[]{"java", "-jar", tempFile.getPath(), ontology_file});
String[] cmdarray = new String[0];

if (only_classes) {
cmdarray = new String[]{"java", "-jar", tempFile.getPath(), "-c", ontology_file};
}
else {
cmdarray = new String[]{"java", "-jar", tempFile.getPath(), ontology_file};

}
Process proc = rt.exec(cmdarray);

BufferedReader stdInput = new BufferedReader(new
InputStreamReader(proc.getInputStream()));
Expand Down
Binary file modified src/main/resources/servers.zip
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pythonql3==0.9.61
connexion >= 2.6.0
obasparql >= 3.3.3
obasparql >= 3.4.1
werkzeug==0.16.1
swagger-ui-bundle >= 0.0.2
python_dateutil >= 2.6.0
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/servers/python/generate-server.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ mkdir -p ${PWD}/$SERVER_DIR/contexts/
echo "Copying query files"
cp -r ../../queries ${PWD}/$SERVER_DIR/queries
cp -r ../context.json ${PWD}/$SERVER_DIR/contexts/
cp -r ../context_class.json ${PWD}/$SERVER_DIR/contexts/
if [ "$?" == "0" ]; then
echo -e "${GREEN}SUCCESS${RESET}"
fi
fi
7 changes: 2 additions & 5 deletions src/test/java/edu/isi/oba/ObaUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,9 @@
import java.io.IOException;

import static org.junit.Assert.*;
import org.semanticweb.owlapi.model.IRI;

import org.semanticweb.owlapi.model.OWLClass;
import org.semanticweb.owlapi.model.OWLEntity;
import org.semanticweb.owlapi.model.OWLOntologyCreationException;
import org.semanticweb.owlapi.search.EntitySearcher;
import uk.ac.manchester.cs.owl.owlapi.OWLClassImpl;

public class ObaUtilsTest {

Expand Down Expand Up @@ -68,7 +65,7 @@ public void run() {
String[] ontologies = new String[]{"ontology1", "ontology2"};
JSONObject context = null;
try {
context = ObaUtils.generate_context_file(ontologies);
context = ObaUtils.generate_context_file(ontologies, false);
} catch (Exception e) {
e.printStackTrace();
}
Expand Down