/**
* 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 net.timbusproject.dpes.alternative.AlternativesBuilder;
import ch.lambdaj.function.convert.Converter;
import ch.lambdaj.function.matcher.Predicate;
import com.hp.hpl.jena.query.QuerySolution;
import net.timbusproject.dpes.alternative.PreservationAlternative;
import net.timbusproject.dpes.alternative.Risk;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
import org.sba_research.timbus.kb.ToolKnowledgeBase;
import org.sbaresearch.owl.AnnotationNotFoundException;
import org.sbaresearch.owl.JenaQueryFacade;
import org.sbaresearch.owl.OwlApiFacade;
import org.sbaresearch.owl.OwlElementNotFoundException;
import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLNamedIndividual;
import org.semanticweb.owlapi.model.OWLOntologyCreationException;
import org.semanticweb.owlapi.model.OWLOntologyStorageException;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import static ch.lambdaj.Lambda.convert;
import static ch.lambdaj.Lambda.filter;
/**
* Creates alternative ontologies where tools at risk are replaced.
*/
public class ToolAlternativesBuilder implements PreservationAlternativesBuilder {
private static final org.slf4j.Logger LOG = LoggerFactory.getLogger(ToolAlternativesBuilder.class);
private final ToolKnowledgeBase knowledgeBase;
private final String baseURL;
private final OwlApiFacade originalModelFacade;
private final String identifier;
private boolean allowGeneratingFakeAlternatives = false;
public ToolAlternativesBuilder(ToolKnowledgeBase knowledgeBase, String baseURL, OwlApiFacade originalModelFacade, String identifier) {
this.knowledgeBase = knowledgeBase;
this.baseURL = baseURL;
this.originalModelFacade = originalModelFacade;
this.identifier = identifier;
}
@Override
public List<PreservationAlternative> createAlternatives(Risk identifiedRisk, IRI individualAtRisk) throws OWLOntologyStorageException, OWLOntologyCreationException, IOException, OwlElementNotFoundException {
LOG.info("Searching for tool alternatives...");
List<PreservationAlternative> alternatives = new ArrayList<>();
String affectedResourceName = AlternativesBuilderHelper.getFragmentOfResource(originalModelFacade, identifiedRisk.getAffectedResource());
List<String> alternativeTools = getAlternativeTools(originalModelFacade, affectedResourceName);
ChangesOntologyBuilder changesOntologyBuilder = new ChangesOntologyBuilder(
originalModelFacade.getOntology(), originalModelFacade.getIndividual(individualAtRisk.toString()), "SoftwareReplacement");
if (alternativeTools.size() < 1) {
LOG.warn("No alternatives found for risk: " + identifiedRisk.getRiskIdentifier());
if (allowGeneratingFakeAlternatives) {
LOG.info("Using fake alternatives for test purposes...");
for (int i = 0; i < 3; ++i) {
String replacingIndividual = affectedResourceName + "-replacement_" + i;
final String alternativeShortDescription = "Replacing identified software " + affectedResourceName + " with " + replacingIndividual;
LOG.info(alternativeShortDescription);
String alternativeOntologyIri = AlternativesBuilderHelper.createIriFragment(baseURL, identifier, identifiedRisk, i);//, originalModel.getOntologyID().getOntologyIRI());
String iriSuffix = identifier + "/" + i;
alternatives.add(AlternativesBuilderHelper.createAlternativeOntology(originalModelFacade.getOntology(), identifiedRisk,
individualAtRisk, replacingIndividual, alternativeOntologyIri, changesOntologyBuilder, iriSuffix, alternativeShortDescription));
}
}
} else {
for (int i = 0; i < alternativeTools.size(); ++i) {
final String alternativeShortDescription = "Replacing identified software " + affectedResourceName + " with " + alternativeTools.get(i);
LOG.info(alternativeShortDescription);
String alternativeOntologyIri = AlternativesBuilderHelper.createIriFragment(baseURL, identifier, identifiedRisk, i);//, originalModel.getOntologyID().getOntologyIRI());
String iriSuffix = identifier + "/" + i;
alternatives.add(AlternativesBuilderHelper.createAlternativeOntology(originalModelFacade.getOntology(), identifiedRisk,
individualAtRisk, alternativeTools.get(i), alternativeOntologyIri, changesOntologyBuilder, iriSuffix, alternativeShortDescription));
}
}
return alternatives;
}
private List<String> getAlternativeTools(OwlApiFacade originalModelFacade, String affectedResourceName) throws OWLOntologyStorageException, OWLOntologyCreationException, IOException, OwlElementNotFoundException {
// get alternatives based on the tool
IRI individualByFragmentAndLabel = originalModelFacade.findIndividualByFragmentAndLabel(affectedResourceName);
if (individualByFragmentAndLabel == null) return new ArrayList<>();
OWLNamedIndividual affectedIndiv = originalModelFacade.getIndividual(individualByFragmentAndLabel.toString());
String affectedResourceFragment = originalModelFacade.createIri(affectedResourceName).getFragment();
try {
affectedResourceFragment = originalModelFacade.hasLabel(affectedIndiv) ? originalModelFacade.getLabel(affectedIndiv) : affectedResourceFragment;
} catch (AnnotationNotFoundException e) {
LOG.error("Unable to get label for resource: " + affectedResourceName + ". Using resource name as fallback.");
}
LOG.info("Getting alternatives for tool: " + affectedResourceFragment);
List<String> alternativeTools = knowledgeBase.getAlternativeTools(affectedResourceFragment);
// get alternatives based on the formats
JenaQueryFacade originalModelQueryFacade = new JenaQueryFacade(originalModelFacade.getJenaModel(false));
List<Pair<String, String>> formatPairs = convertToIDs(originalModelQueryFacade.query(QueryHelper.getQueryFormatPairs(affectedResourceName)), originalModelQueryFacade);
for (Pair<String, String> formatPair : formatPairs) {
alternativeTools.addAll(JenaQueryFacade.removeNamespace(knowledgeBase.getSupportingTools(formatPair)));
}
return filter(new Predicate<String>() {
@Override
public boolean apply(String s) {
if (s == null || s.isEmpty()) return false;
return true;
}
}, alternativeTools);
}
private List<Pair<String, String>> convertToIDs(List<QuerySolution> query, final JenaQueryFacade queryFacade) {
return convert(query, new Converter<QuerySolution, Pair<String, String>>() {
public Pair<String, String> convert(QuerySolution solution) {
return new ImmutablePair<>(getID(solution, "readFormat", queryFacade), getID(solution, "writeFormat", queryFacade));
}
});
}
private String getID(QuerySolution solution, String formatIndex, JenaQueryFacade queryFacade) {
String format = solution.contains(formatIndex) ? solution.get(formatIndex).toString() : null;
if (format == null) return null;
List<String> pronomIDs = JenaQueryFacade.extractColumn(queryFacade.query(QueryHelper.getQueryGetFormatID(format)), "pronom_id");
return (pronomIDs.size() > 0 ? pronomIDs.get(0) : format);
}
}