|
a |
|
b/src/net/timbusproject/dpes/alternative/AlternativesBuilder/FileAlternativesBuilder.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 net.timbusproject.dpes.alternative.PreservationAlternative;
|
|
|
22 |
import net.timbusproject.dpes.alternative.Risk;
|
|
|
23 |
import org.sba_research.timbus.kb.FormatMigrationOption;
|
|
|
24 |
import org.sba_research.timbus.kb.ToolKnowledgeBase;
|
|
|
25 |
import org.sbaresearch.owl.AlternativesBuilder;
|
|
|
26 |
import org.sbaresearch.owl.JenaQueryFacade;
|
|
|
27 |
import org.sbaresearch.owl.OwlApiFacade;
|
|
|
28 |
import org.sbaresearch.owl.OwlElementNotFoundException;
|
|
|
29 |
import org.semanticweb.owlapi.apibinding.OWLManager;
|
|
|
30 |
import org.semanticweb.owlapi.model.*;
|
|
|
31 |
|
|
|
32 |
import java.io.IOException;
|
|
|
33 |
import java.util.ArrayList;
|
|
|
34 |
import java.util.List;
|
|
|
35 |
|
|
|
36 |
/**
|
|
|
37 |
* Creates alternative ontologies where files at risk are replaced.
|
|
|
38 |
*/
|
|
|
39 |
public class FileAlternativesBuilder implements PreservationAlternativesBuilder {
|
|
|
40 |
|
|
|
41 |
public static final IRI IRI_FILE = IRI.create("http://id.loc.gov/ontologies/premis.rdf#File");
|
|
|
42 |
public static final IRI IRI_FORMAT_PROPERTY = IRI.create("http://id.loc.gov/ontologies/premis.rdf#Format");
|
|
|
43 |
public static final IRI IRI_FORMAT_REGISTRY = IRI.create("http://id.loc.gov/ontologies/premis.rdf#FormatRegistry");
|
|
|
44 |
public static final IRI IRI_FORMAT_REGISTRY_PROPERTY = IRI.create("http://id.loc.gov/ontologies/premis.rdf#FormatRegistry");
|
|
|
45 |
public static final IRI IRI_FORMAT_REGISTRY_KEY_PROPERTY = IRI.create("http://www.loc.gov/premis/rdf/v1#hasFormatRegistryKey");
|
|
|
46 |
public static final IRI IRI_FORMAT_REGISTRY_NAME_PROPERTY = IRI.create("http://www.loc.gov/premis/rdf/v1#hasFormatRegistryName");
|
|
|
47 |
public static final IRI IRI_OBJECT_CHARACTERISTICS_PROPERTY = IRI.create("http://multimedialab.elis.ugent.be/users/samcoppe/ontologies/Premis/premis.owl#objectCharacteristics");
|
|
|
48 |
public static final IRI IRI_OBJECT_CHARACTERISTICS = IRI.create("http://www.loc.gov/premis/rdf/v1#ObjectCharacteristics");
|
|
|
49 |
|
|
|
50 |
private final ToolKnowledgeBase knowledgeBase;
|
|
|
51 |
private final String baseURL;
|
|
|
52 |
private final OwlApiFacade originalModelFacade;
|
|
|
53 |
private final String identifier;
|
|
|
54 |
|
|
|
55 |
public FileAlternativesBuilder(ToolKnowledgeBase knowledgeBase, String baseURL, OwlApiFacade originalModelFacade, String identifier) {
|
|
|
56 |
this.knowledgeBase = knowledgeBase;
|
|
|
57 |
this.baseURL = baseURL;
|
|
|
58 |
this.originalModelFacade = originalModelFacade;
|
|
|
59 |
this.identifier = identifier;
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
public List<PreservationAlternative> createAlternatives(Risk identifiedRisk, IRI individualAtRisk) throws OWLOntologyCreationException, OWLOntologyStorageException, IOException, OwlElementNotFoundException {
|
|
|
63 |
System.out.println("Searching for file alternatives...");
|
|
|
64 |
List<PreservationAlternative> alternatives = new ArrayList<>();
|
|
|
65 |
List<FormatMigrationOption> alternativeFormats = getAlternativeFormats(originalModelFacade, individualAtRisk);
|
|
|
66 |
AlternativesBuilder alternativesBuilder = new AlternativesBuilder(originalModelFacade);
|
|
|
67 |
ChangesOntologyBuilder changesOntologyBuilder = new ChangesOntologyBuilder(originalModelFacade.getOntology(), originalModelFacade.getIndividual(individualAtRisk.toString()), "FileReplacement");
|
|
|
68 |
for (int i = 0; i < alternativeFormats.size(); ++i) {
|
|
|
69 |
String formatName = alternativeFormats.get(i).getFormat();
|
|
|
70 |
String toolName = alternativeFormats.get(i).getTool();
|
|
|
71 |
System.out.println(String.format("Replacing %s with %s using %s.", individualAtRisk, formatName, toolName));
|
|
|
72 |
String alternativeOntologyIri = createIriFragment(baseURL, identifier, identifiedRisk, i);
|
|
|
73 |
OwlApiFacade modifiedModel = new OwlApiFacade(alternativesBuilder.copyOntology(IRI.create(alternativeOntologyIri, originalModelFacade.getOntology().getOntologyID().getOntologyIRI().getFragment())));
|
|
|
74 |
|
|
|
75 |
OWLNamedIndividual individual = modifiedModel.addIndividual(formatName + "_file", IRI_FILE.toString());
|
|
|
76 |
OWLNamedIndividual objectCharacteristics = modifiedModel.addIndividual(formatName + "Characteristics", IRI_OBJECT_CHARACTERISTICS.toString());
|
|
|
77 |
modifiedModel.addObjectProperty(individual, IRI_OBJECT_CHARACTERISTICS_PROPERTY.toString(), objectCharacteristics);
|
|
|
78 |
OWLNamedIndividual format = modifiedModel.addIndividual(formatName + "Format");
|
|
|
79 |
modifiedModel.addObjectProperty(objectCharacteristics, IRI_FORMAT_PROPERTY.toString(), format);
|
|
|
80 |
OWLNamedIndividual formatRegistry = modifiedModel.addIndividual(formatName + "FormatRegistry", IRI_FORMAT_REGISTRY.toString());
|
|
|
81 |
modifiedModel.addObjectProperty(format, IRI_FORMAT_REGISTRY_PROPERTY.toString(), formatRegistry);
|
|
|
82 |
modifiedModel.addDataProperty(formatRegistry, IRI_FORMAT_REGISTRY_NAME_PROPERTY.toString(), OWLManager.getOWLDataFactory().getOWLLiteral("http://www.nationalarchives.gov.uk/PRONOM/"));
|
|
|
83 |
List<String> pronom_ids = knowledgeBase.getFormatIDs(formatName);
|
|
|
84 |
for (String pronom_id : pronom_ids) {
|
|
|
85 |
modifiedModel.addDataProperty(formatRegistry, IRI_FORMAT_REGISTRY_KEY_PROPERTY.toString(), OWLManager.getOWLDataFactory().getOWLLiteral(pronom_id));
|
|
|
86 |
}
|
|
|
87 |
if (pronom_ids.isEmpty()) {
|
|
|
88 |
modifiedModel.addDataProperty(formatRegistry, IRI_FORMAT_REGISTRY_KEY_PROPERTY.toString(), OWLManager.getOWLDataFactory().getOWLLiteral("fmt/null"));
|
|
|
89 |
}
|
|
|
90 |
// TODO: copy relations
|
|
|
91 |
// TODO: remove original individual
|
|
|
92 |
|
|
|
93 |
// TODO: add converting tool to the changes ontology!
|
|
|
94 |
OWLOntology eventsOntology = changesOntologyBuilder.createChangesOntology(identifier + "/" + i, modifiedModel.getOntology(), individual);
|
|
|
95 |
alternatives.add(new PreservationAlternative(
|
|
|
96 |
originalModelFacade.toOwlXml(), modifiedModel.toOwlXml(),
|
|
|
97 |
OwlApiFacade.toOwlXml(eventsOntology), identifiedRisk.getRiskIdentifier(), new ArrayList<String>()));
|
|
|
98 |
|
|
|
99 |
}
|
|
|
100 |
// TODO: avoid duplicate filecharacteristics and registries somehow
|
|
|
101 |
return alternatives;
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
private List<FormatMigrationOption> getAlternativeFormats(OwlApiFacade originalModelFacade, IRI fileAtRisk) throws OWLOntologyStorageException, OWLOntologyCreationException, IOException {
|
|
|
105 |
JenaQueryFacade queryFacade = new JenaQueryFacade(originalModelFacade.getJenaModel(false));
|
|
|
106 |
List<String> pronomIDs = JenaQueryFacade.extractColumn(queryFacade.query(QueryHelper.getQueryGetFormatID(fileAtRisk.toString())), "pronom_id");
|
|
|
107 |
List<FormatMigrationOption> alternativeTools = knowledgeBase.getConvertToMigrationOptions(pronomIDs.get(0)); // TODO: also get ConvertFromMigrationOptions!
|
|
|
108 |
System.out.println("Alternative options found: " + alternativeTools);
|
|
|
109 |
return alternativeTools;
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
private String createIriFragment(String baseURL, String identifier, Risk identifiedRisk, int count) {
|
|
|
113 |
return baseURL + "/" + identifier + "/" + identifiedRisk.getRiskIdentifier() + "/" + count + "/";
|
|
|
114 |
}
|
|
|
115 |
}
|