|
a |
|
b/src/net/timbusproject/dpes/alternative/AlternativesBuilder/ToolAlternativesBuilder.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 |
|
|
|
19 |
package net.timbusproject.dpes.alternative.AlternativesBuilder;
|
|
|
20 |
|
|
|
21 |
import ch.lambdaj.function.convert.Converter;
|
|
|
22 |
import com.hp.hpl.jena.query.QuerySolution;
|
|
|
23 |
import net.timbusproject.dpes.alternative.PreservationAlternative;
|
|
|
24 |
import net.timbusproject.dpes.alternative.PreservationIdentifierException;
|
|
|
25 |
import net.timbusproject.dpes.alternative.Risk;
|
|
|
26 |
import org.apache.commons.lang3.tuple.ImmutablePair;
|
|
|
27 |
import org.apache.commons.lang3.tuple.Pair;
|
|
|
28 |
import org.sba_research.timbus.kb.ToolKnowledgeBase;
|
|
|
29 |
import org.sbaresearch.owl.AlternativesBuilder;
|
|
|
30 |
import org.sbaresearch.owl.JenaQueryFacade;
|
|
|
31 |
import org.sbaresearch.owl.OwlApiFacade;
|
|
|
32 |
import org.sbaresearch.owl.OwlElementNotFoundException;
|
|
|
33 |
import org.semanticweb.owlapi.model.*;
|
|
|
34 |
|
|
|
35 |
import java.io.IOException;
|
|
|
36 |
import java.util.ArrayList;
|
|
|
37 |
import java.util.List;
|
|
|
38 |
|
|
|
39 |
import static ch.lambdaj.Lambda.convert;
|
|
|
40 |
|
|
|
41 |
/**
|
|
|
42 |
* Creates alternative ontologies where tools at risk are replaced.
|
|
|
43 |
*/
|
|
|
44 |
public class ToolAlternativesBuilder implements PreservationAlternativesBuilder {
|
|
|
45 |
|
|
|
46 |
private final ToolKnowledgeBase knowledgeBase;
|
|
|
47 |
private final String baseURL;
|
|
|
48 |
private final OwlApiFacade originalModelFacade;
|
|
|
49 |
private final String identifier;
|
|
|
50 |
|
|
|
51 |
public ToolAlternativesBuilder(ToolKnowledgeBase knowledgeBase, String baseURL, OwlApiFacade originalModelFacade, String identifier) {
|
|
|
52 |
this.knowledgeBase = knowledgeBase;
|
|
|
53 |
this.baseURL = baseURL;
|
|
|
54 |
this.originalModelFacade = originalModelFacade;
|
|
|
55 |
this.identifier = identifier;
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
@Override
|
|
|
59 |
public List<PreservationAlternative> createAlternatives(Risk identifiedRisk, IRI individualAtRisk) throws OWLOntologyStorageException, OWLOntologyCreationException, IOException, OwlElementNotFoundException {
|
|
|
60 |
List<PreservationAlternative> alternatives = new ArrayList<>();
|
|
|
61 |
String affectedResourceName = identifiedRisk.getAffectedResource();
|
|
|
62 |
List<String> alternativeTools = getAlternativeTools(originalModelFacade, affectedResourceName);
|
|
|
63 |
ChangesOntologyBuilder changesOntologyBuilder = new ChangesOntologyBuilder(
|
|
|
64 |
originalModelFacade.getOntology(), originalModelFacade.getIndividual(individualAtRisk.toString()), "SoftwareReplacement");
|
|
|
65 |
if (alternativeTools.size() < 1) {
|
|
|
66 |
System.out.println("No alternatives found for risk: " + identifiedRisk.getRiskIdentifier());
|
|
|
67 |
System.out.println("Using fake alternatives for test purposes...");
|
|
|
68 |
for (int i = 0; i < 3; ++i) {
|
|
|
69 |
System.out.println("Replacing identified software " + affectedResourceName);
|
|
|
70 |
String replacingIndividual = affectedResourceName + "-replacement_" + i;
|
|
|
71 |
|
|
|
72 |
String alternativeOntologyIri = createIriFragment(baseURL, identifier, identifiedRisk, i);//, originalModel.getOntologyID().getOntologyIRI());
|
|
|
73 |
String iriSuffix = identifier + "/" + i;
|
|
|
74 |
alternatives.add(createAlternativeOntology(originalModelFacade.getOntology(), identifiedRisk,
|
|
|
75 |
individualAtRisk, replacingIndividual, alternativeOntologyIri, changesOntologyBuilder, iriSuffix));
|
|
|
76 |
}
|
|
|
77 |
} else {
|
|
|
78 |
for (int i = 0; i < alternativeTools.size(); ++i) {
|
|
|
79 |
System.out.println("Replacing identified software " + affectedResourceName);
|
|
|
80 |
String alternativeOntologyIri = createIriFragment(baseURL, identifier, identifiedRisk, i);//, originalModel.getOntologyID().getOntologyIRI());
|
|
|
81 |
String iriSuffix = identifier + "/" + i;
|
|
|
82 |
alternatives.add(createAlternativeOntology(originalModelFacade.getOntology(), identifiedRisk,
|
|
|
83 |
individualAtRisk, alternativeTools.get(i), alternativeOntologyIri, changesOntologyBuilder, iriSuffix));
|
|
|
84 |
}
|
|
|
85 |
}
|
|
|
86 |
return alternatives;
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
private List<String> getAlternativeTools(OwlApiFacade originalModelFacade, String affectedResourceName) throws OWLOntologyStorageException, OWLOntologyCreationException, IOException {
|
|
|
90 |
// get alternatives based on the tool
|
|
|
91 |
List<String> alternativeTools = knowledgeBase.getAlternativeTools(affectedResourceName);
|
|
|
92 |
// get alternatives based on the formats
|
|
|
93 |
JenaQueryFacade originalModelQueryFacade = new JenaQueryFacade(originalModelFacade.getJenaModel(false));
|
|
|
94 |
List<Pair<String, String>> formatPairs = convertToIDs(originalModelQueryFacade.query(QueryHelper.getQueryFormatPairs(affectedResourceName)), originalModelQueryFacade);
|
|
|
95 |
for (Pair<String, String> formatPair : formatPairs) {
|
|
|
96 |
alternativeTools.addAll(JenaQueryFacade.removeNamespace(knowledgeBase.getSupportingTools(formatPair)));
|
|
|
97 |
}
|
|
|
98 |
return alternativeTools;
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
|
|
|
102 |
private PreservationAlternative createAlternativeOntology(OWLOntology originalModel,
|
|
|
103 |
Risk identifiedRisk, IRI individualAtRisk,
|
|
|
104 |
String replacingIndividual, String alternativeOntologyIriPrefix, ChangesOntologyBuilder changesOntologyBuilder, String iriSuffix) throws OWLOntologyCreationException, OwlElementNotFoundException, OWLOntologyStorageException {
|
|
|
105 |
|
|
|
106 |
AlternativesBuilder alternativesBuilder = new AlternativesBuilder(new OwlApiFacade(originalModel));
|
|
|
107 |
OWLOntology modifiedModel = alternativesBuilder.copyOntology(IRI.create(alternativeOntologyIriPrefix, originalModel.getOntologyID().getOntologyIRI().getFragment()));
|
|
|
108 |
OWLOntologyManager modifiedModelOntologyManager = modifiedModel.getOWLOntologyManager();
|
|
|
109 |
|
|
|
110 |
|
|
|
111 |
IRI individualInModifiedModelModifiedIRI = null;
|
|
|
112 |
if (originalModel.containsIndividualInSignature(individualAtRisk)) {
|
|
|
113 |
individualInModifiedModelModifiedIRI = renameIndividual(individualAtRisk, replacingIndividual, modifiedModel);
|
|
|
114 |
}
|
|
|
115 |
List<String> changedOntologies = new ArrayList<>();
|
|
|
116 |
|
|
|
117 |
for (OWLOntology ontology : originalModel.getDirectImports()) {
|
|
|
118 |
if (!ontology.containsIndividualInSignature(individualAtRisk)) continue;
|
|
|
119 |
|
|
|
120 |
AlternativesBuilder alternativesBuilderImportedOntology = new AlternativesBuilder(new OwlApiFacade(ontology));
|
|
|
121 |
OWLOntology modifiedOntology = alternativesBuilderImportedOntology.copyOntology(
|
|
|
122 |
IRI.create(alternativeOntologyIriPrefix, ontology.getOntologyID().getOntologyIRI().getFragment()),
|
|
|
123 |
modifiedModel.getOWLOntologyManager()
|
|
|
124 |
);
|
|
|
125 |
individualInModifiedModelModifiedIRI = renameIndividual(individualAtRisk, replacingIndividual, modifiedOntology);
|
|
|
126 |
|
|
|
127 |
modifiedModelOntologyManager.applyChange(new RemoveImport(modifiedModel,
|
|
|
128 |
modifiedModelOntologyManager.getOWLDataFactory().getOWLImportsDeclaration(ontology.getOntologyID().getOntologyIRI())));
|
|
|
129 |
modifiedModelOntologyManager.applyChange(new AddImport(modifiedModel,
|
|
|
130 |
modifiedModelOntologyManager.getOWLDataFactory().getOWLImportsDeclaration(modifiedOntology.getOntologyID().getOntologyIRI())));
|
|
|
131 |
changedOntologies.add(OwlApiFacade.toOwlXml(modifiedOntology));
|
|
|
132 |
}
|
|
|
133 |
|
|
|
134 |
OWLNamedIndividual modifiedIndividual = modifiedModelOntologyManager.getOWLDataFactory().getOWLNamedIndividual(individualInModifiedModelModifiedIRI);
|
|
|
135 |
|
|
|
136 |
OWLOntology eventsOntology = changesOntologyBuilder.createChangesOntology(iriSuffix, modifiedModel, modifiedIndividual);
|
|
|
137 |
return new PreservationAlternative(
|
|
|
138 |
OwlApiFacade.toOwlXml(originalModel), OwlApiFacade.toOwlXml(modifiedModel),
|
|
|
139 |
OwlApiFacade.toOwlXml(eventsOntology), identifiedRisk.getRiskIdentifier(), changedOntologies);
|
|
|
140 |
}
|
|
|
141 |
|
|
|
142 |
private String createIriFragment(String baseURL, String identifier, Risk identifiedRisk, int count) {
|
|
|
143 |
return baseURL + "/" + identifier + "/" + identifiedRisk.getRiskIdentifier() + "/" + count + "/";
|
|
|
144 |
}
|
|
|
145 |
|
|
|
146 |
private IRI renameIndividual(IRI individualAtRisk, String replacingIndividual, OWLOntology modifiedModel) throws OwlElementNotFoundException {
|
|
|
147 |
OwlApiFacade model = new OwlApiFacade(modifiedModel);
|
|
|
148 |
IRI sourceIRI = model.createIri(OwlApiFacade.getFragment(individualAtRisk));
|
|
|
149 |
if (!model.containsIndividual(sourceIRI.toString())) {
|
|
|
150 |
throw new PreservationIdentifierException("Original individual not found in modified model: " + sourceIRI);
|
|
|
151 |
}
|
|
|
152 |
|
|
|
153 |
AlternativesBuilder alternativesBuilder = new AlternativesBuilder(model);
|
|
|
154 |
alternativesBuilder.renameIndividualByIRI(model.createIri(OwlApiFacade.getFragment(individualAtRisk)), replacingIndividual); // TODO: not sure why fragment only does not work... bug!
|
|
|
155 |
|
|
|
156 |
IRI targetIRI = model.createIri(replacingIndividual);
|
|
|
157 |
if (!model.containsIndividual(targetIRI.toString())) {
|
|
|
158 |
throw new PreservationIdentifierException("Modified individual not found in modified model: " + targetIRI);
|
|
|
159 |
}
|
|
|
160 |
System.out.println("\tReplaced " + sourceIRI + " with " + targetIRI);
|
|
|
161 |
|
|
|
162 |
return targetIRI;
|
|
|
163 |
}
|
|
|
164 |
|
|
|
165 |
private List<Pair<String, String>> convertToIDs(List<QuerySolution> query, final JenaQueryFacade queryFacade) {
|
|
|
166 |
return convert(query, new Converter<QuerySolution, Pair<String, String>>() {
|
|
|
167 |
public Pair<String, String> convert(QuerySolution solution) {
|
|
|
168 |
return new ImmutablePair<>(getID(solution, "readFormat", queryFacade), getID(solution, "writeFormat", queryFacade));
|
|
|
169 |
}
|
|
|
170 |
});
|
|
|
171 |
}
|
|
|
172 |
|
|
|
173 |
private String getID(QuerySolution solution, String formatIndex, JenaQueryFacade queryFacade) {
|
|
|
174 |
String format = solution.contains(formatIndex) ? solution.get(formatIndex).toString() : null;
|
|
|
175 |
if (format == null) return null;
|
|
|
176 |
List<String> pronomIDs = JenaQueryFacade.extractColumn(queryFacade.query(QueryHelper.getQueryGetFormatID(format)), "pronom_id");
|
|
|
177 |
return (pronomIDs.size() > 0 ? pronomIDs.get(0) : format);
|
|
|
178 |
}
|
|
|
179 |
}
|