Skip to content

Commit

Permalink
refactor: Simplify RDF handling (#2952)
Browse files Browse the repository at this point in the history
Co-authored-by: @mpro7
  • Loading branch information
BalduinLandolt committed Nov 29, 2023
1 parent 5b3436b commit 25099ef
Show file tree
Hide file tree
Showing 44 changed files with 501 additions and 942 deletions.
18 changes: 6 additions & 12 deletions integration/src/test/scala/org/knora/webapi/E2ESpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -160,20 +160,14 @@ abstract class E2ESpec
responseToString(response)
}

protected def parseTrig(trigStr: String): RdfModel = {
val rdfFormatUtil: RdfFormatUtil = RdfFeatureFactory.getRdfFormatUtil()
rdfFormatUtil.parseToRdfModel(rdfStr = trigStr, rdfFormat = TriG)
}
protected def parseTrig(trigStr: String): RdfModel =
RdfFormatUtil.parseToRdfModel(rdfStr = trigStr, rdfFormat = TriG)

protected def parseTurtle(turtleStr: String): RdfModel = {
val rdfFormatUtil: RdfFormatUtil = RdfFeatureFactory.getRdfFormatUtil()
rdfFormatUtil.parseToRdfModel(rdfStr = turtleStr, rdfFormat = Turtle)
}
protected def parseTurtle(turtleStr: String): RdfModel =
RdfFormatUtil.parseToRdfModel(rdfStr = turtleStr, rdfFormat = Turtle)

protected def parseRdfXml(rdfXmlStr: String): RdfModel = {
val rdfFormatUtil: RdfFormatUtil = RdfFeatureFactory.getRdfFormatUtil()
rdfFormatUtil.parseToRdfModel(rdfStr = rdfXmlStr, rdfFormat = RdfXml)
}
protected def parseRdfXml(rdfXmlStr: String): RdfModel =
RdfFormatUtil.parseToRdfModel(rdfStr = rdfXmlStr, rdfFormat = RdfXml)

