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
* 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
- Loading branch information
Showing
7 changed files
with
128 additions
and
4 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
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,15 @@ | ||
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . | ||
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> . | ||
@prefix knora-base: <http://www.knora.org/ontology/knora-base#> . | ||
|
||
# value with missing valueHasUri datatype | ||
<http://rdfh.ch/0103/fN89IUgvSSyMxJ7XWssP9w/values/Rl2rfjDlRBWeuRr-EgIgCw> | ||
rdf:type knora-base:UriValue ; | ||
knora-base:attachedToUser <http://rdfh.ch/users/user-unil-valentina-ponzetto> ; | ||
knora-base:hasPermissions "CR knora-admin:ProjectAdmin|D knora-admin:Creator|M knora-admin:ProjectMember|V knora-admin:KnownUser,knora-admin:UnknownUser" ; | ||
knora-base:isDeleted false ; | ||
knora-base:valueCreationDate "2018-01-12T19:21:37.991+00:00"^^xsd:dateTime ; | ||
knora-base:valueHasOrder 1 ; | ||
knora-base:valueHasString "https://viaf.org/viaf/2571146" ; | ||
knora-base:valueHasUUID "PYQ5mqH8TYOKCytMozecWw" ; | ||
knora-base:valueHasUri "https://viaf.org/viaf/2571146" . |
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
54 changes: 54 additions & 0 deletions
54
...c/main/scala/org/knora/webapi/store/triplestore/upgrade/plugins/UpgradePluginPR2079.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,54 @@ | ||
/* | ||
* 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.feature.FeatureFactoryConfig | ||
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 2079. | ||
* Adds missing datatype ^^<http://www.w3.org/2001/XMLSchema#anyURI> and/or value to valueHasUri | ||
*/ | ||
class UpgradePluginPR2079(featureFactoryConfig: FeatureFactoryConfig, log: Logger) extends UpgradePlugin { | ||
private val nodeFactory: RdfNodeFactory = RdfFeatureFactory.getRdfNodeFactory(featureFactoryConfig) | ||
|
||
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 literal: DatatypeLiteral => | ||
if (literal.datatype != OntologyConstants.Xsd.Uri) { | ||
statementsToRemove += statement | ||
|
||
statementsToAdd += nodeFactory.makeStatement( | ||
subj = statement.subj, | ||
pred = statement.pred, | ||
obj = newObjectValue(literal.value), | ||
context = statement.context | ||
) | ||
|
||
log.info( | ||
s"Transformed valueHasIri: $literal to ${newObjectValue(literal.value)}." | ||
) | ||
} | ||
|
||
case _ => () | ||
} | ||
} | ||
} | ||
|
||
model.removeStatements(statementsToRemove.toSet) | ||
model.addStatements(statementsToAdd.toSet) | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
...st/scala/org/knora/webapi/store/triplestore/upgrade/plugins/UpgradePluginPR2079Spec.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,50 @@ | ||
/* | ||
* 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 UpgradePluginPR2079Spec extends UpgradePluginSpec with LazyLogging { | ||
private val nodeFactory: RdfNodeFactory = RdfFeatureFactory.getRdfNodeFactory(defaultFeatureFactoryConfig) | ||
|
||
"Upgrade plugin PR2079" should { | ||
"fix the missing valueHasUri datatype" in { | ||
// Parse the input file. | ||
val model: RdfModel = trigFileToModel("../test_data/upgrade/pr2079.trig") | ||
|
||
// Use the plugin to transform the input. | ||
val plugin = new UpgradePluginPR2079(defaultFeatureFactoryConfig, log) | ||
plugin.transform(model) | ||
|
||
// Check that the datatype was fixed. | ||
val subj = nodeFactory.makeIriNode("http://rdfh.ch/0103/fN89IUgvSSyMxJ7XWssP9w/values/Rl2rfjDlRBWeuRr-EgIgCw") | ||
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") | ||
} | ||
} | ||
} | ||
} |