Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
fix!: transform valueHasUri values from node to string type (DEV-1047) (
#2094) * add test data * add plugin and test * add plugin to Upgrade plan + increase knora-base version * cleanup
- Loading branch information
Showing
8 changed files
with
121 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,7 +36,7 @@ sipi/test | |
*.bak | ||
*.rdb | ||
.sbtrc | ||
**/*.trig | ||
/*.trig | ||
|
||
dependencies.txt | ||
/client-test-data.zip | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . | ||
@prefix knora-base: <http://www.knora.org/ontology/knora-base#> . | ||
|
||
# bad valueHasUri value and missing datatype | ||
<http://rdfh.ch/0103/5LE8P57nROClWUxEPJhiug/values/fEbt5NzaSe6GnCqKoF4Nhg/standoff/2> | ||
rdf:type knora-base:StandoffUriTag ; | ||
knora-base:standoffTagHasEnd 50 ; | ||
knora-base:standoffTagHasStart 20 ; | ||
knora-base:standoffTagHasStartIndex 2 ; | ||
knora-base:standoffTagHasStartParent <http://rdfh.ch/0103/5LE8P57nROClWUxEPJhiug/values/fEbt5NzaSe6GnCqKoF4Nhg/standoff/1> ; | ||
knora-base:standoffTagHasUUID "dd2e785b-041e-4bcb-a77d-570efe8a068c" ; | ||
knora-base:valueHasUri <http://www.maison-george-sand.fr/> . |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
...c/main/scala/org/knora/webapi/store/triplestore/upgrade/plugins/UpgradePluginPR2094.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright © 2021 - 2022 Swiss National Data and Service Center for the Humanities and/or DaSCH Service Platform contributors. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.knora.webapi.store.triplestore.upgrade.plugins | ||
|
||
import com.typesafe.scalalogging.Logger | ||
import org.knora.webapi.messages.OntologyConstants | ||
import org.knora.webapi.messages.util.rdf._ | ||
import org.knora.webapi.store.triplestore.upgrade.UpgradePlugin | ||
|
||
/** | ||
* Transforms a repository for Knora PR 2094. | ||
* Transforms incorrect value of valueHasUri from node to string type adding also | ||
* missing datatype ^^<http://www.w3.org/2001/XMLSchema#anyURI> | ||
*/ | ||
class UpgradePluginPR2094(log: Logger) extends UpgradePlugin { | ||
private val nodeFactory: RdfNodeFactory = RdfFeatureFactory.getRdfNodeFactory() | ||
|
||
override def transform(model: RdfModel): Unit = { | ||
val statementsToRemove: collection.mutable.Set[Statement] = collection.mutable.Set.empty | ||
val statementsToAdd: collection.mutable.Set[Statement] = collection.mutable.Set.empty | ||
val newObjectValue: String => DatatypeLiteral = (value: String) => | ||
nodeFactory.makeDatatypeLiteral(value, OntologyConstants.Xsd.Uri) | ||
|
||
for (statement: Statement <- model) { | ||
if (statement.pred.iri == OntologyConstants.KnoraBase.ValueHasUri) { | ||
statement.obj match { | ||
case node: IriNode => | ||
statementsToRemove += statement | ||
|
||
statementsToAdd += nodeFactory.makeStatement( | ||
subj = statement.subj, | ||
pred = statement.pred, | ||
obj = newObjectValue(node.iri), | ||
context = statement.context | ||
) | ||
|
||
log.info( | ||
s"Transformed valueHasUri $node to ${newObjectValue(node.iri)}." | ||
) | ||
|
||
case _ => () | ||
} | ||
} | ||
} | ||
|
||
model.removeStatements(statementsToRemove.toSet) | ||
model.addStatements(statementsToAdd.toSet) | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
...st/scala/org/knora/webapi/store/triplestore/upgrade/plugins/UpgradePluginPR2094Spec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright © 2021 - 2022 Swiss National Data and Service Center for the Humanities and/or DaSCH Service Platform contributors. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.knora.webapi.store.triplestore.upgrade.plugins | ||
|
||
import com.typesafe.scalalogging.LazyLogging | ||
import dsp.errors.AssertionException | ||
import org.knora.webapi.messages.OntologyConstants | ||
import org.knora.webapi.messages.util.rdf._ | ||
|
||
class UpgradePluginPR2094Spec extends UpgradePluginSpec with LazyLogging { | ||
private val nodeFactory: RdfNodeFactory = RdfFeatureFactory.getRdfNodeFactory() | ||
|
||
"Upgrade plugin PR2094" should { | ||
"fix the missing valueHasUri datatype" in { | ||
// Parse the input file. | ||
val model: RdfModel = trigFileToModel("../test_data/upgrade/pr2094.trig") | ||
|
||
// Use the plugin to transform the input. | ||
val plugin = new UpgradePluginPR2094(log) | ||
plugin.transform(model) | ||
|
||
// Check that the datatype was fixed. | ||
val subj = | ||
nodeFactory.makeIriNode("http://rdfh.ch/0103/5LE8P57nROClWUxEPJhiug/values/fEbt5NzaSe6GnCqKoF4Nhg/standoff/2") | ||
val pred = nodeFactory.makeIriNode(OntologyConstants.KnoraBase.ValueHasUri) | ||
|
||
model | ||
.find( | ||
subj = Some(subj), | ||
pred = Some(pred), | ||
obj = None | ||
) | ||
.toSet | ||
.headOption match { | ||
case Some(statement: Statement) => | ||
statement.obj match { | ||
case datatypeLiteral: DatatypeLiteral => | ||
assert(datatypeLiteral.datatype == OntologyConstants.Xsd.Uri) | ||
|
||
case other => | ||
throw AssertionException(s"Unexpected object for $pred: $other") | ||
} | ||
|
||
case None => throw AssertionException(s"No statement found with subject $subj and predicate $pred") | ||
} | ||
} | ||
} | ||
} |