/**
* Reads or writes a test data file.
Expand Down
12 changes: 4 additions & 8 deletions integration/src/test/scala/org/knora/webapi/R2RSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,11 @@ abstract class R2RSpec
JsonLDUtil.parseJsonLD(responseBodyStr)
}

protected def parseTurtle(turtleStr: String): RdfModel = {
val rdfFormatUtil: RdfFormatUtil = RdfFeatureFactory.getRdfFormatUtil()
rdfFormatUtil.parseToRdfModel(rdfStr = turtleStr, rdfFormat = Turtle)
}
protected def parseTurtle(turtleStr: String): RdfModel =
RdfFormatUtil.parseToRdfModel(rdfStr = turtleStr, rdfFormat = Turtle)

protected def parseRdfXml(rdfXmlStr: String): RdfModel = {
val rdfFormatUtil: RdfFormatUtil = RdfFeatureFactory.getRdfFormatUtil()
rdfFormatUtil.parseToRdfModel(rdfStr = rdfXmlStr, rdfFormat = RdfXml)
}
protected def parseRdfXml(rdfXmlStr: String): RdfModel =
RdfFormatUtil.parseToRdfModel(rdfStr = rdfXmlStr, rdfFormat = RdfXml)

private def adjustFilePath(file: Path): Path =
Paths.get("..", "test_data", "generated_test_data").resolve(file).normalize()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ import org.knora.webapi.util.FileUtil
*/
class JsonLDUtilSpec() extends CoreSpec {

private val rdfFormatUtil: RdfFormatUtil = RdfFeatureFactory.getRdfFormatUtil()
private val rdfModelFactory: RdfModelFactory = RdfFeatureFactory.getRdfModelFactory()

"The JSON-LD tool" should {
"parse JSON-LD text, compact it with an empty context, convert the result to a JsonLDDocument, and convert that back to text" in {
val ontologyJsonLDInputStr =
Expand Down Expand Up @@ -118,7 +115,7 @@ class JsonLDUtilSpec() extends CoreSpec {
val jsonLDDocument: JsonLDDocument = JsonLDUtil.parseJsonLD(inputJsonLD)

// Convert that document to an RDF4J Model.
val outputModel: RdfModel = jsonLDDocument.toRdfModel(rdfModelFactory)
val outputModel: RdfModel = jsonLDDocument.toRdfModel

// Read an isomorphic Turtle file.
val expectedTurtle: String =
Expand All @@ -127,7 +124,7 @@ class JsonLDUtilSpec() extends CoreSpec {
)

// Parse the Turtle to an RDF4J Model.
val expectedModel: RdfModel = rdfFormatUtil.parseToRdfModel(rdfStr = expectedTurtle, rdfFormat = Turtle)
val expectedModel: RdfModel = RdfFormatUtil.parseToRdfModel(rdfStr = expectedTurtle, rdfFormat = Turtle)

// Compare the parsed Turtle with the model generated by the JsonLDDocument.
outputModel should ===(expectedModel)
Expand All @@ -141,13 +138,13 @@ class JsonLDUtilSpec() extends CoreSpec {
)

// Parse it to an RDF4J Model.
val inputModel: RdfModel = rdfFormatUtil.parseToRdfModel(rdfStr = turtle, rdfFormat = Turtle)
val inputModel: RdfModel = RdfFormatUtil.parseToRdfModel(rdfStr = turtle, rdfFormat = Turtle)

// Convert the model to a JsonLDDocument.
val outputJsonLD: JsonLDDocument = JsonLDUtil.fromRdfModel(inputModel)

// Convert the JsonLDDocument back to an RDF4J Model.
val jsonLDOutputModel: RdfModel = outputJsonLD.toRdfModel(rdfModelFactory)
val jsonLDOutputModel: RdfModel = outputJsonLD.toRdfModel

// Compare the generated model with the original one.
jsonLDOutputModel should ===(inputModel)
Expand All @@ -159,7 +156,7 @@ class JsonLDUtilSpec() extends CoreSpec {
)

// Parse it to an RDF4J Model.
val jsonLDExpectedModel: RdfModel = rdfFormatUtil.parseToRdfModel(rdfStr = expectedJsonLD, rdfFormat = JsonLD)
val jsonLDExpectedModel: RdfModel = RdfFormatUtil.parseToRdfModel(rdfStr = expectedJsonLD, rdfFormat = JsonLD)

// Compare that with the model generated by the JsonLDDocument.
jsonLDOutputModel should ===(jsonLDExpectedModel)
Expand All @@ -176,7 +173,7 @@ class JsonLDUtilSpec() extends CoreSpec {
val jsonLDDocument: JsonLDDocument = JsonLDUtil.parseJsonLD(inputJsonLD)

// Convert the document to an RDF4J Model.
val outputModel: RdfModel = jsonLDDocument.toRdfModel(rdfModelFactory)
val outputModel: RdfModel = jsonLDDocument.toRdfModel

// Convert the model back to a JsonLDDocument.
val outputModelAsJsonLDDocument: JsonLDDocument = JsonLDUtil.fromRdfModel(outputModel)
Expand All @@ -191,7 +188,7 @@ class JsonLDUtilSpec() extends CoreSpec {
)

// Parse it to an RDF4J Model.
val expectedModel: RdfModel = rdfFormatUtil.parseToRdfModel(rdfStr = expectedTurtle, rdfFormat = Turtle)
val expectedModel: RdfModel = RdfFormatUtil.parseToRdfModel(rdfStr = expectedTurtle, rdfFormat = Turtle)

// Compare that with the model generated by the JsonLDDocument.
outputModel should ===(expectedModel)
Expand All @@ -204,13 +201,13 @@ class JsonLDUtilSpec() extends CoreSpec {
)

// Parse it to an RDF4J Model.
val inputModel: RdfModel = rdfFormatUtil.parseToRdfModel(rdfStr = turtle, rdfFormat = Turtle)
val inputModel: RdfModel = RdfFormatUtil.parseToRdfModel(rdfStr = turtle, rdfFormat = Turtle)

// Convert the model to a JsonLDDocument.
val outputJsonLD: JsonLDDocument = JsonLDUtil.fromRdfModel(inputModel)

// Convert the JsonLDDocument back to an RDF4J Model.
val jsonLDOutputModel: RdfModel = outputJsonLD.toRdfModel(rdfModelFactory)
val jsonLDOutputModel: RdfModel = outputJsonLD.toRdfModel

// Compare the generated model with the original one.
jsonLDOutputModel should ===(inputModel)
Expand All @@ -226,7 +223,7 @@ class JsonLDUtilSpec() extends CoreSpec {
expectedJsonLDDocument.body should ===(outputJsonLD.body)

// Parse the same file to an RDF4J Model.
val jsonLDExpectedModel: RdfModel = rdfFormatUtil.parseToRdfModel(rdfStr = expectedJsonLD, rdfFormat = JsonLD)
val jsonLDExpectedModel: RdfModel = RdfFormatUtil.parseToRdfModel(rdfStr = expectedJsonLD, rdfFormat = JsonLD)

// Compare that with the model generated by the JsonLDDocument.
jsonLDOutputModel should ===(jsonLDExpectedModel)
Expand All @@ -248,7 +245,7 @@ class JsonLDUtilSpec() extends CoreSpec {
|""".stripMargin

// Parse it to an RDF4J Model.
val inputModel: RdfModel = rdfFormatUtil.parseToRdfModel(rdfStr = turtle, rdfFormat = Turtle)
val inputModel: RdfModel = RdfFormatUtil.parseToRdfModel(rdfStr = turtle, rdfFormat = Turtle)

// Convert the model to a JsonLDDocument.
val outputJsonLD: JsonLDDocument = JsonLDUtil.fromRdfModel(inputModel)
Expand Down Expand Up @@ -322,7 +319,7 @@ class JsonLDUtilSpec() extends CoreSpec {
|""".stripMargin

// Parse it to an RDF4J Model.
val inputModel: RdfModel = rdfFormatUtil.parseToRdfModel(rdfStr = turtle, rdfFormat = Turtle)
val inputModel: RdfModel = RdfFormatUtil.parseToRdfModel(rdfStr = turtle, rdfFormat = Turtle)

// Convert the model to a hierarchical JsonLDDocument.
val hierarchicalJsonLD: JsonLDDocument = JsonLDUtil.fromRdfModel(model = inputModel, flatJsonLD = false)
Expand Down Expand Up @@ -426,9 +423,9 @@ class JsonLDUtilSpec() extends CoreSpec {
assert(jsonLDDocument == expectedJsonLDDocument)

// Convert it to an RdfModel and check the result.
val rdfModel = jsonLDDocument.toRdfModel(rdfModelFactory)
val rdfModel = jsonLDDocument.toRdfModel
val expectedRdfModel =
rdfFormatUtil.parseToRdfModel("[] <http://ns.dasch.swiss/repository#hasLicense> [] .", Turtle)
RdfFormatUtil.parseToRdfModel("[] <http://ns.dasch.swiss/repository#hasLicense> [] .", Turtle)
assert(rdfModel == expectedRdfModel)

// Convert back to JSON-LD and check that it's the same.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ import org.knora.webapi.util.FileUtil
*/
class KnoraResponseV2Spec() extends CoreSpec {

private val rdfFormatUtil: RdfFormatUtil = RdfFeatureFactory.getRdfFormatUtil()

private val turtle =
"""<http://rdfh.ch/foo1> a <http://example.org/foo#Foo>;
| <http://example.org/foo#hasBar> [ a <http://example.org/foo#Bar>;
Expand Down Expand Up @@ -177,14 +175,14 @@ class KnoraResponseV2Spec() extends CoreSpec {
)

// Parse the Turtle to an RDF4J Model.
val parsedTurtle: RdfModel = rdfFormatUtil.parseToRdfModel(rdfStr = turtle, rdfFormat = Turtle)
val parsedTurtle: RdfModel = RdfFormatUtil.parseToRdfModel(rdfStr = turtle, rdfFormat = Turtle)

// Read an isomorphic Turtle file and parse it to an RDF4J Model.
val expectedTurtle: String =
FileUtil.readTextFile(
Paths.get("..", "test_data/generated_test_data/resourcesR2RV2/BookReiseInsHeiligeLand.ttl")
)
val parsedExpectedTurtle: RdfModel = rdfFormatUtil.parseToRdfModel(rdfStr = expectedTurtle, rdfFormat = Turtle)
val parsedExpectedTurtle: RdfModel = RdfFormatUtil.parseToRdfModel(rdfStr = expectedTurtle, rdfFormat = Turtle)

// Compare the two models.
parsedTurtle should ===(parsedExpectedTurtle)
Expand Down

0 comments on commit 25099ef

Please sign in to comment.