/**
* 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();
}
}