Skip to content

Commit 1039197

Browse files
Var to var references - reshape (#32)
* Set theme jekyll-theme-slate * Create index.md Initialise GH pages * Add variable references * Infer references from all var to var relationships
1 parent 59f7b4e commit 1039197

32 files changed

+598
-177
lines changed

binary-array-ld-cli/src/test/kotlin/net/bald/BinaryArrayConvertCliTest.kt

Lines changed: 96 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ class BinaryArrayConvertCliTest {
339339
* Requirements class E-1, E-2
340340
*/
341341
@Test
342-
fun run_withVariableReferenceAttributes_outputsVariableReferences() {
342+
fun run_withVariableValueAttributes_outputsVariableValues() {
343343
val inputFile = writeToNetCdf("/netcdf/var-ref.cdl")
344344
val outputFile = createTempFile()
345345
val contextFile = ResourceFileConverter.toFile("/jsonld/context.json")
@@ -393,9 +393,9 @@ class BinaryArrayConvertCliTest {
393393
}
394394
}
395395
statement(BALD.contains, createResource("http://test.binary-array-ld.net/example/foo/var1")) {
396+
statement(TestVocab.references, createPlainLiteral("var9"))
396397
statement(TestVocab.siblingVar, createResource("http://test.binary-array-ld.net/example/foo/bar/var2"))
397398
statement(RDF.type, BALD.Resource)
398-
statement(BALD.references, createPlainLiteral("var9"))
399399
}
400400
}
401401
statement(BALD.contains, createResource("http://test.binary-array-ld.net/example/var0")) {
@@ -420,39 +420,37 @@ class BinaryArrayConvertCliTest {
420420
outputFile.absolutePath
421421
)
422422

423-
fun sortAnon(res: Resource): String {
424-
return if (res.hasProperty(BALD.target)) {
425-
res.getProperty(BALD.target).`object`.toString()
426-
} else {
427-
res.id.toString()
428-
}
429-
}
430-
431423
val model = createDefaultModel().read(outputFile.toURI().toString(), "ttl")
432424
ModelVerifier(model).apply {
433425
resource("http://test.binary-array-ld.net/example") {
434426
statement(RDF.type, BALD.Container)
435427
statement(BALD.contains, createResource("http://test.binary-array-ld.net/example/")) {
436428
statement(RDF.type, BALD.Container)
437-
statement(BALD.contains, createResource("http://test.binary-array-ld.net/example/elev"), sortAnon = ::sortAnon) {
429+
statement(BALD.contains, createResource("http://test.binary-array-ld.net/example/elev"), sortAnon = ::sortRefs) {
438430
statement(RDF.type, BALD.Array)
439431
statement(RDFS.label, createPlainLiteral("height"))
440432
statement(BALD.references) {
441433
statement(RDF.type, BALD.Reference)
434+
statement(BALD.sourceRefShape) {
435+
list(15, 10)
436+
}
442437
statement(BALD.target, createResource("http://test.binary-array-ld.net/example/lat"))
443438
statement(BALD.targetRefShape) {
444-
list(createTypedLiteral(15), createTypedLiteral(1))
439+
list(15, 1)
445440
}
446441
}
447442
statement(BALD.references) {
448443
statement(RDF.type, BALD.Reference)
444+
statement(BALD.sourceRefShape) {
445+
list(15, 10)
446+
}
449447
statement(BALD.target, createResource("http://test.binary-array-ld.net/example/lon"))
450448
statement(BALD.targetRefShape) {
451-
list(createTypedLiteral(1), createTypedLiteral(10))
449+
list(1, 10)
452450
}
453451
}
454452
statement(BALD.shape) {
455-
list(createTypedLiteral(15), createTypedLiteral(10))
453+
list(15, 10)
456454
}
457455
}
458456
statement(BALD.contains, createResource("http://test.binary-array-ld.net/example/lat")) {
@@ -461,7 +459,7 @@ class BinaryArrayConvertCliTest {
461459
statement(BALD.arrayFirstValue, createTypedLiteral("6.5", XSDDatatype.XSDfloat))
462460
statement(BALD.arrayLastValue, createTypedLiteral("-6.5", XSDDatatype.XSDfloat))
463461
statement(BALD.shape) {
464-
list(createTypedLiteral(15))
462+
list(15)
465463
}
466464
}
467465
statement(BALD.contains, createResource("http://test.binary-array-ld.net/example/lon")) {
@@ -470,11 +468,93 @@ class BinaryArrayConvertCliTest {
470468
statement(BALD.arrayFirstValue, createTypedLiteral("0.5", XSDDatatype.XSDfloat))
471469
statement(BALD.arrayLastValue, createTypedLiteral("9.5", XSDDatatype.XSDfloat))
472470
statement(BALD.shape) {
473-
list(createTypedLiteral(10))
471+
list(10)
474472
}
475473
}
476474
}
477475
}
478476
}
479477
}
478+
479+
@Test
480+
fun run_withVariableReferences_outputsVariableReferences() {
481+
val inputFile = writeToNetCdf("/netcdf/ref-attr.cdl")
482+
val outputFile = createTempFile()
483+
val aliasFile = ResourceFileConverter.toFile("/turtle/var-alias.ttl", "ttl")
484+
485+
run(
486+
"--uri", "http://test.binary-array-ld.net/example",
487+
"--alias", aliasFile.absolutePath,
488+
inputFile.absolutePath,
489+
outputFile.absolutePath
490+
)
491+
492+
val model = createDefaultModel().read(outputFile.toURI().toString(), "ttl")
493+
ModelVerifier(model).apply {
494+
resource("http://test.binary-array-ld.net/example") {
495+
statement(RDF.type, BALD.Container)
496+
statement(BALD.contains, createResource("http://test.binary-array-ld.net/example/")) {
497+
statement(RDF.type, BALD.Container)
498+
statement(BALD.contains, createResource("http://test.binary-array-ld.net/example/var0"), sortAnon = ::sortRefs) {
499+
statement(RDF.type, BALD.Array)
500+
statement(BALD.references) {
501+
statement(RDF.type, BALD.Reference)
502+
statement(BALD.sourceRefShape) {
503+
list(10, 90, 15, 1)
504+
}
505+
statement(BALD.target, createResource("http://test.binary-array-ld.net/example/var1"))
506+
statement(BALD.targetRefShape) {
507+
list(1, 90, 15, 60)
508+
}
509+
}
510+
statement(BALD.references) {
511+
statement(RDF.type, BALD.Reference)
512+
statement(BALD.sourceRefShape) {
513+
list(10, 90, 15)
514+
}
515+
statement(BALD.target, createResource("http://test.binary-array-ld.net/example/var2"))
516+
statement(BALD.targetRefShape) {
517+
list(1, 1, 15)
518+
}
519+
}
520+
statement(BALD.shape) {
521+
list(10, 90, 15)
522+
}
523+
}
524+
statement(BALD.contains, createResource("http://test.binary-array-ld.net/example/var1")) {
525+
statement(TestVocab.references, createResource("http://test.binary-array-ld.net/example/var2"))
526+
statement(RDF.type, BALD.Array)
527+
statement(BALD.references) {
528+
statement(RDF.type, BALD.Reference)
529+
statement(BALD.sourceRefShape) {
530+
list(90, 15, 60)
531+
}
532+
statement(BALD.target, createResource("http://test.binary-array-ld.net/example/var2"))
533+
statement(BALD.targetRefShape) {
534+
list(1, 15, 1)
535+
}
536+
}
537+
statement(BALD.shape) {
538+
list(90, 15, 60)
539+
}
540+
}
541+
statement(BALD.contains, createResource("http://test.binary-array-ld.net/example/var2")) {
542+
statement(RDF.type, BALD.Array)
543+
statement(BALD.shape) {
544+
list(15)
545+
}
546+
}
547+
statement(BALD.isPrefixedBy, createPlainLiteral("prefix_list"))
548+
}
549+
}
550+
}
551+
}
552+
553+
private fun sortRefs(res: Resource): String {
554+
return if (res.hasProperty(BALD.target)) {
555+
res.getProperty(BALD.target).`object`.toString()
556+
} else {
557+
res.id.toString()
558+
}
559+
}
480560
}

binary-array-ld-lib/src/main/kotlin/net/bald/Attribute.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ interface Attribute {
1414
/**
1515
* The values of the attribute, expressed as RDF resource or literal nodes.
1616
*/
17-
val values: List<RDFNode>
17+
val values: Sequence<RDFNode>
1818
}

binary-array-ld-lib/src/main/kotlin/net/bald/AttributeSource.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ interface AttributeSource {
88
* Obtain the list of attributes that describe this entity.
99
* @return The list of attributes.
1010
*/
11-
fun attributes(): List<Attribute>
11+
fun attributes(): Sequence<Attribute>
1212
}

binary-array-ld-lib/src/main/kotlin/net/bald/Dimension.kt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@ package net.bald
55
*/
66
interface Dimension {
77
/**
8-
* The size of the dimension.
8+
* The uniquely identifying name of the dimension in the binary array.
99
*/
10-
val size: Int
10+
val name: String
1111

1212
/**
13-
* The coordinate variable that corresponds to the dimension, if one exists.
14-
* Otherwise, null.
13+
* The size of the dimension.
1514
*/
16-
val coordinate: Var?
15+
val size: Int
1716
}

binary-array-ld-lib/src/main/kotlin/net/bald/Var.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,9 @@ interface Var: AttributeSource {
1818
* The dimensions that specify the shape of the variable.
1919
*/
2020
fun dimensions(): Sequence<Dimension>
21+
22+
/**
23+
* The other variables which the variable references.
24+
*/
25+
fun references(): Sequence<Var>
2126
}

binary-array-ld-lib/src/main/kotlin/net/bald/model/ModelBinaryArrayConverter.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import org.apache.jena.shared.PrefixMapping
1111
object ModelBinaryArrayConverter {
1212
private val modelFct = run {
1313
val attrFct = ModelAttributeBuilder.Factory()
14-
val varFct = ModelVarBuilder.Factory(attrFct)
14+
val refFct = ModelReferenceBuilder.Factory()
15+
val varFct = ModelVarBuilder.Factory(attrFct, refFct)
1516
val containerFct = ModelContainerBuilder.Factory(varFct, attrFct)
1617
ModelBinaryArrayBuilder.Factory(containerFct)
1718
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package net.bald.model
2+
3+
import net.bald.Dimension
4+
import net.bald.Var
5+
import net.bald.vocab.BALD
6+
import org.apache.jena.rdf.model.Literal
7+
import org.apache.jena.rdf.model.Resource
8+
import org.apache.jena.rdf.model.ResourceFactory
9+
import org.apache.jena.vocabulary.RDF
10+
11+
class ModelReferenceBuilder(
12+
v: Var,
13+
private val vRes: Resource
14+
) {
15+
private val sourceShape = Shape(v)
16+
private val model = vRes.model
17+
18+
fun addReference(target: Var) {
19+
val targetShape = Shape(target)
20+
val dimIds = (sourceShape.dims + targetShape.dims).map(Dimension::name).distinct()
21+
val sourceRefShape = sourceShape.shape(dimIds).let(model::createList)
22+
val targetRefShape = targetShape.shape(dimIds).let(model::createList)
23+
val targetRes = target.uri.let(model::createResource)
24+
25+
val reference = model.createResource()
26+
.addProperty(RDF.type, BALD.Reference)
27+
.addProperty(BALD.sourceRefShape, sourceRefShape)
28+
.addProperty(BALD.targetRefShape, targetRefShape)
29+
.addProperty(BALD.target, targetRes)
30+
31+
vRes.addProperty(BALD.references, reference)
32+
}
33+
34+
class Shape(
35+
val dims: List<Dimension>
36+
) {
37+
constructor(v: Var): this(v.dimensions().toList())
38+
39+
private val dimsById = dims.associateBy(Dimension::name)
40+
41+
fun shape(dimIds: List<String>): Iterator<Literal> {
42+
return dimIds.asSequence().map { dimId ->
43+
dimsById[dimId]?.size?.let(ResourceFactory::createTypedLiteral) ?: unitNode
44+
}.iterator()
45+
}
46+
}
47+
48+
companion object {
49+
private val unitNode = ResourceFactory.createTypedLiteral(1)
50+
}
51+
52+
open class Factory {
53+
open fun forVar(v: Var, vRes: Resource): ModelReferenceBuilder {
54+
return ModelReferenceBuilder(v, vRes)
55+
}
56+
}
57+
}

0 commit comments

Comments
 (0)