Child: [e10b76] (diff)

Download this file

TimbusToolKnowledgeBase.java    322 lines (306 with data), 16.8 kB

  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
/**
* Copyright (c) 2013/2014 Verein zur Foerderung der IT-Sicherheit in Oesterreich (SBA).
* The work has been developed in the TIMBUS Project and the above-mentioned are Members of the TIMBUS Consortium.
* TIMBUS is supported by the European Union under the 7th Framework Programme for research and technological
* development and demonstration activities (FP7/2007-2013) under grant agreement no. 269940.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at: http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including without
* limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTIBITLY, or FITNESS FOR A PARTICULAR
* PURPOSE. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise,
* unless required by applicable law or agreed to in writing, shall any Contributor be liable for damages, including
* any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this
* License or out of the use or inability to use the Work.
* See the License for the specific language governing permissions and limitation under the License.
*/
package org.sba_research.timbus.kb.timbus;
import ch.lambdaj.function.convert.Converter;
import com.hp.hpl.jena.query.QuerySolution;
import org.apache.commons.lang3.tuple.Pair;
import org.sba_research.timbus.kb.FormatMigrationOption;
import org.sba_research.timbus.kb.ToolKnowledgeBase;
import org.sbaresearch.owl.JenaQueryFacade;
import java.util.List;
import static ch.lambdaj.Lambda.convert;
public class TimbusToolKnowledgeBase implements ToolKnowledgeBase {
private final JenaQueryFacade queryFacade;
public TimbusToolKnowledgeBase(JenaQueryFacade queryFacade) {
this.queryFacade = queryFacade;
}
@Override
public List<String> getProvidingTools(String action) {
String queryString = "" +
"PREFIX kb: <http://timbus.teco.edu/ontologies/preservationIdentifier/toolKB.owl#>\n" +
"PREFIX kbi: <http://timbus.teco.edu/ontologies/preservationIdentifier/toolKB_instance.owl#>\n" +
"\n" +
"SELECT ?tool\n" +
"{\n" +
" ?tool kb:isProviding ?toolAction .\n" +
" ?toolAction kb:isProviding kbi:" + action + " .\n" +
"}\n" +
"GROUP BY ?tool";
return JenaQueryFacade.extractColumn(queryFacade.query(queryString), "tool");
}
@Override
public List<String> getAlternativeTools(String tool) {
String queryString = "" +
"PREFIX kb: <http://timbus.teco.edu/ontologies/preservationIdentifier/toolKB.owl#>\n" +
"PREFIX kbi: <http://timbus.teco.edu/ontologies/preservationIdentifier/toolKB_instance.owl#>\n" +
"\n" +
"SELECT ?tool (COUNT(?requiredAction) as ?satisfiedActionCount) (max(?requiredActionCount) as ?maxRequiredActionCount)\n" +
"{\n" +
" {\n" +
" SELECT (?abstractAction as ?requiredAction)\n" +
" WHERE {\n" +
" kbi:" + tool + " kb:isProviding ?toolAction .\n" +
" ?toolAction kb:isProviding ?abstractAction .\n" +
" }\n" +
" }\n" +
" { \n" +
" SELECT (count(*) as ?requiredActionCount)\n" +
" WHERE {\n" +
" kbi:" + tool + " kb:isProviding ?toolAction .\n" +
" ?toolAction kb:isProviding ?abstractAction .\n" +
" }\n" +
" }\n" +
" ?tool kb:isProviding ?toolAction .\n" +
" ?toolAction kb:isProviding ?requiredAction .\n" +
" MINUS {kbi:" + tool + " kb:isProviding ?toolAction}\n" +
"}\n" +
"GROUP BY ?tool\n" +
"HAVING (?satisfiedActionCount = ?maxRequiredActionCount)";
return JenaQueryFacade.extractColumn(queryFacade.query(queryString), "tool");
}
@Override
public List<String> getSupportingTools(Pair<String, String> format) {
String queryString = "" +
"PREFIX kb: <http://timbus.teco.edu/ontologies/preservationIdentifier/toolKB.owl#>\n" +
"PREFIX kbi: <http://timbus.teco.edu/ontologies/preservationIdentifier/toolKB_instance.owl#>\n" +
"\n" +
"SELECT ?key ?value ?format ?action ?tool\n" +
"WHERE\n" +
"{\n";
if (format.getLeft() != null) {
queryString += "" +
"# get read format by pronom id\n" +
" ?entry_r kb:hasKey ?key_r .\n" +
" ?entry_r kb:hasValue ?value_r .\n" +
" FILTER(str(?key_r) = \"id\") .\n" +
" FILTER(str(?value_r) = \"" + format.getLeft() + "\") .\n" + // TODO: consider multiple in/outputs
" ?registry_r kb:isConsistingOf ?entry_r .\n" +
" ?format_r kb:isIdentifiedBy ?registry_r .\n";
}
if (format.getRight() != null) {
queryString += "" +
"# get write format by pronom id\n" +
" ?entry_w kb:hasKey ?key_w .\n" +
" ?entry_w kb:hasValue ?value_w .\n" +
" FILTER(str(?key_w) = \"id\") .\n" +
" FILTER(str(?value_w) = \"" + format.getRight() + "\") .\n" +
" ?registry_w kb:isConsistingOf ?entry_w .\n" +
" ?format_w kb:isIdentifiedBy ?registry_w .\n" +
"\n";
}
if (format.getLeft() != null) {
queryString += "" +
" ?abstract_action_read kb:isReading ?format_r .\n" +
" ?action_read kb:isProviding ?abstract_action_read .\n" +
" ?tool kb:isProviding ?action_read .\n" +
"\n";
}
if (format.getRight() != null) {
queryString += "" +
" ?abstract_action_write kb:isWriting ?format_w .\n" +
" ?action_write kb:isProviding ?abstract_action_write .\n" +
" ?tool kb:isProviding ?action_write .\n" +
"\n";
}
queryString += "" +
"}";
return JenaQueryFacade.extractColumn(queryFacade.query(queryString), "tool");
}
@Override
public List<String> getAllFormatIDs() {
String queryString = "" +
"PREFIX kb: <http://timbus.teco.edu/ontologies/preservationIdentifier/toolKB.owl#>\n" +
"PREFIX kbi: <http://timbus.teco.edu/ontologies/preservationIdentifier/toolKB_instance.owl#>\n" +
"\n" +
"SELECT (str(?value) as ?id)\n" + // DISTINCT
"WHERE\n" +
"{\n" +
" ?entry kb:hasKey ?key .\n" +
" ?entry kb:hasValue ?value .\n" +
" FILTER(str(?key) = \"id\") .\n" +
" FILTER(str(?value) != \"fmt/null\") .\n" +
"}\n" +
"GROUP BY ?value\n";
return JenaQueryFacade.extractColumn(queryFacade.query(queryString), "id");
}
/**
* Get a list of alternative formats to a given format.
* Alternative formats are those that can be used by all tools that support the given format.
* TODO: at the moment considers reading actions only, consider writing formats also!
*
* @param formatID The Pronom ID of a format.
* @return The IRI of alternative formats
*
*/
@Deprecated
public List<String> getAlternativeFormats(String formatID) {
String queryString = "" +
"PREFIX kb: <http://timbus.teco.edu/ontologies/preservationIdentifier/toolKB.owl#>\n" +
"PREFIX kbi: <http://timbus.teco.edu/ontologies/preservationIdentifier/toolKB_instance.owl#>\n" +
"\n" +
"SELECT ?alternative_format (COUNT(?tool) as ?supportedBy)\n" +
"WHERE\n" +
"{\n" +
"# get supporting tools\n" +
" {\n" +
" SELECT ?tool\n" +
" WHERE {\n" +
" ?entry_r kb:hasKey ?key_r .\n" +
" ?entry_r kb:hasValue ?value_r .\n" +
" FILTER(str(?key_r) = \"id\") .\n" +
" FILTER(str(?value_r) = \"[id]\") .\n" +
" ?registry_r kb:isConsistingOf ?entry_r .\n" +
" ?format_r kb:isIdentifiedBy ?registry_r .\n" +
"\n" +
" ?abstract_action_read kb:isReading ?format_r .\n" +
" ?action_read kb:isProviding ?abstract_action_read .\n" +
" ?tool kb:isProviding ?action_read .\n" +
" ?tool a kb:Tool .\n" +
" }\n" +
" }\n" +
"# count supporting tools\n" +
" {\n" +
" SELECT (COUNT(?tool) as ?tool_count)\n" +
" WHERE {\n" +
" ?entry_r kb:hasKey ?key_r .\n" +
" ?entry_r kb:hasValue ?value_r .\n" +
" FILTER(str(?key_r) = \"id\") .\n" +
" FILTER(str(?value_r) = \"[id]\") .\n" +
" ?registry_r kb:isConsistingOf ?entry_r .\n" +
" ?format_r kb:isIdentifiedBy ?registry_r .\n" +
"\n" +
" ?abstract_action_read kb:isReading ?format_r .\n" +
" ?action_read kb:isProviding ?abstract_action_read .\n" +
" ?tool kb:isProviding ?action_read .\n" +
" ?tool a kb:Tool .\n" +
" }\n" +
" }\n" +
"# get all alternative formats\n" +
" {\n" +
" SELECT ?alternative_format\n" +
" WHERE\n" +
" {\n" +
" ?entry_r kb:hasKey ?key_r .\n" +
" ?entry_r kb:hasValue ?value_r .\n" +
" FILTER(str(?key_r) = \"id\") .\n" +
" FILTER(str(?value_r) = \"[id]\") .\n" +
" ?registry_r kb:isConsistingOf ?entry_r .\n" +
" ?format_r kb:isIdentifiedBy ?registry_r .\n" +
"\n" +
" ?abstract_action_read kb:isReading ?format_r .\n" +
" ?action_read kb:isProviding ?abstract_action_read .\n" +
" ?tool kb:isProviding ?action_read .\n" +
" ?tool a kb:Tool .\n" +
"\n" +
" ?tool kb:isProviding ?alternative_action_read .\n" +
" ?alternative_action_read kb:isProviding ?alternative_abstract_action_read .\n" +
" ?alternative_abstract_action_read kb:isReading ?alternative_format .\n" +
" }\n" +
" GROUP BY ?alternative_format\n" +
" }\n" +
" ?abstract_action_read kb:isReading ?alternative_format .\n" +
"\n" +
"# map tools to formats\n" +
" ?tool kb:isProviding ?tool_action_read .\n" +
" ?tool_action_read kb:isProviding ?abstract_action_read\n" +
"}\n" +
"GROUP BY ?alternative_format\n" +
"HAVING (?supportedBy = (MAX(?tool_count)))\n";
return JenaQueryFacade.extractColumn(queryFacade.query(queryString.replace("[id]", formatID)), "alternative_format");
}
@Override
public List<String> getFormatName(String formatID) {
String queryString = "" +
"PREFIX kb: <http://timbus.teco.edu/ontologies/preservationIdentifier/toolKB.owl#>\n" +
"PREFIX kbi: <http://timbus.teco.edu/ontologies/preservationIdentifier/toolKB_instance.owl#>\n" +
"\n" +
"SELECT ?format\n" +
"WHERE {\n" +
" ?entry kb:hasKey ?key .\n" +
" ?entry kb:hasValue ?value .\n" +
" FILTER(str(?key) = \"id\") .\n" +
" FILTER(str(?value) = \"[id]\") .\n" +
" ?registry kb:isConsistingOf ?entry .\n" +
" ?format kb:isIdentifiedBy ?registry .\n" +
"}\n";
return JenaQueryFacade.extractColumn(queryFacade.query(queryString.replace("[id]", formatID)), "format");
}
/**
* Get a list of tools that read a given format.
* @param formatID The Pronom ID of the format.
* @return A list of tool IRIs.
*/
public List<String> getReadingTools(String formatID) {
String queryString = "" +
"PREFIX kb: <http://timbus.teco.edu/ontologies/preservationIdentifier/toolKB.owl#>\n" +
"PREFIX kbi: <http://timbus.teco.edu/ontologies/preservationIdentifier/toolKB_instance.owl#>\n" +
"\n" +
"SELECT ?tool\n" +
"WHERE {\n" +
" ?entry_r kb:hasKey ?key_r .\n" +
" ?entry_r kb:hasValue ?value_r .\n" +
" FILTER(str(?key_r) = \"id\") .\n" +
" FILTER(str(?value_r) = \"[id]\") .\n" +
" ?registry_r kb:isConsistingOf ?entry_r .\n" +
" ?format_r kb:isIdentifiedBy ?registry_r .\n" +
"\n" +
" ?abstract_action_read kb:isReading ?format_r .\n" +
" ?action_read kb:isProviding ?abstract_action_read .\n" +
" ?tool kb:isProviding ?action_read .\n" +
" ?tool a kb:Tool .\n" +
"}\n";
return JenaQueryFacade.extractColumn(queryFacade.query(queryString.replace("[id]", formatID)), "tool");
}
@Override
public List<FormatMigrationOption> getFormatMigrationOption(String formatID) {
String queryString = "" +
"PREFIX kb: <http://timbus.teco.edu/ontologies/preservationIdentifier/toolKB.owl#>\n" +
"PREFIX kbi: <http://timbus.teco.edu/ontologies/preservationIdentifier/toolKB_instance.owl#>\n" +
"\n" +
"SELECT ?alternative_format ?tool\n" +
"WHERE\n" +
"{\n" +
" ?entry_r kb:hasKey ?key_r .\n" +
" ?entry_r kb:hasValue ?value_r .\n" +
" FILTER(str(?key_r) = \"id\") .\n" +
" FILTER(str(?value_r) = \"[id]\") .\n" +
" ?registry_r kb:isConsistingOf ?entry_r .\n" +
" ?format_r kb:isIdentifiedBy ?registry_r .\n" +
"\n" +
" ?abstract_action_read kb:isReading ?format_r .\n" +
" ?action_read kb:isProviding ?abstract_action_read .\n" +
" ?tool kb:isProviding ?action_read .\n" +
" ?tool a kb:Tool .\n" +
"\n" +
" ?tool kb:isProviding ?action_write .\n" +
" ?action_write kb:isProviding ?abstract_action_write .\n" +
" ?abstract_action_write kb:isWriting ?alternative_format .\n" +
"\n" +
" FILTER NOT EXISTS { ?abstract_action_write kb:isReading ?read_format . }\n" +
"}\n";
List<QuerySolution> solution = queryFacade.query(queryString.replace("[id]", formatID));
return convert(solution, new Converter<QuerySolution, FormatMigrationOption>() {
@Override
public FormatMigrationOption convert(QuerySolution querySolution) {
String tool = querySolution.get("tool").toString();
String format = querySolution.get("alternative_format").toString();
return new FormatMigrationOption(format, tool);
}
});
}
}