--- a
+++ b/src/main/java/org/sba_research/timbus/kb/timbus/TimbusToolKnowledgeBase.java
@@ -0,0 +1,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);
+ }
+ });
+
+ }
+
+}