Skip to content

Commit

Permalink
merge main
Browse files Browse the repository at this point in the history
  • Loading branch information
seakayone committed Nov 20, 2023
1 parent 4593e50 commit 23068dd
Show file tree
Hide file tree
Showing 17 changed files with 330 additions and 382 deletions.
109 changes: 109 additions & 0 deletions index.html

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

package org.knora.webapi.messages.util.search.gravsearch.transformers
package org.knora.webapi.util.search

import org.knora.webapi.CoreSpec
import org.knora.webapi.messages.IriConversions._
Expand Down Expand Up @@ -80,11 +80,11 @@ class SparqlTransformerSpec extends CoreSpec {
isDeletedStatement,
linkStatement
)
val optimisedPatterns = SparqlTransformer.optimiseIsDeletedWithMinus(patterns)
val optimisedPatterns = SparqlTransformer.optimiseIsDeletedWithFilter(patterns)
val expectedPatterns = Seq(
typeStatement,
linkStatement,
MinusPattern(
FilterNotExistsPattern(
Seq(
StatementPattern(
subj = QueryVariable("foo"),
Expand Down
20 changes: 10 additions & 10 deletions project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ object Dependencies {

val fusekiImage =
"daschswiss/apache-jena-fuseki:2.1.2" // should be the same version as in docker-compose.yml, also make sure to use the same version when deploying it (i.e. version in ops-deploy)!
val sipiImage = "daschswiss/sipi:3.8.3" // base image the knora-sipi image is created from
val sipiImage = "daschswiss/sipi:3.8.6" // base image the knora-sipi image is created from

val ScalaVersion = "2.13.12"

Expand All @@ -22,12 +22,12 @@ object Dependencies {
val JenaVersion = "4.8.0"

val ZioConfigVersion = "3.0.7"
val ZioLoggingVersion = "2.1.14"
val ZioLoggingVersion = "2.1.15"
val ZioNioVersion = "2.0.2"
val ZioMetricsConnectorsVersion = "2.2.1"
val ZioPreludeVersion = "1.0.0-RC21"
val ZioSchemaVersion = "0.2.0"
val ZioVersion = "2.0.18"
val ZioVersion = "2.0.19"

// ZIO - all Scala 3 compatible
val zio = "dev.zio" %% "zio" % ZioVersion
Expand All @@ -40,7 +40,7 @@ object Dependencies {
val zioNio = "dev.zio" %% "zio-nio" % ZioNioVersion
val zioMacros = "dev.zio" %% "zio-macros" % ZioVersion
val zioPrelude = "dev.zio" %% "zio-prelude" % ZioPreludeVersion
val zioSttp = "com.softwaremill.sttp.client3" %% "zio" % "3.9.0"
val zioSttp = "com.softwaremill.sttp.client3" %% "zio" % "3.9.1"

// refined
val refined = Seq(
Expand Down Expand Up @@ -70,8 +70,8 @@ object Dependencies {

// Metrics
val aspectjweaver = "org.aspectj" % "aspectjweaver" % "1.9.20.1"
val kamonCore = "io.kamon" %% "kamon-core" % "2.6.5" // Scala 3 compatible
val kamonScalaFuture = "io.kamon" %% "kamon-scala-future" % "2.6.5" // Scala 3 incompatible
val kamonCore = "io.kamon" %% "kamon-core" % "2.6.6" // Scala 3 compatible
val kamonScalaFuture = "io.kamon" %% "kamon-scala-future" % "2.6.6" // Scala 3 incompatible

// input validation
val commonsValidator =
Expand All @@ -96,11 +96,11 @@ object Dependencies {
val icu4j = "com.ibm.icu" % "icu4j" % "74.1"
val jakartaJSON = "org.glassfish" % "jakarta.json" % "2.0.1"
val jodd = "org.jodd" % "jodd" % "3.2.7"
val rdf4jClient = "org.eclipse.rdf4j" % "rdf4j-client" % "4.3.7"
val rdf4jShacl = "org.eclipse.rdf4j" % "rdf4j-shacl" % "4.3.7"
val rdf4jClient = "org.eclipse.rdf4j" % "rdf4j-client" % "4.3.8"
val rdf4jShacl = "org.eclipse.rdf4j" % "rdf4j-shacl" % "4.3.8"
val saxonHE = "net.sf.saxon" % "Saxon-HE" % "12.3"
val scalaGraph = "org.scala-graph" %% "graph-core" % "1.13.6" // Scala 3 incompatible
val scallop = "org.rogach" %% "scallop" % "5.0.0" // Scala 3 compatible
val scallop = "org.rogach" %% "scallop" % "5.0.1" // Scala 3 compatible
val titaniumJSONLD = "com.apicatalog" % "titanium-json-ld" % "1.3.2"
val xmlunitCore = "org.xmlunit" % "xmlunit-core" % "2.9.1"

Expand All @@ -121,7 +121,7 @@ object Dependencies {
// found/added by the plugin but deleted anyway
val commonsLang3 = "org.apache.commons" % "commons-lang3" % "3.13.0"

val tapirVersion = "1.8.4"
val tapirVersion = "1.8.5"

val tapir = Seq(
"com.softwaremill.sttp.tapir" %% "tapir-pekko-http-server" % tapirVersion,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,24 @@

package org.knora.webapi.messages.util.rdf

import dsp.errors.{BadRequestException, InvalidRdfException}
import org.apache.pekko.http.scaladsl.model.MediaType
import org.knora.webapi.{IRI, RdfMediaTypes, Rendering, SchemaOptions}

import java.io.{BufferedInputStream, BufferedOutputStream, InputStream, OutputStream}
import java.nio.file.{Files, Path}
import scala.util.{Failure, Success, Try}
import java.io.BufferedInputStream
import java.io.BufferedOutputStream
import java.io.InputStream
import java.io.OutputStream
import java.nio.file.Files
import java.nio.file.Path
import scala.util.Failure
import scala.util.Success
import scala.util.Try

import dsp.errors.BadRequestException
import dsp.errors.InvalidRdfException
import org.knora.webapi.IRI
import org.knora.webapi.RdfMediaTypes
import org.knora.webapi.Rendering
import org.knora.webapi.SchemaOptions

/**
* A trait for supported RDF formats.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ final case class ConstructTransformer(
optimisedPatterns <-
ZIO.attempt(
SparqlTransformer.moveBindToBeginning(
SparqlTransformer.optimiseIsDeletedWithMinus(
SparqlTransformer.optimiseIsDeletedWithFilter(
SparqlTransformer.moveLuceneToBeginning(patterns)
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class SelectTransformer(
limitInferenceToOntologies = limitInferenceToOntologies
)
override def optimiseQueryPatterns(patterns: Seq[QueryPattern]): Task[Seq[QueryPattern]] = ZIO.attempt {
moveBindToBeginning(optimiseIsDeletedWithMinus(moveLuceneToBeginning(patterns)))
moveBindToBeginning(optimiseIsDeletedWithFilter(moveLuceneToBeginning(patterns)))
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,13 @@ object SparqlTransformer {
createUniqueVariableFromStatement(baseStatement, "LinkValue")

/**
* Optimises a query by replacing `knora-base:isDeleted false` with a `MINUS` pattern
* Optimises a query by replacing `knora-base:isDeleted false` with a `FILTER NOT EXISTS` pattern
* placed at the end of the block.
*
* @param patterns the block of patterns to be optimised.
* @return the result of the optimisation.
*/
def optimiseIsDeletedWithMinus(patterns: Seq[QueryPattern]): Seq[QueryPattern] = {
def optimiseIsDeletedWithFilter(patterns: Seq[QueryPattern]): Seq[QueryPattern] = {
implicit val stringFormatter: StringFormatter = StringFormatter.getGeneralInstance

// Separate the knora-base:isDeleted statements from the rest of the block.
Expand All @@ -107,17 +107,18 @@ object SparqlTransformer {
case _ => false
}

// Replace the knora-base:isDeleted statements with MINUS patterns.
val filterPatterns: Seq[MinusPattern] = isDeletedPatterns.collect { case statementPattern: StatementPattern =>
MinusPattern(
Seq(
StatementPattern(
subj = statementPattern.subj,
pred = IriRef(OntologyConstants.KnoraBase.IsDeleted.toSmartIri),
obj = XsdLiteral(value = "true", datatype = OntologyConstants.Xsd.Boolean.toSmartIri)
// Replace the knora-base:isDeleted statements with FILTER NOT EXISTS patterns.
val filterPatterns: Seq[FilterNotExistsPattern] = isDeletedPatterns.collect {
case statementPattern: StatementPattern =>
FilterNotExistsPattern(
Seq(
StatementPattern(
subj = statementPattern.subj,
pred = IriRef(OntologyConstants.KnoraBase.IsDeleted.toSmartIri),
obj = XsdLiteral(value = "true", datatype = OntologyConstants.Xsd.Boolean.toSmartIri)
)
)
)
)
}

otherPatterns ++ filterPatterns
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

package org.knora.webapi.messages.v2.responder

import dsp.errors.{AssertionException, BadRequestException}
import dsp.errors.AssertionException
import dsp.errors.BadRequestException
import org.knora.webapi.*
import org.knora.webapi.config.AppConfig
import org.knora.webapi.messages.OntologyConstants
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@ sealed trait ResourcesResponderRequestV2 extends KnoraRequestV2 with RelayedMess
* @param requestingUser the user making the request.
*/
case class ResourcesGetRequestV2(
resourceIris: Seq[IRI],
propertyIri: Option[SmartIri] = None,
valueUuid: Option[UUID] = None,
versionDate: Option[Instant] = None,
withDeleted: Boolean = true,
targetSchema: ApiV2Schema,
schemaOptions: Set[Rendering] = Set.empty,
requestingUser: UserADM
resourceIris: Seq[IRI],
propertyIri: Option[SmartIri] = None,
valueUuid: Option[UUID] = None,
versionDate: Option[Instant] = None,
withDeleted: Boolean = true,
targetSchema: ApiV2Schema,
schemaOptions: Set[Rendering] = Set.empty,
requestingUser: UserADM
) extends ResourcesResponderRequestV2

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import scala.concurrent.Future
import dsp.errors.*
import dsp.valueobjects.Iri
import dsp.valueobjects.UuidUtil
import org.knora.webapi.MarkupRendering.Standoff
import org.knora.webapi.SchemaRendering.apiV2SchemaWithOption
import org.knora.webapi.*
import org.knora.webapi.config.AppConfig
Expand Down Expand Up @@ -1413,15 +1412,15 @@ final case class ResourcesResponderV2Live(
* @return a [[ReadResourcesSequenceV2]].
*/
private def getResourcesV2(
resourceIris: Seq[IRI],
propertyIri: Option[SmartIri] = None,
valueUuid: Option[UUID] = None,
versionDate: Option[Instant] = None,
withDeleted: Boolean = true,
showDeletedValues: Boolean = false,
targetSchema: ApiV2Schema,
schemaOptions: Set[Rendering],
requestingUser: UserADM
resourceIris: Seq[IRI],
propertyIri: Option[SmartIri] = None,
valueUuid: Option[UUID] = None,
versionDate: Option[Instant] = None,
withDeleted: Boolean = true,
showDeletedValues: Boolean = false,
targetSchema: ApiV2Schema,
schemaOptions: Set[Rendering],
requestingUser: UserADM
): Task[ReadResourcesSequenceV2] = {
// eliminate duplicate Iris
val resourceIrisDistinct: Seq[IRI] = resourceIris.distinct
Expand Down Expand Up @@ -2273,7 +2272,7 @@ final case class ResourcesResponderV2Live(
parsedGravsearchQuery <- ZIO.succeed(GravsearchParser.parseQuery(gravsearchQueryForIncomingLinks))
searchResponse <- searchResponderV2.gravsearchV2(
parsedGravsearchQuery,
apiV2SchemaWithOption(Standoff),
apiV2SchemaWithOption(MarkupRendering.Standoff),
request.requestingUser
)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* Copyright © 2021 - 2023 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.responders.v2

import org.knora.webapi.IRI
import org.knora.webapi.store.triplestore.api.TriplestoreService.Queries.Construct
import org.knora.webapi.store.triplestore.api.TriplestoreService.Queries.Select

object SearchQueries {

def selectCountByLabel(
searchTerm: String,
limitToProject: Option[IRI],
limitToResourceClass: Option[IRI]
): Select =
Select(
s"""|PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
|PREFIX knora-base: <http://www.knora.org/ontology/knora-base#>
|SELECT (count(distinct ?resource) as ?count)
|WHERE {
| ?resource <http://jena.apache.org/text#query> (rdfs:label "$searchTerm") ;
| a ?resourceClass .
| ?resourceClass rdfs:subClassOf* knora-base:Resource .
| ${limitToResourceClass.fold("")(resourceClass => s"?resourceClass rdfs:subClassOf* <$resourceClass> .")}
| ${limitToProject.fold("")(project => s"?resource knora-base:attachedToProject <$project> .")}
| FILTER NOT EXISTS { ?resource knora-base:isDeleted true . }
|}
|""".stripMargin
)

def constructSearchByLabel(

Check warning on line 34 in webapi/src/main/scala/org/knora/webapi/responders/v2/SearchQueries.scala

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

webapi/src/main/scala/org/knora/webapi/responders/v2/SearchQueries.scala#L34

Method is longer than 50 lines.
searchTerm: String,
limitToResourceClass: Option[IRI] = None,
limitToProject: Option[IRI] = None,
limit: Int,
offset: Int = 0
): Construct = {
val limitToClassOrProject =
(limitToResourceClass, limitToProject) match {
case (Some(cls), _) => s"?resourceClass rdfs:subClassOf* <$cls> ."
case (_, Some(project)) => s"?resource knora-base:attachedToProject <$project> ."
case _ => ""
}
Construct(
s"""|PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
|PREFIX knora-base: <http://www.knora.org/ontology/knora-base#>
|CONSTRUCT {
| ?resource rdfs:label ?label ;
| a knora-base:Resource ;
| knora-base:isMainResource true ;
| knora-base:isDeleted false ;
| a ?resourceType ;
| knora-base:attachedToUser ?resourceCreator ;
| knora-base:hasPermissions ?resourcePermissions ;
| knora-base:attachedToProject ?resourceProject ;
| knora-base:creationDate ?creationDate ;
| knora-base:lastModificationDate ?lastModificationDate ;
| knora-base:hasValue ?valueObject ;
| ?resourceValueProperty ?valueObject .
| ?valueObject ?valueObjectProperty ?valueObjectValue .
|} WHERE {
| {
| SELECT DISTINCT ?resource ?label
| WHERE {
| ?resource <http://jena.apache.org/text#query> (rdfs:label "$searchTerm") ;
| a ?resourceClass ;
| rdfs:label ?label .
| $limitToClassOrProject
| FILTER NOT EXISTS { ?resource knora-base:isDeleted true . }
| }
| ORDER BY ?resource
| LIMIT $limit
| OFFSET $offset
| }
|
| ?resource a ?resourceType ;
| knora-base:attachedToUser ?resourceCreator ;
| knora-base:hasPermissions ?resourcePermissions ;
| knora-base:attachedToProject ?resourceProject ;
| knora-base:creationDate ?creationDate ;
| rdfs:label ?label .
| OPTIONAL { ?resource knora-base:lastModificationDate ?lastModificationDate . }
| OPTIONAL {
| ?resource ?resourceValueProperty ?valueObject .
| ?resourceValueProperty rdfs:subPropertyOf* knora-base:hasValue .
| ?valueObject a ?valueObjectType ;
| ?valueObjectProperty ?valueObjectValue .
| ?valueObjectType rdfs:subClassOf* knora-base:Value .
| FILTER(?valueObjectType != knora-base:LinkValue)
| FILTER NOT EXISTS { ?valueObject knora-base:isDeleted true . }
| FILTER NOT EXISTS { ?valueObjectValue a knora-base:StandoffTag . }
| }
|}
|""".stripMargin
)
}

}
Loading

0 comments on commit 23068dd

Please sign in to comment.