From f96aaf331ec34f3b76485204663afc681308e270 Mon Sep 17 00:00:00 2001 From: simonoakesepimorphics <37299842+simonoakesepimorphics@users.noreply.github.com> Date: Thu, 26 Aug 2021 15:30:44 +0100 Subject: [PATCH] Test spec (#51) * Set theme jekyll-theme-slate * Create index.md Initialise GH pages * Add variable references * Add test framework * Infer references from all var to var relationships * Fix deps * Set compiler plugin version * Add binary-array-api module containing core interfaces * Add API module dependency * Remove unused imports * Move ModelAliasDefinition class into model package * Add NetCDF LD converter class * Refactor CLI and spec test to use converter impl * Refactor test framework to use converter interface * Update docs and demos * Fix test doc * Fix advanced usage doc * Update root identity resource to match spec * Fix test * Update tests to current versions * Fix mistake in test spec * Fix media types in test graphs * Fix integer node types * Add format and distribution to graph output * Fix tests and remove unused uri property on binary array interface * Add format and distribution to graph Remove unused URI property on binary array interface * Remove unused imports * Fix output format test * update tests * Change converter interface to use URIs to locate resources * Optimise imports * Update docs * Add base vocab * Add base vocab to alias definition * Exclude bald:isPrefixedBy from output graph * Update docs * Rename test files * Fix tests * Add factory method and update docs * Add binary-array-ld-api javadoc links --- binary-array-ld-api/pom.xml | 36 +++++ .../src/main/kotlin/net/bald/Attribute.kt | 0 .../main/kotlin/net/bald/AttributeSource.kt | 0 .../src/main/kotlin/net/bald/BinaryArray.kt | 0 .../src/main/kotlin/net/bald/Container.kt | 0 .../src/main/kotlin/net/bald/Converter.kt | 27 ++++ .../main/kotlin/net/bald/CoordinateRange.kt | 0 .../src/main/kotlin/net/bald/Dimension.kt | 0 .../src/main/kotlin/net/bald/Distribution.kt | 0 .../src/main/kotlin/net/bald/Format.kt | 0 .../src/main/kotlin/net/bald/Var.kt | 0 .../kotlin/net/bald/alias/AliasDefinition.kt | 0 .../kotlin/net/bald/context/ModelContext.kt | 0 .../src/main/kotlin/net/bald/vocab/BALD.kt | 0 binary-array-ld-cli/pom.xml | 10 ++ .../kotlin/net/bald/BinaryArrayConvertCli.kt | 38 ++--- .../net/bald/BinaryArrayConvertSpecTest.kt | 8 ++ .../main/java/net/bald/NetCdfConvertJava.java | 40 +++--- binary-array-ld-lib/pom.xml | 12 ++ .../{alias => model}/ModelAliasDefinition.kt | 3 +- .../net/bald/model/ModelBinaryArrayBuilder.kt | 4 - .../bald/model/ModelBinaryArrayConverter.kt | 1 - .../net/bald/model/ModelReferenceBuilder.kt | 9 +- .../kotlin/net/bald/model/ModelVarBuilder.kt | 10 +- .../bald/model/ModelAliasDefinitionTest.kt | 2 - .../bald/model/ModelAttributeBuilderTest.kt | 6 +- .../bald/model/ModelBinaryArrayBuilderTest.kt | 1 - .../model/ModelBinaryArrayConverterTest.kt | 2 +- .../net/bald/model/ModelVarBuilderTest.kt | 18 +-- binary-array-ld-netcdf/pom.xml | 3 + .../kotlin/net/bald/netcdf/NetCdfContainer.kt | 1 - .../net/bald/netcdf/NetCdfCoordinateRange.kt | 5 +- .../net/bald/netcdf/NetCdfLdConverter.kt | 50 +++++++ .../net/bald/netcdf/NetCdfRootContainer.kt | 4 +- .../main/kotlin/net/bald/netcdf/NetCdfVar.kt | 2 +- .../net/bald/netcdf/NetCdfBinaryArrayTest.kt | 2 +- .../bald/netcdf/ReferenceValueParserTest.kt | 2 +- .../kotlin/net/bald/netcdf/UriParserTest.kt | 2 +- binary-array-ld-test/pom.xml | 32 +++++ .../kotlin/bald/file/ResourceFileConverter.kt | 24 ++++ .../kotlin/bald/json/JsonSpecRequirement.kt | 66 +++++++++ .../src/main/kotlin/bald/json/JsonTestSpec.kt | 14 ++ .../src/main/kotlin/bald/json/YamlUtil.kt | 9 ++ .../main/kotlin/bald/model/ModelVerifier.kt | 2 - .../bald/model/ResourceModelConverter.kt | 12 ++ .../kotlin/bald/model/StatementsVerifier.kt | 3 +- .../src/main/kotlin/bald/spec/BaseSpecTest.kt | 53 +++++++ .../main/kotlin/bald/spec/SpecRequirement.kt | 32 +++++ .../kotlin/bald/spec/TestScenarioProvider.kt | 27 ++++ .../src/main/kotlin/bald/spec/TestSpec.kt | 12 ++ .../src/main/resources/spec/spec.yaml | 38 +++++ .../resources/spec/test/a-identity/input.cdl | 10 ++ .../resources/spec/test/a-identity/output.ttl | 21 +++ .../resources/spec/test/b-prefixing/input.cdl | 16 +++ .../spec/test/b-prefixing/output.ttl | 23 +++ .../resources/spec/test/c-aliasing/input.cdl | 10 ++ .../resources/spec/test/c-aliasing/output.ttl | 23 +++ .../spec/test/d-attributes/input.cdl | 17 +++ .../spec/test/d-attributes/output.ttl | 25 ++++ .../spec/test/ef-variables/input.cdl | 39 +++++ .../spec/test/ef-variables/output.ttl | 85 +++++++++++ .../src/main/resources/turtle/netcdf.ttl | 134 ++++++++++++++++++ docs/alias.md | 4 +- docs/context.md | 4 +- docs/download.md | 5 +- docs/index.md | 1 + docs/lib.md | 48 ++++++- docs/testing.md | 76 ++++++++++ pom.xml | 5 +- 69 files changed, 1076 insertions(+), 92 deletions(-) create mode 100644 binary-array-ld-api/pom.xml rename {binary-array-ld-lib => binary-array-ld-api}/src/main/kotlin/net/bald/Attribute.kt (100%) rename {binary-array-ld-lib => binary-array-ld-api}/src/main/kotlin/net/bald/AttributeSource.kt (100%) rename {binary-array-ld-lib => binary-array-ld-api}/src/main/kotlin/net/bald/BinaryArray.kt (100%) rename {binary-array-ld-lib => binary-array-ld-api}/src/main/kotlin/net/bald/Container.kt (100%) create mode 100644 binary-array-ld-api/src/main/kotlin/net/bald/Converter.kt rename {binary-array-ld-lib => binary-array-ld-api}/src/main/kotlin/net/bald/CoordinateRange.kt (100%) rename {binary-array-ld-lib => binary-array-ld-api}/src/main/kotlin/net/bald/Dimension.kt (100%) rename {binary-array-ld-lib => binary-array-ld-api}/src/main/kotlin/net/bald/Distribution.kt (100%) rename {binary-array-ld-lib => binary-array-ld-api}/src/main/kotlin/net/bald/Format.kt (100%) rename {binary-array-ld-lib => binary-array-ld-api}/src/main/kotlin/net/bald/Var.kt (100%) rename {binary-array-ld-lib => binary-array-ld-api}/src/main/kotlin/net/bald/alias/AliasDefinition.kt (100%) rename {binary-array-ld-lib => binary-array-ld-api}/src/main/kotlin/net/bald/context/ModelContext.kt (100%) rename {binary-array-ld-lib => binary-array-ld-api}/src/main/kotlin/net/bald/vocab/BALD.kt (100%) create mode 100644 binary-array-ld-cli/src/test/kotlin/net/bald/BinaryArrayConvertSpecTest.kt rename binary-array-ld-lib/src/main/kotlin/net/bald/{alias => model}/ModelAliasDefinition.kt (98%) create mode 100644 binary-array-ld-netcdf/src/main/kotlin/net/bald/netcdf/NetCdfLdConverter.kt create mode 100644 binary-array-ld-test/src/main/kotlin/bald/file/ResourceFileConverter.kt create mode 100644 binary-array-ld-test/src/main/kotlin/bald/json/JsonSpecRequirement.kt create mode 100644 binary-array-ld-test/src/main/kotlin/bald/json/JsonTestSpec.kt create mode 100644 binary-array-ld-test/src/main/kotlin/bald/json/YamlUtil.kt create mode 100644 binary-array-ld-test/src/main/kotlin/bald/model/ResourceModelConverter.kt create mode 100644 binary-array-ld-test/src/main/kotlin/bald/spec/BaseSpecTest.kt create mode 100644 binary-array-ld-test/src/main/kotlin/bald/spec/SpecRequirement.kt create mode 100644 binary-array-ld-test/src/main/kotlin/bald/spec/TestScenarioProvider.kt create mode 100644 binary-array-ld-test/src/main/kotlin/bald/spec/TestSpec.kt create mode 100644 binary-array-ld-test/src/main/resources/spec/spec.yaml create mode 100644 binary-array-ld-test/src/main/resources/spec/test/a-identity/input.cdl create mode 100644 binary-array-ld-test/src/main/resources/spec/test/a-identity/output.ttl create mode 100644 binary-array-ld-test/src/main/resources/spec/test/b-prefixing/input.cdl create mode 100644 binary-array-ld-test/src/main/resources/spec/test/b-prefixing/output.ttl create mode 100644 binary-array-ld-test/src/main/resources/spec/test/c-aliasing/input.cdl create mode 100644 binary-array-ld-test/src/main/resources/spec/test/c-aliasing/output.ttl create mode 100644 binary-array-ld-test/src/main/resources/spec/test/d-attributes/input.cdl create mode 100644 binary-array-ld-test/src/main/resources/spec/test/d-attributes/output.ttl create mode 100644 binary-array-ld-test/src/main/resources/spec/test/ef-variables/input.cdl create mode 100644 binary-array-ld-test/src/main/resources/spec/test/ef-variables/output.ttl create mode 100644 binary-array-ld-test/src/main/resources/turtle/netcdf.ttl create mode 100644 docs/testing.md diff --git a/binary-array-ld-api/pom.xml b/binary-array-ld-api/pom.xml new file mode 100644 index 0000000..b63c2c5 --- /dev/null +++ b/binary-array-ld-api/pom.xml @@ -0,0 +1,36 @@ + + + + binary-array-ld + net.binary-array-ld + 1.0.0-SNAPSHOT + + 4.0.0 + + binary-array-ld-api + jar + + Binary Array Linked Data - API + API for Binary Array LD functionality. + + + + + org.apache.jena + jena-core + ${jena.version} + + + + + + + org.jetbrains.dokka + dokka-maven-plugin + + + + + \ No newline at end of file diff --git a/binary-array-ld-lib/src/main/kotlin/net/bald/Attribute.kt b/binary-array-ld-api/src/main/kotlin/net/bald/Attribute.kt similarity index 100% rename from binary-array-ld-lib/src/main/kotlin/net/bald/Attribute.kt rename to binary-array-ld-api/src/main/kotlin/net/bald/Attribute.kt diff --git a/binary-array-ld-lib/src/main/kotlin/net/bald/AttributeSource.kt b/binary-array-ld-api/src/main/kotlin/net/bald/AttributeSource.kt similarity index 100% rename from binary-array-ld-lib/src/main/kotlin/net/bald/AttributeSource.kt rename to binary-array-ld-api/src/main/kotlin/net/bald/AttributeSource.kt diff --git a/binary-array-ld-lib/src/main/kotlin/net/bald/BinaryArray.kt b/binary-array-ld-api/src/main/kotlin/net/bald/BinaryArray.kt similarity index 100% rename from binary-array-ld-lib/src/main/kotlin/net/bald/BinaryArray.kt rename to binary-array-ld-api/src/main/kotlin/net/bald/BinaryArray.kt diff --git a/binary-array-ld-lib/src/main/kotlin/net/bald/Container.kt b/binary-array-ld-api/src/main/kotlin/net/bald/Container.kt similarity index 100% rename from binary-array-ld-lib/src/main/kotlin/net/bald/Container.kt rename to binary-array-ld-api/src/main/kotlin/net/bald/Container.kt diff --git a/binary-array-ld-api/src/main/kotlin/net/bald/Converter.kt b/binary-array-ld-api/src/main/kotlin/net/bald/Converter.kt new file mode 100644 index 0000000..53aae6d --- /dev/null +++ b/binary-array-ld-api/src/main/kotlin/net/bald/Converter.kt @@ -0,0 +1,27 @@ +package net.bald + +import org.apache.jena.rdf.model.Model +import java.net.URI + +/** + * A binary array to linked data converter. + */ +interface Converter { + /** + * Convert a binary array file to a linked data graph. + * @param input The location of the NetCDF binary array file to convert. + * @param uri The URI that identifies the binary array. + * @param contexts The location of files containing JSON-LD contexts. + * @param aliases The location of files containing alias definitions. + * @param downloadUrl The URL from which the NetCDF file can be downloaded, if one exists. Otherwise, null. + * @return The linked data graph. + */ + fun convert( + input: URI, + uri: String? = null, + contexts: List? = null, + aliases: List? = null, + downloadUrl: String? = null + ): Model +} + diff --git a/binary-array-ld-lib/src/main/kotlin/net/bald/CoordinateRange.kt b/binary-array-ld-api/src/main/kotlin/net/bald/CoordinateRange.kt similarity index 100% rename from binary-array-ld-lib/src/main/kotlin/net/bald/CoordinateRange.kt rename to binary-array-ld-api/src/main/kotlin/net/bald/CoordinateRange.kt diff --git a/binary-array-ld-lib/src/main/kotlin/net/bald/Dimension.kt b/binary-array-ld-api/src/main/kotlin/net/bald/Dimension.kt similarity index 100% rename from binary-array-ld-lib/src/main/kotlin/net/bald/Dimension.kt rename to binary-array-ld-api/src/main/kotlin/net/bald/Dimension.kt diff --git a/binary-array-ld-lib/src/main/kotlin/net/bald/Distribution.kt b/binary-array-ld-api/src/main/kotlin/net/bald/Distribution.kt similarity index 100% rename from binary-array-ld-lib/src/main/kotlin/net/bald/Distribution.kt rename to binary-array-ld-api/src/main/kotlin/net/bald/Distribution.kt diff --git a/binary-array-ld-lib/src/main/kotlin/net/bald/Format.kt b/binary-array-ld-api/src/main/kotlin/net/bald/Format.kt similarity index 100% rename from binary-array-ld-lib/src/main/kotlin/net/bald/Format.kt rename to binary-array-ld-api/src/main/kotlin/net/bald/Format.kt diff --git a/binary-array-ld-lib/src/main/kotlin/net/bald/Var.kt b/binary-array-ld-api/src/main/kotlin/net/bald/Var.kt similarity index 100% rename from binary-array-ld-lib/src/main/kotlin/net/bald/Var.kt rename to binary-array-ld-api/src/main/kotlin/net/bald/Var.kt diff --git a/binary-array-ld-lib/src/main/kotlin/net/bald/alias/AliasDefinition.kt b/binary-array-ld-api/src/main/kotlin/net/bald/alias/AliasDefinition.kt similarity index 100% rename from binary-array-ld-lib/src/main/kotlin/net/bald/alias/AliasDefinition.kt rename to binary-array-ld-api/src/main/kotlin/net/bald/alias/AliasDefinition.kt diff --git a/binary-array-ld-lib/src/main/kotlin/net/bald/context/ModelContext.kt b/binary-array-ld-api/src/main/kotlin/net/bald/context/ModelContext.kt similarity index 100% rename from binary-array-ld-lib/src/main/kotlin/net/bald/context/ModelContext.kt rename to binary-array-ld-api/src/main/kotlin/net/bald/context/ModelContext.kt diff --git a/binary-array-ld-lib/src/main/kotlin/net/bald/vocab/BALD.kt b/binary-array-ld-api/src/main/kotlin/net/bald/vocab/BALD.kt similarity index 100% rename from binary-array-ld-lib/src/main/kotlin/net/bald/vocab/BALD.kt rename to binary-array-ld-api/src/main/kotlin/net/bald/vocab/BALD.kt diff --git a/binary-array-ld-cli/pom.xml b/binary-array-ld-cli/pom.xml index c731e8a..d4a206e 100644 --- a/binary-array-ld-cli/pom.xml +++ b/binary-array-ld-cli/pom.xml @@ -27,6 +27,16 @@ org.apache.jena jena-arq ${jena.version} + + + jackson-core + com.fasterxml.jackson.core + + + jackson-databind + com.fasterxml.jackson.core + + net.binary-array-ld diff --git a/binary-array-ld-cli/src/main/kotlin/net/bald/BinaryArrayConvertCli.kt b/binary-array-ld-cli/src/main/kotlin/net/bald/BinaryArrayConvertCli.kt index 24d219e..ff14bd8 100644 --- a/binary-array-ld-cli/src/main/kotlin/net/bald/BinaryArrayConvertCli.kt +++ b/binary-array-ld-cli/src/main/kotlin/net/bald/BinaryArrayConvertCli.kt @@ -1,15 +1,13 @@ package net.bald -import net.bald.alias.AliasDefinition -import net.bald.context.ModelContext -import net.bald.alias.ModelAliasDefinition -import net.bald.model.ModelBinaryArrayConverter -import net.bald.netcdf.NetCdfBinaryArray +import net.bald.netcdf.NetCdfLdConverter import org.apache.commons.cli.DefaultParser import org.apache.commons.cli.HelpFormatter import org.apache.commons.cli.Options -import org.apache.jena.rdf.model.ModelFactory -import java.io.* +import java.io.File +import java.io.FilterOutputStream +import java.io.OutputStream +import java.net.URI import kotlin.system.exitProcess /** @@ -40,11 +38,13 @@ class BinaryArrayConvertCli { } private fun doRun(opts: CommandLineOptions) { - val context = context(opts.contextLocs) - val alias = alias(opts.aliasLocs) - val inputLoc = opts.inputLoc ?: throw IllegalArgumentException("First argument is required: NetCDF file to convert.") - val ba = NetCdfBinaryArray.create(inputLoc, opts.uri, context, alias, opts.downloadUrl) - val model = ba.use(ModelBinaryArrayConverter::convert) + val input = opts.inputLoc?.let(::URI) ?: throw IllegalArgumentException("First argument is required: NetCDF file to convert.") + val uri = opts.uri + val context = opts.contextLocs.map(::URI) + val alias = opts.aliasLocs.map(::URI) + val downloadUrl = opts.downloadUrl + + val model = NetCdfLdConverter.getInstance().convert(input, uri, context, alias, downloadUrl) val outputFormat = opts.outputFormat ?: "ttl" modelOutput(opts.outputLoc).use { output -> @@ -52,20 +52,6 @@ class BinaryArrayConvertCli { } } - private fun context(contextLocs: List): ModelContext { - val prefixes = contextLocs.map { contextLoc -> - ModelFactory.createDefaultModel().read(contextLoc, "json-ld") - } - - return ModelContext.create(prefixes) - } - - private fun alias(aliasLocs: List): AliasDefinition { - return ModelFactory.createDefaultModel().apply { - aliasLocs.forEach(::read) - }.let(ModelAliasDefinition::create) - } - private fun options(opts: Options, vararg args: String): CommandLineOptions { return DefaultParser().parse(opts, args).let(::CommandLineOptions) } diff --git a/binary-array-ld-cli/src/test/kotlin/net/bald/BinaryArrayConvertSpecTest.kt b/binary-array-ld-cli/src/test/kotlin/net/bald/BinaryArrayConvertSpecTest.kt new file mode 100644 index 0000000..6d3a5e7 --- /dev/null +++ b/binary-array-ld-cli/src/test/kotlin/net/bald/BinaryArrayConvertSpecTest.kt @@ -0,0 +1,8 @@ +package net.bald + +import bald.spec.BaseSpecTest +import net.bald.netcdf.NetCdfLdConverter + +class BinaryArrayConvertSpecTest: BaseSpecTest() { + override val converter: Converter = NetCdfLdConverter.getInstance() +} \ No newline at end of file diff --git a/binary-array-ld-demo/src/main/java/net/bald/NetCdfConvertJava.java b/binary-array-ld-demo/src/main/java/net/bald/NetCdfConvertJava.java index 17501a5..4c8d69a 100644 --- a/binary-array-ld-demo/src/main/java/net/bald/NetCdfConvertJava.java +++ b/binary-array-ld-demo/src/main/java/net/bald/NetCdfConvertJava.java @@ -1,23 +1,21 @@ package net.bald; -import net.bald.alias.AliasDefinition; -import net.bald.context.ModelContext; -import net.bald.alias.ModelAliasDefinition; -import net.bald.model.ModelBinaryArrayConverter; -import net.bald.netcdf.NetCdfBinaryArray; +import net.bald.netcdf.NetCdfLdConverter; import org.apache.jena.rdf.model.Model; -import org.apache.jena.rdf.model.ModelFactory; -import org.apache.jena.shared.PrefixMapping; + +import java.io.File; import java.io.FileOutputStream; import java.io.OutputStream; +import java.net.URI; +import java.util.Arrays; /** * Demonstration of how to call the API in Java code. */ public class NetCdfConvertJava { public static void convert() throws Exception { - BinaryArray ba = NetCdfBinaryArray.create("/path/to/input.nc", "http://test.binary-array-ld.net/example"); - Model model = ModelBinaryArrayConverter.convert(ba); + URI input = new File("/path/to/input.nc").toURI(); + Model model = NetCdfLdConverter.Companion.getInstance().convert(input, "http://test.binary-array-ld.net/example", null, null, null); try (OutputStream output = new FileOutputStream("/path/to/output.ttl")) { model.write(output, "ttl"); @@ -25,10 +23,9 @@ public static void convert() throws Exception { } public static void convertWithExternalPrefixes() throws Exception { - PrefixMapping prefix = ModelFactory.createDefaultModel().read("/path/to/context.json", "json-ld"); - ModelContext context = ModelContext.create(prefix); - BinaryArray ba = NetCdfBinaryArray.create("/path/to/input.nc", "http://test.binary-array-ld.net/example", context, null, null); - Model model = ModelBinaryArrayConverter.convert(ba); + URI input = new File("/path/to/input.nc").toURI(); + URI context = new File("/path/to/context.json").toURI(); + Model model = NetCdfLdConverter.Companion.getInstance().convert(input, "http://test.binary-array-ld.net/example", Arrays.asList(context), null, null); try (OutputStream output = new FileOutputStream("/path/to/output.ttl")) { model.write(output, "ttl"); @@ -36,10 +33,19 @@ public static void convertWithExternalPrefixes() throws Exception { } public static void convertWithAliases() throws Exception { - Model aliasModel = ModelFactory.createDefaultModel().read("/path/to/alias.ttl", "ttl"); - AliasDefinition alias = ModelAliasDefinition.create(aliasModel); - BinaryArray ba = NetCdfBinaryArray.create("/path/to/input.nc", "http://test.binary-array-ld.net/example", null, alias, null); - Model model = ModelBinaryArrayConverter.convert(ba); + URI input = new File("/path/to/input.nc").toURI(); + URI alias = new File("/path/to/alias.ttl").toURI(); + Model model = NetCdfLdConverter.Companion.getInstance().convert(input, "http://test.binary-array-ld.net/example", null, Arrays.asList(alias), null); + + try (OutputStream output = new FileOutputStream("/path/to/output.ttl")) { + model.write(output, "ttl"); + } + } + + public static void convertWithDownloadUrl() throws Exception { + String downloadUrl = "http://test.binary-array-ld.net/download/netcdf.nc"; + URI input = new File("/path/to/input.nc").toURI(); + Model model = NetCdfLdConverter.Companion.getInstance().convert(input, "http://test.binary-array-ld.net/example", null, null, downloadUrl); try (OutputStream output = new FileOutputStream("/path/to/output.ttl")) { model.write(output, "ttl"); diff --git a/binary-array-ld-lib/pom.xml b/binary-array-ld-lib/pom.xml index 7f853ee..03edd51 100644 --- a/binary-array-ld-lib/pom.xml +++ b/binary-array-ld-lib/pom.xml @@ -18,6 +18,11 @@ + + net.binary-array-ld + binary-array-ld-api + ${project.parent.version} + org.apache.jena jena-core @@ -47,6 +52,13 @@ org.jetbrains.dokka dokka-maven-plugin + + + + http://kotlin.binary-array-ld.net/javadoc/binary-array-ld-api + + + diff --git a/binary-array-ld-lib/src/main/kotlin/net/bald/alias/ModelAliasDefinition.kt b/binary-array-ld-lib/src/main/kotlin/net/bald/model/ModelAliasDefinition.kt similarity index 98% rename from binary-array-ld-lib/src/main/kotlin/net/bald/alias/ModelAliasDefinition.kt rename to binary-array-ld-lib/src/main/kotlin/net/bald/model/ModelAliasDefinition.kt index 1c440b0..9a76480 100644 --- a/binary-array-ld-lib/src/main/kotlin/net/bald/alias/ModelAliasDefinition.kt +++ b/binary-array-ld-lib/src/main/kotlin/net/bald/model/ModelAliasDefinition.kt @@ -1,5 +1,6 @@ -package net.bald.alias +package net.bald.model +import net.bald.alias.AliasDefinition import net.bald.vocab.BALD import org.apache.jena.rdf.model.* import org.apache.jena.riot.RDFLanguages diff --git a/binary-array-ld-lib/src/main/kotlin/net/bald/model/ModelBinaryArrayBuilder.kt b/binary-array-ld-lib/src/main/kotlin/net/bald/model/ModelBinaryArrayBuilder.kt index 88c5262..eb6593d 100644 --- a/binary-array-ld-lib/src/main/kotlin/net/bald/model/ModelBinaryArrayBuilder.kt +++ b/binary-array-ld-lib/src/main/kotlin/net/bald/model/ModelBinaryArrayBuilder.kt @@ -1,12 +1,8 @@ package net.bald.model import net.bald.BinaryArray -import net.bald.vocab.BALD -import org.apache.jena.rdf.model.Literal import org.apache.jena.rdf.model.Model import org.apache.jena.rdf.model.Resource -import org.apache.jena.rdf.model.ResourceFactory.createResource -import org.apache.jena.rdf.model.ResourceFactory.createStringLiteral import org.apache.jena.shared.PrefixMapping import org.apache.jena.vocabulary.DCAT import org.apache.jena.vocabulary.DCTerms diff --git a/binary-array-ld-lib/src/main/kotlin/net/bald/model/ModelBinaryArrayConverter.kt b/binary-array-ld-lib/src/main/kotlin/net/bald/model/ModelBinaryArrayConverter.kt index 8f324ed..1864049 100644 --- a/binary-array-ld-lib/src/main/kotlin/net/bald/model/ModelBinaryArrayConverter.kt +++ b/binary-array-ld-lib/src/main/kotlin/net/bald/model/ModelBinaryArrayConverter.kt @@ -3,7 +3,6 @@ package net.bald.model import net.bald.BinaryArray import org.apache.jena.rdf.model.Model import org.apache.jena.rdf.model.ModelFactory -import org.apache.jena.shared.PrefixMapping /** * API for converting a [BinaryArray] to a linked data [Model]. diff --git a/binary-array-ld-lib/src/main/kotlin/net/bald/model/ModelReferenceBuilder.kt b/binary-array-ld-lib/src/main/kotlin/net/bald/model/ModelReferenceBuilder.kt index eed47be..1fe5777 100644 --- a/binary-array-ld-lib/src/main/kotlin/net/bald/model/ModelReferenceBuilder.kt +++ b/binary-array-ld-lib/src/main/kotlin/net/bald/model/ModelReferenceBuilder.kt @@ -3,9 +3,10 @@ package net.bald.model import net.bald.Dimension import net.bald.Var import net.bald.vocab.BALD +import org.apache.jena.datatypes.xsd.XSDDatatype import org.apache.jena.rdf.model.Literal import org.apache.jena.rdf.model.Resource -import org.apache.jena.rdf.model.ResourceFactory +import org.apache.jena.rdf.model.ResourceFactory.createTypedLiteral import org.apache.jena.vocabulary.RDF class ModelReferenceBuilder( @@ -40,13 +41,15 @@ class ModelReferenceBuilder( fun shape(dimIds: List): Iterator { return dimIds.asSequence().map { dimId -> - dimsById[dimId]?.size?.let(ResourceFactory::createTypedLiteral) ?: unitNode + dimsById[dimId]?.size?.let { size -> + createTypedLiteral(size.toString(), XSDDatatype.XSDinteger) + } ?: unitNode }.iterator() } } companion object { - private val unitNode = ResourceFactory.createTypedLiteral(1) + private val unitNode = createTypedLiteral("1", XSDDatatype.XSDinteger) } open class Factory { diff --git a/binary-array-ld-lib/src/main/kotlin/net/bald/model/ModelVarBuilder.kt b/binary-array-ld-lib/src/main/kotlin/net/bald/model/ModelVarBuilder.kt index 34f647b..668a28b 100644 --- a/binary-array-ld-lib/src/main/kotlin/net/bald/model/ModelVarBuilder.kt +++ b/binary-array-ld-lib/src/main/kotlin/net/bald/model/ModelVarBuilder.kt @@ -4,7 +4,9 @@ import net.bald.AttributeSource import net.bald.Dimension import net.bald.Var import net.bald.vocab.BALD -import org.apache.jena.rdf.model.* +import org.apache.jena.datatypes.xsd.XSDDatatype +import org.apache.jena.rdf.model.RDFNode +import org.apache.jena.rdf.model.Resource import org.apache.jena.rdf.model.ResourceFactory.createTypedLiteral open class ModelVarBuilder( @@ -24,9 +26,7 @@ open class ModelVarBuilder( private fun addAttributes(source: AttributeSource, resource: Resource) { val builder = attrFct.forResource(resource) - source.attributes().filterNot { attr -> - BALD.references.hasURI(attr.uri) - }.forEach(builder::addAttribute) + source.attributes().forEach(builder::addAttribute) } private fun addCoordinateRange(v: Var, resource: Resource) { @@ -79,7 +79,7 @@ open class ModelVarBuilder( } private fun size(dim: Dimension): RDFNode { - return createTypedLiteral(dim.size) + return createTypedLiteral(dim.size.toString(), XSDDatatype.XSDinteger) } override fun addReferences(resource: Resource) { diff --git a/binary-array-ld-lib/src/test/kotlin/net/bald/model/ModelAliasDefinitionTest.kt b/binary-array-ld-lib/src/test/kotlin/net/bald/model/ModelAliasDefinitionTest.kt index 151f193..68322ff 100644 --- a/binary-array-ld-lib/src/test/kotlin/net/bald/model/ModelAliasDefinitionTest.kt +++ b/binary-array-ld-lib/src/test/kotlin/net/bald/model/ModelAliasDefinitionTest.kt @@ -1,6 +1,5 @@ package net.bald.model -import net.bald.alias.ModelAliasDefinition import net.bald.vocab.BALD import org.apache.jena.rdf.model.ModelFactory import org.apache.jena.rdf.model.ResourceFactory.createProperty @@ -9,7 +8,6 @@ import org.apache.jena.vocabulary.RDFS import org.apache.jena.vocabulary.SKOS import org.junit.jupiter.api.Test import org.junit.jupiter.api.assertThrows -import java.lang.IllegalStateException import kotlin.test.assertEquals import kotlin.test.assertFalse import kotlin.test.assertNull diff --git a/binary-array-ld-lib/src/test/kotlin/net/bald/model/ModelAttributeBuilderTest.kt b/binary-array-ld-lib/src/test/kotlin/net/bald/model/ModelAttributeBuilderTest.kt index fa383b9..ef11e48 100644 --- a/binary-array-ld-lib/src/test/kotlin/net/bald/model/ModelAttributeBuilderTest.kt +++ b/binary-array-ld-lib/src/test/kotlin/net/bald/model/ModelAttributeBuilderTest.kt @@ -6,10 +6,10 @@ import com.nhaarman.mockitokotlin2.mock import com.nhaarman.mockitokotlin2.stub import net.bald.Attribute import org.apache.jena.rdf.model.ModelFactory -import org.apache.jena.rdf.model.ResourceFactory.* -import org.apache.jena.vocabulary.RDF +import org.apache.jena.rdf.model.ResourceFactory.createPlainLiteral +import org.apache.jena.rdf.model.ResourceFactory.createResource import org.apache.jena.vocabulary.RDFS -import org.junit.jupiter.api.* +import org.junit.jupiter.api.Test class ModelAttributeBuilderTest { private val uri = "http://test.binary-array-ld.net/example/var0" diff --git a/binary-array-ld-lib/src/test/kotlin/net/bald/model/ModelBinaryArrayBuilderTest.kt b/binary-array-ld-lib/src/test/kotlin/net/bald/model/ModelBinaryArrayBuilderTest.kt index d5e2072..7a4d47b 100644 --- a/binary-array-ld-lib/src/test/kotlin/net/bald/model/ModelBinaryArrayBuilderTest.kt +++ b/binary-array-ld-lib/src/test/kotlin/net/bald/model/ModelBinaryArrayBuilderTest.kt @@ -15,7 +15,6 @@ import org.apache.jena.shared.PrefixMapping import org.apache.jena.vocabulary.SKOS import org.junit.jupiter.api.Test import org.junit.jupiter.api.assertThrows -import java.lang.IllegalArgumentException import kotlin.test.assertEquals class ModelBinaryArrayBuilderTest { diff --git a/binary-array-ld-lib/src/test/kotlin/net/bald/model/ModelBinaryArrayConverterTest.kt b/binary-array-ld-lib/src/test/kotlin/net/bald/model/ModelBinaryArrayConverterTest.kt index 11d5442..4e06537 100644 --- a/binary-array-ld-lib/src/test/kotlin/net/bald/model/ModelBinaryArrayConverterTest.kt +++ b/binary-array-ld-lib/src/test/kotlin/net/bald/model/ModelBinaryArrayConverterTest.kt @@ -11,7 +11,7 @@ import org.apache.jena.shared.PrefixMapping import org.apache.jena.vocabulary.DCTerms import org.apache.jena.vocabulary.RDF import org.apache.jena.vocabulary.SKOS -import org.junit.jupiter.api.* +import org.junit.jupiter.api.Test /** * Test the full Binary Array -> Linked Data conversion process using a mock binary array. diff --git a/binary-array-ld-lib/src/test/kotlin/net/bald/model/ModelVarBuilderTest.kt b/binary-array-ld-lib/src/test/kotlin/net/bald/model/ModelVarBuilderTest.kt index eeb081d..345067d 100644 --- a/binary-array-ld-lib/src/test/kotlin/net/bald/model/ModelVarBuilderTest.kt +++ b/binary-array-ld-lib/src/test/kotlin/net/bald/model/ModelVarBuilderTest.kt @@ -17,7 +17,7 @@ import org.apache.jena.rdf.model.ResourceFactory.createResource import org.apache.jena.rdf.model.ResourceFactory.createTypedLiteral import org.apache.jena.vocabulary.RDF import org.apache.jena.vocabulary.RDFS -import org.junit.jupiter.api.* +import org.junit.jupiter.api.Test class ModelVarBuilderTest { private val model = ModelFactory.createDefaultModel() @@ -117,9 +117,9 @@ class ModelVarBuilderTest { statement(RDF.type, BALD.Array) statement(BALD.shape) { list( - createTypedLiteral(10), - createTypedLiteral(30), - createTypedLiteral(1000) + createTypedLiteral("10", XSDDatatype.XSDinteger), + createTypedLiteral("30", XSDDatatype.XSDinteger), + createTypedLiteral("1000", XSDDatatype.XSDinteger) ) } } @@ -200,25 +200,25 @@ class ModelVarBuilderTest { statement(BALD.references) { statement(RDF.type, BALD.Reference) statement(BALD.sourceRefShape) { - list(createTypedLiteral(10), createTypedLiteral(90)) + list(createTypedLiteral("10", XSDDatatype.XSDinteger), createTypedLiteral("90", XSDDatatype.XSDinteger)) } statement(BALD.target, createResource("http://test.binary-array-ld.net/example/bar")) statement(BALD.targetRefShape) { - list(createTypedLiteral(10), createTypedLiteral(1)) + list(createTypedLiteral("10", XSDDatatype.XSDinteger), createTypedLiteral("1", XSDDatatype.XSDinteger)) } } statement(BALD.references) { statement(RDF.type, BALD.Reference) statement(BALD.sourceRefShape) { - list(createTypedLiteral(10), createTypedLiteral(90)) + list(createTypedLiteral("10", XSDDatatype.XSDinteger), createTypedLiteral("90", XSDDatatype.XSDinteger)) } statement(BALD.target, createResource("http://test.binary-array-ld.net/example/baz")) statement(BALD.targetRefShape) { - list(createTypedLiteral(1), createTypedLiteral(90)) + list(createTypedLiteral("1", XSDDatatype.XSDinteger), createTypedLiteral("90", XSDDatatype.XSDinteger)) } } statement(BALD.shape) { - list(createTypedLiteral(10), createTypedLiteral(90)) + list(createTypedLiteral("10", XSDDatatype.XSDinteger), createTypedLiteral("90", XSDDatatype.XSDinteger)) } } } diff --git a/binary-array-ld-netcdf/pom.xml b/binary-array-ld-netcdf/pom.xml index da7ca91..fcd58bd 100644 --- a/binary-array-ld-netcdf/pom.xml +++ b/binary-array-ld-netcdf/pom.xml @@ -53,6 +53,9 @@ http://kotlin.binary-array-ld.net/javadoc/binary-array-ld-lib + + http://kotlin.binary-array-ld.net/javadoc/binary-array-ld-api + diff --git a/binary-array-ld-netcdf/src/main/kotlin/net/bald/netcdf/NetCdfContainer.kt b/binary-array-ld-netcdf/src/main/kotlin/net/bald/netcdf/NetCdfContainer.kt index 13efe35..4d0e8ef 100644 --- a/binary-array-ld-netcdf/src/main/kotlin/net/bald/netcdf/NetCdfContainer.kt +++ b/binary-array-ld-netcdf/src/main/kotlin/net/bald/netcdf/NetCdfContainer.kt @@ -2,7 +2,6 @@ package net.bald.netcdf import net.bald.Attribute import net.bald.Container -import net.bald.Var import net.bald.alias.AliasDefinition import org.apache.jena.rdf.model.Property import org.apache.jena.rdf.model.RDFNode diff --git a/binary-array-ld-netcdf/src/main/kotlin/net/bald/netcdf/NetCdfCoordinateRange.kt b/binary-array-ld-netcdf/src/main/kotlin/net/bald/netcdf/NetCdfCoordinateRange.kt index 12c8adf..e893f3d 100644 --- a/binary-array-ld-netcdf/src/main/kotlin/net/bald/netcdf/NetCdfCoordinateRange.kt +++ b/binary-array-ld-netcdf/src/main/kotlin/net/bald/netcdf/NetCdfCoordinateRange.kt @@ -14,6 +14,9 @@ class NetCdfCoordinateRange( override val last: Any? get() = v.read().flip(0).let(::firstElement) private fun firstElement(array: Array): Any? { - return array.takeIf(Array::hasNext)?.next() + return array.takeIf(Array::hasNext)?.next()?.takeUnless { value -> + // For some reason empty array values are rendered as minimum-valued integers, so ignore them. + value == (Int.MIN_VALUE + 1) + } } } \ No newline at end of file diff --git a/binary-array-ld-netcdf/src/main/kotlin/net/bald/netcdf/NetCdfLdConverter.kt b/binary-array-ld-netcdf/src/main/kotlin/net/bald/netcdf/NetCdfLdConverter.kt new file mode 100644 index 0000000..96fb622 --- /dev/null +++ b/binary-array-ld-netcdf/src/main/kotlin/net/bald/netcdf/NetCdfLdConverter.kt @@ -0,0 +1,50 @@ +package net.bald.netcdf + +import net.bald.Converter +import net.bald.alias.AliasDefinition +import net.bald.context.ModelContext +import net.bald.model.ModelAliasDefinition +import net.bald.model.ModelBinaryArrayConverter +import org.apache.jena.rdf.model.Model +import org.apache.jena.rdf.model.ModelFactory +import java.net.URI + +/** + * NetCDF to LD converter API. + * Use [getInstance] to obtain an instance. + */ +class NetCdfLdConverter: Converter { + companion object { + /** + * Factory method for obtaining [NetCdfLdConverter]. + * @return The converter. + */ + fun getInstance(): Converter { + return NetCdfLdConverter() + } + } + + override fun convert(input: URI, uri: String?, contexts: List?, aliases: List?, downloadUrl: String?): Model { + val fileLoc = input.toString() + val context = context(contexts) + val alias = alias(aliases) + val ba = NetCdfBinaryArray.create(fileLoc, uri, context, alias, downloadUrl) + return ba.use(ModelBinaryArrayConverter::convert) + } + + private fun context(contextLocs: List?): ModelContext { + val prefixes = contextLocs?.map { contextLoc -> + ModelFactory.createDefaultModel().read(contextLoc.toString(), "json-ld") + } ?: emptyList() + + return ModelContext.create(prefixes) + } + + private fun alias(aliasLocs: List?): AliasDefinition { + return ModelFactory.createDefaultModel().apply { + aliasLocs?.forEach { aliasLoc -> + aliasLoc.toString().let(::read) + } + }.let(ModelAliasDefinition::create) + } +} \ No newline at end of file diff --git a/binary-array-ld-netcdf/src/main/kotlin/net/bald/netcdf/NetCdfRootContainer.kt b/binary-array-ld-netcdf/src/main/kotlin/net/bald/netcdf/NetCdfRootContainer.kt index 99e55ab..b867905 100644 --- a/binary-array-ld-netcdf/src/main/kotlin/net/bald/netcdf/NetCdfRootContainer.kt +++ b/binary-array-ld-netcdf/src/main/kotlin/net/bald/netcdf/NetCdfRootContainer.kt @@ -1,11 +1,11 @@ package net.bald.netcdf import net.bald.Attribute -import ucar.nc2.Group -import ucar.nc2.Variable import net.bald.Container import net.bald.alias.AliasDefinition import net.bald.vocab.BALD +import ucar.nc2.Group +import ucar.nc2.Variable /** * NetCDF implementation of [Container] based on the root group. diff --git a/binary-array-ld-netcdf/src/main/kotlin/net/bald/netcdf/NetCdfVar.kt b/binary-array-ld-netcdf/src/main/kotlin/net/bald/netcdf/NetCdfVar.kt index 5419665..648d634 100644 --- a/binary-array-ld-netcdf/src/main/kotlin/net/bald/netcdf/NetCdfVar.kt +++ b/binary-array-ld-netcdf/src/main/kotlin/net/bald/netcdf/NetCdfVar.kt @@ -2,7 +2,6 @@ package net.bald.netcdf import net.bald.CoordinateRange import net.bald.Var -import net.bald.vocab.BALD import ucar.nc2.AttributeContainer import ucar.nc2.Variable @@ -74,6 +73,7 @@ class NetCdfVar( .flatten() .mapNotNull(parent::parseReferences) .flatMap(ReferenceCollection::asVars) + .filter { v -> v.dimensions().iterator().hasNext() } } override fun toString(): String { diff --git a/binary-array-ld-netcdf/src/test/kotlin/net/bald/netcdf/NetCdfBinaryArrayTest.kt b/binary-array-ld-netcdf/src/test/kotlin/net/bald/netcdf/NetCdfBinaryArrayTest.kt index 3929b7f..b534231 100644 --- a/binary-array-ld-netcdf/src/test/kotlin/net/bald/netcdf/NetCdfBinaryArrayTest.kt +++ b/binary-array-ld-netcdf/src/test/kotlin/net/bald/netcdf/NetCdfBinaryArrayTest.kt @@ -5,7 +5,7 @@ import bald.netcdf.CdlConverter.writeToNetCdf import net.bald.BinaryArray import net.bald.alias.AliasDefinition import net.bald.context.ModelContext -import net.bald.alias.ModelAliasDefinition +import net.bald.model.ModelAliasDefinition import net.bald.vocab.BALD import org.apache.jena.rdf.model.ModelFactory import org.apache.jena.rdf.model.ResourceFactory.createPlainLiteral diff --git a/binary-array-ld-netcdf/src/test/kotlin/net/bald/netcdf/ReferenceValueParserTest.kt b/binary-array-ld-netcdf/src/test/kotlin/net/bald/netcdf/ReferenceValueParserTest.kt index 29c8637..5967214 100644 --- a/binary-array-ld-netcdf/src/test/kotlin/net/bald/netcdf/ReferenceValueParserTest.kt +++ b/binary-array-ld-netcdf/src/test/kotlin/net/bald/netcdf/ReferenceValueParserTest.kt @@ -2,7 +2,7 @@ package net.bald.netcdf import bald.netcdf.CdlConverter import org.apache.jena.rdf.model.RDFList -import org.junit.jupiter.api.* +import org.junit.jupiter.api.Test import kotlin.test.assertEquals import kotlin.test.assertNotNull import kotlin.test.assertNull diff --git a/binary-array-ld-netcdf/src/test/kotlin/net/bald/netcdf/UriParserTest.kt b/binary-array-ld-netcdf/src/test/kotlin/net/bald/netcdf/UriParserTest.kt index 74bdba6..1e7080c 100644 --- a/binary-array-ld-netcdf/src/test/kotlin/net/bald/netcdf/UriParserTest.kt +++ b/binary-array-ld-netcdf/src/test/kotlin/net/bald/netcdf/UriParserTest.kt @@ -4,7 +4,7 @@ import net.bald.vocab.BALD import org.apache.jena.shared.PrefixMapping import org.apache.jena.vocabulary.DCTerms import org.apache.jena.vocabulary.SKOS -import org.junit.jupiter.api.* +import org.junit.jupiter.api.Test import kotlin.test.assertEquals import kotlin.test.assertNull diff --git a/binary-array-ld-test/pom.xml b/binary-array-ld-test/pom.xml index 3f2d2c1..53bc10a 100644 --- a/binary-array-ld-test/pom.xml +++ b/binary-array-ld-test/pom.xml @@ -17,6 +17,11 @@ Test utilities for Binary Array LD functionality. + + net.binary-array-ld + binary-array-ld-api + ${project.parent.version} + org.apache.jena jena-core @@ -32,6 +37,21 @@ netcdf4 5.3.2 + + com.fasterxml.jackson.core + jackson-core + ${jackson.version} + + + com.fasterxml.jackson.module + jackson-module-kotlin + ${jackson.version} + + + com.fasterxml.jackson.dataformat + jackson-dataformat-yaml + ${jackson.version} + org.jetbrains.kotlin kotlin-test-junit @@ -42,6 +62,11 @@ junit-jupiter-api ${junit.jupiter.version} + + org.junit.jupiter + junit-jupiter-params + ${junit.jupiter.version} + @@ -49,6 +74,13 @@ org.jetbrains.dokka dokka-maven-plugin + + + + http://kotlin.binary-array-ld.net/javadoc/binary-array-ld-api + + + diff --git a/binary-array-ld-test/src/main/kotlin/bald/file/ResourceFileConverter.kt b/binary-array-ld-test/src/main/kotlin/bald/file/ResourceFileConverter.kt new file mode 100644 index 0000000..b9cd0dd --- /dev/null +++ b/binary-array-ld-test/src/main/kotlin/bald/file/ResourceFileConverter.kt @@ -0,0 +1,24 @@ +package bald.file + +import java.io.File + +/** + * Test utility for converting classpath resources to files in tests. + * Resources are in src/main/resources. + */ +object ResourceFileConverter { + /** + * Loads a resource into a temporary file. + * @param loc The location of the classpath resource. + * @param ext File extension to add to the file. + * @return A file containing the contents of the given resource. + */ + fun toFile(loc: String, ext: String? = null): File { + val file = createTempFile(suffix = ext?.let("."::plus)) + javaClass.getResourceAsStream(loc).use { input -> + file.outputStream().use(input::copyTo) + } + + return file + } +} \ No newline at end of file diff --git a/binary-array-ld-test/src/main/kotlin/bald/json/JsonSpecRequirement.kt b/binary-array-ld-test/src/main/kotlin/bald/json/JsonSpecRequirement.kt new file mode 100644 index 0000000..9bf21b3 --- /dev/null +++ b/binary-array-ld-test/src/main/kotlin/bald/json/JsonSpecRequirement.kt @@ -0,0 +1,66 @@ +package bald.json + +import bald.file.ResourceFileConverter +import bald.model.ResourceModelConverter +import bald.netcdf.CdlConverter +import bald.spec.SpecRequirement +import com.fasterxml.jackson.annotation.JsonCreator +import com.fasterxml.jackson.annotation.JsonFormat +import net.bald.Converter +import org.apache.jena.rdf.model.Model +import java.io.File + +class JsonSpecRequirement( + override val name: String, + override val comment: String? = null, + private val root: String? = null, + private val input: Input, + private val output: Output, +): SpecRequirement { + override fun result(converter: Converter): Model { + return input.convert(this, converter) + } + + override fun expectation(): Model { + return output.graph(this) + } + + fun locate(path: String): String { + return if (root == null) path else "$root/$path" + } + + class Input( + private val file: String, + private val uri: String? = null, + @JsonFormat(with = [JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY]) + private val context: List = emptyList(), + @JsonFormat(with = [JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY]) + private val alias: List = emptyList() + ) { + fun convert(parent: JsonSpecRequirement, converter: Converter): Model { + val inputLoc = parent.locate(file).let(CdlConverter::writeToNetCdf).toURI() + val contextLocs = files(context).map(File::toURI) + val aliasLocs = files(alias).map(File::toURI) + return converter.convert(inputLoc, uri, contextLocs, aliasLocs) + } + + private fun files(resourceLocs: List): List { + return resourceLocs.map { loc -> + val ext = loc.substringAfterLast(".", "tmp") + ResourceFileConverter.toFile(loc, ext) + } + } + } + + class Output @JsonCreator constructor( + private val file: String + ) { + fun graph(parent: JsonSpecRequirement): Model { + return parent.locate(file).let(ResourceModelConverter::toModel) + } + } + + override fun toString(): String { + return name + } +} \ No newline at end of file diff --git a/binary-array-ld-test/src/main/kotlin/bald/json/JsonTestSpec.kt b/binary-array-ld-test/src/main/kotlin/bald/json/JsonTestSpec.kt new file mode 100644 index 0000000..4e62032 --- /dev/null +++ b/binary-array-ld-test/src/main/kotlin/bald/json/JsonTestSpec.kt @@ -0,0 +1,14 @@ +package bald.json + +import bald.spec.SpecRequirement +import bald.spec.TestSpec +import com.fasterxml.jackson.annotation.JsonAutoDetect + +@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY) +class JsonTestSpec( + private val tests: List +): TestSpec { + override fun tests(): Sequence { + return tests.asSequence() + } +} \ No newline at end of file diff --git a/binary-array-ld-test/src/main/kotlin/bald/json/YamlUtil.kt b/binary-array-ld-test/src/main/kotlin/bald/json/YamlUtil.kt new file mode 100644 index 0000000..9f52829 --- /dev/null +++ b/binary-array-ld-test/src/main/kotlin/bald/json/YamlUtil.kt @@ -0,0 +1,9 @@ +package bald.json + +import com.fasterxml.jackson.databind.ObjectMapper +import com.fasterxml.jackson.dataformat.yaml.YAMLFactory +import com.fasterxml.jackson.module.kotlin.registerKotlinModule + +object YamlUtil { + val mapper by lazy { ObjectMapper(YAMLFactory()).registerKotlinModule() } +} \ No newline at end of file diff --git a/binary-array-ld-test/src/main/kotlin/bald/model/ModelVerifier.kt b/binary-array-ld-test/src/main/kotlin/bald/model/ModelVerifier.kt index 0ae4013..ebc2cce 100644 --- a/binary-array-ld-test/src/main/kotlin/bald/model/ModelVerifier.kt +++ b/binary-array-ld-test/src/main/kotlin/bald/model/ModelVerifier.kt @@ -1,8 +1,6 @@ package bald.model import org.apache.jena.rdf.model.Model -import org.apache.jena.rdf.model.Resource -import org.junit.jupiter.api.fail import kotlin.test.assertEquals import kotlin.test.assertTrue diff --git a/binary-array-ld-test/src/main/kotlin/bald/model/ResourceModelConverter.kt b/binary-array-ld-test/src/main/kotlin/bald/model/ResourceModelConverter.kt new file mode 100644 index 0000000..40cec5f --- /dev/null +++ b/binary-array-ld-test/src/main/kotlin/bald/model/ResourceModelConverter.kt @@ -0,0 +1,12 @@ +package bald.model + +import org.apache.jena.rdf.model.Model +import org.apache.jena.rdf.model.ModelFactory + +object ResourceModelConverter { + fun toModel(modelLoc: String): Model { + return javaClass.getResourceAsStream(modelLoc).use { input -> + ModelFactory.createDefaultModel().read(input, null, "ttl") + } + } +} \ No newline at end of file diff --git a/binary-array-ld-test/src/main/kotlin/bald/model/StatementsVerifier.kt b/binary-array-ld-test/src/main/kotlin/bald/model/StatementsVerifier.kt index c1bec4f..b7339f7 100644 --- a/binary-array-ld-test/src/main/kotlin/bald/model/StatementsVerifier.kt +++ b/binary-array-ld-test/src/main/kotlin/bald/model/StatementsVerifier.kt @@ -1,5 +1,6 @@ package bald.model +import org.apache.jena.datatypes.xsd.XSDDatatype import org.apache.jena.rdf.model.* import org.apache.jena.rdf.model.ResourceFactory.createTypedLiteral import org.apache.jena.vocabulary.RDF @@ -74,7 +75,7 @@ class StatementsVerifier( } fun list(vararg values: Int) { - values.map(::createTypedLiteral).let(::list) + values.map { value -> createTypedLiteral(value.toString(), XSDDatatype.XSDinteger) }.let(::list) } fun list(values: List) { diff --git a/binary-array-ld-test/src/main/kotlin/bald/spec/BaseSpecTest.kt b/binary-array-ld-test/src/main/kotlin/bald/spec/BaseSpecTest.kt new file mode 100644 index 0000000..aae5b73 --- /dev/null +++ b/binary-array-ld-test/src/main/kotlin/bald/spec/BaseSpecTest.kt @@ -0,0 +1,53 @@ +package bald.spec + +import bald.json.JsonSpecRequirement +import bald.json.JsonTestSpec +import net.bald.Converter +import org.apache.jena.rdf.model.Model +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ArgumentsSource +import kotlin.test.fail + +/** + * Base class for integration tests that run a converter against the BALD specification. + * Runs a series of parameterised tests with parameters provided by [TestScenarioProvider]. + * The test suite is defined in the /spec/spec.yaml resource. + * Tests are defined in [JsonTestSpec] and [JsonSpecRequirement] format. + * To add or modify tests, update /spec/spec.yaml. + */ +abstract class BaseSpecTest { + protected abstract val converter: Converter + + @ParameterizedTest + @ArgumentsSource(TestScenarioProvider::class) + fun test(test: SpecRequirement) { + println("Test ${test.name}:\n${test.comment ?: "No description"}") + + val result = test.result(converter) + val expected = test.expectation() + val success = expected.isIsomorphicWith(result) + + if (success) { + println("Test ${test.name} passed.") + } else { + println("Test ${test.name} failed.") + + expected.diff(result)?.let { diff -> + println("The following statements were expected but not found:") + diff.write(System.out, "ttl") + } + + result.diff(expected)?.let { diff -> + println("The following statements were found but not expected:") + diff.write(System.out, "ttl") + } + + fail("Test ${test.name} failed - see log for details.") + } + } + + private fun Model.diff(to: Model): Model? { + val prefixes = this.nsPrefixMap + to.nsPrefixMap + return difference(to).setNsPrefixes(prefixes).takeUnless(Model::isEmpty) + } +} \ No newline at end of file diff --git a/binary-array-ld-test/src/main/kotlin/bald/spec/SpecRequirement.kt b/binary-array-ld-test/src/main/kotlin/bald/spec/SpecRequirement.kt new file mode 100644 index 0000000..ef56fa2 --- /dev/null +++ b/binary-array-ld-test/src/main/kotlin/bald/spec/SpecRequirement.kt @@ -0,0 +1,32 @@ +package bald.spec + +import net.bald.Converter +import org.apache.jena.rdf.model.Model + +/** + * A scenario which tests a specific requirement in the BALD specification. + */ +interface SpecRequirement { + /** + * The name of the requirement. + */ + val name: String + + /** + * A description of the requirement. + */ + val comment: String? + + /** + * Converts this test's input files and parameters into a graph using the given converter. + * @param converter The converter implementation to test. + * @return The graph resulting from the conversion. + */ + fun result(converter: Converter): Model + + /** + * Obtain the expected RDF graph. + * @return The expected graph result. + */ + fun expectation(): Model +} \ No newline at end of file diff --git a/binary-array-ld-test/src/main/kotlin/bald/spec/TestScenarioProvider.kt b/binary-array-ld-test/src/main/kotlin/bald/spec/TestScenarioProvider.kt new file mode 100644 index 0000000..2f2b860 --- /dev/null +++ b/binary-array-ld-test/src/main/kotlin/bald/spec/TestScenarioProvider.kt @@ -0,0 +1,27 @@ +package bald.spec + +import bald.json.JsonTestSpec +import bald.json.YamlUtil +import org.junit.jupiter.api.extension.ExtensionContext +import org.junit.jupiter.params.provider.Arguments +import org.junit.jupiter.params.provider.ArgumentsProvider +import java.util.stream.Stream +import kotlin.streams.asStream + +/** + * JUnit arguments provider for deserializing [SpecRequirement] instances. + * The test suite is defined in the /spec/spec.yaml resource. + * Tests are defined in [JsonTestSpec] format. + * @see BaseSpecTest + */ +class TestScenarioProvider: ArgumentsProvider { + override fun provideArguments(context: ExtensionContext?): Stream { + return suite().tests().map { test -> Arguments.of(test) }.asStream() + } + + private fun suite(): TestSpec { + return javaClass.getResourceAsStream("/spec/spec.yaml").use { input -> + YamlUtil.mapper.readValue(input, JsonTestSpec::class.java) + } + } +} \ No newline at end of file diff --git a/binary-array-ld-test/src/main/kotlin/bald/spec/TestSpec.kt b/binary-array-ld-test/src/main/kotlin/bald/spec/TestSpec.kt new file mode 100644 index 0000000..f0745e0 --- /dev/null +++ b/binary-array-ld-test/src/main/kotlin/bald/spec/TestSpec.kt @@ -0,0 +1,12 @@ +package bald.spec + +/** + * A suite of tests which implement the BALD specification. + */ +interface TestSpec { + /** + * List the tests. + * @return The tests. + */ + fun tests(): Sequence +} \ No newline at end of file diff --git a/binary-array-ld-test/src/main/resources/spec/spec.yaml b/binary-array-ld-test/src/main/resources/spec/spec.yaml new file mode 100644 index 0000000..0c398ea --- /dev/null +++ b/binary-array-ld-test/src/main/resources/spec/spec.yaml @@ -0,0 +1,38 @@ +tests: + - name: "Conformance Class A: URIs" + comment: | + Verify that the identity of a file is always present within the RDF graph parsed from that file. + Verify that the identity of variables and groups within a file are defined relative to the defined file identity. + root: "/spec/test/a-identity" + input: + file: "input.cdl" + uri: "http://secret.binary-array-ld.net/identity.nc" + output: "output.ttl" + - name: "Conformance Class B: Prefixing" + comment: | + Verify that defined prefixes within a file are recognised and used to expand prefix notation attribute names and values. + root: "/spec/test/b-prefixing" + input: + file: "input.cdl" + uri: "http://secret.binary-array-ld.net/prefix.nc" + output: "output.ttl" + - name: "Conformance Class C: Aliasing" + root: "/spec/test/c-aliasing" + input: + file: "input.cdl" + uri: "http://secret.binary-array-ld.net/alias.nc" + alias: "/turtle/netcdf.ttl" + output: "output.ttl" + - name: "Conformance Class D: Attribute Names" + root: "/spec/test/d-attributes" + input: + file: "input.cdl" + uri: "http://secret.binary-array-ld.net/attributes.nc" + alias: "/turtle/netcdf.ttl" + output: "output.ttl" + - name: "Conformance Class E: Variable-to-variable Referencing; Class F: Coordinate Variables" + root: "/spec/test/ef-variables" + input: + file: "input.cdl" + uri: "http://secret.binary-array-ld.net/reference.nc" + output: "output.ttl" \ No newline at end of file diff --git a/binary-array-ld-test/src/main/resources/spec/test/a-identity/input.cdl b/binary-array-ld-test/src/main/resources/spec/test/a-identity/input.cdl new file mode 100644 index 0000000..394275b --- /dev/null +++ b/binary-array-ld-test/src/main/resources/spec/test/a-identity/input.cdl @@ -0,0 +1,10 @@ +netcdf ogcClassA { +dimensions: + d0 = 1 ; + d1 = 1 ; +variables: + int var0 ; + int var1 ; + +data: +} \ No newline at end of file diff --git a/binary-array-ld-test/src/main/resources/spec/test/a-identity/output.ttl b/binary-array-ld-test/src/main/resources/spec/test/a-identity/output.ttl new file mode 100644 index 0000000..307202f --- /dev/null +++ b/binary-array-ld-test/src/main/resources/spec/test/a-identity/output.ttl @@ -0,0 +1,21 @@ +@prefix bald: . +@prefix dcat: . +@prefix dct: . +@prefix rdf: . +@prefix rdfs: . +@prefix this: . +@prefix xml: . +@prefix xsd: . + +this: a bald:Container ; + dct:format [ a dct:MediaType ; + dct:identifier ] ; + dcat:distribution [ a dcat:Distribution ; + dcat:mediaType [ a dct:MediaType ; + dct:identifier "application/x-netcdf" ] ] ; + bald:contains this:var0, + this:var1 . + +this:var0 a bald:Resource . + +this:var1 a bald:Resource . \ No newline at end of file diff --git a/binary-array-ld-test/src/main/resources/spec/test/b-prefixing/input.cdl b/binary-array-ld-test/src/main/resources/spec/test/b-prefixing/input.cdl new file mode 100644 index 0000000..8f1980b --- /dev/null +++ b/binary-array-ld-test/src/main/resources/spec/test/b-prefixing/input.cdl @@ -0,0 +1,16 @@ +netcdf ogcClassB { +dimensions: + d0 = 1 ; + d1 = 1 ; +variables: + int var0 ; + var0:rdfs__label = "Variable Zero" ; + int var1 ; + var1:rdfs__label = "Variable One" ; + int prefix_list ; + prefix_list:bald__ = "https://www.opengis.net/def/binary-array-ld/" ; + prefix_list:rdf__ = "http://www.w3.org/1999/02/22-rdf-syntax-ns#" ; + prefix_list:rdfs__ = "http://www.w3.org/2000/01/rdf-schema#" ; + :bald__isPrefixedBy = "prefix_list" ; +data: +} \ No newline at end of file diff --git a/binary-array-ld-test/src/main/resources/spec/test/b-prefixing/output.ttl b/binary-array-ld-test/src/main/resources/spec/test/b-prefixing/output.ttl new file mode 100644 index 0000000..179e82d --- /dev/null +++ b/binary-array-ld-test/src/main/resources/spec/test/b-prefixing/output.ttl @@ -0,0 +1,23 @@ +@prefix bald: . +@prefix dcat: . +@prefix dct: . +@prefix rdf: . +@prefix rdfs: . +@prefix this: . +@prefix xml: . +@prefix xsd: . + +this: a bald:Container ; + dct:format [ a dct:MediaType ; + dct:identifier ] ; + dcat:distribution [ a dcat:Distribution ; + dcat:mediaType [ a dct:MediaType ; + dct:identifier "application/x-netcdf" ] ] ; + bald:contains this:var0, + this:var1 . + +this:var0 a bald:Resource ; + rdfs:label "Variable Zero" . + +this:var1 a bald:Resource ; + rdfs:label "Variable One" . \ No newline at end of file diff --git a/binary-array-ld-test/src/main/resources/spec/test/c-aliasing/input.cdl b/binary-array-ld-test/src/main/resources/spec/test/c-aliasing/input.cdl new file mode 100644 index 0000000..98cc0db --- /dev/null +++ b/binary-array-ld-test/src/main/resources/spec/test/c-aliasing/input.cdl @@ -0,0 +1,10 @@ +netcdf ogcClassC { +dimensions: + d0 = 1 ; + d1 = 1 ; +variables: + int var0 ; + int var1 ; +:title = "Sample netCDF file definition with alias terms from the netCDF user guide" ; +data: +} diff --git a/binary-array-ld-test/src/main/resources/spec/test/c-aliasing/output.ttl b/binary-array-ld-test/src/main/resources/spec/test/c-aliasing/output.ttl new file mode 100644 index 0000000..3045582 --- /dev/null +++ b/binary-array-ld-test/src/main/resources/spec/test/c-aliasing/output.ttl @@ -0,0 +1,23 @@ +@prefix NetCDF: . +@prefix bald: . +@prefix dcat: . +@prefix dct: . +@prefix rdf: . +@prefix rdfs: . +@prefix this: . +@prefix xml: . +@prefix xsd: . + +this: a bald:Container ; + NetCDF:title "Sample netCDF file definition with alias terms from the netCDF user guide" ; + dct:format [ a dct:MediaType ; + dct:identifier ] ; + dcat:distribution [ a dcat:Distribution ; + dcat:mediaType [ a dct:MediaType ; + dct:identifier "application/x-netcdf" ] ] ; + bald:contains this:var0, + this:var1 . + +this:var0 a bald:Resource . + +this:var1 a bald:Resource . \ No newline at end of file diff --git a/binary-array-ld-test/src/main/resources/spec/test/d-attributes/input.cdl b/binary-array-ld-test/src/main/resources/spec/test/d-attributes/input.cdl new file mode 100644 index 0000000..6276b88 --- /dev/null +++ b/binary-array-ld-test/src/main/resources/spec/test/d-attributes/input.cdl @@ -0,0 +1,17 @@ +netcdf ogcClassD { +dimensions: + d0 = 1 ; + d1 = 1 ; +variables: + int var0 ; + var0:rdfs__label = "Variable Zero" ; + int var1 ; + var1:rdfs__label = "Variable One" ; + int prefix_list ; + prefix_list:bald__ = "https://www.opengis.net/def/binary-array-ld/" ; + prefix_list:rdf__ = "http://www.w3.org/1999/02/22-rdf-syntax-ns#" ; + prefix_list:rdfs__ = "http://www.w3.org/2000/01/rdf-schema#" ; + :bald__isPrefixedBy = "prefix_list" ; + :title = "Sample netCDF file definition with alias terms from the netCDF user guide" ; +data: +} diff --git a/binary-array-ld-test/src/main/resources/spec/test/d-attributes/output.ttl b/binary-array-ld-test/src/main/resources/spec/test/d-attributes/output.ttl new file mode 100644 index 0000000..5e132e5 --- /dev/null +++ b/binary-array-ld-test/src/main/resources/spec/test/d-attributes/output.ttl @@ -0,0 +1,25 @@ +@prefix NetCDF: . +@prefix bald: . +@prefix dcat: . +@prefix dct: . +@prefix rdf: . +@prefix rdfs: . +@prefix this: . +@prefix xml: . +@prefix xsd: . + +this: a bald:Container ; + NetCDF:title "Sample netCDF file definition with alias terms from the netCDF user guide" ; + dct:format [ a dct:MediaType ; + dct:identifier ] ; + dcat:distribution [ a dcat:Distribution ; + dcat:mediaType [ a dct:MediaType ; + dct:identifier "application/x-netcdf" ] ] ; + bald:contains this:var0, + this:var1 . + +this:var0 a bald:Resource ; + rdfs:label "Variable Zero" . + +this:var1 a bald:Resource ; + rdfs:label "Variable One" . \ No newline at end of file diff --git a/binary-array-ld-test/src/main/resources/spec/test/ef-variables/input.cdl b/binary-array-ld-test/src/main/resources/spec/test/ef-variables/input.cdl new file mode 100644 index 0000000..82f9dc9 --- /dev/null +++ b/binary-array-ld-test/src/main/resources/spec/test/ef-variables/input.cdl @@ -0,0 +1,39 @@ +netcdf tmpMwXy8U { +dimensions: + pdim0 = 11 ; + pdim1 = 17 ; + xy = 2 ; +variables: + int data_variable1(pdim0, pdim1) ; + data_variable1:bald__references = "location_variable" ; + data_variable1:long_name = "data variable one"; + + int data_variable2(pdim0, pdim1) ; + data_variable2:bald__references = "location_variable" ; + data_variable2:long_name = "data variable two"; + + int pdim0(pdim0) ; + + int pdim1(pdim1) ; + + int location_variable(pdim0, pdim1, xy) ; + location_variable:bald__references = "location_reference_system" ; + + int location_reference_system; + location_reference_system:pcode = "4897"; + + int set_collection ; + set_collection:bald__references = "data_variable1 data_variable2" ; + + int list_collection ; + list_collection:bald__references = "( data_variable1 data_variable2 )" ; + +// prefix group +group: prefix_list { + :bald__ = "https://www.opengis.net/def/binary-array-ld/" ; + :rdf__ = "http://www.w3.org/1999/02/22-rdf-syntax-ns#" ; +} +// global attributes: + :bald__isPrefixedBy = "prefix_list" ; + +} \ No newline at end of file diff --git a/binary-array-ld-test/src/main/resources/spec/test/ef-variables/output.ttl b/binary-array-ld-test/src/main/resources/spec/test/ef-variables/output.ttl new file mode 100644 index 0000000..3b09cc1 --- /dev/null +++ b/binary-array-ld-test/src/main/resources/spec/test/ef-variables/output.ttl @@ -0,0 +1,85 @@ +@prefix bald: . +@prefix dcat: . +@prefix dct: . +@prefix rdf: . +@prefix rdfs: . +@prefix this: . +@prefix xml: . +@prefix xsd: . + +this: a bald:Container ; + dct:format [ a dct:MediaType ; + dct:identifier ] ; + dcat:distribution [ a dcat:Distribution ; + dcat:mediaType [ a dct:MediaType ; + dct:identifier "application/x-netcdf" ] ] ; + bald:contains this:data_variable1, + this:data_variable2, + this:list_collection, + this:location_reference_system, + this:location_variable, + this:pdim0, + this:pdim1, + this:set_collection . + +this:list_collection a bald:Resource ; + bald:references ( this:data_variable1 this:data_variable2 ) . + +this:set_collection a bald:Resource ; + bald:references this:data_variable1, + this:data_variable2 . + +this:location_reference_system a bald:Resource ; + this:pcode "4897" . + +this:data_variable1 a bald:Array ; + this:long_name "data variable one" ; + bald:references [ a bald:Reference ; + bald:target this:pdim1 ; + bald:sourceRefShape ( 11 17 ) ; + bald:targetRefShape ( 1 17 ) ], + [ a bald:Reference ; + bald:target this:pdim0 ; + bald:sourceRefShape ( 11 17 ) ; + bald:targetRefShape ( 11 1 ) ], + [ a bald:Reference ; + bald:sourceRefShape ( 11 17 1 ) ; + bald:target this:location_variable ; + bald:targetRefShape ( 11 17 2 ) ], + this:location_variable ; + bald:shape ( 11 17 ) . + +this:data_variable2 a bald:Array ; + this:long_name "data variable two" ; + bald:references [ a bald:Reference ; + bald:sourceRefShape ( 11 17 1 ) ; + bald:target this:location_variable ; + bald:targetRefShape ( 11 17 2 ) ], + [ a bald:Reference ; + bald:target this:pdim0 ; + bald:sourceRefShape ( 11 17 ) ; + bald:targetRefShape ( 11 1 ) ], + [ a bald:Reference ; + bald:target this:pdim1 ; + bald:sourceRefShape ( 11 17 ) ; + bald:targetRefShape ( 1 17 ) ], + this:location_variable ; + bald:shape ( 11 17 ) . + +this:pdim0 a bald:Array ; + bald:shape ( 11 ) . + +this:pdim1 a bald:Array ; + bald:shape ( 17 ) . + +this:location_variable a bald:Array ; + bald:references [ a bald:Reference ; + bald:target this:pdim1 ; + bald:sourceRefShape ( 11 17 2 ) ; + bald:targetRefShape ( 1 17 1 ) ], + [ a bald:Reference ; + bald:target this:pdim0 ; + bald:sourceRefShape ( 11 17 2 ) ; + bald:targetRefShape ( 11 1 1 ) ], + this:location_reference_system ; + bald:shape ( 11 17 2 ) . \ No newline at end of file diff --git a/binary-array-ld-test/src/main/resources/turtle/netcdf.ttl b/binary-array-ld-test/src/main/resources/turtle/netcdf.ttl new file mode 100644 index 0000000..90fdb6b --- /dev/null +++ b/binary-array-ld-test/src/main/resources/turtle/netcdf.ttl @@ -0,0 +1,134 @@ +@prefix cc: . +@prefix void: . +@prefix org: . +@prefix odrs: . +@prefix ssd: . +@prefix owl: . +@prefix xsd: . +@prefix env-ui: . +@prefix skos: . +@prefix rdfs: . +@prefix version: . +@prefix qb: . +@prefix dgu: . +@prefix ui: . +@prefix dct: . +@prefix rdf: . +@prefix reg: . +@prefix ldp: . +@prefix time: . +@prefix api: . +@prefix vann: . +@prefix vs: . +@prefix prov: . +@prefix foaf: . +@prefix dc: . + + + a reg:Register , ldp:Container ; + rdfs:label "Net CDF" ; + dct:description "Vocabulary of terms used in the netCDF User Guide." ; + dct:modified "2020-05-04T14:15:24.798Z"^^xsd:dateTime ; + reg:category ; + owl:versionInfo 1 ; + ldp:contains , , , , , , , , , , ; + ldp:hasMemberRelation ldp:contains . + + + a rdf:Property ; + rdfs:domain ; + rdfs:label "valid max" ; + rdfs:range rdfs:Literal ; + dct:description "Largest valid value of a variable." ; + dct:identifier "valid_max" ; + dct:references . + + + a rdf:Property ; + rdfs:domain ; + rdfs:label " FillValue" ; + rdfs:range rdfs:Literal ; + dct:description "A value used to represent missing or undefined data. Allowed for auxiliary coordinate variables but not allowed for coordinate variables." ; + dct:identifier "_FillValue" ; + dct:references . + + + a rdf:Property ; + rdfs:domain ; + rdfs:label "valid range" ; + rdfs:range rdfs:Literal ; + dct:description "Smallest and largest valid values of a variable." ; + dct:identifier "valid_range" ; + dct:references . + + + a rdf:Property ; + rdfs:domain ; + rdfs:label "title" ; + rdfs:range rdfs:Literal ; + dct:description "Short description of the file contents." ; + dct:identifier "title" ; + dct:references . + + + a rdf:Property ; + rdfs:domain ; + rdfs:label "units" ; + rdfs:range rdfs:Literal ; + dct:description "Units of a variable's content." ; + dct:identifier "units" ; + dct:references . + + + a rdf:Property ; + rdfs:domain ; + rdfs:label "add offset" ; + rdfs:range rdfs:Literal ; + dct:description "If present for a variable, this number is to be added to the data after it is read by an application. If both **`scale_factor`** and **`add_offset`** attributes are present, the data are first scaled before the offset is added. In cases where there is a strong constraint on dataset size, it is allowed to pack the coordinate variables (using add_offset and/or scale_factor), but this is not recommended in general." ; + dct:identifier "add_offset" ; + dct:references . + + + a rdf:Property ; + rdfs:domain ; + rdfs:label "long name" ; + rdfs:range rdfs:Literal ; + dct:description "A descriptive name that indicates a variable's content. This name is not standardized." ; + dct:identifier "long_name" ; + dct:references . + + + a rdf:Property ; + rdfs:domain ; + rdfs:label "valid min" ; + rdfs:range rdfs:Literal ; + dct:description "Smallest valid value of a variable." ; + dct:identifier "valid_min" ; + dct:references . + + + a rdf:Property ; + rdfs:domain ; + rdfs:label "scale factor" ; + rdfs:range rdfs:Literal ; + dct:description "If present for a variable, the data are to be multiplied by this factor after the data are read by an application. See also the **`add_offset`** attribute. In cases where there is a strong constraint on dataset size, it is allowed to pack the coordinate variables (using add_offset and/or scale_factor), but this is not recommended in general." ; + dct:identifier "scale_factor" ; + dct:references . + + + a rdf:Property ; + rdfs:domain ; + rdfs:label "history" ; + rdfs:range rdfs:Literal ; + dct:description "List of the applications that have modified the original data." ; + dct:identifier "history" ; + dct:references . + + + a rdf:Property ; + rdfs:domain ; + rdfs:label "Conventions" ; + rdfs:range rdfs:Literal ; + dct:description "Name of the conventions followed by the dataset." ; + dct:identifier "Conventions" ; + dct:references . diff --git a/docs/alias.md b/docs/alias.md index b85e58a..577873f 100644 --- a/docs/alias.md +++ b/docs/alias.md @@ -25,7 +25,9 @@ See [here](https://docs.oracle.com/javase/8/docs/technotes/guides/net/proxies.ht ## Library -You can provide aliases as an Apache Jena [model](https://jena.apache.org/documentation/javadoc/jena/org/apache/jena/rdf/model/Model.html). +For simple usage, use the `NetCdfLd.convert` method as described [here](lib.md#simple-usage). + +Alternatively, you can provide aliases as an Apache Jena [model](https://jena.apache.org/documentation/javadoc/jena/org/apache/jena/rdf/model/Model.html). You can create a `Model` instance programmatically, or by reading in an RDF file. See the [Jena docs](https://jena.apache.org/tutorials/rdf_api.html) for more information. diff --git a/docs/context.md b/docs/context.md index defa5ad..ff532a7 100644 --- a/docs/context.md +++ b/docs/context.md @@ -16,7 +16,9 @@ java -jar bald-cli.jar --context /path/to/context.json /path/to/netcdf.nc /path/ ## Library -You can provide the context as an Apache Jena [prefix mapping](https://jena.apache.org/documentation/javadoc/jena/org/apache/jena/shared/PrefixMapping.html). +For simple usage, use the `NetCdfLd.convert` method as described [here](lib.md#simple-usage). + +Alternatively, you can provide the context as an Apache Jena [prefix mapping](https://jena.apache.org/documentation/javadoc/jena/org/apache/jena/shared/PrefixMapping.html). Pass a prefix mapping (or list of mappings) to the `ModelContext.create` method to create a `ModelContext` instance. You may also create your own implementation of `ModelContext`. diff --git a/docs/download.md b/docs/download.md index 9f6beb8..94670d6 100644 --- a/docs/download.md +++ b/docs/download.md @@ -23,8 +23,9 @@ Otherwise, you can simply pass in a null value. #### Example ```java -BinaryArray ba = NetCdfBinaryArray.create("/path/to/input.ttl", "http://test.binary-array-ld.net/example", null, null, "http://test.binary-array-ld.net/download/netcdf.nc"); -Model model = ModelBinaryArrayConverter.convert(ba); +String downloadUrl = "http://test.binary-array-ld.net/download/netcdf.nc"; +URI input = new File("/path/to/input.nc").toURI(); +Model model = NetCdfLdConverter.Companion.getInstance().convert(input, "http://test.binary-array-ld.net/example", null, null, downloadUrl); try (OutputStream output = new FileOutputStream("/path/to/output.ttl")) { model.write(output, "ttl"); diff --git a/docs/index.md b/docs/index.md index 853a7f3..8ebc75b 100644 --- a/docs/index.md +++ b/docs/index.md @@ -11,3 +11,4 @@ This includes NetCDF to RDF conversion according to [OGC draft specification](ht * [CLI](cli.md) * [Library](lib.md) * [Javadoc](todo) +* [Testing](testing.md) diff --git a/docs/lib.md b/docs/lib.md index 980c4bc..eb74f56 100644 --- a/docs/lib.md +++ b/docs/lib.md @@ -24,13 +24,55 @@ To use this module, add the following dependency to your Maven project: ``` -Use the `NetCdfBinaryArray.create` method to create a new binary array representation from a NetCDF file. +### Simple Usage + +You can use the `NetCdfLd.convert` method to convert NetCDF binary array metadata to an RDF graph in Apache Jena [model](https://jena.apache.org/documentation/javadoc/jena/org/apache/jena/rdf/model/Model.html) form. +See the [Jena docs](https://jena.apache.org/tutorials/rdf_api.html) for how to use the `Model` class. +The model can be serialised to a file using the `write` method. + +The `NetCdfLd.convert` method accepts the following parameters: + +| Parameter | Type | Description | +|-----------|------|-------------| +| input | URI | The location of the NetCDF binary array file to convert. | +| uri | String | The URI that identifies the binary array. Optional. | +| contexts | List\ | The locations of files containing [JSON-LD contexts](#context). Optional. | +| aliases | List\ | The locations of files containing [alias definitions](#aliases). Optional. | +| downloadUrl | String | The URL from which the NetCDF file can be [downloaded](#download-url). Optional. | + +Optional parameters are optional in Kotlin or nullable in Java. + +#### Example +To read a NetCDF binary array and emit it to a file in [Turtle](https://www.w3.org/TR/turtle/) format: + +Kotlin +```kotlin +val input = File("/path/to/input.nc").toURI() +val model = NetCdfLd.convert(input, "http://test.binary-array-ld.net/example") +File("/path/to/output.ttl").outputStream().use { output -> + model.write(output, "ttl") +} +``` +Java +```java +File input = new File("/path/to/input.nc").toURI(); +Model model = NetCdfLd.INSTANCE.convert(input, "http://test.binary-array-ld.net/example", null, null); + +try (OutputStream output = new FileOutputStream("/path/to/output.ttl")) { + model.write(output, "ttl"); +} +``` + +### Advanced Usage + +For some purposes, you may prefer to use the components of the BALD library individually. + +You can use the `NetCdfBinaryArray.create` method to create a new binary array representation from a NetCDF file. NetCDF and CDL file formats are supported. You can also optionally supply a URI as the identifier of the dataset. You can pass the resulting `BinaryArray` instance to the `ModelBinaryArrayConverter.convert` -method to obtain an RDF graph in Apache Jena [model](https://jena.apache.org/documentation/javadoc/jena/org/apache/jena/rdf/model/Model.html) form. -See the [Jena docs](https://jena.apache.org/tutorials/rdf_api.html) for how to use the `Model` class. +method to obtain the RDF graph as a Jena model. You can also implement the `BinaryArray` interface with your own binary array metadata representations. diff --git a/docs/testing.md b/docs/testing.md new file mode 100644 index 0000000..605be69 --- /dev/null +++ b/docs/testing.md @@ -0,0 +1,76 @@ +# Testing + +The `binary-array-ld-test` module contains various utilities which assist with testing the BALD conversion process. +It contains a testing framework which lets you quickly test an implementation +against the requirements in the [BALD specification](http://docs.opengeospatial.org/DRAFTS/19-002.html). + +## Specification + +The requirements that comprise the specification are represented by a YAML resource in the +`binary-array-ld-test` module. +The YAML file is located at [src/main/resources/spec/spec.yaml](todo). + +The root object is a [spec definition](#spec-definition) containing a set of [requirement definitions](#requirement-definition). +The object structure is described below. + +#### Spec Definition + +The spec definition represents the whole specification. +Its main purpose is to be a container for the set of requirements. + +| Property | Definition | +|----------|------------| +| tests | The list of [requirement definitions](#requirement-definition). | + +#### Requirement Definition + +The requirement definition represents a single requirement in the specification. +Each requirement will generate a single JUnit test, although each test may check multiple aspects of the implementation. + +| Property | Definition | +|----------|------------| +| name | The name of the requirement. | +| comment | A description of the requirement (optional). | +| root | The root path which the input and output resource paths should be resolved against (optional). | +| input | A nested object containing the [input definition](#input-definition). | +| output | The path to the resource containing the expected RDF output. | + +#### Input Definition + +The input definition represents the set of parameters to supply to the converter implementation. + +| Property | Definition | +|----------|------------| +| file | The path to the NetCDF binary array resource to convert. | +| uri | The URI of the binary array, if it has one (optional). | +| alias | The path, or list of paths, to resources containing alias definitions (optional). | +| context | The path, or list of paths, to resources containing JSON-LD contexts (optional). | + +## Test an Implementation + +To test a new converter implementation, simply define a test class which extends `BaseSpecTest`. +The new class must implement the abstract field `converter` of type `Converter` (this represents the implementation you want to test). +Your test class will inherit the tests that are automatically generated by `BaseSpecTest`, +so you can run it immediately without writing any additional test code. + +See below, or `BinaryArrayConvertSpecTest` in the `binary-array-ld-cli` module for examples. + +#### Example + +```kotlin +class MySpecTest: BaseSpecTest() { + override val converter: Converter = MyTestConverter() +} + +class MyTestConverter(): Converter { + override fun convert( + input: URI, + uri: String?, + contexts: List?, + aliases: List?, + downloadUrl: String? + ): Model { + TODO("My implementation here") + } +} +``` diff --git a/pom.xml b/pom.xml index 30f3c9b..bd3a4ce 100644 --- a/pom.xml +++ b/pom.xml @@ -19,6 +19,7 @@ 1.2.0 5.5.2 3.13.1 + 2.12.1 2.2.0 @@ -28,13 +29,14 @@ binary-array-ld-netcdf binary-array-ld-test binary-array-ld-demo + binary-array-ld-api org.jetbrains.kotlin - kotlin-stdlib + kotlin-stdlib-jdk8 ${kotlin.version} @@ -112,6 +114,7 @@ org.apache.maven.plugins maven-compiler-plugin + 3.8.1 1.8 1.8