|
a |
|
b/src/main/java/org/sbaresearch/owl/JenaQueryFacade.java |
|
|
1 |
/**
|
|
|
2 |
* Copyright (c) 2013/2014 Verein zur Foerderung der IT-Sicherheit in Oesterreich (SBA).
|
|
|
3 |
* The work has been developed in the TIMBUS Project and the above-mentioned are Members of the TIMBUS Consortium.
|
|
|
4 |
* TIMBUS is supported by the European Union under the 7th Framework Programme for research and technological
|
|
|
5 |
* development and demonstration activities (FP7/2007-2013) under grant agreement no. 269940.
|
|
|
6 |
*
|
|
|
7 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
|
|
8 |
* the License. You may obtain a copy of the License at: http://www.apache.org/licenses/LICENSE-2.0
|
|
|
9 |
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
|
|
10 |
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including without
|
|
|
11 |
* limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTIBITLY, or FITNESS FOR A PARTICULAR
|
|
|
12 |
* PURPOSE. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise,
|
|
|
13 |
* unless required by applicable law or agreed to in writing, shall any Contributor be liable for damages, including
|
|
|
14 |
* any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this
|
|
|
15 |
* License or out of the use or inability to use the Work.
|
|
|
16 |
* See the License for the specific language governing permissions and limitation under the License.
|
|
|
17 |
*/
|
|
|
18 |
package org.sbaresearch.owl;
|
|
|
19 |
|
|
|
20 |
import ch.lambdaj.function.convert.Converter;
|
|
|
21 |
import ch.lambdaj.function.convert.PropertyExtractor;
|
|
|
22 |
import com.hp.hpl.jena.query.*;
|
|
|
23 |
import com.hp.hpl.jena.rdf.model.Model;
|
|
|
24 |
import org.apache.commons.validator.routines.UrlValidator;
|
|
|
25 |
import org.semanticweb.owlapi.apibinding.OWLManager;
|
|
|
26 |
import org.semanticweb.owlapi.model.IRI;
|
|
|
27 |
|
|
|
28 |
import java.util.List;
|
|
|
29 |
|
|
|
30 |
import static ch.lambdaj.Lambda.*;
|
|
|
31 |
|
|
|
32 |
public class JenaQueryFacade {
|
|
|
33 |
|
|
|
34 |
private final Model model;
|
|
|
35 |
|
|
|
36 |
public JenaQueryFacade(Model model) {
|
|
|
37 |
this.model = model;
|
|
|
38 |
}
|
|
|
39 |
|
|
|
40 |
public List<QuerySolution> query(String queryString) throws InvalidQueryException {
|
|
|
41 |
if (queryString == null || queryString.isEmpty()) {
|
|
|
42 |
throw new IllegalArgumentException("Query is not valid: " + queryString);
|
|
|
43 |
}
|
|
|
44 |
try {
|
|
|
45 |
QueryExecution queryExecution = QueryExecutionFactory.create(QueryFactory.create(queryString), model);
|
|
|
46 |
ResultSet results = queryExecution.execSelect();
|
|
|
47 |
return ResultSetFormatter.toList(results);
|
|
|
48 |
} catch (QueryParseException e) {
|
|
|
49 |
throw new InvalidQueryException(e);
|
|
|
50 |
}
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
public static List<String> extractColumn(List<QuerySolution> solution, String columnName) {
|
|
|
54 |
return extract(solution, on(QuerySolution.class).get(columnName).toString());
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
public static List<String> removeInColumn(List<String> items, final String needle) {
|
|
|
58 |
// this does not work because of limitations in lambdaj: https://code.google.com/p/lambdaj/wiki/KnownLimitations
|
|
|
59 |
//return extract(items, on(String.class).replace(needle, ""));
|
|
|
60 |
return convert(items, new PropertyExtractor<String, String>(needle) {
|
|
|
61 |
@Override
|
|
|
62 |
public String convert(String o) {
|
|
|
63 |
return o.replaceAll(needle, "");
|
|
|
64 |
}
|
|
|
65 |
});
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
public static List<String> removeNamespace(List<String> items) {
|
|
|
69 |
return convert(items, new Converter<String, String>() {
|
|
|
70 |
@Override
|
|
|
71 |
public String convert(String o) {
|
|
|
72 |
return OwlApiFacade.getFragment(IRI.create(o));
|
|
|
73 |
}
|
|
|
74 |
});
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
public static Model getModelFromPath(String path, boolean ignoreMissingImports) throws UnableToReadModelException {
|
|
|
78 |
try {
|
|
|
79 |
// fallback, jena seems to have issues with ontologies in the owl format
|
|
|
80 |
OwlApiFacade api = new OwlApiFacade(OWLManager.createOWLOntologyManager(), OWLManager.getOWLDataFactory());
|
|
|
81 |
UrlValidator defaultValidator = new UrlValidator();
|
|
|
82 |
if (!defaultValidator.isValid(path)) path = "file://" + path;
|
|
|
83 |
api.load(path);
|
|
|
84 |
return api.getJenaModel(ignoreMissingImports);
|
|
|
85 |
} catch (Exception f) {
|
|
|
86 |
throw new UnableToReadModelException(f);
|
|
|
87 |
}
|
|
|
88 |
}
|
|
|
89 |
}
|