Commit 4f3c28ea authored by Jonas Waeber's avatar Jonas Waeber
Browse files

Remove the DUMMY-VALUE requirement from creation relation enrichment.

Improve tests.
parent 137ded76
Pipeline #18485 passed with stage
in 2 minutes and 22 seconds
......@@ -7,7 +7,7 @@ object KEYS {
const val firstToLast = "first-to-last"
const val lastToFirst = "last-to-first"
const val DUMMY_VALUE = "DUMMY-VALUE"
const val extractCreationRelationNamePatternGroupName = "relation"
const val missingLabelDe = "FEHLENDES LABEL"
......
package ch.memobase.model
import ch.memobase.helpers.KEYS.extractCreationRelationNamePatternGroupName
import org.memobase.exceptions.InvalidMappingException
import ch.memobase.transform.ExtractCreationRelationNameTransform
......@@ -12,7 +13,7 @@ data class ExtractCreationRelationName(
)
init {
if (!pattern.contains("(?<relation>")) {
if (!pattern.contains("(?<$extractCreationRelationNamePatternGroupName>")) {
throw InvalidMappingException("The pattern of the 'extractRelationName' transform requires a named group 'relation'.")
}
if (!validLanguageTags.contains(language)) {
......
package ch.memobase.transform
import ch.memobase.helpers.KEYS.extractCreationRelationNamePatternGroupName
import org.apache.logging.log4j.LogManager
import ch.memobase.rdf.MemobaseModel
import org.memobase.rdf.RICO
......@@ -9,11 +10,7 @@ class ExtractCreationRelationNameTransform(private val regex: Regex, private val
private val log = LogManager.getLogger("ExtractCreationRelationName")
override fun transform(item: RicoResource, model: MemobaseModel): List<String> {
// NOTE: If the constant DUMMY-VALUE is changed, it needs to be changed in the mapper service as well
// under ResourceBuilder.addCreationRelation()
if (item.hasType(RICO.CreationRelation) && item.hasProperty(RICO.name, "DUMMY-VALUE")) {
// Remove DUMMY-VALUES regardless of whether a name can be added or not.
item.removeAllProperties(RICO.name, "DUMMY-VALUE")
if (item.hasType(RICO.CreationRelation)) {
val target = item.getResource(RICO.creationRelationHasTarget)
if (target.hasType(RICO.Person) && target.hasProperty(RICO.name)) {
val updatedNames = target.listProperties(RICO.name).mapNotNull { statement ->
......@@ -22,7 +19,7 @@ class ExtractCreationRelationNameTransform(private val regex: Regex, private val
val fullValue = value.string
val match = regex.find(fullValue)
val targetValue = if (match != null) {
match.groups["relation"]?.value.let { relation ->
match.groups[extractCreationRelationNamePatternGroupName]?.value.let { relation ->
if (relation != null) {
// add names to creation relation!
if (value.language.isNullOrEmpty()) {
......
......@@ -17,8 +17,9 @@
*/
package org.memobase
import ch.memobase.helpers.KEYS
import ch.memobase.helpers.KEYS.extractCreationRelationNamePatternGroupName
import ch.memobase.rdf.MemobaseModel
import ch.memobase.rdf.RicoResource
import ch.memobase.transform.ExtractCreationRelationNameTransform
import org.apache.jena.riot.RDFDataMgr
import org.apache.jena.riot.RDFFormat
......@@ -29,6 +30,7 @@ import org.junit.jupiter.api.assertAll
import org.memobase.rdf.NS
import org.memobase.rdf.RICO
import java.io.FileOutputStream
import java.util.function.Consumer
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
internal class TestExtractCreationRelationNameTransform {
......@@ -44,7 +46,6 @@ internal class TestExtractCreationRelationNameTransform {
val resource =
memobaseModel.createRicoResource(RICO.CreationRelation)
.addLiteral(RICO.name, KEYS.DUMMY_VALUE)
.addProperty(RICO.creationRelationHasTarget, person)
person.addProperty(RICO.agentIsTargetOfCreationRelation, resource)
......@@ -56,8 +57,8 @@ internal class TestExtractCreationRelationNameTransform {
.addProperty(RICO.creationRelationHasSource, record)
val n = ExtractCreationRelationNameTransform(
Regex("\\(?<relation>.+\\)"),
"NONE"
Regex("\\((?<$extractCreationRelationNamePatternGroupName>.+)\\)"),
""
)
val output = n.transform(resource, memobaseModel)
RDFDataMgr.write(
......@@ -65,16 +66,74 @@ internal class TestExtractCreationRelationNameTransform {
memobaseModel,
RDFFormat.TURTLE_PRETTY
)
val resourceReqs = Consumer<RicoResource> { r ->
val value = r.getStringLiteral(RICO.name)
assertThat(value)
.isEqualTo("Autor")
val type = r.hasType(RICO.CreationRelation)
assertThat(type).isTrue()
}
assertAll("",
{
assertThat(output).isEmpty()
},
{
assertThat(resource).satisfies(resourceReqs)
}
)
}
@Test
fun `test extract creation relation name normalizer without a match`() {
val memobaseModel = MemobaseModel()
NS.prefixMapping.map {
memobaseModel.setNsPrefix(it.key, it.value)
}
val person = memobaseModel.createRicoResource(RICO.Person)
.addLiteral(RICO.name, "Markus Mäder")
val resource =
memobaseModel.createRicoResource(RICO.CreationRelation)
.addProperty(RICO.creationRelationHasTarget, person)
person.addProperty(RICO.agentIsTargetOfCreationRelation, resource)
val record = memobaseModel.createRicoResource(RICO.Record)
.addProperty(RICO.recordResourceOrInstantiationIsSourceOfCreationRelation, resource)
resource
.addProperty(RICO.creationRelationHasSource, record)
val n = ExtractCreationRelationNameTransform(
Regex("\\((?<$extractCreationRelationNamePatternGroupName>.+)\\)"),
""
)
val output = n.transform(resource, memobaseModel)
RDFDataMgr.write(
FileOutputStream("src/test/resources/tmp/turtle-output-extract-creation-relation-name-without-a-match.ttl"),
memobaseModel,
RDFFormat.TURTLE_PRETTY
)
val resourceReqs = Consumer<RicoResource> { r ->
val value = r.getStringLiteral(RICO.name)
assertThat(value)
.isEqualTo(null)
val type = r.hasType(RICO.CreationRelation)
assertThat(type).isTrue()
}
assertAll("",
{
assertThat(output).isEmpty()
},
{
assertThat(resource).satisfies {
it.hasProperty(RICO.name, "Autor")
}.satisfies {
!it.hasProperty(RICO.name, KEYS.DUMMY_VALUE)
}
assertThat(resource).satisfies(resourceReqs)
}
)
}
......
@prefix schema: <http://schema.org/> .
@prefix internal: <http://memobase.ch/internal/> .
@prefix mbrs: <https://memobase.ch/recordSet/> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix wdt: <http://www.wikidata.org/prop/direct/> .
@prefix mbpo: <https://memobase.ch/physical/> .
@prefix mbcb: <https://memobase.ch/institution/> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix skos: <http://www.w3.org/2004/02/skos/core#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix wd: <http://www.wikidata.org/entity/> .
@prefix wdtn: <http://www.wikidata.org/prop/direct-normalized/> .
@prefix mbdo: <https://memobase.ch/digital/> .
@prefix rdau: <http://rdaregistry.info/Elements/u/> .
@prefix fedora: <http://fedora.info/definitions/v4/repository#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rico: <https://www.ica.org/standards/RiC/ontology#> .
@prefix ebucore: <http://www.ebu.ch/metadata/ontologies/ebucore/ebucore#> .
@prefix ldp: <http://www.w3.org/ns/ldp#> .
@prefix dcterms: <http://purl.org/dc/terms/> .
@prefix mbr: <https://memobase.ch/record/> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix dc: <http://purl.org/dc/elements/1.1/> .
_:b0 a rico:CreationRelation ;
rico:creationRelationHasSource [ a rico:Record ;
rico:recordResourceOrInstantiationIsSourceOfCreationRelation
_:b0
] ;
rico:creationRelationHasTarget [ a rico:Person ;
rico:agentIsTargetOfCreationRelation
_:b0 ;
rico:name "Markus Mäder"
] .
......@@ -30,5 +30,6 @@ _:b0 a rico:CreationRelation ;
rico:creationRelationHasTarget [ a rico:Person ;
rico:agentIsTargetOfCreationRelation
_:b0 ;
rico:name "Markus Mäder (Autor)"
] .
rico:name "Markus Mäder"
] ;
rico:name "Autor" .
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment