/
OntologyResponderV2.scala
5001 lines (4236 loc) · 225 KB
/
OntologyResponderV2.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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright © 2015-2021 the contributors (see Contributors.md).
*
* This file is part of Knora.
*
* Knora 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.
*
* Knora 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 Knora. If not, see <http://www.gnu.org/licenses/>.
*/
package org.knora.webapi.responders.v2
import java.time.Instant
import akka.http.scaladsl.util.FastFuture
import akka.pattern._
import org.knora.webapi._
import org.knora.webapi.exceptions._
import org.knora.webapi.feature.FeatureFactoryConfig
import org.knora.webapi.messages.IriConversions._
import org.knora.webapi.messages.StringFormatter.{SalsahGuiAttribute, SalsahGuiAttributeDefinition}
import org.knora.webapi.messages.admin.responder.projectsmessages.{
ProjectGetRequestADM,
ProjectGetResponseADM,
ProjectIdentifierADM
}
import org.knora.webapi.messages.admin.responder.usersmessages.UserADM
import org.knora.webapi.messages.store.triplestoremessages._
import org.knora.webapi.messages.util.rdf.{SparqlSelectResult, VariableResultsRow}
import org.knora.webapi.messages.util.{ErrorHandlingMap, KnoraSystemInstances, OntologyUtil, ResponderData}
import org.knora.webapi.messages.v2.responder.SuccessResponseV2
import org.knora.webapi.messages.v2.responder.ontologymessages.Cardinality.{KnoraCardinalityInfo, OwlCardinalityInfo}
import org.knora.webapi.messages.v2.responder.ontologymessages._
import org.knora.webapi.messages.v2.responder.standoffmessages.StandoffDataTypeClasses
import org.knora.webapi.messages.{OntologyConstants, SmartIri}
import org.knora.webapi.responders.Responder.handleUnexpectedMessage
import org.knora.webapi.responders.{IriLocker, Responder}
import org.knora.webapi.util._
import org.knora.webapi.util.cache.CacheUtil
import scala.collection.immutable
import scala.concurrent.Future
/**
* Responds to requests dealing with ontologies.
*
* The API v2 ontology responder reads ontologies from two sources:
*
* - The triplestore.
* - The constant knora-api v2 ontologies that are defined in Scala rather than in the triplestore, [[KnoraBaseToApiV2SimpleTransformationRules]] and [[KnoraBaseToApiV2ComplexTransformationRules]].
*
* It maintains an in-memory cache of all ontology data. This cache can be refreshed by sending a [[LoadOntologiesRequestV2]].
*
* Read requests to the ontology responder may contain internal or external IRIs as needed. Response messages from the
* ontology responder will contain internal IRIs and definitions, unless a constant API v2 ontology was requested,
* in which case the response will be in the requested API v2 schema.
*
* In API v2, the ontology responder can also create and update ontologies. Update requests must contain
* [[ApiV2Complex]] IRIs and definitions.
*
* The API v1 ontology responder, which is read-only, delegates most of its work to this responder.
*/
class OntologyResponderV2(responderData: ResponderData) extends Responder(responderData) {
// The name of the ontology cache.
private val OntologyCacheName = "ontologyCache"
// The cache key under which cached ontology data is stored.
private val OntologyCacheKey = "ontologyCacheData"
// The global ontology cache lock. This is needed because every ontology update replaces the whole ontology cache
// (because definitions in one ontology can refer to definitions in another ontology). Without a global lock,
// concurrent updates (even to different ontologies) could overwrite each other.
private val ONTOLOGY_CACHE_LOCK_IRI = "http://rdfh.ch/ontologies"
/**
* The in-memory cache of ontologies.
*
* @param ontologies a map of ontology IRIs to ontologies.
* @param subClassOfRelations a map of subclasses to their base classes.
* @param superClassOfRelations a map of base classes to their subclasses.
* @param subPropertyOfRelations a map of subproperties to their base proeprties.
* @param guiAttributeDefinitions a map of salsah-gui:Guielement individuals to their GUI attribute definitions.
* @param standoffProperties a set of standoff properties.
*/
private case class OntologyCacheData(ontologies: Map[SmartIri, ReadOntologyV2],
subClassOfRelations: Map[SmartIri, Seq[SmartIri]],
superClassOfRelations: Map[SmartIri, Set[SmartIri]],
subPropertyOfRelations: Map[SmartIri, Set[SmartIri]],
guiAttributeDefinitions: Map[SmartIri, Set[SalsahGuiAttributeDefinition]],
standoffProperties: Set[SmartIri]) {
lazy val allPropertyDefs: Map[SmartIri, PropertyInfoContentV2] = ontologies.values
.flatMap(_.properties.map {
case (propertyIri, readPropertyInfo) => propertyIri -> readPropertyInfo.entityInfoContent
})
.toMap
}
/**
* Receives a message of type [[OntologiesResponderRequestV2]], and returns an appropriate response message.
*/
def receive(msg: OntologiesResponderRequestV2) = msg match {
case LoadOntologiesRequestV2(featureFactoryConfig, requestingUser) =>
loadOntologies(featureFactoryConfig, requestingUser)
case EntityInfoGetRequestV2(classIris, propertyIris, requestingUser) =>
getEntityInfoResponseV2(classIris, propertyIris, requestingUser)
case StandoffEntityInfoGetRequestV2(standoffClassIris, standoffPropertyIris, requestingUser) =>
getStandoffEntityInfoResponseV2(standoffClassIris, standoffPropertyIris, requestingUser)
case StandoffClassesWithDataTypeGetRequestV2(requestingUser) =>
getStandoffStandoffClassesWithDataTypeV2(requestingUser)
case StandoffAllPropertyEntitiesGetRequestV2(requestingUser) => getAllStandoffPropertyEntitiesV2(requestingUser)
case CheckSubClassRequestV2(subClassIri, superClassIri, requestingUser) =>
checkSubClassV2(subClassIri, superClassIri)
case SubClassesGetRequestV2(resourceClassIri, requestingUser) => getSubClassesV2(resourceClassIri, requestingUser)
case OntologyKnoraEntityIrisGetRequestV2(namedGraphIri, requestingUser) =>
getKnoraEntityIrisInNamedGraphV2(namedGraphIri, requestingUser)
case OntologyEntitiesGetRequestV2(ontologyIri, allLanguages, requestingUser) =>
getOntologyEntitiesV2(ontologyIri, allLanguages, requestingUser)
case ClassesGetRequestV2(resourceClassIris, allLanguages, requestingUser) =>
getClassDefinitionsFromOntologyV2(resourceClassIris, allLanguages, requestingUser)
case PropertiesGetRequestV2(propertyIris, allLanguages, requestingUser) =>
getPropertyDefinitionsFromOntologyV2(propertyIris, allLanguages, requestingUser)
case OntologyMetadataGetByProjectRequestV2(projectIris, requestingUser) =>
getOntologyMetadataForProjectsV2(projectIris, requestingUser)
case OntologyMetadataGetByIriRequestV2(ontologyIris, requestingUser) =>
getOntologyMetadataByIriV2(ontologyIris, requestingUser)
case createOntologyRequest: CreateOntologyRequestV2 => createOntology(createOntologyRequest)
case changeOntologyMetadataRequest: ChangeOntologyMetadataRequestV2 =>
changeOntologyMetadata(changeOntologyMetadataRequest)
case deleteOntologyCommentRequest: DeleteOntologyCommentRequestV2 =>
deleteOntologyComment(deleteOntologyCommentRequest)
case createClassRequest: CreateClassRequestV2 => createClass(createClassRequest)
case changeClassLabelsOrCommentsRequest: ChangeClassLabelsOrCommentsRequestV2 =>
changeClassLabelsOrComments(changeClassLabelsOrCommentsRequest)
case addCardinalitiesToClassRequest: AddCardinalitiesToClassRequestV2 =>
addCardinalitiesToClass(addCardinalitiesToClassRequest)
case changeCardinalitiesRequest: ChangeCardinalitiesRequestV2 =>
changeClassCardinalities(changeCardinalitiesRequest)
case deleteClassRequest: DeleteClassRequestV2 => deleteClass(deleteClassRequest)
case createPropertyRequest: CreatePropertyRequestV2 => createProperty(createPropertyRequest)
case changePropertyLabelsOrCommentsRequest: ChangePropertyLabelsOrCommentsRequestV2 =>
changePropertyLabelsOrComments(changePropertyLabelsOrCommentsRequest)
case deletePropertyRequest: DeletePropertyRequestV2 => deleteProperty(deletePropertyRequest)
case deleteOntologyRequest: DeleteOntologyRequestV2 => deleteOntology(deleteOntologyRequest)
case other => handleUnexpectedMessage(other, log, this.getClass.getName)
}
/**
* Represents the contents of a named graph representing an ontology.
*
* @param ontologyIri the ontology IRI, which is also the IRI of the named graph.
* @param constructResponse the triplestore's response to a CONSTRUCT query that gets the contents of the named graph.
*/
private case class OntologyGraph(ontologyIri: SmartIri, constructResponse: SparqlExtendedConstructResponse)
/**
* Loads and caches all ontology information.
*
* @param featureFactoryConfig the feature factory configuration.
* @param requestingUser the user making the request.
* @return a [[SuccessResponseV2]].
*/
private def loadOntologies(featureFactoryConfig: FeatureFactoryConfig,
requestingUser: UserADM): Future[SuccessResponseV2] = {
val loadOntologiesFuture: Future[SuccessResponseV2] = for {
_ <- Future {
if (!(requestingUser.id == KnoraSystemInstances.Users.SystemUser.id || requestingUser.permissions.isSystemAdmin)) {
throw ForbiddenException(s"Only a system administrator can reload ontologies")
}
}
// Get all ontology metadata.
allOntologyMetadataSparql <- FastFuture.successful(
org.knora.webapi.messages.twirl.queries.sparql.v2.txt
.getAllOntologyMetadata(triplestore = settings.triplestoreType)
.toString())
allOntologyMetadataResponse: SparqlSelectResult <- (storeManager ? SparqlSelectRequest(allOntologyMetadataSparql))
.mapTo[SparqlSelectResult]
allOntologyMetadata: Map[SmartIri, OntologyMetadataV2] = buildOntologyMetadata(allOntologyMetadataResponse)
knoraBaseOntologyMetadata: OntologyMetadataV2 = allOntologyMetadata.getOrElse(
OntologyConstants.KnoraBase.KnoraBaseOntologyIri.toSmartIri,
throw InconsistentRepositoryDataException(s"No knora-base ontology found"))
knoraBaseOntologyVersion: String = knoraBaseOntologyMetadata.ontologyVersion.getOrElse(
throw InconsistentRepositoryDataException(
"The knora-base ontology in the repository is not up to date. See the Knora documentation on repository updates."))
_ = if (knoraBaseOntologyVersion != KnoraBaseVersion) {
throw InconsistentRepositoryDataException(
s"The knora-base ontology in the repository has version '$knoraBaseOntologyVersion', but this version of Knora requires '$KnoraBaseVersion'. See the Knora documentation on repository updates.")
}
// Get the contents of each named graph containing an ontology.
ontologyGraphResponseFutures: Iterable[Future[OntologyGraph]] = allOntologyMetadata.keys.map { ontologyIri =>
val ontologyGraphConstructQuery = org.knora.webapi.messages.twirl.queries.sparql.v2.txt
.getOntologyGraph(
triplestore = settings.triplestoreType,
ontologyGraph = ontologyIri
)
.toString
(storeManager ? SparqlExtendedConstructRequest(
sparql = ontologyGraphConstructQuery,
featureFactoryConfig = featureFactoryConfig
)).mapTo[SparqlExtendedConstructResponse].map { response =>
OntologyGraph(ontologyIri = ontologyIri, constructResponse = response)
}
}
ontologyGraphs: Iterable[OntologyGraph] <- Future.sequence(ontologyGraphResponseFutures)
_ = makeOntologyCache(allOntologyMetadata, ontologyGraphs)
} yield SuccessResponseV2("Ontologies loaded.")
loadOntologiesFuture.recover {
case exception: Throwable =>
exception match {
case inconsistentRepositoryDataException: InconsistentRepositoryDataException =>
log.error(inconsistentRepositoryDataException.message)
SuccessResponseV2(
s"An error occurred when loading ontologies: ${inconsistentRepositoryDataException.message}")
case other => throw other
}
}
}
/**
* Given ontology metdata and ontology graphs read from the triplestore, constructs the ontology cache.
*
* @param allOntologyMetadata a map of ontology IRIs to ontology metadata.
* @param ontologyGraphs a list of ontology graphs.
*/
private def makeOntologyCache(allOntologyMetadata: Map[SmartIri, OntologyMetadataV2],
ontologyGraphs: Iterable[OntologyGraph]): Unit = {
// Get the IRIs of all the entities in each ontology.
// A map of ontology IRIs to class IRIs in each ontology.
val classIrisPerOntology: Map[SmartIri, Set[SmartIri]] = getEntityIrisFromOntologyGraphs(
ontologyGraphs = ontologyGraphs,
entityTypes = Set(OntologyConstants.Owl.Class)
)
// A map of ontology IRIs to property IRIs in each ontology.
val propertyIrisPerOntology: Map[SmartIri, Set[SmartIri]] = getEntityIrisFromOntologyGraphs(
ontologyGraphs = ontologyGraphs,
entityTypes = Set(
OntologyConstants.Owl.ObjectProperty,
OntologyConstants.Owl.DatatypeProperty,
OntologyConstants.Owl.AnnotationProperty,
OntologyConstants.Rdf.Property
)
)
// A map of ontology IRIs to named individual IRIs in each ontology.
val individualIrisPerOntology: Map[SmartIri, Set[SmartIri]] = getEntityIrisFromOntologyGraphs(
ontologyGraphs = ontologyGraphs,
entityTypes = Set(OntologyConstants.Owl.NamedIndividual)
)
// Construct entity definitions.
// A map of class IRIs to class definitions.
val allClassDefs: Map[SmartIri, ClassInfoContentV2] = ontologyGraphs.flatMap { ontologyGraph =>
constructResponseToClassDefinitions(
classIris = classIrisPerOntology(ontologyGraph.ontologyIri),
constructResponse = ontologyGraph.constructResponse
)
}.toMap
// A map of property IRIs to property definitions.
val allPropertyDefs: Map[SmartIri, PropertyInfoContentV2] = ontologyGraphs.flatMap { ontologyGraph =>
constructResponseToPropertyDefinitions(
propertyIris = propertyIrisPerOntology(ontologyGraph.ontologyIri),
constructResponse = ontologyGraph.constructResponse
)
}.toMap
// A map of OWL named individual IRIs to named individuals.
val allIndividuals: Map[SmartIri, IndividualInfoContentV2] = ontologyGraphs.flatMap { ontologyGraph =>
constructResponseToIndividuals(
individualIris = individualIrisPerOntology(ontologyGraph.ontologyIri),
constructResponse = ontologyGraph.constructResponse
)
}.toMap
// A map of salsah-gui:Guielement individuals to their GUI attribute definitions.
val allGuiAttributeDefinitions: Map[SmartIri, Set[SalsahGuiAttributeDefinition]] = makeGuiAttributeDefinitions(
allIndividuals)
// Determine relations between entities.
// A map of class IRIs to their immediate base classes.
val directSubClassOfRelations: Map[SmartIri, Set[SmartIri]] = allClassDefs.map {
case (classIri, classDef) => classIri -> classDef.subClassOf
}
// A map of property IRIs to their immediate base properties.
val directSubPropertyOfRelations: Map[SmartIri, Set[SmartIri]] = allPropertyDefs.map {
case (propertyIri, propertyDef) => propertyIri -> propertyDef.subPropertyOf
}
val allClassIris = allClassDefs.keySet
val allPropertyIris = allPropertyDefs.keySet
// A map in which each class IRI points to the full sequence of its base classes.
val allSubClassOfRelations: Map[SmartIri, Seq[SmartIri]] = allClassIris.toSeq.map { classIri =>
// get the hierarchically ordered base classes.
val baseClasses: Seq[SmartIri] = OntologyUtil.getAllBaseDefs(classIri, directSubClassOfRelations)
// prepend the classIri to the sequence of base classes because a class is also a subclass of itself.
(classIri, classIri +: baseClasses)
}.toMap
// A map in which each class IRI points to the full set of its subclasses. A class is also
// a subclass of itself.
val allSuperClassOfRelations: Map[SmartIri, Set[SmartIri]] = calculateSuperClassOfRelations(allSubClassOfRelations)
// Make a map in which each property IRI points to the full set of its base properties. A property is also
// a subproperty of itself.
val allSubPropertyOfRelations: Map[SmartIri, Set[SmartIri]] = allPropertyIris.map { propertyIri =>
(propertyIri, OntologyUtil.getAllBaseDefs(propertyIri, directSubPropertyOfRelations).toSet + propertyIri)
}.toMap
// A set of all subproperties of knora-base:resourceProperty.
val allKnoraResourceProps: Set[SmartIri] = allPropertyIris.filter { prop =>
val allPropSubPropertyOfRelations = allSubPropertyOfRelations(prop)
prop == OntologyConstants.KnoraBase.ResourceProperty.toSmartIri ||
allPropSubPropertyOfRelations.contains(OntologyConstants.KnoraBase.HasValue.toSmartIri) ||
allPropSubPropertyOfRelations.contains(OntologyConstants.KnoraBase.HasLinkTo.toSmartIri)
}
// A set of all subproperties of knora-base:hasLinkTo.
val allLinkProps: Set[SmartIri] = allPropertyIris.filter(prop =>
allSubPropertyOfRelations(prop).contains(OntologyConstants.KnoraBase.HasLinkTo.toSmartIri))
// A set of all subproperties of knora-base:hasLinkToValue.
val allLinkValueProps: Set[SmartIri] = allPropertyIris.filter(prop =>
allSubPropertyOfRelations(prop).contains(OntologyConstants.KnoraBase.HasLinkToValue.toSmartIri))
// A set of all subproperties of knora-base:hasFileValue.
val allFileValueProps: Set[SmartIri] = allPropertyIris.filter(prop =>
allSubPropertyOfRelations(prop).contains(OntologyConstants.KnoraBase.HasFileValue.toSmartIri))
// A map of the cardinalities defined directly on each resource class. Each class IRI points to a map of
// property IRIs to OwlCardinalityInfo objects.
val directClassCardinalities: Map[SmartIri, Map[SmartIri, OwlCardinalityInfo]] = allClassDefs.map {
case (classIri, classDef) =>
classIri -> classDef.directCardinalities.map {
case (propertyIri, knoraCardinalityInfo) =>
propertyIri -> Cardinality.knoraCardinality2OwlCardinality(knoraCardinalityInfo)
}
}
// Allow each class to inherit cardinalities from its base classes.
val classCardinalitiesWithInheritance: Map[SmartIri, Map[SmartIri, OwlCardinalityInfo]] = allClassIris.map {
resourceClassIri =>
val resourceClassCardinalities: Map[SmartIri, OwlCardinalityInfo] = inheritCardinalitiesInLoadedClass(
classIri = resourceClassIri,
directSubClassOfRelations = directSubClassOfRelations,
allSubPropertyOfRelations = allSubPropertyOfRelations,
directClassCardinalities = directClassCardinalities
)
resourceClassIri -> resourceClassCardinalities
}.toMap
// Construct a ReadClassInfoV2 for each class.
val readClassInfos: Map[SmartIri, ReadClassInfoV2] = makeReadClassInfos(
classDefs = allClassDefs,
directClassCardinalities = directClassCardinalities,
classCardinalitiesWithInheritance = classCardinalitiesWithInheritance,
directSubClassOfRelations = directSubClassOfRelations,
allSubClassOfRelations = allSubClassOfRelations,
allSubPropertyOfRelations = allSubPropertyOfRelations,
allPropertyDefs = allPropertyDefs,
allKnoraResourceProps = allKnoraResourceProps,
allLinkProps = allLinkProps,
allLinkValueProps = allLinkValueProps,
allFileValueProps = allFileValueProps
)
// Construct a ReadPropertyInfoV2 for each property definition.
val readPropertyInfos: Map[SmartIri, ReadPropertyInfoV2] = makeReadPropertyInfos(
propertyDefs = allPropertyDefs,
directSubPropertyOfRelations = directSubPropertyOfRelations,
allSubPropertyOfRelations = allSubPropertyOfRelations,
allSubClassOfRelations = allSubClassOfRelations,
allGuiAttributeDefinitions = allGuiAttributeDefinitions,
allKnoraResourceProps = allKnoraResourceProps,
allLinkProps = allLinkProps,
allLinkValueProps = allLinkValueProps,
allFileValueProps = allFileValueProps
)
// Construct a ReadIndividualV2 for each OWL named individual.
val readIndividualInfos = makeReadIndividualInfos(allIndividuals)
// A ReadOntologyV2 for each ontology to be cached.
val readOntologies: Map[SmartIri, ReadOntologyV2] = allOntologyMetadata.map {
case (ontologyIri, ontologyMetadata) =>
ontologyIri -> ReadOntologyV2(
ontologyMetadata = ontologyMetadata,
classes = readClassInfos.filter {
case (classIri, _) => classIri.getOntologyFromEntity == ontologyIri
},
properties = readPropertyInfos.filter {
case (propertyIri, _) => propertyIri.getOntologyFromEntity == ontologyIri
},
individuals = readIndividualInfos.filter {
case (individualIri, _) => individualIri.getOntologyFromEntity == ontologyIri
},
isWholeOntology = true
)
}
// A set of the IRIs of all properties used in cardinalities in standoff classes.
val propertiesUsedInStandoffCardinalities: Set[SmartIri] = readClassInfos.flatMap {
case (_, readClassInfo) =>
if (readClassInfo.isStandoffClass) {
readClassInfo.allCardinalities.keySet
} else {
Set.empty[SmartIri]
}
}.toSet
// A set of the IRIs of all properties whose subject class constraint is a standoff class.
val propertiesWithStandoffTagSubjects: Set[SmartIri] = readPropertyInfos.flatMap {
case (propertyIri, readPropertyInfo) =>
readPropertyInfo.entityInfoContent.getPredicateIriObject(
OntologyConstants.KnoraBase.SubjectClassConstraint.toSmartIri) match {
case Some(subjectClassConstraint: SmartIri) =>
readClassInfos.get(subjectClassConstraint) match {
case Some(subjectReadClassInfo: ReadClassInfoV2) =>
if (subjectReadClassInfo.isStandoffClass) {
Some(propertyIri)
} else {
None
}
case None => None
}
case None => None
}
}.toSet
// Construct the ontology cache data.
val ontologyCacheData: OntologyCacheData = OntologyCacheData(
ontologies = new ErrorHandlingMap[SmartIri, ReadOntologyV2](readOntologies, { key =>
s"Ontology not found: $key"
}),
subClassOfRelations = new ErrorHandlingMap[SmartIri, Seq[SmartIri]](allSubClassOfRelations, { key =>
s"Class not found: $key"
}),
superClassOfRelations = new ErrorHandlingMap[SmartIri, Set[SmartIri]](allSuperClassOfRelations, { key =>
s"Class not found: $key"
}),
subPropertyOfRelations = new ErrorHandlingMap[SmartIri, Set[SmartIri]](allSubPropertyOfRelations, { key =>
s"Property not found: $key"
}),
guiAttributeDefinitions =
new ErrorHandlingMap[SmartIri, Set[SalsahGuiAttributeDefinition]](allGuiAttributeDefinitions, { key =>
s"salsah-gui:Guielement not found: $key"
}),
standoffProperties = propertiesUsedInStandoffCardinalities ++ propertiesWithStandoffTagSubjects
)
// Check property subject and object class constraints.
readPropertyInfos.foreach {
case (propertyIri, readPropertyInfo) =>
val allSuperPropertyIris: Set[SmartIri] = allSubPropertyOfRelations.getOrElse(propertyIri, Set.empty[SmartIri])
readPropertyInfo.entityInfoContent.getPredicateIriObject(
OntologyConstants.KnoraBase.SubjectClassConstraint.toSmartIri) match {
case Some(subjectClassConstraint) =>
// Each property's subject class constraint, if provided, must be a subclass of the subject class constraints of all its base properties.
checkPropertyConstraint(
cacheData = ontologyCacheData,
internalPropertyIri = propertyIri,
constraintPredicateIri = OntologyConstants.KnoraBase.SubjectClassConstraint.toSmartIri,
constraintValueToBeChecked = subjectClassConstraint,
allSuperPropertyIris = allSuperPropertyIris,
errorSchema = InternalSchema,
errorFun = { msg: String =>
throw InconsistentRepositoryDataException(msg)
}
)
// If the property is defined in a project-specific ontology, its subject class constraint, if provided, must be a Knora resource or standoff class.
if (!propertyIri.isKnoraBuiltInDefinitionIri) {
val baseClassesOfSubjectClassConstraint = allSubClassOfRelations(subjectClassConstraint)
if (!(baseClassesOfSubjectClassConstraint.contains(OntologyConstants.KnoraBase.Resource.toSmartIri) ||
baseClassesOfSubjectClassConstraint.contains(OntologyConstants.KnoraBase.StandoffTag.toSmartIri))) {
throw InconsistentRepositoryDataException(
s"Property $propertyIri is defined in a project-specific ontology, but its knora-base:subjectClassConstraint, $subjectClassConstraint, is not a subclass of knora-base:Resource or knora-base:StandoffTag")
}
}
case None => ()
}
readPropertyInfo.entityInfoContent.getPredicateIriObject(
OntologyConstants.KnoraBase.ObjectClassConstraint.toSmartIri) match {
case Some(objectClassConstraint) =>
// Each property's object class constraint, if provided, must be a subclass of the object class constraints of all its base properties.
checkPropertyConstraint(
cacheData = ontologyCacheData,
internalPropertyIri = propertyIri,
constraintPredicateIri = OntologyConstants.KnoraBase.ObjectClassConstraint.toSmartIri,
constraintValueToBeChecked = objectClassConstraint,
allSuperPropertyIris = allSuperPropertyIris,
errorSchema = InternalSchema,
errorFun = { msg: String =>
throw InconsistentRepositoryDataException(msg)
}
)
case None =>
// A resource property must have an object class constraint, unless it's knora-base:resourceProperty.
if (readPropertyInfo.isResourceProp && propertyIri != OntologyConstants.KnoraBase.ResourceProperty.toSmartIri) {
throw InconsistentRepositoryDataException(
s"Property $propertyIri has no knora-base:objectClassConstraint")
}
}
}
// Check references between ontologies.
checkReferencesBetweenOntologies(ontologyCacheData)
// Update the cache.
storeCacheData(ontologyCacheData)
}
/**
* Checks a reference between an ontology entity and another ontology entity to see if the target
* is in a non-shared ontology in another project.
*
* @param ontologyCacheData the ontology cache data.
* @param sourceEntityIri the entity whose definition contains the reference.
* @param targetEntityIri the entity that's the target of the reference.
* @param errorFun a function that throws an exception with the specified message if the reference is invalid.
*/
private def checkOntologyReferenceInEntity(ontologyCacheData: OntologyCacheData,
sourceEntityIri: SmartIri,
targetEntityIri: SmartIri,
errorFun: String => Nothing): Unit = {
if (targetEntityIri.isKnoraDefinitionIri) {
val sourceOntologyIri = sourceEntityIri.getOntologyFromEntity
val sourceOntologyMetadata = ontologyCacheData.ontologies(sourceOntologyIri).ontologyMetadata
val targetOntologyIri = targetEntityIri.getOntologyFromEntity
val targetOntologyMetadata = ontologyCacheData.ontologies(targetOntologyIri).ontologyMetadata
if (sourceOntologyMetadata.projectIri != targetOntologyMetadata.projectIri) {
if (!(targetOntologyIri.isKnoraBuiltInDefinitionIri || targetOntologyIri.isKnoraSharedDefinitionIri)) {
errorFun(
s"Entity $sourceEntityIri refers to entity $targetEntityIri, which is in a non-shared ontology that belongs to another project")
}
}
}
}
/**
* Checks a property definition to ensure that it doesn't refer to any other non-shared ontologies.
*
* @param ontologyCacheData the ontology cache data.
* @param propertyDef the property definition.
* @param errorFun a function that throws an exception with the specified message if the property definition is invalid.
*/
private def checkOntologyReferencesInPropertyDef(ontologyCacheData: OntologyCacheData,
propertyDef: PropertyInfoContentV2,
errorFun: String => Nothing): Unit = {
// Ensure that the property isn't a subproperty of any property in a non-shared ontology in another project.
for (subPropertyOf <- propertyDef.subPropertyOf) {
checkOntologyReferenceInEntity(
ontologyCacheData = ontologyCacheData,
sourceEntityIri = propertyDef.propertyIri,
targetEntityIri = subPropertyOf,
errorFun = errorFun
)
}
// Ensure that the property doesn't have subject or object constraints pointing to a non-shared ontology in another project.
propertyDef.getPredicateIriObject(OntologyConstants.KnoraBase.SubjectClassConstraint.toSmartIri) match {
case Some(subjectClassConstraint) =>
checkOntologyReferenceInEntity(
ontologyCacheData = ontologyCacheData,
sourceEntityIri = propertyDef.propertyIri,
targetEntityIri = subjectClassConstraint,
errorFun = errorFun
)
case None => ()
}
propertyDef.getPredicateIriObject(OntologyConstants.KnoraBase.ObjectClassConstraint.toSmartIri) match {
case Some(objectClassConstraint) =>
checkOntologyReferenceInEntity(
ontologyCacheData = ontologyCacheData,
sourceEntityIri = propertyDef.propertyIri,
targetEntityIri = objectClassConstraint,
errorFun = errorFun
)
case None => ()
}
}
/**
* Checks a class definition to ensure that it doesn't refer to any non-shared ontologies in other projects.
*
* @param ontologyCacheData the ontology cache data.
* @param classDef the class definition.
* @param errorFun a function that throws an exception with the specified message if the property definition is invalid.
*/
private def checkOntologyReferencesInClassDef(ontologyCacheData: OntologyCacheData,
classDef: ClassInfoContentV2,
errorFun: String => Nothing): Unit = {
for (subClassOf <- classDef.subClassOf) {
checkOntologyReferenceInEntity(
ontologyCacheData = ontologyCacheData,
sourceEntityIri = classDef.classIri,
targetEntityIri = subClassOf,
errorFun = errorFun
)
}
for (cardinalityPropIri <- classDef.directCardinalities.keys) {
checkOntologyReferenceInEntity(
ontologyCacheData = ontologyCacheData,
sourceEntityIri = classDef.classIri,
targetEntityIri = cardinalityPropIri,
errorFun = errorFun
)
}
}
/**
* Checks references between ontologies to ensure that they do not refer to non-shared ontologies in other projects.
*
* @param ontologyCacheData the ontology cache data.
*/
private def checkReferencesBetweenOntologies(ontologyCacheData: OntologyCacheData): Unit = {
for (ontology <- ontologyCacheData.ontologies.values) {
for (propertyInfo <- ontology.properties.values) {
checkOntologyReferencesInPropertyDef(
ontologyCacheData = ontologyCacheData,
propertyDef = propertyInfo.entityInfoContent,
errorFun = { msg: String =>
throw InconsistentRepositoryDataException(msg)
}
)
}
for (classInfo <- ontology.classes.values) {
checkOntologyReferencesInClassDef(
ontologyCacheData = ontologyCacheData,
classDef = classInfo.entityInfoContent,
errorFun = { msg: String =>
throw InconsistentRepositoryDataException(msg)
}
)
}
}
}
/**
* Given a list of ontology graphs, finds the IRIs of all subjects whose `rdf:type` is contained in a given set of types.
*
* @param ontologyGraphs a list of ontology graphs.
* @param entityTypes the types of entities to be found.
* @return a map of ontology IRIs to sets of the IRIs of entities with matching types in each ontology.
*/
private def getEntityIrisFromOntologyGraphs(ontologyGraphs: Iterable[OntologyGraph],
entityTypes: Set[IRI]): Map[SmartIri, Set[SmartIri]] = {
val entityTypesAsIriLiterals = entityTypes.map(entityType => IriLiteralV2(entityType))
ontologyGraphs.map { ontologyGraph =>
val entityIrisInGraph: Set[SmartIri] = ontologyGraph.constructResponse.statements.foldLeft(Set.empty[SmartIri]) {
case (acc, (subjectIri: IriSubjectV2, subjectStatements: Map[SmartIri, Seq[LiteralV2]])) =>
val subjectTypeLiterals: Seq[IriLiteralV2] = subjectStatements
.getOrElse(OntologyConstants.Rdf.Type.toSmartIri,
throw InconsistentRepositoryDataException(s"Subject $subjectIri has no rdf:type"))
.collect {
case iriLiteral: IriLiteralV2 => iriLiteral
}
if (subjectTypeLiterals.exists(entityTypesAsIriLiterals.contains)) {
acc + subjectIri.value.toSmartIri
} else {
acc
}
case (acc, _) => acc
}
ontologyGraph.ontologyIri -> entityIrisInGraph
}.toMap
}
/**
* Given the triplestore's response to `getAllOntologyMetadata.scala.txt`, constructs a map of ontology IRIs
* to ontology metadata for the ontology cache.
*
* @param allOntologyMetadataResponse the triplestore's response to the SPARQL query `getAllOntologyMetadata.scala.txt`.
* @return a map of ontology IRIs to ontology metadata.
*/
private def buildOntologyMetadata(
allOntologyMetadataResponse: SparqlSelectResult): Map[SmartIri, OntologyMetadataV2] = {
allOntologyMetadataResponse.results.bindings.groupBy(_.rowMap("ontologyGraph")).map {
case (ontologyGraph: IRI, rows: Seq[VariableResultsRow]) =>
val ontologyIri = rows.head.rowMap("ontologyIri")
if (ontologyIri != ontologyGraph) {
throw InconsistentRepositoryDataException(
s"Ontology $ontologyIri must be stored in named graph $ontologyIri, but it is in $ontologyGraph")
}
val ontologySmartIri = ontologyIri.toSmartIri
if (!ontologySmartIri.isKnoraOntologyIri) {
throw InconsistentRepositoryDataException(s"Ontology $ontologySmartIri is not a Knora ontology")
}
val ontologyMetadataMap: Map[IRI, String] = rows.map { row =>
val pred =
row.rowMap.getOrElse("ontologyPred",
throw InconsistentRepositoryDataException(s"Empty predicate in ontology $ontologyIri"))
val obj = row.rowMap.getOrElse(
"ontologyObj",
throw InconsistentRepositoryDataException(s"Empty object for predicate $pred in ontology $ontologyIri"))
pred -> obj
}.toMap
val projectIri: SmartIri = ontologyMetadataMap
.getOrElse(
OntologyConstants.KnoraBase.AttachedToProject,
throw InconsistentRepositoryDataException(s"Ontology $ontologyIri has no knora-base:attachedToProject"))
.toSmartIri
val ontologyLabel: String =
ontologyMetadataMap.getOrElse(OntologyConstants.Rdfs.Label, ontologySmartIri.getOntologyName)
val lastModificationDate: Option[Instant] = ontologyMetadataMap
.get(OntologyConstants.KnoraBase.LastModificationDate)
.map(
instant =>
stringFormatter.xsdDateTimeStampToInstant(
instant,
throw InconsistentRepositoryDataException(s"Invalid UTC instant: $instant")))
val ontologyVersion: Option[String] = ontologyMetadataMap.get(OntologyConstants.KnoraBase.OntologyVersion)
ontologySmartIri -> OntologyMetadataV2(
ontologyIri = ontologySmartIri,
projectIri = Some(projectIri),
label = Some(ontologyLabel),
lastModificationDate = lastModificationDate,
ontologyVersion = ontologyVersion
)
}
}
/**
* Constructs a map of class IRIs to [[ReadClassInfoV2]] instances, based on class definitions loaded from the
* triplestore.
*
* @param classDefs a map of class IRIs to class definitions.
* @param directClassCardinalities a map of the cardinalities defined directly on each class. Each resource class
* IRI points to a map of property IRIs to [[OwlCardinalityInfo]] objects.
* @param classCardinalitiesWithInheritance a map of the cardinalities defined directly on each class or inherited from
* base classes. Each class IRI points to a map of property IRIs to
* [[OwlCardinalityInfo]] objects.
* @param directSubClassOfRelations a map of class IRIs to their immediate base classes.
* @param allSubClassOfRelations a map of class IRIs to all their base classes.
* @param allSubPropertyOfRelations a map of property IRIs to all their base properties.
* @param allPropertyDefs a map of property IRIs to property definitions.
* @param allKnoraResourceProps a set of the IRIs of all Knora resource properties.
* @param allLinkProps a set of the IRIs of all link properties.
* @param allLinkValueProps a set of the IRIs of link value properties.
* @param allFileValueProps a set of the IRIs of all file value properties.
* @return a map of resource class IRIs to their definitions.
*/
private def makeReadClassInfos(classDefs: Map[SmartIri, ClassInfoContentV2],
directClassCardinalities: Map[SmartIri, Map[SmartIri, OwlCardinalityInfo]],
classCardinalitiesWithInheritance: Map[SmartIri, Map[SmartIri, OwlCardinalityInfo]],
directSubClassOfRelations: Map[SmartIri, Set[SmartIri]],
allSubClassOfRelations: Map[SmartIri, Seq[SmartIri]],
allSubPropertyOfRelations: Map[SmartIri, Set[SmartIri]],
allPropertyDefs: Map[SmartIri, PropertyInfoContentV2],
allKnoraResourceProps: Set[SmartIri],
allLinkProps: Set[SmartIri],
allLinkValueProps: Set[SmartIri],
allFileValueProps: Set[SmartIri]): Map[SmartIri, ReadClassInfoV2] = {
classDefs.map {
case (classIri, classDef) =>
val ontologyIri = classIri.getOntologyFromEntity
// Get the OWL cardinalities for the class.
val allOwlCardinalitiesForClass: Map[SmartIri, OwlCardinalityInfo] = classCardinalitiesWithInheritance(classIri)
val allPropertyIrisForCardinalitiesInClass: Set[SmartIri] = allOwlCardinalitiesForClass.keys.toSet
// Identify the Knora resource properties, link properties, link value properties, and file value properties in the cardinalities.
val knoraResourcePropsInClass = allPropertyIrisForCardinalitiesInClass.filter(allKnoraResourceProps)
val linkPropsInClass = allPropertyIrisForCardinalitiesInClass.filter(allLinkProps)
val linkValuePropsInClass = allPropertyIrisForCardinalitiesInClass.filter(allLinkValueProps)
val fileValuePropsInClass = allPropertyIrisForCardinalitiesInClass.filter(allFileValueProps)
// Make sure there is a link value property for each link property.
val missingLinkValueProps = linkPropsInClass.map(_.fromLinkPropToLinkValueProp) -- linkValuePropsInClass
if (missingLinkValueProps.nonEmpty) {
throw InconsistentRepositoryDataException(
s"Resource class $classIri has cardinalities for one or more link properties without corresponding link value properties. The missing (or incorrectly defined) property or properties: ${missingLinkValueProps
.mkString(", ")}")
}
// Make sure there is a link property for each link value property.
val missingLinkProps = linkValuePropsInClass.map(_.fromLinkValuePropToLinkProp) -- linkPropsInClass
if (missingLinkProps.nonEmpty) {
throw InconsistentRepositoryDataException(
s"Resource class $classIri has cardinalities for one or more link value properties without corresponding link properties. The missing (or incorrectly defined) property or properties: ${missingLinkProps
.mkString(", ")}")
}
// Make sure that the cardinality for each link property is the same as the cardinality for the corresponding link value property.
for (linkProp <- linkPropsInClass) {
val linkValueProp: SmartIri = linkProp.fromLinkPropToLinkValueProp
val linkPropCardinality: OwlCardinalityInfo = allOwlCardinalitiesForClass(linkProp)
val linkValuePropCardinality: OwlCardinalityInfo = allOwlCardinalitiesForClass(linkValueProp)
if (!linkPropCardinality.equalsWithoutGuiOrder(linkValuePropCardinality)) {
throw InconsistentRepositoryDataException(
s"In class $classIri, the cardinality for $linkProp is different from the cardinality for $linkValueProp")
}
}
// The class's direct cardinalities.
val directCardinalities: Map[SmartIri, KnoraCardinalityInfo] = directClassCardinalities(classIri).map {
case (propertyIri, owlCardinalityInfo) =>
propertyIri -> Cardinality.owlCardinality2KnoraCardinality(propertyIri = propertyIri.toString,
owlCardinality = owlCardinalityInfo)
}
val directCardinalityPropertyIris = directCardinalities.keySet
val allBaseClasses: Seq[SmartIri] = allSubClassOfRelations(classIri)
val isKnoraResourceClass = allBaseClasses.contains(OntologyConstants.KnoraBase.Resource.toSmartIri)
val isStandoffClass = allBaseClasses.contains(OntologyConstants.KnoraBase.StandoffTag.toSmartIri)
val isValueClass = !(isKnoraResourceClass || isStandoffClass) && allBaseClasses.contains(
OntologyConstants.KnoraBase.Value.toSmartIri)
// If the class is defined in project-specific ontology, do the following checks.
if (!ontologyIri.isKnoraBuiltInDefinitionIri) {
// It must be either a resource class or a standoff class, but not both.
if (!(isKnoraResourceClass ^ isStandoffClass)) {
throw InconsistentRepositoryDataException(
s"Class $classIri must be a subclass either of knora-base:Resource or of knora-base:StandoffTag (but not both)")
}
// All its cardinalities must be on properties that are defined.
val cardinalitiesOnMissingProps = directCardinalityPropertyIris.filterNot(allPropertyDefs.keySet)
if (cardinalitiesOnMissingProps.nonEmpty) {
throw InconsistentRepositoryDataException(
s"Class $classIri has one or more cardinalities on undefined properties: ${cardinalitiesOnMissingProps
.mkString(", ")}")
}
// It cannot have cardinalities both on property P and on a subproperty of P.
val maybePropertyAndSubproperty: Option[(SmartIri, SmartIri)] = findPropertyAndSubproperty(
propertyIris = allPropertyIrisForCardinalitiesInClass,
subPropertyOfRelations = allSubPropertyOfRelations
)
maybePropertyAndSubproperty match {
case Some((basePropertyIri, propertyIri)) =>
throw InconsistentRepositoryDataException(
s"Class $classIri has a cardinality on property $basePropertyIri and on its subproperty $propertyIri")
case None => ()
}
if (isKnoraResourceClass) {
// If it's a resource class, all its directly defined cardinalities must be on Knora resource properties, not including knora-base:resourceProperty or knora-base:hasValue.
val cardinalitiesOnInvalidProps = directCardinalityPropertyIris.filterNot(allKnoraResourceProps)
if (cardinalitiesOnInvalidProps.nonEmpty) {
throw InconsistentRepositoryDataException(
s"Resource class $classIri has one or more cardinalities on properties that are not Knora resource properties: ${cardinalitiesOnInvalidProps
.mkString(", ")}")
}
Set(OntologyConstants.KnoraBase.ResourceProperty, OntologyConstants.KnoraBase.HasValue).foreach {
invalidProp =>
if (directCardinalityPropertyIris.contains(invalidProp.toSmartIri)) {
throw InconsistentRepositoryDataException(
s"Class $classIri has a cardinality on property $invalidProp, which is not allowed")
}
}
// Check for invalid cardinalities on boolean properties.
checkForInvalidBooleanCardinalities(
classIri = classIri,
directCardinalities = directCardinalities,
allPropertyDefs = allPropertyDefs,
schemaForErrors = InternalSchema,
errorFun = { msg: String =>
throw InconsistentRepositoryDataException(msg)
}
)
// All its base classes with Knora IRIs must also be resource classes.
for (baseClass <- classDef.subClassOf) {
if (baseClass.isKnoraDefinitionIri && !allSubClassOfRelations(baseClass).contains(
OntologyConstants.KnoraBase.Resource.toSmartIri)) {
throw InconsistentRepositoryDataException(
s"Class $classIri is a subclass of knora-base:Resource, but its base class $baseClass is not")
}
}
// It must have an rdfs:label.
if (!classDef.predicates.contains(OntologyConstants.Rdfs.Label.toSmartIri)) {
throw InconsistentRepositoryDataException(s"Class $classIri has no rdfs:label")
}
} else {
// If it's a standoff class, none of its cardinalities must be on Knora resource properties.
val cardinalitiesOnInvalidProps = directCardinalityPropertyIris.filter(allKnoraResourceProps)
if (cardinalitiesOnInvalidProps.nonEmpty) {
throw InconsistentRepositoryDataException(
s"Standoff class $classIri has one or more cardinalities on properties that are Knora resource properties: ${cardinalitiesOnInvalidProps
.mkString(", ")}")
}
// All its base classes with Knora IRIs must also be standoff classes.
for (baseClass <- classDef.subClassOf) {
if (baseClass.isKnoraDefinitionIri) {
if (isStandoffClass && !allSubClassOfRelations(baseClass).contains(
OntologyConstants.KnoraBase.StandoffTag.toSmartIri)) {
throw InconsistentRepositoryDataException(
s"Class $classIri is a subclass of knora-base:StandoffTag, but its base class $baseClass is not")
}
}
}
}
}
// Each class must be a subclass of all the classes that are subject class constraints of the properties in its cardinalities.
checkSubjectClassConstraintsViaCardinalities(
internalClassDef = classDef,
allBaseClassIris = allBaseClasses.toSet,
allClassCardinalityKnoraPropertyDefs = allPropertyDefs.filterKeys(allOwlCardinalitiesForClass.keySet),
errorSchema = InternalSchema,
errorFun = { msg: String =>
throw InconsistentRepositoryDataException(msg)
}
)
val inheritedCardinalities: Map[SmartIri, KnoraCardinalityInfo] = allOwlCardinalitiesForClass
.filterNot {
case (propertyIri, _) => directCardinalityPropertyIris.contains(propertyIri)
}
.map {
case (propertyIri, owlCardinalityInfo) =>
propertyIri -> Cardinality.owlCardinality2KnoraCardinality(propertyIri = propertyIri.toString,
owlCardinality = owlCardinalityInfo)
}
// Get the class's standoff data type, if any. A standoff class that has a datatype is a subclass of one of the classes
// in StandoffDataTypeClasses.
val standoffDataType: Set[SmartIri] = allSubClassOfRelations(classIri).toSet
.intersect(StandoffDataTypeClasses.getStandoffClassIris.map(_.toSmartIri))
if (standoffDataType.size > 1) {
throw InconsistentRepositoryDataException(
s"Class $classIri is a subclass of more than one standoff datatype: ${standoffDataType.mkString(", ")}")
}
// A class can be instantiated if it's in a built-in ontology and marked with knora-base:canBeInstantiated, or if it's
// a resource class in a project-specific ontology.
val canBeInstantiated = if (ontologyIri.isKnoraBuiltInDefinitionIri) {
classDef.predicates