Skip to content

Commit

Permalink
Merge branch 'master' of github.com:binary-array-ld/net.binary_array_…
Browse files Browse the repository at this point in the history
…ld.bald
  • Loading branch information
simonoakesepimorphics committed Jun 1, 2021
2 parents d90179c + 1039197 commit 5d2f9b4
Show file tree
Hide file tree
Showing 32 changed files with 598 additions and 177 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ class BinaryArrayConvertCliTest {
* Requirements class E-1, E-2
*/
@Test
fun run_withVariableReferenceAttributes_outputsVariableReferences() {
fun run_withVariableValueAttributes_outputsVariableValues() {
val inputFile = writeToNetCdf("/netcdf/var-ref.cdl")
val outputFile = createTempFile()
val contextFile = ResourceFileConverter.toFile("/jsonld/context.json")
Expand Down Expand Up @@ -393,9 +393,9 @@ class BinaryArrayConvertCliTest {
}
}
statement(BALD.contains, createResource("http://test.binary-array-ld.net/example/foo/var1")) {
statement(TestVocab.references, createPlainLiteral("var9"))
statement(TestVocab.siblingVar, createResource("http://test.binary-array-ld.net/example/foo/bar/var2"))
statement(RDF.type, BALD.Resource)
statement(BALD.references, createPlainLiteral("var9"))
}
}
statement(BALD.contains, createResource("http://test.binary-array-ld.net/example/var0")) {
Expand All @@ -420,39 +420,37 @@ class BinaryArrayConvertCliTest {
outputFile.absolutePath
)

fun sortAnon(res: Resource): String {
return if (res.hasProperty(BALD.target)) {
res.getProperty(BALD.target).`object`.toString()
} else {
res.id.toString()
}
}

val model = createDefaultModel().read(outputFile.toURI().toString(), "ttl")
ModelVerifier(model).apply {
resource("http://test.binary-array-ld.net/example") {
statement(RDF.type, BALD.Container)
statement(BALD.contains, createResource("http://test.binary-array-ld.net/example/")) {
statement(RDF.type, BALD.Container)
statement(BALD.contains, createResource("http://test.binary-array-ld.net/example/elev"), sortAnon = ::sortAnon) {
statement(BALD.contains, createResource("http://test.binary-array-ld.net/example/elev"), sortAnon = ::sortRefs) {
statement(RDF.type, BALD.Array)
statement(RDFS.label, createPlainLiteral("height"))
statement(BALD.references) {
statement(RDF.type, BALD.Reference)
statement(BALD.sourceRefShape) {
list(15, 10)
}
statement(BALD.target, createResource("http://test.binary-array-ld.net/example/lat"))
statement(BALD.targetRefShape) {
list(createTypedLiteral(15), createTypedLiteral(1))
list(15, 1)
}
}
statement(BALD.references) {
statement(RDF.type, BALD.Reference)
statement(BALD.sourceRefShape) {
list(15, 10)
}
statement(BALD.target, createResource("http://test.binary-array-ld.net/example/lon"))
statement(BALD.targetRefShape) {
list(createTypedLiteral(1), createTypedLiteral(10))
list(1, 10)
}
}
statement(BALD.shape) {
list(createTypedLiteral(15), createTypedLiteral(10))
list(15, 10)
}
}
statement(BALD.contains, createResource("http://test.binary-array-ld.net/example/lat")) {
Expand All @@ -461,7 +459,7 @@ class BinaryArrayConvertCliTest {
statement(BALD.arrayFirstValue, createTypedLiteral("6.5", XSDDatatype.XSDfloat))
statement(BALD.arrayLastValue, createTypedLiteral("-6.5", XSDDatatype.XSDfloat))
statement(BALD.shape) {
list(createTypedLiteral(15))
list(15)
}
}
statement(BALD.contains, createResource("http://test.binary-array-ld.net/example/lon")) {
Expand All @@ -470,11 +468,93 @@ class BinaryArrayConvertCliTest {
statement(BALD.arrayFirstValue, createTypedLiteral("0.5", XSDDatatype.XSDfloat))
statement(BALD.arrayLastValue, createTypedLiteral("9.5", XSDDatatype.XSDfloat))
statement(BALD.shape) {
list(createTypedLiteral(10))
list(10)
}
}
}
}
}
}

@Test
fun run_withVariableReferences_outputsVariableReferences() {
val inputFile = writeToNetCdf("/netcdf/ref-attr.cdl")
val outputFile = createTempFile()
val aliasFile = ResourceFileConverter.toFile("/turtle/var-alias.ttl", "ttl")

run(
"--uri", "http://test.binary-array-ld.net/example",
"--alias", aliasFile.absolutePath,
inputFile.absolutePath,
outputFile.absolutePath
)

val model = createDefaultModel().read(outputFile.toURI().toString(), "ttl")
ModelVerifier(model).apply {
resource("http://test.binary-array-ld.net/example") {
statement(RDF.type, BALD.Container)
statement(BALD.contains, createResource("http://test.binary-array-ld.net/example/")) {
statement(RDF.type, BALD.Container)
statement(BALD.contains, createResource("http://test.binary-array-ld.net/example/var0"), sortAnon = ::sortRefs) {
statement(RDF.type, BALD.Array)
statement(BALD.references) {
statement(RDF.type, BALD.Reference)
statement(BALD.sourceRefShape) {
list(10, 90, 15, 1)
}
statement(BALD.target, createResource("http://test.binary-array-ld.net/example/var1"))
statement(BALD.targetRefShape) {
list(1, 90, 15, 60)
}
}
statement(BALD.references) {
statement(RDF.type, BALD.Reference)
statement(BALD.sourceRefShape) {
list(10, 90, 15)
}
statement(BALD.target, createResource("http://test.binary-array-ld.net/example/var2"))
statement(BALD.targetRefShape) {
list(1, 1, 15)
}
}
statement(BALD.shape) {
list(10, 90, 15)
}
}
statement(BALD.contains, createResource("http://test.binary-array-ld.net/example/var1")) {
statement(TestVocab.references, createResource("http://test.binary-array-ld.net/example/var2"))
statement(RDF.type, BALD.Array)
statement(BALD.references) {
statement(RDF.type, BALD.Reference)
statement(BALD.sourceRefShape) {
list(90, 15, 60)
}
statement(BALD.target, createResource("http://test.binary-array-ld.net/example/var2"))
statement(BALD.targetRefShape) {
list(1, 15, 1)
}
}
statement(BALD.shape) {
list(90, 15, 60)
}
}
statement(BALD.contains, createResource("http://test.binary-array-ld.net/example/var2")) {
statement(RDF.type, BALD.Array)
statement(BALD.shape) {
list(15)
}
}
statement(BALD.isPrefixedBy, createPlainLiteral("prefix_list"))
}
}
}
}

private fun sortRefs(res: Resource): String {
return if (res.hasProperty(BALD.target)) {
res.getProperty(BALD.target).`object`.toString()
} else {
res.id.toString()
}
}
}
2 changes: 1 addition & 1 deletion binary-array-ld-lib/src/main/kotlin/net/bald/Attribute.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ interface Attribute {
/**
* The values of the attribute, expressed as RDF resource or literal nodes.
*/
val values: List<RDFNode>
val values: Sequence<RDFNode>
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ interface AttributeSource {
* Obtain the list of attributes that describe this entity.
* @return The list of attributes.
*/
fun attributes(): List<Attribute>
fun attributes(): Sequence<Attribute>
}
9 changes: 4 additions & 5 deletions binary-array-ld-lib/src/main/kotlin/net/bald/Dimension.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ package net.bald
*/
interface Dimension {
/**
* The size of the dimension.
* The uniquely identifying name of the dimension in the binary array.
*/
val size: Int
val name: String

/**
* The coordinate variable that corresponds to the dimension, if one exists.
* Otherwise, null.
* The size of the dimension.
*/
val coordinate: Var?
val size: Int
}
5 changes: 5 additions & 0 deletions binary-array-ld-lib/src/main/kotlin/net/bald/Var.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,9 @@ interface Var: AttributeSource {
* The dimensions that specify the shape of the variable.
*/
fun dimensions(): Sequence<Dimension>

/**
* The other variables which the variable references.
*/
fun references(): Sequence<Var>
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import org.apache.jena.shared.PrefixMapping
object ModelBinaryArrayConverter {
private val modelFct = run {
val attrFct = ModelAttributeBuilder.Factory()
val varFct = ModelVarBuilder.Factory(attrFct)
val refFct = ModelReferenceBuilder.Factory()
val varFct = ModelVarBuilder.Factory(attrFct, refFct)
val containerFct = ModelContainerBuilder.Factory(varFct, attrFct)
ModelBinaryArrayBuilder.Factory(containerFct)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package net.bald.model

import net.bald.Dimension
import net.bald.Var
import net.bald.vocab.BALD
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.vocabulary.RDF

class ModelReferenceBuilder(
v: Var,
private val vRes: Resource
) {
private val sourceShape = Shape(v)
private val model = vRes.model

fun addReference(target: Var) {
val targetShape = Shape(target)
val dimIds = (sourceShape.dims + targetShape.dims).map(Dimension::name).distinct()
val sourceRefShape = sourceShape.shape(dimIds).let(model::createList)
val targetRefShape = targetShape.shape(dimIds).let(model::createList)
val targetRes = target.uri.let(model::createResource)

val reference = model.createResource()
.addProperty(RDF.type, BALD.Reference)
.addProperty(BALD.sourceRefShape, sourceRefShape)
.addProperty(BALD.targetRefShape, targetRefShape)
.addProperty(BALD.target, targetRes)

vRes.addProperty(BALD.references, reference)
}

class Shape(
val dims: List<Dimension>
) {
constructor(v: Var): this(v.dimensions().toList())

private val dimsById = dims.associateBy(Dimension::name)

fun shape(dimIds: List<String>): Iterator<Literal> {
return dimIds.asSequence().map { dimId ->
dimsById[dimId]?.size?.let(ResourceFactory::createTypedLiteral) ?: unitNode
}.iterator()
}
}

companion object {
private val unitNode = ResourceFactory.createTypedLiteral(1)
}

open class Factory {
open fun forVar(v: Var, vRes: Resource): ModelReferenceBuilder {
return ModelReferenceBuilder(v, vRes)
}
}
}
Loading

0 comments on commit 5d2f9b4

Please sign in to comment.