Child: [f38528] (diff)

Download this file

PackageKnowledgeBase.java    168 lines (148 with data), 8.2 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
/**
* 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;
import ch.lambdaj.function.convert.Converter;
import com.hp.hpl.jena.query.QuerySolution;
import org.apache.commons.lang3.tuple.Pair;
import org.sbaresearch.owl.JenaQueryFacade;
import java.util.List;
import static ch.lambdaj.Lambda.convert;
/**
* @author Rudolf Mayer
* @author Johannes Binder
*/
public class PackageKnowledgeBase {
private final JenaQueryFacade queryFacade;
public PackageKnowledgeBase(JenaQueryFacade jenaQueryFacade) {
this.queryFacade = jenaQueryFacade;
}
public List<String> getAllPackageNames() {
String queryString = "" +
"PREFIX cudf: <http://timbus.teco.edu/ontologies/DSOs/CUDF.owl#>\n" +
"\n" +
"SELECT ?package\n" +
"WHERE {\n" +
" ?package a cudf:Package .\n" +
"}\n" +
"GROUP BY ?package\n" +
"ORDER BY ?package\n";
return JenaQueryFacade.removeNamespace(JenaQueryFacade.extractColumn(queryFacade.query(queryString), "package"));
}
public List<String> getAllVirtualPackageNames() {
String queryString = "" +
"PREFIX cudf: <http://timbus.teco.edu/ontologies/DSOs/CUDF.owl#>\n" +
"\n" +
"SELECT ?virtualPackage\n" +
"WHERE {\n" +
" ?virtualPackage a cudf:VirtualPackage .\n" +
"}\n" +
"GROUP BY ?virtualPackage\n" +
"ORDER BY ?virtualPackage\n";
return JenaQueryFacade.removeNamespace(JenaQueryFacade.extractColumn(queryFacade.query(queryString), "virtualPackage"));
}
public List<String> getAlternativePackages(String packageName) {
String queryString = ("" +
"PREFIX kbp: <http://timbus.teco.edu/ontologies/KB/PackageAlternativesUbuntu_12_10_amd64.owl#>\n" +
"PREFIX cudf: <http://timbus.teco.edu/ontologies/DSOs/CUDF.owl#>\n" +
"\n" +
"SELECT ?alternative\n" +
"WHERE {\n" +
" kbp:[package] cudf:provides ?virtual .\n" +
" ?alternative cudf:provides ?virtual .\n" +
" ?alternative a cudf:Package .\n" +
"}\n" +
"GROUP BY ?alternative\n" +
"HAVING (?alternative != kbp:[package])").replace("[package]", packageName);
return JenaQueryFacade.removeNamespace(JenaQueryFacade.extractColumn(queryFacade.query(queryString), "alternative"));
}
public List<String> getAlternativePackages(String packageName, String virtualPackage) {
String queryString = ("" +
"PREFIX kbp: <http://timbus.teco.edu/ontologies/KB/PackageAlternativesUbuntu_12_10_amd64.owl#>\n" +
"PREFIX cudf: <http://timbus.teco.edu/ontologies/DSOs/CUDF.owl#>\n" +
"\n" +
"SELECT ?alternative\n" +
"WHERE {\n" +
" ?alternative cudf:provides kbp:[virtual_package] .\n" +
" ?alternative a cudf:Package .\n" +
"}\n" +
"GROUP BY ?alternative\n" +
"HAVING (?alternative != kbp:[package])").replace("[package]", packageName).replace("[virtual_package]", virtualPackage);
return JenaQueryFacade.removeNamespace(JenaQueryFacade.extractColumn(queryFacade.query(queryString), "alternative"));
}
public List<String> getProvidedVirtualPackages(String packageIRI1, String packageIRI2) {
String queryString = ("" +
"PREFIX kbp: <http://timbus.teco.edu/ontologies/KB/PackageAlternativesUbuntu_12_10_amd64.owl#>\n" +
"PREFIX cudf: <http://timbus.teco.edu/ontologies/DSOs/CUDF.owl#>\n" +
"\n" +
"SELECT ?provided\n" +
"WHERE {\n" +
" kbp:[package1] cudf:provides ?provided .\n" +
" kbp:[package2] cudf:provides ?provided .\n" +
"}\n" +
"GROUP BY ?provided\n" +
"ORDER BY ?provided").replace("[package1]", packageIRI1).replace("[package2]", packageIRI2);
List<QuerySolution> querySolution = queryFacade.query(queryString);
return JenaQueryFacade.removeNamespace(JenaQueryFacade.extractColumn(querySolution, "provided"));
}
public List<String> getProvidedVirtualPackages(String packageIRI) {
String queryString = ("" +
"PREFIX kbp: <http://timbus.teco.edu/ontologies/KB/PackageAlternativesUbuntu_12_10_amd64.owl#>\n" +
"PREFIX cudf: <http://timbus.teco.edu/ontologies/DSOs/CUDF.owl#>\n" +
"\n" +
"SELECT ?virtual\n" +
"WHERE {\n" +
" kbp:[package] cudf:provides ?virtual .\n" +
" ?virtual a cudf:VirtualPackage .\n" +
"}\n" +
"GROUP BY ?virtual\n" +
"ORDER BY ?virtual").replace("[package]", packageIRI);
return JenaQueryFacade.removeNamespace(JenaQueryFacade.extractColumn(queryFacade.query(queryString), "virtual"));
}
/**
* Note: This expects the ontology to be the model where replacements should be performed, not the package knowledgebase.
* TODO: move to a separate class
*/
public List<String> getVirtualPackages(String packageIRI) {
String queryString = ("" +
"PREFIX cudf: <http://timbus.teco.edu/ontologies/DSOs/CUDF.owl#>\n" +
"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n" +
"\n" +
"SELECT ?iri (str(?label) as ?strLabel)\n" +
"WHERE {\n" +
" ?iri cudf:providedBy <[packageIRI]> .\n" +
" ?iri a cudf:VirtualPackage .\n" +
" OPTIONAL { ?iri rdfs:label ?label }\n" +
"}\n" +
"GROUP BY ?iri ?label\n").replace("[packageIRI]", packageIRI);
return JenaQueryFacade.removeNamespace(extractIriOrLabel(queryFacade.query(queryString)));
}
/**
* TODO: move to jena facade
*/
public static List<String> extractIriOrLabel(List<QuerySolution> solution) {
return convert(solution, new Converter<QuerySolution, String>() {
@Override
public String convert(QuerySolution solution) {
String label = solution.get("strLabel").toString();
String iri = solution.get("iri").toString();
if (label != null && !label.isEmpty()) return label;
return iri;
}
});
}
}