From 184af4cfec8ba44e1900e2235af5d77010c1a1bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Szeremeta?= Date: Tue, 6 Apr 2021 02:26:53 +0200 Subject: [PATCH] JSON-LD output fix --- .../java/pl/edu/uwb/ii/sdfeater/File.java | 7 +++- .../java/pl/edu/uwb/ii/sdfeater/Molecule.java | 34 +++++++++---------- .../pl/edu/uwb/ii/sdfeater/OutputTest.java | 4 +-- 3 files changed, 24 insertions(+), 21 deletions(-) diff --git a/src/main/java/pl/edu/uwb/ii/sdfeater/File.java b/src/main/java/pl/edu/uwb/ii/sdfeater/File.java index c08b837..fd276c7 100644 --- a/src/main/java/pl/edu/uwb/ii/sdfeater/File.java +++ b/src/main/java/pl/edu/uwb/ii/sdfeater/File.java @@ -63,6 +63,7 @@ class File { * @param subject Subject type */ void parse(Molecule molecule, SDFEater.Format format, SDFEater.Subject subject) { + StringBuilder output_str = new StringBuilder(); try { FileInputStream fstream = new FileInputStream(filename); BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); @@ -250,7 +251,7 @@ void parse(Molecule molecule, SDFEater.Format format, SDFEater.Subject subject) break; case jsonldhtml: case jsonld: - molecule.printJSONLDMolecule(subject); + output_str.append(molecule.constructJSONLDMolecule(subject)); break; case rdfa: molecule.printRDFaMolecule(subject); @@ -387,6 +388,8 @@ void parse(Molecule molecule, SDFEater.Format format, SDFEater.Subject subject) jenaModel.write(System.out, "NTRIPLES"); break; case jsonld: + output_str.setLength(output_str.length() - 2); + System.out.println(output_str); System.out.println(" ],\n" + " \"@context\" : {\n" + " \"identifier\" : {\n" + @@ -440,6 +443,8 @@ void parse(Molecule molecule, SDFEater.Format format, SDFEater.Subject subject) break; // JSON-LD with HTML case jsonldhtml: + output_str.setLength(output_str.length() - 2); + System.out.println(output_str); System.out.println(" ],\n" + " \"@context\" : {\n" + " \"identifier\" : {\n" + diff --git a/src/main/java/pl/edu/uwb/ii/sdfeater/Molecule.java b/src/main/java/pl/edu/uwb/ii/sdfeater/Molecule.java index f7e5cd7..ea2e8f2 100644 --- a/src/main/java/pl/edu/uwb/ii/sdfeater/Molecule.java +++ b/src/main/java/pl/edu/uwb/ii/sdfeater/Molecule.java @@ -391,13 +391,24 @@ void addToJenaModel(SDFEater.Subject subject) { } /** - * Print main molecule data in JSON-LD + * Construct molecule data in JSON-LD * * @param subject subject type + * @return Molecule data in JSON-LD */ - void printJSONLDMolecule(SDFEater.Subject subject) { + StringBuilder constructJSONLDMolecule(SDFEater.Subject subject) { StringBuilder output_str = new StringBuilder(); + output_str.append(" {\n"); + if (subject == SDFEater.Subject.iri) { + output_str.append(" \"@id\" : \"https://example.com/molecule#entity" + createID() + "\",\n"); + } else if (subject == SDFEater.Subject.uuid) { + output_str.append(" \"@id\" : \"urn:uuid:" + uuid + "\",\n"); + } else if (subject == SDFEater.Subject.bnode) { + output_str.append(" \"@id\" : \"_:b" + createID() + "\",\n"); + } + output_str.append(" \"@type\" : \"https://schema.org/MolecularEntity\",\n"); + for (Map.Entry> entry : properties.entrySet()) { String key = entry.getKey(); List values = entry.getValue(); @@ -435,23 +446,10 @@ void printJSONLDMolecule(SDFEater.Subject subject) { } } - if (output_str.length() > 0) { - output_str.setLength(output_str.length() - 2); - System.out.println(" {"); - if (subject == SDFEater.Subject.iri) { - System.out.println(" \"@id\" : \"https://example.com/molecule#entity" + createID() + "\","); - } else if (subject == SDFEater.Subject.uuid) { - System.out.println(" \"@id\" : \"urn:uuid:" + uuid + "\","); - } else if (subject == SDFEater.Subject.bnode) { - System.out.println(" \"@id\" : \"_:b" + createID() + "\","); - } - System.out.println(" \"@type\" : \"https://schema.org/MolecularEntity\","); - - System.out.print(output_str); - System.out.println(); - System.out.println(" },"); - } + output_str.setLength(output_str.length() - 2); + output_str.append("\n },\n"); + return output_str; } /** diff --git a/src/test/java/pl/edu/uwb/ii/sdfeater/OutputTest.java b/src/test/java/pl/edu/uwb/ii/sdfeater/OutputTest.java index 1328fa7..9e6f215 100644 --- a/src/test/java/pl/edu/uwb/ii/sdfeater/OutputTest.java +++ b/src/test/java/pl/edu/uwb/ii/sdfeater/OutputTest.java @@ -20,7 +20,7 @@ class OutputTest { private final ByteArrayOutputStream outputStreamCaptor = new ByteArrayOutputStream(); private final String[] testMoleculeDataFields = {"alternateName", "description", "identifier", "inChI", "inChIKey", "iupacName", "molecularFormula", "molecularWeight", "smiles"}; - private final String[] testMoleculeData = {"(+)-2-fenchanone", "A fenchone that has", "4695-62-9", "InChI=1S/C10H16O/c1-9(2)7-4-5-10(3,6-7)8(9)11/h7H,4-6H2,1-3H3/t7-,10+/m1/s1", "LHXDLQBQYFFVNW-XCBNKYQSSA-N", "(1S,4R)-fenchan-2-one", "C10H16O", "152.23340", "CC1(C)[C@@H]2CC[C@@](C)(C2)C1=O", "(-)-Epicatechin", "A catechin with", "490-46-0", "InChI=1S/C15H14O6/c16-8-4-11(18)9-6-13(20)15(21-14(9)5-8)7-1-2-10(17)12(19)3-7/h1-5,13,15-20H,6H2/t13-,15-/m1/s1", "PFTAWBLQPZVEMU-UKRRQHHQSA-N", "(2R,3R)-2-(3,4-dihydroxyphenyl)-3,4-dihydro-2H-chromene-3,5,7-triol", "C15H14O6", "290.26810", "[H][C@@]1(Oc2cc(O)cc(O)c2C[C@H]1O)c1ccc(O)c(O)c1"}; + private final String[] testMoleculeData = {"(+)-2-fenchanone", "A fenchone that has", "4695-62-9", "InChI", "1S/C10H16O/c1-9(2)7-4-5-10(3,6-7)8(9)11/h7H,4-6H2,1-3H3/t7-,10+/m1/s1", "LHXDLQBQYFFVNW-XCBNKYQSSA-N", "(1S,4R)-fenchan-2-one", "C10H16O", "152.23340", "CC1(C)[C@@H]2CC[C@@](C)(C2)C1", "(-)-Epicatechin", "A catechin with", "490-46-0", "1S/C15H14O6/c16-8-4-11(18)9-6-13(20)15(21-14(9)5-8)7-1-2-10(17)12(19)3-7/h1-5,13,15-20H,6H2/t13-,15-/m1/s1", "PFTAWBLQPZVEMU-UKRRQHHQSA-N", "(2R,3R)-2-(3,4-dihydroxyphenyl)-3,4-dihydro-2H-chromene-3,5,7-triol", "C15H14O6", "290.26810", "[H][C@@]1(Oc2cc(O)cc(O)c2C[C@H]1O)c1ccc(O)c(O)c1"}; private final File file = new File(Paths.get("src", "test", "resources", "chebi_test.sdf").toFile().getAbsolutePath()); private Molecule molecule; @@ -577,7 +577,7 @@ void jsonldHaveUUIDSubject() { void jsonldHaveBNodeSubject() { file.parse(molecule, SDFEater.Format.jsonld, SDFEater.Subject.bnode); String out = outputStreamCaptor.toString(); - String[] required = {"_:b0"}; + String[] required = {"_:b"}; assertTrue(stringContainsAllValues(out, required)); }