/
replaceClassCardinalities.scala.txt
135 lines (121 loc) · 4.95 KB
/
replaceClassCardinalities.scala.txt
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
@*
* 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/>.
*@
@import org.knora.webapi._
@import org.knora.webapi.messages.SmartIri
@import org.knora.webapi.messages.v2.responder.ontologymessages._
@import org.knora.webapi.messages.v2.responder.ontologymessages.Cardinality._
@import java.time.Instant
@*
* Replace's a class's cardinalities with new ones.
*
* @param triplestore the name of the triplestore being used.
* @param ontologyNamedGraphIri the IRI of the named graph where the ontology is stored.
* @param ontologyIri the IRI of the ontology containing the class.
* @param classIri the IRI of the class to be updated.
* @param newCardinalities the class's new cardinalities.
* @param lastModificationDate the xsd:dateTimeStamp that was attached to the ontology when it was last modified.
* @param currentTime an xsd:dateTimeStamp that will be attached to the ontology.
*@
@(triplestore: String,
ontologyNamedGraphIri: SmartIri,
ontologyIri: SmartIri,
classIri: SmartIri,
newCardinalities: Map[SmartIri, KnoraCardinalityInfo],
lastModificationDate: Instant,
currentTime: Instant)
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX knora-base: <http://www.knora.org/ontology/knora-base#>
PREFIX salsah-gui: <http://www.knora.org/ontology/salsah-gui#>
@* Delete the existing cardinalities and insert the new ones in separate update operations,
because the WHERE clause for deleting the existing ones returns several solutions.
If the INSERT was done in the same update, it would be run once for each solution,
which would cause redundant blank nodes to be inserted. *@
DELETE {
GRAPH <@ontologyNamedGraphIri> {
<@classIri> rdfs:subClassOf ?restriction .
?restriction ?restrictionPred ?restrictionObj .
}
}
@* Ensure that inference is not used in the WHERE clause of this update. *@
@if(triplestore.startsWith("graphdb")) {
USING <http://www.ontotext.com/explicit>
}
WHERE {
GRAPH <@ontologyNamedGraphIri> {
<@ontologyIri> rdf:type owl:Ontology ;
knora-base:lastModificationDate "@lastModificationDate"^^xsd:dateTime .
<@classIri> rdf:type owl:Class .
OPTIONAL {
<@classIri> rdfs:subClassOf ?restriction .
FILTER isBlank(?restriction)
?restriction rdf:type owl:Restriction ;
?restrictionPred ?restrictionObj .
}
}
};
INSERT {
GRAPH <@ontologyNamedGraphIri> {
@for((propertyIri, knoraCardinality) <- newCardinalities) {
@defining(Cardinality.knoraCardinality2OwlCardinality(knoraCardinality)) { owlCardinalityInfo =>
<@classIri> rdfs:subClassOf [ rdf:type owl:Restriction ;
owl:onProperty <@propertyIri> ;
@owlCardinalityInfo.guiOrder match {
case Some(guiOrder) => {
salsah-gui:guiOrder "@guiOrder"^^xsd:nonNegativeInteger ;
}
case None => {}
}
<@owlCardinalityInfo.owlCardinalityIri> "@owlCardinalityInfo.owlCardinalityValue"^^xsd:nonNegativeInteger ] .
}
}
}
}
@* Ensure that inference is not used in the WHERE clause of this update. *@
@if(triplestore.startsWith("graphdb")) {
USING <http://www.ontotext.com/explicit>
}
WHERE {
GRAPH <@ontologyNamedGraphIri> {
<@ontologyIri> rdf:type owl:Ontology ;
knora-base:lastModificationDate "@lastModificationDate"^^xsd:dateTime .
<@classIri> rdf:type owl:Class .
}
};
DELETE {
GRAPH <@ontologyNamedGraphIri> {
<@ontologyIri> knora-base:lastModificationDate "@lastModificationDate"^^xsd:dateTime .
}
} INSERT {
GRAPH <@ontologyNamedGraphIri> {
<@ontologyIri> knora-base:lastModificationDate "@currentTime"^^xsd:dateTime .
}
}
@* Ensure that inference is not used in the WHERE clause of this update. *@
@if(triplestore.startsWith("graphdb")) {
USING <http://www.ontotext.com/explicit>
}
WHERE {
GRAPH <@ontologyNamedGraphIri> {
<@ontologyIri> rdf:type owl:Ontology ;
knora-base:lastModificationDate "@lastModificationDate"^^xsd:dateTime .
}
}