Skip to content

Commit e08a5d5

Browse files
Refactor URI responsibility onto Container and Var interfaces
1 parent 9c4d7ad commit e08a5d5

File tree

16 files changed

+114
-69
lines changed

16 files changed

+114
-69
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package net.bald
22

3+
import org.apache.jena.shared.PrefixMapping
4+
35
/**
46
* An entity which is described by its [Attribute]s.
57
*/
@@ -9,5 +11,5 @@ interface AttributeSource {
911
* @param prefixMapping The prefix mapping to use to expand compact URIs.
1012
* @return The list of attributes.
1113
*/
12-
fun attributes(prefixMapping: org.apache.jena.shared.PrefixMapping): List<Attribute>
14+
fun attributes(prefixMapping: PrefixMapping): List<Attribute>
1315
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@ package net.bald
66
*/
77
interface Container: AttributeSource {
88
/**
9-
* The local name of the container, if it has one.
10-
* The root container may have no name or an empty name.
9+
* The URI of the container.
1110
*/
12-
val name: String?
11+
val uri: String
1312

1413
/**
1514
* Obtain the variables associates with this container.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ package net.bald
55
*/
66
interface Var: AttributeSource {
77
/**
8-
* The local name of the variable.
8+
* The URI of the variable.
99
*/
10-
val name: String
10+
val uri: String
1111
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class AliasContainer(
1010
private val container: Container,
1111
private val alias: AliasDefinition
1212
): AliasAttributeSource(container, alias), Container {
13-
override val name: String? get() = container.name
13+
override val uri: String get() = container.uri
1414

1515
override fun vars(): Sequence<Var> {
1616
return container.vars().map { v ->

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ class AliasVar(
99
private val v: Var,
1010
alias: AliasDefinition
1111
): AliasAttributeSource(v, alias), Var {
12-
override val name: String get() = v.name
12+
override val uri: String get() = v.uri
1313
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ open class ModelContainerBuilder(
1111
private val attrFct: ModelAttributeBuilder.Factory
1212
) {
1313
open fun addContainer(container: Container) {
14-
val containerUri = parent.withTrailingSlash() + (container.name ?: "")
15-
val containerRes = parent.model.createResource(containerUri, BALD.Container)
14+
val containerRes = parent.model.createResource(container.uri, BALD.Container)
1615
addSubContainers(container, containerRes)
1716
addVars(container, containerRes)
1817
addAttributes(container, containerRes)

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ open class ModelVarBuilder(
1010
private val attrFct: ModelAttributeBuilder.Factory
1111
) {
1212
open fun addVar(v: Var) {
13-
val varUri = container.withTrailingSlash() + v.name
14-
val vRes = container.model.createResource(varUri, BALD.Resource)
13+
val vRes = container.model.createResource(v.uri, BALD.Resource)
1514
container.addProperty(BALD.contains, vRes)
1615
addAttributes(v, vRes)
1716
}

binary-array-ld-lib/src/test/kotlin/net/bald/model/ModelBinaryArrayConverterTest.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,18 @@ class ModelBinaryArrayConverterTest {
2323
return ModelBinaryArrayConverter.convert(ba)
2424
}
2525

26-
private fun newVar(name: String): Var {
26+
private fun newVar(uri: String): Var {
2727
return mock {
28-
on { this.name } doReturn name
28+
on { this.uri } doReturn uri
2929
}
3030
}
3131

3232
@Test
3333
fun convert_returnsModel() {
34-
val vars = listOf(newVar("foo"), newVar("bar"), newVar("baz"))
34+
val uri = "http://test.binary-array-ld.net/example"
35+
val vars = listOf(newVar("$uri/foo"), newVar("$uri/bar"), newVar("$uri/baz"))
3536
val root = mock<Container> {
37+
on { this.uri } doReturn "$uri/"
3638
on { vars() } doReturn vars.asSequence()
3739
on { subContainers() } doReturn emptySequence()
3840
}
@@ -41,7 +43,7 @@ class ModelBinaryArrayConverterTest {
4143
.setNsPrefix("skos", SKOS.uri)
4244
.setNsPrefix("dct", DCTerms.NS)
4345
val ba = mock<BinaryArray> {
44-
on { uri } doReturn "http://test.binary-array-ld.net/example"
46+
on { this.uri } doReturn uri
4547
on { this.root } doReturn root
4648
on { prefixMapping } doReturn prefix
4749
}

binary-array-ld-lib/src/test/kotlin/net/bald/model/ModelContainerBuilderTest.kt

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class ModelContainerBuilderTest {
3030
private val vars = listOf<Var>(mock(), mock(), mock())
3131
private val attrs = listOf<Attribute>(mock(), mock(), mock())
3232
private val container = mock<Container> {
33+
on { uri } doReturn "http://test.binary-array-ld.net/example/foo"
3334
on { vars() } doReturn vars.asSequence()
3435
on { attributes(any()) } doReturn attrs
3536
on { subContainers() } doReturn emptySequence()
@@ -39,7 +40,7 @@ class ModelContainerBuilderTest {
3940
fun addContainer_addsContainerToModel() {
4041
builder.addContainer(container)
4142
ResourceVerifier(parent).statements {
42-
statement(BALD.contains, model.createResource("${parent.uri}/")) {
43+
statement(BALD.contains, model.createResource("${parent.uri}/foo")) {
4344
statement(RDF.type, BALD.Container)
4445
}
4546
}
@@ -48,7 +49,7 @@ class ModelContainerBuilderTest {
4849
@Test
4950
fun addContainer_addsVars() {
5051
builder.addContainer(container)
51-
verify(varFct).forContainer(model.createResource("${parent.uri}/"))
52+
verify(varFct).forContainer(model.createResource("${parent.uri}/foo"))
5253
verify(varBuilder).addVar(vars[0])
5354
verify(varBuilder).addVar(vars[1])
5455
verify(varBuilder).addVar(vars[2])
@@ -58,24 +59,9 @@ class ModelContainerBuilderTest {
5859
fun addContainer_addsAttributes() {
5960
builder.addContainer(container)
6061
verify(container).attributes(model)
61-
verify(attrFct).forResource(model.createResource("${parent.uri}/"))
62+
verify(attrFct).forResource(model.createResource("${parent.uri}/foo"))
6263
verify(attrBuilder).addAttribute(attrs[0])
6364
verify(attrBuilder).addAttribute(attrs[1])
6465
verify(attrBuilder).addAttribute(attrs[2])
6566
}
66-
67-
@Test
68-
fun addContainer_parentWithTrailingSlash_addsContainerToModel() {
69-
val parent = model.createResource("http://test.binary-array-ld.net/example/")
70-
container.stub {
71-
on { name } doReturn "foo"
72-
}
73-
74-
ModelContainerBuilder.Factory(varFct, attrFct).forParent(parent).addContainer(container)
75-
ResourceVerifier(parent).statements {
76-
statement(BALD.contains, model.createResource("${parent.uri}foo")) {
77-
statement(RDF.type, BALD.Container)
78-
}
79-
}
80-
}
8167
}

binary-array-ld-lib/src/test/kotlin/net/bald/model/ModelVarBuilderTest.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,16 @@ class ModelVarBuilderTest {
2222
}
2323
private val builder = ModelVarBuilder.Factory(attrFct).forContainer(container)
2424

25-
private fun newVar(name: String, attrs: List<Attribute> = emptyList()): Var {
25+
private fun newVar(uri: String, attrs: List<Attribute> = emptyList()): Var {
2626
return mock {
27-
on { this.name } doReturn name
27+
on { this.uri } doReturn uri
2828
on { attributes(any()) } doReturn attrs
2929
}
3030
}
3131

3232
@Test
3333
fun addVar_addsResourceToContainer() {
34-
val v = newVar("foo")
34+
val v = newVar("http://test.binary-array-ld.net/example/foo")
3535
builder.addVar(v)
3636

3737
ResourceVerifier(container).statements {
@@ -43,9 +43,9 @@ class ModelVarBuilderTest {
4343

4444
@Test
4545
fun addVar_multiple_addsResourcesToContainer() {
46-
val v1 = newVar("foo")
47-
val v2 = newVar("bar")
48-
val v3 = newVar("baz")
46+
val v1 = newVar("http://test.binary-array-ld.net/example/foo")
47+
val v2 = newVar("http://test.binary-array-ld.net/example/bar")
48+
val v3 = newVar("http://test.binary-array-ld.net/example/baz")
4949

5050
builder.addVar(v1)
5151
builder.addVar(v2)
@@ -68,7 +68,7 @@ class ModelVarBuilderTest {
6868
fun addVar_containerWithoutTrailingSlash_addsResourceToContainer() {
6969
val container = model.createResource("http://test.binary-array-ld.net/example")
7070

71-
val v = mock<Var> { on { name } doReturn "foo" }
71+
val v = mock<Var> { on { uri } doReturn "http://test.binary-array-ld.net/example/foo" }
7272
ModelVarBuilder.Factory(attrFct).forContainer(container).addVar(v)
7373

7474
ResourceVerifier(container).statements {
@@ -84,7 +84,7 @@ class ModelVarBuilderTest {
8484
mock { on { name } doReturn "type" },
8585
mock { on { name } doReturn "label" }
8686
)
87-
val v = newVar("foo", attrs)
87+
val v = newVar("http://test.binary-array-ld.net/example/foo", attrs)
8888
builder.addVar(v)
8989

9090
verify(v).attributes(model)

0 commit comments

Comments
 (0)