/
Cardinalities.scala
553 lines (475 loc) · 23.4 KB
/
Cardinalities.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
/*
* Copyright © 2015-2021 Data and Service Center for the Humanities (DaSCH)
*
* This file is part of the DaSCH Service Platform.
*
* The DaSCH Service Platform is free software: you can redistribute it
* and/or modify it under the terms of the GNU Affero General Public
* License as published by the Free Software Foundation, either version 3
* of the License, or (at your option) any later version.
*
* The DaSCH Service Platform is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with the DaSCH Service Platform. If not, see
* <http://www.gnu.org/licenses/>.
*/
package org.knora.webapi.responders.v2.ontology
import akka.actor.ActorRef
import akka.http.scaladsl.util.FastFuture
import akka.pattern._
import akka.util.Timeout
import org.knora.webapi.InternalSchema
import org.knora.webapi.exceptions.{BadRequestException, InconsistentRepositoryDataException}
import org.knora.webapi.messages.IriConversions._
import org.knora.webapi.messages.OntologyConstants.KnoraBase
import org.knora.webapi.messages.store.triplestoremessages.{
SparqlAskRequest,
SparqlAskResponse,
SparqlUpdateRequest,
SparqlUpdateResponse
}
import org.knora.webapi.messages.v2.responder.CanDoResponseV2
import org.knora.webapi.messages.v2.responder.ontologymessages.Cardinality.KnoraCardinalityInfo
import org.knora.webapi.messages.v2.responder.ontologymessages._
import org.knora.webapi.messages.{OntologyConstants, SmartIri, StringFormatter}
import org.knora.webapi.settings.KnoraSettingsImpl
import java.time.Instant
import scala.concurrent.{ExecutionContext, Future}
/**
* Contains methods used for dealing with cardinalities on a class
*/
object Cardinalities {
/**
* FIXME(DSP-1856): Only works if a single cardinality is supplied.
*
* @param settings the applications settings.
* @param storeManager the store manager actor.
* @param deleteCardinalitiesFromClassRequest the requested cardinalities to be deleted.
* @param internalClassIri the Class from which the cardinalities are deleted.
* @param internalOntologyIri the Ontology of which the Class and Cardinalities are part of.
* @return a [[CanDoResponseV2]] indicating whether a class's cardinalities can be deleted.
*/
def canDeleteCardinalitiesFromClass(
settings: KnoraSettingsImpl,
storeManager: ActorRef,
deleteCardinalitiesFromClassRequest: CanDeleteCardinalitiesFromClassRequestV2,
internalClassIri: SmartIri,
internalOntologyIri: SmartIri
)(implicit ec: ExecutionContext, stringFormatter: StringFormatter, timeout: Timeout): Future[CanDoResponseV2] = {
for {
cacheData: Cache.OntologyCacheData <- Cache.getCacheData
ontology = cacheData.ontologies(internalOntologyIri)
submittedClassDefinition: ClassInfoContentV2 =
deleteCardinalitiesFromClassRequest.classInfoContent.toOntologySchema(InternalSchema)
// Check that the ontology exists and has not been updated by another user since the client last read it.
_ <- OntologyHelpers.checkOntologyLastModificationDateBeforeUpdate(
settings,
storeManager,
internalOntologyIri = internalOntologyIri,
expectedLastModificationDate = deleteCardinalitiesFromClassRequest.lastModificationDate,
featureFactoryConfig = deleteCardinalitiesFromClassRequest.featureFactoryConfig
)
// Check that the class's rdf:type is owl:Class.
rdfType: SmartIri = submittedClassDefinition.requireIriObject(
OntologyConstants.Rdf.Type.toSmartIri,
throw BadRequestException(s"No rdf:type specified")
)
_ = if (rdfType != OntologyConstants.Owl.Class.toSmartIri) {
throw BadRequestException(s"Invalid rdf:type for property: $rdfType")
}
// Check that cardinalities were submitted.
_ = if (submittedClassDefinition.directCardinalities.isEmpty) {
throw BadRequestException("No cardinalities specified")
}
// Check that only one cardinality was submitted.
_ = if (submittedClassDefinition.directCardinalities.size > 1) {
throw BadRequestException("Only one cardinality is allowed to be submitted.")
}
// Check that the class exists
currentClassDefinition: ClassInfoContentV2 <-
classExists(
cacheData,
deleteCardinalitiesFromClassRequest.classInfoContent.toOntologySchema(InternalSchema),
internalClassIri,
internalOntologyIri
)
// FIXME: This seems not to work or is not needed. Check that it is a subclass of knora-base:Resource
// _ <- isKnoraResourceClass(deleteCardinalitiesFromClassRequest.classInfoContent.toOntologySchema(InternalSchema))
// Check that the submitted cardinality to delete is defined on this class
cardinalitiesToDelete: Map[SmartIri, Cardinality.KnoraCardinalityInfo] =
deleteCardinalitiesFromClassRequest.classInfoContent.toOntologySchema(InternalSchema).directCardinalities
_ = cardinalitiesToDelete.foreach(p =>
isCardinalityDefinedOnClass(cacheData, p._1, p._2, internalClassIri, internalOntologyIri)
)
// Check if property is used in resources
submittedPropertyToDelete: SmartIri = cardinalitiesToDelete.head._1
propertyIsUsed: Boolean <- isPropertyUsedInResources(settings, storeManager, submittedPropertyToDelete)
// Make an update class definition in which the cardinality to delete is removed
submittedPropertyToDeleteIsLinkProperty: Boolean = cacheData
.ontologies(submittedPropertyToDelete.getOntologyFromEntity)
.properties(submittedPropertyToDelete)
.isLinkProp
newClassDefinitionWithRemovedCardinality =
currentClassDefinition.copy(
directCardinalities =
if (submittedPropertyToDeleteIsLinkProperty) {
// if we want to remove a link property, then we also need to remove the corresponding link property value
currentClassDefinition.directCardinalities - submittedPropertyToDelete - submittedPropertyToDelete.fromLinkPropToLinkValueProp
} else
currentClassDefinition.directCardinalities - submittedPropertyToDelete
)
// FIXME: Refactor. From here on is copy-paste from `changeClassCardinalities`, which I don't fully understand
// Check that the new cardinalities are valid, and don't add any inherited cardinalities.
allBaseClassIrisWithoutInternal: Seq[SmartIri] =
newClassDefinitionWithRemovedCardinality.subClassOf.toSeq.flatMap { baseClassIri =>
cacheData.subClassOfRelations.getOrElse(
baseClassIri,
Seq.empty[SmartIri]
)
}
allBaseClassIris: Seq[SmartIri] = internalClassIri +: allBaseClassIrisWithoutInternal
(newInternalClassDefWithLinkValueProps, cardinalitiesForClassWithInheritance) =
OntologyHelpers
.checkCardinalitiesBeforeAddingAndIfNecessaryAddLinkValueProperties(
internalClassDef = newClassDefinitionWithRemovedCardinality,
allBaseClassIris = allBaseClassIris.toSet,
cacheData = cacheData,
// since we only want to delete (and have already removed what we want), the rest of the link properties
// need to be marked as wanting to keep.
existingLinkPropsToKeep =
newClassDefinitionWithRemovedCardinality.directCardinalities.keySet // gets all keys from the map as a set
.map(propertyIri =>
cacheData.ontologies(propertyIri.getOntologyFromEntity).properties(propertyIri)
) // turn the propertyIri into a ReadPropertyInfoV2
.filter(_.isLinkProp) // we are only interested in link properties
.map(_.entityInfoContent.propertyIri) // turn whatever is left back to a propertyIri
)
// Check that the class definition doesn't refer to any non-shared ontologies in other projects.
_ = Cache.checkOntologyReferencesInClassDef(
ontologyCacheData = cacheData,
classDef = newInternalClassDefWithLinkValueProps,
errorFun = { msg: String =>
throw BadRequestException(msg)
}
)
} yield CanDoResponseV2(!propertyIsUsed)
}
/**
* FIXME(DSP-1856): Only works if a single cardinality is supplied.
* Deletes the supplied cardinalities from a class, if the referenced properties are not used in instances
* of the class and any subclasses.
*
* @param settings the applications settings.
* @param storeManager the store manager actor.
* @param deleteCardinalitiesFromClassRequest the requested cardinalities to be deleted.
* @param internalClassIri the Class from which the cardinalities are deleted.
* @param internalOntologyIri the Ontology of which the Class and Cardinalities are part of.
* @return a [[ReadOntologyV2]] in the internal schema, containing the new class definition.
*/
def deleteCardinalitiesFromClass(
settings: KnoraSettingsImpl,
storeManager: ActorRef,
deleteCardinalitiesFromClassRequest: DeleteCardinalitiesFromClassRequestV2,
internalClassIri: SmartIri,
internalOntologyIri: SmartIri
)(implicit ec: ExecutionContext, stringFormatter: StringFormatter, timeout: Timeout): Future[ReadOntologyV2] =
for {
cacheData: Cache.OntologyCacheData <- Cache.getCacheData
ontology = cacheData.ontologies(internalOntologyIri)
submittedClassDefinition: ClassInfoContentV2 =
deleteCardinalitiesFromClassRequest.classInfoContent.toOntologySchema(InternalSchema)
// Check that the ontology exists and has not been updated by another user since the client last read it.
_ <- OntologyHelpers.checkOntologyLastModificationDateBeforeUpdate(
settings,
storeManager,
internalOntologyIri = internalOntologyIri,
expectedLastModificationDate = deleteCardinalitiesFromClassRequest.lastModificationDate,
featureFactoryConfig = deleteCardinalitiesFromClassRequest.featureFactoryConfig
)
// Check that the class's rdf:type is owl:Class.
rdfType: SmartIri = submittedClassDefinition.requireIriObject(
OntologyConstants.Rdf.Type.toSmartIri,
throw BadRequestException(s"No rdf:type specified")
)
_ = if (rdfType != OntologyConstants.Owl.Class.toSmartIri) {
throw BadRequestException(s"Invalid rdf:type for property: $rdfType")
}
// Check that cardinalities were submitted.
_ = if (submittedClassDefinition.directCardinalities.isEmpty) {
throw BadRequestException("No cardinalities specified")
}
// Check that only one cardinality was submitted.
_ = if (submittedClassDefinition.directCardinalities.size > 1) {
throw BadRequestException("Only one cardinality is allowed to be submitted.")
}
// Check that the class exists
currentClassDefinition: ClassInfoContentV2 <-
classExists(
cacheData,
deleteCardinalitiesFromClassRequest.classInfoContent.toOntologySchema(InternalSchema),
internalClassIri,
internalOntologyIri
)
// Check that the submitted cardinality to delete is defined on this class
cardinalitiesToDelete: Map[SmartIri, Cardinality.KnoraCardinalityInfo] =
deleteCardinalitiesFromClassRequest.classInfoContent.toOntologySchema(InternalSchema).directCardinalities
_ = cardinalitiesToDelete.foreach(p =>
isCardinalityDefinedOnClass(cacheData, p._1, p._2, internalClassIri, internalOntologyIri)
)
// Check if property is used in resources
submittedPropertyToDelete: SmartIri = cardinalitiesToDelete.head._1
propertyIsUsed: Boolean <- isPropertyUsedInResources(settings, storeManager, submittedPropertyToDelete)
_ = if (propertyIsUsed) {
throw BadRequestException("Property is used in data. The cardinality cannot be deleted.")
}
// Make an update class definition in which the cardinality to delete is removed
submittedPropertyToDeleteIsLinkProperty: Boolean = cacheData
.ontologies(submittedPropertyToDelete.getOntologyFromEntity)
.properties(submittedPropertyToDelete)
.isLinkProp
newClassDefinitionWithRemovedCardinality =
currentClassDefinition.copy(
directCardinalities =
if (submittedPropertyToDeleteIsLinkProperty) {
// if we want to remove a link property, then we also need to remove the corresponding link property value
currentClassDefinition.directCardinalities - submittedPropertyToDelete - submittedPropertyToDelete.fromLinkPropToLinkValueProp
} else
currentClassDefinition.directCardinalities - submittedPropertyToDelete
)
// FIXME: Refactor. From here on is copy-paste from `changeClassCardinalities`, which I don't fully understand
// Check that the new cardinalities are valid, and don't add any inherited cardinalities.
allBaseClassIrisWithoutInternal: Seq[SmartIri] =
newClassDefinitionWithRemovedCardinality.subClassOf.toSeq.flatMap { baseClassIri =>
cacheData.subClassOfRelations.getOrElse(
baseClassIri,
Seq.empty[SmartIri]
)
}
allBaseClassIris: Seq[SmartIri] = internalClassIri +: allBaseClassIrisWithoutInternal
(newInternalClassDefWithLinkValueProps, cardinalitiesForClassWithInheritance) =
OntologyHelpers
.checkCardinalitiesBeforeAddingAndIfNecessaryAddLinkValueProperties(
internalClassDef = newClassDefinitionWithRemovedCardinality,
allBaseClassIris = allBaseClassIris.toSet,
cacheData = cacheData,
// since we only want to delete (and have already removed what we want), the rest of the link properties
// need to be marked as wanting to keep.
existingLinkPropsToKeep =
newClassDefinitionWithRemovedCardinality.directCardinalities.keySet // gets all keys from the map as a set
.map(propertyIri =>
cacheData.ontologies(propertyIri.getOntologyFromEntity).properties(propertyIri)
) // turn the propertyIri into a ReadPropertyInfoV2
.filter(_.isLinkProp) // we are only interested in link properties
.map(_.entityInfoContent.propertyIri) // turn whatever is left back to a propertyIri
)
// Check that the class definition doesn't refer to any non-shared ontologies in other projects.
_ = Cache.checkOntologyReferencesInClassDef(
ontologyCacheData = cacheData,
classDef = newInternalClassDefWithLinkValueProps,
errorFun = { msg: String =>
throw BadRequestException(msg)
}
)
// Prepare to update the ontology cache. (No need to deal with SPARQL-escaping here, because there
// isn't any text to escape in cardinalities.)
propertyIrisOfAllCardinalitiesForClass = cardinalitiesForClassWithInheritance.keySet
inheritedCardinalities: Map[SmartIri, KnoraCardinalityInfo] =
cardinalitiesForClassWithInheritance.filterNot { case (propertyIri, _) =>
newInternalClassDefWithLinkValueProps.directCardinalities.contains(propertyIri)
}
readClassInfo = ReadClassInfoV2(
entityInfoContent = newInternalClassDefWithLinkValueProps,
allBaseClasses = allBaseClassIris,
isResourceClass = true,
canBeInstantiated = true,
inheritedCardinalities = inheritedCardinalities,
knoraResourceProperties = propertyIrisOfAllCardinalitiesForClass.filter(propertyIri =>
OntologyHelpers.isKnoraResourceProperty(propertyIri, cacheData)
),
linkProperties = propertyIrisOfAllCardinalitiesForClass.filter(propertyIri =>
OntologyHelpers.isLinkProp(propertyIri, cacheData)
),
linkValueProperties = propertyIrisOfAllCardinalitiesForClass.filter(propertyIri =>
OntologyHelpers.isLinkValueProp(propertyIri, cacheData)
),
fileValueProperties = propertyIrisOfAllCardinalitiesForClass.filter(propertyIri =>
OntologyHelpers.isFileValueProp(propertyIri, cacheData)
)
)
// Add the cardinalities to the class definition in the triplestore.
currentTime: Instant = Instant.now
updateSparql = org.knora.webapi.messages.twirl.queries.sparql.v2.txt
.replaceClassCardinalities(
triplestore = settings.triplestoreType,
ontologyNamedGraphIri = internalOntologyIri,
ontologyIri = internalOntologyIri,
classIri = internalClassIri,
newCardinalities = newInternalClassDefWithLinkValueProps.directCardinalities,
lastModificationDate = deleteCardinalitiesFromClassRequest.lastModificationDate,
currentTime = currentTime
)
.toString()
_ <- (storeManager ? SparqlUpdateRequest(updateSparql)).mapTo[SparqlUpdateResponse]
// Check that the ontology's last modification date was updated.
_ <- OntologyHelpers.checkOntologyLastModificationDateAfterUpdate(
settings,
storeManager,
internalOntologyIri = internalOntologyIri,
expectedLastModificationDate = currentTime,
featureFactoryConfig = deleteCardinalitiesFromClassRequest.featureFactoryConfig
)
// Check that the data that was saved corresponds to the data that was submitted.
loadedClassDef <- OntologyHelpers.loadClassDefinition(
settings,
storeManager,
classIri = internalClassIri,
featureFactoryConfig = deleteCardinalitiesFromClassRequest.featureFactoryConfig
)
_ = if (loadedClassDef != newInternalClassDefWithLinkValueProps) {
throw InconsistentRepositoryDataException(
s"Attempted to save class definition $newInternalClassDefWithLinkValueProps, but $loadedClassDef was saved"
)
}
// Update subclasses and write the cache.
updatedOntology = ontology.copy(
ontologyMetadata = ontology.ontologyMetadata.copy(
lastModificationDate = Some(currentTime)
),
classes = ontology.classes + (internalClassIri -> readClassInfo)
)
_ = Cache.storeCacheData(
Cache.updateSubClasses(
baseClassIri = internalClassIri,
cacheData = cacheData.copy(
ontologies = cacheData.ontologies + (internalOntologyIri -> updatedOntology)
)
)
)
// Read the data back from the cache.
response: ReadOntologyV2 <- OntologyHelpers.getClassDefinitionsFromOntologyV2(
classIris = Set(internalClassIri),
allLanguages = true,
requestingUser = deleteCardinalitiesFromClassRequest.requestingUser
)
} yield response
/**
* Check if a property entity is used in resource instances. Returns `true` if
* it is used, and `false` if it is not used.
*
* @param settings application settings.
* @param storeManager store manager actor ref.
* @param internalPropertyIri the IRI of the entity that is being checked for usage.
* @param ec the execution context onto with the future will run.
* @param timeout the timeout for the future.
* @return a [[Boolean]] denoting if the property entity is used.
*/
def isPropertyUsedInResources(
settings: KnoraSettingsImpl,
storeManager: ActorRef,
internalPropertyIri: SmartIri
)(implicit ec: ExecutionContext, timeout: Timeout): Future[Boolean] =
for {
request <- Future(
org.knora.webapi.queries.sparql.v2.txt
.isPropertyUsed(
triplestore = settings.triplestoreType,
internalPropertyIri = internalPropertyIri.toString,
ignoreKnoraConstraints = true,
ignoreRdfSubjectAndObject = true
)
.toString()
)
response: SparqlAskResponse <-
(storeManager ? SparqlAskRequest(request)).mapTo[SparqlAskResponse]
} yield response.result
/**
* Checks if the class is defined inside the ontology found in the cache.
*
* @param cacheData the cached ontology data
* @param submittedClassInfoContentV2 the submitted class information
* @param internalClassIri the internal class IRI
* @param internalOntologyIri the internal ontology IRI
* @return `true` if the class is defined inside the ontology found in the cache, otherwise throws an exception.
*/
def classExists(
cacheData: Cache.OntologyCacheData,
submittedClassInfoContentV2: ClassInfoContentV2,
internalClassIri: SmartIri,
internalOntologyIri: SmartIri
)(implicit ec: ExecutionContext): Future[ClassInfoContentV2] = for {
currentOntologyState: ReadOntologyV2 <- Future(cacheData.ontologies(internalOntologyIri))
currentClassDefinition = currentOntologyState.classes
.getOrElse(
internalClassIri,
throw BadRequestException(
s"Class ${submittedClassInfoContentV2.classIri} does not exist"
)
)
.entityInfoContent
} yield currentClassDefinition
/**
* Checks if the class is a subclass of `knora-base:Resource`.
*
* @param submittedClassInfoContentV2 the class to check
* @return `true` if the class is a subclass of `knora-base:Resource`, otherwise throws an exception.
*/
def isKnoraResourceClass(
submittedClassInfoContentV2: ClassInfoContentV2
)(implicit ec: ExecutionContext, stringFormatter: StringFormatter): Future[Boolean] =
if (submittedClassInfoContentV2.subClassOf.contains(KnoraBase.Resource.toSmartIri)) {
FastFuture.successful(true)
} else {
FastFuture.failed(
throw BadRequestException(
s"Class ${submittedClassInfoContentV2.classIri} is not a subclass of ${KnoraBase.Resource.toSmartIri}. $submittedClassInfoContentV2"
)
)
}
/**
* Check if the cardinality for a property is defined on a class.
*
* @param cacheData the cached ontology data.
* @param propertyIri the property IRI for which we want to check if the cardinality is defined on the class.
* @param cardinalityInfo the cardinality that should be defined for the property.
* @param internalClassIri the class we are checking against.
* @param internalOntologyIri the ontology containing the class.
* @return `true` if the cardinality is defined on the class, otherwise throws an exception.
*/
def isCardinalityDefinedOnClass(
cacheData: Cache.OntologyCacheData,
propertyIri: SmartIri,
cardinalityInfo: KnoraCardinalityInfo,
internalClassIri: SmartIri,
internalOntologyIri: SmartIri
)(implicit ec: ExecutionContext): Future[Boolean] = {
val currentOntologyState: ReadOntologyV2 = cacheData.ontologies(internalOntologyIri)
val currentClassState: ClassInfoContentV2 = currentOntologyState.classes
.getOrElse(
internalClassIri,
throw BadRequestException(
s"Class ${internalClassIri} does not exist"
)
)
.entityInfoContent
val existingCardinality = currentClassState.directCardinalities
.getOrElse(
propertyIri,
throw BadRequestException(
s"Cardinality for property ${propertyIri} is not defined."
)
)
if (existingCardinality.cardinality.equals(cardinalityInfo.cardinality)) {
FastFuture.successful(true)
} else {
FastFuture.failed(
throw BadRequestException(
s"Submitted cardinality for property ${propertyIri} does not match existing cardinality."
)
)
}
}
}