Skip to content

Commit de1e5a4

Browse files
authored
fix!: fix valueHasUri bad values and missing types (DEV-1036) (#2079)
* add plugin, test and test data * add another case to fix * fix filenames * add logging & plugin to repo upgrade plan * cleanup * review changes * fix knora-base version * remove the standoff case
1 parent ef3634e commit de1e5a4

7 files changed

Lines changed: 128 additions & 4 deletions

File tree

‎.gitignore‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ sipi/test
2121
/docs/architecture/.structurizr
2222
/docs/architecture/workspace.json
2323

24+
2425
**/target/
2526
*.aux
2627
*.bbl
@@ -35,15 +36,14 @@ sipi/test
3536
*.bak
3637
*.rdb
3738
.sbtrc
38-
.trig
39+
*.trig
3940

4041
dependencies.txt
4142
/client-test-data.zip
4243
/sipi/images/082E/*
4344
/sipi/images/originals/082E/*
4445
sipi/images/1111/*
4546
sipi/images/originals/1111/*
46-
/dependencies.bzl
4747

4848
.idea/
4949
.metals/

‎knora-ontologies/knora-base.ttl‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
rdf:type owl:Ontology ;
2020
rdfs:label "The Knora base ontology"@en ;
2121
:attachedToProject knora-admin:SystemProject ;
22-
:ontologyVersion "knora-base v20" .
22+
:ontologyVersion "knora-base v21" .
2323

2424

2525
#################################################################

‎test_data/upgrade/pr2079.trig‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
2+
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
3+
@prefix knora-base: <http://www.knora.org/ontology/knora-base#> .
4+
5+
# value with missing valueHasUri datatype
6+
<http://rdfh.ch/0103/fN89IUgvSSyMxJ7XWssP9w/values/Rl2rfjDlRBWeuRr-EgIgCw>
7+
rdf:type knora-base:UriValue ;
8+
knora-base:attachedToUser <http://rdfh.ch/users/user-unil-valentina-ponzetto> ;
9+
knora-base:hasPermissions "CR knora-admin:ProjectAdmin|D knora-admin:Creator|M knora-admin:ProjectMember|V knora-admin:KnownUser,knora-admin:UnknownUser" ;
10+
knora-base:isDeleted false ;
11+
knora-base:valueCreationDate "2018-01-12T19:21:37.991+00:00"^^xsd:dateTime ;
12+
knora-base:valueHasOrder 1 ;
13+
knora-base:valueHasString "https://viaf.org/viaf/2571146" ;
14+
knora-base:valueHasUUID "PYQ5mqH8TYOKCytMozecWw" ;
15+
knora-base:valueHasUri "https://viaf.org/viaf/2571146" .

‎webapi/src/main/scala/org/knora/webapi/package.scala‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ package object webapi {
1111
* The version of `knora-base` and of the other built-in ontologies that this version of Knora requires.
1212
* Must be the same as the object of `knora-base:ontologyVersion` in the `knora-base` ontology being used.
1313
*/
14-
val KnoraBaseVersion: String = "knora-base v20"
14+
val KnoraBaseVersion: String = "knora-base v21"
1515

1616
/**
1717
* `IRI` is a synonym for `String`, used to improve code readability.

‎webapi/src/main/scala/org/knora/webapi/store/triplestore/upgrade/RepositoryUpdatePlan.scala‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ object RepositoryUpdatePlan {
5454
versionNumber = 20,
5555
plugin = new UpgradePluginPR2018(featureFactoryConfig, log),
5656
prBasedVersionString = Some("PR 2018")
57+
),
58+
PluginForKnoraBaseVersion(
59+
versionNumber = 21,
60+
plugin = new UpgradePluginPR2079(featureFactoryConfig, log),
61+
prBasedVersionString = Some("PR 2079")
5762
)
5863
)
5964

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright © 2021 - 2022 Swiss National Data and Service Center for the Humanities and/or DaSCH Service Platform contributors.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package org.knora.webapi.store.triplestore.upgrade.plugins
7+
8+
import com.typesafe.scalalogging.Logger
9+
import org.knora.webapi.feature.FeatureFactoryConfig
10+
import org.knora.webapi.messages.OntologyConstants
11+
import org.knora.webapi.messages.util.rdf._
12+
import org.knora.webapi.store.triplestore.upgrade.UpgradePlugin
13+
14+
/**
15+
* Transforms a repository for Knora PR 2079.
16+
* Adds missing datatype ^^<http://www.w3.org/2001/XMLSchema#anyURI> and/or value to valueHasUri
17+
*/
18+
class UpgradePluginPR2079(featureFactoryConfig: FeatureFactoryConfig, log: Logger) extends UpgradePlugin {
19+
private val nodeFactory: RdfNodeFactory = RdfFeatureFactory.getRdfNodeFactory(featureFactoryConfig)
20+
21+
override def transform(model: RdfModel): Unit = {
22+
val statementsToRemove: collection.mutable.Set[Statement] = collection.mutable.Set.empty
23+
val statementsToAdd: collection.mutable.Set[Statement] = collection.mutable.Set.empty
24+
val newObjectValue: String => DatatypeLiteral = (value: String) =>
25+
nodeFactory.makeDatatypeLiteral(value, OntologyConstants.Xsd.Uri)
26+
27+
for (statement: Statement <- model) {
28+
if (statement.pred.iri == OntologyConstants.KnoraBase.ValueHasUri) {
29+
statement.obj match {
30+
case literal: DatatypeLiteral =>
31+
if (literal.datatype != OntologyConstants.Xsd.Uri) {
32+
statementsToRemove += statement
33+
34+
statementsToAdd += nodeFactory.makeStatement(
35+
subj = statement.subj,
36+
pred = statement.pred,
37+
obj = newObjectValue(literal.value),
38+
context = statement.context
39+
)
40+
41+
log.info(
42+
s"Transformed valueHasIri: $literal to ${newObjectValue(literal.value)}."
43+
)
44+
}
45+
46+
case _ => ()
47+
}
48+
}
49+
}
50+
51+
model.removeStatements(statementsToRemove.toSet)
52+
model.addStatements(statementsToAdd.toSet)
53+
}
54+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright © 2021 - 2022 Swiss National Data and Service Center for the Humanities and/or DaSCH Service Platform contributors.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package org.knora.webapi.store.triplestore.upgrade.plugins
7+
8+
import com.typesafe.scalalogging.LazyLogging
9+
import dsp.errors.AssertionException
10+
import org.knora.webapi.messages.OntologyConstants
11+
import org.knora.webapi.messages.util.rdf._
12+
13+
class UpgradePluginPR2079Spec extends UpgradePluginSpec with LazyLogging {
14+
private val nodeFactory: RdfNodeFactory = RdfFeatureFactory.getRdfNodeFactory(defaultFeatureFactoryConfig)
15+
16+
"Upgrade plugin PR2079" should {
17+
"fix the missing valueHasUri datatype" in {
18+
// Parse the input file.
19+
val model: RdfModel = trigFileToModel("../test_data/upgrade/pr2079.trig")
20+
21+
// Use the plugin to transform the input.
22+
val plugin = new UpgradePluginPR2079(defaultFeatureFactoryConfig, log)
23+
plugin.transform(model)
24+
25+
// Check that the datatype was fixed.
26+
val subj = nodeFactory.makeIriNode("http://rdfh.ch/0103/fN89IUgvSSyMxJ7XWssP9w/values/Rl2rfjDlRBWeuRr-EgIgCw")
27+
val pred = nodeFactory.makeIriNode(OntologyConstants.KnoraBase.ValueHasUri)
28+
29+
model
30+
.find(
31+
subj = Some(subj),
32+
pred = Some(pred),
33+
obj = None
34+
)
35+
.toSet
36+
.headOption match {
37+
case Some(statement: Statement) =>
38+
statement.obj match {
39+
case datatypeLiteral: DatatypeLiteral =>
40+
assert(datatypeLiteral.datatype == OntologyConstants.Xsd.Uri)
41+
42+
case other =>
43+
throw AssertionException(s"Unexpected object for $pred: $other")
44+
}
45+
46+
case None => throw AssertionException(s"No statement found with subject $subj and predicate $pred")
47+
}
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)