Skip to content

Commit f059341

Browse files
Refactor attribute URI responsibility onto interface
Refactor binary array impl to incorporate model context
1 parent 7f46ab1 commit f059341

File tree

24 files changed

+156
-356
lines changed

24 files changed

+156
-356
lines changed

binary-array-ld-cli/src/main/kotlin/net/bald/BinaryArrayConvertCli.kt

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package net.bald
22

3-
import net.bald.alias.AliasBinaryArray
4-
import net.bald.context.ContextBinaryArray
3+
import net.bald.context.ModelContext
54
import net.bald.model.ModelAliasDefinition
65
import net.bald.model.ModelBinaryArrayConverter
76
import net.bald.netcdf.NetCdfBinaryArray
@@ -39,33 +38,25 @@ class BinaryArrayConvertCli {
3938
}
4039

4140
private fun doRun(opts: CommandLineOptions) {
41+
val context = context(opts.contextLocs, opts.aliasLocs)
4242
val inputLoc = opts.inputLoc ?: throw IllegalArgumentException("First argument is required: NetCDF file to convert.")
43-
val ba = NetCdfBinaryArray.create(inputLoc, opts.uri)
44-
.withContext(opts.contextLocs)
45-
.withAlias(opts.aliasLocs)
43+
val ba = NetCdfBinaryArray.create(inputLoc, opts.uri, context)
4644
val model = ba.use(ModelBinaryArrayConverter::convert)
4745

4846
modelOutput(opts.outputLoc).use { output ->
4947
model.write(output, "ttl")
5048
}
5149
}
5250

53-
private fun BinaryArray.withContext(contextLocs: List<String>): BinaryArray {
54-
val contexts = contextLocs.map { contextLoc ->
51+
private fun context(contextLocs: List<String>, aliasLocs: List<String>): ModelContext {
52+
val prefixes = contextLocs.map { contextLoc ->
5553
ModelFactory.createDefaultModel().read(contextLoc, "json-ld")
5654
}
57-
return ContextBinaryArray.create(this, contexts)
58-
}
55+
val alias = ModelFactory.createDefaultModel().apply {
56+
aliasLocs.forEach(::read)
57+
}.let(ModelAliasDefinition::create)
5958

60-
private fun BinaryArray.withAlias(aliasLocs: List<String>): BinaryArray {
61-
return if (aliasLocs.isEmpty()) {
62-
this
63-
} else {
64-
val alias = ModelFactory.createDefaultModel().apply {
65-
aliasLocs.forEach(::read)
66-
}.let(ModelAliasDefinition::create)
67-
AliasBinaryArray.create(this, alias)
68-
}
59+
return ModelContext.create(prefixes, alias)
6960
}
7061

7162
private fun options(opts: Options, vararg args: String): CommandLineOptions {
@@ -88,4 +79,4 @@ fun main(args: Array<String>) {
8879
println("Conversion failed due to error: ${e.message}")
8980
exitProcess(1)
9081
}
91-
}
82+
}

binary-array-ld-demo/src/main/java/net/bald/NetCdfConvertJava.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package net.bald;
22

3-
import net.bald.alias.AliasBinaryArray;
43
import net.bald.context.AliasDefinition;
4+
import net.bald.context.ModelContext;
55
import net.bald.model.ModelAliasDefinition;
66
import net.bald.context.ContextBinaryArray;
77
import net.bald.model.ModelBinaryArrayConverter;
@@ -26,23 +26,25 @@ public static void convert() throws Exception {
2626
}
2727

2828
public static void convertWithExternalPrefixes() throws Exception {
29-
BinaryArray ba = NetCdfBinaryArray.create("/path/to/input.nc", "http://test.binary-array-ld.net/example");
30-
PrefixMapping context = ModelFactory.createDefaultModel().read("/path/to/context.json", "json-ld");
31-
BinaryArray contextBa = ContextBinaryArray.create(ba, context);
32-
Model model = ModelBinaryArrayConverter.convert(contextBa);
29+
PrefixMapping prefix = ModelFactory.createDefaultModel().read("/path/to/context.json", "json-ld");
30+
ModelContext context = ModelContext.create(prefix, null);
31+
BinaryArray ba = NetCdfBinaryArray.create("/path/to/input.nc", "http://test.binary-array-ld.net/example", context);
32+
Model model = ModelBinaryArrayConverter.convert(ba);
3333

3434
try (OutputStream output = new FileOutputStream("/path/to/output.ttl")) {
3535
model.write(output, "ttl");
3636
}
3737
}
3838

3939
public static void convertWithAliases() throws Exception {
40-
BinaryArray ba = NetCdfBinaryArray.create("/path/to/input.nc", "http://test.binary-array-ld.net/example");
40+
PrefixMapping prefix = PrefixMapping.Factory.create();
4141
Model aliasModel = ModelFactory.createDefaultModel().read("/path/to/alias.ttl", "ttl");
4242
AliasDefinition alias = ModelAliasDefinition.create(aliasModel);
43-
BinaryArray aliasBa = AliasBinaryArray.create(ba, alias);
43+
ModelContext context = ModelContext.create(prefix, alias);
4444

45-
Model model = ModelBinaryArrayConverter.convert(aliasBa);
45+
BinaryArray ba = NetCdfBinaryArray.create("/path/to/input.nc", "http://test.binary-array-ld.net/example", context);
46+
47+
Model model = ModelBinaryArrayConverter.convert(ba);
4648

4749
try (OutputStream output = new FileOutputStream("/path/to/output.ttl")) {
4850
model.write(output, "ttl");

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,7 @@ interface Attribute {
99
/**
1010
* The URI which identifies the attribute, if it has one. Otherwise, null.
1111
*/
12-
val uri: String?
13-
14-
/**
15-
* The local name of the attribute.
16-
*/
17-
val name: String
12+
val uri: String
1813

1914
/**
2015
* The values of the attribute, expressed as RDF resource or literal nodes.
Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
package net.bald
22

3-
import org.apache.jena.shared.PrefixMapping
4-
53
/**
64
* An entity which is described by its [Attribute]s.
75
*/
86
interface AttributeSource {
97
/**
108
* Obtain the list of attributes that describe this entity.
11-
* @param prefixMapping The prefix mapping to use to expand compact URIs.
129
* @return The list of attributes.
1310
*/
14-
fun attributes(prefixMapping: PrefixMapping): List<Attribute>
11+
fun attributes(): List<Attribute>
1512
}

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

Lines changed: 0 additions & 27 deletions
This file was deleted.

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

Lines changed: 0 additions & 19 deletions
This file was deleted.

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

Lines changed: 0 additions & 35 deletions
This file was deleted.

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

Lines changed: 0 additions & 27 deletions
This file was deleted.

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

Lines changed: 0 additions & 14 deletions
This file was deleted.

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ open class ModelAttributeBuilder(
77
private val resource: Resource
88
) {
99
open fun addAttribute(attr: Attribute) {
10-
val propUri = attr.uri ?: resource.withTrailingSlash() + attr.name
11-
val prop = resource.model.createProperty(propUri)
10+
val prop = resource.model.createProperty(attr.uri)
1211
attr.values.forEach { value ->
1312
resource.addProperty(prop, value)
1413
}

0 commit comments

Comments
 (0)