Parent: [87ad8f] (diff)

Child: [b89528] (diff)

Download this file

PreservationIdentifierClient.java    144 lines (124 with data), 8.6 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
/**
* 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;
import org.apache.commons.cli.*;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.io.IRIDocumentSource;
import org.semanticweb.owlapi.io.OWLXMLOntologyFormat;
import org.semanticweb.owlapi.io.StringDocumentSource;
import org.semanticweb.owlapi.io.StringDocumentTarget;
import org.semanticweb.owlapi.model.*;
import org.semanticweb.owlapi.model.OWLOntologyStorageException;
import javax.xml.ws.BindingProvider;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* @author Rudolf Mayer
*/
public class PreservationIdentifierClient {
public static final String DEFAULT_ONTOLOGY = "http://timbus.teco.edu/ontologies/examples/MusicClassification/MusicClassificationDSOs_withHtmlFormat.owl";
public static final String DEFAULT_SERVER_ADDRESS = "http://testbed.timbusproject.net:7080/pi/preservationIdentifier?wsdl";
public static final String DEFAULT_ALTERNATIVE_ONTOLOGIES_BASE_URL = "http://testbed.timbusproject.net/test";
public static void main(String[] args) throws OwlElementNotFoundException_Exception, org.semanticweb.owlapi.model.OWLOntologyCreationException, IOException_Exception, OWLOntologyStorageException_Exception, OWLOntologyCreationException_Exception, OWLOntologyStorageException {
String ontology;
String baseurl;
String serverAddress;
// CLI parsing
Options options = new Options();
Option[] allOptions = new Option[] {
new Option("o", "ontology", true, "The ontology to use. Defaults to " + DEFAULT_ONTOLOGY),
new Option("b", "baseurl", true, "The baseurl to use for alternative ontologies. Defaults to " + DEFAULT_ALTERNATIVE_ONTOLOGIES_BASE_URL),
new Option("a", "address", true, "The address of the server. Defaults to " + DEFAULT_SERVER_ADDRESS),
};
for (Option option : allOptions) {
options.addOption(option);
}
CommandLineParser parser = new BasicParser();
try {
CommandLine cmd = parser.parse(options, args);
// has the buildfile argument been passed?
ontology = cmd.hasOption("ontology") ? cmd.getOptionValue("ontology") : DEFAULT_ONTOLOGY;
baseurl = cmd.hasOption("baseurl") ? cmd.getOptionValue("baseurl") : DEFAULT_ALTERNATIVE_ONTOLOGIES_BASE_URL;
serverAddress = cmd.hasOption("address") ? cmd.getOptionValue("address") : DEFAULT_SERVER_ADDRESS;
} catch (ParseException e) {
System.err.println("CLI parsing failed. Reason: " + e.getMessage());
// automatically generate the help statement
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp("ant", options);
return;
}
System.out.println(String.format("Using: \nontology: %s\nbaseurl: %s\nserver: %s", ontology, baseurl, serverAddress));
System.out.println("Loading ontology...");
OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
OWLOntology originalContextModel = manager.loadOntologyFromOntologyDocument(new IRIDocumentSource(IRI.create(ontology)),
new OWLOntologyLoaderConfiguration().setMissingImportHandlingStrategy(MissingImportHandlingStrategy.THROW_EXCEPTION));
String identifier = RandomStringUtils.randomAlphanumeric(20);
String originalContextModelAsXML = ontologyToString(originalContextModel);
ArrayList<Risk> identifiedRisks = new ArrayList<Risk>();
identifiedRisks.add(createRisk("risk1", "Taverna")); // get fake alternatives
// identifiedRisks.add(createRisk("risk2", "InternetExplorer")); // get alternatives if using testdata/MusicClassificationDSOs_withHtmlFormat.owl
// identifiedRisks.add(createRisk("risk3", "HTML_File")); // get alternatives if using testdata/MusicClassificationDSOs_withHtmlFormat.owl
identifiedRisks.add(createRisk("risk4", "DoesNotExist")); // causes entry in the shortLog property
System.out.println("Retrieving alternatives...");
PreservationIdentifierService pi = new PreservationIdentifierServiceService().getPreservationIdentifierServicePort();
((BindingProvider)pi).getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, serverAddress);
PreservationAlternatives preservationAlternatives = pi.identifyPreservationAlternatives(
originalContextModelAsXML, identifiedRisks, baseurl, identifier);
System.out.println(String.format("Found %d alternatives...", preservationAlternatives.getPreservationAlternatives().size()));
System.out.println("Writing alternatives...");
int index = 1;
for (PreservationAlternative preservationAlternative : preservationAlternatives.getPreservationAlternatives()) {
String path = System.getProperty("java.io.tmpdir") + "/pi-" + preservationAlternative.getRiskIdentifier() + "-" + index + "/";
//noinspection ResultOfMethodCallIgnored
new File(path).mkdirs();
writeToFile(path + "original.owl", preservationAlternative.getOriginalContextModel());
writeToFile(path + "modified.owl", preservationAlternative.getModifiedContextModel());
writeToFile(path + "changes.owl", preservationAlternative.getChanges());
for (String importedOntology : preservationAlternative.getModifiedImportedOntologies()) {
OWLOntology imported = OWLManager.createOWLOntologyManager().loadOntologyFromOntologyDocument(new StringDocumentSource(importedOntology));
writeToFile(path + imported.getOntologyID().getOntologyIRI().getFragment(), importedOntology);
}
index++;
}
//System.out.println(String.format("Long log: \n%s", preservationAlternatives.getLongLog())); // detailed log
System.out.println(String.format("Short log: \n%s", preservationAlternatives.getShortLog()));
}
private static Risk createRisk(String identifier, String affectedResource) {
Risk identifiedRisk = new Risk();
identifiedRisk.setRiskIdentifier(identifier);
identifiedRisk.setAffectedResource(affectedResource);
return identifiedRisk;
}
public static void writeToFile(final String pathname, final String str) {
try {
FileUtils.write(new File(pathname), str);
System.out.println("Wrote ontology to " + pathname);
} catch (IOException e) {
e.printStackTrace();
}
}
public static String ontologyToString(OWLOntology ontology) throws org.semanticweb.owlapi.model.OWLOntologyStorageException {
StringDocumentTarget stringDocumentTarget = new StringDocumentTarget();
OWLManager.createOWLOntologyManager().saveOntology(ontology, new OWLXMLOntologyFormat(), stringDocumentTarget);
return stringDocumentTarget.toString();
}
}